All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.uam.eps.ir.relison.diffusion.metrics.SimulationMetric Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2020 Information Retrieval Group at Universidad Autónoma
 * de Madrid, http://ir.ii.uam.es
 * 
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package es.uam.eps.ir.relison.diffusion.metrics;

import es.uam.eps.ir.relison.diffusion.data.Data;
import es.uam.eps.ir.relison.diffusion.simulation.Iteration;
import es.uam.eps.ir.relison.diffusion.simulation.Simulation;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Interface for the different metrics to apply over the simulation.
 *
 * @author Javier Sanz-Cruzado ([email protected])
 * @author Pablo Castells ([email protected])
 *
 * @param  Type of the users.
 * @param  Type of the information pieces.
 * @param  Type of the features.
 */
public interface SimulationMetric
{    
    /**
     * Obtains the name of the metric.
     * @return the name of the metric.
     */
    String getName();

    /**
     * Indicates if the metric has been initialized or not.
     * @return true if it has been initialized, false if it has not.
     */
    boolean isInitialized();

    /**
     * Calculates the metric for the current state of the simulation.
     * @return the value of the metric for the current state of the simulation
     */
    double calculate();

    /**
     * Calculates the metric for each iteration of a simulation.
     * @param data the data.
     * @param simulation the whole simulation.
     * @return the value of the metric for each iteration.
     */
    default List calculate(Data data, Simulation simulation)
    {
        if(simulation == null || data == null)
            return new ArrayList<>();

        this.clear();
        this.initialize(data);
        int numIter = simulation.getNumIterations();
        List values = new ArrayList<>();
        for(int i = 0; i < numIter; ++i)
        {
            this.update(simulation.getIteration(i));
            values.add(this.calculate());
        }
        return values;
    }

    /**
     * Updates the different values which are necessary for computing a metric, given
     * the information received by users in an iteration of the simulation.
     * @param iteration the new iteration.
     */
    void update(Iteration iteration);

    /**
     * Resets the metric.
     */
    void clear();

    /**
     * Initializes the metric.
     * @param data the data.
     */
    void initialize(Data data);

    /**
     * Initializes and establishes a given state to the metric from a simulation backup.
     * @param data the data.
     * @param backupSim the simulation backup.
     */
    default void initialize(Data data, Simulation backupSim)
    {
        this.initialize(data);
        for(int i = 0; i < backupSim.getNumIterations(); ++i)
        {
            this.update(backupSim.getIteration(i));
        }
    }
    
}