C
C#•17mo ago
FongEšŸ‘‘

I really need to complete the simulated annealing method for the traveling salesman problem

Deadlines are running out, there is a program that should work, but it has errors that I cannot fix due to poor knowledge of the code, please help. I don't even know what part of the code is wrong
No description
10 Replies
FongEšŸ‘‘
FongEšŸ‘‘OP•17mo ago
When I enter all the values I only get 1 error(screenshot) 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));
}
}
MiKom
MiKom•17mo ago
Do you create a new instance of the EuclideanPath class anywhere? The error is weird though because it seems like you're calling a non-static method on a class name. That shouldn't even compile.
FongEšŸ‘‘
FongEšŸ‘‘OP•17mo ago
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
MiKom
MiKom•17mo ago
Well, EuclideanPath constructor takes cities as a parameter, it has to come from somewhere. I presume that cities should be an input to the program? So you probably have to construct the liset of the cities by reading some data from somewhere.
FongEšŸ‘‘
FongEšŸ‘‘OP•17mo ago
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;
}
MiKom
MiKom•17mo ago
ok, then:
var cities = GenerateCities(20); //or whatever number you want
EuclideanPath euclideanPath = new(cities);
var cities = GenerateCities(20); //or whatever number you want
EuclideanPath euclideanPath = new(cities);
should work
FongEšŸ‘‘
FongEšŸ‘‘OP•17mo ago
I don’t quite understand where to write this part of the code, does it replace my GenerateCities method?
MiKom
MiKom•17mo ago
probably in the "CalculateEnergy" method also, I updated the code above it was wrong and then replace EuclideanPath with euclideanPath. Class with an instance
FongEšŸ‘‘
FongEšŸ‘‘OP•17mo ago
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();
}
MiKom
MiKom•17mo ago
Do you have a debugger available?

Did you find this page helpful?