Solving a puzzle game using AI

后端 未结 1 1763
心在旅途
心在旅途 2021-02-04 21:51

I have made a puzzle where the player slides blocks around to goals - the rules are fairly simple:

  1. Only one slider block may move at a time
  2. The object is
1条回答
  •  礼貌的吻别
    2021-02-04 22:17

    Your problem is a classical instance of State-Space search problem. There are different algorithms that can be used based on the characteristics of your particular instance.

    Regardless of your particular instance you need to have defined four components:

    1. An initial state, in your problem the initial configuration
    2. A function that returns all the possible states reachable from any state of the space, in your problem the reachable states are all the possible configurations of the puzzle that can be obtained moving a slider. A state is said expanded when its reachable states are visited.
    3. A goal test, a function that determines whether the given state is a goal state, in your problem you would check if all goal blocks are filled,
    4. A cost function, that gives the cost of passing from a state to another, allowing your algorithm to choose the best action to perform. In your case, I think you can use a constant value of 1 for each action, and search for the minimum number of actions to be performed to reach one of the goal states.

    Since your problem can be defined in terms of these four components your problem can be solved with one of the following algorithms:

    1. Breadth-first search (BFS): We start by testing whether the initial state is a goal state (obviously not for non-trivial problems), then we expand the initial state, test each of its reachable states, if not, expand each these states, and so on going for levels... It can be implemented using a queue.
    2. Depth-fisrt search (DFS): Starting with the initial state, test for goal, then expand a neighbour state, test for goal, the expand that state, and so on going down to the deepest levels of the state space. This can be implemented with a stack.

    To evaluate which algorithm is the most appropriate we can consider these factors:

    1. Completeness: is it guaranteed that the algorithm will find a solution when it exists?
    2. Optimality: can the algorithm find an optimal solution?
    3. Time complexity: how long it will take?
    4. Space complexity: how much memory does it need?

    If we consider the BFS strategy we can see that it is complete since it systematically explores the state space by levels, it is optimal only if the cost function does not decrease when the depth of a state increases (this is our case, since all the actions have a constant cost). Now it comes the bad news: assume that the expansion of each node can give at most http://latex.codecogs.com/png.download?b states, and the first solution is at depth , then you will need to store and expand at most states.

    In the case of DFS we must consider that it can be stuck searching in a path for a lot of time (potentially infinite) when a different choice could lead to a solution somewhere near. So when the state space is infinite this algorithm is neither complete nor optimal. If we consider as the maximum depth of the state space, we will get a space complexity of at most , whereas the time complexity remains exponential: .

    So, what can we do? We can mix these two strategies and use the Iterative Deepening depth first search. In this search we will run iteratively a DFS limiting the maximum depth starting from 0 to a maximum depth level. This method has the advantages of both search strategies: space complexity, where is the depth of the first optimal solution, time (we can't do better), it is complete (it will find a solution because it iteratively explores all states by levels) and it is optimal (assuming the cost function is nondecreasing with path length) for the same reasons BFS is optimal.

    Reference: Artificial intelligence: a modern approach

    Note: obviously there exist other uninformed strategies, but as stated on the book IDDFS is a good choice for uninformed search problems where you don't have additional information on the search space. Refer to the book for other types of search strategies, for example informed search, were have an idea of how far is a goal state from the current state.

    0 讨论(0)
提交回复
热议问题