static void MethodOfBranchAndBoundary(long startPointId) { int NUMBER_CITIES = 11; double[,] array = new double[11, 11]; int i = 0; int j = 0; idHospitals.Insert(0, startPointId); for (i = 0; i < 11; ++i) { for (j = 0; j < 11; ++j) { if (i != j) { AStarAlgorithm(idHospitals[i], idHospitals[j], 1); array[i, j] = AStarWeigth[1]; } else { array[i, j] = 100000; } } } double[,] maintemp = new double[NUMBER_CITIES, NUMBER_CITIES]; double x = 0; x = reduce(array, x, 9999, 9999); lobject l1 = new lobject(NUMBER_CITIES); l1.city = 0; l1.cost = x; l1.matrix = array; l1.st.Push(0); l1.remainingcity = new int[NUMBER_CITIES - 1]; l1.city_left_to_expand = NUMBER_CITIES - 1; for (int o = 0; o < 10; o++) { l1.remainingcity[o] = o + 1; } int count = 0; Stack <lobject> s = new Stack <lobject>(); s.Push(l1); temp_best_solution = new lobject(NUMBER_CITIES); double current_best_cost = 1000000; while (s.Count != 0) { List <lobject> main = new List <lobject>(); lobject hell = new lobject(NUMBER_CITIES); hell = s.Pop(); //Console.WriteLine("{0}",s.Count); if (hell.city_left_to_expand == 0) { if (hell.cost <= current_best_cost) { temp_best_solution = hell; current_best_cost = temp_best_solution.cost; } } else if (hell.city_left_to_expand != 0) { if (hell.cost <= current_best_cost) { count++; expand(main, hell); double[] arrow = new double[main.Count()]; for (int pi = 0; pi < main.Count(); pi++) { lobject help = (lobject)main.ElementAt(pi); arrow[pi] = help.cost; } double[] tempppp = decreasing_sort(arrow); for (int pi = 0; pi < tempppp.Length; pi++) { s.Push((lobject)main.ElementAt((int)tempppp[pi])); } } } } List <long> startPointAddList = new List <long>(); startPointAddList = AddjestedList[idHospitals[(int)temp_best_solution.st.ElementAt(0)]]; for (int k = 0; k < 10; ++k) { AStarAlgorithm(idHospitals[(int)temp_best_solution.st.ElementAt(k)], idHospitals[(int)temp_best_solution.st.ElementAt(k + 1)], k); NeighborWays[k] = AStarWays[k]; if ((int)temp_best_solution.st.ElementAt(k) == 0) { orderOfHospitals[0] = (int)temp_best_solution.st.ElementAt(k); } AddjestedList.Remove(idHospitals[(int)temp_best_solution.st.ElementAt(k)]); } AddjestedList.Add(idHospitals[(int)temp_best_solution.st.ElementAt(0)], startPointAddList); AStarAlgorithm(idHospitals[(int)temp_best_solution.st.ElementAt(10)], idHospitals[(int)temp_best_solution.st.ElementAt(0)], 10); NeighborWays[10] = AStarWays[10]; idHospitals.RemoveAt(0); }
public static void expand(List <lobject> l, lobject o) { int length = o.remainingcity.Length; for (int i = 0; i < length; i++) { if (o.remainingcity[i] == 0) { continue; } double cost = 0; cost = o.cost; int city = o.city; Stack <double> st = new Stack <double>(); for (int st_i = 0; st_i < o.st.Count; st_i++) { double k = o.st.ElementAt(st_i); //Console.WriteLine(k); st.Push(k); } st.Push(o.remainingcity[i]); double[,] temparray = new double[11, 11]; for (int i_1 = 0; i_1 < 5; i_1++) { for (int i_2 = 0; i_2 < 5; i_2++) { temparray[i_1, i_2] = o.matrix[i_1, i_2]; } } cost = cost + temparray[city, o.remainingcity[i]]; for (int j = 0; j < 11; j++) { temparray[city, j] = 9999; temparray[j, o.remainingcity[i]] = 9999; } temparray[o.remainingcity[i], 0] = 9999; double cost1 = reduce(temparray, cost, city, o.remainingcity[i]); lobject finall = new lobject(11); finall.city = o.remainingcity[i]; finall.cost = cost1; finall.matrix = temparray; int[] temp_array = new int[o.remainingcity.Length]; for (int i_3 = 0; i_3 < temp_array.Length; i_3++) { temp_array[i_3] = o.remainingcity[i_3]; } temp_array[i] = 0; finall.remainingcity = temp_array; finall.city_left_to_expand = o.city_left_to_expand - 1; finall.st = st; l.Add(finall); } }