private void CheckNodeMove(StorageState current, StorageNode fromNode, StorageNode toNode, ConcurrentQueue <StorageState> neighbours) { if (toNode != null) { if (toNode.SpaceAvailable(current.State) >= fromNode.SpaceUsed(current.State) && fromNode.SpaceAvailable(current.State) >= toNode.SpaceUsed(current.State)) { StorageState moveResult = current.Clone(); // Are we moving the goal data? if (fromNode.X == current.DesiredDataOnX && fromNode.Y == current.DesiredDataOnY) { moveResult.DesiredDataOnX = toNode.X; moveResult.DesiredDataOnY = toNode.Y; } if (toNode.X == current.DesiredDataOnX && toNode.Y == current.DesiredDataOnY) { moveResult.DesiredDataOnX = fromNode.X; moveResult.DesiredDataOnY = fromNode.Y; } int tempStorage = fromNode.SpaceUsed(moveResult.State); moveResult.State[fromNode.NodeName] = moveResult.State[toNode.NodeName]; moveResult.State[toNode.NodeName] = tempStorage; neighbours.Enqueue(moveResult); } } }
private int SolveCostEstimate(StorageState startState) { // Number of spots that an open node is away from the current desired state int openDistance = 200; int openGoalX, openGoalY = 0; bool isGoalNode; int depth; Tuple <int, int> moveOpenTo = startState.CurrentOpenNodeWayPoint(out isGoalNode, out depth); openGoalX = moveOpenTo.Item1; openGoalY = moveOpenTo.Item2; for (int x = 0; x < MaxX(); x++) { for (int y = 0; y < MaxY(); y++) { StorageNode checkNode = FindNode(x, y); StorageNode sourceNode = FindNode(openGoalX, openGoalY); if (checkNode.SpaceAvailable(startState.State) > sourceNode.SpaceUsed(startState.State) && sourceNode.SpaceAvailable(startState.State) > checkNode.SpaceUsed(startState.State)) { int candidateOpenDistance = Math.Abs(openGoalX - x) + (Math.Abs(openGoalY - y)); if (!isGoalNode) { candidateOpenDistance += (50 * depth); } if (candidateOpenDistance < openDistance) { openDistance = candidateOpenDistance; if (openDistance == (50 * depth)) { startState.HitWayPoint(); } } } } } // number of spots that the desired state is away from the root note int distanceFromRoot = startState.DesiredDataOnX + startState.DesiredDataOnY; _lastDistanceFromOpen = openDistance; _lastDistanceFromRoot = distanceFromRoot; // score increases exponentialy with distance return((openDistance * openDistance * 10) + (distanceFromRoot * distanceFromRoot)); }
private void PrintState(StorageState tempCurrent) { for (int y = 0; y <= MaxY(); y++) { StringBuilder line = new StringBuilder(); for (int x = 0; x <= MaxX(); x++) { StorageNode node = FindNode(x, y); string display = ""; display = node.SpaceUsed(tempCurrent.State).ToString() + "/" + node.SpaceAvailable(tempCurrent.State).ToString(); if (tempCurrent.DesiredDataOnX == x && tempCurrent.DesiredDataOnY == y) { display += "(G)"; } line.Append(display.PadRight(6)); } Console.WriteLine(line.ToString()); } }