aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/population')
-rw-r--r--src/jcgp/backend/population/Chromosome.java10
-rw-r--r--src/jcgp/backend/population/Population.java53
2 files changed, 48 insertions, 15 deletions
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index 18ae9bb..41ba06e 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -43,12 +43,12 @@ public class Chromosome {
*/
public Chromosome(Chromosome clone) {
// store a reference to the parameters
- this.resources = clone.getParameters();
+ this.resources = clone.getResources();
// allocate memory for all elements of the chromosome
instantiateElements();
// initialise all connections based on argument
- copyConnections(clone);
+ copyGenes(clone);
}
/**
@@ -102,7 +102,7 @@ public class Chromosome {
/**
* @param clone
*/
- public void copyConnections(Chromosome clone) {
+ public void copyGenes(Chromosome clone) {
int arity = resources.getInt("arity");
// copy nodes - [rows][columns]
@@ -119,7 +119,7 @@ public class Chromosome {
} else if (copyConnection instanceof Node) {
connections[i] = nodes[((Node) copyConnection).getRow()][((Node) copyConnection).getColumn()];
} else {
- System.out.println("Warning: Connection of subtype " + copyConnection.getClass().toString() + " is not explicitly handled by copy constructor.");
+ System.out.println("Error: Connection of subtype " + copyConnection.getClass().toString() + " is not explicitly handled by copy method.");
}
}
// initialise with copied arguments
@@ -329,7 +329,7 @@ public class Chromosome {
}
}
- public Resources getParameters() {
+ public Resources getResources() {
return resources;
}
}
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index 7049d79..7b62d27 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -6,27 +6,41 @@ import jcgp.JCGP.Resources;
public class Population {
private Chromosome[] chromosomes;
- private int fittest;
+ private Resources resources;
+ /**
+ * Initialise a random population according to the parameters specified
+ * in the resources.
+ *
+ * @param resources the CGP resources
+ */
public Population(Resources resources) {
+ this.resources = resources;
+
chromosomes = new Chromosome[(resources.getInt("popSize"))];
for (int c = 0; c < chromosomes.length; c++) {
chromosomes[c] = new Chromosome(resources);
}
}
+ /**
+ * Initialise a population of copies of the given chromosome.
+ *
+ * @param parent
+ * @param resources
+ */
public Population(Chromosome parent, Resources resources) {
+ this.resources = resources;
+
chromosomes = new Chromosome[(resources.getInt("popSize"))];
- // make a clone for safety
- this.chromosomes[0] = new Chromosome(parent);
// generate the rest of the individuals
- for (int c = 1; c < chromosomes.length; c++) {
- chromosomes[c] = new Chromosome(chromosomes[0]);
+ for (int c = 0; c < chromosomes.length; c++) {
+ chromosomes[c] = new Chromosome(parent);
}
}
/**
- * Returns all chromosomes, parents first, then offspring.
+ * Returns the indexed chromosome.
*
* @param index
* @return
@@ -35,11 +49,30 @@ public class Population {
return chromosomes[index];
}
- public void setBestIndividual(int index) {
- fittest = index;
+ /**
+ * @return a random chromosome from this population.
+ */
+ public Chromosome getRandomChromosome() {
+ return chromosomes[resources.getRandomInt(chromosomes.length)];
}
+
- public Chromosome getBestIndividual() {
- return chromosomes[fittest];
+ /**
+ * Copy a chromosome into a different position.
+ * After this returns, the target chromosome has
+ * identical connections and functions to the source
+ * one, though they are separate instances.
+ *
+ * This method does nothing if source == target.
+ *
+ * @param source
+ * @param target
+ */
+ public void copyChromosome(int source, int target) {
+ if (source != target) {
+ chromosomes[target].copyGenes(chromosomes[source]);
+ }
}
+
+
}