用软件构造的思维理解猴子摘香蕉问题

  • 背景简介/问题描述

一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为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程序至关重要。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值