Check in algorithm topic: Ques20201024

Question 1

description

Your goal is to get the robot out of the maze. The robot faces north and the starting position is in the middle of the maze. You can turn the robot to face east, south, west, and north. You can make the robot walk a certain distance forward, and it will stop before hitting the wall.

a. Formalize the question, how big is the state space?

The formalization is as follows:

State: The state is determined by the position and orientation of the robot. When the next position has only 4 orientations to choose from, the number of possible states is 4. Next time there are also 4 orientations to choose from. The actions of choosing the orientation and moving forward are continuous.

First acquaintance status: The robot faces north.

Transfer model: You can walk forward facing any direction, unless you are about to hit a wall.

Target test: Test whether the current position is out of the maze.

Path consumption: The total dissipation value is the distance the robot moves.

Since there are no restrictions on the position and orientation of the robot during the movement, and the movement of the robot is not discrete, the state may increase exponentially, and the state space is infinite.

b. Walk in the maze, turn at the intersection of two or more roads, re-formalize this question, how big is the state space now?

State: The state is determined by the position of the robot. When the position is an intersection, it can turn to the next state. The state needs to record the position and number of the intersection.

First acquaintance state: The robot faces north in the middle of the maze.

Transfer model: Go to the next intersection unless you are about to hit a wall.

Target test: Test whether the current position is out of the maze.

Path consumption: The total dissipation value is the distance traveled.

When the robot is moving, the state space is affected by the number of intersections (it does not change its orientation at other times). When the intersection is 1, the possible state is 4, when it is 2, the possible state is 8, and when the intersection is n, the state is The space is 4n.

c. Starting from any point in the maze, we can move in any of the four directions until we can turn, and we only need to do this to re-formalize the problem. Do we only need to record the direction of the robot? ?

State: The state is determined by the position and orientation of the robot. When the position is an intersection, there are 4 directions for the next position to choose from, and it can also turn to the next state. The state needs to record the position of the intersection And the number.

First acquaintance state: The robot faces north in the middle of the maze.

Transfer model: You can walk forward facing any direction, unless you are about to hit a wall.

Target test: Test whether the current position is out of the maze.

Path consumption: The total dissipation value is the distance traveled.

There is no need to record the direction information, only the location of the intersection, because there will be a new state at the intersection, regardless of the direction information.

d. In our initial description of the problem, we have abstracted the real world, restricted the robot's actions and removed details, and listed three simplifications we made.

Simplification 1: The moving speed of the robot

Simplification 2: In what way does the robot act as a driving force

Simplification 3: Robot sensor type, camera or infrared ranging

Question 2

The issue of missionaries and savages. Three missionaries and three savages are on one bank of the river. There is a boat that can carry one or two people. Please try to make everyone cross to the other bank of the river, and require that there be no more wild people than missionaries anywhere. This question is famous in AI because it is the first thesis topic to discuss the formalization of the question from an analytical point of view.

a. Please formalize the problem in detail, describing only the characteristics necessary to ensure that the problem is solved. Draw a complete state space diagram.

Problem solving reference: https://www.jianshu.com/p/0af3a6bb1e43

Some premises: There is a round-trip from the left bank to the right bank, where both missionaries and wild men can sail, and the state cannot be cycled during the transformation process, that is, discarding it will restore the state of the previous step. The question is formalized as follows:

Status: Use a triple (m, c, b) to represent the status on the river bank, where m and c represent the number of priests and savages on a certain bank, and b is used to indicate whether the ship is from the left bank to the other bank or returning ( b=1 means the ship is on this shore, b=0 means the ship is not). The constraints are: on board both sides of the strait

≤2, which is the number of missionaries and wild men on board.

Initial state of acquaintance: The initial state of the triad is (3,3,1), that is, both the left bank savages and missionaries are 3.

Transfer model: the boat drives from the left bank to the right bank, b from 0-->1, and then from 1-->0. As long as the restriction conditions are not met, it enters the next state.

Target test: whether to reach the final state (0,0,0).

Path consumption: the length of the path taken.

State space diagram:

b. Apply a suitable search algorithm to find the optimal solution to the problem. Is it a good idea to check the duplicate status for this problem?

 Use the depth-first search algorithm to solve the problem, the code is as follows:

#include<iostream>
#include<cstdio>
#include<map>
#include<string>
#include<set>
#include<vector>
using namespace std;
using namespace std;
int N;
set<string>ans;
void print(vector<int>way) {
    int i = 0;
    string h;
    for (i = 1; i < way.size(); i++) {
       
        if (way[i] < 100) {
            h += "0";
        }
        h += to_string(way[i]);
        h += " ";
    }
    ans.insert(h);
}
void dfs(int pre, int ni, int nj, int b, set<int>s, vector<int>way) {

    int now = ni * 100 + nj * 10 + b;// 
    s.insert(pre);
    if (s.count(now) || (N - ni) < 0 || (N - nj) < 0 || ni < 0 || nj < 0 || (ni < nj && ni != 0) || ((N - ni) < (N - nj) && (N - ni) != 0)) {
        // 限制条件,若传教士和野人数都小于0,或者剩下的传教士比野人少都直接return
        return;
    }
    if (pre == 0) {
        // 如果上一轮就截止了,pre == 0 代表已到终点状态
        print(way);
        return;
    }
    way.push_back(pre);
    if (b == 1) { // 需前往对岸
      
        // 传教士+野人=1
        dfs(now, ni - 1, nj, 0, s, way);
        dfs(now, ni, nj - 1, 0, s, way);
        // 传教士+野人=2
        dfs(now, ni - 2, nj, 0, s, way);
        dfs(now, ni, nj - 2, 0, s, way);
        dfs(now, ni - 1, nj - 1, 0, s, way);
    }
    else {
     
         // 传教士+野人=1
        dfs(now, ni + 1, nj, 1, s, way);
        dfs(now, ni, nj + 1, 1, s, way);
        // 传教士+野人=2
        dfs(now, ni + 2, nj, 1, s, way);
        dfs(now, ni, nj + 2, 1, s, way);
        dfs(now, ni + 1, nj + 1, 1, s, way);
    }
}
//Ques20201024
int main() {
    int b = 1;
    // 初始化b的值
    int m, c;
    int pre = -1;
    set<int>s;
    chooseN = 3;
    N = c = m = 3;
    vector<int>way;
    dfs(pre, m, c, b, s, way);
    for (auto it = ans.begin(); it != ans.end(); it++) {
        cout << *it << endl;
    }
}

running result

There are a total of 4 optimal paths obtained by the solution, as shown in the diagram space above.

c. The state space of this problem is very simple. What do you think makes it difficult for people to solve it?

In the process of solving, two people may be transported, and then the same two people will be transported back (or use another form but it is still a cycle, that is, it does not work), so it is difficult to remove this duplication. Where to solve. The best way is to use a state with memory. If you have encountered this state before, just return directly and return to the previous state. 

Guess you like

Origin blog.csdn.net/Toky_min/article/details/109259297