Back to Cosmos

Vaccum Cleaner (1)

code/artificial_intelligence/src/Vaccum_Cleaner/Vaccum_cleaner (1).ipynb

latest4.3 KB
Original Source
python
import matplotlib.pyplot as plt
from matplotlib import colors
import random

Visualising the room

python
def visuals(room):
  plt.grid(color='black', linestyle='--', linewidth=2)
  plt.xlim(0, 5)
  plt.ylim(0, 5)
  for i in range(5):
    for j in range(5):
      if(room[i][j]==1):
        plt.scatter(i+0.5,j+0.5,color='red', s=1000)
      elif(room[i][j]==-1):
        plt.scatter(i+0.5,j+0.5,color='blue', s=1000)
      else:
        plt.scatter(i+0.5,j+0.5,color='green', s=1000)
  plt.show()
python
def evening():
  for i in range(15):                                                              
    x = random.randint(0,4) 
    y = random.randint(0,4)
    if(room[x][y]!=-1): #If the Tile doesn't contain an obstacle then placing the dirt on it.
      room[x][y] = 1
    else:
      room[x][y] = -1
  return room 
python
def cleanRoom(Battery, Room):
  for i in range(5):
    j=0
    print("#Current Battery Status: ",Battery)
    while(j<5):
      #Once we see the tile containing the obstacle the cleaner cannot move to ABOVE tile there for going for Below Tile
      if(Room[i][j]==-1): 
        print("As there is an Obstacle Going to the BELOW TILE")
        Battery-=1
        j = 5
      else:
        print("----------------------------------------------")
        #If the Current Tile is Dirty
        print("*Checking the Current Tile")
        if(Room[i][j]!=-1 and Room[i][j]==1):
          print("The Current Tile is Dirty Cleaning it")
          Room[i][j]=0
          Battery-=1
          print("The location "+str(i)+" "+str(j)+" is Cleaned")
        elif(Room[i][j]!=-1 and Room[i][j]==0):
          print("The Location "+ str(i)+" "+str(j) + " is Clean")
          Battery-=1
        else:
          print("There is an Obstacle at Location " +  str(i)+" "+str(j))

        #Checking whether the ABOVE Side is clean or not
        
        print("*Checking the ABOVE Tile")
        if((j<4) and (Room[i][j+1]!=-1 and Room[i][j+1]==1)):
          print("The Location "+ str(i)+" "+str(j+1) + " is Dirty")
          Battery-=1
          Room[i][j+1]=0
          print("Moving the Cleaner ABOVE and Cleaning it and Positioning it back to "  + str(i)+" "+str(j))
        elif((j<4) and (Room[i][j+1]==0 and Room[i][j+1]!=-1)):
          print("The Location "+ str(i)+" "+str(j+1) + " is Clean")
          Battery-=1
        else:
          print("There is an Obstacle at Location " +  str(i)+" "+str(j+1))

        #After Cleaning the ABOVE Side Checking for the Below
        print("*Checking the RIGHT Tile")
        if((i<4) and (Room[i+1][j]!=-1 and Room[i+1][j]==1)):
          print("The Location "+ str(i+1)+" "+str(j) + " is Dirty")
          Battery-=1
          Room[i+1][j]=0
          print("Moving the Cleaner ABOVE and Cleaning it and Positioning it back to "  + str(i)+" "+str(j))
        elif((i<4) and (Room[i+1][j]!=-1 and Room[i+1][j]==0)):
          print("The Location "+ str(i+1)+" "+str(j) + " is Clean")
          Battery-=1
        else:
          print("Obstacle Present at "+str(i+1)+" "+str(j))
        
        #Checking the LEFT Tile
        print("*Checking the LEFT Tile")
        if((i>1)and (Room[i-1][j]!=-1 and Room[i-1][j]==1)):
          print("The Location "+ str(i-1)+" "+str(j) + " is Dirty")
          Room[i-1][j]=0
          Battery-=1
          print("Moving the Cleaner ABOVE and Cleaning it and Positioning it back to "  + str(i)+" "+str(j))
        elif((i>1) and (Room[i-1][j]!=-1 and Room[i-1][j]==0)):
          print("The Location "+ str(i-1)+" "+str(j) + " is Clean")
          Battery-=1
        elif((i>=1)):
          print("Obstacle Present at "+str(i-1)+" "+str(j))
        else:
          print("We are at LEFT-MOST Level")
      j+=1
      print("The Room After Cleaning is ")
      visuals(Room)

  return Battery
python
room = [[0,0,0,0,-1], [0,-1,0,0,0], [0,0,0,0,0], [0,0,0,0,-1], [0,0,0,-1,0]]    #initial condition in the morning
visuals(room)
python
room = evening()
Battery = 100
visuals(room)
print ("Vacuum is placed at Location A at 0,0")
print("**** Cleaning Room A ****")
Battery = cleanRoom(Battery, room)
print("********The Room A After Cleaning is ")
visuals(room)
print("The Battery Remaining in the Vaccum Cleaner After Cleaning Room A is ",Battery)
print("************************************************************")