Class 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 copy
      protected java.util.List<java.lang.Integer> bestSolution
      Vector that holds the global best solution found
      protected 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 list
      protected int currentIterationNumber
      current iteration number
      protected static int destinationNode
      destination node of the search
      protected Enviroment E
      Instance of an Enviroment which holds all of the necesary information to solve an optimizacion problem
      protected Graph Graph
      Enviroment's graph local copy
      protected static int initialNode
      initial node of the search
      protected static int maxNumIterations
      the maximum number of iterations
      protected int numberOfAnts
      Enviroment's number of ants local copy
      protected int numberOfNodes
      Enviroment's graph number of node
      protected java.util.HashMap<java.lang.Integer,java.lang.Double> Parameters
      Hashtable that holds the parameters
      protected Matrix Pheromones
      Enviroment's pheromones matrix local copy
    • 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 actions
      abstract 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 solution
      java.util.List<java.lang.Integer> getBestSolution()
      Returns best solution
      int getCurrentIterationNumber()
      Returns current iteration number
      Enviroment getE()
      Returns the enviroment
      int getNumberOfNodes()
      Gets the number of nodes of the problem graph
      double 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 logic
      abstract void pheromonesUpdate()
      Updates the pheromone trail contained in the enviroment E according to some ACO algorithm specific logic
      protected void setE​(Enviroment E)
      Sets the enviroment
      void setNumberOfNodes​(int numberOfNodes)
      Sets the number of nodes of the graph problem
      protected void setParam​(int key, double param)
      Sets a parameter
      abstract void solve()
      This is the body of the algorithm.
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 the decisionRule() 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 the decisionRule() 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 parameter
        param - 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 node
        currentSolution - 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 node
        currentSolution - 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 interface java.util.Comparator<Ant>
        Parameters:
        o1 - ant 1
        o2 - 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