Search code examples
javascriptartificial-intelligenceundefinedp5.jsagent

popular 'vaccumCleaner' toy problem solution using simple reflex agent


I'm student of bscs and I'm studying 'Artificial Intelligence'.

It is a simple reflex agent program, It is working on 'Python' but the same thing I tried on p5.js (JavaScript) to make a UI also.

But I got this error, can anyone tell me why this.currentRoom is not getting this.room1??

I'm adding error screenshot over here

or you can just copy it and on online editor to get actually what is going on.

sorry If I'm asking in bad manner actually its my very first time that I'm asking on 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'
        }
    }
}


Solution

  • When you have a piece of complicated code that you don't understand, the best thing you can do is narrow your problem down to a simplified example program.

    For example, you might isolate your problem into this code:

    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);
      }
    }
    

    This code exhibits the same problem as your code, without any extra code unrelated to your problem.

    If you run this code, you'll notice that the constructor value print statement is never triggered. That's the clue to look more closely at the constructor.

    Your problem is a typo: you have contructor instead of constructor.