示例#1
0
        /// <summary>
        /// Main program.
        /// </summary>
        static void Main(string[] args)
        {
            // Get the selected fitness type.
            IFitness myFitness = GetFitnessMethod();

            // Genetic algorithm setup.
            _ga = new GA(_crossoverRate, _mutationRate, 100, 10000000, _genomeSize);

            // Get the target fitness for this method.
            _targetFitness = myFitness.TargetFitness;

            // Run the genetic algorithm and get the best brain.
            string program = GAManager.Run(_ga, fitnessFunction, OnGeneration);

            // Display the final program.
            Console.WriteLine(program);
            Console.WriteLine();

            // Compile to executable.
            BrainPlus.Compile(program, "output.exe", myFitness);

            // Run the result for the user.
            string result = myFitness.RunProgram(program);

            Console.WriteLine(result);

            Console.ReadKey();
        }
示例#2
0
        public string Generate(IGeneticAlgorithm ga)
        {
            // Generate functions.
            IFitness myFitness;
            string   originalTargetString = _targetParams.TargetString;
            string   program;
            string   appendCode = "";

            // Split string into terms.
            string[] parts = _targetParams.TargetString.Split(new char[] { ' ' });

            // Build corpus of unique terms to generate functions.
            Dictionary <string, string> terms = new Dictionary <string, string>();

            foreach (string part in parts)
            {
                if (!string.IsNullOrEmpty(part))
                {
                    terms[part] = part;
                }
            }

            foreach (string term in terms.Values)
            {
                _targetParams.TargetString = term;

                // Get the target fitness for this method.
                myFitness = _getFitnessFunc();

                _targetParams.TargetFitness = myFitness.TargetFitness;

                // Run the genetic algorithm and get the best brain.
                program = GAManager.Run(ga, _fitnessFunc, _generationFunc);

                // Trim extraneous loop instructions from the end.
                program = program.Replace("[]", "");

                appendCode += program + "@";

                // Reset the target fitness.
                myFitness.ResetTargetFitness();
                _bestStatus.Fitness        = 0;
                _bestStatus.TrueFitness    = 0;
                _bestStatus.Output         = "";
                _bestStatus.LastChangeDate = DateTime.Now;
                _bestStatus.Program        = "";
                _bestStatus.Ticks          = 0;

                // Notify parent of progress.
                _onStepComplete(appendCode, term);
            }

            // Restore target string.
            _targetParams.TargetString = originalTargetString;

            return(appendCode);
        }
        public string Generate(IGeneticAlgorithm ga)
        {
            // Generate functions.
            IFitness myFitness;
            string   originalTargetString = _targetParams.TargetString;
            string   program;
            string   appendCode = "";

            // Split string into terms of 3-characters.
            string[] parts = SplitInParts(_targetParams.TargetString, _chunkSize).ToArray();

            // Build corpus of unique terms to generate functions.
            Dictionary <string, string> terms = new Dictionary <string, string>();

            foreach (string part in parts)
            {
                terms[part] = part;
            }

            foreach (string term in terms.Values)
            {
                _targetParams.TargetString = term;

                // Get the target fitness for this method.
                myFitness = _getFitnessFunc();

                _targetParams.TargetFitness = myFitness.TargetFitness;

                // Run the genetic algorithm and get the best brain.
                program = GAManager.Run(ga, _fitnessFunc, _generationFunc);

                appendCode += program + "@";

                // Reset the target fitness.
                myFitness.ResetTargetFitness();
                _bestStatus.Fitness        = 0;
                _bestStatus.TrueFitness    = 0;
                _bestStatus.Output         = "";
                _bestStatus.LastChangeDate = DateTime.Now;
                _bestStatus.Program        = "";
                _bestStatus.Ticks          = 0;
            }

            // Restore target string.
            _targetParams.TargetString = originalTargetString;

            return(appendCode);
        }
示例#4
0
        /// <summary>
        /// Main program.
        /// </summary>
        static void Main(string[] args)
        {
            // Genetic algorithm setup.
            _ga = new GA(_crossoverRate, _mutationRate, 100, 10000000, _genomeSize);

            if (_functionGenerator != null)
            {
                // Generate additional functions.
                _appendCode += _functionGenerator.Generate(_ga);
            }

            // Generate main program. Instantiate the fitness method.
            IFitness myFitness = GetFitness();

            // Get the target fitness for this method.
            _targetParams.TargetFitness = myFitness.TargetFitness;

            // Run the genetic algorithm and get the best brain.
            string program = GAManager.Run(_ga, fitnessFunction, OnGeneration);

            // Append any functions.
            if (!string.IsNullOrEmpty(_appendCode))
            {
                program += "@" + _appendCode;
            }

            // Display the final program.
            Console.WriteLine(program);
            Console.WriteLine();

            // Compile to executable.
            BrainPlus.Compile(program, "output.exe", myFitness);

            // Run the result for the user.
            string result = myFitness.RunProgram(program);

            Console.WriteLine(result);

            Console.ReadKey();
        }
示例#5
0
 private void Awake()
 {
     Instance = this;
     DontDestroyOnLoad(this);
 }
示例#6
0
文件: frmModel.cs 项目: wrbrooks/VB3
 private void GAComplete(GAManager gaManager)
 {
     RunComplete(gaManager.Results);
 }
示例#7
0
文件: frmModel.cs 项目: wrbrooks/VB3
        private void RunGA()
        {
            _projMgr.ModelRunning = true;

            ResetGraphs();
            ResetOutputFields();

            double crossoverRate = Convert.ToDouble(txtCrossoverRate.Text);
            double mutationRate = Convert.ToDouble(txtMutRate.Text);
            int numGen = Convert.ToInt32(txtNumGen.Text);
            int popSize = Convert.ToInt32(txtPopSize.Text);

            FitnessCriteria fitnessCriteria = GetFitnessCriteria();

            _maxIndVars = Convert.ToInt32(txtMaxVars.Text);

            if (chkSeed.Checked)
            {
                int seed = Convert.ToInt32(txtSeed.Text);
                RandomNumbers.SetRandomSeed(seed);
            }
            else
                RandomNumbers.SetRandomSeed();

            _maxVIF = Convert.ToInt32(txtMaxVIF.Text);

            List<IIndividual> initPop = new List<IIndividual>(popSize);
            for (int i = 0; i < popSize; i++)
            {
                initPop.Add(new MLRIndividual(_maxIndVars, _totVar, fitnessCriteria, _maxVIF, _decisionThreshold, _mandateThreshold));
                //initPop.Add(new MLRIndividual(_maxIndVars, _totVar, fitnessCriteria, _maxVIF));
            }

            Population population = new Population(initPop);

            population.CrossoverMethod = new MLROnePointCrossover(crossoverRate);

            if (fitnessCriteria == FitnessCriteria.Akaike)
            {
                population.Comparer = new AscendSort();
                AICSelector aicSelector = new AICSelector();
                aicSelector.Comparer = population.Comparer;
                population.Selector = aicSelector;
            }
            else if (fitnessCriteria == FitnessCriteria.AICC)
            {
                population.Comparer = new AscendSort();
                AICCSelector aiccSelector = new AICCSelector();
                aiccSelector.Comparer = population.Comparer;
                population.Selector = aiccSelector;
            }
            else if (fitnessCriteria == FitnessCriteria.BIC)
            {
                population.Comparer = new AscendSort();
                BICSelector bicSelector = new BICSelector();
                bicSelector.Comparer = population.Comparer;
                population.Selector = bicSelector;
            }
            else if (fitnessCriteria == FitnessCriteria.Press)
            {
                population.Comparer = new AscendSort();
                PressSelector pressSelector = new PressSelector();
                pressSelector.Comparer = population.Comparer;
                population.Selector = pressSelector;
            }
            else if (fitnessCriteria == FitnessCriteria.AdjustedR2)
            {
                population.Comparer = new DescendSort();
                AdjR2Selector adjR2Selector = new AdjR2Selector();
                adjR2Selector.Comparer = population.Comparer;
                population.Selector = adjR2Selector;
            }
            else if (fitnessCriteria == FitnessCriteria.R2)
            {
                population.Comparer = new DescendSort();
                R2Selector r2Selector = new R2Selector();
                r2Selector.Comparer = population.Comparer;
                population.Selector = r2Selector;
            }
            else if (fitnessCriteria == FitnessCriteria.RMSE)
            {
                population.Comparer = new AscendSort();
                RMSESelector rmseSelector = new RMSESelector();
                rmseSelector.Comparer = population.Comparer;
                population.Selector = rmseSelector;
            }
            else if (fitnessCriteria == FitnessCriteria.Sensitivity)
            {
                population.Comparer = new DescendSort();
                SensitivitySelector sensitivitySelector = new SensitivitySelector();
                sensitivitySelector.Comparer = population.Comparer;
                population.Selector = sensitivitySelector;
            }
            else if (fitnessCriteria == FitnessCriteria.Specificity)
            {
                population.Comparer = new DescendSort();
                SpecificitySelector specificitySelector = new SpecificitySelector();
                specificitySelector.Comparer = population.Comparer;
                population.Selector = specificitySelector;
            }
            else if (fitnessCriteria == FitnessCriteria.Accuracy)
            {
                population.Comparer = new DescendSort();
                AccuracySelector accuracySelector = new AccuracySelector();
                accuracySelector.Comparer = population.Comparer;
                population.Selector = accuracySelector;
            }

            population.ChromosomeComparer = new CompareChromosomes();

            population.Initialize();

            population.Mutator = new MLRMutator(mutationRate, _totVar);

            //GALib.GAManager ga = new GALib.GAManager();
            _gaManager = new GALib.GAManager();
            _gaManager.Init(population);
            _gaManager.NumberOfGenerations = numGen;
            _gaManager.GAProgress += new GAManager.GAProgressHandler(GAUpdate);
            _gaManager.GAComplete += new GAManager.GACompleteHandler(GAComplete);

            GAManager ga = new GAManager();
            _runThread = new Thread(_gaManager.Run);
            _runThread.Start();
            //list = ga.Run(population);
        }