Вы находитесь на странице: 1из 15

University of Birmingham

EE3J1
Artificial intelligence

Coursework Assignment

Name: Dongning Zhu


Student ID: 1054485
Email: DXZ885@bham.ac.uk
Tutor: Dr. Edward Tarte

1
Content page

1. Introduction………………………………………………………………………….3
2. Fox and hounds game principle…………………………………………………..3
2.1 Analysis of the game principle………………………………………………..4
3
. My program design…………………………………………………………………4
3.1 Functions created in my program design……………………………………5
3.2 Flow chart for the first level program…………………………………………6
3.3 How the functions operate has been listed as follows……………………..7
3.4 Flow chart explanation………..……………………………………………….8
4. Artificial intelligence…………………………………………………………………8
4.1 Minimax…………………………………………………………………………8
4.1.1 For minimax with no depth………………………………………….9
4.1.2 For minimax with depth…………………………………………….10
4.2 Alpha-beta pruning……………………………………………………………11
5
. My achievements…………………………………………………………………...12
5.1 The first level achievement…………………………………………………..12
5.2 The second level achievement………………………………………………12
5.3 The third level achievements………………………………………………...13
5.4 Amount of memory consumed……………………………………………….13
6
. Conclusion…………………………………………………………………………...14
7
. Reference……………………………………………………………………………15

2
1. Introduction

As for the 2010 EE3J1 course, a game called fox and hounds has been required to
be designed individually utilizing programming software named python. This course
and assignment demand us to create and design the game with artificial intelligence
embedded, which means a computer could play a role either as fox and hounds
automatically as an advanced version of person playing with person.

For this purpose, it is essential for me to be familiar with and master how to program
the python as my first stage and have a good understanding of artificial intelligence
concepts and how this great idea could be applied onto my python programming.

Artificial intelligence is a kind of intelligence which a computer could analysis the


data and state automatically with certain logic in order to accomplish some task by
itself. Therefore, in our case, the computer should have its own capability to analyze
the situations by itself and make some judgements.

The contents in this paper afterwards have been divided into fourth parts. In the first
part, the fox and hound game principle are notified as the goal which my robot
should realize. In the second part, the design of the foxandhound game is described
to give a comprehensive explaination and outllook. In the third part, concepts of
artificial intelligence have been done in order to illustrate that how they could be
applied onto my programming. Last but not the least; the paper has posted multiple
state in my programming achievements

2. Fox and hounds game principle

Since a game called fox and hounds is required to be programmed and realized, I
need to articulate about the principle of fox and hounds and the principle should be
fully underastood and could be dissected into several parts. They are presented as
follows:

Firstly, it should be played on an 8X8 checkerboard.

Secondly, a fox should be placed on the edge of board and four hounds should be
located on the opposite edge of the board. A figure of this kind of board will be
shown below to display a straightforward image.

3
(Figure 1: the board of fox and hound game which I design)

Thirdly, fox could move one position diagonally from the start point either forward or
backwards, while a hound could also move diagonally from their start point but only
forward and be prohibited to move backwards.

Fourthly, the condition to win the game is that the fox should cross the board and
finish on hound’s initial squares or the fox has passed all the hounds which means
no hounds could catch up with the fox again.

All these principles of fox and hounds game will be incisively analysed in order to
program it and these information have provided a strong condition for the program
design.

2.1 Analysis of the game principle

At first glance of the game principle, it appears that it is really difficult for hounds to
catch up with fox as they could only move forward, which has greatly influence the
flexibility of movements. However, a balance for both sides has existed because
hounds has more number than fox, which will make it possible to catch the fox in
some circumstances but with a little hard attempt.

The best method to achieve it is to always keep the hounds extend to occupy the
whole horizontal line, leaving no opportunity for fox to cross through them. This is the
best defence strategy for hounds to catch the fox but require to move cautiously and
hard to control as the fox is much flexible to move.

3. My program design

It is necessary to be familiar with the python language syntax before the game has
started to be programmed. In my program design, I start my attempt my
programming from basic level to high level since the difficult and complex of the
programming has been increased dramatically. Therefore, three levels of
programming have been achieved by me. They are listed as follows:

The first level is the games played with person to person

4
The second level is the games played by one person as hounds and a computer as
fox which play randomly. Additionally, it has been played between computer and
computer as hounds which play randomly

The third level is the games played by one person as fox and a computer as hounds
which play with the help of artificial intelligence, which will be deliberated explained
later.

3.1 Functions created in my program design

For the first level of programming, I created one class called ‘hounds_and_fox’ and
several functions have been added inside this class. They are LeFoxMoves,
LegalMoves, make_move, foxposition and play_game, TermTest

For the second level of programming, the syntax of move has been changed with the
introducing of random function to realize my purpose of moving artificially.

As for my third level programming, a new function called move_hound has been
added for computer to play the move automatically with certain algorithms.

The relationship between different functions in different levels is the same, which will
be displayed as follows:

Main function

Print the title of


the game

Game instance
from class Fox
andHound

Function
game_play

5
(Figure 2: main function flow chart)

3.2 Flow chart for the first level program

Start

Game.initial

Game.display

TermTest==0

TermTest ==1 If To_move==’F’

elif
Print Print
Print LeFoxMoves hound_legal_move

Foxposition Input fx
TermTest ==-1
Input fy

Input tx
Input tx
Print

Input ty Input ty

Make_move
Make_move
6
(Figure 3: play_game function flow chart)

3.3 How the functions operate has been listed as follows

1 2.
State Move
State

LegalMoves
Make_move

W for fox or hounds move

Legalmoves

3. 4.
State
Move_hound

LegalMoves
TermTest
function Find fox position

Find hound
Return number position
4

Calculate the
5.
distance
State

Find the largest


foxposition distance
.

Find the
counterpart point
foxp

7
(Figure 4: function explanation)

AS in my second third level programming design, a random function will replace the
syntax of raw_input to obtain the value of a hound movement.

The move hound function in my third level programming design will replace the
syntax of raw_input to obtain the value of a hound movement.

3.4 Flow chart explanation

The flow charts shown above have illustrated how my programming design executes
from start state to the final state.

For the first level of program, which is person with person, the exact move has been
determined and input manually in order to realize the function of person to person
play for minimax game. This is a basic design of my game.

For the second level of program, the fox move will be dependent on computer
random functions. Although it is a kind of low level artificial intelligence, we could still
utilize this program to have a play with computer automatically, which has realized a
little about artificial intelligence.

As for the third level of program, the hounds move will be absolutely dependent on
the search algorithm created by the ‘move_hound’ function. The move_hound
function has been designed based on following principles. Firstly, the positions of fox
and hounds have been discovered and stored in two separate lists. Then the
distance between the fox and hounds has been calculated and compared to find the
largest one. After that the counterpart point of the hound could be found, which will
return from the function and pass to the move function to execute.

4. Artificial intelligence

The core part of how the computer moves should be reckoned as artificial
intelligence design for this coursework. Before I go through the concrete content of
AI, it is essential to acquire the ideas and be familiar with what AI is.

Artificial intelligence is a kind of machines that perceives its situation and takes
methods which could maximize its chance of success for the side going to play. In
my search design, it has been separated into several kinds from minimax to alpha-
beta pruning. These two types will be explained deliberately with the pseudo-code
shown.

4.1 Minimax

8
Minimax is a type of state space search algorithms and is a search procedure of
depth-limited. The function of minimax will automatically evaluate each state. Its
target is to find maximum value for the side about to move in next step and pick up
the best move from the worst move played by rival.

Minimax has been applied in decision theory, game theory, statistics and philosophy
for maximizing the gain while minimizing the adversarial steps. It is often
implemented in a mixed strategy.

For minimax search algorithm, it requires a utility function to return a score at the end
of the state if the search function would not predict the step until the last step, which
could determine which player has won. However, it would have an evaluation
function to feedback as score if the search function has a limited depth to determine
which move is best among all the possible choice.

The pseudo code for both conditions will be presented as follows:

4.1.1 For minimax with no depth

def minimax_decision(state, game):

player = game.to_move(state)

def max_value(state):

if game.terminal_test(state):

return 1

v = -infinity

for (a, s) in game.successors(state):

v = max(v, min_value(s))

def min_value(state):

if game.terminal_test(state):

return -1

v = infinity

for (a, s) in game.successors(state):

v = min(v, max_value(s))

return v

9
# Body of minimax_decision starts here:

action, state = argmax(game.successors(state),

lambda ((a, s)): min_value(s))

return action

The first type of minimax code with no depth could be inherited from the program of
tictactoe game. They have utilized the same principle. The only difference is that the
game. successor function should be modified a little to be suitable to the fox and
hounds game.

4.1.2 For minimax with depth

def minimax_decision(depth, state, game):

player = game.to_move(state)

def max_value(state):

if Depth==0:

return evaluation function return value

v = -infinity

for (a, s) in game.successors(state):

v = max(v, min_value(s))

def min_value(state):

if Depth==0:

return evaluation function return value

v = infinity

for (a, s) in game.successors(state):

v = min(v, max_value(s))

return v

# Body of minimax_decision starts here:

action, state = argmax(game.successors(state),

10
lambda ((a, s)): min_value(s))

return action

Since the minimax function code with depth has not been realized by me, it is a
theoretical design of programming, which could be embedded into my second
achievement file with evaluation functions.

4.2 Alpha-beta pruning

Alpha-beta pruning is an advanced version of minimax which could reduce node


number generated by the minimax algorithm. It is often utilized in a checkerboard
game. It will stop evaluating a move when the move will be worse than a previously
examined move. There is no need to evaluate that move again. Alpha-beta pruning
is an optimization of minimax search algorithms. It could improve the analyzing time
of one successor step and save the memory storage.

The benefit of alpha-beta pruning could be explained that some branches of the
search tree can be eliminated. The search time can in this way be limited to the
'more promising' subtree, and a deeper search could be performed as an
advancement.

Def alphabeta (node, depth, alpha, beta, player)

If node is a terminal node or depth==0

Return the evaluation function value

If player=player_max

For (a,s) in range game.successor

α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))

if β≤α

return α

If player=player_max

For (a,s) in range game.successor

α := min(α, alphabeta(child, depth-1, α, β, not(Player) ))

if β≤α

return β

11
action, state = argmax(game.successors(state),

lambda ((a, s)): min_value(s, -infinity, infinity, 0))

return action

The above alpha-beta function is just a attempt for me as I have not succeed it in my
practical design and this idea has referred from the game.py file for TicTacToe
game.

5. My achievements

5.1 The first level achievement

The pictures below have shown some situations happened in my first level
achievement which is person vs person.

1. 2

(Figure 5: initial state) (Figure 6: hound move state)

3.

(Figure 7: fox state)

4. 5.

12
(Figure 8: middle state) (Figure 9: ‘f’ wins)

5.2 The second level achievement

(Figure 10: gamestates generated by the computer automatically)

5.3 The third level achievements

13
(Figure 11: gamestates generated by the computer with an evaluation function )

The value of temp illustrates how the evaluation function works in my programming
design. The values reflected by computer in the screen indicate which hound should
be moved into another state while others remain the same positions.

Actually, the hounds are hard to win because I have not succeeded in applying the
minimax or alpha-beta pruning into my program. Thus, my artificial intelligence is not
so perfect for computer to have a smart analysis of the situations on the
checkerboard.

5.4 Amount of memory consumed

As for the first level, the 20% of total memory will be occupied when the
programming sentence is being executed, while the 35% of total memory will be
utilized for the second level of programming.

Compare to the two above, the third level will consume more memory storage to
40% mainly because an evaluation function has been operated every time, which
has increased the amount of calculating memory.

6. Conclusion

The fox and hounds game has been successfully designed by me in the basic three
levels. The two people could play with each other as my first design stage. Secondly,
one person could play as fox to compete with the computer as hounds in a random
way. Thirdly, one person could play as fox to compete with the computer playing as
hounds automatically with the support of the evaluation function that I have

14
designed. However, it is still a low level artificial intelligence since the minimax or
alpha-beta algorithm has not been succeed being applied onto my third level design.

Although it is not a perfect design of artificial intelligence in my game, I still


commanded the principle of how the search algorithms works to increase the
capability of artificial intelligence for computer, which will possessed more abilities
than human being analysis. Additionally, I was interested in the development of
artificial intelligence during the study of this course. The only flaw that I encountered
when programming the python was not one-hundred percentages familiar with the
syntax of python especially when I was attempting to express some complex relation
between the functions.

All in all, with the deep considerations of the design of fox and hounds game, it is
convinced that the concept of the artificial intelligence has been deep rooted in my
thought as I will consider to try to develop my programming skills introduced by the
artificial intelligence.

7. Reference

[1] Artificial intelligence lecture notes, Dr Neil Cooke, 2010-12-17.

[2] The quick python book, Vernon L.Ceder, 2010, 2 nd edition, manning.

[3] Artificial intelligence--a system Approach, Time Jones, 2008, Infinity Science
Press.

[4] Artificial intelligence-a modern approach, Russel and Norving, 2002.

15

Вам также может понравиться