I am currently having a problem where I an trying to generate a list of random Tour objects (Travelling Salesman). When I debug through the code, everything works fine, but when I just run the code (or even run over this section of the code) my list is full of all of the same object.
for (int i = 0; i < populationSize; i++)
{
var newTour = new Tour(distanceCalculator);
newTour.GenerateIndividual();
this.Tours[i] = newTour;
newTour = null;
}
The GenerateIndividual
method is a method on the Tour
object which fills the tour with cities, then randomizes it:
public void GenerateIndividual()
{
Path = new List<Location>(new Location[GenericGraph.NumberOfLocations()]);
// Loop through all our destination cities and add them to our tour
for (int cityIndex = 0; cityIndex < GenericGraph.NumberOfLocations(); cityIndex++)
{
SetLocation(cityIndex, GenericGraph.LocationList[cityIndex]);
}
// Randomly reorder the tour
Path = Path.Shuffle().ToList();
}
I know the shuffle is working because the Path is always in a random order. The problem is the Tour in the Path is only updated if I debug into the for-loop. For example, if my populationSize
is 10, and I debug through 5 times, I will have a population with 5 randomized Tours, then the final 5 tours will be the same as the last I debugged over. What is happening here? Is the newTour
object only being reset when I debug, but when I run C# is using the same object over and over?