aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/parser')
-rw-r--r--src/jcgp/backend/parser/ChromosomeParser.java131
-rw-r--r--src/jcgp/backend/parser/FunctionParser.java58
-rw-r--r--src/jcgp/backend/parser/ParameterParser.java76
-rw-r--r--src/jcgp/backend/parser/TestCaseParser.java13
4 files changed, 278 insertions, 0 deletions
diff --git a/src/jcgp/backend/parser/ChromosomeParser.java b/src/jcgp/backend/parser/ChromosomeParser.java
new file mode 100644
index 0000000..74f638c
--- /dev/null
+++ b/src/jcgp/backend/parser/ChromosomeParser.java
@@ -0,0 +1,131 @@
+package jcgp.backend.parser;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.util.Scanner;
+
+import jcgp.backend.population.Chromosome;
+import jcgp.backend.population.Connection;
+import jcgp.backend.population.Node;
+import jcgp.backend.resources.Resources;
+
+/**
+ * This class includes a method for parsing .chr files and another
+ * for writing .chr files from given chromosomes.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class ChromosomeParser {
+
+ /**
+ * Use this method to parse .chr files into a given chromosome.
+ * <br><br>
+ * This is not fully defensive as it doesn't check for number of inputs,
+ * doesn't compare rows and columns individually and doesn't account for levels back. It
+ * is not viable to implement these defensive measures with the chromosome format used
+ * by CGP.
+ *
+ * @param file the .chr file to parse from
+ * @param chromosome the chromosome to configure
+ * @param resources the experiment resources
+ */
+ public static void parse(File file, Chromosome chromosome, Resources resources) {
+ /*
+ * Count the nodes to make sure the size of the .chr file matches the experiment parameters.
+ *
+ * We do this by using the scanner to get the node and output portions of the file as they
+ * are separated by 3 tab characters. Every number is replaced by a single known character,
+ * and the length of the string with the new characters is compared with that of a string
+ * where the new known character has been removed, yielding the total number of values.
+ *
+ */
+ FileReader fr;
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return;
+ }
+ Scanner in = new Scanner(fr);
+ in.useDelimiter("\\t\\t\\t");
+ String geneString = in.next().replaceAll("[0-9]+", "g");
+ String outString = in.next().replaceAll("[0-9]+", "o");
+ int geneCount = geneString.length() - geneString.replace("g", "").length();
+ int outCount = outString.length() - outString.replace("o", "").length();
+ in.close();
+
+
+ // if the acquired values match the current parameters, apply them to the chromosome
+ if ((geneCount == resources.nodes() * (resources.arity() + 1))
+ && outCount == resources.outputs()) {
+ // prepare a new scanner
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return;
+ }
+ in = new Scanner(fr);
+
+ int gene;
+ Connection newConnection;
+ Node changingNode;
+ // for all nodes, columns first
+ for (int c = 0; c < resources.columns(); c ++) {
+ for (int r = 0; r < resources.rows(); r ++) {
+ // store the changing node
+ changingNode = chromosome.getNode(r, c);
+
+ // for every connection
+ for (int i = 0; i < resources.arity(); i ++) {
+ // get connection number from the .chr file
+ gene = in.nextInt();
+ if (gene < resources.inputs()) {
+ // connection was an input
+ newConnection = chromosome.getInput(gene);
+ } else {
+ // connection was another node, calculate which from its number
+ newConnection = chromosome.getNode((gene - resources.inputs()) % resources.rows(),
+ (gene - resources.inputs()) / resources.rows());
+ }
+ changingNode.setConnection(i, newConnection);
+ }
+
+ // set the function, straight indexing should work - this is not entirely
+ // safe, but it is not viable to check for functionset compatibility
+ changingNode.setFunction(resources.getFunction(in.nextInt()));
+ }
+ }
+
+ // outputs
+ for (int o = 0; o < resources.outputs(); o ++) {
+ gene = in.nextInt();
+ if (gene < resources.inputs()) {
+ // connection was an input
+ newConnection = chromosome.getInput(gene);
+ } else {
+ // connection was another node, calculate which from its number
+ newConnection = chromosome.getNode((gene - resources.inputs()) % resources.rows(),
+ (gene - resources.inputs()) / resources.rows());
+ }
+ chromosome.getOutput(o).setConnection(0, newConnection);
+ }
+ in.close();
+ }
+ }
+
+ /**
+ * Writes a chromosome into the specified .chr file.
+ *
+ *
+ *
+ * @param file
+ * @param chromosome
+ */
+ public static void save(File file, Chromosome chromosome) {
+
+ }
+
+}
diff --git a/src/jcgp/backend/parser/FunctionParser.java b/src/jcgp/backend/parser/FunctionParser.java
new file mode 100644
index 0000000..64a095c
--- /dev/null
+++ b/src/jcgp/backend/parser/FunctionParser.java
@@ -0,0 +1,58 @@
+package jcgp.backend.parser;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.util.Scanner;
+
+import jcgp.backend.function.FunctionSet;
+import jcgp.backend.modules.problem.Problem;
+
+public class FunctionParser {
+
+private FunctionSet functionSet;
+
+ private FunctionParser(Problem problem) {
+ this.functionSet = problem.getFunctionSet();
+ }
+
+ public static void parseFunctions(File file, Problem problem) {
+ FunctionParser pp = new FunctionParser(problem);
+
+ FileReader fr;
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return;
+ }
+
+ Scanner in = new Scanner(fr);
+
+ while (in.hasNextLine()) {
+ String line = in.nextLine();
+ if (line.substring(line.length() - 1).matches("[0-9]")) {
+ pp.parseAndSet(line.split("[^0-9]+"));
+ }
+ }
+
+ in.close();
+ }
+
+ private void parseAndSet(String[] splitString) {
+// System.out.println("new line");
+// for (int i= 0; i < splitString.length; i++) {
+// System.out.println(i + ": " + splitString[i]);
+// }
+ int functionIndex = Integer.parseInt(splitString[splitString.length - 1]);
+ System.out.println("index: " + functionIndex);
+ if (Integer.parseInt(splitString[0]) != 0) {
+ System.out.println("enabling: " + functionSet.getFunction(functionIndex).getName());
+ functionSet.enableFunction(functionIndex);
+ } else {
+ System.out.println("disabling: " + functionSet.getFunction(functionIndex).getName());
+ functionSet.disableFunction(functionIndex);
+ }
+
+ }
+}
diff --git a/src/jcgp/backend/parser/ParameterParser.java b/src/jcgp/backend/parser/ParameterParser.java
new file mode 100644
index 0000000..473b632
--- /dev/null
+++ b/src/jcgp/backend/parser/ParameterParser.java
@@ -0,0 +1,76 @@
+package jcgp.backend.parser;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.util.Scanner;
+import jcgp.backend.resources.ModifiableResources;
+
+public class ParameterParser {
+
+ private ModifiableResources resources;
+
+ private ParameterParser(ModifiableResources resources) {
+ this.resources = resources;
+ }
+
+ public static void parseParameters(File file, ModifiableResources resources) {
+ ParameterParser pp = new ParameterParser(resources);
+
+ FileReader fr;
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return;
+ }
+
+ Scanner in = new Scanner(fr);
+
+ while (in.hasNextLine()) {
+ pp.parseAndSet(in.nextLine().split("( |\t)+"));
+ }
+
+ in.close();
+ }
+
+ private void parseAndSet(String[] splitString) {
+ switch (splitString[1]) {
+
+ case "population_size":
+ resources.setPopulationSize(Integer.parseInt(splitString[0]));
+ break;
+
+ case "num_generations":
+ resources.setGenerations(Integer.parseInt(splitString[0]));
+ break;
+
+ case "num_runs_total":
+ resources.setRuns(Integer.parseInt(splitString[0]));
+ break;
+
+ case "num_rows":
+ resources.setRows(Integer.parseInt(splitString[0]));
+ break;
+
+ case "num_cols":
+ resources.setColumns(Integer.parseInt(splitString[0]));
+ break;
+
+ case "levels_back":
+ resources.setLevelsBack(Integer.parseInt(splitString[0]));
+ break;
+
+ case "report_interval":
+ resources.setReportInterval(Integer.parseInt(splitString[0]));
+ break;
+
+ case "global_seed":
+ resources.setSeed(Integer.parseInt(splitString[0]));
+ break;
+
+ default:
+ break;
+ }
+ }
+}
diff --git a/src/jcgp/backend/parser/TestCaseParser.java b/src/jcgp/backend/parser/TestCaseParser.java
new file mode 100644
index 0000000..c997177
--- /dev/null
+++ b/src/jcgp/backend/parser/TestCaseParser.java
@@ -0,0 +1,13 @@
+package jcgp.backend.parser;
+
+import java.io.File;
+
+import jcgp.backend.modules.problem.TestCaseProblem;
+
+public class TestCaseParser {
+
+ public static void parse(File file, TestCaseProblem<?> problem) {
+
+ }
+
+}