FongEšŸ‘‘
FongEšŸ‘‘
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
The problem is that the number of cities is taken from the textbox
if (int.TryParse(txtCitiesCountTextBox.Text, out numCities) && numCities > 0)
{
cities = GenerateCities(numCities);
DrawCities();
}
if (int.TryParse(txtCitiesCountTextBox.Text, out numCities) && numCities > 0)
{
cities = GenerateCities(numCities);
DrawCities();
}
16 replies
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
I donā€™t quite understand where to write this part of the code, does it replace my GenerateCities method?
16 replies
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
Cities are created through this function
private List<Point> GenerateCities(int numCities)
{
List<Point> cities = new List<Point>();
for (int i = 0; i < numCities; i++)
{
int x = random.Next(picDrawing.Width - 10);
int y = random.Next(picDrawing.Height - 10);
cities.Add(new Point(x, y));
}
return cities;
}
private List<Point> GenerateCities(int numCities)
{
List<Point> cities = new List<Point>();
for (int i = 0; i < numCities; i++)
{
int x = random.Next(picDrawing.Width - 10);
int y = random.Next(picDrawing.Height - 10);
cities.Add(new Point(x, y));
}
return cities;
}
16 replies
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
Now I have created an EuclideanPath class object in the CalculateEnergy function. The class object looks like this: EuclideanPath euclideanPath = new EuclideanPath(cities); But it says that the name сities does not exist in the current context
16 replies
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
I understand there is an error somewhere here
public double CalculateEnergy(int[] permutation)
{
double energy = 0;
for (int i = 0; i < permutation.Length - 1; i++)
{
energy += EuclideanPath.CalculateDistance(permutation[i], permutation[i + 1]);
}
energy += EuclideanPath.CalculateDistance(permutation.Last(), permutation.First());
return energy;
}
public class EuclideanPath : IEuclideanPath
{
private List<Point> cities;

public EuclideanPath(List<Point> cities)
{
this.cities = cities;
}

public double CalculateDistance(int city1, int city2)
{
Point p1 = cities[city1];
Point p2 = cities[city2];
return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
}
}
public double CalculateEnergy(int[] permutation)
{
double energy = 0;
for (int i = 0; i < permutation.Length - 1; i++)
{
energy += EuclideanPath.CalculateDistance(permutation[i], permutation[i + 1]);
}
energy += EuclideanPath.CalculateDistance(permutation.Last(), permutation.First());
return energy;
}
public class EuclideanPath : IEuclideanPath
{
private List<Point> cities;

public EuclideanPath(List<Point> cities)
{
this.cities = cities;
}

public double CalculateDistance(int city1, int city2)
{
Point p1 = cities[city1];
Point p2 = cities[city2];
return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
}
}
16 replies
CC#
Created by FongEšŸ‘‘ on 12/18/2023 in #help
I really need to complete the simulated annealing method for the traveling salesman problem
When I enter all the values I only get 1 error(screenshot)
16 replies