コード例 #1
0
        private static void SokobanIDAStarTest(string levelPath, int maxCost, RewardType rewardType, int maxTableSize, bool useNormalizedPosition, bool useTunnelMacro, bool useGoalMacro, bool useGoalCut)
        {
            string[]       levels       = ReadSokobanLevels(levelPath);
            IPuzzleState[] states       = new IPuzzleState[levels.Length];
            int            solvedLevels = 0;
            Stopwatch      stopwatch    = new Stopwatch();
            long           stateInitTime;
            long           solvingTime;

            //GoalMacroWrapper.BuildMacroTree(null);
            for (int i = 0; i < states.Length; i++)
            {
                stopwatch.Restart();
                states[i] = new AbstractSokobanState(levels[i], rewardType, useNormalizedPosition, useGoalMacro, useTunnelMacro, useGoalCut, null);
                stopwatch.Stop();
                stateInitTime = stopwatch.ElapsedMilliseconds;
                IDAStarSearch idaStar = new IDAStarSearch();
                //Log("Level" + (i + 1) + ":\n" + states[i].PrettyPrint());
                stopwatch.Restart();
                List <IPuzzleMove> solution = idaStar.Solve(states[i], maxCost, maxTableSize, 700);
                stopwatch.Stop();
                solvingTime = stopwatch.ElapsedMilliseconds;
                string moves     = "";
                int    pushCount = 0;

                if (solution != null)
                {
                    foreach (IPuzzleMove m in solution)
                    {
                        //Debug.WriteLine(states[i]);
                        //Debug.WriteLine(m);
                        SokobanPushMove push = (SokobanPushMove)m;
                        foreach (IPuzzleMove basicMove in push.MoveList)
                        {
                            moves += basicMove;
                            if (basicMove.move > 3)//the move is a push move
                            {
                                pushCount++;
                            }
                        }

                        states[i].DoMove(m);
                    }
                    if (states[i].EndState())
                    {
                        solvedLevels++;
                    }
                    Log("Level " + (i + 1) + " solved: " + (states[i].EndState()) + " with " + idaStar.NodeCount + " nodes; solution length:" + moves.Count() + "/" + pushCount + " - Init Time: " + TimeFormat(stateInitTime) + " - Solving Time: " + TimeFormat(solvingTime));
                    Log("Moves: " + moves);
                    Log("Solved " + solvedLevels + "/" + (i + 1));
                    Console.Write("\rSolved " + solvedLevels + "/" + (i + 1));
                }
            }
        }
コード例 #2
0
        static SokobanPushMove SolveMacro(AbstractSokobanState s)
        {
            s.UseGoalCut     = false;
            s.UseGoalMacro   = false;
            s.UseTunnelMacro = false;
            List <IPuzzleMove> solution = idaStar.Solve(s, 10000, 10000, 700);

            if (solution.Count == 0)
            {
                return(null);
            }
            List <SokobanGameMove> moveList = new List <SokobanGameMove>();

            foreach (SokobanPushMove push in solution)
            {
                moveList.AddRange(push.MoveList);
            }
            return(new SokobanPushMove(moveList, new Position(s.State.PlayerX, s.State.PlayerY), moveList[solution.Count - 1].BoxIndex));
        }
コード例 #3
0
 public IPuzzleMove selectMove(IPuzzleState gameState)
 {
     //if (moveList != null)
     //{
     //    if (moveList.Count > 0)
     //    {
     //        IPuzzleMove move = moveList[0];
     //        moveList.RemoveAt(0);
     //        return move;
     //    }
     //    else
     //    {
     //        moveList = null;
     //        return (IPuzzleMove)(-1);
     //    }
     //}
     //else
     //{
     //    moveList = idaStar.Solve(gameState, maxNodes, tableSize, maxDepth);
     //    if (moveList.Count == 0)
     //    {
     //        return (IPuzzleMove)(-1);
     //    }
     //    IPuzzleMove move = moveList[0];
     //    moveList.RemoveAt(0);
     //    return move;
     //}
     if (RNG.NextDouble() < epsilon)
     {
         return(gameState.GetMoves()[RNG.Next(gameState.GetMoves().Count)]);
     }
     moveList = idaStar.Solve(gameState, maxNodes, tableSize, maxDepth);
     if (moveList.Count > 0)
     {
         return(moveList[0]);
     }
     else
     {
         return((IPuzzleMove)(-1));
     }
 }
コード例 #4
0
        private static void SamegameIDAStarTest(string levelPath, int maxCost, int tableSize)//TODO Need a good heuristic for samegame
        {
            string[]       levels       = ReadSamegameLevels(levelPath);
            IPuzzleState[] states       = new IPuzzleState[levels.Length];
            int            solvedLevels = 0;
            double         totalScore   = 0;

            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new SamegameGameState(levels[i], null, null);
                IDAStarSearch idaStar = new IDAStarSearch();
                Log("Level" + (i + 1) + ":\n" + states[i].PrettyPrint());
                List <IPuzzleMove> solution = null;
                string             moves    = "";
                while (!states[i].isTerminal())
                {
                    solution = idaStar.Solve(states[i], maxCost, tableSize, 100);
                    if (solution.Count > 0)
                    {
                        moves += solution[0];
                        states[i].DoMove(solution[0]);
                    }
                    else
                    {
                        break;
                    }
                }
                if (states[i].EndState())
                {
                    solvedLevels++;
                }
                totalScore += states[i].GetResult();
                Log("Level " + (i + 1) + " solved: " + (states[i].EndState()) + " solution length:" + moves.Count() + " Score: " + states[i].GetResult());
                Log("Moves: " + moves);
                Log("Solved " + solvedLevels + "/" + (i + 1));
                Console.Write("\rSolved " + solvedLevels + "/" + (i + 1));
            }
            Log("Total score: " + totalScore);
        }