示例#1
0
        // *** IOptimizer interface implementation ***

        public ArrayList <double> Optimize(double[] initParamVec, IEval eval)
        {
            Utils.ThrowException(initParamVec == null ? new ArgumentNullException("initParamVec") : null);
            Utils.ThrowException(eval == null ? new ArgumentNullException("eval") : null);
            ArrayList <double> paramVec = new ArrayList <double>(initParamVec);

            Utils.ThrowException(paramVec.Count == 0 ? new ArgumentValueException("initParamVec") : null);
            SetInitPopul(paramVec.Count, paramVec.Count * 10); // *** make this multiplier configurable
            mPopul[0] = new Pair <double, ArrayList <double> >(0, paramVec);
            double             bestGlobalVal = double.MinValue;
            ArrayList <double> bestParamVec  = null;

            // evaluate initial population
            for (int i = 0; i < mPopul.Count; i++)
            {
                Pair <double, ArrayList <double> > indiv = mPopul[i];
                double localVal = eval.Eval(indiv.Second);
                mPopul[i] = new Pair <double, ArrayList <double> >(localVal, indiv.Second);
                if (localVal > bestGlobalVal)
                {
                    bestGlobalVal = localVal; bestParamVec = indiv.Second;
                }
            }
            // optimize
            int    numNoChangeIter = 0;
            double bestVal         = bestGlobalVal;

            while (numNoChangeIter < mMinNoChangeIter)
            {
                SetNextPopul(eval, mWgtFactor, mCrossover, ref bestVal, ref bestParamVec);
                if (bestVal > bestGlobalVal)
                {
                    numNoChangeIter = 0; bestGlobalVal = bestVal;
                }
                else
                {
                    numNoChangeIter++;
                }
                mLogger.Info("Optimize", "Iteration status:\r\n" +
                             "No-change iterations: {0} / {1}\r\n" +
                             "Current best solution vector: {2}\r\n" +
                             "Current best solution score:  {3}", numNoChangeIter, mMinNoChangeIter, bestParamVec, bestGlobalVal);
            }
            return(bestParamVec);
        }
示例#2
0
        private void SetNextPopul(IEval eval, double wgtFactor, double crossover, ref double bestVal, ref ArrayList <double> bestParamVec)
        {
            ArrayList <Pair <double, ArrayList <double> > > nextPopul = new ArrayList <Pair <double, ArrayList <double> > >(mPopul.Count);

            // for each individual in the population ...
            foreach (Pair <double, ArrayList <double> > indivInfo in mPopul)
            {
                ArrayList <double> indiv = indivInfo.Second;
                ArrayList <double> rndIndiv1 = null, rndIndiv2 = null, rndIndiv3 = null;
                // randomly select parents
                GetRandomIndiv(indiv, ref rndIndiv1, ref rndIndiv2, ref rndIndiv3);
                // create initial candidate v
                ArrayList <double> v = ComputeInitialCandidate(rndIndiv1, rndIndiv2, rndIndiv3, wgtFactor);
                // create final candidate u
                ArrayList <double> u = indiv.Clone();
                for (int i = 0; i < u.Count; i++)
                {
                    if (mRandom.NextDouble() < crossover)
                    {
                        u[i] = v[i];
                    }
                }
                // accept or reject candidate u
                double newVal = eval.Eval(u);
                if (newVal > indivInfo.First)
                {
                    nextPopul.Add(new Pair <double, ArrayList <double> >(newVal, u));
                    if (newVal > bestVal)
                    {
                        bestVal = newVal; bestParamVec = u;
                    }
                }
                else
                {
                    nextPopul.Add(indivInfo);
                    if (indivInfo.First > bestVal)
                    {
                        bestVal = indivInfo.First; bestParamVec = indivInfo.Second;
                    }
                }
            }
            mPopul = nextPopul;
        }
示例#3
0
        static void OutputFormula(RuntimeData runtime, IFormula f, int level)
        {
            for (int i = 0; i < f.Count; i++)
            {
                if (f.Items[i] is IFormula)
                {
                    Console.WriteLine("{0}{1} [{2}]{3}",
                                      "".PadLeft(level * 2, ' '),
                                      i + 1,
                                      f.Items[i].GetType().Name,
                                      f.Items[i]);
                    OutputFormula(runtime, f.Items[i] as IFormula, level + 1);
                }
                else if (f.Items[i] is Operator)
                {
                    Operator e = (f.Items[i] as Operator);
                    Console.WriteLine("{0}{1}. {2}[{3}] => {4}",
                                      "".PadLeft(level * 2, ' '),
                                      i + 1,
                                      f.Items[i],
                                      f.Items[i].GetType().Name,
                                      e.Evaluator.Name);
                }
                else
                {
                    IEval e  = (f.Items[i] as IEval);
                    var   ee = e != null?e.Eval(runtime) : null;

                    Console.WriteLine("{0}{1}. {2}[{3}] => {4}",
                                      "".PadLeft(level * 2, ' '),
                                      i + 1,
                                      f.Items[i],
                                      f.Items[i].GetType().Name,
                                      e != null ? ee.ToString() + "[" + ee.GetType().Name + "]" : "不明");
                }
            }
        }
示例#4
0
        public void LtlDisjunctionTest(string input, string ltl)
        {
            IEval evaluation = Program.BuildAstLTL(_ltlEval, ltl);

            Assert.True(evaluation.Eval(input), $"{input} should be true");
        }
示例#5
0
 public void ConstEvalTest1() =>
 Assert.Equal(7, _constExpr1.Eval());
示例#6
0
 // *** IOptimizer interface implementation ***
 public ArrayList<double> Optimize(double[] initParamVec, IEval eval)
 {
     Utils.ThrowException(initParamVec == null ? new ArgumentNullException("initParamVec") : null);
     Utils.ThrowException(eval == null ? new ArgumentNullException("eval") : null);
     ArrayList<double> paramVec = new ArrayList<double>(initParamVec);
     Utils.ThrowException(paramVec.Count == 0 ? new ArgumentValueException("initParamVec") : null);
     SetInitPopul(paramVec.Count, paramVec.Count * 10); // *** make this multiplier configurable
     mPopul[0] = new Pair<double, ArrayList<double>>(0, paramVec);
     double bestGlobalVal = double.MinValue;
     ArrayList<double> bestParamVec = null;
     // evaluate initial population
     for (int i = 0; i < mPopul.Count; i++)
     {
         Pair<double, ArrayList<double>> indiv = mPopul[i];
         double localVal = eval.Eval(indiv.Second);
         mPopul[i] = new Pair<double, ArrayList<double>>(localVal, indiv.Second);
         if (localVal > bestGlobalVal) { bestGlobalVal = localVal; bestParamVec = indiv.Second; }
     }
     // optimize
     int numNoChangeIter = 0;
     double bestVal = bestGlobalVal;
     while (numNoChangeIter < mMinNoChangeIter)
     {
         SetNextPopul(eval, mWgtFactor, mCrossover, ref bestVal, ref bestParamVec);
         if (bestVal > bestGlobalVal) { numNoChangeIter = 0; bestGlobalVal = bestVal; } else { numNoChangeIter++; }
         mLogger.Info("Optimize", "Iteration status:\r\n" +
             "No-change iterations: {0} / {1}\r\n" +
             "Current best solution vector: {2}\r\n" +
             "Current best solution score:  {3}", numNoChangeIter, mMinNoChangeIter, bestParamVec, bestGlobalVal);
     }
     return bestParamVec;
 }
示例#7
0
 private void SetNextPopul(IEval eval, double wgtFactor, double crossover, ref double bestVal, ref ArrayList<double> bestParamVec)
 {
     ArrayList<Pair<double, ArrayList<double>>> nextPopul = new ArrayList<Pair<double, ArrayList<double>>>(mPopul.Count);
     // for each individual in the population ...
     foreach (Pair<double, ArrayList<double>> indivInfo in mPopul)
     {
         ArrayList<double> indiv = indivInfo.Second;
         ArrayList<double> rndIndiv1 = null, rndIndiv2 = null, rndIndiv3 = null;
         // randomly select parents
         GetRandomIndiv(indiv, ref rndIndiv1, ref rndIndiv2, ref rndIndiv3);
         // create initial candidate v
         ArrayList<double> v = ComputeInitialCandidate(rndIndiv1, rndIndiv2, rndIndiv3, wgtFactor);
         // create final candidate u
         ArrayList<double> u = indiv.Clone();
         for (int i = 0; i < u.Count; i++)
         {
             if (mRandom.NextDouble() < crossover)
             {
                 u[i] = v[i];
             }
         }
         // accept or reject candidate u
         double newVal = eval.Eval(u);
         if (newVal > indivInfo.First)
         {
             nextPopul.Add(new Pair<double, ArrayList<double>>(newVal, u));
             if (newVal > bestVal) { bestVal = newVal; bestParamVec = u; }
         }
         else
         {
             nextPopul.Add(indivInfo);
             if (indivInfo.First > bestVal) { bestVal = indivInfo.First; bestParamVec = indivInfo.Second; }
         }
     }
     mPopul = nextPopul;
 }