public void PrintArithmeticModel(ref IArithmeticModel model, String preMsg, String postMsg) { OpenFile(); if (preMsg != null) { this.sw.WriteLine(preMsg); } string termVal = ""; foreach (var variable in this.context.Variables) { if (TryGetTermValueAsString(ref model, variable.Term, this.termManager.GetLayout(variable.Term), out termVal)) { this.sw.WriteLine("{0} = {1}", variable.Term, termVal); } else { this.sw.WriteLine("{0} = <unknown>", variable.Term); } } if (postMsg != null) { this.sw.WriteLine(postMsg); } CloseFile(); }
private double GreaterThanOrEqualsDistance(Term left, Term right, ref IArithmeticModel model) { Term diff; if (!TrySubtractTwoTerms(model.GetValue(right), model.GetValue(left), out diff)) { return(Double.MaxValue); } double distance; if (!TryConvertTermToDouble(model.GetValue(diff), out distance)) { return(Double.MaxValue); } if (Double.IsInfinity(distance) || Double.IsNaN(distance) || Double.IsNegativeInfinity(distance) || Double.IsPositiveInfinity(distance)) { return(Double.MaxValue); } else if (distance <= 0.0) { return(0.0); } else { return(distance + this.K); } }
public ESSolution(PexEvolutionStrategyArithmeticSolver solver, IArithmeticModel model, int variableCount) { this.solver = solver; this.currentModel = model; this.stdDeviations = new double[variableCount]; this.fitness = Double.MaxValue; }
public ESSolution(PexEvolutionStrategyArithmeticSolver solver, IArithmeticModel model, double[] stdDeviations, double fitness) { this.solver = solver; this.currentModel = model; this.stdDeviations = new double[stdDeviations.Length]; stdDeviations.CopyTo(this.stdDeviations, 0); this.fitness = fitness; }
public void Dispose() { if (this.currentModel != null) { this.currentModel.Dispose(); this.currentModel = null; } }
public void UpdateModel(IArithmeticModel model) { if (this.currentModel != null) { this.currentModel.Dispose(); this.currentModel = null; } this.currentModel = model; }
private bool Terminate(out TryGetModelResult result, out IArithmeticModel model) { for (int i = 0; i < this.populationSize; i++) { if (base.context.IsValidModel(this.parentPopulation[i].currentModel)) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "es method found solution after {0} fitness evaluations", base.fitnessEvaluations); base.logManager.LogSuccess(base.fitnessEvaluations); } base.modelBuilder = base.context.CreateArithmeticModelBuilder(this.parentPopulation[i].currentModel); model = base.modelBuilder.ToArithmeticModel(); result = TryGetModelResult.Success; Dispose(); return(true); } } if (base.context.HasTimedOut) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "es method timed out after {0} unsuccessful fitness evaluations", base.fitnessEvaluations); base.logManager.LogFailure(); } result = TryGetModelResult.Timeout; model = null; Dispose(); return(true); } else { result = TryGetModelResult.NoModelFound; model = null; if (base.fitnessEvaluations >= base.fitnessBudget) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "es method failed to find a solution after {0} fitness evaluations", base.fitnessEvaluations); base.logManager.LogFailure(); } Dispose(); return(true); } return(false); } }
private bool EvaluateCurrentModel() { this.currentModel = base.modelBuilder.ToArithmeticModel(); this.currentFitness = EvaluateArithmeticModel(ref this.currentModel); if (this.currentFitness < this.bestFitness) { UpdateBestModel(); return(true); } else { return(false); } }
private void Dispose() { if (this.bestModel != null && this.bestModel != base.context.InitialModel) { this.bestModel.Dispose(); this.bestModel = null; } if (this.currentModel != null && this.currentModel != base.context.InitialModel) { this.currentModel.Dispose(); this.currentModel = null; } base.modelBuilder = null; }
private bool Terminate(out TryGetModelResult result, out IArithmeticModel model) { model = base.modelBuilder.ToArithmeticModel(); if (base.context.IsValidModel(model)) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "AVM found solution after {0} fitness evaluations", base.fitnessEvaluations); base.logManager.LogSuccess(base.fitnessEvaluations); } Dispose(); result = TryGetModelResult.Success; return(true); } model.Dispose(); model = null; if (base.context.HasTimedOut) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "AVM timed out after {0} unsuccessful fitness evaluations", base.fitnessEvaluations); base.logManager.LogFailure(); } Dispose(); result = TryGetModelResult.Timeout; return(true); } else if (base.fitnessEvaluations >= base.fitnessBudget) { if (IsLoggingEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "AVM failed to find a solution after {0} fitness evaluations", base.fitnessEvaluations); base.logManager.LogFailure(); } Dispose(); result = TryGetModelResult.NoModelFound; return(true); } else { result = TryGetModelResult.NoModelFound; return(false); } }
protected double EvaluateArithmeticModel(ref IArithmeticModel model) { double distance = 0.0; double fitnessValue = 0.0; Term innerTerm; SafeSet <Term> visited = new SafeSet <Term>(); this.fitnessEvaluations++; foreach (var variable in this.context.Variables) { foreach (var constraint in variable.Constraints) { if (!visited.Add(constraint)) { continue; } if (this.termManager.TryGetInnerLogicallyNegatedValue(constraint, out innerTerm)) { distance = EvaluateConstraint(ref model, innerTerm, true); } else { distance = EvaluateConstraint(ref model, constraint, false); } if (distance == Double.MaxValue) { fitnessValue = distance; break; } else { fitnessValue += distance; } } } visited.ClearAndTrim(); visited = null; return(fitnessValue); }
protected double EvaluateConstraint(ref IArithmeticModel model, Term constraint, bool isNegated) { Term left, right; BinaryOperator @operator; if (this.termManager.TryGetBinary(constraint, out @operator, out left, out right)) { TryDecomposeDecimalComparer(ref model, right, ref left, ref right); if (@operator == BinaryOperator.Ceq) { return(EqualsDistance(left, right, isNegated, ref model)); } else { return(LessThanDistance(left, right, isNegated, ref model)); } } return(this.K); }
private bool TryDecomposeDecimalComparer(ref IArithmeticModel model, Term value, ref Term leftValue, ref Term rightValue) { IFunction f; Term t; Term[] args; TypeEx fdt; if (this.termManager.TryGetFunctionApplication(value, out f, out t, out args) && f.Method != null && f.Method.TryGetDeclaringType(out fdt) && fdt == SystemTypes.Decimal && args.Length == 2) { leftValue = args[0]; rightValue = args[1]; return(true); } return(false); }
private double EqualsDistance(Term left, Term right, bool isNegated, ref IArithmeticModel model) { if (isNegated) { return(NotEqualsDistance(left, right, ref model)); } Term diff; if (!TrySubtractTwoTerms(model.GetValue(left), model.GetValue(right), out diff)) { return(Double.MaxValue); } double distance; if (!TryConvertTermToDouble(model.GetValue(diff), out distance)) { return(Double.MaxValue); } distance = Math.Abs(distance); if (Double.IsInfinity(distance) || Double.IsNaN(distance) || Double.IsNegativeInfinity(distance) || Double.IsPositiveInfinity(distance)) { return(Double.MaxValue); } else if (distance <= 0.0) { return(0.0); } else { return(distance); } }
private TryGetModelResult Evolve(out IArithmeticModel model) { TryGetModelResult result = TryGetModelResult.None; //SafeDebugger.Break(); CreateInitialPopulation(); EvaluateIntermediatePopulation(); for (int i = 0; i < this.populationSize; i++) { this.parentPopulation[i] = this.intermediatePopulation[i].MakeDeepCopy(); } while (!Terminate(out result, out model)) { ClearIntermediatePopulation(); if (this.populationSize > 1 && this.recombination != RecombinationStrategy.None && this.parentPopulation.Length > 1) { ESSolution offspring = null; for (int i = 0; i < this.offspringPopulationSize; i++) { Recombine(ref offspring); if (offspring != null) { Mutate(ref offspring); this.intermediatePopulation.Add(offspring.MakeDeepCopy()); offspring.Dispose(); offspring = null; } } } else { ESSolution offspring = null; int poolIndex = 0; for (int i = 0; i < this.offspringPopulationSize; i++) { if (poolIndex >= this.parentPopulation.Length) { poolIndex = 0; } offspring = this.parentPopulation[poolIndex].MakeDeepCopy(); Mutate(ref offspring); this.intermediatePopulation.Add(offspring.MakeDeepCopy()); offspring.Dispose(); offspring = null; poolIndex++; } } EvaluateIntermediatePopulation(); //selection if (!this.selectFromOffspringOnly) { for (int i = 0; i < this.populationSize; i++) { this.intermediatePopulation.Add(this.parentPopulation[i].MakeDeepCopy()); } } this.intermediatePopulation.Sort(); for (int i = 0; i < this.populationSize && i < this.intermediatePopulation.Count; i++) { this.parentPopulation[i].Dispose(); this.parentPopulation[i] = this.intermediatePopulation[i].MakeDeepCopy(); } } return(result); }
public override TryGetModelResult TryGetArithmeticModel(IArithmeticSolvingContext context, out IArithmeticModel model) { InitializeCustomSolver(context); this.bestModel = context.InitialModel; ResetExplorationParameters(true); TryGetModelResult result = StartSearch(out model); return(result); }
private TryGetModelResult StartSearch(out IArithmeticModel model) { TryGetModelResult result; bool foundImprovingMove = false; //SafeDebugger.Break(); if (!TryBuildVariableVector()) { model = null; return(TryGetModelResult.NoModelFound); } this.bestFitness = 0; EvaluateCurrentModel(); this.bestFitness = this.currentFitness; if (IsLoggingVerboseEnabled) { base.context.Host.Log.LogVerbose(PexLogCategories.ArithmeticSolver, "starting alternating variable method search"); } bool randomizeLocal = true; while (!Terminate(out result, out model)) { if (RequiresRestart()) { if (randomizeLocal) { if (IsLoggingVerboseEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "performing localized random restart after {0} fitness evaluations", base.fitnessEvaluations); } RandomizeFraction(); randomizeLocal = false; } else { if (IsLoggingVerboseEnabled) { base.context.Host.Log.LogMessage(PexLogCategories.ArithmeticSolver, "performing random restart after {0} fitness evaluations", base.fitnessEvaluations); } for (int i = 0; i < this.variableInfos.Count; i++) { var variableInfo = this.variableInfos[i]; var precision = variableInfo.precision; if (precision != 0) { this.variableInfos[i] = new VariableInfo(this.variableInfos[i].variable, 0); } } RandomizeInputVariablesBitConverter(); randomizeLocal = true; } ResetExplorationParameters(true); this.bestFitness = 0; EvaluateCurrentModel(); UpdateBestModel(); this.bestFitness = this.currentFitness; } else { foundImprovingMove = ExploreNeighbourhood(); bool restartExploration = false; bool terminateSearch = false; while (foundImprovingMove && !(terminateSearch = Terminate(out result, out model))) { if (this.increasedPrecision) { this.precisionChangeSuccess = true; } foundImprovingMove = MakePatternMove(); restartExploration = true; } if (result == TryGetModelResult.Success || terminateSearch) { break; } if (restartExploration) { ResetExplorationParameters(true); } ResetModelBuilder(); } } return(result); }
public abstract TryGetModelResult TryGetArithmeticModel(IArithmeticSolvingContext context, out IArithmeticModel model);
private bool TryGetTermValueAsString(ref IArithmeticModel model, Term currentTerm, Layout layout, out string result) { Term term = model.GetValue(currentTerm); if (layout == Layout.I1) { byte bVal; if (this.termManager.TryGetI1Constant(term, out bVal)) { result = Convert.ToString(bVal); return(true); } } else if (layout == Layout.I2) { short sVal; if (this.termManager.TryGetI2Constant(term, out sVal)) { result = Convert.ToString(sVal); return(true); } } else if (layout == Layout.I4) { int iVal; if (this.termManager.TryGetI4Constant(term, out iVal)) { result = Convert.ToString(iVal); return(true); } } else if (layout == Layout.I8) { long lVal; if (this.termManager.TryGetI8Constant(term, out lVal)) { result = Convert.ToString(lVal); return(true); } } else if (layout == Layout.R4) { float fVal; if (this.termManager.TryGetR4Constant(term, out fVal)) { result = fVal.ToString("R"); return(true); } } else if (layout == Layout.R8) { double dVal; if (this.termManager.TryGetR8Constant(term, out dVal)) { result = dVal.ToString("R"); return(true); } } else if (layout.StructType.IsDecimalType) { decimal decValue; if (this.termManager.TryGetDecimalConstant(term, out decValue)) { result = Convert.ToString(decValue); return(true); } } result = null; return(false); }
public void PrintArithmeticModel(ref IArithmeticModel model) { PrintArithmeticModel(ref model, null, null); }
public override TryGetModelResult TryGetArithmeticModel(IArithmeticSolvingContext context, out IArithmeticModel model) { InitializeCustomSolver(context); this.variableCount = context.Variables.ToArray <IArithmeticVariable>().Length; model = null; TryGetModelResult result = (this.variableCount == 0)? TryGetModelResult.NoModelFound:Evolve(out model); return(result); }
protected void UpdateModelBuilder(ref IArithmeticModel model) { this.modelBuilder = this.context.CreateArithmeticModelBuilder(model); }