Package libai.ants.algorithms
Class Metaheuristic
- java.lang.Object
-
- libai.ants.algorithms.Metaheuristic
-
- All Implemented Interfaces:
java.util.Comparator<Ant>
- Direct Known Subclasses:
AntColonySystem
,AntSystem
,MMAS
public abstract class Metaheuristic extends java.lang.Object implements java.util.Comparator<Ant>
This is the core class of the Ant Framework. It defines the basic operations that any ACO algorithm must implement. This is an abstract class that must be extended by the class that implements a particular ACO algorithm for instance: AntSystem, AntColonySystem, MIX-MAX ACO, ...This class is composed of an instance of the Enviroment class, which holds the necesary information to solve a given optimization problem
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
alpha
Determine the relevance of the pheromone trail tau_ij when an Ant decides the next node to be incorporated in a solution.protected Ant[]
Ants
Enviroment's ant array local copyprotected java.util.List<java.lang.Integer>
bestSolution
Vector that holds the global best solution foundprotected static int
beta
Determine the relevance of the heuristic information n_ij when an Ant decides the next node to be incorporated in a solution.protected java.util.HashMap<java.lang.Integer,java.util.List<Node>>
candidates
Vector that holds the candidate listprotected int
currentIterationNumber
current iteration numberprotected static int
destinationNode
destination node of the searchprotected Enviroment
E
Instance of an Enviroment which holds all of the necesary information to solve an optimizacion problemprotected Graph
Graph
Enviroment's graph local copyprotected static int
initialNode
initial node of the searchprotected static int
maxNumIterations
the maximum number of iterationsprotected int
numberOfAnts
Enviroment's number of ants local copyprotected int
numberOfNodes
Enviroment's graph number of nodeprotected java.util.HashMap<java.lang.Integer,java.lang.Double>
Parameters
Hashtable that holds the parametersprotected Matrix
Pheromones
Enviroment's pheromones matrix local copy
-
Constructor Summary
Constructors Modifier Constructor Description protected
Metaheuristic()
Constructor.protected
Metaheuristic(Enviroment E)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
candidateList(int max)
Generate, per node in the graph, a list of candidates nodes according to some problem specific heuristic.abstract void
checkParameters()
Checks whether or not all of the algorithm's parameters exists.int
compare(Ant o1, Ant o2)
Compares to ants' solutions based on the quality metric of f.abstract java.util.List<java.lang.Integer>
constrains(int i, java.util.List<java.lang.Integer> currentSolution)
Given a node in the graph and an ant's current solution, returns a list of possible nodes to visit.abstract void
daemonActions()
This function is called on each iteration and it provides a way to implement centralized actionsabstract int
decisionRule(int i, java.util.List<java.lang.Integer> currentSolution)
Used by ants to decided the next node to visit.double
f(java.util.List<java.lang.Integer> solution)
This function determines the lenght of a solutionjava.util.List<java.lang.Integer>
getBestSolution()
Returns best solutionint
getCurrentIterationNumber()
Returns current iteration numberEnviroment
getE()
Returns the enviromentint
getNumberOfNodes()
Gets the number of nodes of the problem graphdouble
getParam(int key)
abstract double
heuristicInfo(double number)
Calculates the heuristic information.abstract void
pheromonesEvaporation()
Evaporates the pheromone trail contained in the enviroment E according to some ACO algorithm specific logicabstract void
pheromonesUpdate()
Updates the pheromone trail contained in the enviroment E according to some ACO algorithm specific logicprotected void
setE(Enviroment E)
Sets the enviromentvoid
setNumberOfNodes(int numberOfNodes)
Sets the number of nodes of the graph problemprotected void
setParam(int key, double param)
Sets a parameterabstract void
solve()
This is the body of the algorithm.
-
-
-
Field Detail
-
initialNode
protected static final int initialNode
initial node of the search- See Also:
- Constant Field Values
-
destinationNode
protected static final int destinationNode
destination node of the search- See Also:
- Constant Field Values
-
maxNumIterations
protected static final int maxNumIterations
the maximum number of iterations- See Also:
- Constant Field Values
-
alpha
protected static final int alpha
Determine the relevance of the pheromone trail tau_ij when an Ant decides the next node to be incorporated in a solution. Used in thedecisionRule()
method- See Also:
- Constant Field Values
-
beta
protected static final int beta
Determine the relevance of the heuristic information n_ij when an Ant decides the next node to be incorporated in a solution. Used in thedecisionRule()
method- See Also:
- Constant Field Values
-
E
protected Enviroment E
Instance of an Enviroment which holds all of the necesary information to solve an optimizacion problem
-
Parameters
protected java.util.HashMap<java.lang.Integer,java.lang.Double> Parameters
Hashtable that holds the parameters
-
bestSolution
protected java.util.List<java.lang.Integer> bestSolution
Vector that holds the global best solution found
-
candidates
protected java.util.HashMap<java.lang.Integer,java.util.List<Node>> candidates
Vector that holds the candidate list
-
currentIterationNumber
protected int currentIterationNumber
current iteration number
-
Pheromones
protected Matrix Pheromones
Enviroment's pheromones matrix local copy
-
numberOfAnts
protected int numberOfAnts
Enviroment's number of ants local copy
-
Ants
protected Ant[] Ants
Enviroment's ant array local copy
-
Graph
protected Graph Graph
Enviroment's graph local copy
-
numberOfNodes
protected int numberOfNodes
Enviroment's graph number of node
-
-
Constructor Detail
-
Metaheuristic
protected Metaheuristic(Enviroment E)
Constructor. Allocates the enviroment.- Parameters:
E
- enviroment
-
Metaheuristic
protected Metaheuristic()
Constructor. Empty constructor.
-
-
Method Detail
-
getE
public Enviroment getE()
Returns the enviroment- Returns:
- the enviroment.
-
setE
protected void setE(Enviroment E)
Sets the enviroment- Parameters:
E
- the enviroment.
-
getNumberOfNodes
public int getNumberOfNodes()
Gets the number of nodes of the problem graph- Returns:
- number of nodes of the problem graph
-
setNumberOfNodes
public void setNumberOfNodes(int numberOfNodes)
Sets the number of nodes of the graph problem- Parameters:
numberOfNodes
-numberOfNodes
-
setParam
protected void setParam(int key, double param)
Sets a parameter- Parameters:
key
- of the parameterparam
- the value of the parameter
-
getParam
public double getParam(int key)
-
pheromonesUpdate
public abstract void pheromonesUpdate()
Updates the pheromone trail contained in the enviroment E according to some ACO algorithm specific logic
-
pheromonesEvaporation
public abstract void pheromonesEvaporation()
Evaporates the pheromone trail contained in the enviroment E according to some ACO algorithm specific logic
-
decisionRule
public abstract int decisionRule(int i, java.util.List<java.lang.Integer> currentSolution)
Used by ants to decided the next node to visit.- Parameters:
i
- source nodecurrentSolution
-currentSolution
- Returns:
- destination node
-
solve
public abstract void solve() throws AntFrameworkException
This is the body of the algorithm. Implements the fundamental logic and calls to other functions: pheromonesUpdate,pheromonesEvaporation.. according to some ACO algorithm specific logic- Throws:
AntFrameworkException
- AntFrameworkException
-
checkParameters
public abstract void checkParameters() throws java.lang.Exception
Checks whether or not all of the algorithm's parameters exists. If some obligatory parameter do not exist, the function throws an exception. If some other parameter do not exists but it is possible to set a default value, here is the place to do it.- Throws:
java.lang.Exception
- Exception
-
candidateList
public abstract void candidateList(int max)
Generate, per node in the graph, a list of candidates nodes according to some problem specific heuristic. The candidates are stored in this class's candidates hashtable- Parameters:
max
- maximum number of candidates allowed
-
constrains
public abstract java.util.List<java.lang.Integer> constrains(int i, java.util.List<java.lang.Integer> currentSolution)
Given a node in the graph and an ant's current solution, returns a list of possible nodes to visit. This function must be implemented on the problem level, and usually obeys some problem related restricctions For instance, in the case of the TSP, this function must check which nodes have been visited and return only those wich have not been visited. For the case of the short route, this function returns all adjacents nodes to node i.- Parameters:
i
- current nodecurrentSolution
- ant's current solutin- Returns:
- list of possible nodes to visit
-
daemonActions
public abstract void daemonActions()
This function is called on each iteration and it provides a way to implement centralized actions
-
heuristicInfo
public abstract double heuristicInfo(double number)
Calculates the heuristic information. Must be implement on the problem level- Parameters:
number
- a number being considered- Returns:
- heuristic information
-
getBestSolution
public java.util.List<java.lang.Integer> getBestSolution()
Returns best solution- Returns:
- best solution
-
getCurrentIterationNumber
public int getCurrentIterationNumber()
Returns current iteration number- Returns:
- current iteration number
-
f
public double f(java.util.List<java.lang.Integer> solution)
This function determines the lenght of a solution- Parameters:
solution
- a path constructed by an ant- Returns:
- lenght of a path
-
compare
public int compare(Ant o1, Ant o2)
Compares to ants' solutions based on the quality metric of f.- Specified by:
compare
in interfacejava.util.Comparator<Ant>
- Parameters:
o1
- ant 1o2
- ant 2- Returns:
- 0 if solution of ant_1 = solution of ant_2, 1 if solution of ant_1 greater than solution of ant_2 and -1 otherwise
-
-