aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r--src/jcgp/backend/function/IntegerArithmetic.java10
-rw-r--r--src/jcgp/backend/modules/es/EvolutionaryStrategy.java4
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java22
-rw-r--r--src/jcgp/backend/modules/es/TournamentSelection.java8
-rw-r--r--src/jcgp/backend/modules/problem/TestCaseProblem.java13
-rw-r--r--src/jcgp/backend/population/Population.java22
6 files changed, 36 insertions, 43 deletions
diff --git a/src/jcgp/backend/function/IntegerArithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java
index c08a72a..9bb02b5 100644
--- a/src/jcgp/backend/function/IntegerArithmetic.java
+++ b/src/jcgp/backend/function/IntegerArithmetic.java
@@ -1,6 +1,5 @@
package jcgp.backend.function;
-import jcgp.backend.exceptions.InvalidArgumentsException;
import jcgp.backend.population.Connection;
public class IntegerArithmetic extends FunctionSet {
@@ -20,7 +19,7 @@ public class IntegerArithmetic extends FunctionSet {
@Override
public Integer run(Connection... connections) {
if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
+ throw new IllegalArgumentException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
Integer arg2 = ((Integer) connections[1].getValue());
@@ -45,7 +44,7 @@ public class IntegerArithmetic extends FunctionSet {
@Override
public Integer run(Connection... connections) {
if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
+ throw new IllegalArgumentException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
Integer arg2 = ((Integer) connections[1].getValue());
@@ -70,12 +69,11 @@ public class IntegerArithmetic extends FunctionSet {
@Override
public Integer run(Connection... connections) {
if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
+ throw new IllegalArgumentException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
Integer arg2 = ((Integer) connections[1].getValue());
Integer result = arg1 * arg2;
-
return result;
}
}
@@ -95,7 +93,7 @@ public class IntegerArithmetic extends FunctionSet {
@Override
public Integer run(Connection... connections) {
if (connections.length < 2) {
- throw new InvalidArgumentsException("Not enough connections were given.");
+ throw new IllegalArgumentException("Not enough connections were given.");
} else {
Integer arg1 = ((Integer) connections[0].getValue());
Integer arg2 = ((Integer) connections[1].getValue());
diff --git a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
index 1117e99..8ab287d 100644
--- a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
+++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
@@ -8,7 +8,5 @@ import jcgp.backend.resources.Resources;
public interface EvolutionaryStrategy extends Module {
public abstract void evolve(Population population, Mutator mutator, Resources resources);
-
- public abstract int getFittestChromosome();
-
+
}
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
index 67236f9..6a3883b 100644
--- a/src/jcgp/backend/modules/es/MuPlusLambda.java
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -17,8 +17,6 @@ import jcgp.backend.resources.parameters.ParameterStatus;
*/
public class MuPlusLambda implements EvolutionaryStrategy {
- private int fittestChromosome;
-
private IntegerParameter parents, offspring;
private BooleanParameter report;
@@ -62,22 +60,13 @@ public class MuPlusLambda implements EvolutionaryStrategy {
@Override
public void evolve(Population population, Mutator mutator, Resources resources) {
// select fittest chromosomes
- fittestChromosome = 0;
-
- if (report.get()) resources.reportln("[ES] Selecting fittest chromosome...");
- for (int i = 0; i < resources.populationSize(); i++) {
- if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness());
- if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) {
- fittestChromosome = i;
- }
- }
- if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome);
+ if (report.get()) resources.reportln("[ES] Selected chromosome: " + population.getFittestIndex());
// create copies of fittest chromosome, mutate them
for (int i = 0; i < resources.populationSize(); i++) {
- if (i != fittestChromosome) {
+ if (i != population.getFittestIndex()) {
if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
- population.copyChromosome(fittestChromosome, i);
+ population.copyChromosome(population.getFittestIndex(), i);
if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
mutator.mutate(population.getChromosome(i), resources);
}
@@ -88,11 +77,6 @@ public class MuPlusLambda implements EvolutionaryStrategy {
}
@Override
- public int getFittestChromosome() {
- return fittestChromosome;
- }
-
- @Override
public Parameter<?>[] getLocalParameters() {
return new Parameter[] {parents, offspring, report};
}
diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java
index 8286101..4070468 100644
--- a/src/jcgp/backend/modules/es/TournamentSelection.java
+++ b/src/jcgp/backend/modules/es/TournamentSelection.java
@@ -8,8 +8,6 @@ import jcgp.backend.resources.parameters.Parameter;
public class TournamentSelection implements EvolutionaryStrategy {
- private int fittestChromosome;
-
private IntegerParameter tournament;
public TournamentSelection() {
@@ -30,15 +28,9 @@ public class TournamentSelection implements EvolutionaryStrategy {
public void evolve(Population population, Mutator mutator,
Resources parameters) {
tournament.set(tournament.get() + 1);
- fittestChromosome = 0;
// TODO implement this
}
-
- @Override
- public int getFittestChromosome() {
- return fittestChromosome;
- }
@Override
public String toString() {
diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java
index 68318cf..ee72860 100644
--- a/src/jcgp/backend/modules/problem/TestCaseProblem.java
+++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java
@@ -52,8 +52,6 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
private IntegerParameter maxFitness;
private final int inputCount, outputCount;
- private U type;
-
public TestCaseProblem(Resources resources) {
super();
@@ -67,12 +65,14 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
}
};
testCases = FXCollections.observableArrayList();
- //testCases = new ObservableList<TestCase<U>>();
}
@Override
public void evaluate(Population population, Resources resources) {
+ // set fittest to 0, change it whenever a fitter one is found
+ population.setFittest(0);
+
// for every chromosome in the population
for (int i = 0; i < resources.populationSize(); i++) {
// assume an initial fitness of 0
@@ -89,6 +89,9 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
}
// assign the resulting fitness to the respective individual
population.getChromosome(i).setFitness(fitness);
+ if (fitness >= population.getFittest().getFitness()) {
+ population.setFittest(i);
+ }
}
}
@@ -147,10 +150,6 @@ public abstract class TestCaseProblem<U extends Object> extends Problem {
public int getOutputCount() {
return outputCount;
}
-
- public Class<?> getType() {
- return type.getClass();
- }
}
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index fdeec82..a850595 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -6,6 +6,7 @@ public class Population {
private Chromosome[] chromosomes;
private final Resources resources;
+ private int fittest = 0;
/**
* Initialise a random population according to the parameters specified
@@ -76,5 +77,26 @@ public class Population {
chromosomes[c].reinitialiseConnections();
}
}
+
+ public void setFittest(int fittest) {
+ this.fittest = fittest;
+ }
+
+ public void setFittest(Chromosome fittest) {
+ for (int i = 0; i < chromosomes.length; i++) {
+ if (chromosomes[i] == fittest) {
+ this.fittest = i;
+ return;
+ }
+ }
+ }
+
+ public Chromosome getFittest() {
+ return chromosomes[fittest];
+ }
+
+ public int getFittestIndex() {
+ return fittest;
+ }
}