コード例 #1
0
 private void Tick(FitnessEvaluation fitnessEvaluation)
 {
     lock (fitnessEvaluation)
     {
         if (fitnessEvaluation.Queued)
         {
             bool ticked;
             lock (TickTournamentLock) //Has to wait for a tournament to complete before updating any fitness
             {
                 ticked = fitnessEvaluation.Tick();
             }
             if (ticked)
             {
                 fitnessEvaluation.Queued = false;
                 Console.WriteLine("Caclulated fitness {0}", fitnessEvaluation.SquaredErrorSum);
                 _onFitnessCalculated(fitnessEvaluation);
             }
             else if (fitnessEvaluation.Queued)
             {
                 lock (_fitnessEvaluations)
                 {
                     _fitnessEvaluations.Enqueue(fitnessEvaluation);
                     Console.WriteLine("After Enqueueing again: _fitessEvaluations.Count = {0}", _fitnessEvaluations.Count);
                 }
             }
         }
     }
 }
コード例 #2
0
        public bool AnalyzeTestCasedifferences(FitnessEvaluation fitnessEvaluation, Program simplifiedProgram,
                                               StringBuilder stringBuilder)
        {
            //We need to evaluate unknown test-cases too.
            fitnessEvaluation.Evaluate(false, true, stringBuilder);

            var simplifiedFitnessEvaluation = new FitnessEvaluation(simplifiedProgram, fitnessEvaluation.Problem);

            simplifiedFitnessEvaluation.Evaluate(false, true, fitnessEvaluation.Results, stringBuilder);
            for (int i = 0; i < fitnessEvaluation.Results.Length; ++i)
            {
                if (fitnessEvaluation.Results[i] != simplifiedFitnessEvaluation.Results[i])
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #3
0
 public void Enqueue(FitnessEvaluation fitnessEvaluation)
 {
     lock (fitnessEvaluation)
     {
         if (fitnessEvaluation.Tick())
         {
             fitnessEvaluation.Queued = false;
             //Console.WriteLine("Caclulated fitness without enqueuing {0}", fitnessEvaluation.SquaredErrorSum);
             _onFitnessCalculated(fitnessEvaluation);
         }
         else if (!fitnessEvaluation.Queued)
         {
             fitnessEvaluation.Queued = true;
             lock (_fitnessEvaluations)
             {
                 _fitnessEvaluations.Enqueue(fitnessEvaluation);
                 Monitor.PulseAll(_fitnessEvaluations);
                 Console.WriteLine("After Enqueueing: _fintessEvaluations.Count = {0}", _fitnessEvaluations.Count);
             }
         }
     }
 }
コード例 #4
0
        public Program Simplify(FitnessEvaluation fitnessEvaluation, StringBuilder stringBuilder)
        {
            int      pc           = 0;
            var      callTree     = new CallTree(_varnumber, _code, ref pc);
            var      constantsSet = new ConstantsSet(_constantsSet);
            CallTree simplifiedCallTree;

            if (fitnessEvaluation != null)
            {
                Print(stringBuilder);
            }
            do
            {
                callTree.Reset();
                simplifiedCallTree = callTree.Simplify(constantsSet, fitnessEvaluation != null ? stringBuilder : null);
                if (fitnessEvaluation != null && simplifiedCallTree != null)
                {
                    stringBuilder.AppendLine("After removing redundant code:");

                    var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                        constantsSet,
                                                        WorkingVariablesCount);
                    simplifiedprogram.Print(stringBuilder);
                    if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                    {
                        break;
                    }
                }
                CallTree tmpCallTree =
                    (simplifiedCallTree ?? callTree).PurgeUnassignedVariables(constantsSet,
                                                                              new List <int>(), stringBuilder);

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing unassigned variables:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                tmpCallTree =
                    (simplifiedCallTree ?? callTree).PurgeUnusedAssignmentsToVariables(constantsSet, new List <int>(), stringBuilder);

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing assignments to variables not used again:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                tmpCallTree = (simplifiedCallTree ?? callTree).PurgeRedundantUsagesOfAssignedVariables(constantsSet, new Dictionary <int, CallTree>());

                if (tmpCallTree != null)
                {
                    simplifiedCallTree = tmpCallTree;
                    if (fitnessEvaluation != null)
                    {
                        stringBuilder.AppendLine("After removing redundant usages of assigned variables:");
                        var simplifiedprogram = new Program(_varnumber, new List <int>(simplifiedCallTree.Encode()),
                                                            constantsSet,
                                                            WorkingVariablesCount);
                        simplifiedprogram.Print(stringBuilder);
                        if (AnalyzeTestCasedifferences(fitnessEvaluation, simplifiedprogram, stringBuilder))
                        {
                            break;
                        }
                    }
                }

                if (simplifiedCallTree != null && simplifiedCallTree.Equals(callTree))
                {
                    stringBuilder.AppendLine("Simplified call tree is equal to original call tree, giving up");
                    new Program(_varnumber, new List <int>(callTree.Encode()), constantsSet, WorkingVariablesCount).Print(stringBuilder);
                    //break;
                }
                callTree = simplifiedCallTree ?? callTree;
            } while (simplifiedCallTree != null);
            return(new Program(_varnumber, new List <int>(callTree.Encode()), constantsSet, WorkingVariablesCount));
        }