对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,香蕉位置在B,箱子位置为C),如何行动可摘取到香蕉2.1猴子摘香蕉问题PEAS性能环境执行器感知器猴子站在箱香蕉MoveSite子上摘到香箱子ClimbHold蕉房间(a,b,c)PushOnGraspHangJump2.2定义谓词Site(x,w):物体x的位置是wHold(z):z手中拿着香蕉On(z):z

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

  • 背景简介/问题描述

一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,香蕉位置在B,箱子位置为C),如何行动可摘取到香蕉

  • 算法介绍

2.1猴子摘香蕉问题PEAS

性能

环境

执行器

感知器

猴子站在箱

香蕉

Move

Site

子上摘到香

箱子

Climb

Hold

蕉

房间(a,b,c)

Push

On

Grasp

Hang

Jump

2.2定义谓词

Site(x,w):物体x的位置是w

Hold(z):z手中拿着香蕉

On(z):z站在了箱子上

Hang(y,w):y悬挂在位置w

2.3定义变元定义域:

x∈{猴子,箱子}

y∈{香蕉}

z∈{猴子}

w∈{a,b,c}

2.4定义操作:

Move(u,v):猴子从u走到v

Climb():猴子爬上了箱子

Push(u,v):猴子推着箱子从u走到v

Grasp():猴子拿到了香蕉

Jump():猴子从箱子上跳下来

2.5定义初始状态:

设置综合数据库(五元组):{Monkey,Banana,Box,On,Hang}

其中Monkey表示猴子现在位置(a,b,c)

Banana表示香蕉现在位置(a,b,c)

Box表示箱子现在位置(a,b,c)

On表示猴子是否站在箱子上(0:未站在1:已站在)

Hang表示香蕉是否悬挂(0:未悬挂1:悬挂)

则初始状态表示为{m,b,box,0/1,1}(若On=1,则m==box)

接受状态表示为{b,b,b,1,0}

2.6定义规则集(根据操作集):

R1:Move(x,w):{x,y,z,0,1}->{w,y,z,0,1}

R2:Climb():{x,y,x,0,1}->{x,y,x,1,1}

R3:Push(x,w):{x,y,x,0,1}->{w,y,w,0,1}

R4:Grasp():{x,x,x,1,1}->{x,x,x,1,0}

R5:Jump():{x,y,x,1,1}->{x,y,x,0,1}

2.7执行步骤:

根据初始状态和接受状态,有如下操作判断

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

  • 算法实现

使用java,设计box类、monkey类、banana类、position类

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

其中Position代码如下

package monkey;

import java.util.Objects;

public class Position {

    private int x;
    private int y;

    public int getX() {

        return x;
    }

    public int getY() {

        return y;
    }
    public void setPosition(int x,int y){

        this.x=x;
        this.y=y;
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Position position = (Position) o;
        return x == position.x && y == position.y;
    }

    @Override
    public int hashCode() {

        return Objects.hash(x, y);
    }
}

其中box类代码如下

package monkey;

public class Box {

    private final Position box_position=new Position();
    private boolean OnBox=false;
    public void setBox_position(int x,int y){

        box_position.setPosition(x,y);
    }
    public void Push(Monkey monkey,int x,int y){

        monkey.Move(x,y);
        System.out.println(“the box is being pushed from(“+box_position.getX()+‘,’
                +box_position.getY()+“)to(“+x+‘,’+y+“)”);
        box_position.setPosition(x,y);
    }
    public void Jump(){

        if(OnBox){

            OnBox=false;
            System.out.println(“the monkey has jumped off the box”);
        }
        else{

            System.out.println(“the monkey is standing under the box”);
        }
    }
    public void Climb(){

        if(!OnBox){

            OnBox=true;
            System.out.println(“the monkey has climbed on the box”);
        }
        else{

            System.out.println(“the monkey is standing on the box”);
        }
    }
    public Position getBox_position() {

        return box_position;
    }
    public boolean isOnBox() {

        return OnBox;
    }
}

其中banana代码如下

package monkey;

public class Banana {

    private final Position banana_position=new Position();
    private boolean Hang=true;

    public Position getBanana_position() {

        return banana_position;
    }
    public void setBanana_position(int x,int y){

        banana_position.setPosition(x,y);
    }

    public boolean isHang() {

        return Hang;
    }

    public void Grasp(){

        Hang=false;
    }
}

其中monkey代码如下

package monkey;

public class Monkey {

    private final Position monkey_position=new Position();

    public void setMonkey_position(int x,int y) {

        monkey_position.setPosition(x,y);
    }
    public void Move (int x,int y){

        System.out.println(“the monkey is going from(“+monkey_position.getX()+‘,’
                +monkey_position.getY()+“)to(“+x+‘,’+y+“)”);
        monkey_position.setPosition(x,y);
    }
    public Position getMonkey_position(){

        return monkey_position;
    }
}

其中main函数如下

package monkey;

import java.util.Scanner;

import static java.lang.System.exit;

public class main {

    public static void main(String[] args) {

        int x,y;
        Monkey monkey = new Monkey();
        Banana banana = new Banana();
        Box box = new Box();
        Scanner sc =new Scanner(System.in);
        System.out.println(“Please Enter the position of the banana(x,y):”);
        x=sc.nextInt();
        y=sc.nextInt();
        banana.setBanana_position(x,y);
        System.out.println(“the banana is hanging at (“+banana.getBanana_position().getX()+‘,’
                +banana.getBanana_position().getY()+‘)’);
        System.out.println(“Please Enter the position of the box(x,y):”);
        x=sc.nextInt();
        y=sc.nextInt();
        box.setBox_position(x,y);
        System.out.println(“the box is at (“+box.getBox_position().getX()+‘,’
                +box.getBox_position().getY()+‘)’);
        System.out.println(“Please Enter the position of the monkey(x,y):”);
        x=sc.nextInt();
        y=sc.nextInt();
        monkey.setMonkey_position(x,y);
        System.out.println(“the monkey is at (“+monkey.getMonkey_position().getX()+‘,’
                +monkey.getMonkey_position().getY()+‘)’);
        if(!monkey.getMonkey_position().equals(box.getBox_position())){

            monkey.Move(box.getBox_position().getX(),box.getBox_position().getY());
        }
        if(!monkey.getMonkey_position().equals(banana.getBanana_position())){

            box.Push(monkey,banana.getBanana_position().getX(),banana.getBanana_position().getY());
        }
        box.Climb();
        banana.Grasp();
        if(!banana.isHang())
            System.out.println(“the monkey has grasped the banana.”);
        exit(0);
    }
}

结果如下图(例一)

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

(例二)

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

(例三)

对猴子摘香蕉问题给出产生式系统描述_猴子接香蕉的编程

  • 讨论及结论

环境可理解为java的对象类,动作器可理解为方法,感受器可理解为参数,我们要尽可能防止表示泄露,因此设置参数用private、setter、getter的组合来传递以及更改参数,而且要时刻记得checkRep,此外猴子摘香蕉问题,猴子会主动搬箱子,抓取香蕉,以及爬上或跳下箱子。那我们在构造方法时应该在哪一个类中构建呢?我们主观印象会觉得猴子是主语,他做出了这些动作,应该将方法都添加到猴子类中,但事实上,我们会发现将方法添加到宾语,也就是箱子、香蕉这些类会更加方便,这是因为虽然猴子爬上或搬箱子,别的动物甚至某种类也可以完成这些动作,可以被搬,可以被攀爬或跳下是箱子的属性,因此我们在构造方法时要先判断好对谁创建一个方法,入参、返回值,规约,检查入口。这些对编写java程序至关重要。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189376.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle中更改表名语句,转:取Oracle 表名 字段名 注释等实用语句

    oracle中更改表名语句,转:取Oracle 表名 字段名 注释等实用语句1、查找表的所有索引(包括索引名,类型,构成列):selectt.*,i.index_typefromuser_ind_columnst,user_indexesiwheret.index_name=i.index_nameandt.table_name=i.table_nameandt.table_name=要查询的表2、查找表的主键(包括名称,构成列):select…

    2022年5月17日
  • PathFileExists用法「建议收藏」

    PathFileExists用法「建议收藏」BOOLPathFileExists(LPCTSTRpszPath);Determinesifafileexists.—经检测,该函数可以检测文件或目录是否存在!RemarksThi

    2022年6月30日
  • MQTT服务器搭建 windows[通俗易懂]

    MQTT服务器搭建 windows[通俗易懂]typora-copy-images-to:upload软件简介MQ遥测传输(MQTT)是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放、简单、轻量、易于实现。这些特点使它适用于受限环境。例如,但不仅限于此:网络代价昂贵,带宽低、不可靠。在嵌入设备中运行,处理器和内存资源有限。该协议的特点有:使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合。对负载内容屏蔽的消息传输。使用TCP/IP提供网络连接。有三种消息发布服务质量:“至多一次”,消息发布.

    2022年4月29日
  • strcasecmp 的使用

    strcasecmp 的使用

    2022年2月20日
  • java认证考试试卷_java认证考试试题及答案

    java认证考试试卷_java认证考试试题及答案java认证考试试题及答案故答案为C。12.Whatistheresultafterthefollowingcodeexecutes?1shorts=0x00FD;2byteb=(byte)s;3System.out.println(b);Select1correctanswer:A.Compiletimeerrorinline1B.Comp…

    2022年7月7日
  • jvav是什么梗?jvav是什么?jvav史上最牛语言[通俗易懂]

    本文纯属娱乐 本文纯属娱乐 本文纯属娱乐 本文纯属娱乐 本文纯属娱乐 本文纯属娱乐jvav目录前言Jvav之父:jvav的诞生什么是Jvav?怎么下载Jvav?怎么加入jvav?jvav相关的书籍![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050212315631.png?x-oss-process=image/watermark,type_ZmFu…

    2022年4月5日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号