-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add standard param files that define Ant System and Ant Colony System…
… algorithms. Revised the knapsack and TSP examples to use these, making for pretty simple examples of using ACO.
- Loading branch information
Showing
12 changed files
with
345 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ ecj/src/main/resources/ec/app/mona/1-MonaLisaOut.png | |
*.idx | ||
*.log | ||
*.out | ||
*.toc | ||
*.toc | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
22 changes: 11 additions & 11 deletions
22
...in/resources/ec/app/tsp/aco.master.params → ...ain/resources/ec/app/tsp/as.master.params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 33 additions & 33 deletions
66
...ain/resources/ec/app/tsp/aco.slave.params → ...main/resources/ec/app/tsp/as.slave.params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.