Example #1
0
        private IChromosome GenerateGANumChromosome()
        {
            var ch = GANumChromosome.NewChromosome();

            ch.Generate();
            return(ch);
        }
        /// <summary>
        /// Evaluates function agains terminals
        /// </summary>
        /// <param name="chromosome"></param>
        /// <param name="functionSet"></param>
        /// <returns></returns>
        public float Evaluate(IChromosome chromosome, IFunctionSet functionSet)
        {
            GANumChromosome ch = chromosome as GANumChromosome;

            if (ch == null)
            {
                return(0);
            }
            else
            {
                //prepare terminals
                var term = Globals.gpterminals.SingleTrainingData;
                for (int i = 0; i < ch.val.Length; i++)
                {
                    term[i] = ch.val[i];
                }

                var y = functionSet.Evaluate(_funToOptimize, -1);


                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    y = float.NaN;
                }

                //Save output in to output variable
                term[term.Length - 1] = y;

                if (IsMinimize)
                {
                    y *= -1;
                }

                return((float)y);
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lines"></param>
        /// <param name="factory"></param>
        /// <param name="curLine"></param>
        /// <param name="typeChromosome"></param>
        /// <returns></returns>
        private int SecondPopulationFromString(string[] lines, GPFactory factory, int curLine, int typeChromosome = 1)
        {
            curLine++;
            //Line 8: populationSize; maxFitness; BestChromosome
            if (lines.Length <= curLine)
            {
                //MessageBox.Show("Fie is corrupt!");
                return(-1);
            }
            var str = lines[curLine].Split(';');


            if (lines[curLine] == "-" || lines[curLine] == "-\r")
            {
                return(curLine += 3);
            }

            //first number is popSIze
            int popSize = 0;

            if (!int.TryParse(str[0], out popSize))
            {
                popSize = 0;
            }

            if (_optimizePanel != null)
            {
                //last number i +s optimization type
                string optType = str[str.Length - 1];
                if (optType == "0" || optType == "0\r")
                {
                    _optimizePanel.SetOptType(false);
                }
                else
                {
                    _optimizePanel.SetOptType(true);
                }

                if (typeChromosome == 1)
                {
                    PrepareGP(false);
                }
                else if (typeChromosome == 2)
                {
                    PrepareGA(false);
                    GANumChromosome.functionSet = factory.GetFunctionSet();
                }
            }

            curLine++;
            //Get Optimization function

            List <IChromosome> chromosomes = new List <IChromosome>();

            curLine++;
            for (int i = 0; i < popSize; i++)
            {
                var ch = GANumChromosome.CreateFromString(lines[i + curLine]);
                chromosomes.Add(ch);
            }
            if (factory != null)
            {
                factory.SetChromosomes(chromosomes);
                factory.CalculatePopulation();
            }
            return(popSize == 0?2:popSize + curLine);
        }