Artificial Intelligence Theory Experiment 1

Experiment 1: Use predicates to express problems-knowledge representation and reasoning experiments

(1) Monkey picking bananas

Experiment content:
Use first-order predicate logic to solve the monkey picking banana problem: There is a monkey in the room, a box, and a bunch of bananas hanging on the ceiling. The location is shown in Figure 1. In order to get the banana, the monkey must move the box. Go under the banana and then climb onto the box. Please define the necessary predicates and list the initial state of the problem (the state shown in the figure below), and the target state (the monkey got the banana and stood on the box, the box is at position b). (Additional: the predicate calculation process from the initial state to the target state.)
Insert picture description here
1. Define the predicate that describes the state of the environment.
AT(x,w): x is at w, individual domain: x?{monkey}, w?{a,b,c,box};
HOLD(x,t): x holds t in the hand, individual domain: t ?{box,banana};
EMPTY(x): x is empty in the hand;
ON(t,y): t is at y, individual domain: y?{b,c,ceiling};
CLEAR(y): on y Is empty;
BOX(u): u is a box, individual domain: u?{box};
BANANA(v): v is banana, individual domain: v?{banana};
2. Use predicates, connectives, and quantifiers to Indicates the state of the environment.
The initial state of the problem can be expressed as: So: AT(monkey,a)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)?
The goal state to be achieved by BANANA(banana) is: Sg:
AT(monkey,box)?HOLD(monkey,banana)?ON(box,b)?CLEAR(ceiling)?CLEAR©?BOX(box)?BANANA(banana) )
3. To transform from the initial state to the target state, the monkey needs to complete a series of operations, and define operation predicates to represent its actions.
WALK(m,n): the monkey walks from m to n, the individual domain: m,n?{a,b,c};
CARRY(s,r): the monkey gets s at r, the individual domain: r? {c,ceiling},s?{box,banana};
CLIMB(u,b): Monkey climbs up u at b;
these 3 operations can also be represented by conditions and actions respectively. The condition is directly expressed by a predicate formula, which is a condition that must be met in order to complete the corresponding operation; when the facts in the condition make it all true, the operation rule can be activated, and then the action part of the rule can be executed. The action is expressed by the change of the state before and after the action, that is, the state after the action is described by deleting or adding a predicate formula from before the action.
WALK(m,n): Monkey walks from m to n.
Condition: AT(monkey,m)
Action: (1) Delete: AT(monkey,m); (2) Increase: AT(monkey,n)
CARRY(s ,r): The monkey gets s at r.
Conditions: AT(monkey,r)?EMPTY(monkey)?ON(s,r)?BOX(box)?BANANA(banana)
Action: (1) Delete: EMPTY( monkey) ^ ON(s,r); (2)Increase: HOLD(monkey,s) ^ CLEAR®
CLIMB(u,b): Monkey climbs up at b place u
Condition: AT(monkey,b)?HOLD(monkey ,u)?CLEAR(b)?BOX(box)?BANANA(banana)
Action: (1) Delete: AT(monkey,b) ^ HOLD(monkey,u) ^ CLEAR©; (2) Add: AT(monkey ,u)EMPTY(monkey) ^ ON(u,c)
4. According to the action plan, replace the state step by step until the target state
AT(monkey,a)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)?BANANA(banana)
AT(monkey,c)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)?BANANA(banana)
AT(monkey,c)?HOLD(monkey,box)?ON(banana,ceiling)?CLEAR(b)?CLEAR©?BOX(box)?BANANA(banana)
AT(monkey,b)?HOLD(monkey,box)?ON(banana,ceiling)?CLEAR(b)?CLEAR©?BOX(box)?BANANA(banana)
AT(monkey,box)?EMPTY(monkey)?ON(box,b)?ON(banana,ceiling)?CLEAR©?BOX(box)?BANANA(banana)
AT(monkey,box)?HOLD(monkey,banana)?ON(box,b)?CLEAR(ceiling)?CLEAR©?BOX(box)?BANANA(banana)(目标得解)
猴子行动的规则序列是:WALK(a,c)→CARRY(c,box)→WALK(c,b)→CLIMB(box,b)→
CARRY(banana,ceiling)
5.参考代码

#include <stdio.h>
struct State
{
    
    
	int monkey; /*-1:Monkey at A;0: Monkey at B;1:Monkey at C;*/
	int box; /*-1:box at A;0:box at B;1:box at C;*/
	int banana; /*Banana at B,Banana=0*/
	int monbox; /*-1: monkey on the box;1: monkey  the box;*/
};
struct State States[150];
const char* routesave[150];
/*function monkeygoto,it makes the monkey goto the other place*/
void monkeygoto(int b, int i)
{
    
    
	int a;
	a = b;
	// 判断箱子是否在A位置
	if (a == -1)
	{
    
    
		routesave[i] = "Monkey go to A";
		States[i + 1] = States[i];
		States[i + 1].monkey = -1;
	}
	// 判断箱子是否在B位置
	else if (a == 0)
	{
    
    
		routesave[i] = "Monkey go to B";
		States[i + 1] = States[i];
		States[i + 1].monkey = 0;
	}
	// 判断箱子是否在C位置
	else if (a == 1)
	{
    
    
		// 猴子跑到C处
		routesave[i] = "Monkey go to C";
		// 将上一个状态传给下一个状态
		States[i + 1] = States[i];
		// 更新猴子的位置
		States[i + 1].monkey = 1;
	}
	// 否则出错
	else
	{
    
    
		printf("parameter is wrong");
	}
}
/*end function monkeyygoto*/
/*function movebox,the monkey move the box to the other place*/
void movebox(int a, int i)
{
    
    
	int B;
	B = a;
	// 要把箱子移动到A
	if (B == -1)
	{
    
    
		routesave[i] = "monkey move box to A";
		States[i + 1] = States[i];
		States[i + 1].monkey = -1;
		States[i + 1].box = -1;
	}
	// 要把箱子移动到B
	else if (B == 0)
	{
    
    
		routesave[i] = "monkey move box to B";
		States[i + 1] = States[i];
		// 改变猴子和箱子的状态
		States[i + 1].monkey = 0;
		States[i + 1].box = 0;
	}
	// 要把箱子移动到C
	else if (B == 1)
	{
    
    
		routesave[i] = "monkey move box to C";
		States[i + 1] = States[i];
		States[i + 1].monkey = 1;
		States[i + 1].box = 1;
	}
	else
	{
    
    
		printf("parameter is wrong");
	}
}
/*end function movebox*/
/*function climbonto,the monkey climb onto the box*/
void climbonto(int i)
{
    
    
	routesave[i] = "Monkey climb onto the box";
	States[i + 1] = States[i];
	// 猴子爬上盒子
	States[i + 1].monbox = 1;
}
/*function climbdown,monkey climb down from the box*/
void climbdown(int i)
{
    
    
	routesave[i] = "Monkey climb down from the box";
	States[i + 1] = States[i];
	States[i + 1].monbox = -1;
}
/*function reach,if the monkey,box,and banana are at the same place,the monkey reach banana*/
void reach(int i)
{
    
    
	routesave[i] = "Monkey reach the banana";
}
/*output the solution to the problem*/
void showSolution(int i)
{
    
    
	int c;
	printf("%s \n", "Result to problem:");
	for (c = 0; c < i + 1; c++)
	{
    
    
		printf("Step %d : %s \n", c + 1, routesave[c]);
	}
	printf("\n");
}
/*perform next step*/
void nextStep(int i)
{
    
    
	int c;
	int j;
	// 如果超过150步
	if (i >= 150)
	{
    
    
		printf("%s  \n", "steplength reached 150,have problem ");
		return;
	}
	for (c = 0; c < i; c++) /*if the current state is same to previous,retrospect*/
	{
    
    
		if (States[c].monkey == States[i].monkey && States[c].box == States[i].box && States[c].banana == States[i].banana && States[c].monbox == States[i].monbox)
		{
    
    
			return;
		}
	}
	// 如果各个状态都满足条件,则成功,退出...
	if (States[i].monbox == 1 && States[i].monkey == 0 && States[i].banana == 0 && States[i].box == 0)
	{
    
    
		showSolution(i);
		printf("Press any key to continue \n");
		getchar();/*to save screen for user,press any key to continue*/
		return;
	}
	j = i + 1;
	// 判断第i步,猴子的是否在B位置
	if (States[i].monkey == 0)
	{
    
    
		// 判断第i步,箱子是否在B位置
		if (States[i].box == 0)
		{
    
    
			if (States[i].monbox == -1)
			{
    
    
				climbonto(i);
				reach(i + 1);
				nextStep(j);
				/*monkeygoto(-1,i);
				nextStep(j);
				monkeygoto(0,i);
				nextStep(j);
				movebox(-1,i);
				nextStep(j);
				movebox(0,i);
				nextStep(j);*/
			}
			else
			{
    
    
				reach(i + 1);
				nextStep(j);
				/*climbdown(i);
				nextStep(j);*/
			}
		}
		// 判断第i步,箱子是否在C位置
		else if (States[i].box == 1)
		{
    
    
			/*monkeygoto(-1,i);
			nextStep(j);*/
			monkeygoto(1, i);
			nextStep(j);
			movebox(0, i);
			nextStep(j);
			climbonto(i);
			reach(i + 1);
			nextStep(j);
		}
		// 判断第i步,箱子是否在A位置
		else /*box==-1*/
		{
    
    
			monkeygoto(-1, i);
			nextStep(j);
			movebox(0, i);
			nextStep(j);
			climbonto(i);
			reach(i + 1);
			nextStep(j);
		}
	}
	/*end if*/
	// 判断第i步,猴子的是否在A位置
	if (States[i].monkey == -1)
	{
    
    
		// 判断第i步,箱子是否在A位置
		if (States[i].box == -1)
		{
    
    
			if (States[i].monbox == -1)
			{
    
    
				movebox(0, i);
				nextStep(j);
				climbonto(i);
				reach(i + 1);
				nextStep(j);
			}
			else
			{
    
    
				climbdown(i);
				nextStep(j);
				movebox(0, i);
				nextStep(j);
				climbonto(i);
				reach(i + 1);
				nextStep(j);
			}
		}
		// 判断第i步,箱子是否在B位置
		else if (States[i].box == 0)
		{
    
    
			monkeygoto(0, i);
			nextStep(j);
			climbonto(i);
			reach(i + 1);
			nextStep(j);
		}
		// 判断第i步,箱子是否在C位置
		else
		{
    
    
			// 1:代表要移动到的位置
			monkeygoto(1, i);
			// 进行下一步
			nextStep(j);
			// 移动箱子,0:代表要把箱子移动到的位置
			movebox(0, i);
			// 继续执行下一步
			nextStep(j);
			// 猴子上箱子
			climbonto(i);
			// 获得香蕉
			reach(i + 1);
			// 进行下一步
			nextStep(j);
		}
	}
	/*end if*/
	// 判断第i步,猴子的是否在C位置
	if (States[i].monkey == 1)
	{
    
    
		if (States[i].box == 1)
		{
    
    
			if (States[i].monbox == -1)
			{
    
    
				movebox(0, i);
				nextStep(j);
				climbonto(i);
				reach(i + 1);
				nextStep(j);
			}
			else
			{
    
    
				climbdown(i);
				nextStep(j);
				movebox(0, i);
				nextStep(j);
				climbonto(i);
				reach(i + 1);
				nextStep(j);
			}
		}
		else if (States[i].box == -1)
		{
    
    
			monkeygoto(-1, i);
			nextStep(j);
			movebox(0, i);
			nextStep(j);
			movebox(0, i);
			nextStep(j);
			climbonto(i);
			reach(i + 1);
			nextStep(j);
		}
		else
		{
    
    
			monkeygoto(0, i);
			nextStep(j);
			movebox(0, i);
			nextStep(j);
			climbonto(i);
			reach(i + 1);
			nextStep(j);
		}
	}
	/*end if*/
}/*end nextStep*/
int main()
{
    
    
	// 定义初始状态
	States[0].monkey = -1;
	States[0].box = 1;
	States[0].banana = 0;
	States[0].monbox = -1;
	nextStep(0);
}

Insert picture description here

(2) The problem of missionaries (pastors) and wild men

Problem description:
There are n priests and n savages preparing to cross the river, but there is only one boat that can accommodate c people. In order to prevent the savages from invading the priest, it is required that the number of priests should not be less than the number of savages (unless the number of priests is 0), and assuming that both savages and priests can row a boat, try to design an algorithm to determine whether they can cross the river, and if so, give the best plan with the least number of boat trips.
Experimental steps:
Input: the number of priests (that is, the number of savages): n; the maximum number of people in the boat at a time: c.
Output: If there is no solution to the problem, it will display Failed, otherwise, ** will display Successed and output all feasible solutions, and mark which group is the best solution. **Use triples (X1, X2, X3) to represent the state of crossing the river. And use arrows to connect adjacent states to indicate the transition process: initial state -> intermediate state -> target state.
Example: When input n=2, c=2, output: 221->200->211->010->021->000;
where: X1 represents the number of priests on the initial shore; X2 represents the wild men on the initial shore Number of people; X3 represents the current position of the boat (1 represents the starting shore, 0 represents the destination shore).
Requirements: Write out the design ideas and source programs of the algorithm, and have a user interface to achieve human-computer interaction (console or window can be), input and output results, such as:
Please input n: 2 Please input c: 2
Optimal Procedure: 221->200->211->010->021->000
Successed or Failed?: Successed

Guess you like

Origin blog.csdn.net/wjl__ai__/article/details/108667075