net.sourceforge.cilib.algorithm
Class Algorithm

java.lang.Object
  extended by net.sourceforge.cilib.algorithm.Algorithm
All Implemented Interfaces:
Serializable, Runnable, Cloneable
Direct Known Subclasses:
MultistartOptimisationAlgorithm, PopulationBasedAlgorithm, SingularAlgorithm

public abstract class Algorithm
extends Object
implements Cloneable, Runnable

All algorithms in CIlib should be subclasses of Algorithm. This class handles stopping criteria, events, threading and measurements. Subclasses of Algorithm must provide an implementation for protected abstract void performIteration(). If a subclass overrides initialise() then it must call super.initialise(). Failure to do so will cause an InitialisationException to be thrown when run() is called.

Author:
Edwin Peer
See Also:
Serialized Form

Field Summary
protected  OptimisationProblem optimisationProblem
           
 
Constructor Summary
protected Algorithm()
          Default constructor for Algorithm classes.
protected Algorithm(Algorithm copy)
          Copy constructor.
 
Method Summary
 void addAlgorithmListener(AlgorithmListener listener)
          Adds an algorithm event listener.
 void addStoppingCondition(StoppingCondition stoppingCondition)
          Adds a stopping condition.
protected abstract  void algorithmIteration()
          The actual operations that the current Algorithm performs within a single iteration.
static Algorithm get()
          Accessor for the top-level currently executing algorithm running in the current thread.
static List<Algorithm> getAlgorithmList()
          Static accessor to allow the current level of algorithm composition to be returned.
abstract  OptimisationSolution getBestSolution()
          Get the best current solution.
abstract  Algorithm getClone()
          Create a cloned copy of the current object and return it.
 int getIterations()
          Returns the number of iterations that have been performed by the algorihtm.
 OptimisationProblem getOptimisationProblem()
          Get the specified OptimisationProblem.
 double getPercentageComplete()
          Returns the percentage the algorithm is from completed (as a fraction).
abstract  List<OptimisationSolution> getSolutions()
          Get the collection of best solutions.
 List<StoppingCondition> getStoppingConditions()
          Get the current list of StoppingCondition instances that are associated with the current Algorithm.
 void initialise()
          Initialises the algorithm.
 boolean isFinished()
          Returns true if the algorithm has finished executing.
 void performInitialisation()
          Perform the needed initialisation required before the execution of the algorithm starts.
 void performIteration()
          Perform the actions of the current Algorithm for a single iteration.
 void performUninitialisation()
          Perform the needed unintialisation steps after the algorithm completes it's execution.
 void removeAlgorithmListener(AlgorithmListener listener)
          Removes an algorithm event listener.
 void removeStoppingCondition(StoppingCondition stoppingCondition)
          Removes a stopping condition.
 void reset()
          Reset the Algorithm internals if needed.
 void run()
          Executes the algorithm.
 void setOptimisationProblem(OptimisationProblem problem)
          Set the optimisation problem to be solved.
 void terminate()
          Terminates the algorithm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

optimisationProblem

protected OptimisationProblem optimisationProblem
Constructor Detail

Algorithm

protected Algorithm()
Default constructor for Algorithm classes. Sets up the correct state for the instance and initialises the needed containers needed for the different AlgorithmEvents that are generated.


Algorithm

protected Algorithm(Algorithm copy)
Copy constructor. Create a deep copy of the provided instance and return it.

Parameters:
copy - The instance to copy.
Method Detail

getClone

public abstract Algorithm getClone()
Create a cloned copy of the current object and return it. In general the created copy will be a deep copy of the provided instance. As a result this operation an be quite expensive if used incorrectly.

Specified by:
getClone in interface Cloneable
Returns:
An exact clone of the current object instance.
See Also:
Object.clone()

reset

public void reset()
Reset the Algorithm internals if needed.


initialise

public final void initialise()
Initialises the algorithm. Must be called before run() is called.


performIteration

public final void performIteration()
Perform the actions of the current Algorithm for a single iteration. This method calls algorithmIteration() after it performs some internal tasks by maintaining the stack of the currently executing algorithm instances.


algorithmIteration

protected abstract void algorithmIteration()
The actual operations that the current Algorithm performs within a single iteration.


performInitialisation

public void performInitialisation()
Perform the needed initialisation required before the execution of the algorithm starts.


performUninitialisation

public void performUninitialisation()
Perform the needed unintialisation steps after the algorithm completes it's execution.


run

public void run()
Executes the algorithm.

Specified by:
run in interface Runnable
Throws:
InitialisationException - algorithm was not properly initialised.

addStoppingCondition

public final void addStoppingCondition(StoppingCondition stoppingCondition)
Adds a stopping condition.

Parameters:
stoppingCondition - A StoppingCondition to be added.

removeStoppingCondition

public final void removeStoppingCondition(StoppingCondition stoppingCondition)
Removes a stopping condition.

Parameters:
stoppingCondition - The StoppingCondition to be removed.

addAlgorithmListener

public final void addAlgorithmListener(AlgorithmListener listener)
Adds an algorithm event listener. Event listeners are notified at various stages during the execution of an algorithm.

Parameters:
listener - An AlgorithmListener to be added.

removeAlgorithmListener

public final void removeAlgorithmListener(AlgorithmListener listener)
Removes an algorithm event listener.

Parameters:
listener - The AlgorithmListener to be removed.

getIterations

public final int getIterations()
Returns the number of iterations that have been performed by the algorihtm.

Returns:
The number of iterations.

getPercentageComplete

public final double getPercentageComplete()
Returns the percentage the algorithm is from completed (as a fraction). The percentage complete is calculated based on the stopping condition that is closest to finished.

Returns:
The percentage complete as a fraction.

isFinished

public final boolean isFinished()
Returns true if the algorithm has finished executing.

Returns:
true if the algorithm is finished

terminate

public final void terminate()
Terminates the algorithm.


get

public static Algorithm get()
Accessor for the top-level currently executing algorithm running in the current thread.

Returns:
the instance of the algorithm that is running in the current thread.

getAlgorithmList

public static List<Algorithm> getAlgorithmList()
Static accessor to allow the current level of algorithm composition to be returned.

Returns:
An unmodifiable list of algorithms.
See Also:
AlgorithmStack.asList()

getStoppingConditions

public List<StoppingCondition> getStoppingConditions()
Get the current list of StoppingCondition instances that are associated with the current Algorithm.

Returns:
The list of StoppingCondition instances associated with the current Algorithm.

setOptimisationProblem

public void setOptimisationProblem(OptimisationProblem problem)
Set the optimisation problem to be solved. By default, the problem is null. That is, it is necessary to set the optimisation problem before calling initialise().

Parameters:
problem - An implementation of the OptimisationProblemAdapter interface.

getOptimisationProblem

public OptimisationProblem getOptimisationProblem()
Get the specified OptimisationProblem.

Returns:
The specified OptimisationProblem.

getBestSolution

public abstract OptimisationSolution getBestSolution()
Get the best current solution. This best solution is determined from the personal bests of the particles.

Returns:
The OptimisationSolution representing the best solution.

getSolutions

public abstract List<OptimisationSolution> getSolutions()
Get the collection of best solutions. This result does not actually make sense in the normal PSO algorithm, but rather in a MultiObjective optimization.

Returns:
The Collection<OptimisationSolution> containing the solutions.


Copyright © 2009 CIRG. All Rights Reserved.