Пример #1
0
        protected virtual bool GetNextGen(Equation EvolvedEquation, Equation OldEquation, int toCalc)
        {
            bool     BestCandEvolved     = false;
            Equation BestEvolvedEquation = BestCandidate.MakeClone(new Equation(EInfo, Randomizer));

            Debug.WriteLine(BestCandidate.CreateFunction());
            //int simplestCount = 0;
            for (double i = 0; i < EInfo.CandidatesPerGen * EInfo.EvolvedCandidatesPerGen; i++)
            {
                BestCandidate.MakeClone(EvolvedEquation);
                EvolveCand.EvolveCandidate(EInfo, EvolvedEquation);
                EvolvedEquation.CalcPartialOffSet(toCalc);
                //if (EvolvedEquation.CreateFunction() == "f(x) = x")
                //{
                //    simplestCount++;
                //}
                //Debug.WriteLine(EvolvedEquation.CreateFunction());
                //Thread.Sleep(500);
                bool EvolvedToBetter = ChangeIfBetter(EvolvedEquation, OldEquation, BestEvolvedEquation);
                BestCandEvolved = (EvolvedToBetter) ? true : BestCandEvolved;
                ResetSingle(EvolvedEquation);
            }
            //Debug.WriteLine(simplestCount);
            //Thread.Sleep(500);
            int[] Indexes = SmartCand.CanSmartChangeNumbers(BestCandidate, OldEquation);
            for (double i = 0; i < EInfo.CandidatesPerGen * EInfo.SmartCandidatesPerGen; i++)
            {
                BestCandidate.MakeClone(EvolvedEquation);
                SmartCand.SmartifyCandidate(EInfo, EvolvedEquation, BestCandidate, OldEquation, Indexes);
                EvolvedEquation.CalcPartialOffSet(toCalc);
                bool EvolvedToBetter = ChangeIfBetter(EvolvedEquation, OldEquation, BestEvolvedEquation);
                BestCandEvolved = (EvolvedToBetter) ? true : BestCandEvolved;
                ResetSingle(EvolvedEquation);
            }
            for (double i = 0; i < EInfo.CandidatesPerGen * EInfo.RandomCandidatesPerGen; i++)
            {
                RandomCand.MakeRandomEquation(EvolvedEquation);
                EvolvedEquation.CalcPartialOffSet(toCalc);
                bool EvolvedToBetter = ChangeIfBetter(EvolvedEquation, OldEquation, BestEvolvedEquation);
                BestCandEvolved = (EvolvedToBetter) ? true : BestCandEvolved;
                ResetSingle(EvolvedEquation);
            }
            if (BestCandEvolved)
            {
                ResetSingle(OldEquation);
                BestEvolvedEquation.MakeClone(OldEquation);

                ResetSingle(BestCandidate);
                BestEvolvedEquation.MakeClone(BestCandidate);
                BestCandidate.CompressEquation();
            }
            return(BestCandEvolved);
        }
Пример #2
0
 public void CheckNewChild(Equation child)
 {
     for (int i = children.Length - 1; i >= 0; i--)
     {
         if (children[i].OffSet > child.OffSet || children[i].NumberOfAllOperators == 0)
         {
             Equation worstChild = children[0];
             for (int y = 0; y < i; y++)
             {
                 children[y] = children[y + 1];
             }
             worstChild.Cleanup();
             child.MakeClone(worstChild);
             children[i] = worstChild;
             break;
         }
     }
 }
Пример #3
0
 protected bool ChangeIfBetter(Equation Eq, Equation OldEquation, Equation BestEvolvedCand)
 {
     if (Tools.IsANumber(Eq.OffSet))
     {
         if (Eq.OffSet < BestEvolvedCand.OffSet &&
             Eq._toCalc >= BestEvolvedCand._toCalc ||
             Eq.OffSet == BestEvolvedCand.OffSet &&
             Eq.NumberOfAllOperators < BestEvolvedCand.NumberOfAllOperators &&
             Eq._toCalc >= BestEvolvedCand._toCalc)
         //if (Eq.OffSet < BestEvolvedCand.OffSet)
         //if (Eq.OffSet < BestEvolvedCand.OffSet || Eq.OffSet == BestEvolvedCand.OffSet && Eq.OperatorsLeft < BestEvolvedCand.OperatorsLeft)
         {
             ResetSingle(BestEvolvedCand);
             BestEvolvedCand = Eq.MakeClone(BestEvolvedCand);
             return(true);
         }
     }
     return(false);
 }