aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/population/Node.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/population/Node.java')
-rw-r--r--src/jcgp/backend/population/Node.java41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java
index 3bcf3da..7712c50 100644
--- a/src/jcgp/backend/population/Node.java
+++ b/src/jcgp/backend/population/Node.java
@@ -3,6 +3,7 @@ package jcgp.backend.population;
import java.util.ArrayList;
import jcgp.backend.function.Function;
+import jcgp.backend.resources.Resources;
/**
* Nodes make up the main part of the chromosome,
@@ -16,6 +17,10 @@ import jcgp.backend.function.Function;
* (determined by the maximum arity of the function set)
* and must be reinstantiated if the experiment arity
* changes.
+ * <br><br>
+ * When mutating a node, it is easiest to use {@code mutate()}.
+ * Alternatively, you may also perform a specific mutation using
+ * {@code setConnection(...)} and {@code setFunction(...)}.
*
* @author Eduardo Pedroni
*
@@ -122,6 +127,24 @@ public class Node implements Mutable, Connection {
}
}
}
+
+ /**
+ * This method sets the indexed connection to the specified new connection.
+ * If the given connection is null or disrespects levels back, it is discarded
+ * and no connections are changed.
+ *
+ * @param index the connection index to set.
+ * @param newConnection the {@code Connection} to connect to.
+ */
+ public void setConnection(int index, Connection newConnection) {
+ // connection must not be null
+ if (newConnection != null) {
+ //if () {
+ connections[index] = newConnection;
+ chromosome.recomputeActiveNodes();
+ //}
+ }
+ }
@Override
public boolean copyOf(Mutable element) {
@@ -174,11 +197,19 @@ public class Node implements Mutable, Connection {
}
@Override
- public void setConnection(int index, Connection newConnection) {
- // connection must not be null
- if (newConnection != null) {
- connections[index] = newConnection;
- chromosome.recomputeActiveNodes();
+ public void mutate() {
+ Resources resources = chromosome.getResources();
+
+ // choose to mutate the function or a connection
+ int geneType = resources.getRandomInt(1 + resources.arity());
+
+ // if the int is less than 1, mutate function, else mutate connections
+ if (geneType < 1) {
+ setFunction(resources.getRandomFunction());
+ } else {
+ // if we decided to mutate connection, subtract 1 from geneType so it fits into the arity range
+ geneType--;
+ setConnection(geneType, chromosome.getRandomConnection(column));
}
}