public void Recombinate() { IndividualEvaluations evaluations = new IndividualEvaluations(); Random random = new Random(); _populationAfterCrossing = new Population(); for (var i = 0; i < _matingPool.Size() / 2; i++) { var chosenIndividual1 = random.Next(_matingPool.Size()); var chosenIndividual2 = random.Next(_matingPool.Size()); var newIndividuals = evaluations.Cross(_matingPool.GetIndividual(chosenIndividual1), _matingPool.GetIndividual(chosenIndividual2)); var tempCrsProb = random.NextDouble(); if (tempCrsProb < _crossingProbability) { _populationAfterCrossing.Add(newIndividuals[0]); _populationAfterCrossing.Add(newIndividuals[1]); } else { _populationAfterCrossing.Add(_matingPool.GetIndividual(chosenIndividual1)); _populationAfterCrossing.Add(_matingPool.GetIndividual(chosenIndividual2)); } } }
public Population SetFitnessAndSelection() { _matingPool = new Population(); int numberOfCiterions = 2; _subPopulations = _beginingPopulation.DevidePopulation(); _subPopulations[0].SortF1(); _subPopulations[1].SortF2(); for (int i = 0; i < numberOfCiterions; i++) { for (int j = 0; j < (int)_subPopulations[i].Size(); j++) { if (i == 0) { _matingPool.Add(_subPopulations[i].GetIndividual(j)); } else { _matingPool.Add(_subPopulations[i].GetIndividual(j)); } } } return(_matingPool); }
public void Run() { if (Population == null || Population.Count <= 0) { throw new Exception("the initial population has not been defined"); } if (NumberOfGenerations <= 0) { throw new Exception("the number of the generations must be greater than zero"); } for (int i = 0; i < NumberOfGenerations; i++) { Population.Add(Population.Last().GenerateNextGeneration(Evaluation)); } }
public void Mutation() { IndividualEvaluations evaluations = new IndividualEvaluations(); _populationAfterMutation = new Population(); _populationAfterCrossing.GenerateProbabilities(); for (var i = 0; i < _populationAfterCrossing.Size(); i++) { _populationAfterMutation.Add( _populationAfterCrossing.GetIndividual(i).GetMutationProbability() < _mutationProbability ? evaluations.Mutate(_populationAfterCrossing.GetIndividual(i)) : _populationAfterCrossing.GetIndividual(i)); _populationAfterMutation.GetIndividual(i).Calculate(_function1, _function2); } }
public Population RUN(Population population) { var newPopulation = new Population(); for (int i = 0; i < population.Elements.Count; i++) { var element = population.Elements[i]; if (Utils.Random.NextDouble() >= 1 - Algorithm.MutationChance) { var length = element.Data.Length; var startIndex = Utils.Random.Next(0, length); var endIndex = Utils.Random.Next(0, length); Algorithm.Mutate(ref element, startIndex, endIndex); } newPopulation.Add(element); } return(newPopulation); }
public void Run(object param1 = null, object param2 = null, object param3 = null, object param4 = null, object param5 = null) { if (Population == null || Population.Count <= 0) { throw new Exception("the initial population has not been defined"); } if (NumberOfGenerations <= 0) { throw new Exception("the number of the generations must be greater than zero"); } Evaluation.Param1 = param1; Evaluation.Param2 = param2; Evaluation.Param3 = param3; Evaluation.Param4 = param4; Evaluation.Param5 = param5; for (int i = 0; i < NumberOfGenerations; i++) { Population.Add(Population.Last().GenerateNextGeneration(Evaluation)); } }
public List <Population> DevidePopulation() { var FirstSet = new Population(); var SecondSet = new Population(); var populationMiddle = _population.Count / 2; for (var i = 0; i < populationMiddle; i++) { FirstSet.Add(_population[i]); } for (var i = populationMiddle; i < _population.Count; i++) { SecondSet.Add(_population[i]); } var devidedPopulations = new List <Population> { FirstSet, SecondSet }; return(devidedPopulations); }
private void button1_Click(object sender, EventArgs e) { FunctionStruct functionStruct1 = new FunctionStruct(comboBox1.SelectedItem.ToString(), func1.Text); FunctionStruct functionStruct2 = new FunctionStruct(comboBox1.SelectedItem.ToString(), func2.Text); Population nonDominatedPopulation = new Population(); //chart2.Visible = false; chart1.Series["Series1"].Points.Clear(); chart2.Series["Series1"].Points.Clear(); //chart1.ChartAreas[0].AxisX.RoundAxisValues(); dataGridView1.Rows.Clear(); int lo = 0; int up = 0; int[] lotab = new int[comboBox1.SelectedIndex + 1]; int[] uptab = new int[comboBox1.SelectedIndex + 1]; for (int i = 0; i <= comboBox1.SelectedIndex; i++) { switch (i) { case 0: lo = int.Parse(tbL1.Text); up = int.Parse(tbU1.Text); break; case 1: lo = int.Parse(tbL2.Text); up = int.Parse(tbU2.Text); break; case 2: lo = int.Parse(tbL3.Text); up = int.Parse(tbU3.Text); break; case 3: lo = int.Parse(tbL4.Text); up = int.Parse(tbU4.Text); break; case 4: lo = int.Parse(tbL5.Text); up = int.Parse(tbU5.Text); break; } lotab[i] = lo; uptab[i] = up; } Constraints constraints = new Constraints(lotab, uptab); Vega vega = new Vega(int.Parse(popSize.Text), int.Parse(iterations.Text), Double.Parse(crossingPb.Text, CultureInfo.InvariantCulture), Double.Parse(mutationPb.Text, CultureInfo.InvariantCulture), functionStruct1, functionStruct2, constraints); vega.Initialization(); var result = vega.Iterate(); var index = 0; if (result.GetIndividual(0).GetFitness2() < result.GetIndividual(1).GetFitness2()) { nonDominatedPopulation.Add(result.GetIndividual(0)); } else { nonDominatedPopulation.Add(result.GetIndividual(1)); index = 1; } result.SortF1(); chart2.Series["Series1"].ChartType = SeriesChartType.Point; for (var i = 0; i < result.Size(); i++) { chart2.Series["Series1"].Points.AddXY(result.GetIndividual(i).GetFitness1(), result.GetIndividual(i).GetFitness2()); } for (var i = 1; i < result.Size(); i++) { if (result.GetIndividual(i).GetFitness2() < nonDominatedPopulation.GetIndividual(nonDominatedPopulation.Size() - 1).GetFitness2()) { nonDominatedPopulation.Add(result.GetIndividual(i)); } } if (nonDominatedPopulation.GetIndividual(0).GetFitness1() > nonDominatedPopulation.GetIndividual(1).GetFitness1()) { nonDominatedPopulation.DeleteIndividual(0); } chart1.Series["Series1"].ChartType = SeriesChartType.Point; for (var i = 0; i < nonDominatedPopulation.Size(); i++) { string[] mystring = new string[] { "", "", "", "", "", "", "" }; chart1.Series["Series1"].Points.AddXY(nonDominatedPopulation.GetIndividual(i).GetFitness1(), nonDominatedPopulation.GetIndividual(i).GetFitness2()); mystring[0] = nonDominatedPopulation.GetIndividual(i).GetFitness1().ToString(); mystring[1] = nonDominatedPopulation.GetIndividual(i).GetFitness2().ToString(); for (var j = 0; j < nonDominatedPopulation.GetIndividual(i).GetArgsVector().Count; j++) { mystring[j + 2] = Math.Round(nonDominatedPopulation.GetIndividual(i).GetArgsVector()[j], 3).ToString(); } dataGridView1.Rows.Add(mystring); } }