package stoppers; import java.lang.*; import java.io.*; import main.BetterTokenizer; import java.math.*; import main.Cell; import main.GeneralInput; import main.Model; public class MetaFPStripeStop extends SimpleStop { FPStripeStop [] stoppers = new FPStripeStop[20]; int numStoppers=0; float sMin=0.5f; // higher is better float sFloor = 0.1f; int numEvalSteps = 1; // how many final steps to compute a score from boolean evaluating = false; int evalCount = 0; static int ABSOLUTESTABILITY = 0, HOWMANYON = 1, HOWMANYONANDSTABLE = 2, SCORETHRESHOLD = 3; int passCriterion = SCORETHRESHOLD; float stabilityThreshold = 0.1f; public MetaFPStripeStop() {} protected void loadParameter(String info, BetterTokenizer tokenizer) throws Exception { if(info.equals("NumberEvaluationSteps")) { GeneralInput.nextToken(tokenizer); numEvalSteps = (int)tokenizer.nval; } else if(info.equals("StripeFloor")) { GeneralInput.nextToken(tokenizer); sFloor = (float)tokenizer.nval; updateStoppers();} else if(info.equals("StripeThreshold")) { GeneralInput.nextToken(tokenizer); sMin = (float)tokenizer.nval; updateStoppers(); } else if(info.equals("PassCriterion")) { if(GeneralInput.nextToken(tokenizer) != BetterTokenizer.TT_WORD) System.out.println("Warning: MetaFPStripeStop got a bad passCriterion. Defaulting to ScoreThreshold"); else if(tokenizer.sval.equals("AbsoluteStability")) passCriterion = ABSOLUTESTABILITY; else if(tokenizer.sval.equals("FractionStoppersPassed")) passCriterion = HOWMANYON; else if(tokenizer.sval.equals("FractionStoppersPassedAndStable")) passCriterion = HOWMANYONANDSTABLE; else passCriterion = SCORETHRESHOLD; } else if(info.equals("StabilityThreshold")) { GeneralInput.nextToken(tokenizer); stabilityThreshold = (float)tokenizer.nval; } else if(info.equals("Stopper")) { FPStripeStop newStopper = new FPStripeStop(); newStopper.init(model); newStopper.loadParameters(tokenizer); newStopper.stopTime = this.stopTime; newStopper.sMin = this.sMin; newStopper.sFloor = this.sFloor; stoppers[numStoppers++] = newStopper; System.out.println("MetaFPStripeStop: new stopper loaded"); } else super.loadParameter(info,tokenizer); } private void updateStoppers() { for(int i = 0; i < numStoppers; i++) { stoppers[i].stopTime = this.stopTime; stoppers[i].sMin = this.sMin; stoppers[i].sFloor = this.sFloor; } } public boolean didPass() { if(passCriterion == ABSOLUTESTABILITY) { for(int i = 0; i < numStoppers; i++) { if(stoppers[i].getHowStable()/numEvalSteps > stabilityThreshold) return false; } return true; } else if(passCriterion == HOWMANYON) { float hmo = 0f; for(int i = 0; i < numStoppers; i++) { if(stoppers[i].didPass()) hmo += 1f; } if((1 - hmo/numStoppers) <= Cutoff) return true; else return false; } else if(passCriterion == HOWMANYONANDSTABLE) { float hmo = 0f; for(int i = 0; i < numStoppers; i++) { if(stoppers[i].getHowStable() > stabilityThreshold) if(stoppers[i].didPass()) hmo += 1f; } if((1 - hmo/numStoppers) <= Cutoff) return true; else return false; } else { if(score < Cutoff) return true; else return false; } } public boolean stop(float time) { if(time > stopTime && !evaluating) { // start evaluating evaluating = true; for(int i = 0; i < numStoppers; i++) stoppers[i].setEvaluating(true); evalCount = numEvalSteps; return false; } if(evaluating) { // compute next addition to score if(evalCount > 0) { score = 0; for(int i = 0; i < numStoppers; i++) { score += stoppers[i].computeStripeScore(); } score = score/numStoppers; cumScore += score; evalCount--; return false; } else { // evaluation phase done score = cumScore/numEvalSteps; evaluating = false; for(int i = 0; i < numStoppers; i++) stoppers[i].setEvaluating(false); return true; } } else // time < stopTime return false; } public void reset() { for(int i = 0; i < numStoppers; i++) stoppers[i].reset(); score = 0f; evalCount = 0; evaluating = false; cumScore=0f; lastTime=0f; } public void initCells() { for(int i = 0; i < numStoppers; i++) stoppers[i].initCells(); } }