Package org.moeaframework
Class Executor
java.lang.Object
org.moeaframework.Executor
Configures and executes algorithms while hiding the underlying boilerplate code needed to setup and safely execute
an algorithm. For example, the following demonstrates its typical use:
NondominatedPopulation result = new Executor() .withAlgorithm("NSGAII") .withProblem("DTLZ2_2") .withMaxEvaluations(10000) .run();
The problem and algorithm must be specified prior to calling run()
. Additional parameters for each
algorithm can be assigned using the withProperty
methods:
NondominatedPopulation result = new Executor() .withAlgorithm("NSGAII") .withProblem("DTLZ2_2") .withMaxEvaluations(10000) .withProperty("populationSize", 100) .withProperty("sbx.rate", 1.0) .withProperty("sbx.distributionIndex", 15.0) .withProperty("pm.rate", 0.05) .withProperty("pm.distributionIndex", 20.0) .run();
The evaluation of function evaluations can be distributed across multiple cores or computers by using
distributeOnAllCores()
, distributeOn(int)
, or distributeWith(ExecutorService)
.
Checkpoint files can be saved in order to resume interrupted runs using the withCheckpointFrequency(int)
and withCheckpointFile(File)
methods. For example:
NondominatedPopulation result = new Executor() .withAlgorithm("NSGAII") .withProblem("DTLZ2_2") .withMaxEvaluations(100000) .distributeOnAllCores() .withCheckpointFrequency(1000) .withCheckpointFile(new File("example.state")) .run();
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Informs this executor to stop processing and returns any results collected thus far.Enables this executor to save checkpoints after every iteration of the algorithm.Clears the properties.protected TerminationCondition
Returns the termination condition for this executor.distributeOn
(int numberOfThreads) Enables this executor to distribute solution evaluations across the specified number of threads.Enables this executor to distribute solution evaluations across all processors on the local host.distributeWith
(ExecutorService executorService) Sets theExecutorService
used by this executor to distribute solution evaluations.protected Problem
When configured to run on multiple threads or with an , will wrap the problem instance in a distributed problem.Returns the instrumenter used by this executor; ornull
if no instrumenter has been assigned.boolean
Returnstrue
if the canceled flag is set;false
otherwise.removeProperty
(String key) Unsets a property.Deletes the checkpoint file if it exists.run()
Runs this executor with its configured settings.runSeeds
(int numberOfSeeds) Runs this executor with its configured settings multiple times, returning the individual end-of-run approximation sets.protected NondominatedPopulation
runSingleSeed
(int seed, int numberOfSeeds) Runs this executor with its configured settings.usingAlgorithmFactory
(AlgorithmFactory algorithmFactory) Sets the algorithm factory used by this executor.usingProblemFactory
(ProblemFactory problemFactory) withAlgorithm
(String algorithmName) Sets the algorithm used by this executor.withCheckpointFile
(File checkpointFile) Sets the checkpoint file where the algorithm state is stored.withCheckpointFrequency
(int checkpointFrequency) Sets the frequency at which this executor saves checkpoints.withEpsilon
(double... epsilon) Sets the ε values; equivalent to setting the propertyepsilon
.withInstrumenter
(Instrumenter instrumenter) Sets the instrumenter used to record information about the runtime behavior of algorithms executed by this executor.withMaxEvaluations
(int maxEvaluations) Sets the maximum number of evaluations; equivalent to setting the propertymaxEvaluations
.withMaxTime
(long maxTime) Sets the maximum elapsed time in milliseconds; equivalent to setting the propertymaxTime
.withProblem
(String problemName) withProblem
(Problem problemInstance) withProblemClass
(Class<?> problemClass, Object... problemArguments) withProblemClass
(String problemClassName, Object... problemArguments) withProgressListener
(ProgressListener listener) Adds the given progress listener to receive periodic progress reports.withProperties
(TypedProperties properties) Sets all properties.withProperty
(String key, boolean value) Sets aboolean
property.withProperty
(String key, byte value) Sets abyte
property.withProperty
(String key, byte[] values) Sets abyte
array property.withProperty
(String key, double value) Sets adouble
property.withProperty
(String key, double[] values) Sets adouble
array property.withProperty
(String key, float value) Sets afloat
property.withProperty
(String key, float[] values) Sets afloat
array property.withProperty
(String key, int value) Sets anint
property.withProperty
(String key, int[] values) Sets anint
array property.withProperty
(String key, long value) Sets along
property.withProperty
(String key, long[] values) Sets along
array property.withProperty
(String key, short value) Sets ashort
property.withProperty
(String key, short[] values) Sets ashort
array property.withProperty
(String key, String value) Sets aString
property.withProperty
(String key, String[] values) Sets aString
array property.withSameProblemAs
(org.moeaframework.ProblemBuilder builder) withTerminationCondition
(TerminationCondition condition) Adds a custom termination condition for use with this executor.
-
Constructor Details
-
Executor
public Executor()Constructs a new executor initialized with default settings.
-
-
Method Details
-
cancel
public void cancel()Informs this executor to stop processing and returns any results collected thus far. This method is thread-safe. -
isCanceled
public boolean isCanceled()Returnstrue
if the canceled flag is set;false
otherwise. After canceling a run, the flag will remain set totrue
until another run is started. This method is thread-safe.- Returns:
true
if the canceled flag is set;false
otherwise
-
withInstrumenter
Sets the instrumenter used to record information about the runtime behavior of algorithms executed by this executor.- Parameters:
instrumenter
- the instrumeter- Returns:
- a reference to this executor
-
getInstrumenter
Returns the instrumenter used by this executor; ornull
if no instrumenter has been assigned.- Returns:
- the instrumenter used by this executor; or
null
if no instrumenter has been assigned
-
usingAlgorithmFactory
Sets the algorithm factory used by this executor.- Parameters:
algorithmFactory
- the algorithm factory- Returns:
- a reference to this executor
-
withSameProblemAs
-
usingProblemFactory
-
withProblem
-
withProblem
-
withProblemClass
-
withProblemClass
public Executor withProblemClass(String problemClassName, Object... problemArguments) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
withTerminationCondition
Adds a custom termination condition for use with this executor. UsewithMaxEvaluations(int)
andwithMaxTime(long)
to set NFE or time limits.- Parameters:
condition
- the termination condition to add- Returns:
- a reference to this executor
-
withAlgorithm
Sets the algorithm used by this executor.- Parameters:
algorithmName
- the algorithm name- Returns:
- a reference to this executor
-
distributeWith
Sets theExecutorService
used by this executor to distribute solution evaluations. The caller is responsible for ensuring the executor service is shutdown after use.- Parameters:
executorService
- the executor service- Returns:
- a reference to this executor
-
distributeOn
Enables this executor to distribute solution evaluations across the specified number of threads.- Parameters:
numberOfThreads
- the number of threads- Returns:
- a reference to this executor
- Throws:
IllegalArgumentException
- ifnumberOfThreads <= 0
-
distributeOnAllCores
Enables this executor to distribute solution evaluations across all processors on the local host.- Returns:
- a reference to this executor
-
withCheckpointFile
Sets the checkpoint file where the algorithm state is stored. This method must be invoked in order to enable checkpoints.- Parameters:
checkpointFile
- the checkpoint file- Returns:
- a reference to this executor
-
withCheckpointFrequency
Sets the frequency at which this executor saves checkpoints.- Parameters:
checkpointFrequency
- the checkpoint frequency, specifying the number of evaluations between checkpoints- Returns:
- a reference to this executor
-
checkpointEveryIteration
Enables this executor to save checkpoints after every iteration of the algorithm.- Returns:
- a reference to this executor
-
resetCheckpointFile
Deletes the checkpoint file if it exists.- Returns:
- a reference to this executor
- Throws:
IOException
- if the checkpoint file could not be deleted
-
withEpsilon
Sets the ε values; equivalent to setting the propertyepsilon
.- Parameters:
epsilon
- the ε values- Returns:
- a reference to this executor
-
withMaxEvaluations
Sets the maximum number of evaluations; equivalent to setting the propertymaxEvaluations
.- Parameters:
maxEvaluations
- the maximum number of evaluations- Returns:
- a reference to this executor
-
withMaxTime
Sets the maximum elapsed time in milliseconds; equivalent to setting the propertymaxTime
.- Parameters:
maxTime
- the maximum elapsed time in milliseconds- Returns:
- a reference to this executor
-
removeProperty
Unsets a property.- Parameters:
key
- the property key- Returns:
- a reference to this executor
-
withProperty
Sets aString
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets afloat
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets adouble
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets abyte
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets ashort
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets anint
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets along
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets aboolean
property.- Parameters:
key
- the property keyvalue
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets aString
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets afloat
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets adouble
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets abyte
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets ashort
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets anint
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
withProperty
Sets along
array property.- Parameters:
key
- the property keyvalues
- the property value- Returns:
- a reference to this executor
-
clearProperties
Clears the properties.- Returns:
- a reference to this executor
-
withProperties
Sets all properties. This will clear any existing properties, including themaxEvaluations
ormaxTime
.- Parameters:
properties
- the properties- Returns:
- a reference to this executor
-
withProgressListener
Adds the given progress listener to receive periodic progress reports.- Parameters:
listener
- the progress listener to add- Returns:
- a reference to this executor
-
createTerminationCondition
Returns the termination condition for this executor.- Returns:
- the termination condition
-
runSeeds
Runs this executor with its configured settings multiple times, returning the individual end-of-run approximation sets. If the run is canceled, the list contains any complete seeds that finished prior to cancellation.- Parameters:
numberOfSeeds
- the number of seeds to run- Returns:
- the individual end-of-run approximation sets
-
run
Runs this executor with its configured settings.- Returns:
- the end-of-run approximation set; or
null
if canceled
-
getDistributedProblemInstance
When configured to run on multiple threads or with an , will wrap the problem instance in a distributed problem.- Returns:
- the wrapped problem instance
-
runSingleSeed
Runs this executor with its configured settings.- Parameters:
seed
- the current seed being run, such that1 <= seed <= numberOfSeeds
numberOfSeeds
- to total number of seeds being run- Returns:
- the end-of-run approximation set; or
null
if canceled
-