Docsity

Final Project Report on Artificial Intelligence | CS 627, Exams of Computer Science

Toggle sidebar
bg1
CS627 Alex Mikhaylov
Dr. P. Benjamin May 5, 1999
Artificial Intelligence
Final Project
The Final project includes implementations of Depth-first search and Breadth-first search
applied to the missionaries and cannibals problem. The following is the formal definition
of the problem (Artificial Intelligence, S. Russell & P. Norvig):
States: a state consists of an ordered sequence of three numbers representing the
number of missionaries, cannibals, and boats on the bank of the river from which they
started. Thus the start state is (3,3,1).
Operators: from each state the possible operators are to take either one missionary,
one cannibal, two missionaries, two cannibals, or one of each across in the boat.
Thus, these are the most five operators, although most states have fewer because it’s
necessary to avoid the illegal states.
Goal test: reached state (0,0,0)
Path cost: number of crossing.
Both of the search techniques were implemented using the General-Search algorithm
with a queuing function that puts the newly generated states in a queue. Theoretically the
Depth-first search has the following characteristics: not complete, exponential time
complexity, linear space complexity, not optimal. The characteristics for the Breadth-first
search are: complete, optimal if all operators have the same cost, exponential time and
space complexity. The solutions that were found by these searches in the missionaries
and cannibals problem proved it:
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Final Project Report on Artificial Intelligence | CS 627 and more Exams Computer Science in PDF only on Docsity!

CS627 Alex Mikhaylov

Dr. P. Benjamin May 5, 1999

Artificial Intelligence

Final Project

The Final project includes implementations of Depth-first search and Breadth-first search

applied to the missionaries and cannibals problem. The following is the formal definition

of the problem (Artificial Intelligence, S. Russell & P. Norvig):

 States: a state consists of an ordered sequence of three numbers representing the

number of missionaries, cannibals, and boats on the bank of the river from which they

started. Thus the start state is (3,3,1).

 Operators: from each state the possible operators are to take either one missionary,

one cannibal, two missionaries, two cannibals, or one of each across in the boat.

Thus, these are the most five operators, although most states have fewer because it’s

necessary to avoid the illegal states.

 Goal test: reached state (0,0,0)

 Path cost: number of crossing.

Both of the search techniques were implemented using the General-Search algorithm

with a queuing function that puts the newly generated states in a queue. Theoretically the

Depth-first search has the following characteristics: not complete, exponential time

complexity, linear space complexity, not optimal. The characteristics for the Breadth-first

search are: complete, optimal if all operators have the same cost, exponential time and

space complexity. The solutions that were found by these searches in the missionaries

and cannibals problem proved it:

Searching Deep

FOUND!!!

Went through 12 states FOUND!!! Went through 53 states

Hence, one can see that indeed space

complexity in Depth-first search is linear when in Breadth-first search it’s exponential. In case of the missionaries and cannibals problem the solution found by Depth-first search happened to be the optimal one. Actually, it seems that in this problem there is only one solution, which was found by both Depth-first and Breadth-first searches. For the details on how the program was developed please see comments in the

  • ML:0 MR:3 CL:0 CR:3 B:
  • ML:0 MR:3 CL:2 CR:1 B:
  • ML:0 MR:3 CL:1 CR:2 B:
  • ML:0 MR:3 CL:3 CR:0 B:
  • ML:0 MR:3 CL:2 CR:1 B:
  • ML:2 MR:1 CL:2 CR:1 B:
  • ML:1 MR:2 CL:1 CR:2 B:
  • ML:3 MR:0 CL:1 CR:2 B:
  • ML:3 MR:0 CL:0 CR:3 B:
  • ML:3 MR:0 CL:2 CR:1 B:
  • ML:2 MR:1 CL:2 CR:1 B:
  • ML:3 MR:0 CL:3 CR:0 B:
    • Depth: 1 ML:0 MR:3 CL:0 CR:3 B: Searching Wide
    • Depth: 2 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 2 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 2 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 3 ML:0 MR:3 CL:1 CR:2 B:
    • Depth: 3 ML:0 MR:3 CL:1 CR:2 B:
    • Depth: 4 ML:0 MR:3 CL:3 CR:0 B:
    • Depth: 4 ML:0 MR:3 CL:3 CR:0 B:
    • Depth: 4 ML:0 MR:3 CL:3 CR:0 B:
    • Depth: 4 ML:0 MR:3 CL:3 CR:0 B:
    • Depth: 5 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 5 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 5 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 5 ML:0 MR:3 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 6 ML:2 MR:1 CL:2 CR:1 B:
    • Depth: 7 ML:1 MR:2 CL:1 CR:2 B:
    • Depth: 7 ML:1 MR:2 CL:1 CR:2 B:
    • Depth: 7 ML:1 MR:2 CL:1 CR:2 B:
    • Depth: 7 ML:1 MR:2 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 8 ML:3 MR:0 CL:1 CR:2 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 9 ML:3 MR:0 CL:0 CR:3 B:
    • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 10 ML:3 MR:0 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 11 ML:2 MR:1 CL:2 CR:1 B:
      • Depth: 12 ML:3 MR:0 CL:3 CR:0 B:

MisRight=0; CanLeft=0; next=NULL; CanRight=0; } void State::Initialize() { Boat=right; MisLeft=0; MisRight=3; CanLeft=0; CanRight=3; operation=any; next=NULL; } bool State::IsValid() { //Check if # of missioners>=0, number of cannibals>=0, //and missioners>=cannibals if (MisRight==0) { if ((MisLeft>=CanLeft) && (MisLeft>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } else { if (MisLeft==0) { if ((MisRight>=CanRight) && (MisRight>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } else { if ((MisRight>=CanRight) && (MisLeft>=CanLeft) && (MisRight>=0) && (MisLeft>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } } } void State::PrintState() { //Print out this state cout<<"ML:"<<MisLeft<<" MR:"<<MisRight<< " CL:"<<CanLeft<<" CR:"<<CanRight<< " B:"<<Boat<<endl;

bool State::IsGoal() { //Checks whether it is a goal state //(whether ML=3, MR=0, CL=3, CR=0, B=left) if ((MisLeft==3) && (CanLeft==3) && (MisRight==0) && (CanRight==0) && (Boat==left)) { return (true); } else return (false); } State* State::GetChildQueue() { State* TempChild_two_m; State* TempChild_two_c; State* TempChild_one_m; State* TempChild_one_c; State* TempChild_one_m_one_c; State* Head; //pointer to the head of the child queue to be formed State* temptr; //pointer to move along child queue TempChild_two_m = new State; TempChild_two_c = new State; TempChild_one_m = new State; TempChild_one_c = new State; TempChild_one_m_one_c = new State; Head=new State; temptr=new State; Head=NULL; temptr=NULL; if (Boat==right) { //boat is on the right bank //try to transport 2 missioners TempChild_two_m->Boat=left; TempChild_two_m->MisRight=MisRight-2; TempChild_two_m->MisLeft=MisLeft+2; TempChild_two_m->CanRight=CanRight; TempChild_two_m->CanLeft=CanLeft; if (TempChild_two_m->IsValid() && operation!=two_m) { //the generated child is valid and is not the result of the same operation as parent TempChild_two_m->next=NULL; TempChild_two_m->operation=two_m; if (Head==NULL) { //it is the first child in the queue, make it head Head=TempChild_two_m; temptr=Head;

else { temptr->next=TempChild_one_m; temptr=TempChild_one_m; } } else { TempChild_one_m=NULL; delete TempChild_one_m; } //try to transport 1 cannibal TempChild_one_c->Boat=left; TempChild_one_c->MisRight=MisRight; TempChild_one_c->MisLeft=MisLeft; TempChild_one_c->CanRight=CanRight-1; TempChild_one_c->CanLeft=CanLeft+1; if ((TempChild_one_c->IsValid()) && (operation!=one_c)) { TempChild_one_c->next=NULL; TempChild_one_c->operation=one_c; if (Head==NULL) { Head=TempChild_one_c; temptr=Head; } else { temptr->next=TempChild_one_c; temptr=TempChild_one_c; } } else { TempChild_one_c=NULL; delete TempChild_one_c; } //try to transport 1 missioner and 1 cannibal TempChild_one_m_one_c->Boat=left; TempChild_one_m_one_c->MisRight=MisRight-1; TempChild_one_m_one_c->MisLeft=MisLeft+1; TempChild_one_m_one_c->CanRight=CanRight-1; TempChild_one_m_one_c->CanLeft=CanLeft+1; if ((TempChild_one_m_one_c->IsValid()) && (operation! =one_m_one_c)) { TempChild_one_m_one_c->next=NULL; TempChild_one_m_one_c->operation=one_m_one_c; if (Head==NULL) { Head=TempChild_one_m_one_c; temptr=Head; } else

temptr->next=TempChild_one_m_one_c; temptr=TempChild_one_m_one_c; } } else { TempChild_one_m_one_c=NULL; delete TempChild_one_m_one_c; } } else {//boat is on the left bank //try to transport 2 missioners TempChild_two_m->Boat=right; TempChild_two_m->MisRight=MisRight+2; TempChild_two_m->MisLeft=MisLeft-2; TempChild_two_m->CanRight=CanRight; TempChild_two_m->CanLeft=CanLeft; if ((TempChild_two_m->IsValid()) && (operation!=two_m)) { TempChild_two_m->next=NULL; TempChild_two_m->operation=two_m; if (Head==NULL) { Head=TempChild_two_m; temptr=Head; } else { temptr->next=TempChild_two_m; temptr=TempChild_two_m; } } else { TempChild_two_m=NULL; delete TempChild_two_m; } //try to transport 2 cannibals TempChild_two_c->Boat=right; TempChild_two_c->MisRight=MisRight; TempChild_two_c->MisLeft=MisLeft; TempChild_two_c->CanRight=CanRight+2; TempChild_two_c->CanLeft=CanLeft-2; if ((TempChild_two_c->IsValid()) && (operation!=two_c)) { TempChild_two_c->next=NULL; TempChild_two_c->operation=two_c; if (Head==NULL) { Head=TempChild_two_c; temptr=Head; } else

else { TempChild_one_c=NULL; delete TempChild_one_c; } //try to transport 1 missioner and 1 cannibal TempChild_one_m_one_c->Boat=right; TempChild_one_m_one_c->MisRight=MisRight+1; TempChild_one_m_one_c->MisLeft=MisLeft-1; TempChild_one_m_one_c->CanRight=CanRight+1; TempChild_one_m_one_c->CanLeft=CanLeft-1; if ((TempChild_one_m_one_c->IsValid()) && (operation! =one_m_one_c)) { TempChild_one_m_one_c->next=NULL; TempChild_one_m_one_c->operation=one_m_one_c; if (Head==NULL) { Head=TempChild_one_m_one_c; temptr=Head; } else { temptr->next=TempChild_one_m_one_c; temptr=TempChild_one_m_one_c; } } else { TempChild_one_m_one_c=NULL; delete TempChild_one_m_one_c; } } //return pointer to the head of the child queue return (Head); } void State::SearchDeep() {//deep search algorithm State* ChildQueuePtr; ChildQueuePtr=new State; ChildQueuePtr=NULL; if (Found) //check whether recursion has to be stopped return; NumStates++; PrintState(); if (!IsGoal()) //if this is not goal state { ChildQueuePtr=GetChildQueue(); //get child queue for this state

while (!ChildQueuePtr==NULL) { ChildQueuePtr->SearchDeep(); //perform recursive deep search on the head //of the child queue ChildQueuePtr=ChildQueuePtr->next; //move along child queue } return; } else { cout<<"FOUND!!!"<<endl; cout<<"Went through "<<NumStates<<" states"<<endl; Found=true; //set Found=true to stop recursion return; } } State* State::GetTail() {//get tail of the queue referenced by this state State* temptr; temptr=new State; temptr=this; while (!temptr->next==NULL) temptr=temptr->next; return (temptr); } void State::SearchWide() { /wide search algorithm this function first checks all states at the certain depth, then, if it does not come across the goal state, gets children for all the states at that depth, links all the children into one queue and passes that queue back to this function for recursion/ State* ChildQuePtr; State* temptr; State* ChildHead; State* NextChild; ChildQuePtr=new State; temptr=new State; ChildHead=new State; NextChild=new State; ChildQuePtr=NULL; ChildHead=NULL; NextChild=NULL; if(Found) //stop recursion if goal state is found return; temptr=this;

Found=false; NumStates=0; cout<<"Searching Deep"<<endl<<endl; //perform deep search InitState.SearchDeep(); //re-initialize global variables Found=false; Depth=0; NumStates=0; cout<<"Searching Wide"<<endl<<endl; //perform wide search InitState.SearchWide(); }