static void ExpandStateOneBlank(int boxSize, byte[] state, SuccessorList successors) { int e = 0; while (state[e] != 0) { e += 1; } if (e < (state.Length - boxSize)) { CreateSuccessor(state, successors.Push(), e, e + boxSize); // down } if (e % boxSize < (boxSize - 1)) { CreateSuccessor(state, successors.Push(), e, e + 1); // right } if (e % boxSize > 0) { CreateSuccessor(state, successors.Push(), e, e - 1); // left } if (e > (boxSize - 1)) { CreateSuccessor(state, successors.Push(), e, e - boxSize); // up } }
static byte[][] IDAStarDriver(byte[] initialState, byte[] goal, Func <byte[], uint> h) { idash = h; nextBest = idash(initialState); nodeCounter = 0; int boxSize = (int)Math.Sqrt(initialState.Length); noParent = new byte[initialState.Length]; var bestPath = noSolution; successorList = new SuccessorList(initialState.Length); while (bestPath == noSolution && nextBest != uint.MaxValue) { uint threshold = nextBest; nextBest = uint.MaxValue; bestPath = IDAStar(initialState, boxSize, noParent, goal, 0u, threshold, successorList); Console.WriteLine(); } return(bestPath.ToArray()); }
static Stack <byte[]> IDAStar(byte[] current, int boxSize, byte[] parent, byte[] goal, uint cost, uint upperbound, SuccessorList successors) { if (AreSame(current, goal)) { return(new Stack <byte[]>(new[] { current })); } var newCost = cost + 1; ExpandStateOneBlank(boxSize, current, successors); while (successors.Size > 0) { var successor = successors.Pop(); if (AreSame(successor, parent)) { continue; } nodeCounter += 1; var h = idash(successor); var newF = newCost + h; if (newF > upperbound) { if (newF < nextBest) { nextBest = newF; } } else { var p = IDAStar(successor, boxSize, current, goal, newCost, upperbound, successors.Next); if (p != noSolution) { p.Push(current); return(p); } } } if (parent == noParent || (timer.ElapsedMilliseconds - lastMillis) > 1000) { var nextBestDisplay = nextBest == uint.MaxValue ? "inf" : nextBest.ToString(); Console.Write("\rtime: {0}, upperbound: {1}, nextBest: {2}, eval: {3:n0}", timer.Elapsed.ToString(timerFormat), upperbound, nextBestDisplay, nodeCounter); lastMillis = timer.ElapsedMilliseconds; } return(noSolution); }