aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/JCGP.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/JCGP.java')
-rw-r--r--src/jcgp/JCGP.java287
1 files changed, 287 insertions, 0 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java
new file mode 100644
index 0000000..21161fa
--- /dev/null
+++ b/src/jcgp/JCGP.java
@@ -0,0 +1,287 @@
+package jcgp;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Random;
+import java.util.Set;
+
+import javafx.beans.property.Property;
+import jcgp.function.Arithmetic;
+import jcgp.function.BitwiseLogic;
+import jcgp.function.BooleanLogic;
+import jcgp.function.Function;
+import jcgp.function.FunctionSet;
+import jcgp.modules.ea.EvolutionaryAlgorithm;
+import jcgp.modules.ea.StandardEA;
+import jcgp.modules.fitness.FitnessFunction;
+import jcgp.modules.fitness.TestCase;
+import jcgp.modules.fitness.TestCaseEvaluator;
+import jcgp.modules.mutator.Mutator;
+import jcgp.modules.mutator.PointMutator;
+import jcgp.parameters.BooleanParameter;
+import jcgp.parameters.IntegerParameter;
+import jcgp.parameters.Parameter;
+import jcgp.population.Population;
+
+public class JCGP {
+
+ /**
+ *
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+ public static class Resources {
+ private HashMap<String, Parameter> moduleParameters = new HashMap<String, Parameter>();
+ private HashMap<String, Parameter> coreParameters = new HashMap<String, Parameter>();
+ private HashMap<String, Parameter> allParameters = new HashMap<String, Parameter>();
+
+ private Random numberGenerator;
+
+ // function sets
+ private FunctionSet[] functionSets = new FunctionSet[] {
+ new Arithmetic(),
+ new BitwiseLogic(),
+ new BooleanLogic() };
+ private FunctionSet functionSet = functionSets[0];
+
+ private TestCase[] testCases;
+
+ public Resources() {
+ createCoreParameters();
+
+ numberGenerator = new Random((int) get("seed"));
+ functionSet = functionSets[0];
+
+ set("arity", functionSet.getMaxArity());
+ }
+
+ public Object get(String key) {
+ return allParameters.get(key).getValue();
+ }
+
+ public void set(String key, Object value) {
+ allParameters.get(key).setValue(value);
+ }
+
+ public Property<?> getProperty(String key) {
+ return allParameters.get(key).valueProperty();
+ }
+
+ public void setManagedParameter(String key, boolean value) {
+ allParameters.get(key).setManaged(value);
+ }
+
+ public void setHiddenParameter(String key, boolean value) {
+ allParameters.get(key).setHidden(value);
+ }
+
+ public boolean contains(String key) {
+ return allParameters.containsKey(key);
+ }
+
+ private void createCoreParameters() {
+ coreParameters.put("rows", new IntegerParameter(9, "Rows"));
+ coreParameters.put("columns", new IntegerParameter(10, "Columns"));
+ coreParameters.put("inputs", new IntegerParameter(3, "Inputs"));
+ coreParameters.put("outputs", new IntegerParameter(3, "Outputs"));
+ coreParameters.put("popSize", new IntegerParameter(5, "Population"));
+ coreParameters.put("levelsBack", new IntegerParameter(2, "Levels back"));
+
+ coreParameters.put("nodes", new IntegerParameter(90, "Nodes", true, true));
+
+ coreParameters.put("generations", new IntegerParameter(1000000, "Generations"));
+ coreParameters.put("currentGen", new IntegerParameter(0, "Generation"));
+ coreParameters.put("runs", new IntegerParameter(5, "Runs"));
+
+ coreParameters.put("arity", new IntegerParameter(0, "Max arity", true, true));
+
+ coreParameters.put("seed", new IntegerParameter(123, "Random seed", true, true));
+
+ coreParameters.put("debug", new BooleanParameter(false, "Debug"));
+
+ allParameters.putAll(coreParameters);
+ }
+
+ private void resetParameters(EvolutionaryAlgorithm ea, Mutator mu, FitnessFunction ff) {
+ Iterator<Entry<String, Parameter>> it = coreParameters.entrySet().iterator();
+ while (it.hasNext()) {
+ ((Parameter) ((Map.Entry<String, Parameter>) it.next()).getValue()).reset();
+ }
+
+ allParameters.clear();
+ allParameters.putAll(coreParameters);
+ addModuleParameters(ea, mu, ff);
+ }
+
+ private void addModuleParameters(EvolutionaryAlgorithm ea, Mutator mu, FitnessFunction ff) {
+ moduleParameters.clear();
+ moduleParameters.putAll(ea.activate(this));
+ moduleParameters.putAll(mu.activate(this));
+ moduleParameters.putAll(ff.activate(this));
+
+ allParameters.putAll(moduleParameters);
+ }
+
+ public Iterator<Entry<String, Parameter>> iterator() {
+ return allParameters.entrySet().iterator();
+ }
+
+ /*
+ * Utility functions
+ */
+ public int getRandomInt(int limit) {
+ return numberGenerator.nextInt(limit);
+ }
+
+ public double getRandomDouble(int limit) {
+ return numberGenerator.nextDouble() * limit;
+ }
+
+ public double getRandomDouble() {
+ return numberGenerator.nextDouble();
+ }
+
+ /*
+ * Function set functions
+ */
+ public Function getRandomFunction() {
+ return functionSet.getFunction(numberGenerator.nextInt(functionSet.getFunctionCount()));
+ }
+
+ public Function getFunction(int index) {
+ return functionSet.getFunction(index);
+ }
+
+ public void setFunctionSet(int index) {
+ functionSet = functionSets[index];
+ }
+
+ /*
+ * Test cases
+ */
+ public void setTestCases(TestCase ... testCases) {
+ this.testCases = testCases;
+ }
+
+ public TestCase getTestCase(int index) {
+ return testCases[index];
+ }
+
+ public int getTestCaseCount() {
+ return testCases.length;
+ }
+ }
+
+ private Resources resources = new Resources();
+
+ /*
+ * The following arrays contain all available modules. These collections are read by the GUI
+ * when generating menus, so modules not added here will *NOT* be selectable in the GUI.
+ *
+ * Each array is accompanied by a field which contains a reference to the currently selected
+ * module, 0 by default.
+ */
+ // mutators
+ private Mutator[] mutators = new Mutator[] {
+ new PointMutator() };
+ private Mutator mutator = mutators[0];
+
+ // evolutionary algorithms
+ private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] {
+ new StandardEA() };
+ private EvolutionaryAlgorithm evolutionaryAlgorithm = evolutionaryAlgorithms[0];
+
+ // fitness evaluators
+ private FitnessFunction[] fitnessFunctions = new FitnessFunction[] {
+ new TestCaseEvaluator() };
+ private FitnessFunction fitnessFunction = fitnessFunctions[0];
+
+ // the population of chromosomes
+ private Population population = new Population(resources);
+
+
+ public JCGP() {
+ resources.addModuleParameters(evolutionaryAlgorithm, mutator, fitnessFunction);
+
+ resources.setTestCases(new TestCase(new Integer[]{1, 2, 3}, new Integer[]{4, 5, 6}),
+ new TestCase(new Integer[]{1, 12, 4}, new Integer[]{6, 21, 2}));
+
+// for (int i = 0; i < (int) resources.get("generations"); i++) {
+//
+// resources.set("currentGen", ((int) resources.get("currentGen")) + 1);
+//
+// fitnessFunction.evaluate(population, resources);
+// evolutionaryAlgorithm.evolve(population, mutator, resources);
+//
+// System.out.println("fitness: " + evolutionaryAlgorithm.getFittestChromosome().getFitness());
+//
+// if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 6) {
+// System.out.println("solution found");
+// evolutionaryAlgorithm.getFittestChromosome().printNodes();
+// break;
+// }
+// }
+ }
+
+
+ public Resources getResources() {
+ return resources;
+ }
+
+
+ public Population getPopulation() {
+ return population;
+ }
+
+
+ /**
+ * @return the mutators
+ */
+ public Mutator[] getMutators() {
+ return mutators;
+ }
+
+
+ /**
+ * @return the mutator
+ */
+ public Mutator getMutator() {
+ return mutator;
+ }
+
+
+ /**
+ * @return the evolutionaryAlgorithms
+ */
+ public EvolutionaryAlgorithm[] getEvolutionaryAlgorithms() {
+ return evolutionaryAlgorithms;
+ }
+
+
+ /**
+ * @return the evolutionaryAlgorithm
+ */
+ public EvolutionaryAlgorithm getEvolutionaryAlgorithm() {
+ return evolutionaryAlgorithm;
+ }
+
+
+ /**
+ * @return the fitnessFunctions
+ */
+ public FitnessFunction[] getFitnessFunctions() {
+ return fitnessFunctions;
+ }
+
+
+ /**
+ * @return the fitnessFunction
+ */
+ public FitnessFunction getFitnessFunction() {
+ return fitnessFunction;
+ }
+
+}