public async void MOGA_FitnessEvaluation(int[] token) { List <Task> lTasks = new List <Task>(); int remainTasks = tempPool.Count; // Test inputs Generation // Fitness Evaluation for (int k = 0; k < tempPool.Count;) { // Single Thread //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds); lTasks.Add(Task.Run(() => { GlobalVar.mutex_K.WaitOne(); //Console.WriteLine(k); GAEncoding ga = tempPool.ElementAt(k).Key; k = k + 1; GlobalVar.mutex_K.ReleaseMutex(); //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds); ga.TrueFitnessCal(numOfLabel, labelMatrix); // Console.WriteLine("{0}'th finished assement", k); })); if (lTasks.Count == 8 || (tempPool.Count - k - 1 < 8)) //8,8 { for (int i = 0; i < lTasks.Count; i++) { await lTasks[i]; } lTasks.Clear(); } } token[0] = 1; //Console.WriteLine("Fitness Evaluation Done"); }
private void MOGA_Initialization() { cePool = new CEPool(); tempPool = new CEPool(); ceDB = new CEPool(); Console.BackgroundColor = ConsoleColor.DarkRed; numOfNNLocalSearch = 1000; //(pNumOfParams + 1) * (pNumOfParams + 2) / 2 * dimension; for (int i = 0; i < popSize; i++) { var newSolution = new GAEncoding(pNumOfParams, dimension, lowbounds, highbounds); newSolution.FixInputs(lowbounds, highbounds); tempPool.Add(newSolution, new double[] { -1, -1 }); } }
internal GAEncoding PanmicticDiscreteRecomb(CEPool currentPool, int[] selectedList) { GAEncoding newSolution = new GAEncoding(_numOfParams, _dimension, _lowbounds, _highbounds); for (int i = 0; i < _dimension; i++) { for (int j = 0; j < _numOfParams; j++) { int selectedIndex = selectedList[GlobalVar.rnd.Next(0, selectedList.Length - 1 + 1)]; newSolution.weights[j, i] = currentPool.ElementAt(selectedIndex).Key.weights[j, i]; } } newSolution.FixInputs(_lowbounds, _highbounds); newSolution.UpdateID(); return(newSolution); }
// Recombination: create a new solution, call this function; // Add the new solution to the population internal GAEncoding PanmicticAvgRecomb(CEPool currentPool, int[] selectedList) { // Select solution to combine // Find Average Matrix <double> avgWeight = Matrix.Build.Dense(_numOfParams, _dimension); for (int i = 0; i < selectedList.Length; i++) { avgWeight += currentPool.ElementAt(selectedList[i]).Key.weights; } avgWeight = avgWeight / selectedList.Length; // ... Check the sum of each row should be equal to a constant max GAEncoding newSolution = new GAEncoding(_numOfParams, _dimension, _lowbounds, _highbounds); newSolution.weights = avgWeight; newSolution.FixInputs(_lowbounds, _highbounds); return(newSolution); }
public List <string> Print_Solution(GAEncoding solution) { string theta = null; string weight = null; for (int i = 0; i < solution.thetaDelta.RowCount; i++) { string tmp = "("; Vector <double> vTemp = solution.thetaDelta.Row(i); for (int j = 0; j < dimension; j++) { tmp = tmp + Math.Round(vTemp[j], 2) + " "; } tmp.Remove(tmp.Length - 1, 1); tmp = tmp + ")"; theta = theta + tmp + " "; } theta.Remove(theta.Length - 1, 1); for (int i = 0; i < solution.weights.RowCount; i++) { string tmp = "["; Vector <double> vTemp = solution.weights.Row(i); for (int j = 0; j < dimension; j++) { tmp = tmp + Math.Round(vTemp[j], 2) + " "; } tmp.Remove(tmp.Length - 1, 1); tmp = tmp + "]"; weight = weight + tmp + " "; } weight.Remove(weight.Length - 1, 1); return(new List <string>() { theta, weight }); }
public void Reproduction() { GAEncoding child = new GAEncoding(pNumOfParams, dimension, lowbounds, highbounds); int i = 0; var rankOneSols = cePool.Where(x => x.Key.rank == 1).ToList(); foreach (var s in rankOneSols) { tempPool.Add(Copy.DeepCopy(s.Key), s.Value); } while (tempPool.Count < popSize) { int[] selectedList = FitnessPropotionateSampling(0, popSize - 1, pPanCombSize); child = Copy.DeepCopy(cePool.ElementAt(selectedList[0]).Key); if (GlobalVar.rnd.NextDouble() <= pmCrossOverRate) { if (GlobalVar.rnd.NextDouble() <= 0.8) { child = cePool.ElementAt(0).Key.PanmicticDiscreteRecomb(cePool, selectedList); } else { child = cePool.ElementAt(0).Key.PanmicticAvgRecomb(cePool, selectedList); } } if (GlobalVar.rnd.NextDouble() <= pmMutationRate) { child.RealVectSimpleMutation(); } if (child != null) { tempPool.Add(child, new double[] { -1, -1 }); i = i + 1; } } }