diff options
| author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-30 21:07:37 +0100 | 
|---|---|---|
| committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-03-30 21:07:37 +0100 | 
| commit | 04b35ccdad6e18701ede823e333118b0b22907c2 (patch) | |
| tree | 0e993a5ffee4e63c4a2a6eca137da72b2453f868 /src/jcgp/JCGP.java | |
| parent | 2bf2d3ac2c578de481ecfd545d58be73c5628996 (diff) | |
Running into some issues with running the CGP loop in the background with bindings.
Diffstat (limited to 'src/jcgp/JCGP.java')
| -rw-r--r-- | src/jcgp/JCGP.java | 158 | 
1 files changed, 108 insertions, 50 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index dc121c9..1742b6a 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -2,28 +2,33 @@ package jcgp;  import java.util.HashMap;  import java.util.Iterator; -import java.util.Map;  import java.util.Map.Entry;  import java.util.Random; -import javafx.beans.property.Property; +import javafx.beans.property.SimpleIntegerProperty;  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.ea.MuPlusLambda; +import jcgp.modules.ea.TournamentSelection;  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.DoubleParameter;  import jcgp.parameters.IntegerParameter;  import jcgp.parameters.Parameter;  import jcgp.population.Population; +/** + * @author Eduardo Pedroni + * + */  public class JCGP {  	/** @@ -49,52 +54,57 @@ public class JCGP {  		public Resources() {  			createCoreParameters(); -			numberGenerator = new Random((int) get("seed")); -			 +			numberGenerator = new Random(getInt("seed"));  			set("arity", functionSet.getMaxArity());  		} -		public Object get(String key) { -			return parameters.get(key).getValue(); -		} -		  		public int getInt(String key) { -			return (int) parameters.get(key).getValue(); +			if (parameters.get(key) instanceof IntegerParameter) { +				return ((IntegerParameter) parameters.get(key)).get(); +			} else if (parameters.get(key) instanceof DoubleParameter) { +				return (int) ((DoubleParameter) parameters.get(key)).get(); +			} else { +				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int."); +			}  		}  		public double getDouble(String key) { -			return (double) parameters.get(key).getValue(); +			if (parameters.get(key) instanceof IntegerParameter) { +				return (double) ((IntegerParameter) parameters.get(key)).get(); +			} else if (parameters.get(key) instanceof DoubleParameter) { +				return ((DoubleParameter) parameters.get(key)).get(); +			} else { +				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to double."); +			}  		}  		public boolean getBoolean(String key) { -			return (boolean) parameters.get(key).getValue(); +			if (parameters.get(key) instanceof BooleanParameter) { +				return ((BooleanParameter) parameters.get(key)).get(); +			} else { +				throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int."); +			}  		}  		public void set(String key, Object value) { -			parameters.get(key).setValue(value); -		} -		 -		public Property<?> getProperty(String key) { -			return parameters.get(key).valueProperty(); +			if (parameters.get(key) instanceof IntegerParameter) { +				((IntegerParameter) parameters.get(key)).set(((Integer) value).intValue()); +			} else if (parameters.get(key) instanceof DoubleParameter) { +				((DoubleParameter) parameters.get(key)).set(((Double) value).doubleValue()); +			} else if (parameters.get(key) instanceof BooleanParameter) { +				((BooleanParameter) parameters.get(key)).set(((Boolean) value).booleanValue()); +			}  		}  		public Parameter getParameter(String key) {  			return parameters.get(key);  		} -		public void setManagedParameter(String key, boolean value) { -			parameters.get(key).setManaged(value); -		} -		 -		public void setHiddenParameter(String key, boolean value) { -			parameters.get(key).setHidden(value); -		} -		  		public boolean contains(String key) {  			return parameters.containsKey(key);  		} -		private void createCoreParameters() {		 +		private void createCoreParameters() {  			parameters.put("rows", new IntegerParameter(8, "Rows"));  			parameters.put("columns", new IntegerParameter(9, "Columns"));  			parameters.put("inputs", new IntegerParameter(3, "Inputs")); @@ -102,28 +112,23 @@ public class JCGP {  			parameters.put("popSize", new IntegerParameter(5, "Population"));  			parameters.put("levelsBack", new IntegerParameter(2, "Levels back")); -			parameters.put("nodes", new IntegerParameter(72, "Nodes", true, true)); -			 +			IntegerParameter nodes = new IntegerParameter(1, "Nodes", false, true); +			nodes.valueProperty().bind(((SimpleIntegerProperty) ((IntegerParameter) parameters.get("rows")).valueProperty()).multiply((SimpleIntegerProperty) ((IntegerParameter) parameters.get("columns")).valueProperty())); +			parameters.put("nodes", nodes); +						  			parameters.put("generations", new IntegerParameter(1000000, "Generations")); -			parameters.put("currentGen", new IntegerParameter(0, "Generation", true, false)); +			parameters.put("currentGen", new IntegerParameter(1, "Generation", false, false)); +			  			parameters.put("runs", new IntegerParameter(5, "Runs")); +			parameters.put("currentRun", new IntegerParameter(1, "Run", false, false));  			parameters.put("arity", new IntegerParameter(0, "Max arity", true, true)); -  			parameters.put("seed", new IntegerParameter(123, "Seed")); -			parameters.put("debug", new BooleanParameter(false, "Debug")); +			parameters.put("verbose", new BooleanParameter(false, "Verbose"));  			parameters.put("report", new IntegerParameter(1, "Report"));  		} -		// TODO fix this up -		private void resetParameters(EvolutionaryAlgorithm ea, Mutator mu, FitnessFunction ff) { -			Iterator<Entry<String, Parameter>> it = parameters.entrySet().iterator(); -		    while (it.hasNext()) { -		    	((Parameter) ((Map.Entry<String, Parameter>) it.next()).getValue()).reset(); -		    } -		} -  		/**  		 *   		 *  @@ -152,11 +157,11 @@ public class JCGP {  		 * FunctionSet functions  		 */  		public Function getRandomFunction() { -			return functionSet.getFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount())); +			return functionSet.getAllowedFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount()));  		}  		public Function getFunction(int index) { -			return functionSet.getFunction(index); +			return functionSet.getAllowedFunction(index);  		}  		public void setFunctionSet(int index) { @@ -205,27 +210,36 @@ public class JCGP {  	// mutators  	private Mutator[] mutators = new Mutator[] {  			new PointMutator() }; -	private Mutator mutator = mutators[0]; +	private Mutator mutator;  	// evolutionary algorithms  	private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] { -			new StandardEA() }; -	private EvolutionaryAlgorithm evolutionaryAlgorithm = evolutionaryAlgorithms[0]; +			new MuPlusLambda(), +			new TournamentSelection()}; +	private EvolutionaryAlgorithm evolutionaryAlgorithm;  	// fitness evaluators  	private FitnessFunction[] fitnessFunctions = new FitnessFunction[] {  			new TestCaseEvaluator() }; -	private FitnessFunction fitnessFunction = fitnessFunctions[0]; - -	// the population of chromosomes	 -	private Population population = new Population(resources); - +	private FitnessFunction fitnessFunction; +	/* +	 * the population of chromosomes +	 */ +	private Population population; +	  	public JCGP() { +		population = new Population(resources); + +		evolutionaryAlgorithm = evolutionaryAlgorithms[0]; +		 +		mutator = mutators[0]; +		 +		fitnessFunction = fitnessFunctions[0]; +		  		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); @@ -301,4 +315,48 @@ public class JCGP {  		return fitnessFunction;  	} +	 +	/** +	 * @param mutator the mutator to set +	 */ +	public void setMutator(int index) { +		this.mutator = mutators[index]; +	} + + +	/** +	 * @param evolutionaryAlgorithm the evolutionaryAlgorithm to set +	 */ +	public void setEvolutionaryAlgorithm(int index) {		 +		this.evolutionaryAlgorithm = evolutionaryAlgorithms[index]; +	} + + +	/** +	 * @param fitnessFunction the fitnessFunction to set +	 */ +	public void setFitnessFunction(int index) { +		this.fitnessFunction = fitnessFunctions[index]; +	} +	 +	public void nextGeneration() { +		resources.set("currentGen", resources.getInt("currentGen") + 1); +		 +		fitnessFunction.evaluate(population, resources); +		evolutionaryAlgorithm.evolve(population, mutator, resources); +		 +		if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 6) { +			System.out.println("solution found"); +			evolutionaryAlgorithm.getFittestChromosome().printNodes(); +		} else { +			System.out.println("Generation: " + resources.getInt("currentGen") + ", fitness: " + evolutionaryAlgorithm.getFittestChromosome().getFitness()); +		} +	} +	 +	public void start() {		 +		while (resources.getInt("currentGen") <= resources.getInt("generations")) { +			nextGeneration(); +		} +		// TODO print something? +	}  }  | 
