package iterators; import java.io.*; import main.BetterTokenizer; import java.lang.*; import java.util.*; import stoppers.SimpleStop; import main.ModelRunner; import main.GeneralInput; import main.Cell; import parameters.ParameterSet; import initialconditions.InitialCondition; import initialconditions.InitialConditionSet; import initialconditions.InitialConditionStorage; public class ICSamplingFunction extends Function { final int MAX_MODE=1, ADD_MODE=2, AVG_MODE=3, COMB_MODE=4, THRESHHOLD_MODE=5; final int RANDOM=1, SCANNING=2, NOISY=3, DIFFUSION=4; float accumulatedScore=0.0f; float worstScore=0.0f; int numAboveThreshhold=0; int numEvaluations=0; int scoringMode=AVG_MODE; int samplingMode=RANDOM; boolean verbose=false; boolean debug=false; int samplesPerAxis=10; int numSamples=1000; float noiseLevel=0.1f; float threshhold=0.2f; Random rng; Vector ICs = new Vector(); Vector BGs = new Vector(); public ICSamplingFunction() { } // no arg constructor for instantiating by name public ICSamplingFunction(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 { numEvaluations = 0; rng = new Random(); InitialCondition ic; pars.setModel(); accumulatedScore = worstScore = 0f; InitialConditionStorage tempICs = new InitialConditionStorage(runner.getModel()); // treats the initial conditions in the network input file as a "ground state" and stores it if(samplingMode == RANDOM) { for(int i = 0; i < numSamples; i++) { for(Enumeration enum = BGs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); float newValue = ic.getLowerBound() + ((ic.getUpperBound() - ic.getLowerBound()) * rng.nextFloat()); ic.changeValue(newValue); } for(Enumeration enum = ICs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); float newValue = ic.getLowerBound() + ((ic.getUpperBound() - ic.getLowerBound()) * rng.nextFloat()); ic.changeValue(newValue); } evaluateIt(); } } else if(samplingMode == SCANNING) { int i = 0; int numBGs = BGs.size(); int numICs = ICs.size(); int numAxes = numICs + numBGs; float mins[] = new float[numAxes]; float maxs[] = new float[numAxes]; float vals[] = new float[numAxes]; float incr[] = new float[numAxes]; for(Enumeration enum = BGs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); mins[i] = ic.getLowerBound(); maxs[i] = ic.getUpperBound(); vals[i] = mins[i]; incr[i] = (maxs[i] - mins[i]) / samplesPerAxis; i++; } for(Enumeration enum = ICs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); mins[i] = ic.getLowerBound(); maxs[i] = ic.getUpperBound(); vals[i] = mins[i]; incr[i] = (maxs[i] - mins[i]) / samplesPerAxis; i++; } boolean done = false; do { i = 0; for(Enumeration enum = BGs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); ic.changeValue(vals[i++]); } for(Enumeration enum = ICs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); ic.changeValue(vals[i++]); } evaluateIt(); i = 0; vals[i] += incr[i]; while(vals[i] > maxs[i] && i < numAxes) { vals[i] = mins[i]; i++; if(i < numAxes) vals[i] += incr[i]; } if(i == numAxes) done = true; } while(!done); } else if(samplingMode == NOISY) { for(int i = 0; i < numSamples; i++) { // nothing to do; initial conditions should be set up to generate a certain noise level // later I will add a check to make sure that they have been evaluateIt(); } } else if(samplingMode == DIFFUSION) { System.out.println("ICSamplingFunction: Diffusion mode not implemented yet"); // can't remember anymore what this mode was going to do } tempICs.setInitialConditions(runner.getModel()); // set everything back to "ground state" float finalScore = 0f; if(scoringMode == ADD_MODE) finalScore = accumulatedScore; else if(scoringMode == MAX_MODE) finalScore = worstScore; else if(scoringMode == AVG_MODE) finalScore = (accumulatedScore / numEvaluations); else if(scoringMode == COMB_MODE) finalScore = (accumulatedScore / (1 + accumulatedScore)); else finalScore = numAboveThreshhold / numEvaluations; if(verbose || debug) System.out.println("ICVaryingFunction final score = " + finalScore); return finalScore; } private void evaluateIt() { if(verbose || debug) System.out.println("Evaluation #" + (numEvaluations+1)); setModel(runner.getCells()); runner.startRun(); try { while(runner.running) { Thread.sleep(200); } } catch (InterruptedException e) {} float thisScore = runner.stopper.getScore(); if(verbose || debug) System.out.println('\t' + "got score of " + thisScore); accumulatedScore += thisScore; if(thisScore > worstScore) worstScore = thisScore; if(thisScore > threshhold) numAboveThreshhold++; numEvaluations++; } public void setModel(Cell [] cells) { // IMPORTANT NOTE: this approach assumes that the pre-existing initial conditions // stored in the nodes are an appropriate "ground state", and simply overwrites // the specified initial conditions. InitialCondition ic; // Set background levels first for(Enumeration enum = BGs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); ic.setModel(cells); if(debug) ic.print(); } // Now do specific conditions for(Enumeration enum = ICs.elements(); enum.hasMoreElements();) { ic = (InitialCondition)enum.nextElement(); ic.setModel(cells); if(debug) ic.print(); } } 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 scoringMode = THRESHHOLD_MODE; } else if(info.equals("SamplingMode")) { GeneralInput.nextToken(tokenizer); String mode = tokenizer.sval; if(mode.equals("Random")) samplingMode = RANDOM; else if(mode.equals("Scanning")) samplingMode = SCANNING; else if(mode.equals("Noisy")) samplingMode = NOISY; else if(mode.equals("Diffusion")) samplingMode = DIFFUSION; } else if(info.equals("InitialConditions")) { GeneralInput.indent++; info = GeneralInput.findNextIDToken(tokenizer); while(!info.equals("endInitialConditions")) { loadICs(info, tokenizer); info = GeneralInput.findNextIDToken(tokenizer); } GeneralInput.indent--; } else if(info.equals("Verbose")) verbose = true; else if(info.equals("Debug")) debug = true; else if(info.equals("SamplesPerAxis")) { GeneralInput.nextToken(tokenizer); samplesPerAxis = (int)tokenizer.nval; } else if(info.equals("SampleNumber")) { GeneralInput.nextToken(tokenizer); numSamples = (int)tokenizer.nval; } else if(info.equals("NoiseLevel")) { GeneralInput.nextToken(tokenizer); noiseLevel = (float)tokenizer.nval; } else if(info.equals("Threshhold")) { GeneralInput.nextToken(tokenizer); threshhold = (float)tokenizer.nval; } } protected void loadICs(String init_name, BetterTokenizer tokenizer) throws Exception { if(init_name.equals("BackgroundLevel")) { InitialCondition newIC = new InitialCondition(); newIC.loadParameters(tokenizer); BGs.addElement(newIC); } else { try { Class c = Class.forName("initialconditions." + init_name); InitialCondition newIC = (InitialCondition)c.newInstance(); newIC.loadParameters(tokenizer); ICs.addElement(newIC); } catch(Exception e) { System.out.println("Problem loading initial condition " + init_name + ":" + e.toString()); } } } }