//sift down will always work from the route (0) node public void sift_down(int node) { int c = get_min(node); if (debug) { Console.WriteLine("SIFT DOWN get_min: " + c); } if (c == -1) { //it has no children } else { while (c > 0 && nodes[c].Priority < nodes[node].Priority) { //now need to switch the nodes state_data temp = nodes[node]; nodes[node] = nodes[c]; nodes[c] = temp; //and update the pointers int tempi = pointers[node]; pointers[node] = pointers[c]; pointers[c] = tempi; //The current node is now the child node = c; //update the child c = get_min(node); } } }
public virtual void Load() { // GetState(); base_state temp = (base_state)_state; manager_state.Instance.Load(gameObject.name, out temp, TypeState.MOB); _state = (state_data)temp; // * testing // _healthInst = _state.Health; HealthRestore(); HealthDrain(_health - _state.Health); _resurrect = _state.Resurrect; // _hostiles = _state.Hostiles; _hostiles.Clear(); if (gameObject.layer == game_variables.Instance.LayerMob) _hostiles.Add(controller_player.Instance.Motor.transform); // Clear(); // _target = _state.Target; // _target = null; // _flag = _state.Flag; // if (_collider) // _collider.radius = _state.Radius; // * testing feedback action if (_action) _action.Disable(); // _inventory = _state.Inventory; _timerIframes = 0f; }
//This is what makes insertion take log time because the node might bubble up to the top of the tree private void bubble_up(int node) { if (debug) { Console.WriteLine("BUBBLING"); } if (debug) { Console.WriteLine("weight: " + nodes[node]); } int p = ceiling(node); if (debug) { Console.WriteLine("node: " + node + " p: " + p); } if (debug) { Console.WriteLine("node: " + nodes[node].Priority); } if (debug && node != 0) { Console.WriteLine("p: " + nodes[p].Priority); } //While it's not the root and its parent is bigger than it is while (node != 0 && nodes[p].Priority > nodes[node].Priority) { //now need to switch the nodes state_data temp = nodes[node]; nodes[node] = nodes[p]; nodes[p] = temp; //and update the pointers int tempi = pointers[node]; pointers[node] = pointers[p]; pointers[p] = tempi; //The current node is now the parent node = p; //update the parent p = ceiling(node); if (debug) { Console.WriteLine("IN LOOP:: node: " + node + " p: " + p); } } if (debug) { Console.WriteLine("AT END:: node: " + node + " p: " + p); } if (debug) { Console.WriteLine("weight at end: " + nodes[node]); } }
public virtual void New() { // _healthInst = _health; _resurrect = false; _hostiles.Clear(); if (gameObject.layer == game_variables.Instance.LayerMob) _hostiles.Add(controller_player.Instance.Motor.transform); Clear(); // Save(); // _inventory = 1; _state = new state_data(); }
//Should run in O(log(|V|)) public void insert(state_data data, int id) { if (debug) { Console.WriteLine("INSERTING"); } pointers[next_open] = id; nodes[next_open] = data; bubble_up(next_open); next_open++; if (debug) { Console.WriteLine("next open: " + next_open); } Length++; if (length > Highest) { highest = length; } }
//Should run in O(log(|V|)) public state_data delete_min() { if (debug) { Console.WriteLine("\n DELETE MIN"); } if (debug) { Console.WriteLine("\n Pointers[0] " + pointers[0]); } //set the return index value state_data min = nodes[0]; //change the first value to the last value and clear the last value pointers[0] = pointers[next_open - 1]; nodes[0] = nodes[next_open - 1]; if (debug) { Console.WriteLine("nodes[next_open-1]: " + nodes[next_open - 1]); } if (debug) { Console.WriteLine("nodes[0] after switch: " + nodes[0]); } nodes[next_open - 1] = null; pointers[next_open - 1] = -1; next_open--; if (debug) { Console.WriteLine("next open: " + next_open); } //sift_down the new root node sift_down(0); Length--; return(min); }
/// <summary> /// performs a Branch and Bound search of the state space of partial tours /// stops when time limit expires and uses BSSF as solution /// </summary> /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns> public string[] bBSolveProblem() { string[] results = new string[3]; results[COST] = "not implemented"; // load results into array here, replacing these dummy values results[TIME] = "-1"; results[COUNT] = "-1"; int count = 0; Console.Out.WriteLine("City costs: "); for (int j = 0; j < Cities.Length; j++) { for (int h = 0; h < Cities.Length; h++) { Console.Write(Cities[j].costToGetTo(Cities[h]) + ","); } Console.WriteLine(); } Stopwatch timer = new Stopwatch(); timer.Start(); PriorityQueue queue = new PriorityQueue(); queue.make_queue(Cities.Length * Cities.Length * Cities.Length * Cities.Length); //Here I run the greedy algorithm and use that BSSF result as my starting BSSF greedySolveProblem(); Console.Out.WriteLine("Greedy BSSF: " + costOfBssf()); matrix_and_bound initial_matrix = construct_initial_matrix(); for (int j = 0; j < Cities.Length; j++) { for (int h = 0; h < Cities.Length; h++) { Console.Write(initial_matrix.Matrix[j, h] + ","); } Console.WriteLine(); } Console.WriteLine(initial_matrix.Lower_bound); //Console.WriteLine(); //This loop will do my initial population of the queue by looping through each city and getting the lower bound for (int i = 0; i < Cities.Length; i++) { for (int k = 0; k < Cities.Length; k++) { if (i != k) { //I need to get the lower bound of each reduced matrix and the bound matrix_and_bound current = matrix_reduction(initial_matrix, i, k); //Console.WriteLine(); //If the lower bound is less than current bssf add to queue for later checking, otherwise ignore if (current.Lower_bound < costOfBssf()) { //need to create new state_data object with state data set state_data data = new state_data(); //I guess depth doesn't matter to be exact so long as it's relative, so I'll keep this first one as 0 data.Depth = 0; data.Mb = current; data.add_city(Cities[i]); data.add_city(Cities[k]); data.add_city_index(i); data.add_city_index(k); data.set_priority(); Console.Out.WriteLine("bound " + data.Mb.Lower_bound); //I'm not sure this id is necessary but I'll have to see data.Id = id; queue.insert(data, id); id++; } } } } Console.Out.WriteLine("Queue length Initial " + queue.Length); //now run while queue is not empty and timer is less than 60 while (timer.Elapsed.TotalSeconds < 60 && queue.Length > 0) { //pop off of queue and repeat above matrix reduction process state_data current = queue.delete_min(); //if it's greater or equal to the current bssf I can just ignore it, this is the culling if (current.Mb.Lower_bound < costOfBssf()) { //Priority queue looks like it's working //Console.Out.WriteLine(current.Mb.Lower_bound); /* * Now I need to matrix reduce each child of the current node, * see if it's still smaller than the current bssf if it's a leaf node * I put it as the current bssf, otherwise I push it on the queue */ for (int k = 0; k < Cities.Length; k++) { if (!current.City_list.Contains(k)) { matrix_and_bound child = matrix_reduction(current.Mb, (int)current.City_list[current.City_list.Count - 1], k); //Console.Out.WriteLine("here"); //Console.Out.WriteLine("Current depth: " + ); for (int j = 0; j < Cities.Length; j++) { for (int h = 0; h < Cities.Length; h++) { Console.Write(child.Matrix[j, h] + ","); } Console.WriteLine(); } Console.WriteLine(child.Lower_bound); if (child.Lower_bound < costOfBssf()) { //need to create new state_data object with state data set state_data data = new state_data(); //I guess depth doesn't matter to be exact so long as it's relative, so I'll keep this first one as 0 data.Depth = current.Depth + 1; data.Mb = child; //The last value in the path is the current city data.Path = current.Path; data.City_list = current.City_list; data.add_city(Cities[k]); /* Console.Out.WriteLine("The Current Path and length " + data.City_list.Count); * for (int j = 0; j < data.City_list.Count; j++) * { * Console.Out.Write(data.City_list[j] + "->"); * } * Console.Out.WriteLine();*/ data.City_list = current.City_list; data.add_city_index(k); data.set_priority(); //Console.Out.WriteLine("Set Priority " + data.Priority); //I'm not sure this id is necessary but I'll have to see data.Id = id; id++; // Console.Out.WriteLine("Intermediate Lower Bound " + data.Mb.Lower_bound); if (data.City_list.Count < Cities.Length) { queue.insert(data, id); } else if (data.Mb.Lower_bound < costOfBssf())//it's a leaf node and it's less than the current BSSF { Console.Out.WriteLine("Current Lower Bound " + costOfBssf()); Console.Out.WriteLine("Final Lower Bound " + data.Mb.Lower_bound); for (int j = 0; j < data.City_list.Count; j++) { Console.Out.Write(data.City_list[j] + "->"); } bssf = new TSPSolution(data.Path); count++; } } } } } } results[COST] = costOfBssf().ToString(); results[TIME] = timer.Elapsed.ToString(); results[COUNT] = count.ToString(); return(results); }