Population of chromosomes.

The class represents population - collection of individuals (chromosomes) and provides functionality for common population's life cycle - population growing with help of genetic operators and selection of chromosomes to new generation with help of selection algorithm. The class may work with any type of chromosomes implementing IChromosome interface, use any type of fitness functions implementing IFitnessFunction interface and use any type of selection algorithms implementing ISelectionMethod interface.

Ejemplo n.º 1
0
        public static League sortTeamMembers(List<TeamMember> members, int numTeams)
        {
            League league = new League(members, numTeams);

            Population population = new Population(members.Count, league, new FitnessCalculator(), new EliteSelection());
            population.MutationRate = .5;
            population.CrossoverRate = .2;
            population.RunEpoch();
            //run epoch until you get the same best chromosome 10 times
            double bestFitness = -Double.MaxValue;
            int count = 0;
            while(count < 5000)
            {
                population.RunEpoch();
                double fitness = ((League)population.BestChromosome).getFitness();
                Console.WriteLine("Fitness: " + fitness);
                if (fitness > bestFitness)
                {
                    bestFitness = fitness;
                    count = 0;
                }
                else
                {
                    count++;
                }
            }

            //while (((League)population.BestChromosome).getFitness() != 80.0)
            //    population.RunEpoch();
            League best = ((League)population.BestChromosome);
            return best;
        }
Ejemplo n.º 2
0
 private void atualizaDadosPara(int iteracao,Population populacao)
 {
     log = "Geração: " + iteracao +
         "\n Método de Seleção : " + populacao.SelectionMethod +
         "\n Avaliação Média: " + populacao.FitnessAvg +
         "\n Melhor Avaliação : " + populacao.FitnessMax +
         "\n Melhor indivíduo: " + populacao.BestChromosome.ToString();
     this.logBox.Text = log;
     desenhaTabuleiro((ShortArrayChromosome)populacao.BestChromosome);
 }
Ejemplo n.º 3
0
        private static void HardestOpponent()
        {
            var challenges = Library.Monsters.ToDictionary(x => x, x => 0.0);

            while (true)
            {
                var minScore = challenges.Values.Min();
                var monster = challenges.First(x => x.Value == minScore).Key;

                var simulator = monster.MaxPartyMembers > 1 ?
                (ASimulator)new PartySimulator(monster, 40, 40, false) :
                (ASimulator)new SingleHeroSimulator(monster, 40, 40);

                var fitnessEvaluator = new PartyFitness(simulator);
                var population = new Population(20,
                    new ShortArrayChromosome(fitnessEvaluator.ChromosomeLength, fitnessEvaluator.ChromosomeMaxValue),
                    fitnessEvaluator,
                    new RankSelection());
                population.RandomSelectionPortion = 0.15;

                double lastFitness = 0;
                int stagnationLimit = minScore < 1e-4 ? 1000 : 10000;

                for (int stagnation = 0; stagnation < stagnationLimit; stagnation++)
                {
                    population.RunEpoch();
                    if (population.BestChromosome.Fitness > lastFitness)
                    {
                        lastFitness = population.BestChromosome.Fitness;
                        stagnation = 0;

                        if (lastFitness > challenges[monster])
                        {
                            challenges[monster] = lastFitness;

                            Console.WriteLine(monster.Name);
                            Console.Write(fitnessEvaluator.Translate(population.BestChromosome).Trim());
                            Console.WriteLine(" " + population.BestChromosome.Fitness);
                            Console.WriteLine();
                        }
                    }

                    population.MutationRate = stagnation > 1000 ?
                        Math.Min(stagnation / 3000.0, 0.25) :
                        0.1;
                }
            }
        }
Ejemplo n.º 4
0
        private void button1_Click(object sender, EventArgs e)
        {
            //ShortArrayChromosome[] individos = new ShortArrayChromosome[nPopulacao];
            //for (int i = 0; i < individos.Length; i++)
            //{
            //    individos[i] = new ShortArrayChromosome(nRainhas, nRainhas);
            //    individos[i].Generate();
            //    Console.WriteLine(i + " : " + individos[i].ToString());
            //}

            configuraAlgoritimo();
            int selecao = selecaoBox.SelectedIndex;
            ISelectionMethod metodoDeSelecao = (selecao == 0) ? (ISelectionMethod)new RouletteWheelSelection() :
                                                    (selecao == 1) ? (ISelectionMethod)new EliteSelection() :
                                                        (ISelectionMethod)new RankSelection();
										   
            AvaliadorDeRainhas avaliador = new AvaliadorDeRainhas();
            Population populacao = new Population(nPopulacao, new ShortArrayChromosome(nRainhas, nRainhas - 1), avaliador, metodoDeSelecao);
            populacao.CrossoverRate = crossoverRate;
            populacao.MutationRate = mutationRate;
            //populacao.AutoShuffling = true;

            int iteracao = 0;
            int pararEm = nParada;
            while (iteracao < nGeracoes)
            {
                //populacao.Shuffle();
                //populacao.Selection();
                //populacao.Crossover();
                //populacao.Mutate();
                populacao.RunEpoch();
                //MessageBox.Show("iteração: " + iteracao + "\n Avaliacao Media: " + populacao.FitnessAvg + "\n Melhor individo: " + populacao.BestChromosome.ToString());
                if (nParada > 0 && iteracao == pararEm)
                {
                    atualizaDadosPara(iteracao, populacao);
                    MessageBox.Show("Visualização\nGeração: " + iteracao+"\n OK para Continuar");
                    pararEm += nParada;
                }
                if (populacao.BestChromosome.Fitness == nRainhas)
                    break;
                iteracao++;
            }
            //constroiTabuleiroNoConsole((ShortArrayChromosome)populacao.BestChromosome);
            atualizaDadosPara(iteracao,populacao);
        }
Ejemplo n.º 5
0
 static void Main(string[] args)
 {
     DoubleArrayChromosome weightValues = new DoubleArrayChromosome(new WeightRandomGenerator(),  new WeightRandomGenerator(), new WeightRandomGenerator(),6);
     Population weightPop = new Population(40, weightValues, new AriesFF(), new EliteSelection());
     int counter = 0;
     bool stopEvo = false;
     AriesFF fintessEval = new AriesFF();
     double error = 0;
     while (!stopEvo)
     {
         weightPop.RunEpoch();
         counter++;
         IChromosome best = weightPop.BestChromosome;
         error = fintessEval.Evaluate(best);
         stopEvo = counter > 1000 || error < 0.12;
     }
     Console.WriteLine("Stopped after " + counter.ToString());
     DoubleArrayChromosome solution = (DoubleArrayChromosome)weightPop.BestChromosome;
     Console.WriteLine(solution.ToString());
     Console.ReadKey();
 }
Ejemplo n.º 6
0
        // Worker thread
        void SearchSolution()
        {
            // create population
            Population population = new Population(populationSize,
                new BinaryChromosome(chromosomeLength),
                userFunction,
                (selectionMethod == 0) ? (ISelectionMethod)new EliteSelection() :
                (selectionMethod == 1) ? (ISelectionMethod)new RankSelection() :
                                           (ISelectionMethod)new RouletteWheelSelection()
                );
            // set optimization mode
            userFunction.Mode = (optimizationMode == 0) ?
                OptimizationFunction1D.Modes.Maximization :
                OptimizationFunction1D.Modes.Minimization;
            // iterations
            int i = 1;
            // solution
            double[,] data = new double[(showOnlyBest) ? 1 : populationSize, 2];


            // loop
            while (!needToStop)
            {
                // run one epoch of genetic algorithm
                population.RunEpoch();

                // show current solution
                if (showOnlyBest)
                {
                    data[0, 0] = userFunction.Translate(population.BestChromosome);
                    data[0, 1] = userFunction.OptimizationFunction(data[0, 0]);
                }
                else
                {
                    for (int j = 0; j < populationSize; j++)
                    {
                        data[j, 0] = userFunction.Translate(population[j]);
                        data[j, 1] = userFunction.OptimizationFunction(data[j, 0]);
                    }
                }
                chart.UpdateDataSeries("solution", data);

                // set current iteration's info
                SetText(currentIterationBox, i.ToString());
                SetText(currentValueBox, userFunction.Translate(population.BestChromosome).ToString("F3"));

                // increase current iteration
                i++;

                //
                if ((iterations != 0) && (i > iterations))
                    break;
            }

            // enable settings controls
            EnableControls(true);
        }
Ejemplo n.º 7
0
		// Worker thread
		void SearchSolution( )
		{
			// constants
			double[] constants = new double[10] { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 };
			// create fitness function
			TimeSeriesPredictionFitness fitness = new TimeSeriesPredictionFitness(
				data, windowSize, predictionSize, constants );
			// create gene function
			IGPGene gene = ( functionsSet == 0 ) ?
				(IGPGene) new SimpleGeneFunction( windowSize + constants.Length ) :
				(IGPGene) new ExtendedGeneFunction( windowSize + constants.Length );
			// create population
			Population population = new Population( populationSize,
				( geneticMethod == 0 ) ?
				(IChromosome) new GPTreeChromosome( gene ) :
				(IChromosome) new GEPChromosome( gene, headLength ),
				fitness,
				( selectionMethod == 0 ) ? (ISelectionMethod) new EliteSelection( ) :
				( selectionMethod == 1 ) ? (ISelectionMethod) new RankSelection( ) :
				(ISelectionMethod) new RouletteWheelSelection( )
				);
			// iterations
			int i = 1;
			// solution array
			int			solutionSize = data.Length - windowSize;
			double[,]	solution = new double[solutionSize, 2];
			double[]	input = new double[windowSize + constants.Length];

			// calculate X values to be used with solution function
			for ( int j = 0; j < solutionSize; j++ )
			{
				solution[j, 0] = j + windowSize;
			}
			// prepare input
			Array.Copy( constants, 0, input, windowSize, constants.Length );

			// loop
			while ( !needToStop )
			{
				// run one epoch of genetic algorithm
				population.RunEpoch( );

				try
				{
					// get best solution
					string bestFunction = population.BestChromosome.ToString( );

					// calculate best function and prediction error
					double learningError = 0.0;
					double predictionError = 0.0;
					// go through all the data
					for ( int j = 0, n = data.Length - windowSize; j < n; j++ )
					{
						// put values from current window as variables
						for ( int k = 0, b = j + windowSize - 1; k < windowSize; k++ )
						{
							input[k] = data[b - k];
						}

						// evalue the function
						solution[j, 1] = PolishExpression.Evaluate( bestFunction, input );

						// calculate prediction error
						if ( j >= n - predictionSize )
						{
							predictionError += Math.Abs( solution[j, 1] - data[windowSize + j] );
						}
						else
						{
							learningError += Math.Abs( solution[j, 1] - data[windowSize + j] );
						}
					}
					// update solution on the chart
					chart.UpdateDataSeries( "solution", solution );
				
					// set current iteration's info
                    SetText( currentIterationBox, i.ToString( ) );
                    SetText( currentLearningErrorBox, learningError.ToString( "F3" ) );
                    SetText( currentPredictionErrorBox, predictionError.ToString( "F3" ) );
				}
				catch
				{
					// remove any solutions from chart in case of any errors
					chart.UpdateDataSeries( "solution", null );
				}


				// increase current iteration
				i++;

				//
				if ( ( iterations != 0 ) && ( i > iterations ) )
					break;
			}

			// show solution
            SetText( solutionBox, population.BestChromosome.ToString( ) );
			for ( int j = windowSize, k = 0, n = data.Length; j < n; j++, k++ )
			{
                AddSubItem( dataList, j, solution[k, 1].ToString( ) );
			}

			// enable settings controls
			EnableControls( true );
		}
Ejemplo n.º 8
0
        /// <summary>
        /// Perform migration between two populations.
        /// </summary>
        /// 
        /// <param name="anotherPopulation">Population to do migration with.</param>
        /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param>
        /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param>
        /// 
        /// <remarks><para>The method performs migration between two populations - current and the
        /// <paramref name="anotherPopulation">specified one</paramref>. During migration
        /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from
        /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref>
        /// and put into another population replacing worst members there.</para></remarks>
        /// 
        public void Migrate( Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector )
        {
            int currentSize = this.size;
            int anotherSize = anotherPopulation.Size;

            // create copy of current population
            List<IChromosome> currentCopy = new List<IChromosome>( );

            for ( int i = 0; i < currentSize; i++ )
            {
                currentCopy.Add( population[i].Clone( ) );
            }

            // create copy of another population
            List<IChromosome> anotherCopy = new List<IChromosome>( );

            for ( int i = 0; i < anotherSize; i++ )
            {
                anotherCopy.Add( anotherPopulation.population[i].Clone( ) );
            }

            // apply selection to both populations' copies - select members to migrate
            migrantsSelector.ApplySelection( currentCopy, numberOfMigrants );
            migrantsSelector.ApplySelection( anotherCopy, numberOfMigrants );

            // sort original populations, so the best chromosomes are in the beginning
            population.Sort( );
            anotherPopulation.population.Sort( );

            // remove worst chromosomes from both populations to free space for new members
            population.RemoveRange( currentSize - numberOfMigrants, numberOfMigrants );
            anotherPopulation.population.RemoveRange( anotherSize - numberOfMigrants, numberOfMigrants );

            // put migrants to corresponding populations
            population.AddRange( anotherCopy );
            anotherPopulation.population.AddRange( currentCopy );

            // find best chromosomes in each population
            FindBestChromosome( );
            anotherPopulation.FindBestChromosome( );
        }
Ejemplo n.º 9
0
        // Worker thread
        void SearchSolution()
        {
            // create fitness function
            TSPFitnessFunction fitnessFunction = new TSPFitnessFunction(map);

            // create population
            Population population = new Population(populationSize,
                (greedyCrossover) ? new TSPChromosome(map) : new PermutationChromosome(citiesCount),
                fitnessFunction,
                (selectionMethod == 0) ? (ISelectionMethod)new EliteSelection() :
                (selectionMethod == 1) ? (ISelectionMethod)new RankSelection() :
                (ISelectionMethod)new RouletteWheelSelection()
                );

            // iterations
            int i = 1;

            // path
            double[,] path = new double[citiesCount + 1, 2];

            // loop
            while (!needToStop)
            {
                // run one epoch of genetic algorithm
                population.RunEpoch();

                // display current path
                ushort[] bestValue = ((PermutationChromosome)population.BestChromosome).Value;

                for (int j = 0; j < citiesCount; j++)
                {
                    path[j, 0] = map[bestValue[j], 0];
                    path[j, 1] = map[bestValue[j], 1];
                }
                path[citiesCount, 0] = map[bestValue[0], 0];
                path[citiesCount, 1] = map[bestValue[0], 1];

                mapControl.UpdateDataSeries("path", path);

                // set current iteration's info
                SetText(currentIterationBox, i.ToString());
                SetText(pathLengthBox, fitnessFunction.PathLength(population.BestChromosome).ToString());

                // increase current iteration
                i++;

                //
                if ((iterations != 0) && (i > iterations))
                    break;
            }

            // enable settings controls
            EnableControls(true);
        }
Ejemplo n.º 10
0
        private static void Optimize(string monsterName, string[] startingBuild = null, ushort startingBuildFrontRow = 0)
        {
            var monster = Library.Monsters.First(x => x.Name == monsterName);

            var simulator = monster.MaxPartyMembers > 1 ?
                (ASimulator)new PartySimulator(monster, 40, 40, false) :
                (ASimulator)new SingleHeroSimulator(monster, 40, 40);

            double lastFitness = 0;
            while(true)
            {
                var fitnessEvaluator = new PartyFitness(simulator);
                var ancestor = (startingBuild == null) ?
                    new ShortArrayChromosome(fitnessEvaluator.ChromosomeLength, fitnessEvaluator.ChromosomeMaxValue) :
                    fitnessEvaluator.TranslateBack(simulator.TranslateBack(startingBuild), startingBuildFrontRow);

                var population = new Population(20,
                    ancestor,
                    fitnessEvaluator,
                    new RankSelection());
                population.RandomSelectionPortion = 0.15;

                for(int stagnation = 0; stagnation < 5000; stagnation++)
                {
                    population.RunEpoch();
                    if (population.BestChromosome.Fitness > lastFitness)
                    {
                        lastFitness = population.BestChromosome.Fitness;
                        Console.Write(fitnessEvaluator.Translate(population.BestChromosome).Trim());
                        Console.WriteLine(" " + population.BestChromosome.Fitness);
                        Console.WriteLine();
                        stagnation = 0;
                    }

                    population.MutationRate = stagnation > 1000 ?
                        Math.Min(stagnation / 3000.0, 0.25) :
                        0.1;
                }
            }
        }
Ejemplo n.º 11
0
        public MelodySequence Generate()
        {
            NoteGene baseGene = new NoteGene(GPGeneType.Function);
            baseGene.Function = NoteGene.FunctionTypes.Concatenation;
            GPCustomTree tree = new GPCustomTree(baseGene);
            if (base_seq != null)
            {
                tree.Generate(base_seq.ToArray());
                tree.Mutate();
                tree.Crossover(tree);

                int length = base_seq.Length;
                int depth = (int)Math.Ceiling(Math.Log(length, 2));
                GPCustomTree.MaxInitialLevel = depth - 2;
                GPCustomTree.MaxLevel = depth + 5;
            }
            var selection = new EliteSelection();
            pop = new Population(30, tree, fitnessFunction, selection);

            pop.AutoShuffling = true;
            pop.CrossoverRate = 0.9;
            pop.MutationRate = 0.1;

            int percentage = MaxGenerations / 100;

            for (int i = 0; i < MaxGenerations; i++)
            {
                if(i>percentage)
                {
                    if (OnPercentage != null)
                        OnPercentage(this, i, pop.FitnessAvg);
                    percentage += MaxGenerations / 100;
                }

                pop.RunEpoch();
                if ((int)(i) % 100 == 0)
                    Console.WriteLine(i / (float)MaxGenerations * 100 + "% : " + pop.FitnessAvg);
            }

            GPCustomTree best = pop.BestChromosome as GPCustomTree;
            var notes = best.GenerateNotes();
            return new MelodySequence(notes);
        }
Ejemplo n.º 12
0
        protected override void StartTraining(bool aIsRunning)
        {
            if (aIsRunning)
            {
                mStopWatch = new Stopwatch();
                mStopWatch.Start();

                Stopwatch st = new Stopwatch();
                st.Start();

                min = Double.MaxValue;
                mintest = Double.MaxValue;

                // create grid application
                Settings settings = Settings.LoadSettings();
                GA = new GApplication(new GConnection(settings.AlchemiURL, settings.AlchemiPort, settings.AlchemiUser, settings.AlchemiPassword));
                GA.ApplicationName = "Plain Neural Network model";

                // add GridThread module (this executable) as a dependency
                GA.Manifest.Add(new ModuleDependency(typeof(AForge.PolishExpression).Module));
                GA.Manifest.Add(new ModuleDependency(typeof(AForge.Neuro.ActivationNetwork).Module));
                GA.Manifest.Add(new ModuleDependency(typeof(SingleNNTraining).Module));

                // subscribe to events
                GA.ThreadFinish += new GThreadFinish(ThreadFinished);
                GA.ThreadFailed += new GThreadFailed(ThreadFailed);
                GA.ApplicationFinish += new GApplicationFinish(ApplicationFinished);

                // Набор от данни използван за обучение
                mInput = new double[ExtractEnd - ExtractBegin + 1][];
                mOutput = new double[ExtractEnd - ExtractBegin + 1][];
                // Тестов набор от данни
                mTestInput = new double[TestEnd - TestBegin + 1][];
                mTestOutput = new double[TestEnd - TestBegin + 1][];
                for (int i = 0; i < ExtractEnd - ExtractBegin + 1; i++)
                {
                    mInput[i] = new double[IndependentVariables.Count];
                    mOutput[i] = new double[DependentVariables.Count];
                }

                for (int i = 0; i < TestEnd - TestBegin + 1; i++)
                {
                    mTestInput[i] = new double[IndependentVariables.Count];
                    mTestOutput[i] = new double[DependentVariables.Count];
                }

                MainDataSetTableAdapters.VARIABLESTableAdapter adptVariables = new Plig.TimeSeries.Client.MainDataSetTableAdapters.VARIABLESTableAdapter();

                mReinitializations = Convert.ToInt32(txtReinitializations.Text);
                results = new List<NNResultSet>(mReinitializations);

                mEpochs = Convert.ToInt32(txtEpochs.Text);
                ReinitsFinished = 0;

                int index_of_variable = 0;
                foreach (int variable_id in IndependentVariables.Keys)
                {
                    double minObsValue = adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value;
                    double factor = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, ExtractEnd, variable_id).Value - adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value);
                    Dictionary<int, double?> observations = VariableObservations(variable_id);
                    foreach (int counter in observations.Keys)
                    {
                        if ((counter >= ExtractBegin) && (counter <= ExtractEnd))
                        {
                            mInput[counter - ExtractBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                        if ((counter >= TestBegin) && (counter <= TestEnd))
                        {
                            mTestInput[counter - TestBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                    }
                    index_of_variable++;
                }

                index_of_variable = 0;
                foreach (int variable_id in DependentVariables.Keys)
                {
                    double minObsValue = adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value;
                    double factor = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, ExtractEnd, variable_id).Value - adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value);
                    Dictionary<int, double?> observations = VariableObservations(variable_id);
                    foreach (int counter in observations.Keys)
                    {
                        if ((counter >= ExtractBegin) && (counter <= ExtractEnd))
                        {
                            mOutput[counter - ExtractBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                        if ((counter >= TestBegin) && (counter <= TestEnd))
                        {
                            mTestOutput[counter - TestBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }
                    }
                    index_of_variable++;
                }

                double momentum = 0.0;
                double learning_rate = 0.1;

                learning_rate = Convert.ToDouble(txtLearningRate.Text);
                momentum = Convert.ToDouble(txtMomentum.Text);

                int min_neurons = 1;
                int max_neurons = 1;
                if (rbFixedHiddenNeurons.Checked)
                {
                    min_neurons = Convert.ToInt32(txtHiddenNeurons.Text);
                    max_neurons = min_neurons;
                }

                if (rbFlexibleHiddenNeurons.Checked)
                {
                    min_neurons = Convert.ToInt32(txtMinHiddenNeurons.Text);
                    max_neurons = Convert.ToInt32(txtMaxHiddenNeurons.Text);
                }

                int TotalNNCalculations = 0;
                for (int hidden_neurons = min_neurons; hidden_neurons <= max_neurons; hidden_neurons++)
                {

                    for (int i = 0; i < mReinitializations; i++)
                    //              Parallel.For(0, mReinitializations - 1, i =>
                    {
                        // create thread
                        SingleNNTraining single_nn = new SingleNNTraining(mInput, mOutput, mEpochs);
                        single_nn.HiddenNeurons = hidden_neurons;
                        single_nn.Iterations = mEpochs;
                        single_nn.Momentum = momentum;
                        single_nn.LearningRate = learning_rate;

                        // добавяне на набора от данни за тестване
                        single_nn.TestInput = mTestInput;
                        single_nn.TestOutput = mTestOutput;

                        // add thread to application
                        GA.Threads.Add(single_nn);
                        TotalNNCalculations++;
                    }
                    //                );
                }

                pbProgressTraining.Minimum = 0;
                pbProgressTraining.Maximum = TotalNNCalculations - 1;

                GA.Start();

                st.Stop();
                lbResults.Items.Add("Време необходимо за конструиране на данните: " + st.Elapsed.ToString());

                int populationCount = Convert.ToInt32(txtPopulationCount.Text);
                Population population = new Population(populationCount,
                    new DoubleArrayChromosome(new AForge.Math.Random.UniformOneGenerator(),
                        new AForge.Math.Random.GaussianGenerator(0, 1),new AForge.Math.Random.GaussianGenerator(0, 1),
                        (mOutput.Length + mInput.Length + 1) * 10 + mOutput.Length),
                    new NeuralNetworkFitness(mInput, mOutput, new int[] { IndependentVariables.Count, 10, DependentVariables.Count }),
                    new EliteSelection());

                int geneticEpochs = Convert.ToInt32(txtGeneticEpochs.Text);
                pbGeneticProgress.Maximum = geneticEpochs;

                for (int i = 0; i < geneticEpochs; i++)
                {
                    population.RunEpoch();
                    pbGeneticProgress.Value = i + 1;
                    Application.DoEvents();
                }

                NeuralNetworkFitness nnf = new NeuralNetworkFitness(mInput, mOutput, new int[] { IndependentVariables.Count, 5, DependentVariables.Count });
                mBestGeneticActivationNetwork = (ActivationNetwork)nnf.Translate(population.BestChromosome);

            }
            else
            {
                if (GA.Running)
                {
                    foreach (GThread thread in GA.Threads)
                    {
                        thread.Abort();
                    }
            //                     GA.Stop();
                    lbResults.Items.Add("Обучението е прекъснато.");
                }
            }
        }
Ejemplo n.º 13
0
        public override void takeTurn()
        {
            Board.useDictionary = true;
            bool tempRecording = GameController.recording;
            GameController.recording = false;
            fitness.cards = Board.current.viewableCards.FindAll(x => x.Deck != Card.Decks.nobles);
            var ga = new Population(popSize, new BuyOrderChromosome(2*fitness.cards.Count), fitness, new RankSelection(), random);
            lastBestChromosome = ga.population[0] as BuyOrderChromosome;
            ga.CrossoverRate = 0.5;

               //     if (!predicted.Equals(Board.current.PrevMove)) RecordHistory.current.record("!!! Prediction failed.");
               //     predicted = null;
            int i = 0;
            turnTimer.Restart();
            while (fitness.timesEvaluated < evaluations)//(turnTimer.Elapsed < Board.current.notCurrentPlayer.turnTimer.Elapsed && i < 40)
            {
                ga.RunEpoch();
                ga.AddChromosome(lastBestChromosome);
                if (ga.BestChromosome.Fitness > lastBestChromosome.Fitness) lastBestChromosome = ga.BestChromosome as BuyOrderChromosome;
                if (i % 6 == 0) Board.current.ResetDictionary();
                CONSOLE.Overwrite(6, "Generations " + i + " Evaluation " + fitness.timesEvaluated);
                RecordHistory.current.plot(i + "," + ga.FitnessMax + Environment.NewLine);
                if ((GameController.turn % 3 == 0) && (i < 4)) takeSnapshot(ga);
                //setData(i);
                i++;
            }
            fitness.timesEvaluated = 0;
            GameController.recording = tempRecording;
            if (lastBestChromosome == null) throw new Exception("Null chromosome after evaluation " + i);
            lastBestChromosome.Evaluate(fitness);
            Move m = fitness.simulateMyTurn(lastBestChromosome, Board.current).PrevMove;
            RecordHistory.current.record(this + " took move " + m);

            //foreach (BuyOrderChromosome p in chromosomes)
            //{
            //    RecordHistory.writeToFile("Chromosomes.txt", string.Format("{0:0.00}",p.Fitness) + "|" + p.depth + "|" + p.Value.String() + "|" + string.Format("{0:0.00}",p.parentFitness));
            //}
            //RecordHistory.writeToFile("Chromosomes.txt", "");
            //List<BuyOrderChromosome> chromosomes = BuyOrderChromosome.diversify(ga.population);
            //CONSOLE.Overwrite(12, "XOver Impnts over gen.: " + xoverimpnts.String());
            //CONSOLE.Overwrite(13, "Mut Impnts over gen.: " + mutimpnts.String());
            //CONSOLE.Overwrite(14, "Crossover Improvements: " + BuyOrderChromosome.crossOverImprovements + " / " + BuyOrderChromosome.totalCrossOvers);
            //CONSOLE.Overwrite(15, "Mutation Improvements: " + BuyOrderChromosome.mutationImprovements + " / " + BuyOrderChromosome.totalMutations);
            //CONSOLE.Overwrite(16, "Number of Diverse Chromosomes: " + chromosomes.Count);

            //totalChromosomes += ga.population.Count;
            //diverseChromosomes += chromosomes.Count;
            //CONSOLE.Overwrite(17, "Total diverse/chromosomes: " + ((double)diverseChromosomes / totalChromosomes));
            //for (int j = 0; j < chromosomes.Count; j++)
            //{
            //    CONSOLE.Overwrite(18 + j, chromosomes[j].Value.String() + "   depth: " +chromosomes[j].depth + "    fitness: " + chromosomes[j].Fitness);
            //}
            takeAction(m);
            Board.useDictionary = false;
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Records the population fitness data in /snapshot_x.csv
 /// </summary>
 /// <param name="ga"></param>
 private void takeSnapshot(Population ga)
 {
     List<double> fitnesses = ga.getFitnesses();
     List<double> parents = ga.getParentFitnesses();
     List<string> snap = new List<string>();
     for (int j = 0; j < fitnesses.Count; j++) snap.Add(fitnesses[j].ToString() + "," + parents[j].ToString());
     RecordHistory.current.snapshot(snap);
 }
Ejemplo n.º 15
0
        public IA(Game game, int players)
            : base(game)
        {
            rndSeedGen = new Random();
            rndControl = new Random();
            rndMovControl = new Random();
            this.comidas = null;
            this.jugadores = null;
            this.numWeights = HIDDEN_UNITS0 * (INPUT_UNITS + 1) + HIDDEN_UNITS1 * (HIDDEN_UNITS0 + 1) + OUTPUT_UNITS * (HIDDEN_UNITS1 + 1);
            redes = new ActivationNetwork[players];
            for (int i = 0; i < redes.Length; i++)
            {
                redes[i] = new ActivationNetwork(new SigmoidFunction(400), INPUT_UNITS, HIDDEN_UNITS0, HIDDEN_UNITS1, OUTPUT_UNITS);
            }
            inputVector = new double[INPUT_UNITS];
            outputVector = new double[OUTPUT_UNITS];
            doneEvents = new ManualResetEvent[players];
            for (int i = 0; i < players; i++) doneEvents[i] = new ManualResetEvent(false);

            //Se puede jugar con los parametros de los rangos para modificar la evolucion de las redes
            //Tambien se puede modificar el metodo de seleccion.
            chromosomeGenerator = new UniformGenerator(new Range(-10f, 10f), rndSeedGen.Next(-100, 100));
            mutationAdditionGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100));
            mutationMultiplierGenerator = new UniformGenerator(new Range(-8f, 8f), rndSeedGen.Next(-100, 100));
            fitnessFunction = new GameFitnessFunction();
            selectionMethod = new EliteSelection();
            padre = new gameChromosome(chromosomeGenerator, mutationMultiplierGenerator, mutationAdditionGenerator, numWeights);
            poblacion = new Population(WorldGame.JUGADORES, padre, fitnessFunction, selectionMethod);
        }
        /// <summary>
        /// Runs learning epoch.
        /// </summary>
        /// 
        /// <param name="input">Array of input vectors.</param>
        /// <param name="output">Array of output vectors.</param>
        /// 
        /// <returns>Returns summary squared learning error for the entire epoch.</returns>
        /// 
        /// <remarks><para><note>While running the neural network's learning process, it is required to
        /// pass the same <paramref name="input"/> and <paramref name="output"/> values for each
        /// epoch. On the very first run of the method it will initialize evolutionary fitness
        /// function with the given input/output. So, changing input/output in middle of the learning
        /// process, will break it.</note></para></remarks>
        ///
        public double RunEpoch( double[][] input, double[][] output )
        {
            Debug.Assert( input.Length > 0 );
            Debug.Assert( output.Length > 0 );
            Debug.Assert( input.Length == output.Length );
            Debug.Assert( network.InputsCount == input.Length );

            // check if it is a first run and create population if so
            if ( population == null )
            {
                // sample chromosome
                DoubleArrayChromosome chromosomeExample = new DoubleArrayChromosome(
                    chromosomeGenerator, mutationMultiplierGenerator, mutationAdditionGenerator,
                    numberOfNetworksWeights );

                // create population ...
                population = new Population( populationSize, chromosomeExample,
                    new EvolutionaryFitness( network, input, output ), selectionMethod );
                // ... and configure it
                population.CrossoverRate = crossOverRate;
                population.MutationRate = mutationRate;
                population.RandomSelectionPortion = randomSelectionRate;
            }

            // run genetic epoch
            population.RunEpoch( );

            // get best chromosome of the population
            DoubleArrayChromosome chromosome = (DoubleArrayChromosome) population.BestChromosome;
            double[] chromosomeGenes = chromosome.Value;

            // put best chromosome's value into neural network's weights
            int v = 0;

            for ( int i = 0; i < network.Layers.Length; i++ )
            {
                Layer layer = network.Layers[i];

                for ( int j = 0; j < layer.Neurons.Length; j++ )
                {
                    ActivationNeuron neuron = layer.Neurons[j] as ActivationNeuron;

                    for ( int k = 0; k < neuron.Weights.Length; k++ )
                    {
                        neuron.Weights[k] = chromosomeGenes[v++];
                    }
                    neuron.Threshold = chromosomeGenes[v++];
                }
            }

            Debug.Assert( v == numberOfNetworksWeights );

            return 1.0 / chromosome.Fitness;
        }
Ejemplo n.º 17
0
		// Worker thread
		void SearchSolution( )
		{
			// create fitness function
			SymbolicRegressionFitness fitness = new SymbolicRegressionFitness( data, new double[] { 1, 2, 3, 5, 7 } );
			// create gene function
			IGPGene gene = ( functionsSet == 0 ) ?
				(IGPGene) new SimpleGeneFunction( 6 ) :
				(IGPGene) new ExtendedGeneFunction( 6 );
			// create population
			Population population = new Population( populationSize,
				( geneticMethod == 0 ) ?
					(IChromosome) new GPTreeChromosome( gene ) :
					(IChromosome) new GEPChromosome( gene, 15 ),
				fitness,
				( selectionMethod == 0 ) ? (ISelectionMethod) new EliteSelection( ) :
				( selectionMethod == 1 ) ? (ISelectionMethod) new RankSelection( ) :
										   (ISelectionMethod) new RouletteWheelSelection( )
				);
			// iterations
			int i = 1;
			// solution array
			double[,]	solution = new double[50, 2];
			double[]	input = new double[6] { 0, 1, 2, 3, 5, 7 };

			// calculate X values to be used with solution function
			for ( int j = 0; j < 50; j++ )
			{
				solution[j, 0] = chart.RangeX.Min + (double) j * chart.RangeX.Length / 49;
			}

			// loop
			while ( !needToStop )
			{
				// run one epoch of genetic algorithm
				population.RunEpoch( );

				try
				{
					// get best solution
					string bestFunction = population.BestChromosome.ToString( );

					// calculate best function
					for ( int j = 0; j < 50; j++ )
					{
						input[0] = solution[j, 0];
						solution[j, 1] = PolishExpression.Evaluate( bestFunction, input );
					}
					chart.UpdateDataSeries( "solution", solution );
					// calculate error
					double error = 0.0;
					for ( int j = 0, k = data.GetLength( 0 ); j < k; j++ )
					{
						input[0] = data[j, 0];
						error += Math.Abs( data[j, 1] - PolishExpression.Evaluate( bestFunction, input ) );
					}

					// set current iteration's info
                    SetText( currentIterationBox, i.ToString( ) );
                    SetText( currentErrorBox, error.ToString( "F3" ) );
				}
				catch
				{
					// remove any solutions from chart in case of any errors
					chart.UpdateDataSeries( "solution", null );
				}

				// increase current iteration
				i++;

				//
				if ( ( iterations != 0 ) && ( i > iterations ) )
					break;
			}

			// show solution
            SetText( solutionBox, population.BestChromosome.ToString( ) );

			// enable settings controls
			EnableControls( true );
		}
Ejemplo n.º 18
0
        private void OnWorkerDoWorkEvolutionary(object sender, DoWorkEventArgs e)
        {
            ArrayList parameters = e.Argument as ArrayList;
            Debug.Assert(parameters != null);
            bool useRed = (bool)parameters[0], useGreen = (bool)parameters[1], useBlue = (bool)parameters[2];
            List<int> slotSizes = (List<int>)parameters[3];

            ProgramFitnessFunction fitnessFunction = new ProgramFitnessFunction(this.puzzle, slotSizes);
            ISelectionMethod selectionMethod = new EliteSelection();
            const int PopulationSize = 500;
            Population population = new Population(
                PopulationSize,
                new ProgramChromosome(slotSizes.Sum(), slotSizes.Count, useRed, useGreen, useBlue),
                fitnessFunction,
                selectionMethod);

            int iteration = 0;
            population.MutationRate = 0.6;
            population.CrossoverRate = 0.6;
            while (true)
            {
                population.RunEpoch();
                backgroundWorker.ReportProgress(++iteration, population.FitnessMax - 1);

                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                if (population.FitnessMax - 1 == this.puzzle.StarsCount)
                {
                    e.Result = fitnessFunction.ChromosomeToProgram((ProgramChromosome)population.BestChromosome);
                    return;
                }
            }
        }