Beispiel #1
0
        public string HashState()
        {
            if (_hashCalced != null)
            {
                return(_hashCalced);
            }

            StringBuilder state = new StringBuilder();

            state.Append(DesiredDataOnX);
            state.Append("-");
            state.Append(DesiredDataOnY);
            List <string> keys = new List <String>(_storageState.Keys);

            keys.Sort();
            foreach (string key in keys)
            {
                StorageNode hashNode           = _nodes.First(n => n.NodeName == key);
                int         nodeRepresentation = hashNode.X * 100 + hashNode.Y;
                string      hash = nodeRepresentation.ToString() + "-" + _storageState[key];
                state.Append(hash);
            }
            _hashCalced = state.ToString().GetHashCode().ToString();
            return(_hashCalced);
        }
Beispiel #2
0
 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);
         }
     }
 }
Beispiel #3
0
        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));
        }
Beispiel #4
0
 private void CalcAllPossibleValidMoves(StorageState current, ConcurrentQueue <StorageState> neighbours)
 {
     for (int x = 0; x <= MaxX(); x++)
     {
         for (int y = 0; y <= MaxY(); y++)
         {
             StorageNode node = FindNode(x, y);
             // Can we move the node's data left?
             CheckNodeMove(current, node, node.Left, neighbours);
             // Can we move the node's data right?
             CheckNodeMove(current, node, node.Right, neighbours);
             // Can we move the node's data up?
             CheckNodeMove(current, node, node.Top, neighbours);
             // Can we move the node's data down?
             CheckNodeMove(current, node, node.Bottom, neighbours);
         }
     }
 }
Beispiel #5
0
        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());
            }
        }