aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/es
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/es')
-rw-r--r--src/jcgp/backend/modules/es/EvolutionaryStrategy.java13
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java43
-rw-r--r--src/jcgp/backend/modules/es/TournamentSelection.java33
3 files changed, 52 insertions, 37 deletions
diff --git a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
index 1a14552..30a3c4a 100644
--- a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
+++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
@@ -33,12 +33,21 @@ import jcgp.backend.resources.Resources;
public abstract class EvolutionaryStrategy extends Module {
/**
+ * For internal use only, initialises the resources field.
+ *
+ * @param resources the experiment's resources.
+ */
+ protected EvolutionaryStrategy(Resources resources) {
+ super(resources);
+ }
+
+ /**
* Performs the selection algorithm and uses the mutator to create
* the next generation of solutions.
*
* @param population the population to evolve.
* @param mutator the mutator with which to mutate the promoted individuals.
- * @param resources parameters and utilities for optional reference.
*/
- public abstract void evolve(Population population, Mutator mutator, Resources resources);
+ public abstract void evolve(Population population, Mutator mutator);
+
}
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
index 8186b11..3743ee6 100644
--- a/src/jcgp/backend/modules/es/MuPlusLambda.java
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -28,21 +28,21 @@ import jcgp.backend.resources.Resources;
*
*/
public class MuPlusLambda extends EvolutionaryStrategy {
-
+
private IntegerParameter mu, lambda;
private BooleanParameter report;
-
+
/**
* Creates a new instance of MuPlusLambda.
*
* @param resources a reference to the experiment's resources.
*/
public MuPlusLambda(final Resources resources) {
- super();
+ super(resources);
mu = new IntegerParameter(1, "Parents (μ)") {
@Override
public void validate(Number newValue) {
- if (newValue.intValue() + lambda.get() != resources.populationSize()) {
+ if (newValue.intValue() + lambda.get() != getResources().populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
} else if (newValue.intValue() <= 0) {
@@ -53,11 +53,11 @@ public class MuPlusLambda extends EvolutionaryStrategy {
}
}
};
-
+
lambda = new IntegerParameter(4, "Offspring (λ)") {
@Override
public void validate(Number newValue) {
- if (newValue.intValue() + mu.get() != resources.populationSize()) {
+ if (newValue.intValue() + mu.get() != getResources().populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
} else if (newValue.intValue() <= 0) {
@@ -68,29 +68,30 @@ public class MuPlusLambda extends EvolutionaryStrategy {
}
}
};
-
+
report = new BooleanParameter(false, "Report");
-
+
setName("(μ + λ)");
registerParameters(mu, lambda, report);
}
-
+
@Override
- public void evolve(Population population, Mutator mutator, Resources resources) {
- /* Population is sorted in ascending order of fitness, so leave the last
- * mu chromosomes intact
- */
- for (int i = 0; i < resources.populationSize() - mu.get(); i++) {
+ public void evolve(Population population, Mutator mutator) {
+ // sort the population in order of best fitness
+ population.sort();
+
+ // population is now sorted in ascending order of fitness, the last chromosomes are the fittest
+ for (int i = 0; i < getResources().populationSize() - mu.get(); i++) {
// select a random parent out of the lambda offspring individuals
- int randomParent = resources.populationSize() - 1 - resources.getRandomInt(mu.get());
- if (report.get()) resources.reportln("[ES] Copying Chr " + randomParent + " to population position " + i);
+ int randomParent = getResources().populationSize() - 1 - getResources().getRandomInt(mu.get());
+ if (report.get()) getResources().reportln("[ES] Copying Chr " + randomParent + " to population position " + i);
population.copyChromosome(randomParent, i);
-
+
// mutate selected chromosome
- if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
- mutator.mutate(population.getChromosome(i), resources);
+ if (report.get()) getResources().reportln("[ES] Mutating copied chromosome");
+ mutator.mutate(population.get(i));
}
-
- if (report.get()) resources.reportln("[ES] Generation is complete");
+
+ if (report.get()) getResources().reportln("[ES] Generation is complete");
}
}
diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java
index 209caca..6d467f5 100644
--- a/src/jcgp/backend/modules/es/TournamentSelection.java
+++ b/src/jcgp/backend/modules/es/TournamentSelection.java
@@ -44,7 +44,7 @@ public class TournamentSelection extends EvolutionaryStrategy {
* @param resources a reference to the experiment's resources.
*/
public TournamentSelection(final Resources resources) {
- super();
+ super(resources);
tournamentSize = new IntegerParameter(1, "Tournament size") {
@Override
public void validate(Number newValue) {
@@ -73,17 +73,22 @@ public class TournamentSelection extends EvolutionaryStrategy {
}
@Override
- public void evolve(Population population, Mutator mutator, Resources resources) {
+ public void evolve(Population population, Mutator mutator) {
/* Create an entirely new population by isolating random subsets of
* the original population and choosing the fittest individual within
* that subset. Each chosen individual is mutated and copied back into the
* population.
*/
- Chromosome[] newPopulation = new Chromosome[resources.populationSize()];
+
+ // sort the population by fitness to make things easier
+ population.sort();
+
+ // this array holds the new population temporarily, until it is copied over
+ Chromosome[] newPopulation = new Chromosome[getResources().populationSize()];
// start by selecting all of the chromosomes that will be promoted
- for (int i = 0; i < resources.populationSize(); i++) {
- if (report.get()) resources.reportln("[ES] Starting tournament " + i);
+ for (int i = 0; i < getResources().populationSize(); i++) {
+ if (report.get()) getResources().reportln("[ES] Starting tournament " + i);
/* the population is sorted in ascending order of fitness,
* meaning the higher the index of the contender, the fitter
@@ -91,21 +96,21 @@ public class TournamentSelection extends EvolutionaryStrategy {
*/
int[] contenders = new int[tournamentSize.get()];
for (int t = 0; t < tournamentSize.get() - 1; t++) {
- contenders[t] = resources.getRandomInt(resources.populationSize());
+ contenders[t] = getResources().getRandomInt(getResources().populationSize());
}
- if (report.get()) resources.reportln("[ES] Selected contenders: " + Arrays.toString(contenders));
+ if (report.get()) getResources().reportln("[ES] Selected contenders: " + Arrays.toString(contenders));
Arrays.sort(contenders);
- if (report.get()) resources.reportln("[ES] Chr " + contenders[contenders.length - 1] + " wins the tournament, copying and mutating...");
+ if (report.get()) getResources().reportln("[ES] Chr " + contenders[contenders.length - 1] + " wins the tournament, copying and mutating...");
// create a copy of the selected chromosome and mutate it
- newPopulation[i] = new Chromosome(population.getChromosome(contenders[contenders.length - 1]));
- mutator.mutate(newPopulation[i], resources);
+ newPopulation[i] = new Chromosome(population.get(contenders[contenders.length - 1]));
+ mutator.mutate(newPopulation[i]);
}
- if (report.get()) resources.reportln("[ES] Tournaments are finished, copying new chromosomes into population");
+ if (report.get()) getResources().reportln("[ES] Tournaments are finished, copying new chromosomes into population");
// newPopulation has been generated, copy into the population
- for (int c = 0; c < resources.populationSize(); c++) {
- population.getChromosome(c).copyGenes(newPopulation[c]);
+ for (int c = 0; c < getResources().populationSize(); c++) {
+ population.get(c).copyGenes(newPopulation[c]);
}
- if (report.get()) resources.reportln("[ES] Generation is complete");
+ if (report.get()) getResources().reportln("[ES] Generation is complete");
}
}