public HopfieldTank(TSP p, double A = 2.0, double B = 2.0, double C = 4, double D = 0.9, int epochs = 10000)
        {
            _problem    = p;
            this.A      = A;
            this.B      = B;
            this.D      = D;
            this.C      = C;
            this.epochs = epochs;

            Initialize(new Random().Next(99999));
        }
Beispiel #2
0
        private void radButton1_Click(object sender, EventArgs e)
        {
            Distance_lbl.Text = "0";
            if (HF.IsChecked)
            {
                Tuple <double[, ], List <double> > solution;
                TSP prob = new TSP(LH);
                solution = prob.Solve();
                for (int j = 0; j < CountCities; j++)
                {
                    for (int i = 0; i < CountCities; i++)
                    {
                        if (solution.Item1[i, j] == 1)
                        {
                            Map.AddConnection(j == 0 ? Cities[i] : Map.Connections[Map.Connections.Count - 1].Target,
                                              Cities[i]);

                            RadDiagramConnection connection1 =
                                (RadDiagramConnection)Map.Connections[Map.Connections.Count - 1];
                            connection1.BackColor         = Color.LightSalmon;
                            connection1.AllowDelete       = false;
                            connection1.IsDraggingEnabled = false;
                            connection1.IsEditable        = false;
                            connection1.TargetCapSize     = new SizeF(20, 25);
                            connection1.IsHitTestVisible  = true;
                            connection1.Content           = j + 1;
                            connection1.ForeColor         = Color.LightSalmon;
                            connection1.Font  = MouseCoords_lbl.Font;
                            Distance_lbl.Text =
                                (Convert.ToInt32(Distance_lbl.Text) +
                                 (connection1.StartPoint - connection1.EndPoint).LengthSquared).ToString();
                            Map.Refresh();
                            //connection1.Position = connection1.Target.Position;
                            System.Windows.Forms.Application.DoEvents();
                            break;
                        }
                    }
                }
                for (int i = 0; i < solution.Item2.Count; i++)
                {
                    Energy_list.Items.Add("Epoch: {0} => Energy = {1}", i + 1, solution.Item2[i]);
                }
            }
            if (GA.IsChecked)
            {
                int          j    = 0;
                Genetic.Path path = new GeneticAlgorithm(new Population(LG)).Solve(10000);
                foreach (var city in path.Cities)
                {
                    Map.AddConnection(j == 0 ? Cities[j] : Map.Connections[Map.Connections.Count - 1].Target,
                                      Cities[j]);

                    RadDiagramConnection connection1 =
                        (RadDiagramConnection)Map.Connections[Map.Connections.Count - 1];
                    connection1.BackColor         = Color.LightSalmon;
                    connection1.AllowDelete       = false;
                    connection1.IsDraggingEnabled = false;
                    connection1.IsEditable        = false;
                    connection1.TargetCapSize     = new SizeF(20, 25);
                    connection1.IsHitTestVisible  = true;
                    connection1.Content           = j + 1;
                    connection1.ForeColor         = Color.LightSalmon;
                    connection1.Font  = MouseCoords_lbl.Font;
                    Distance_lbl.Text =
                        (Convert.ToInt32(Distance_lbl.Text) +
                         (connection1.StartPoint - connection1.EndPoint).LengthSquared).ToString();
                    Energy_list.Items.Clear();
                    Map.Refresh();
                    //connection1.Position = connection1.Target.Position;
                    System.Windows.Forms.Application.DoEvents();

                    j++;
                }
            }
            if (SA.IsChecked)
            {
                var result = new SimulatedAnnealing.SimulatedAnnealing(LG).Solve();
                for (int j = 0; j < CountCities; j++)
                {
                    Map.AddConnection(j == 0 ? Cities[j] : Map.Connections[Map.Connections.Count - 1].Target,
                                      Cities[j]);

                    RadDiagramConnection connection1 =
                        (RadDiagramConnection)Map.Connections[Map.Connections.Count - 1];
                    connection1.BackColor         = Color.LightSalmon;
                    connection1.AllowDelete       = false;
                    connection1.IsDraggingEnabled = false;
                    connection1.IsEditable        = false;
                    connection1.TargetCapSize     = new SizeF(20, 25);
                    connection1.IsHitTestVisible  = true;
                    connection1.Content           = j + 1;
                    connection1.ForeColor         = Color.LightSalmon;
                    connection1.Font  = MouseCoords_lbl.Font;
                    Distance_lbl.Text =
                        (Convert.ToInt32(Distance_lbl.Text) +
                         (connection1.StartPoint - connection1.EndPoint).LengthSquared).ToString();
                    Energy_list.Items.Clear();
                    Map.Refresh();
                    //connection1.Position = connection1.Target.Position;
                    System.Windows.Forms.Application.DoEvents();
                }
            }
            //int k = 0;
            //foreach (RadDiagramConnection road in Map.Connections)
            //{
            //    //Console.WriteLine(road.Source.ToString()+road.Target.ToString() );
            //    Console.WriteLine(road.Position);
            //}
        }
        public static void Main(string[] args)
        {
            //string[] lines = File.ReadAllLines(@"D:/distances.txt");
            //int n = Convert.ToInt32(lines[0]), index = 1;
            //var distances = Matrix<double>.Build.Dense(n, n, 0);
            //for (int i = 0; i < n; i++)
            //{
            //    for (int j = 0; j < n; j++)
            //    {
            //        if(i != j)
            //        {
            //            distances[i, j] = Convert.ToDouble(lines[index++]);
            //        }
            //    }
            //}
            //Console.WriteLine();
            ////IList<int> result = new Hopfield(n, distances).Solve();
            //var result = new SimulatedAnnealing.SimulatedAnnealing(distances).Solve();
            //Console.WriteLine($"Path = {result.Item1.Select(x => x.ToString()).Aggregate((x, y) => x + " " + y)}\nCost = {result.Item2}");
            //Console.ReadLine();

            //cityLocations(1,:) = [0 3];
            //cityLocations(2,:) = [1 5];
            //cityLocations(3,:) = [4 5];
            //cityLocations(4,:) = [5 2];
            //cityLocations(5,:) = [4 0];
            //cityLocations(6,:) = [1 0];
            List <City> l = new List <City>();

            //l.Add(new City { Name = "city1", Position = new PointF(0, 3) });
            //l.Add(new City { Name = "city2", Position = new PointF(1, 5) });
            //l.Add(new City { Name = "city3", Position = new PointF(4, 5) });
            //l.Add(new City { Name = "city4", Position = new PointF(5, 2) });
            //l.Add(new City { Name = "city5", Position = new PointF(4, 0) });
            //l.Add(new City { Name = "city6", Position = new PointF(1, 0) });

            l.Add(new City {
                Name = "city1", Position = new PointF(3, 3)
            });
            l.Add(new City {
                Name = "city2", Position = new PointF(4, 5)
            });
            l.Add(new City {
                Name = "city3", Position = new PointF(5, 1)
            });
            l.Add(new City {
                Name = "city4", Position = new PointF(7, 3)
            });

            //City city1 = new City { Name = "city1", Position = new PointF(8, 6) },
            //     city2 = new City { Name = "city2", Position = new PointF(0, 0) },
            //     city3 = new City { Name = "city3", Position = new PointF(35, 0) },
            //     city4 = new City { Name = "city4", Position = new PointF(4, 3) };
            //l.Add(city1);
            //l.Add(city2);
            //l.Add(city3);
            //l.Add(city4);
            //TSP prob = new TSP(l);
            //prob.Solve();

            //int index = 1;
            //File.ReadAllLines(@"D:/tspTest.txt").ToList().ForEach(line => { var vals = Regex.Split(line, " +").Select(e => Convert.ToDouble(e)).ToArray();
            //l.Add(new City() { Name = "City" + index++, Position = new PointF((float)vals[0], (float)vals[1]) } );});
            //Genetic.Path path = new GeneticAlgorithm(new Population(l, 48)).Solve(10000);
            //Console.WriteLine($"Path = {path.Cities.Select(c => c.Value.ToString()).Aggregate((f, s) => f + "-" + s)}\nCost = {path.Cities.Zip(path.Cities.Skip(1), (f, s) => new Tuple<City?, City?>(f, s)).Select(e => e.Item1.Value.DistanceTo(e.Item2.Value)).Sum()}");
            //Console.ReadKey();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RadForm2());
            TSP prob = new TSP(l);

            //prob.Solve();
            //int index = 1;
            //File.ReadAllLines(@"D:/tspTest.txt").ToList().ForEach(line => { var vals = Regex.Split(line, " +").Select(e => Convert.ToDouble(e)).ToArray(); l.Add(new City() { Name = "City" + index++, Position = new PointF((float)vals[0], (float)vals[1]) } );});
            //Genetic.Path path = new GeneticAlgorithm(new Population(l, 48)).Solve(10000);
            //Console.WriteLine($"Path = {path.Cities.Select(c => c.Value.ToString()).Aggregate((f, s) => f + "-" + s)}\nCost = {path.Cities.Zip(path.Cities.Skip(1), (f, s) => new Tuple<City?, City?>(f, s)).Select(e => e.Item1.Value.DistanceTo(e.Item2.Value)).Sum()}");
            Console.ReadKey();
        }