EvolutionState implements Singleton { public void setup(final EvolutionState state, final Parameter base) { Parameter p; data = new HashMap[random.length]; for(int i = 0; i < data.length; i++) data[i] = new HashMap(); p = new Parameter(P_CHECKPOINT); checkpoint = parameters.getBoolean(p,null,false); p = new Parameter(P_CHECKPOINTPREFIX); checkpointPrefix = parameters.getString(p,null); if (checkpointPrefix==null) { Parameter p2 = new Parameter(P_CHECKPOINTPREFIX_OLD); checkpointPrefix = parameters.getString(p2,null); if (checkpointPrefix==null) { output.fatal("No checkpoint prefix specified.",p); } else { output.warning("The parameter \"prefix\" is deprecated. Please use \"checkpoint-prefix\".", p2); } } else { Parameter p2 = new Parameter(P_CHECKPOINTPREFIX_OLD); if (parameters.getString(p2,null) != null) { output.warning("You have BOTH the deprecated parameter \"prefix\" and its replacement \"checkpoint-prefix\" defined. The replacement will be used, Please remove the \"prefix\" parameter.", p2); } } p = new Parameter(P_CHECKPOINTMODULO); checkpointModulo = parameters.getInt(p,null,1); if (checkpointModulo==0) output.fatal("The checkpoint modulo must be an integer >0.",p); p = new Parameter(P_CHECKPOINTDIRECTORY); if (parameters.exists(p, null)) { checkpointDirectory = parameters.getFile(p,null); if (checkpointDirectory==null) output.fatal("The checkpoint directory name is invalid: " + checkpointDirectory, p); if (!checkpointDirectory.isDirectory()) output.fatal("The checkpoint directory location is not a directory: " + checkpointDirectory, p); } else checkpointDirectory = null; p = new Parameter(P_EVALUATIONS); if (parameters.exists(p, null)) { numEvaluations = parameters.getInt(p, null, 1); if (numEvaluations <= 0) output.fatal("If defined, the number of evaluations must be an integer >= 1", p, null); } p = new Parameter(P_GENERATIONS); if (parameters.exists(p, null)) { numGenerations = parameters.getInt(p, null, 1); if (numGenerations <= 0) output.fatal("If defined, the number of generations must be an integer >= 1.", p, null); } if (numEvaluations != UNDEFINED && numGenerations != UNDEFINED) { state.output.warning("Both generations and evaluations defined: whichever happens first is when ECJ will stop."); } else if (numEvaluations == UNDEFINED && numGenerations == UNDEFINED) output.fatal("Either evaluations or generations must be defined.", new Parameter(P_GENERATIONS), new Parameter(P_EVALUATIONS)); p=new Parameter(P_QUITONRUNCOMPLETE); quitOnRunComplete = parameters.getBoolean(p,null,false); p=new Parameter(P_INITIALIZER); initializer = (Initializer) (parameters.getInstanceForParameter(p,null,Initializer.class)); initializer.setup(this,p); p=new Parameter(P_FINISHER); finisher = (Finisher) (parameters.getInstanceForParameter(p,null,Finisher.class)); finisher.setup(this,p); p=new Parameter(P_BREEDER); breeder = (Breeder) (parameters.getInstanceForParameter(p,null,Breeder.class)); breeder.setup(this,p); p=new Parameter(P_EVALUATOR); evaluator = (Evaluator) (parameters.getInstanceForParameter(p,null,Evaluator.class)); evaluator.setup(this,p); p=new Parameter(P_STATISTICS); statistics = (Statistics) (parameters.getInstanceForParameterEq(p,null,Statistics.class)); statistics.setup(this,p); p=new Parameter(P_EXCHANGER); exchanger = (Exchanger) (parameters.getInstanceForParameter(p,null,Exchanger.class)); exchanger.setup(this,p); p=new Parameter(P_INNOVATIONNUMBER); innovationNumber = parameters.getLong(p, null, Long.MIN_VALUE); generation = 0; } EvolutionState(); void setup(final EvolutionState state, final Parameter base); void resetFromCheckpoint(); void finish(int result); void startFromCheckpoint(); void startFresh(); int evolve(); void incrementEvaluations(int val); void run(int condition); public ParameterDatabase parameters; public MersenneTwisterFast[] random; public HashMap[] data; public Output output; public int breedthreads; public int evalthreads; public boolean checkpoint; public File checkpointDirectory; public String checkpointPrefix; public int checkpointModulo; public int randomSeedOffset; public boolean quitOnRunComplete; public Object[] job; public String[] runtimeArguments; static final int UNDEFINED; public int generation; public int evaluations; public int numGenerations; public long numEvaluations; public Population population; public Initializer initializer; public Finisher finisher; public Breeder breeder; public Evaluator evaluator; public Statistics statistics; public Exchanger exchanger; public long innovationNumber; final static int C_STARTED_FRESH; final static int C_STARTED_FROM_CHECKPOINT; final static int R_SUCCESS; final static int R_FAILURE; final static int R_NOTDONE; final static String P_INITIALIZER; final static String P_FINISHER; final static String P_BREEDER; final static String P_EVALUATOR; final static String P_STATISTICS; final static String P_EXCHANGER; final static String P_GENERATIONS; static final String P_EVALUATIONS; final static String P_QUITONRUNCOMPLETE; final static String P_CHECKPOINTPREFIX; final static String P_CHECKPOINTMODULO; final static String P_CHECKPOINTDIRECTORY; final static String P_CHECKPOINT; final static String P_INNOVATIONNUMBER; }
@Test public void testSetup() { instance.setup(instance, null); assertEquals(instance.checkpointModulo, 1); assertEquals(instance.checkpointPrefix, "ec"); assertEquals(instance.numGenerations, 100); assertTrue(instance.initializer instanceof StubInitializer); assertTrue(instance.finisher instanceof StubFinisher); assertTrue(instance.breeder instanceof StubBreeder); assertTrue(instance.evaluator instanceof StubEvaluator); assertTrue(instance.statistics instanceof TestStatistics); assertTrue(instance.exchanger instanceof StubExchanger); }