package iterators; import java.io.*; import main.BetterTokenizer; import java.lang.*; import java.util.*; import stoppers.SimpleStop; import main.ModelRunner; import main.GeneralInput; import parameters.ParameterSet; import initialconditions.InitialConditionSet; import initialconditions.InitialConditionStorage; // TO DO: add scoring weights capability; not sure if I shoud add this to InitialConditionSet or this object public class ICVaryingFunction extends Function { Vector ICSets = new Vector(); final int MAX_MODE=1, ADD_MODE=2, AVG_MODE=3, COMB_MODE=4; float accumulatedScore=0.0f; float worstScore=0.0f; int scoringMode=COMB_MODE; boolean verbose=false; public ICVaryingFunction() { } // no arg constructor for instantiating by name public ICVaryingFunction(ModelRunner run) { // initializing constructor this.init(run); } public void init(ModelRunner run) { runner = run; } public void setStopper(SimpleStop stopper) { runner.setStopper(stopper); } public float evaluate(ParameterSet pars) throws Exception { pars.setModel(); // temporarily set model parameters accumulatedScore = worstScore = 0f; int i=0; InitialConditionStorage tempICs = new InitialConditionStorage(runner.getModel());//ELI 9/14 //ELI 9/14 InitialConditionSet tempICSet = new InitialConditionSet(); //ELI 9/14 tempICSet.setFrom(runner.getCells()); for(Enumeration enum = ICSets.elements(); enum.hasMoreElements();) { i++; InitialConditionSet thisIC = (InitialConditionSet)enum.nextElement(); if(verbose) System.out.print("ICVaryingFunction trying set " + i + ";"); thisIC.setModel(runner.getCells()); runner.startRun(); try { while(runner.running) { Thread.sleep(200); } } catch (InterruptedException e) {} float thisScore = runner.stopper.getScore(); if(verbose) System.out.println('\t' + "got score of " + thisScore); accumulatedScore += thisScore; if(thisScore > worstScore) worstScore = thisScore; } //ELI 9/14 tempICSet.setModel(runner.getCells()); tempICs.setInitialConditions(runner.getModel()); float finalScore = 0f; if(scoringMode == ADD_MODE) finalScore = accumulatedScore; else if(scoringMode == MAX_MODE) finalScore = worstScore; else if(scoringMode == AVG_MODE) finalScore = (accumulatedScore / i); else finalScore = (accumulatedScore / (1 + accumulatedScore)); if(verbose) System.out.println("ICVaryingFunction final score = " + finalScore); return finalScore; } public void loadParameters(BetterTokenizer tokenizer) throws Exception { super.loadParameters(tokenizer); } protected void loadParameter(String info, BetterTokenizer tokenizer) throws Exception { if(info.equals("ScoringMode")) { GeneralInput.nextToken(tokenizer); String mode = tokenizer.sval; if(mode.equals("Max")) scoringMode = MAX_MODE; else if(mode.equals("Add")) scoringMode = ADD_MODE; else if(mode.equals("Average")) scoringMode = AVG_MODE; else if(mode.equals("Combination")) scoringMode = COMB_MODE; } else if(info.equals("InitialConditions")) { InitialConditionSet newICSet = new InitialConditionSet(); newICSet.loadParameters(tokenizer, null); ICSets.addElement(newICSet); } else if(info.equals("Verbose")) verbose = true; } }