diff options
Diffstat (limited to 'src/jcgp/JCGP.java')
| -rw-r--r-- | src/jcgp/JCGP.java | 171 | 
1 files changed, 31 insertions, 140 deletions
| diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 898f229..f60fc22 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -1,13 +1,17 @@  package jcgp;  import java.io.File; -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.Set;  import jcgp.backend.modules.es.EvolutionaryStrategy; +import jcgp.backend.modules.es.MuPlusLambda; +import jcgp.backend.modules.es.TournamentSelection; +import jcgp.backend.modules.mutator.FixedPointMutator;  import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.modules.mutator.PercentPointMutator; +import jcgp.backend.modules.mutator.ProbabilisticMutator; +import jcgp.backend.modules.problem.DigitalCircuitProblem;  import jcgp.backend.modules.problem.Problem; +import jcgp.backend.modules.problem.SymbolicRegressionProblem;  import jcgp.backend.modules.problem.TestCaseProblem;  import jcgp.backend.parsers.ChromosomeParser;  import jcgp.backend.parsers.FunctionParser; @@ -19,8 +23,6 @@ import jcgp.backend.resources.ModifiableResources;  import jcgp.backend.resources.Resources;  import jcgp.backend.statistics.StatisticsLogger; -import org.reflections.Reflections; -  /**   *    * Top-level JCGP class. This class is the entry point for a CGP experiment.  @@ -51,21 +53,30 @@ public class JCGP {  	 * module, 0 by default.  	 */  	// mutators -	private ArrayList<Mutator> mutators; +	private Mutator[] mutators = new Mutator[] { +			new PercentPointMutator(resources), +			new FixedPointMutator(resources), +			new ProbabilisticMutator(resources) +	};  	private Mutator mutator;  	// evolutionary algorithms -	private ArrayList<EvolutionaryStrategy> evolutionaryStrategies; +	private EvolutionaryStrategy[] evolutionaryStrategies = new EvolutionaryStrategy[] { +			new MuPlusLambda(resources), +			new TournamentSelection(resources) +	};  	private EvolutionaryStrategy evolutionaryStrategy;  	// problem types -	private ArrayList<Problem> problems; +	private Problem[] problems = new Problem[] { +			new DigitalCircuitProblem(resources), +			new SymbolicRegressionProblem(resources) +	};  	private Problem problem;  	private Population population;  	private StatisticsLogger statistics = new StatisticsLogger(); -	private Reflections reflections;  	// these record the best results found in the run, in case the runs ends before a perfect solution is found  	private int lastImprovementGeneration = 0, activeNodes = 0; @@ -104,135 +115,15 @@ public class JCGP {  	 * Creates a new instance of JCGP.  	 */  	public JCGP() { -		// prepare reflections instance -		reflections = new Reflections("jcgp"); -		 -		// generate module lists from all encountered valid modules -		createEvolutionaryStrategyList(); -		createMutatorList(); -		createProblemList(); +		// initialise modules +		setEvolutionaryStrategy(0); +		setMutator(0); +		setProblem(0);  		// create a new population  		population = new Population(resources);  	} - -	/** -	 * Iterates through all classes in the classpath and builds a list -	 * of instances of all valid evolutionary strategies found. In order -	 * to be included in this list, a class must implement {@code EvolutionaryStrategy} -	 * and must contain a constructor with a {@code Resources} object as its only argument. -	 */ -	private void createEvolutionaryStrategyList() { -		Set<Class<? extends EvolutionaryStrategy>> esList = reflections.getSubTypesOf(EvolutionaryStrategy.class); -		evolutionaryStrategies = new ArrayList<EvolutionaryStrategy>(); -		 -		// go through all subclasses of EvolutionaryStrategy -		for (Class<? extends EvolutionaryStrategy> esType : esList) { -			Constructor<? extends EvolutionaryStrategy> constructor; -			try { -				constructor = esType.getConstructor(Resources.class); -			} catch (NoSuchMethodException | SecurityException e) { -				// constructor most likely doesnt exist, try next class -				System.out.println("Warning: could not find valid constructor for " + esType.getName() + ", skipping..."); -				continue; -			} -			 -			if (constructor != null) { -				EvolutionaryStrategy es; -				try { -					es = (EvolutionaryStrategy) constructor.newInstance(resources); -				} catch (Exception e) { -					// could not instantiate for some reason, keep going -					System.out.println("Warning: could not instantiate " + esType.getName() + ", skipping..."); -					continue; -				} -				// add it to the list if it was instantiated properly -				evolutionaryStrategies.add(es); -			} -		} -		// choose the initial module -		evolutionaryStrategy = evolutionaryStrategies.get(0); -	} -	/** -	 * Iterates through all classes in the classpath and builds a list -	 * of instances of all valid mutators found. In order to be included  -	 * in this list, a class must implement {@code Mutator} -	 * and must contain a constructor with a {@code Resources} object as its only argument. -	 */ -	private void createMutatorList() { -		Set<Class<? extends Mutator>> mutatorList = reflections.getSubTypesOf(Mutator.class); -		mutators = new ArrayList<Mutator>(); -		 -		// go through all subclasses of Mutator -		for (Class<? extends Mutator> mutatorType : mutatorList) { -			Constructor<? extends Mutator> constructor; -			try { -				constructor = mutatorType.getConstructor(Resources.class); -			} catch (NoSuchMethodException | SecurityException e) { -				// constructor most likely doesnt exist, try next class -				System.out.println("Warning: could not find valid constructor for "  -				+ mutatorType.getName() + ", skipping..."); -				continue; -			} -			 -			if (constructor != null) { -				Mutator mutator; -				try { -					mutator = (Mutator) constructor.newInstance(resources); -				} catch (Exception e) { -					// could not instantiate for some reason, keep going -					System.out.println("Warning: could not instantiate " + mutatorType.getName() + ", skipping..."); -					continue; -				} -				// add it to the list if it was instantiated properly -				mutators.add(mutator); -			} -		} -		// choose the initial module -		mutator = mutators.get(0); -	} -	 -	/** -	 * Iterates through all classes in the classpath and builds a list -	 * of instances of all valid problem types found. In order to be included  -	 * in this list, a class must implement {@code Problem} -	 * and must contain a constructor with a {@code Resources} object as its only argument. -	 */ -	private void createProblemList() { -		Set<Class<? extends Problem>> problemTypes = reflections.getSubTypesOf(Problem.class); -		problems = new ArrayList<Problem>(); -		 -		// go through all subclasses of Problem -		for (Class<? extends Problem> problemType : problemTypes) { -			 -			Constructor<? extends Problem> constructor; -			try { -				constructor = problemType.getConstructor(Resources.class); -			} catch (NoSuchMethodException | SecurityException e) { -				// constructor most likely doesnt exist, try next class -				System.out.println("Warning: could not find valid constructor for "  -				+ problemType.getName() + ", skipping..."); -				continue; -			} -			 -			if (constructor != null) { -				Problem problem; -				try { -					problem = (Problem) constructor.newInstance(resources); -				} catch (Exception e) { -					// could not instantiate for some reason, keep going -					System.out.println("Warning: could not instantiate " + problemType.getName() + ", skipping..."); -					continue; -				} -				// add it to the list if it was instantiated properly -				problems.add(problem); -			} -		} -		// choose the initial module -		problem = problems.get(0); -		resources.setFunctionSet(problem.getFunctionSet()); -	}  	/**  	 * Returns a reference to the {@code ModifiableResources} used by the @@ -256,7 +147,7 @@ public class JCGP {  	/**  	 * @return a complete list of the experiment's mutators.  	 */ -	public ArrayList<Mutator> getMutators() { +	public Mutator[] getMutators() {  		return mutators;  	} @@ -272,7 +163,7 @@ public class JCGP {  	/**  	 * @return a complete list of the experiment's evolutionary strategies.  	 */ -	public ArrayList<EvolutionaryStrategy> getEvolutionaryStrategies() { +	public EvolutionaryStrategy[] getEvolutionaryStrategies() {  		return evolutionaryStrategies;  	} @@ -288,7 +179,7 @@ public class JCGP {  	/**  	 * @return a complete list of the experiment's problem types.  	 */ -	public ArrayList<Problem> getProblems() { +	public Problem[] getProblems() {  		return problems;  	} @@ -305,7 +196,7 @@ public class JCGP {  	 * @param index the index of the desired mutator.  	 */  	public void setMutator(int index) { -		this.mutator = mutators.get(index); +		this.mutator = mutators[index];  		resources.println("[CGP] Mutator selected: " + mutator.toString());  	} @@ -314,7 +205,7 @@ public class JCGP {  	 * @param index the index of the desired evolutionary strategy.  	 */  	public void setEvolutionaryStrategy(int index) {	 -		this.evolutionaryStrategy = evolutionaryStrategies.get(index); +		this.evolutionaryStrategy = evolutionaryStrategies[index];  		resources.println("[CGP] Evolutionary strategy selected: " + evolutionaryStrategy.toString());  	} @@ -323,7 +214,7 @@ public class JCGP {  	 * @param index the index of the desired problem type.  	 */  	public void setProblem(int index) { -		this.problem = problems.get(index); +		this.problem = problems[index];  		resources.setFunctionSet(problem.getFunctionSet());  		resources.setFitnessOrientation(problem.getFitnessOrientation());  	} @@ -348,7 +239,7 @@ public class JCGP {  			if (resources.currentGeneration() < resources.generations()) {  				// we still have generations left to go -				int perfect = problem.perfectSolutionFound(population); +				int perfect = problem.hasPerfectSolution(population);  				if (perfect >= 0) {  					// log results  					statistics.logRun(resources.currentGeneration(), population.get(perfect).getFitness(), population.get(perfect).getActiveNodes().size(), true); | 
