public IActionResult LocalBeamSearchA(string stringArray, int dim, int states, int maxCounter) { int[] array = _IFunctions.StringToInt(stringArray, dim); int[][] Array = _IFunctions.array1Dto2D(array, dim); State[] BestStates = new State[states]; for (int i = 0; i < states; i++) //generating k random states { BestStates[i] = new State(dim); BestStates[i].niz = _IFunctions.GenerateArray(dim); BestStates[i].h = _IFunctions.Heuristic(BestStates[i].niz, dim); } bool change = true; while (change) //sort initial states { change = false; for (int i = 0; i < states - 1; i++) { if (BestStates[i].h > BestStates[i + 1].h) { State s = BestStates[i]; BestStates[i] = BestStates[i + 1]; BestStates[i + 1] = s; change = true; } } } State[] allStates = new State[states * states]; int counter2 = 0; while (BestStates[0].h != 0 && counter2 < maxCounter) // do until we find the state with h0, or until the counter reaches final status { int counter = 0; for (int i = 0; i < states; i++) { for (int j = 0; j < states; j++) { allStates[counter] = new State(dim); allStates[counter].niz = _IFunctions.rndSuccessor(_IFunctions.CopyRow(BestStates[j].niz, dim), dim); //for k states we are making k random single moves allStates[counter].h = _IFunctions.Heuristic(allStates[counter].niz, dim); counter++; } } change = true; //sort expanded array while (change) { change = false; for (int i = 0; i < states * states - 1; i++) { if (allStates[i].h > allStates[i + 1].h) { State s = allStates[i]; allStates[i] = allStates[i + 1]; allStates[i + 1] = s; change = true; } } } for (int i = 0; i < states; i++) //selecting k best states { BestStates[i].niz = _IFunctions.CopyRow(allStates[i].niz, dim); BestStates[i].h = _IFunctions.Heuristic(allStates[i].niz, dim); } counter2++; } LocalBeamSearchVM VM = new LocalBeamSearchVM() { array = _IFunctions.array2Dto1D(BestStates[0].niz, dim), dimension = dim, heuristic = _IFunctions.Heuristic(BestStates[0].niz, dim), dimensions = new List <SelectListItem>() { new SelectListItem { Text = "4x4", Value = "4" }, new SelectListItem { Text = "5x5", Value = "5" }, new SelectListItem { Text = "6x6", Value = "6" }, new SelectListItem { Text = "7x7", Value = "7" }, new SelectListItem { Text = "8x8", Value = "8" }, new SelectListItem { Text = "9x9", Value = "9" }, new SelectListItem { Text = "10x10", Value = "10" }, new SelectListItem { Text = "11x11", Value = "11" }, new SelectListItem { Text = "12x12", Value = "12" }, }, counter = counter2, nmbrStates = states, maxCounter = maxCounter }; return(PartialView("LocalBeamSearchDone", VM)); }
public IActionResult SimulatedAnealingA(string stringArray, int dim, int T0, int coolingFactor) { int[] array = _IFunctions.StringToInt(stringArray, dim); int[][] Array = _IFunctions.array1Dto2D(array, dim); int[][] array2; int deltaH; int Hs = 1; int counter = 0; while (T0 > 0 && Hs != 0) //do until the temperature reaches 0, or we found state with h0 { array2 = _IFunctions.rndSuccessor(Array, dim); //make single random move Hs = _IFunctions.Heuristic(array2, dim); deltaH = Hs - _IFunctions.Heuristic(Array, dim); if (deltaH < 0) //if the random state is better than previous state we use it as current state { Array = _IFunctions.CopyRow(array2, dim); } else if (Math.Pow(2.71828, -(deltaH / T0)) > new Random().NextDouble()) //if the state is worse than previous state, we calculate propability { //for selecting it anyway Array = _IFunctions.CopyRow(array2, dim); } T0 -= coolingFactor; //decreasing temperature counter++; } SimulatedAnealingVM VM = new SimulatedAnealingVM() { array = _IFunctions.array2Dto1D(Array, dim), dimension = dim, heuristic = _IFunctions.Heuristic(Array, dim), dimensions = new List <SelectListItem>() { new SelectListItem { Text = "4x4", Value = "4" }, new SelectListItem { Text = "5x5", Value = "5" }, new SelectListItem { Text = "6x6", Value = "6" }, new SelectListItem { Text = "7x7", Value = "7" }, new SelectListItem { Text = "8x8", Value = "8" }, new SelectListItem { Text = "9x9", Value = "9" }, new SelectListItem { Text = "10x10", Value = "10" }, new SelectListItem { Text = "11x11", Value = "11" }, new SelectListItem { Text = "12x12", Value = "12" }, }, counter = counter, T0 = T0, coolingFactor = coolingFactor }; return(PartialView("SimulatedAnealingDone", VM)); }
public int[][] GetBestH(int[][] array, int dim, int startH) { int h = startH; int h2; int[][] array2 = _IFunctions.CopyRow(array, dim); int[][] nizFinal = _IFunctions.CopyRow(array, dim); List <int> Is = new List <int>(); List <int> Js = new List <int>(); List <int> Hs = new List <int>(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (array[i][j] != 1) //searching space where the queen is not in for each column { array2[i][j] = 1; //setting queen in new position for (int k = 0; k < dim; k++) { if (array2[k][j] == 1 && i != k) { array2[k][j] = 0; //remove old position for queen break; } } h2 = _IFunctions.Heuristic(array2, dim); //Calculate heurstic for array with new position of queen if (h2 < h) { h = h2; nizFinal = _IFunctions.CopyRow(array2, dim); } else if (h2 == h) { Is.Add(i); Js.Add(j); Hs.Add(h2); } array2 = _IFunctions.CopyRow(array, dim); } } } if (h >= startH) // if there are states with same heuristic we randomly pick state { for (int i = 0; i < Hs.Count(); i++) { if (Hs[i] != h) { Is.RemoveAt(i); Js.RemoveAt(i); Hs.RemoveAt(i); } } int rnd = new Random().Next(0, Is.Count()); for (int i = 0; i < dim; i++) { if (nizFinal[i][Js[rnd]] == 1) { nizFinal[i][Js[rnd]] = 0; break; } } nizFinal[Is[rnd]][Js[rnd]] = 1; } return(nizFinal); }