Emre Ugur

Ph.D.

CMPE540 Principles of Artificial Intelligence
Project 3


Announced: 14/04/2020
Due: 05/05/2020

Download

  • Please download p3.tgz and implement your algorithms within this framework.

Submission Instructions

  • Please follow these instructions, otherwise you will significant points.
  • The project will be automatically evaluated. Still you need to prepare a very short report. See the end of this document (or click here) for more information about what I expect in the report.
  • Name your report as student-id.pdf. e.g.: 2014301162.pdf
  • Copy this report into p3/ directory.
  • Create an archive. Name it student-id.tgz with the following command.
    $ tar -czvf 2014301162.tgz p3/
  • Attach the archive file to an email and send it to emre.ugur@boun.edu.tr with the following subject line: [CMPE540 Project 3]


Discussion and cheating

I don't like this section, but still should write about this.. You are welcome to discuss the topics about the project. But please do not cheat. There are programs that can automatically check the similarity of the source code and/or execution. Read the department policy about what is considered as cheating. If I am convinced that you cheated, then you will get F.

Questions about the project

  • The details are explained in the lecture.
  • Please warn me as soon as you see a mistake through email or mailing-list.
  • In case you have questions, please send them to the mailing-list so that
    • Everybody can see the answer to that question
    • People other than me can reply the questions as well

Project objectives

In this project, you will implement the logical rules for the domain below, and verify them using Prolog.

Introduction to the problem

The environment includes:
  • Blue marker: Our agent with direction.
  • E: Enemy
  • P: Pit
  • .: Food
  • %: Wall
Directions:
  • North, East, South, West
Actions:
  • Forward: Move forward one grid in the current direction
  • Eat: Eat the food in the current grid
  • ClockWise: Turn clockwise
  • CounterClockWise: Turn counter-clockwise
  • Attack: Move forward one grid and kill the enemy if there is an alive enemy in the new grid.
Environment Model:
  • Enemy, food, and pit create smell, smell and breeze in the 4-neighbourhood.
  • Enemy, food, and pit does not create smell, smell and breeze in their own grid.
  • Enemy is roughly visible from 5 grids away if it is in the same direction.
  • If agent steps in a grid with a pit or an alive enemy, it dies.
  • If agent steps in a grid with dead enemy, nothing happens.
  • Food and its smell disappear after food being eaten.
  • Enemy and its smell does not disappear after enemy being killed.
  • If agent hits the wall as a result of forward or attack actions, "bump" is sensed.
  • If agent successfully eats the food, "full" is sensed.

The objective is to let the agent wander around without being killed. But this objective will be detailed in the following questions. Assume max time step is 50.

Compilation

  • If your linux terminal does support colored output (many of them do), compile with "make", otherwise with "make NO_COLOR=1".
  • Install swiprolog: sudo apt-get install swi-prolog

Execution

  • Execution command line arguments are provided if you make a mistake in use
  • Simply type "./p3 -help" which is a mistake :).


Warming up

  • A random search which randomly selects one action is implemented for you:
    ./p3 -debug 0 -steps 5 -search random -layout layouts/killDanger2.lay -update 1
  • The agent is not killed by any pit or enemy. It is ok, we will not be checking any score, etc.


The main loop in p3.cpp

  • 26 creates an environment.
  • 27 creates an agent.
  • 28 initializes knowledge-base (KB) with initial position and direction sentences (e.g. location(1,1,1). dir(1,east).)
  • 31 perceives food smeel, enemy smell, pit breeze, and enemy sight
  • 32 tell KB about the perceived sentences
  • 33 asks inference engine (prolog) to find out the possible consequences of its actions, and select an action based on the consequence and rule table.
  • 34 executes the selected action, updates KB with isBumped and isFull.


Agent's reasoning

Agent's reasoning works as follows:
  • rules.pl contains all the rules that are implemented in the project.
  • current.pl contains the history of percepts and actions.
  • all.pl is a temporary file that contains the complete KB used in prolog inference.
  • In each step t,
    • for each action a
      • the action is added to all.pl, 'action(t,a).'
      • the next grid of the agent is inferred, 'location(t,r,c)."
      • some other predicates are inferred for this next grid, such as
        • is the grid clear at time T (isClear(T,R,C))
        • does the grid contain a pit at time T (hasPit(T,R,C))
        • does the grid contain no pit at time T (hasNotPit(T,R,C)), etc.
      • action selection is done based on such reasoning.
  • The main function (on the right) that makes inference is "bool LogicAgent::callProlog (string KB, string query)"


Debugging

As you only need to add rules to rules.pl; you might only get error in calling prolog. This will be automatically detected. In case there is a problem (such as undefined procedures), you will get this screen. And the following error message for one example:
swipl -s KB/all.pl -t 'isClear(2,1,2).'; echo $?
The command above was not successful
Something is wrong with your rules in rules.pl
Run the command yourself and see the reason.
[Error] in exiting...
If you get a message like this, run the corresponding command, and see the problem yourself.

Important

Do not forget, your agent is almost blind-folded. After initial step, it cannot get any information regarding to its own location or around grids directly from environment object. In other words, your agent CANNOT/SHOULD NOT use any information other than:
  • initial position and direction, automatically set by agent->initKB();
  • walls start from (0,0) and extend
  • percepts provided by the environment:
    • isPitBreeze, isEnemySmell, isFoodSmell, isEnemySight, isBumped, isFull

Action decision for agent

  • Our agent should move following the following rules. Select an action with the following priorities. Select if it
    • is a good move (i.e. eating the food in the current grid),
    • is a good move (i.e. moving to a grid with food),
    • is a good move (i.e. attacking to an alive enemy),
    • is an ok move (not hitting an alive enemy or pit),
    • is not a bad move (i.e. moving to a clear grid),
  • If several actions provide the same consequence, select based on their ordering.
  • If no action is selected, select "forward".

In this project, you need to implement the following predicates (depending on Q1-4):
  • isClear(T,X,Y).
  • hasFood(T,X,Y).
  • hasNotFood(T,X,Y).
  • hasEnemy(T,X,Y).
  • hasNotEnemy(T,X,Y).
  • hasPit(T,X,Y).
  • hasNotPit(T,X,Y).
  • hasDeadEnemy(T,X,Y).
isClear(T,X,Y) is already partially implemented for you.

Q1: Bump no more! (4pt)

Improve isWall predicate and update isClear if necessary, so that the agent does not bump to the already bumped wall.
./p3 -debug 0 -steps 10 -search prolog -layout layouts/wallsOnly.lay -update 100
Grading: The order of "SELECTED ACTION"s.

Q2: Avoid dangers (4pt)

Implement hasNotEnemy and hasNotPit predicates. Your agent should avoid all the dangers in the following:
./p3 -debug 0 -steps 1 -search prolog -layout layouts/avoidDanger.lay -update 100
Grading: The order of "SELECTED ACTION"s.

Q3: Make sure dangers and kill enemies (4pt)

Implement hasEnemy, hasPit and hasDeadEnemy predicates. Your agent should avoid all the dangers, make sure the location of the enemies and pits when possible, and therefore be able to kill the enemies.
./p3 -debug 0 -steps 1 -search prolog -layout layouts/killDanger1.lay -update 1
Grading: The order of "SELECTED ACTION"s.

Q4: Eat food (4pt)

Implement hasFood and hasNotFood predicates. Your agent should be able to locate the grid with food (when possible), step in and eat the food.
./p3 -debug 0 -steps 1 -search prolog -layout layouts/foodClassic.lay -update 1
Grading: The order of "SELECTED ACTION"s.

Report

No report is necessary this time.