public static void SimulatedAnealing() { int i = 0; bool change = false; //stopwatch.Start(); while (/*temperature > limit &&*/ i < imax) { int r = random.Next(opt2chance + opt2halfchance); if (r < opt2chance) { //Console.WriteLine("opt2"); Bezorger B = getRandomBezorgerNonEmpty; Node n1 = B.getRandomNode; Node n2 = B.getRandomNode; Node ni = n1.next; while (ni != null && ni != n2) { ni = ni.next; } if (ni != n2) { ni = n2; n2 = n1; n1 = ni; } int opt2cost = B.costsFlipOrder(n1, n2); if (ShouldChange(opt2cost, temperature)) { B.FlipOrder(n1, n2); CurrentCost += opt2cost; } } else //(r < opt2chance + opt2halfchance) { //Console.WriteLine("opt2half"); int opt2halfcost = 0; Bezorger B1 = getRandomBezorgerNonEmpty; Node n = B1.getRandomNode; //to make this easier to reason about, actually temporarily changing B1's route // -prevents B2 from returning n as location // -prevents B2's change cost to possibly be wrong. Node reinsertlocationb1 = n.previous; opt2halfcost += B1.costsRemoveNode(n); B1.RemoveNode(n); Bezorger B2 = bezorgers[random.Next(bezorgers.Length)]; Node l = B2.getRandomNodeOrNull; opt2halfcost += B2.costsAddAfter(n, l); if (ShouldChange(opt2halfcost, temperature)) { B2.AddAfter(n, l); CurrentCost += opt2halfcost; } else { B1.AddAfter(n, reinsertlocationb1); //put back n where it belongs, should not change } } if (CurrentCost < BestSolutionCost) { BestSolutionCost = CurrentCost; BestSolutionOutput = StringSolution(); change = true; foreach (Bezorger b in Program.bezorgers) { b.GetLength(); } } //Console.WriteLine("----"); i++; if (i % changepercooldown == 0) { switch (schedule) { case "constant": break; case "linear": temperature -= cooldown; break; case "exponential": temperature *= cooldown; break; case "logarithmic": temperature = EnergyBarrier / Math.Log(i + 1); break; case "speed": Epsilon = -(DateTime.Now.Ticks - tijd.Ticks); if (change) { change = false; tijd = DateTime.Now; } temperature = -V_s * temperature / Epsilon / Math.Sqrt(Capacity); break; } } } //stopwatch.Stop(); if (temperature <= limit) { Console.WriteLine("temperature limit reached"); } if (i >= imax) { Console.WriteLine("iteration limit reached"); } }
static void Main(string[] args) { //initialize uninitialized data, no clue wether these values are correct or not. switch (schedule) { case "constant": temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "linear": cooldown = 0.01; temperature = 15; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "exponential": cooldown = 0.98; temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "logarithmic": OptimalCost = 100; temperature = 11; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "speed": V_s = 1; Epsilon = -5; Capacity = 10; temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; } //load map into nodelist, restaurant into resX+resY InputFromFile("GeoSquare2.txt"); //initialize map-dependant data for (int num = 0; num < bezorgers.Length; num++) { bezorgers[num] = new Bezorger(num); } //create initial solution, optimality is not important, at all at this point for (int i = 0; i < nodelist.Count; i++) { bezorgers[i % bezorgers.Length].AddNodetoEnd(nodelist[i]); } foreach (Bezorger B in bezorgers) { BestSolutionCost += B.routeLength; } CurrentCost = BestSolutionCost; BestSolutionOutput = StringSolution(); Console.WriteLine(BestSolutionCost); Console.WriteLine(BestSolutionOutput); EnergyBarrier = (CurrentCost - OptimalCost) / 10; Capacity = CurrentCost; tijd = DateTime.Now; //optimalize SimulatedAnealing(); //output //BestSolutionOutput = StringSolution(); -> The solution the program has in the last state, is not the bestsolution Console.WriteLine(BestSolutionCost); Console.WriteLine(BestSolutionOutput); foreach (Bezorger b in bezorgers) { Console.WriteLine(b.aantalbezorgingen); } Console.ReadLine(); }
static void Main(string[] args) { //initialize uninitialized data, no clue wether these values are correct or not. switch(schedule) { case "constant": temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "linear": cooldown = 0.01; temperature = 15; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "exponential": cooldown = 0.98; temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "logarithmic": OptimalCost = 100; temperature = 11; limit = 1.0; changepercooldown = 800; imax = 1000000; break; case "speed": V_s = 1; Epsilon = -5; Capacity = 10; temperature = 10.0; limit = 1.0; changepercooldown = 800; imax = 1000000; break; } //load map into nodelist, restaurant into resX+resY InputFromFile("GeoSquare2.txt"); //initialize map-dependant data for (int num = 0; num < bezorgers.Length; num++) bezorgers[num] = new Bezorger(num); //create initial solution, optimality is not important, at all at this point for (int i = 0; i < nodelist.Count; i++) { bezorgers[i % bezorgers.Length].AddNodetoEnd(nodelist[i]); } foreach (Bezorger B in bezorgers) BestSolutionCost += B.routeLength; CurrentCost = BestSolutionCost; BestSolutionOutput = StringSolution(); Console.WriteLine(BestSolutionCost); Console.WriteLine(BestSolutionOutput); EnergyBarrier = (CurrentCost - OptimalCost)/10; Capacity = CurrentCost; tijd = DateTime.Now; //optimalize SimulatedAnealing(); //output //BestSolutionOutput = StringSolution(); -> The solution the program has in the last state, is not the bestsolution Console.WriteLine(BestSolutionCost); Console.WriteLine(BestSolutionOutput); foreach (Bezorger b in bezorgers) Console.WriteLine(b.aantalbezorgingen); Console.ReadLine(); }