So this is trying to be a bruteforce method to a variation of the TSP. In this variation you start at a certain warehouse and you have a weight limit, you must pick up all of the packages at a warehouse and must take the roads from that warehouse only and after you deliver all of the packages you have to return to the starting warehouse. But I get stack overflow (how funny) from the method bellow, probably an infinite loop I haven’t caught.
Tried ChatGPT. Didn’t work.
Here is the code:
`public List<List> InitialTrip(List warehouses)
{
List<List> trails = new List<List>();
Trip(this, warehouses, trails);
return trails;
}
private void Trip(Bus bus, List<Warehouse> warehouses, List<List<string>> trails)
{
if (bus.emptyWarehouses == warehouses.Count && bus.CurrentWarehouse.Name == bus.startingWarehouseName)
{
trails.Add(new List<string>(bus.Trail));
return;
}
foreach (Road road in bus.CurrentWarehouse.Roads)
{
Bus busCopy = new Bus(bus);
busCopy.UnloadPackages();
if (busCopy.CanLoadPackages(busCopy.CurrentWarehouse.PickPacks))
{
busCopy.LoadPackages();
}
else
{
continue;
}
if (busCopy.CurrentWarehouse.PickPacks.Count == 0 && busCopy.CurrentWarehouse.DropPacks.Count == 0)
{
busCopy.emptyWarehouses++;
}
string previousWarehouse = busCopy.CurrentWarehouse.Name;
busCopy.Trail.Add(road.End);
busCopy.MoveToWarehouse(Warehouse.FindByName(road.End, warehouses));
Trip(busCopy, warehouses, trails);
// Restore the busCopy state
busCopy.Trail.RemoveAt(busCopy.Trail.Count - 1);
busCopy.MoveToWarehouse(Warehouse.FindByName(previousWarehouse, warehouses));
}
}
public Bus(Bus busCopy)
{
CurrentWarehouse = busCopy.CurrentWarehouse;
startingWarehouseName = busCopy.startingWarehouseName;
lastVisitedWarehouse = busCopy.lastVisitedWarehouse;
emptyWarehouses = busCopy.emptyWarehouses;
WeightLimit = busCopy.WeightLimit;
CurrentWeight = busCopy.CurrentWeight;
Packages = new List<Package>(busCopy.Packages);
Trail = new List<string>(busCopy.Trail);
}`