Esempio n. 1
0
        public int TakeAndRun(Solution s)
        {
            Console.WriteLine("TakeAndRun-Heuristic!");
              // returns the actual costs of the solution
              int total = 0;
              for (int i = 0; i < s.n; i++)
              {
            foreach (Deliveryman d in s.rs)
            {
              int cur_x = 0;
              int cur_y = 0;
              for (int t = 0; t < d.route.Count; t++)
              {
            if (t % 2 == 0)
            {
              int a = Math.Abs(cur_x - s.cs[d.route[t]-1].X);
              int b = Math.Abs(cur_y - s.cs[d.route[t]-1].Y);

              total += a + b;
            }
              }
            }
              }
              return total;
        }
 public SimulatedAnnealing(Solution sol, float temperature, float cooling_rate, int neighbor_function)
 {
     s = sol;
     t = temperature;
     a = cooling_rate;
     g = neighbor_function;
     rnd = new Random();
 }
 public Solution LocalSearch()
 {
     for(int x = 0; x < iterations; x++)
     {
         Solution s_next = s.NextNeighbor(2); //Neighboring state by swapping two customers in route
         cost_cur = s_next.costs(); //Calculate cost of neighboring state
         if (cost_cur < cost_opt) //If cost of neighboring state is less than current optimal...
         {
             cost_opt = cost_cur; //...update optimal cost...
             s = s_next; //...and update best route
             x = 0;
         }
     }
     return s;
 }
Esempio n. 4
0
        public Display(Solution solution)
        {
            InitializeComponent();
            sol = solution;
            this.Text = "Solution Graph (costs: " + sol.costs() + ")";
            this.Size = new Size(800, 800);

            // depot point
            depot = new Point(this.Width / 2, this.Height / 2);

            // different strokes for different folks
            definteBrushes();

            // paint event
            this.Paint += new PaintEventHandler(drawSolution);
        }
Esempio n. 5
0
        public int NoHeuristic(Solution s)
        {
            Console.WriteLine("No Heuristic!");
              // returns the actual costs of the solution
              int total_time = 0;
              for (int i = 0; i < s.n; i++)
              {
            // number of customers on the route
            int nodes = s.rs[i].route.Count;
            // total distance of the route
            int dist = 0;
            // customer id's and pos
            int id_a = 0;
            int x_a = 0;
            int y_a = 0;
            int id_b, x_b, y_b;

            for (int j = 0; j < nodes; j++)
            {
              // get node id
              id_b = s.rs[i].route[j];
              // get node position
              x_b = s.cs[id_b - 1].X;
              y_b = s.cs[id_b - 1].Y;
              // calculate distance between node a and b
              dist += Math.Abs(x_a - x_b) + Math.Abs(y_a - y_b);
              total_time += dist;
              // a = b
              id_a = id_b;
              x_a = x_b;
              y_a = y_b;
            }
            // back to depot
            //dist += Math.Abs(x_a - 0) + Math.Abs(y_a - 0);
              }
              // calculate average delivery duration per route
              return total_time;
        }
Esempio n. 6
0
 public Untwiner(Solution sol)
 {
     s = sol;
 }
        // run Simulated Annealing
        public Solution run()
        {
            int cool = 0;
            int states = 0;
            int accepted_b = 0;
            int accepted_w = 0;
            //double sump = 0;
            //int nump = 0;

            while (t > 0.000001)
            {
                float s_h = s.costs();
                int iteraties = 0;
                //Console.WriteLine("Temperature: " + t.ToString() + " costs: " + s_h.ToString());

                bool accept = false;
                while (!accept && iteraties < 10)
                {
                    Solution next = s.NextNeighbor(g);
                    states++;
                    float next_h = next.costs();
                    iteraties++;
                    if (next_h <= s_h)
                    {
                        s = next;
                        accept = true;
                        accepted_b++;
                    }
                    else
                    {
                        double p = Math.Exp(-(next_h - s_h) / t);

                        // sump += p;
                        // nump++;

                        //Console.WriteLine("kans op acceptatie: " + p + "d: " + (next_h - s_h).ToString());
                        double r = rnd.NextDouble();
                        if (p > r)
                        {
                            // accept solution
                            s = next;
                            accept = true;
                            accepted_w++;
                        }
                    }
                }

                // cooling
                cool++;
                //cool += iteraties;
                if (cool > 16)
                {
                    cool = 0;
                    t = a * t;
                    //Console.WriteLine("gem p: " + (sump / nump).ToString() + " temperature: " + t.ToString() + " p: " + nump.ToString());
                    //Console.ReadLine();
                    //t = 0.000001f;
                }
            }
            //s.Untwine();
            //Console.WriteLine("states generated: " + states + " accepted better: " + accepted_b + " accepted worse: " + accepted_w);
            return s;
        }
 public IterativeImprovement(Solution initial_solution, int iter)
 {
     s = initial_solution;
     iterations = iter;
     cost_opt = s.costs(); //Initial optimal cost
 }
Esempio n. 9
0
        private void InputTests()
        {
            // The files used in this example are created in the topic
              // How to: Write to a Text File. You can change the path and
              // file name to substitute text files of your own.

              // Example #2
              // Read each line of the file into a string array. Each element
              // of the array is one line of the file.
              OpenFileDialog sfd = new OpenFileDialog();
              string s = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
              sfd.InitialDirectory = s;
              sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

              DialogResult dr = sfd.ShowDialog();
              string path = sfd.FileName;
              string name = Path.GetFileNameWithoutExtension(path);
              string[] lines = System.IO.File.ReadAllLines(path);
              nr_II = int.Parse(lines[0].Split(' ')[0]);
              nr_SA = int.Parse(lines[0].Split(' ')[1]);
              lines[0] = "";

              int i = 1;
              gemiddeld_costs = new long[lines.Length - 1];
              gemiddeld_tijd = new double[lines.Length - 1];
              initial_costs = new long[lines.Length - 1];
              N = new int[lines.Length - 1];
              M = new int[lines.Length - 1];
              cool = new double[nr_SA];
              tem = new int[nr_SA];

              foreach (string line in lines)
              {
            if (line != "")
            {
              // Use a tab to indent each line of the file.
              Test t = new Test(line);

              // create initial solutions
              if (!initsolutions.ContainsKey(new Tuple<string, string>(t.custom.ToString(), t.delivery.ToString())))
              {
            // blalj
            // if initial tests do not exist, create them
            int aantaltests = 30;
            Solution[] inits = new Solution[aantaltests];
            Random rnd = new Random();
            for (int a = 0; a < aantaltests; a++)
            {
              Input p = new Input(t.custom, rnd);
              InitialPath init = new InitialPath(p, rnd);
              inits[a] = init.getSolution(t.initial, t.delivery);
            }
            initsolutions.Add(new Tuple<string, string>(t.custom.ToString(), t.delivery.ToString()), inits);
              }

              // run test
              RunTest(t, path, name, i, lines.Length - 1);
              i++;
            }
              }
              Logger l = new Logger(7 * gemiddeld_tijd.Length + 16);
              l.AddLine("Initial-costs:");
              var x = 0;
              foreach (long d in initial_costs)
              {
            if (x == nr_II)
              l.AddLine("");
            l.AddLine(d.ToString());
            x++;
              }
              l.AddLine("");
              x = 0;
              l.AddLine("Gemiddelde costs:");
              foreach (long d in gemiddeld_costs)
              {
            if (x == nr_II)
              l.AddLine("");
            l.AddLine(d.ToString());
            x++;
              }
              l.AddLine("");
              x = 0;
              l.AddLine("Gemiddelde tijd: ");
              foreach (double d in gemiddeld_tijd)
              {
            if (x == nr_II)
              l.AddLine("");
            l.AddLine(d.ToString());
            x++;
              }

              l.AddLine("");
              l.AddLine("N: ");
              foreach (int n in N)
            l.AddLine(n.ToString());

              l.AddLine("");
              l.AddLine("M: ");
              foreach (int m in M)
            l.AddLine(m.ToString());

              l.AddLine("");
              l.AddLine("Cool-rate: ");
              foreach (double m in cool)
            l.AddLine(m.ToString());

              l.AddLine("");
              l.AddLine("Temperature: ");
              foreach (int m in tem)
            l.AddLine(m.ToString());

              l.Write(path, name);
        }