Пример #1
0
        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();
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
 public ESSolution(PexEvolutionStrategyArithmeticSolver solver, IArithmeticModel model, int variableCount)
 {
     this.solver        = solver;
     this.currentModel  = model;
     this.stdDeviations = new double[variableCount];
     this.fitness       = Double.MaxValue;
 }
Пример #4
0
 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;
 }
Пример #5
0
 public void Dispose()
 {
     if (this.currentModel != null)
     {
         this.currentModel.Dispose();
         this.currentModel = null;
     }
 }
Пример #6
0
 public void UpdateModel(IArithmeticModel model)
 {
     if (this.currentModel != null)
     {
         this.currentModel.Dispose();
         this.currentModel = null;
     }
     this.currentModel = model;
 }
Пример #7
0
        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);
            }
        }
Пример #8
0
 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);
     }
 }
Пример #9
0
 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;
 }
Пример #10
0
        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);
            }
        }
Пример #11
0
        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);
        }
Пример #12
0
        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);
        }
Пример #13
0
        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);
        }
Пример #14
0
        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);
            }
        }
Пример #15
0
        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);
        }
Пример #16
0
        public override TryGetModelResult TryGetArithmeticModel(IArithmeticSolvingContext context, out IArithmeticModel model)
        {
            InitializeCustomSolver(context);

            this.bestModel = context.InitialModel;

            ResetExplorationParameters(true);

            TryGetModelResult result = StartSearch(out model);

            return(result);
        }
Пример #17
0
        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);
        }
Пример #18
0
 public abstract TryGetModelResult TryGetArithmeticModel(IArithmeticSolvingContext context, out IArithmeticModel model);
Пример #19
0
        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);
        }
Пример #20
0
 public void PrintArithmeticModel(ref IArithmeticModel model)
 {
     PrintArithmeticModel(ref model, null, null);
 }
Пример #21
0
        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);
        }
Пример #22
0
 protected void UpdateModelBuilder(ref IArithmeticModel model)
 {
     this.modelBuilder = this.context.CreateArithmeticModelBuilder(model);
 }