OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++...

59
OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    4

Transcript of OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++...

Page 1: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library

Page 2: 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

Page 3: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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)

Page 4: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Types of Game

deterministic Stochastic

Perfect information Chess, checkers, connect-4, go, othello

Backgammon, monopoly

Imperfect information

Bridge, poker, scrabble

Page 5: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

MiniMax 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

3 2

3 12 8

5

5 7 9 4 2 7

Max:

Min:

A11

A2

A3

A13A12

A21 A23A22

A31 A33A32

A1

Page 6: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

MiniMaxFunction 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)

Page 7: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

MiniMax Properties Complete: yes, if search tree is finite Optimal : yes, if opponent plays optimal Time complexity : O(bm) 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

Page 8: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Evaluation Functions For chess for example typically linear weighted

sum of features Eval(s) = w1 f1(s) + w2 f2(s) + …wn fn(s)

w1=9

f1(s)= #white queens - #black queens

w2=5

f2(s) = #white rooks - #black rooks

etc.

Page 9: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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

Page 10: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Pruning Example

3

3

3 12 8

Max:

Min:

A11

A2

A3

A13A12

A1

2

2

A21A22

A23

? ?

5

5

A21A22

7

2

A23

2

Page 11: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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

Page 12: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Containers, Iterators, Algorithms

Container

AlgorithmIterator

Container

Iterator

Algorithm

Objects

Iterator

Iterator

Algorithm

Algorithms use iterators to interact with objectsstored in containers

Page 13: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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

<vector> : one-dimensional array <list> : double linked list <deque> : double-ended queue <queue> : queue <stack> : stack <set> : set <map> : associative array

Page 14: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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,

<vector>, <list> and <deque> are sequential containers In an ordinary C++ array the size is fixed and can not change during run-time, it is also tedious to insert or delete elements. Advantage: quick

random access <vector> is an expandable array that can shrink or grow in size, but still has the disadvantage that inserting or deleting elements in the middle is

costly as it requires to copy chunks of memory

Page 15: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Sequence Containers <list> 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) <deque> is a double-ended queue, that means one can insert and delete elements from both ends, it is a kind of combination between a stack (last in first out) and a queue (first in first out) and

constitutes a compromise between a <vector> and a <list>

Page 16: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

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.

Page 17: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Associative Containers A <set> stores a number of items which contain keys The keys are the attributes used to order the items, for example a set might store objects of the class Person which are ordered alphabetically using their

name A <map> stores pairs of objects: a key object and an associated value object. A <map> is somehow similar to an array except instead of accessing its elements with index numbers, you access them with indices of an arbitrary type. <set> and <map> only allow one key of each value, whereas <multiset> and <multimap> allow multiple identical key values

Page 18: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Vector Container

12 7 9 21 13

int array[5] = {12, 7, 9, 21, 13 };vector<int> v(array,array+5);

v.begin();

12 7 9 2113

v.push_back(15);

12 7 9 21…

15

12 7 9 21 15

v[3]

0 1 2 3 4

v.pop_back();

Page 19: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Vector Container#include <vector>

#include <iostream>

vector<int> v(3); // create a vector of ints of size 3

v[0]=23;

v[1]=12;

v[2]=9; // vector full

v.push_back(17); // put a new value at the end of array

for (int i=0; i<v.size(); i++) // member function size() of vector

cout << v[i] << ” ”; // random access to i-th element

cout << endl;

Page 20: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Constructors for Vector

A vector can be initialized by specifying its size and a prototype element or by another vector

vector<Date> x(1000); // creates vector of size 1000,

// requires default constructor for Date

vector<Date> dates(10,Date(17,12,1999)); // initializes

// all elements with 17.12.1999

vector<Date> y(x); // initializes vector y with vector x

Page 21: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

vector<int>

array_

Iterators 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 iterating through a container.

17

4

23

12

size_ 4

vector<int>::iterator

The iterator corresponding tothe class vector<int> is ofthe type vector<int>::iterator

Page 22: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Iterators The container member functions begin() and

end() return an iterator to the first and past the last element of a container

vector<int> v

array_ 17

4

23

12

size_ 4

v.end()

v.begin()

Page 23: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Iterators One can have multiple iterators pointing to

different or identical elements in the container

vector<int> v

array_ 17

4

23

12

size_ 4i3

i1

i2

Page 24: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Iterators#include <vector>

#include <iostream>

vector<int> v; // initialize empty vector

v.push_back(13);

v.push_back(9);

v.push_back(8);

vector<int>::iterator iter=v.begin(); // iterator for class vector

// define iterator for vector and point it to first element of v

cout << ”first element of v=” << *iter; // de-reference iter

iter++; // move iterator to next element

iter=v.end()-1; // move iterator to last element

Page 25: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Iteratorsint max(vector<int>::iterator start, vector<int>::iterator end)

{

int tmpmax=*start;

while(start != stop)

{

if (*start > tmpmax)

tmpmax=*start;

++start;

}

return tmpmax;

}

cout << ”max of v = ” << max(v.begin(),v.end());

Page 26: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Iterator Categories 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 bidirectionalrandomaccess

Page 27: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

For_Each() Algorithm#include <vector>

#include <algorithm>

#include <iostream>

void show(int n)

{

cout << n << ” ”;

}

int arr[] = { 12, 3, 17, 8 }; // standard C array

vector<int> v(arr, arr+4); // initialize vector with C array

for_each (v.begin(), v.end(), show); // apply function show

// to each element of vector v

Page 28: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Find() Algorithm#include <vector>

#include <algorithm>

#include <iostream>

int key;

int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array

vector<int> v(arr, arr+7); // initialize vector with C array

vector<int>::iterator iter;

cout << ”enter value :”;

cin >> key;

iter=find(v.begin(),v.end(),key); // finds integer key in v

if (iter != v.end()) // found the element

cout << ”Element ” << key << ” found” << endl;

else

cout << ”Element ” << key << ” not in vector v” << endl;

Page 29: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Find_If() Algorithm#include <vector>

#include <algorithm>

#include <iostream>

Bool mytest(int n) { return (n>21) && (n <36); };

int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array

vector<int> v(arr, arr+7); // initialize vector with C array

vector<int>::iterator iter;

iter=find_if(v.begin(),v.end(),mytest);

// finds element in v for which mytest is true

if (iter != v.end()) // found the element

cout << ”found ” << *iter << endl;

else

cout << ”not found” << endl;

Page 30: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Count_If() Algorithm#include <vector>

#include <algorithm>

#include <iostream>

Bool mytest(int n) { return (n>14) && (n <36); };

int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array

vector<int> v(arr, arr+7); // initialize vector with C array

int n=count_if(v.begin(),v.end(),mytest);

// counts element in v for which mytest is true

cout << ”found ” << n << ” elements” << endl;

Page 31: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Linked List 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.

nextdata

Element Element Element

nextdata

nextdata

Element Element Elementnextprevdata

nextprevdata

nextprevdata

Page 32: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

List Container 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

Page 33: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

List Container12 7 9 21 13

int array[5] = {12, 7, 9, 21, 13 };list<int> li(array,array+5);

7 9 21

12 li.push_front(8);

12 7 9 21

15

li.pop_front();

12 7 9 2113

li.push_back(15);

12 7 9 21…

15

li.pop_back();

8

7 12 17 21 23

li.insert()19

Page 34: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Sort & Merge Sort and merge allow you to sort and merge

elements in a container#include <list>

int arr1[]= { 6, 4, 9, 1, 7 };

int arr2[]= { 4, 2, 1, 3, 8 };

list<int> l1(arr1, arr1+5); // initialize l1 with arr1

list<int> l2(arr2, arr2+5); // initialize l2 with arr2

l1.sort(); // l1 = {1, 4, 6, 7, 9}

l2.sort(); // l2= {1, 2, 3, 4, 8 }

l1.merge(l2); // merges l2 into l1

// l1 = { 1, 1, 2, 3, 4, 4, 6, 7, 8, 9}, l2= {}

Page 35: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Functions Objects 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 <list>

#include <functional>

int arr1[]= { 6, 4, 9, 1, 7 };

list<int> l1(arr1, arr1+5); // initialize l1 with arr1

l1.sort(greater<int>()); // uses function object greater<int>

// for sorting in reverse order l1 = { 9, 7, 6, 4, 1 }

Page 36: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Function Objects The accumulate algorithm accumulates data over

the elements of the containing, for example computing the sum of elements

#include <list>

#include <functional>

#include <numeric>

int arr1[]= { 6, 4, 9, 1, 7 };

list<int> l1(arr1, arr1+5); // initialize l1 with arr1

int sum = accumulate(l1.begin(), l1.end() , 0, plus<int>());

int sum = accumulate(l1.begin(), l1.end(),0); // equivalent

int fac = accumulate(l1.begin(), l1.end() , 0, times<int>());

Page 37: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

User Defined Function Objects

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

Page 38: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

User Defined Function Objects

template <class T>

class squared _sum // user-defined function object

{

public:

T operator()(T n1, T n2) { return n1+n2*n2; }

};

vector<complex> vc;

complex sum_vc;

vc.push_back(complex(2,3));

vc.push_back(complex(1,5));

vc.push_back(complex(-2,4));

sum_vc = accumulate(vc.begin(), vc.end() ,

complex(0,0) , squared_sum<complex>() );

// computes the sum of squares of a vector of complex numbers

Page 39: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Associative Containers 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 key which is usually a single value like a number or string

The value is an attribute of the objects in the container

The STL contains two basic associative containers sets and multisets maps and multimaps

Page 40: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Sets and Multisets#include <set>string names[] = {”Ole”, ”Hedvig”, ”Juan”, ”Lars”, ”Guido”}; set<string, less<string> > nameSet(names,names+5);// create a set of names in which elements are alphabetically// ordered string is the key and the object itselfnameSet.insert(”Patric”); // inserts more namesnameSet.insert(”Maria”);nameSet.erase(”Juan”); // removes an elementset<string, less<string> >::iterator iter; // set iteratorstring searchname; cin >> searchname;iter=nameSet.find(searchname); // find matching name in setif (iter == nameSet.end()) // check if iterator points to end of set cout << searchname << ” not in set!” <<endl;else cout << searchname << ” is in set!” <<endl;

Page 41: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Set and Multisets

string names[] = {”Ole”, ”Hedvig”, ”Juan”, ”Lars”, ”Guido”, ”Patric”, ”Maria”, ”Ann”};

set<string, less<string> > nameSet(names,names+7);

set<string, less<string> >::iterator iter; // set iterator

iter=nameSet.lower_bound(”K”);

// set iterator to lower start value ”K”

while (iter != nameSet.upper_bound(”Q”))

cout << *iter++ << endl;

// displays Lars, Maria, Ole, Patric

Page 42: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Maps and Multimaps

A map stores pairs <key, value> of a key object and associated value object.

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

Page 43: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Maps and Multimaps

#include <map>

string names[]= {”Ole”, ”Hedvig”, ”Juan”, ”Lars”, ”Guido”, ”Patric”, ”Maria”, ”Ann”};

int numbers[]= {75643, 83268, 97353, 87353, 19988, 76455, 77443,12221};

map<string, int, less<string> > phonebook;

map<string, int, less<string> >::iterator iter;

for (int j=0; j<8; j++)

phonebook[names[j]]=numbers[j]; // initialize map phonebook

for (iter = phonebook.begin(); iter !=phonebook.end(); iter++)

cout << (*iter).first << ” : ” << (*iter).second << endl;

cout << ”Lars phone number is ” << phonebook[”Lars”] << endl;

Page 44: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

Fill out the questionaire on the course webpage

Use the comment boxes for suggestions, complaints, negative and positive aspects of the course.

Page 45: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

Do you think the course in general was Easy Medium Difficult

What was most/least difficult Exam Seminars Labs Lectures

Page 46: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

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

Page 47: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

What do you think about the course literature Larman book?

Would you recommend the book to someone else?

Page 48: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis 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

Page 49: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

What do you think of the seminars? Useful Partially useful Not useful at all

Assistents Competent Partially Competent Incompetent

Page 50: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis 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

Page 51: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

What do you think about the lab hours Help by assistents

Good Acceptable Unacceptable

Availability, number of hours, waiting time Good Acceptable Unacceptable

Page 52: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

Did you feel that you got enough and competent help in general, help beside the labs, pointers to reading, hints, tips Good Acceptable Unacceptable

Page 53: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

What do you think about the lab assignments Difficulty

Easy Suitable Difficult

Programming tasks Interesting Partially interesting Uninteresting

Page 54: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

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

Page 55: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

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

Page 56: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis 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

Page 57: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

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

Page 58: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

Was it a problem for you that some of the lectures were taught in English? Yes Somewhat of a problem No

Page 59: OOMPA Lecture 17 Artificial intelligence and game playing Course evaluation & discussion C++ standard template library.

Course Analysis

What did you like best about the course? What did you like least about the course?