Java: 'agent' 对象内部的 'world' 对象如何获取有关 'world' 的信息?

标签 java oop artificial-intelligence agent loose-coupling

很抱歉这个问题的措辞有点疯狂,但我对面向代理的思维非常陌生(这些是“模式”吗?),并且对 java 来说只是稍微不那么新鲜,而且我正在努力解决感觉像非常基本的问题。

我“凭直觉”(即盲目地)做了很多这样的事情,而不是试图理解别人的代码 - 部分原因是我很难理解“高于我水平”的代码,但也因为我希望这样做是“我的” way' 将帮助我稍后了解正确的方法。

基本上,我正在环境(房屋)中对代理(机器人真空吸尘器)进行建模。房屋包含房间的集合(HashMap)。 House 有一个方法 getRoom(int key),它返回与给定键匹配的 Room。 Agent 有一个 State,此时它会跟踪房间 ID(这也是 House 中的 key ),这是机器人为了导航世界而“所在”的房间;状态还描述了房间是否已知为脏的。当构建代理时,它会使用 ID 进行初始化(它必须在“房间中”创建),但不会给出房间的脏/干净状态。我希望代理检查污垢 - 这将涉及调用 House 中的 getRoom() 方法。然而,以我目前所学的Java,我不知道如何做到这一点。我知道我可以通过在 java 中创建 House 或将方法设置为静态来访问该方法,但这些是行不通的 - 代理需要了解内存中的特定房屋,即已使用 Rooms 初始化的房屋.

tl;dr:Agent 对象如何获取对存储在另一个环境对象内的 HashMap 中的对象的引用?

P.S 这是我想象的通过这种方法实现的“更高层次”视角的模型: 我直觉地希望代理对自己的规则和行为完全负责,这样高层的代码看起来更像是:

agent.percieve()                           //agent checks the room it thinks its in for dirt and exits
if (agent.gotDirt()) agent.clean()         //agent activates its cleaning device if it found dirt in this room
if (agent.gotDirt() == false) agent.move() //agent picks an exit and leaves the room

最佳答案

真空吸尘器(即您所说的“代理”,但为什么要这样命名它,因为它实际上是一个真空吸尘器?)只需要对其所属的 House 对象的引用:

// a single House is constructed
House house = new House(); 
// omitted: add rooms to the house...

// create a first vacuum cleaner for the house. A reference to the house is given to this cleaner
VacuumCleaner vacuumCleaner = new VacuumCleaner(house);
System.out(vacuumCleaner.isRoomClean(2)); // prints false, because room 2 is full of dirt
vacuumCleaner.cleanRoom(2);
System.out(vacuumCleaner.isRoomClean(2)); // prints true, because the vacuum cleaner removed the dirt from room 2

// now let's create a second vacuum cleaner for the same house
VacuumCleaner vacuumCleaner2 = new VacuumCleaner(house);
System.out(vacuumCleaner2.isRoomClean(2)); // prints true, because room 2 has no dirt: it has previously been removed from the room by the first vacuum cleaner.

编辑

VacuumCleaner 类如下所示:

public class VacuumCleaner
    /**
     * The house that this vacuum cleaner cleans 
     */
    private House house;

    public VacuumCleaner(House houseToClean) {
        this.house = houseToClean;
    }

    public boolean isRoomDirty(int roomId) {
        // find the room in the house, and see if it contains dirt
    }

    public void cleanRoom(int roomId) {
        // find the room in the house, and remove dirt from it
    }
}

关于Java: 'agent' 对象内部的 'world' 对象如何获取有关 'world' 的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314621/

相关文章:

java - 将同步方法转换为非阻塞算法

javaoctave jar 文件在 Mac OSX Eclipse 中失败

java - 简单的对象持久化策略 - hibernate ?

c# - 如何在不知道提前指定数量的情况下创建对象数组

algorithm - 为什么减少 K 近邻中的 K 会增加复杂性?

java - 如何设置JTextField的透明文本颜色

java - 将 Collection<T> 转换为 List<T>

oop - 在类图中表示 main()

artificial-intelligence - 是否存在 "Society of Mind"模型的实现(最好是开源)

algorithm - 遗传算法代码中的排序选择