package iterators; import java.io.*; import main.BetterTokenizer; import java.lang.*; import main.Network; import main.Model; import stoppers.SimpleStop; import main.ModelRunner; import main.Experiment; import main.GeneralInput; import parameters.ParameterSet; import affectors.Affector; public class UberIteratorFunction extends Function { static final int RANDOM_RANGE=0,FROM_FILE_LIST=1; static final int SAVE_START=0, SAVE_END=1, SAVE_BOTH=2, NO_SAVE= -1; Object [] objects; // each object must implement interface Evaluator ParameterSet fullState = new ParameterSet("fullstate"); String [] IDTags; String [] evalTypes; // MODIFICATION emm 5/31 These are only used to set integration mode for stoppers and experiments. Iterators do it through LoadParameters String [] integrationMode; float [] FPStabilizers; // END OF MODIFICATION int [] savePars; boolean [] startAtEnd; boolean [] autoPass; int numObjects; int runmode; int goodcount = 0; PrintWriter ps; public UberIteratorFunction(ModelRunner run, PrintWriter p) { super(run); fullState = run.getModel().getModelParameterSet().copy(); objects = new Object[10]; evalTypes = new String[10]; IDTags = new String[10]; integrationMode = new String[10]; savePars = new int[10]; startAtEnd = new boolean[10]; autoPass = new boolean[10]; FPStabilizers = new float[10]; numObjects = 0; this.ps = p; } public void setRunMode(int rm) { runmode = rm; } public void setOutputStream(PrintWriter ps) { this.ps = ps; } // A total kludge to avoid an annoying bug (Ed) public void setFPStabilizer(float fps) { FPStabilizers[numObjects] = fps; } public void addObject(Network network, Model model, BetterTokenizer tokenizer, String eval_type, String class_name, String id_tag, String save_pars, String start_where, boolean auto_pass, String int_mode) throws Exception { if(numObjects == 10) throw new Exception("UberIteratorFunction.addObjects: current number of objects is " + (numObjects + 1) + "\nYou must go back and change array sizes in CooptimizerFunction"); if(eval_type.equals("Stopper")) { Class c = Class.forName("stoppers." + class_name); SimpleStop obj = (SimpleStop)c.newInstance(); obj.init(model); obj.loadParameters(tokenizer); objects[numObjects] = obj; System.out.println(id_tag + ":\t" + class_name + " loaded"); } else if(eval_type.equals("Iterator")) { Class c = Class.forName("iterators." + class_name); ModelIterator obj = (ModelIterator)c.newInstance(); obj.init(network, model); obj.loadParameters(tokenizer); obj.setPrintStream(ps); objects[numObjects] = obj; System.out.println(id_tag + ":\t" + class_name + " loaded"); } else if(eval_type.equals("Experiment")) { Class c = Class.forName("main." + class_name); Experiment obj = (Experiment)c.newInstance(); obj.init(model); obj.loadParameters(tokenizer); objects[numObjects] = obj; System.out.println(id_tag + ":\t" + class_name + " loaded"); } else if(eval_type.equals("Cooptimize")) { eval_type = "Iterator"; Class c = Class.forName("iterators." + class_name); ModelIterator obj = (ModelIterator)c.newInstance(); obj.init(network, model); obj.loadParameters(tokenizer); CooptimizerFunction fun = new CooptimizerFunction(new ModelRunner(model, new SimpleStop())); // make a new cooptimizer function String e_type = GeneralInput.findNextIDToken(tokenizer); String c_name = ""; while(!e_type.equals("end" + id_tag)) { // load in cooptimizer elements if(e_type.equals("Experiment")) c_name = "Experiment"; else c_name = GeneralInput.nextWordToken(tokenizer); fun.addObject(model,tokenizer,e_type,c_name); e_type = GeneralInput.findNextIDToken(tokenizer); } obj.setFunction(fun); // add function to iterator objects[numObjects] = obj; System.out.println(id_tag + ":\tcooptimizer loaded with iterator " + class_name); } else throw new Exception("UberIteratorFunction:\t Got a bad evaluator type" + eval_type); evalTypes[numObjects] = eval_type; IDTags[numObjects] = id_tag; integrationMode[numObjects] = int_mode; if(save_pars.equals("Savestartingpars")) savePars[numObjects] = SAVE_START; else if(save_pars.equals("Savefinalpars")) savePars[numObjects] = SAVE_END; else if(save_pars.equals("Saveboth")) savePars[numObjects] = SAVE_BOTH; else { savePars[numObjects] = NO_SAVE; System.out.println("WARNING: You are not saving anything from " + eval_type + " " + class_name); } if(start_where.equals("StartAtEnd")) startAtEnd[numObjects] = true; else startAtEnd[numObjects] = false; autoPass[numObjects++] = auto_pass; } public void addIterator(ModelIterator newIT, String eval_type,String class_name, String id_tag, String save_pars, String start_where, boolean auto_pass) { objects[numObjects] = newIT; evalTypes[numObjects] = eval_type; IDTags[numObjects] = id_tag; if(save_pars.equals("Savestartingpars")) savePars[numObjects] = SAVE_START; else if(save_pars.equals("Savefinalpars")) savePars[numObjects] = SAVE_END; else if(save_pars.equals("Saveboth")) savePars[numObjects] = SAVE_BOTH; else savePars[numObjects] = NO_SAVE; if(start_where.equals("StartAtEnd")) startAtEnd[numObjects] = true; else startAtEnd[numObjects] = false; autoPass[numObjects++] = auto_pass; } /** Save out a list of all the types of data that might be output together with a good parameter set. This list goes in the header of the cam file. */ protected void saveOutputTags() { ps.println("&ValueFieldNames"); for(int i = 0; i < numObjects; i++) { if(evalTypes[i].equals("Iterator")) { ((ModelIterator) objects[i]).saveOutputTags(ps, "\t"); } else if(evalTypes[i].equals("Stopper")) { ((SimpleStop)objects[i]).saveOutputTags(ps, "\t"); } } ps.println("&endValueFieldNames"); } public float evaluate(ParameterSet pars) throws Exception { int nfc = 0; float theScore = 0; boolean firstsave = true; boolean pass = false; int i = 0; fullState.getModel(); fullState.setFrom(pars); ParameterSet init_pars = new ParameterSet("fullmodel"); pars.setPass(true); while(pars.didPass() && i < numObjects) { if(i > 0 && !startAtEnd[i]) { fullState.setFrom(init_pars); } else { init_pars.setFrom(fullState); } if(evalTypes[i].equals("Stopper")) { // this is a simple case SimpleStop theStopper = (SimpleStop) objects[i]; fullState.setModel(); runner.setRunMode(integrationMode[i]); // runner.setFPStabilizer(FPStabilizers[i]); runner.setStopper((theStopper)); runner.doStartRun(); runner.doRun(); theScore = theStopper.getScore(); if(autoPass[i]) pass = true; else pass = theStopper.didPass(); System.out.println(IDTags[i] + ": " + theScore); } else if(evalTypes[i].equals("Experiment")) { // this is a simple case Experiment theExperiment = (Experiment) objects[i]; fullState.setModel(); theExperiment.startExperiment(); runner.setRunMode(integrationMode[i]); // runner.setFPStabilizer(FPStabilizers[i]); runner.setStopper(theExperiment.stopper); runner.doStartRun(); runner.doRun(); theExperiment.endExperiment(); theScore = theExperiment.stopper.getScore(); if(autoPass[i]) pass = true; else pass = theExperiment.stopper.didPass(); } else { // must be an iterator ModelIterator theIterator = (ModelIterator) objects[i]; fullState.setModel(); theIterator.setState(fullState); // pass full state to iterator //COMEBACK if(((ModelIterator) objects[i]).isSaveExtra()) // ((ModelIterator) objects[i]).ps = new PrintWriter(new FileOutputStream(outFileName), true); // Make a new file for extra output theIterator.doStartRun(); theIterator.doRun(); theIterator.getState(fullState); fullState.setScore(theScore = theIterator.getValue()); fullState.setNumFuncCalls(nfc = theIterator.numFunctionCalls); if(autoPass[i]) pass = true; else pass = theIterator.didPass(); } if(pass) { // if the score was below some Threshold if(savePars[i]>=0) { // save pars if((runmode != FROM_FILE_LIST) && (firstsave == true)) { ps.println("&par" + goodcount); firstsave = false; } ps.println("\t&" + IDTags[i]); if(evalTypes[i].equals("Iterator")) { ((ModelIterator) objects[i]).saveOutput(ps, "\t"); if(((ModelIterator) objects[i]).isSaveExtra()) ((ModelIterator) objects[i]).ps.close(); } else if(evalTypes[i].equals("Stopper")) { ((SimpleStop)objects[i]).saveOutput(ps, "\t"); } if(savePars[i] == SAVE_START || savePars[i] == SAVE_BOTH) { ps.println("\t&IPARS"); init_pars.toString(ps, "\t\t"); ps.println("\t&endIPARS"); } if(savePars[i] == SAVE_END || savePars[i] == SAVE_BOTH) { ps.println("\t&FPARS"); fullState.toString(ps, "\t\t"); ps.println("\t&endFPARS"); } } } else pars.setPass(false); // this parameter set failed i++; } if(i == numObjects) goodcount++; return theScore; } /** This is here to gracefully stop processing when the user stops the iterator before its finished. Currently just calls stopRun for all iterators in its object list that are running. Also calls super.stopRun() to stop its modelRunner. */ public void stopRun() { super.stopRun(); for(int i = 0; i < numObjects; i++) { if(evalTypes[i].equals("Iterator")) { if(((ModelIterator) objects[i]).isRunning()) ((ModelIterator) objects[i]).stopRun(); } } } }