es.udc.gii.common.eaf.algorithm.parallel.evaluation
Class DistributedEvaluation

java.lang.Object
  extended by java.util.Observable
      extended by es.udc.gii.common.eaf.algorithm.parallel.evaluation.DistributedEvaluation
All Implemented Interfaces:
EvaluationStrategy, Configurable, java.io.Serializable

public class DistributedEvaluation
extends java.util.Observable
implements EvaluationStrategy, java.io.Serializable

This clas is an evaluation strategy for a distributed environment.

An instance of this class has an instance of the EvaluationTopology class which is in charge of the underlying communications between master and slaves. It also has an instance of a implementation of SerialEvaluationStrategy which asumes the real evaluation of the individuals.

Both masters and slaves perfom evaluation of individuals. That is, the master scatters the individuals and elavuates some individuals. The slaves perfom only evaluations.

Each master has two threads. One thread is responsible for evaluating a chunk of the population and the other thread exchanges chunks of individuals between the master and the slaves. When the evaluation thread has evaluated a chunk of individuals it tries to get another chunk and repeats its procedure. When a slave has finished evaluating a received chunk, it sends the chunk back to the master and waits for another. As soon as the master receives the evaluated chunk within its communication thread, this thread sends another free chunk to the slave who is waiting. The process goes on until no more individuals remain to be evaluated.

The master stops its execution when no more individuals are pending for evaluation, but the slaves wait until the finish() method is called by the master. Be sure to call this method in order to terminate the whole process appropiately in your algorithm.

Since:
1.0
Author:
Grupo Integrado de IngenierĂ­a (www.gii.udc.es)
See Also:
Serialized Form

Nested Class Summary
private  class DistributedEvaluation.CommunicationThread
           
 class DistributedEvaluation.DistributedEvaluationStatistics
           
 
Field Summary
private  java.util.concurrent.CyclicBarrier barrier
          Barrier for synchronizing the threads.
private  int[] chunkIndex
          Stores the first index of a chunk for each process.
private  int chunkSize
          The maximum amount of individuals sent each time to a node for evaluating them.
private  boolean collectStatistics
          true if statistics about the evaluations should be collected.
private  boolean commThreadMustWait
           
private  java.lang.Thread communicationThread
          The communication thread.
private  FitnessComparator comparator
           
private  java.util.List<Constraint> constraints
          the problem's constraints.
static int CURRENT_EVALUATION_ENDED
           
static int CURRENT_EVALUATION_NOT_STARTED
           
static int CURRENT_EVALUATION_STARTED
           
static int EVALUATED
           
private  int evaluatedIndividuals
          Number of evaluated individuals so far.
static int EVALUATION_NOT_INITIALIZED
           
private  SerialEvaluationStrategy evaluationStrategy
          Evaluation strategy that is in charge for the real evaluation of the individuals.
protected  boolean finished
          true if the evaluation process is finished.
private  int firtstInd
          Pointer to the first not evaluated individual.
private  java.util.List<ObjectiveFunction> functions
          The problem's objective functions.
private  java.util.List<Individual> individualsToEvaluate
          The individuals to be evaluated.
private static int NO_CHUNK
          Special value for process with no chunk in chunkIndex.
private  int popSize
          The amount of individuals for evaluation.
static int RECEIVED
           
static int SENT
           
private  int state
           
private  DistributedEvaluation.DistributedEvaluationStatistics statistics
          Some statistics: eval time, idle time, total evals, etc.
static int TO_EVALUATE
           
private  EvaluationTopology topology
          The master-slave topology defining the communication functions between master and slaves.
 
Constructor Summary
DistributedEvaluation()
          Creates a new instance of DistributedEvaluation
DistributedEvaluation(SerialEvaluationStrategy evaluationStrategy, EvaluationTopology topology, int chunkSize)
          Creates a new instance of DistributedEvaluation.
 
Method Summary
 void configure(org.apache.commons.configuration.Configuration conf)
          Configures this class.
 void evaluate(Individual individual, java.util.List<ObjectiveFunction> functions, java.util.List<Constraint> constraints)
          Evaluates an individual.
 void evaluate(java.util.List<Individual> individuals, java.util.List<ObjectiveFunction> functions, java.util.List<Constraint> constraints)
          Evaluates a list of individiduals.
private  void evaluationThread()
           
 void finish()
           
private  java.util.List<Individual> getChunk(int forRank)
          Retrives a chunk from the not evaluated part of the individual's list.
 int getChunkSize()
           
 FitnessComparator getComparator()
           
 SerialEvaluationStrategy getEvaluationStrategy()
           
 java.util.List<Individual> getIndividualsToEvaluate()
           
 int getState()
           
 DistributedEvaluation.DistributedEvaluationStatistics getStatistics()
           
 EvaluationTopology getTopology()
           
 boolean isCollectStatistics()
           
 boolean isFinished()
           
 boolean isMaster()
           
protected  void master(java.util.List<Individual> individuals, java.util.List<ObjectiveFunction> functions, java.util.List<Constraint> constraints)
           
 void notifyObservers(int state, java.lang.Object arg)
           
 void reset()
          Resets this object.
private  void setChunk(int fromRank, java.util.List<Individual> evaluated)
          Sets an evaluated chunk in the place where it belongs within the individuals list.
 void setChunkSize(int chunkSize)
           
 void setCollectStatistics(boolean collectStatistics)
           
 void setComparator(FitnessComparator comparator)
           
 void setEvaluationStrategy(SerialEvaluationStrategy evaluationStrategy)
           
protected  void setState(int state)
           
 void setTopology(EvaluationTopology topology)
           
protected  void slave(java.util.List<ObjectiveFunction> functions, java.util.List<Constraint> constraints)
           
private  void updateComparator(EvaluationObject eo)
           
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SENT

public static final int SENT
See Also:
Constant Field Values

RECEIVED

public static final int RECEIVED
See Also:
Constant Field Values

TO_EVALUATE

public static final int TO_EVALUATE
See Also:
Constant Field Values

EVALUATED

public static final int EVALUATED
See Also:
Constant Field Values

CURRENT_EVALUATION_ENDED

public static final int CURRENT_EVALUATION_ENDED
See Also:
Constant Field Values

CURRENT_EVALUATION_NOT_STARTED

public static final int CURRENT_EVALUATION_NOT_STARTED
See Also:
Constant Field Values

CURRENT_EVALUATION_STARTED

public static final int CURRENT_EVALUATION_STARTED
See Also:
Constant Field Values

EVALUATION_NOT_INITIALIZED

public static final int EVALUATION_NOT_INITIALIZED
See Also:
Constant Field Values

state

private int state

comparator

private FitnessComparator comparator

evaluationStrategy

private SerialEvaluationStrategy evaluationStrategy
Evaluation strategy that is in charge for the real evaluation of the individuals.


topology

private EvaluationTopology topology
The master-slave topology defining the communication functions between master and slaves.


finished

protected boolean finished
true if the evaluation process is finished.


chunkSize

private int chunkSize
The maximum amount of individuals sent each time to a node for evaluating them. The sent individuals might be fewer if there aren't enough available.


functions

private java.util.List<ObjectiveFunction> functions
The problem's objective functions.


constraints

private java.util.List<Constraint> constraints
the problem's constraints.


popSize

private int popSize
The amount of individuals for evaluation.


barrier

private java.util.concurrent.CyclicBarrier barrier
Barrier for synchronizing the threads.


collectStatistics

private boolean collectStatistics
true if statistics about the evaluations should be collected.


statistics

private DistributedEvaluation.DistributedEvaluationStatistics statistics
Some statistics: eval time, idle time, total evals, etc.


chunkIndex

private int[] chunkIndex
Stores the first index of a chunk for each process.


individualsToEvaluate

private java.util.List<Individual> individualsToEvaluate
The individuals to be evaluated.


firtstInd

private int firtstInd
Pointer to the first not evaluated individual.


NO_CHUNK

private static final int NO_CHUNK
Special value for process with no chunk in chunkIndex.

See Also:
Constant Field Values

evaluatedIndividuals

private int evaluatedIndividuals
Number of evaluated individuals so far.


communicationThread

private transient java.lang.Thread communicationThread
The communication thread.


commThreadMustWait

private transient boolean commThreadMustWait
Constructor Detail

DistributedEvaluation

public DistributedEvaluation()
Creates a new instance of DistributedEvaluation


DistributedEvaluation

public DistributedEvaluation(SerialEvaluationStrategy evaluationStrategy,
                             EvaluationTopology topology,
                             int chunkSize)
Creates a new instance of DistributedEvaluation.

The argument chunkSize is the amount of individuals sent each time to a node for evaluating them. If is set to 0, then a chunk is choosen so that: let S be the size of the population to be evaluated, let n be the number of proceses (master and slaves), then the chunk size will be S/n (integer division).

For instance: if there are a master, two slaves and chunkSize is 2, then the master tries to retain the first 2 individuals, tries to send the next 2 to the first slave and the next 2 to the second slave. After that, two threads of execution exist in the master: one for evaluating and one for comunication.

The evaluation thread tries to get new chunks and evaluates them until no more chunk can be retrived. The comunication thread listens for messages from slaves and sends them new chunks when slaves are ready until no more chunk might be retrived. There's no way to control which thread runs, the s.o. is responsible for doing that, and they interleave with each other.

Method Detail

notifyObservers

public void notifyObservers(int state,
                            java.lang.Object arg)

evaluationThread

private void evaluationThread()

getChunk

private java.util.List<Individual> getChunk(int forRank)
Retrives a chunk from the not evaluated part of the individual's list.

Parameters:
forRank - - rank of the process which is going to evaluate the chunk.
Returns:
An empty list if there is no more individual to evaluate or a list with individuals not evaluated yet. The size of the list is less or equal the chunk size.

setChunk

private void setChunk(int fromRank,
                      java.util.List<Individual> evaluated)
Sets an evaluated chunk in the place where it belongs within the individuals list.

Parameters:
fromRank - - rank of the process who has evaluated the chunk
evaluated - - the evaluated individuals

master

protected void master(java.util.List<Individual> individuals,
                      java.util.List<ObjectiveFunction> functions,
                      java.util.List<Constraint> constraints)

updateComparator

private void updateComparator(EvaluationObject eo)

slave

protected void slave(java.util.List<ObjectiveFunction> functions,
                     java.util.List<Constraint> constraints)

finish

public void finish()

configure

public void configure(org.apache.commons.configuration.Configuration conf)
Configures this class.

Configuration example:

    ...
    <EvaluationStrategy>
       <Class>es.udc.gii.common.eaf.algorithm.parallel.evaluation.DistributedEvaluation<Class>
       <ChunkSize>3</ChunkSize>
       <Races>2</Races>
       <CollectStatistics/>
    </EvaluationStrategy>     
    ...
 

This configures a master-slave model with 2 masters (races). The master sends 3 individuals each time a slave asks for individuals (chunk size). Statistics about the whole process are collected (CollectStatistics).

If ChunkSize is set to 0, then a chunk is choosen so that: let S be the size of the population to be evaluated, let n be the number of proceses (master and slaves), then the chunk size will be S/n (integer division).

Specified by:
configure in interface Configurable
Parameters:
conf - Configuration.
See Also:
Configuration

evaluate

public void evaluate(java.util.List<Individual> individuals,
                     java.util.List<ObjectiveFunction> functions,
                     java.util.List<Constraint> constraints)
Evaluates a list of individiduals.

Specified by:
evaluate in interface EvaluationStrategy
Parameters:
individuals - A list of individuals to be evaluated by the list of objective functions.
functions - An objective functions' list with wich we will evaluate the individuals' list.
constraints - A list of constraints with wich we will evaluate the individuals list.

evaluate

public void evaluate(Individual individual,
                     java.util.List<ObjectiveFunction> functions,
                     java.util.List<Constraint> constraints)
Evaluates an individual. This will be always done by a master.

Specified by:
evaluate in interface EvaluationStrategy
Parameters:
individual - An individual to be evaluated by the list of objective functions.
functions - An objective functions' list with wich we will evaluate the individual.
constraints - A list of constraints with wich we will evaluate the individual.

reset

public void reset()
Resets this object. After doing this, this object can be used again in a new evaluation process.


getChunkSize

public int getChunkSize()

setChunkSize

public void setChunkSize(int chunkSize)

getTopology

public EvaluationTopology getTopology()

setTopology

public void setTopology(EvaluationTopology topology)

getEvaluationStrategy

public SerialEvaluationStrategy getEvaluationStrategy()

setEvaluationStrategy

public void setEvaluationStrategy(SerialEvaluationStrategy evaluationStrategy)

isMaster

public boolean isMaster()

isFinished

public boolean isFinished()

getState

public int getState()

setState

protected void setState(int state)

getComparator

public FitnessComparator getComparator()

setComparator

public void setComparator(FitnessComparator comparator)

isCollectStatistics

public boolean isCollectStatistics()

setCollectStatistics

public void setCollectStatistics(boolean collectStatistics)

getStatistics

public DistributedEvaluation.DistributedEvaluationStatistics getStatistics()

getIndividualsToEvaluate

public java.util.List<Individual> getIndividualsToEvaluate()