Академический Документы
Профессиональный Документы
Культура Документы
EE3J1
Artificial intelligence
Coursework Assignment
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.
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
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:
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.
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:
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.
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
Game instance
from class Fox
andHound
Function
game_play
5
(Figure 2: main function flow chart)
Start
Game.initial
Game.display
TermTest==0
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)
1 2.
State Move
State
LegalMoves
Make_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
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.
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.
player = game.to_move(state)
def max_value(state):
if game.terminal_test(state):
return 1
v = -infinity
v = max(v, min_value(s))
def min_value(state):
if game.terminal_test(state):
return -1
v = infinity
v = min(v, max_value(s))
return v
9
# Body of minimax_decision starts here:
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.
player = game.to_move(state)
def max_value(state):
if Depth==0:
v = -infinity
v = max(v, min_value(s))
def min_value(state):
if Depth==0:
v = infinity
v = min(v, max_value(s))
return v
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.
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.
If player=player_max
if β≤α
return α
If player=player_max
if β≤α
return β
11
action, state = argmax(game.successors(state),
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
The pictures below have shown some situations happened in my first level
achievement which is person vs person.
1. 2
3.
4. 5.
12
(Figure 8: middle state) (Figure 9: ‘f’ wins)
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.
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.
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
[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.
15