решение проблемы с игрушками «VacumCleaner» с помощью простого рефлекторного агента - PullRequest
2 голосов
/ 17 октября 2019

Я изучаю bscs и изучаю «Искусственный интеллект».

Это простая программа агента рефлекса, она работает на 'Python', но то же самое, что я пробовал на p5.js(JavaScript) для создания пользовательского интерфейса.

Но я получил эту ошибку, может кто-нибудь сказать мне, почему this.currentRoom не получает this.room1 ??

Я добавляюскриншот ошибки здесь

или вы можете просто скопировать его и в онлайн-редакторе, чтобы узнать, что происходит на самом деле. в первый раз, когда я спрашиваю о stackoverflow.

function setup(){
   createCanvas(600,400);
    vc = new VAgent();
    twoRooms = new VEnvironment(vc);
    twoRooms.executeStep(6);

}
function draw(){
    background(0);
}

class Room{
    constructor(location,status){
        this.location=location;
        this.status=status;
    }
    getAll(){
        console.log(this.location);
        console.log(this.status);
    }
}

class VEnvironment{
    contructor(agent){
        this.agent=agent;
        this.room1=new Room('a','Dirty');
        this.room2=new Room('b','Dirty');
        this.currentRoom=this.room1;
        this.actionStatus='';
        this.step=0;

    }

    executeStep(n){
        for(var i=0;i<n;i++){
            this.displayPerception();
            this.agent.sense(this);
            var res = this.agent.action();
            if(res=='clean'){
               this.currentRoom.status=='clean'
            }else if(res=='Right'){
               this.currentRoom=this.room2;
            }else{
                this.currentRoom=this.room1;
            }
            this.displayAction();
            this.step++;
        }
    }
    displayPerception(){
        console.log('Agent is Present At Room '+this.currentRoom.location+' And The Status For Room Is '+this.currentRoom.status);
    }
    displayAction(){
        console.log('Agent took at'+this.step+' And Action was ... '+this.currentRoom+'...');
    }
}


class VAgent{
    constructor(){

    }

    sense(currentEnv){
        this.en=currentEnv;
    }
    action(){
        if(this.en.currentRoom.status=='dirty'){
           return 'Clean'
        }else if(this.en.currentRoom.location=='a'){
           return 'Left'
        }else{
            return 'Right'
        }
    }
}

1 Ответ

2 голосов
/ 17 октября 2019

Если у вас есть сложный код, который вы не понимаете, лучшее, что вы можете сделать, - это сузить проблему до упрощенного примера программы .

Например,Вы можете изолировать свою проблему в этом коде:

function setup() {
  createCanvas(600, 400);

  const myContainer = new Container();
  myContainer.displayValue();
}

function draw() {
  background(0);
}

class Value {
  constructor() {
    this.x = 42;
  }
}

class Container {
  contructor() {
    this.value = new Value();
    this.currentvalue = this.value;
    console.log('constructor value: ' + this.currentValue);
  }

  displayValue() {
    console.log('display value: ' + this.value.x);
  }
}

Этот код представляет ту же проблему, что и ваш код, без какого-либо дополнительного кода, не связанного с вашей проблемой.

Если вы запустите этот код, выВы заметите, что оператор печати constructor value никогда не срабатывает. Это ключ к более внимательному рассмотрению конструктора.

Ваша проблема - опечатка: у вас contructor вместо constructor.

...