Skip to content

Commit

Permalink
Add standard param files that define Ant System and Ant Colony System…
Browse files Browse the repository at this point in the history
… algorithms. Revised the knapsack and TSP examples to use these, making for pretty simple examples of using ACO.
  • Loading branch information
SigmaX committed Aug 28, 2019
1 parent 35ac88d commit 5046578
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ ecj/src/main/resources/ec/app/mona/1-MonaLisaOut.png
*.idx
*.log
*.out
*.toc
*.toc
*~
71 changes: 71 additions & 0 deletions ecj/src/main/java/ec/co/ant/ACSLocalUpdateRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2019 by Sean Luke
Licensed under the Academic Free License version 3.0
See the file "LICENSE" for more information
*/
package ec.co.ant;

import ec.EvolutionState;
import ec.Individual;
import ec.Subpopulation;
import ec.co.Component;
import ec.co.ConstructiveIndividual;
import ec.util.Parameter;
import java.util.Arrays;
import java.util.List;

/**
*
* @author Eric O. Scott
*/
public class ACSLocalUpdateRule implements UpdateRule {
final public static String P_RHO = "rho";
final public static String P_MINIMUM_PHEROMONE = "minimum-pheromone";

private double rho;
private double minimumPheromone;

public double getMinimumPheromone() { return minimumPheromone; }

public double getRho() { return rho; }

@Override
public void setup(final EvolutionState state, final Parameter base)
{
assert(state != null);
assert(base != null);
rho = state.parameters.getDouble(base.push(P_RHO), null, 0.0);
minimumPheromone = state.parameters.getDouble(base.push(P_RHO), null, 0.0);
assert(repOK());
}

@Override
public void updatePheromones(final EvolutionState state, final PheromoneTable pheromones, final List individuals) {
assert(pheromones != null);
assert(individuals != null);
assert(!individuals.isEmpty());

for (final Object ind : individuals)
{
for (final Object oo : (ConstructiveIndividual)ind)
{
assert(oo instanceof Component);
final Component c = (Component) oo;

final double oldPheromone = pheromones.get(state, c, 0); // Using thread 0 because we are in a single-threaded function
pheromones.set(c, (1-rho)*oldPheromone + rho*minimumPheromone);
}
}

assert(repOK());
}

public final boolean repOK()
{
return P_RHO != null
&& !P_RHO.isEmpty()
&& !Double.isInfinite(rho)
&& !Double.isNaN(rho)
&& rho >= 0.0;
}
}
48 changes: 48 additions & 0 deletions ecj/src/main/resources/ec/app/knapsack/acs.params
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2019 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

##############################
# Ant Colony System applied to TSP
##############################
parent.0 = ../../co/ant/acs.params

generations = 200

# Population size sets the number of ants per generation
pop.subpop.0.size = 20


##############################
# AntSpecies holds the construction and update rules that define ACO algorithms
##############################
pop.subpop.0.species.ind = ec.co.ConstructiveIndividual

# AS uses SimpleConstructionRule to implement psuedo-random proportionate selection
pop.subpop.0.species.construction-rule = ec.co.ant.SimpleConstructionRule
pop.subpop.0.species.construction-rule.component-selector = ec.co.ant.PseudorandomProportionateComponentSelector
pop.subpop.0.species.construction-rule.component-selector.prob-best = 0.5
pop.subpop.0.species.construction-rule.component-selector.alpha = 1.0
pop.subpop.0.species.construction-rule.component-selector.beta = 1.0

# Here we specify ACS's global rule for pheromone updates, using the global best ant
# to deposite pheromones at the generation boundary
pop.subpop.0.species.update-rule = ec.co.ant.GlobalUpdateRule
pop.subpop.0.species.update-rule.rho = 0.3
# You could switch this to ITERATION-BEST if you like
pop.subpop.0.species.update-rule.best-strategy = GLOBAL_BEST

# Here we add ACS's local update rule, which decayse pheromones visited by each ant as soon as it is evaluated (rather than waiting for the generation boundary)
pop.subpop.0.species.local-update-rule = ec.co.ant.ACSLocalUpdateRule
pop.subpop.0.species.local-update-rule.rho = 0.1
pop.subpop.0.species.local-update-rule.minimum-pheromone = 0.000001


##############################
# Problem
##############################
eval.problem = ec.app.knapsack.KnapsackProblem
# Example instance P01 from http://people.sc.fsu.edu/~jburkardt/datasets/knapsack_01/knapsack_01.html
eval.problem.knapsack-size = 165
eval.problem.sizes = 23 31 29 44 53 38 63 85 89 82
eval.problem.values = 92 57 49 68 60 43 67 84 87 72
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

parent.0 = ../../simple/simple.params
##############################
# Ant System applied to Knapsack
##############################

breed = ec.co.ConstructiveBreeder
parent.0 = ../../co/ant/as.params

generations = 200

# Population size sets the number of ants per generation
pop.subpop.0.size = 10
pop.subpop.0.species = ec.co.ant.AntSpecies
pop.subpop.0.species.fitness = ec.simple.SimpleFitness

##############################
# AntSpecies holds the construction and update rules that define ACO algorithms
##############################
pop.subpop.0.species.ind = ec.co.ConstructiveIndividual
pop.subpop.0.species.update-rule = ec.co.ant.AntSystemUpdateRule
pop.subpop.0.species.update-rule.decay-rate = 0.1
pop.subpop.0.species.update-rule.deposit-rule = ANT-CYCLE
pop.subpop.0.species.pheromone-table = ec.co.ant.SimplePheromoneTable

pop.subpop.0.species.construction-rule = ec.co.ant.SimpleConstructionRule
pop.subpop.0.species.construction-rule.component-selector = ec.co.ant.ProportionateComponentSelector
pop.subpop.0.species.construction-rule.component-selector.alpha = 1.0
pop.subpop.0.species.construction-rule.component-selector.beta = 1.0

pop.subpop.0.species.update-rule = ec.co.ant.AntSystemUpdateRule
pop.subpop.0.species.update-rule.decay-rate = 0.1
pop.subpop.0.species.update-rule.deposit-rule = ANT-CYCLE

eval.problem = ec.app.knapsack.KnapsackProblem
# Example instance P01 from http://people.sc.fsu.edu/~jburkardt/datasets/knapsack_01/knapsack_01.html
eval.problem.knapsack-size = 165
Expand Down
46 changes: 46 additions & 0 deletions ecj/src/main/resources/ec/app/tsp/acs.params
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2019 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

##############################
# Ant Colony System applied to TSP
##############################
parent.0 = ../../co/ant/acs.params

generations = 200

# Population size sets the number of ants per generation
pop.subpop.0.size = 20


##############################
# AntSpecies holds the construction and update rules that define ACO algorithms
##############################
pop.subpop.0.species.ind = ec.app.tsp.TSPIndividual

# AS uses SimpleConstructionRule to implement psuedo-random proportionate selection
pop.subpop.0.species.construction-rule = ec.co.ant.SimpleConstructionRule
pop.subpop.0.species.construction-rule.component-selector = ec.co.ant.PseudorandomProportionateComponentSelector
pop.subpop.0.species.construction-rule.component-selector.prob-best = 0.5
pop.subpop.0.species.construction-rule.component-selector.alpha = 1.0
pop.subpop.0.species.construction-rule.component-selector.beta = 1.0

# Here we specify ACS's global rule for pheromone updates, using the global best ant
# to deposite pheromones at the generation boundary
pop.subpop.0.species.update-rule = ec.co.ant.GlobalUpdateRule
pop.subpop.0.species.update-rule.rho = 0.3
# You could switch this to ITERATION-BEST if you like
pop.subpop.0.species.update-rule.best-strategy = GLOBAL_BEST

# Here we add ACS's local update rule, which decayse pheromones visited by each ant as soon as it is evaluated (rather than waiting for the generation boundary)
pop.subpop.0.species.local-update-rule = ec.co.ant.ACSLocalUpdateRule
pop.subpop.0.species.local-update-rule.rho = 0.1
pop.subpop.0.species.local-update-rule.minimum-pheromone = 0.000001


##############################
# Problem
##############################
eval.problem = ec.app.tsp.TSPProblem
#eval.problem.file = $src/main/resources/ec/app/tsp/test4.tsp
eval.problem.file = $src/main/resources/ec/app/tsp/berlin52.tsp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2018 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information
parent.0 = aco.params
parent.1 = ../../eval/master.params
# That's it! We're not changing any of the parameters
# but you might want to look at your options in
# ec/eval/master.params
# Copyright 2018 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

parent.0 = as.params
parent.1 = ../../eval/master.params

# That's it! We're not changing any of the parameters
# but you might want to look at your options in
# ec/eval/master.params

Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

parent.0 = ../../simple/simple.params
##############################
# Ant System applied to TSP
##############################

parent.0 = ../../co/ant/as.params

breed = ec.co.ConstructiveBreeder
generations = 200

# Population size sets the number of ants per generation
pop.subpop.0.size = 20
pop.subpop.0.species = ec.co.ant.AntSpecies
pop.subpop.0.species.fitness = ec.simple.SimpleFitness

##############################
# AntSpecies holds the construction and update rules that define ACO algorithms
##############################
pop.subpop.0.species.ind = ec.app.tsp.TSPIndividual
pop.subpop.0.species.update-rule = ec.co.ant.AntSystemUpdateRule
pop.subpop.0.species.update-rule.decay-rate = 0.1
pop.subpop.0.species.update-rule.deposit-rule = ANT-CYCLE
pop.subpop.0.species.pheromone-table = ec.co.ant.SimplePheromoneTable

# AS uses SimpleConstructionRule to implement proportionate selection
pop.subpop.0.species.construction-rule = ec.co.ant.SimpleConstructionRule
pop.subpop.0.species.construction-rule.component-selector = ec.co.ant.ProportionateComponentSelector
pop.subpop.0.species.construction-rule.component-selector.alpha = 1.0
pop.subpop.0.species.construction-rule.component-selector.beta = 1.0
pop.subpop.0.species.pheromone

# Here we specify AS's global rule for pheromone updates
pop.subpop.0.species.update-rule = ec.co.ant.AntSystemUpdateRule
pop.subpop.0.species.update-rule.decay-rate = 0.1
pop.subpop.0.species.update-rule.deposit-rule = ANT-CYCLE


##############################
# Problem
##############################
eval.problem = ec.app.tsp.TSPProblem
#eval.problem.file = $src/main/resources/ec/app/tsp/test4.tsp
eval.problem.file = $src/main/resources/ec/app/tsp/berlin52.tsp
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# Copyright 2006 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information
parent.0 = aco.master.params
# You may want to customize each slave by giving it a unique
# name like this:
#
# eval.slave.name = my-slave-name
#
# (do this differently for each slave:, slave-2, slave-3, etc.)
# Otherwise, each slave will give itself an approximately unique name based
# on the current time and the IP address of the slave. The unique name
# doesn't HAVE to be unique -- it's just for printing/debug purposes.
# We want to make sure that the Slave's Statistics object does not
# override the Master's statistics files. The easiest way to do this
# is to eliminate the Statistics object in the slave by setting it
# to the (empty default) Statistics class with no children.
eval.stat = ec.Statistics
eval.stat.num-children = 0
# The slave needs to know where the Master is. You need to
# change this.
eval.master.host = 127.0.0.1
eval.return-inds = false
evalthreads = 1
# Copyright 2006 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

parent.0 = as.master.params


# You may want to customize each slave by giving it a unique
# name like this:
#
# eval.slave.name = my-slave-name
#
# (do this differently for each slave:, slave-2, slave-3, etc.)
# Otherwise, each slave will give itself an approximately unique name based
# on the current time and the IP address of the slave. The unique name
# doesn't HAVE to be unique -- it's just for printing/debug purposes.


# We want to make sure that the Slave's Statistics object does not
# override the Master's statistics files. The easiest way to do this
# is to eliminate the Statistics object in the slave by setting it
# to the (empty default) Statistics class with no children.

eval.stat = ec.Statistics
eval.stat.num-children = 0


# The slave needs to know where the Master is. You need to
# change this.
eval.master.host = 127.0.0.1

eval.return-inds = false
evalthreads = 1
eval.slave.one-shot = false
36 changes: 36 additions & 0 deletions ecj/src/main/resources/ec/co/ant/acs.params
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 by Sean Luke and George Mason University
# Licensed under the Academic Free License version 3.0
# See the file "LICENSE" for more information

##############################
# Ant Colony System boiler plate
##############################

# To implement ACS, we adapt ECJ's steady-state EA model to support local
# and global pheromone update rules.
parent.1 = ../../simple/simple.params
parent.0 = ../../steadystate/steadystate.params

breed = ec.steadystate.SteadyStateBreeder

# generations = 200

# Population size sets the number of ants per generation
pop.subpops = 1
#pop.subpop.0.size = 20
pop.subpop.0.species = ec.co.ant.AntSpecies

# To use an ACO algorithm with SteadyStateEvolutionState, we need to tell it to
# throw away the population after each generation boundary. Otherwise it will try
# to "insert" ants into a persistent population, which is not how ACO works!
steady.empty-at-generation-boundary = True

##############################
# AntSpecies holds the construction and update rules that define ACO algorithms
##############################
pop.subpop.0.species.fitness = ec.simple.SimpleFitness
pop.subpop.0.species.ind = ec.app.tsp.TSPIndividual
pop.subpop.0.species.pheromone-table = ec.co.ant.SimplePheromoneTable

# Further species parameters (construction-rule, update-rule, etc) must be
# set by the application.
Loading

0 comments on commit 5046578

Please sign in to comment.