private IList <TaskPair> FindParents() { ICollection <TaskPair> parentSet = new SortedSet <TaskPair> { new TaskPair(IndividualTask.X_start, IndividualTask.count_fx(IndividualTask.X_start)) }; for (int i = 0; i < parentsQuantity - 1; i++) { TaskPair newTaskPair = new TaskPair(); int[] x_new = new int[n]; Array.Copy(IndividualTask.X_start, x_new, n); for (int j = 0; j < n; j++) { int boolval = rand.Next(2); if (boolval == 1) { x_new[j] += rand.Next( Math.Max(-5, (IndividualTask.Constraints[j][0] - IndividualTask.X_start[j])), Math.Min(6, (IndividualTask.Constraints[j][1] - IndividualTask.X_start[j]))); } } newTaskPair.X = x_new; newTaskPair.Fx = IndividualTask.count_fx(x_new); parentSet.Add(newTaskPair); } parentsQuantity = parentsQuantity / 3; if (parentsQuantity % 2 != 0) { parentsQuantity++; } return(parentSet.Take(parentsQuantity).ToList()); }
public TaskPair ExecuteAlgorithm() { IList <TaskPair> parents = FindParents(); long record = parents.First().Fx; int lasting = 0; TaskPair taskPair = get_children(parents, record, lasting); return(taskPair); }
private TaskPair ExecuteGeneticAndSaveResults(Task task) { Genetic genetic = new Genetic(task, 100); timeCounter = Stopwatch.StartNew(); TaskPair pair1 = genetic.ExecuteAlgorithm(); timeCounter.Stop(); geneticTime.Add(timeCounter.ElapsedTicks); return(pair1); }
private TaskPair ExecuteAnnealingAndSaveResults(Task task) { SimulatedAnnealing simulatedAnnealing = new SimulatedAnnealing(task, 200, 10); timeCounter = Stopwatch.StartNew(); TaskPair pair2 = simulatedAnnealing.executeAlgorithm(); timeCounter.Stop(); annealingTime.Add(timeCounter.ElapsedTicks); return(pair2); }
private TaskPair generateChild(TaskPair parent1, TaskPair parent2) //генеруємо нащадка { TaskPair child = new TaskPair(); child.X = new int[this.n]; for (int i = 0; i < n; i++) { double alpha = Convert.ToDouble(rand.Next(-25, 125)) / 100; child.X[i] = Convert.ToInt32(parent1.X[i] + alpha * (parent2.X[i] - parent1.X[i])); } child.Fx = IndividualTask.count_fx(child.X); return(child); }
private bool balanceIsReached(IList <TaskPair> points, TaskPair currentPoint, float epsilon) { IList <double> Ratio = new List <double>(); foreach (TaskPair point in points) { if (point != currentPoint) { Ratio.Add(Convert.ToDouble(Math.Abs(point.Fx - currentPoint.Fx)) / point.Fx); } } return((Ratio.Count != 0)?(Ratio.Min() <= epsilon):false); }
private bool childIsAdmissible(TaskPair child) { bool admissible = true; for (int i = 0; i < n; i++) { if (child.X[i] < IndividualTask.Constraints[i][0] || child.X[i] > IndividualTask.Constraints[i][1]) { admissible = false; break; } } return(admissible); }
public CounterResult CountTime(int i, int productsQuantity, int leftSum, int rightSum, int leftAmount, int rightAmount) { List <TaskPair[]> Fxs = new List <TaskPair[]>(); CounterResult result = new CounterResult(); //int i = 1000; //user-defined number of Individual Tasks to generate int geneticBetter = 0; int annealBetter = 0; int wolfBetter = 0; //int productsQuantity = 3; //may be randomized or user-defined while (i != 0) { Order generatedOrder = RandomHandler.GetRandomizedOrder(productsQuantity, leftSum, rightSum, leftAmount, rightAmount); Task task = ConvertOrderToTask(generatedOrder); TaskPair pair1 = ExecuteGeneticAndSaveResults(task); TaskPair pair2 = ExecuteAnnealingAndSaveResults(task); TaskPair pair3 = ExecuteFrankWolfAndSaveResults(generatedOrder, task); TaskPair[] pairs = new TaskPair[3] { pair1, pair2, pair3 }; //counting situations when one algorithm gives results better than other //(you can remove it or use to compare results) if (pair1.Fx != 0 || pair2.Fx != 0 || pair3.Fx != 0) { if (pair1.Fx <= pair2.Fx && pair1.Fx <= pair3.Fx) { geneticBetter++; } if (pair2.Fx <= pair1.Fx && pair2.Fx <= pair3.Fx) { annealBetter++; } if (pair3.Fx <= pair1.Fx && pair3.Fx <= pair2.Fx) { wolfBetter++; } } Fxs.Add(pairs); i--; } result.AnnealAvg = annealingTime.Average(); result.GeneticAvg = geneticTime.Average(); result.WolfAvg = wolfTime.Average(); result.GeneticBetter = geneticBetter; result.AnnealBetter = annealBetter; result.FrankBetter = wolfBetter; return(result); }
public TaskPair executeAlgorithm() { int temperature = startTemperature; int h = 0; float epsilon = 0.01F; int[] x_current = new int[n]; Array.Copy(IndividualTask.X_start, x_current, n); TaskPair bestSolution = new TaskPair(x_current, IndividualTask.count_fx(x_current)); TaskPair currentSolution = bestSolution; while (h <= H && bestSolution.Fx != 0 && temperature != 0) { IList <TaskPair> z = new List <TaskPair>(); while (bestSolution.Fx != 0 && !balanceIsReached(z, currentSolution, epsilon)) { z.Add(currentSolution); TaskPair Y = generateNewPoint(x_current); if (Y.Fx == 0) { bestSolution = Y; break; } long delta = Y.Fx - currentSolution.Fx; double p = Math.Min(1, Math.Pow(Math.E, (-delta / temperature))); int minKsi = 75 + (75 * (temperature - startTemperature) / startTemperature); int maxKsi = 100 * temperature / startTemperature; double ksi = Convert.ToDouble(rand.Next(minKsi, maxKsi + 1)) / 100; if (p > ksi || delta < 0) { currentSolution = Y; if (Y.Fx < bestSolution.Fx) { bestSolution = Y; } } } h++; double alpha = Convert.ToDouble(rand.Next(50, 100)) / 100; temperature = Convert.ToInt32(temperature * alpha); } return(bestSolution); }
private TaskPair get_children(IList <TaskPair> parents, long record, int lasting) { if (parents.Count == 1) { return(parents.ElementAt(0)); } if (parents.Count % 2 != 0) { parents.RemoveAt(parents.Count - 1); } ICollection <TaskPair> iterationNodes = new SortedSet <TaskPair>(); foreach (TaskPair parent in parents) { iterationNodes.Add(parent); } TaskPair.Shuffle(parents); for (int k = 0; k < parents.Count; k += 2) { int quantity = rand.Next(1, 5); while (quantity != 0) { TaskPair child = generateChild(parents[k], parents[k + 1]); ChildMutation(child); if (childIsAdmissible(child)) { child.Fx = IndividualTask.count_fx(child.X); iterationNodes.Add(child); quantity--; } } } long newRecord = iterationNodes.First().Fx; if ((newRecord == record && lasting == 99) || newRecord == 0) { return(iterationNodes.First()); } else { IList <TaskPair> iterationNodesList = iterationNodes .Take(3 * iterationNodes.Count / 4) .ToList(); IList <TaskPair> new_nodes = iterationNodes .Take(parentsQuantity / 2) .ToList(); foreach (TaskPair addedNode in new_nodes) { iterationNodesList.Remove(addedNode); } TaskPair.Shuffle(iterationNodesList); for (int i = 0; i < Math.Min(parentsQuantity / 2, iterationNodesList.Count); i++) { new_nodes.Add(iterationNodesList.ElementAt(i)); } if (new_nodes.Count % 2 != 0) { new_nodes.RemoveAt(new_nodes.Count - 1); } if (newRecord == record) { return(get_children(new_nodes, newRecord, lasting + 1)); } else { return(get_children(new_nodes, newRecord, 0)); } } }
private void ChildMutation(TaskPair child) { int IdxToMutate = rand.Next(n); child.X[IdxToMutate]--; }