Transcript Document
OOMPA Lecture 17
Artificial intelligence and game playing Course evaluation & discussion C++ standard template library
Lab4
Design and implement a general game playing framework for deterministic two player zero-sum games Implement Min-Max-search Implement the games TicTacToe Connect-Four Extra point Implement alpha-beta pruning Implement checkers
Games as Search Problems
The behavior / actions of the opponent are unpredictable, therefore search for a “worst-case” plan.
Time limit, therefore complete search is not feasible and an approximation is needed Algorithm for perfect play (van Neumann 1944) Finite horizon, approximate evaluation (Zuse 1945, Shannon 1950, Samuel 1952) Pruning search tree (McCarthy 1956)
Types of Game
deterministic Stochastic Perfect information Chess, checkers, connect-4, go, othello Backgammon, monopoly Imperfect information Bridge, poker, scrabble
MiniMax
Max: Min: A 11 3 Optimal strategy for deterministic, perfect-information game Idea: Choose move that results in position with highest minmax-value = best achievable payoff against best opponents play 5 A 1 A 2 A 3 3 5 2 A 12 12 A 13 8 A 21 5 A 22 7 A 23 9 A 31 4 A 32 2 A 33 7
MiniMax
Function MINIMAX-DECISION(game) returns a move for each move in PossibleMoves[game] do value[move] <- MINIMAX-VALUE(apply(move,state),game) end return the move with the highest value[move] Function MINIMAX-VALUE(state, game) returns a utility value if TERMINAL-TEST[game](state) then return UTILITY[game](state) else if MAX is to move in state return the highest MINIMAX-VALUE of SUCCESSORS(state) else return the lowest MINIMAX-VALUE of SUCCESSORS(state)
MiniMax Properties
Complete: yes, if search tree is finite Optimal : yes, if opponent plays optimal Time complexity : O(b m ) Space complexity : O(bm) depth first search Chess b~35 possible moves in each state, m~100 moves per game -> exact solution infeasible Standard solution cutoff test for search (e.g. depth limit) Evaluation function : approximates utility of board position
Evaluation Functions
For chess for example typically linear weighted sum of features Eval(s) = w 1 w 1 =9 f 1 (s) + w 2 f 2 (s) + …w n f n (s) f 1 (s)= #white queens - #black queens w 2 =5 f 2 (s) = #white rooks - #black rooks etc.
Cutting of Search
MINIMAXCUTOFF is identical to MINIMAXVALUE except 1. TERMINAL? is replaced by CUTOFF?
2. UTILITY is replaced by EVAL Ply = one half-move (move by one player) Chess: 4-ply = novice 8-ply = PC, human master 12-ply = Deep Blue, Kasparov
Pruning Example
Max: Min: A 11 3 3 A 12 12 A 1 A 13 8 3 A 2 2 A 21 2 A 22 ?
A 23 ?
A 3 2 5 A 21 5 A 22 7 A 23 2
Standard Template Library
The standard template library (STL) contains Containers Algorithms Iterators A
container
is a way that stored data is organized in memory, for example an array of elements.
Algorithms
in the STL are procedures that are applied to containers to process their data, for example search for an element in an array, or sort an array.
Iterators
are a generalization of the concept of pointers, they point to elements in a container, for example you can increment an iterator to point to the next element in an array
Containers, Iterators, Algorithms
Algorithms use iterators to interact with objects stored in containers Objects Container Iterator Container Iterator Algorithm Iterator Algorithm Iterator Algorithm
Containers
A container is a way to store data, either built-in data types like int and float, or class objects The STL provides several basic kinds of containers : double linked list
Sequence Containers
A sequence container stores a set of elements in sequence, in other words each element (except for the first and last one) is preceded by one specific element and followed by another, and
Sequence Containers
is a double linked list (each element has points to its successor and predecessor), it is quick to insert or delete elements but provides no random access (e.g. return 5th element in list)
Associative Containers
An associative container is non-sequential but uses a
key
to access elements. The keys, typically a number or a string, are used by the container to arrange the stored elements in a specific order, for example in a dictionary the entries are ordered alphabetically.
Associative Containers
A
Vector Container
int array[5] = {12, 7, 9, 21, 13 }; vector
Vector Container
#include A vector can be initialized by specifying its size and a prototype element or by another vector vector vector Iterators are pointer-like entities that are used to access individual elements in a container. Often they are used to move sequentially from element to element, a process called through a container. vector The container member functions begin() and end() return an iterator to the first and past the last element of a container vector One can have multiple iterators pointing to different or identical elements in the container vector #include { int max(vector Not every iterator can be used with every container for example the list class provides no random access iterator Every algorithm requires an iterator with a certain level of capability for example to use the [] operator you need a random access iterator Iterators are divided into five categories in which a higher (more specific) category always subsumes a lower (more general) category, e.g. An algorithm that accepts a forward iterator will also work with a bidirectional iterator and a random access iterator input output forward bidirectional random access #include #include #include #include A linked list is composed of a chain of elements (links). Each element contains some data and a pointer to the next element in the list. In a double linked list, each element also contains a pointer to its predecessor. Element Element Element next data next data next data Element next prev data Element next prev data Element next prev data A list container is a double linked list, in which each element contains a pointer to its successor and predecessor. It is possible to insert and remove elements at arbitrary location in the list, without having to copy large chunks of memory as with vectors Lists do not allow random access but are efficient to insert new elements and to sort and merge lists int array[5] = {12, 7, 9, 21, 13 }; list Sort and merge allow you to sort and merge elements in a container #include Some algorithms like sort, merge, accumulate can take a function object as argument. A function object is an object of a template class that has a single member function : the overloaded operator () It is also possible to use user-defined functions instead of pre-defined function objects #include The accumulate algorithm accumulates data over the elements of the containing, for example computing the sum of elements #include { class squared _sum // user-defined function object public: int operator()(int n1, int n2) { return n1+n2*n2; } }; int sq = accumulate(l1.begin(), l1.end() , 0, squared_sum() ); // computes the sum of squares { template In an associative container the items are not arranged in sequence, but usually as a tree structure or a hash table. The main advantage of associative containers is the speed of searching (binary search like in a dictionary) Searching is done using a which is usually a single value like a number or string The container is an attribute of the objects in the The STL contains two basic associative containers sets and multisets maps and multimaps #include string names[] = {”Ole”, ”Hedvig”, ”Juan”, ”Lars”, ”Guido”, ”Patric”, ”Maria”, ”Ann”}; set A map stores pairs The key object contains a key that will be searched for and the value object contains additional data The key could be a string, for example the name of a person and the value could be a number, for example the telephone number of a person #include Fill out the questionaire on the course webpage Use the comment boxes for suggestions, complaints, negative and positive aspects of the course. Do you think the course in general was Easy Medium Difficult What was most/least difficult Exam Seminars Labs Lectures Do you think the course was interesting and useful for you? Yes Partially useful No Do you think your previous knowledge (e.g. programming experience in JAVA) was sufficient for this course? Yes Somewhat No What do you think about the course literature Larman book? Would you recommend the book to someone else? What do you think about the lectures? Pedagogics Presentation Lecture notes, references Which topics did you find most/least interesting OO analysis and design Object oriented programming Extreme programming Design patterns UML C++ Smalltalk What do you think of the seminars? Useful Partially useful Not useful at all Assistents Competent Partially Competent Incompetent What do you think about the style of seminars? Presentation by students or rather assistant More or fewer discussions More or fewer practical exercises Did you learn most on OOA/D by Attending the lectures Attending the seminars Reading the book Doing the labs What do you think about the lab hours Help by assistents Good Acceptable Unacceptable Availability, number of hours, waiting time Good Acceptable Unacceptable Did you feel that you got enough and competent help in general, help beside the labs, pointers to reading, hints, tips Good Acceptable Unacceptable What do you think about the lab assignments Difficulty Easy Suitable Difficult Programming tasks Interesting Partially interesting Uninteresting Which lab assignment did you like best/least ? Lab 1 class hierarchy (graphics) Lab 2 design patterns Lab 3 bank (CORBA) Lab 4 game playing C++ Lab 5 Smalltalk How much time did you spend on the labs in total? Less than 60 hours 60-120 hours More than 120 hours What do you think about the lab redovisning? Fair Mostly fair Unfair What do you think about the exam? Difficulty Comprehensibility Prior information about the exam (test exam) Would you like to see more/less Practical assignments (drawing UML diagrams) Multiple choice questions Verbal questions What is the percentage of your study time this semester that you spend on this course? Less than 25% 25-50% More than 50% Do you think that 6 points for the course are not enough enough too many Was it a problem for you that some of the lectures were taught in English? Yes Somewhat of a problem No What did you like best about the course? What did you like least about the course?Constructors for Vector
Iterators
iterating
Iterators
Iterators
Iterators
Iterators
Iterator Categories
For_Each() Algorithm
Find() Algorithm
Find_If() Algorithm
Count_If() Algorithm
Linked List
List Container
List Container
Sort & Merge
int arr1[]= { 6, 4, 9, 1, 7 }; int arr2[]= { 4, 2, 1, 3, 8 }; list
Functions Objects
#include
Function Objects
#include
User Defined Function Objects
User Defined Function Objects
Associative Containers
key
value
Sets and Multisets
Set and Multisets
Maps and Multimaps
Maps and Multimaps
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis
Course Analysis