void init(City[] Cities) { r = new Random(SEED); species = new List <individual>(POPULATION); // Initialize to random cities for (int j = 0; j < POPULATION; j++) { species.Add(new individual(Cities.Length)); // Default solve problem int[] perm = new int[Cities.Length]; //{ int i, swap, temp1, count = 0; string[] results = new string[3]; ArrayList Route = new ArrayList(); Random rnd = new Random(j); do { for (i = 0; i < perm.Length; i++) // create a random permutation template { perm[i] = i; } for (i = 0; i < perm.Length; i++) { swap = i; while (swap == i) { swap = rnd.Next(0, Cities.Length); } temp1 = perm[i]; perm[i] = perm[swap]; perm[swap] = temp1; } Route.Clear(); for (i = 0; i < Cities.Length; i++) // Now build the route using the random permutation { Route.Add(Cities[perm[i]]); } bssf = new ProblemAndSolver.TSPSolution(Route); count++; } while (pAS.costOfBssf() == double.PositiveInfinity); // until a valid route is found //} species[j].copy(perm); //int[] temp1 = ; //species.Add(new individual(temp1)); species[j].cost = determineFitness(ref Cities, species[j].path); } // Borrowing my generate cost function BnB temp = new BnB(); temp.generateCosts(Cities, out cityBase); }
// Determine the fitness by calculating the length. // Space and time complexity = o(n). double determineFitness(ref City[] Cities, int[] path) { ArrayList Route = new ArrayList(); Route.Clear(); for (int i = 0; i < Cities.Length; i++) { Route.Add(Cities[path[i]]); } ProblemAndSolver.TSPSolution bssf = new ProblemAndSolver.TSPSolution(Route); return(bssf.costOfRoute()); }
public void solve(ref City[] Cities, int maxTime, ref string timeOut, ref int bssfUpdates, ref ProblemAndSolver.TSPSolution bssf, ref double bssfCost) { Stopwatch timer = new Stopwatch(); timer.Start(); // At the very least, stop when you run out of time while (timer.ElapsedMilliseconds < maxTime) { init(Cities); // Pick two parents int indexA = parentSelection(-1, Cities.Length); int[] parentA = species[indexA].path; int[] parentB = species[parentSelection(indexA, Cities.Length)].path; // Have them reproduce individual child = new individual(); child.path = reproduce(ref parentA, ref parentB, Cities.Length); child.cost = determineFitness(ref Cities, child.path); // Store the child somewhere // TODO - DO SOMETHING COOL // for now just store at least cost guy. If none, don't keep for (int i = 0; i < Cities.Length; i++) { if (species[i].cost > child.cost) { species[i] = new individual(child); bssfUpdates++; break; } } } // Find out the best int best = 0; for (int i = 1; i < Cities.Length; i++) { if (species[i].cost < species[best].cost) { best = i; } } ArrayList Route = new ArrayList(); Route.Clear(); for (int i = 0; i < species[best].path.Length; i++) { Route.Add(Cities[species[best].path[i]]); } bssf = new ProblemAndSolver.TSPSolution(Route); bssfCost = bssf.costOfRoute(); // Return variables timer.Stop(); timeOut = timer.Elapsed.ToString(); }
// Space complexity = o(n^2*v). For each edge (v) we need an array of nodes^2. // Time complexity = o(v*n*(n^2+v)) or o(v*n^3 + nv^2). For each edge (v) we need to go through a loop n times that calls walkToNextNode which is n^2. And addToNodeList which is v. public void solve(priorityQueue H, int startNode, City[] Cities, ref double costOfBssf, ref int countHittingBottom, ref ProblemAndSolver.TSPSolution bssf, int maxTime, ref string timeOut, ref int bssfUpdates, ref int maxUsedSize, ref int pruned, ref int generatedNodes) { // variables int cityCount; ArrayList Route = new ArrayList(); Stopwatch timer = new Stopwatch(); timer.Start(); // First set the first node generatedNodes++; node start; generateCosts(Cities, out start); //testGenerateCosts(Cities,out start); cityCount = start.costs.Length; // dynamic so it is decoupled to City[] int maxQueue = 10000 * cityCount; start.path = new List <int>(maxQueue); start.path.Add(startNode); reducer(ref start); addToNodeList(ref nodes, start); // Make the queue and send it in H.makequeue(maxQueue); H.insert(startNode, 0); // doesn't matter the weight. Just use 0 // Note max_time is in milliseconds while (!H.isEmpty() && timer.ElapsedMilliseconds < maxTime) { int u = H.deletemin(); node current = nodes[u]; // u is a pointer to the nodes list. // If worse then what we have found so far, prune. if (current.bound >= costOfBssf) { // We will prune it. Inc pruned. pruned++; // clear from node list by allowing to skip to end } else { bool didGenerateNodes = false; // generate children for (int i = 0; i < cityCount; i++) { // skip if you can't go there. if (current.costs[current.getLastVisitedNode()][i] == INFINITY) { continue; } generatedNodes++; // We are making a new node didGenerateNodes = true; node temp = new node(current);; // Deep copy walkToNextNode(ref temp, current.getLastVisitedNode(), i); if (temp.bound >= costOfBssf) // if it's not worth it, don't add to queue { pruned++; } else // otherwise add to array and queue { int index = addToNodeList(ref nodes, temp); if (!H.insert(index, queueWeight(temp))) { throw new Exception("Out of space"); } } } // If you haven't generated any nodes (ie you can't go anywhere), see if you are done if (!didGenerateNodes) { if (current.path.Count == cityCount + 1) { // You have got to them all and back to start, good job! // Save Route.Clear(); for (int i = 0; i < current.path.Count; i++) { Route.Add(Cities[current.path[i]]); } bssf = new ProblemAndSolver.TSPSolution(Route); costOfBssf = bssf.costOfRoute(); //costOfBssf = current.bound; bssfUpdates++; } } } // clear from node list nodes[u].deleted = true; } // You are done so update variables to pass back timer.Stop(); timeOut = timer.Elapsed.ToString(); maxUsedSize = H.maxUsedSize; }