void optimizePortfolio(int populationSize, int optimalValue) { Random rn = new Random(10); double genesSum = 0; double secGenesSum = 0; double thirdGenesSum = 0; double fourthGenesSum = 0; double childGenesSum = 0; double child2GenesSum = 0; stopWatch.Reset(); Elapsed_Time = "Time: 00:00:00.00"; stopWatch.Start(); population.initializePopulation(populationSize); population.calculateFitness(); Console.WriteLine("Generation: " + generationCount + " Fittest: " + population.fittest + " Second Fittest: " + population.secondFittest + " least fit: " + population.leastFittest); /* * for (int i = 0; i < 10; i++) * { * Console.WriteLine("Genes: " + population.getFittest().genes[i]); * } */ Read(population.fittest, generationCount); //Continue genetics until optimal fitness value is found while (population.fittest < optimalValue) { Thread.Sleep(50); ++generationCount; selection(); crossover(); if (rn.Next() % 10 < mutationRate) { mutation(); } replacement(populationSize); for (int i = 0; i < fittest.genes.Length; i++) { genesSum += fittest.genes[i]; secGenesSum += secondFittest.genes[i]; thirdGenesSum += thirdFittest.genes[i]; fourthGenesSum += fourthFittest.genes[i]; childGenesSum += child.genes[i]; child2GenesSum += child2.genes[i]; } for (int i = 0; i < fittest.genes.Length; i++) { // make genes add to 1 //fittest.genes[i] = (fittest.genes[i] / genesSum) * 1; //secondFittest.genes[i] = (secondFittest.genes[i] / secGenesSum) * 1; //thirdFittest.genes[i] = (thirdFittest.genes[i] / thirdGenesSum) * 1; //fourthFittest.genes[i] = (fourthFittest.genes[i] / fourthGenesSum) * 1; child.genes[i] = (child.genes[i] / childGenesSum) * 1; child2.genes[i] = (child2.genes[i] / child2GenesSum) * 1; } genesSum = 0; secGenesSum = 0; thirdGenesSum = 0; fourthGenesSum = 0; childGenesSum = 0; child2GenesSum = 0; for (int i = 0; i < fittest.genes.Length; i++) { genesSum += fittest.genes[i]; secGenesSum += secondFittest.genes[i]; thirdGenesSum += thirdFittest.genes[i]; fourthGenesSum += fourthFittest.genes[i]; childGenesSum += child.genes[i]; child2GenesSum += child2.genes[i]; } Console.WriteLine("Genes Sum: " + genesSum); Console.WriteLine("SecGenes Sum: " + secGenesSum); Console.WriteLine("ThirdGenes Sum: " + thirdGenesSum); Console.WriteLine("FourthGenes Sum: " + fourthGenesSum); Console.WriteLine("Child Genes Sum: " + childGenesSum); Console.WriteLine("Child2 Genes Sum: " + child2GenesSum); genesSum = 0; secGenesSum = 0; thirdGenesSum = 0; fourthGenesSum = 0; childGenesSum = 0; child2GenesSum = 0; updateFitnessValues(); population.calculateFitness(); Console.WriteLine("Generation: " + generationCount + " Fittest: " + population.fittest + " Second Fittest: " + population.secondFittest); Appl_Weight = population.getFittest().genes[0] * 100; Msft_Weight = population.getFittest().genes[1] * 100; Amzn_Weight = population.getFittest().genes[2] * 100; Nflx_Weight = population.getFittest().genes[3] * 100; Fb_Weight = population.getFittest().genes[4] * 100; Bxc_Weight = population.getFittest().genes[5] * 100; Xom_Weight = population.getFittest().genes[6] * 100; Jnj_Weight = population.getFittest().genes[7] * 100; Mcd_Weight = population.getFittest().genes[8] * 100; Jpm_Weight = population.getFittest().genes[9] * 100; /* * for (int i = 0; i < 10; i++) * { * Console.WriteLine("Genes: " + population.getFittest().genes[i]); * } */ //Update chart Read(population.fittest, generationCount); Console.WriteLine("\n\n"); } Console.WriteLine("\nSolution found in generation " + generationCount); Console.WriteLine("Fitness: " + population.getFittest().fitness); Console.WriteLine("Genes: "); for (int i = 0; i < 10; i++) { Console.WriteLine(population.getFittest().genes[i]); } Console.WriteLine(""); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Elapsed_Time = "Time: " + String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); }