【C++】人工智能实验一猴子摘香蕉传教士与野人(含完整代码与状态迁移...

【C++】⼈⼯智能实验⼀猴⼦摘⾹蕉传教⼠与野⼈(含完整代码与状态迁移图)⽂章⽬录
⼀、猴⼦摘⾹蕉问题
1、问题描述
利⽤⼀阶谓词逻辑求解猴⼦摘⾹蕉问题:房内有⼀个猴⼦,⼀个箱⼦,天花板上挂了⼀串⾹蕉,其位置如图1所⽰,猴⼦为了拿到⾹蕉,它必须把箱⼦搬到⾹蕉下⾯,然后再爬到箱⼦上。请定义必要的谓词,列出问题的初始化状态(即下图所⽰状态),⽬标状态(猴⼦拿到了⾹蕉,站在箱⼦上,箱⼦位于位置b)。
(附加:从初始状态到⽬标状态的谓词演算过程。)
2、解题思路
卧式炭化炉猴⼦按照先到箱⼦所在位置/从箱⼦上爬下来→把箱⼦搬到⾹蕉下⾯→爬上箱⼦摘⾹蕉的逻辑进⾏着。
所以需要编写四个⾏动逻辑——⾛到箱⼦所在位置、从箱⼦上爬下来、把箱⼦搬到⾹蕉上⾯、爬上箱⼦摘⾹蕉。
使⽤⼀个结构定义猴⼦、箱⼦、⾹蕉、相对箱⼦的位置状态——
猴子的B和人的B一样吗猴⼦在A点则标-1,猴⼦在B点则标0,猴⼦在C点则标1
箱⼦在A点则标-1,箱⼦在B点则标0,箱⼦在C点则标1
⾹蕉在A点则标-1,⾹蕉在B点则标0,⾹蕉在C点则标1
猴⼦爬上箱⼦则标1,没爬上则标-1
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];
输⼊⼀个初始状态(a, b, c, d)
根据问题,确定终⽌状态是猴⼦摘到⾹蕉{(x,x,x,0)}(x 属于 {0,-1, 1})。
使⽤递归调⽤的⽅式搜索路径,每次递归前先判断当前状态是否与之前的状态重复,若重复则认为形成⼀个环路,回到上⼀步寻其他⽅式通往新的状态。
3、实验结果及分析
实验结果⼀
分析:
初始时,猴⼦站在A位置,箱⼦在C位置,⾹蕉在B位置,猴⼦没有站在箱⼦上。
猴⼦摘⾹蕉的步骤如下:
猴⼦⾛去C位置→猴⼦把箱⼦从C位置搬到B位置→猴⼦爬上箱⼦→猴⼦摘到⾹蕉
实验结果⼆
单元测试流程分析:
初始时,猴⼦站在A位置,箱⼦在B位置,⾹蕉在B位置,猴⼦没有站在箱⼦上。
猴⼦摘⾹蕉的步骤如下:
猴⼦⾛去B位置→猴⼦爬上箱⼦→猴⼦摘到⾹蕉
实验结果三
分析:
初始时,猴⼦站在A位置,箱⼦在A位置,⾹蕉在B位置,猴⼦站在箱⼦上。
猴⼦摘⾹蕉的步骤如下:
猴⼦从箱⼦上爬下来→猴⼦把箱⼦从A位置搬到B位置→猴⼦爬上箱⼦→猴⼦摘到⾹蕉4、实验结果
当传教⼠与野⼈为五⼈,船最多允许三⼈过河时,程序运⾏结果如下
解的状态迁移图
1、550->441->440->331->330->221->220->111->110->001
2、550->441->440->331->330->221->220->011->110->001
3、550->441->540->331->330->221->220->111->110->001
4、550->441->540->331->330->221->220->011->110->001
5、实验代码
#include"stdafx.h"
#include<string.h>
#include<iostream>
#include<stdio.h>
using namespace std;
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];
char* routesave[150];
/
*function monkeygoto,it makes the monkey goto the other place*/
void monkeygoto(int b,int i)
{
int a;
a = b;
if(a ==-1)
{
routesave[i]="Monkey go to A";
States[i +1]= States[i];
States[i +1].monkey =-1;
}
else if(a ==0)
{
routesave[i]="Monkey go to B";
States[i +1]= States[i];
States[i +1].monkey =0;
}
else if(a ==1)
{
routesave[i]="Monkey go to C";
States[i +1]= States[i];
States[i +1].monkey =1;
}
else
{
printf("parameter is wrong");
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;
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;
}
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;
}
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");
}

本文发布于:2024-09-21 15:19:53,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/341261.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:状态   位置   实验
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议