Beispiel #1
0
        protected internal virtual GAIndividual ReproduceBySimplification(int inIndex)
        {
            PushGPIndividual i = (PushGPIndividual)ReproduceByClone(inIndex);

            i = Autosimplify(i, _reproductionSimplifications);
            return(i);
        }
Beispiel #2
0
        protected internal override GAIndividual ReproduceByMutation(int inIndex)
        {
            PushGPIndividual i = (PushGPIndividual)ReproduceByClone(inIndex);
            int totalsize      = i._program.ProgramSize();
            int which          = ReproductionNodeSelection(i);
            int oldsize        = i._program.SubtreeSize(which);
            int newsize        = 0;

            if (_useFairMutation)
            {
                int range = (int)Math.Max(1, _fairMutationRange * oldsize);
                newsize = Math.Max(1, oldsize + Rng.Next(2 * range) - range);
            }
            else
            {
                newsize = Rng.Next(_maxRandomCodeSize);
            }
            object newtree;

            if (newsize == 1)
            {
                newtree = _interpreter.RandomAtom();
            }
            else
            {
                newtree = _interpreter.RandomCode(newsize);
            }
            if (newsize + totalsize - oldsize <= _maxPointsInProgram)
            {
                i._program.ReplaceSubtree(which, newtree);
            }
            return(i);
        }
Beispiel #3
0
        protected internal override void InitIndividual(GAIndividual inIndividual)
        {
            PushGPIndividual i     = (PushGPIndividual)inIndividual;
            int     randomCodeSize = Rng.Next(_maxRandomCodeSize) + 2;
            Program p = _interpreter.RandomCode(randomCodeSize);

            i.SetProgram(p);
        }
Beispiel #4
0
        public virtual void RunTestProgram(Program p, int inTestCaseIndex)
        {
            PushGPIndividual i    = new PushGPIndividual(p);
            GATestCase       test = _testCases[inTestCaseIndex];

            Console.Out.WriteLine("Executing program: " + p);
            EvaluateTestCase(i, test._input, test._output);
            Console.Out.WriteLine(_interpreter);
        }
Beispiel #5
0
        protected internal virtual PushGPIndividual Autosimplify(PushGPIndividual inIndividual, int steps)
        {
            PushGPIndividual simplest = (PushGPIndividual)inIndividual.Clone();
            PushGPIndividual trial    = (PushGPIndividual)inIndividual.Clone();

            EvaluateIndividual(simplest, true);
            float bestError   = simplest.GetFitness();
            bool  madeSimpler = false;

            for (int i = 0; i < steps; i++)
            {
                madeSimpler = false;
                float method = Rng.Next(100);
                if (trial._program.ProgramSize() <= 0)
                {
                    break;
                }
                if (method < _simplifyFlattenPercent)
                {
                    // Flatten random thing
                    int    pointIndex = Rng.Next(trial._program.ProgramSize());
                    object point      = trial._program.Subtree(pointIndex);
                    if (point is Program)
                    {
                        trial._program.Flatten(pointIndex);
                        madeSimpler = true;
                    }
                }
                else
                {
                    // Remove small number of random things
                    int numberToRemove = Rng.Next(3) + 1;
                    for (int j = 0; j < numberToRemove; j++)
                    {
                        int trialSize = trial._program.ProgramSize();
                        if (trialSize > 0)
                        {
                            int pointIndex = Rng.Next(trialSize);
                            trial._program.ReplaceSubtree(pointIndex, new Program());
                            trial._program.Flatten(pointIndex);
                            madeSimpler = true;
                        }
                    }
                }
                if (madeSimpler)
                {
                    EvaluateIndividual(trial, true);
                    if (trial.GetFitness() <= bestError)
                    {
                        simplest  = (PushGPIndividual)trial.Clone();
                        bestError = trial.GetFitness();
                    }
                }
                trial = (PushGPIndividual)simplest.Clone();
            }
            return(simplest);
        }
Beispiel #6
0
        /// <summary>Selects a node to use during crossover or mutation.</summary>
        /// <remarks>
        /// Selects a node to use during crossover or mutation. The selection
        /// mechanism depends on the global parameter _nodeSelectionMode.
        /// </remarks>
        /// <param name="inInd">= Individual to select node from.</param>
        /// <returns>Index of the node to use for reproduction.</returns>
        protected internal virtual int ReproductionNodeSelection(PushGPIndividual inInd)
        {
            int totalSize    = inInd._program.ProgramSize();
            int selectedNode = 0;

            if (totalSize <= 1)
            {
                selectedNode = 0;
            }
            else
            {
                if (_nodeSelectionMode.Equals("unbiased"))
                {
                    selectedNode = Rng.Next(totalSize);
                }
                else
                {
                    if (_nodeSelectionMode.Equals("leaf-probability"))
                    {
                        // TODO Implement. Currently runs unbiased
                        // note: if there aren't any internal nodes, must select leaf, and
                        // if no leaf, must select internal
                        selectedNode = Rng.Next(totalSize);
                    }
                    else
                    {
                        // size-tournament
                        int maxSize = -1;
                        selectedNode = 0;
                        for (int j = 0; j < _nodeSelectionTournamentSize; j++)
                        {
                            int nextwhich     = Rng.Next(totalSize);
                            int nextwhichsize = inInd._program.SubtreeSize(nextwhich);
                            if (nextwhichsize > maxSize)
                            {
                                selectedNode = nextwhich;
                                maxSize      = nextwhichsize;
                            }
                        }
                    }
                }
            }
            return(selectedNode);
        }
Beispiel #7
0
        protected internal override string FinalReport()
        {
            string report = string.Empty;

            report += base.FinalReport();
            if (!_targetFunctionString.Equals(string.Empty))
            {
                report += ">> Target Function: " + _targetFunctionString + "\n\n";
            }
            PushGPIndividual simplified = Autosimplify((PushGPIndividual)_populations[_currentPopulation][_bestIndividual], _finalSimplifications);

            // Note: The number of evaluations here will likely be higher than that
            // given during the last generational report, since evaluations made
            // during simplification count towards the total number of
            // simplifications.
            report += ">> Number of Evaluations: " + _interpreter.GetEvaluationExecutions() + "\n";
            report += ">> Best Program: " + _populations[_currentPopulation][_bestIndividual] + "\n";
            report += ">> Fitness (mean): " + _bestMeanFitness + "\n";
            if (_testCases.Count == _bestErrors.Count)
            {
                report += ">> Errors: (";
                for (int i = 0; i < _testCases.Count; i++)
                {
                    if (i != 0)
                    {
                        report += " ";
                    }
                    report += "(" + _testCases[i]._input + " ";
                    report += Math.Abs(_bestErrors[i]) + ")";
                }
                report += ")\n";
            }
            report += ">> Size: " + _bestSize + "\n\n";
            report += "<<<<<<<<<< After Simplification >>>>>>>>>>\n";
            report += ">> Best Program: ";
            report += simplified._program + "\n";
            report += ">> Size: ";
            report += simplified._program.ProgramSize() + "\n\n";
            return(report);
        }
Beispiel #8
0
        protected internal override GAIndividual ReproduceByCrossover(int inIndex)
        {
            PushGPIndividual a = (PushGPIndividual)ReproduceByClone(inIndex);
            PushGPIndividual b = (PushGPIndividual)TournamentSelect(_tournamentSize, inIndex);

            if (a._program.ProgramSize() <= 0)
            {
                return(b);
            }
            if (b._program.ProgramSize() <= 0)
            {
                return(a);
            }
            int aindex = ReproductionNodeSelection(a);
            int bindex = ReproductionNodeSelection(b);

            if (a._program.ProgramSize() + b._program.SubtreeSize(bindex) - a._program.SubtreeSize(aindex) <= _maxPointsInProgram)
            {
                a._program.ReplaceSubtree(aindex, b._program.Subtree(bindex));
            }
            return(a);
        }
Beispiel #9
0
        protected internal override string Report()
        {
            string report = base.Report();

            if (double.IsInfinity(_populationMeanFitness))
            {
                _populationMeanFitness = double.MaxValue;
            }
            report += ";; Best Program:\n  " + _populations[_currentPopulation][_bestIndividual] + "\n\n";
            report += ";; Best Program Fitness (mean): " + _bestMeanFitness + "\n";
            if (_testCases.Count == _bestErrors.Count)
            {
                report += ";; Best Program Errors: (";
                for (int i = 0; i < _testCases.Count; i++)
                {
                    if (i != 0)
                    {
                        report += " ";
                    }
                    report += "(" + _testCases[i]._input + " ";
                    report += Math.Abs(_bestErrors[i]) + ")";
                }
                report += ")\n";
            }
            report += ";; Best Program Size: " + _bestSize + "\n\n";
            report += ";; Mean Fitness: " + _populationMeanFitness + "\n";
            report += ";; Mean Program Size: " + _averageSize + "\n";
            PushGPIndividual simplified = Autosimplify((PushGPIndividual)_populations[_currentPopulation][_bestIndividual], _reportSimplifications);

            report += ";; Number of Evaluations Thus Far: " + _interpreter.GetEvaluationExecutions() + "\n";
            // string mem = (Runtime.GetRuntime().TotalMemory() / 10000000.0f).ToString();
            // report += ";; Memory usage: " + mem + "\n\n";
            report += ";; Partial Simplification (may beat best):\n  ";
            report += simplified._program + "\n";
            report += ";; Partial Simplification Size: ";
            report += simplified._program.ProgramSize() + "\n\n";
            return(report);
        }