Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:dydaktyka:ggp:gdl [2019/01/03 22:50]
msl reverts <code prolog> to <code> because of the environment bug
en:dydaktyka:ggp:gdl [2019/06/27 15:49]
Line 1: Line 1:
-====== - Modeling Games in GDL  ====== 
- 
-The main goal of this class is to learn how to model turn games in a rule based language. After finishing the class, student should be able to model simple games and point out the difficulties related with this task. 
- 
-===== - Preliminaries ===== 
- 
-GDL (Game Description Language) is a language designed to describe turn games. It was created to be used in so called General Game Playing, a subdomain of the AI, focused on creating bots able to play in any game that can be formally described. 
- 
-The class is based on [[http://​logic.stanford.edu/​ggp/​chapters/​chapter_02.html|a tutorial from Stanford]] and [[http://​www.inf.tu-dresden.de/​content/​institutes/​ki/​cl/​study/​winter09/​ggp/​kapitel2.pdf|slides from the university in Dresden]]. 
- 
-===== - Modeling the World ===== 
- 
-There are two main approaches to model the world of the game: 
-  - coarse-grained --- where every state of the game is a state in a giant state machine. Every unique state corresponds to one state in the machine. The state machine may grow quite impressive. Beside that state doesn'​t have any structure so is no really helpful if you want to exploit it to solve the problem in an intelligent manner. 
-  - fine-grained --- state of the world is defined by so called [[https://​en.wikipedia.org/​wiki/​Fluent_(artificial_intelligence)|fluents]],​ which can be treated as variables. The state here is structured (the current values of the fluents) which we may use. 
- 
-GDL belongs to the fine-grained family, so there are: 
-  * **fluents** (actions made by the players are a special kind of fluent 
-  * game rules, which are represented by... rules  
- 
-===== - Example: Tic-Tac-Toe ===== 
- 
-We will show the basic features of the GDL on the example of the popular [[http://​playtictactoe.org/​|tic-tac-toe game]]. ​ 
- 
-==== Players ==== 
- 
-Let's start with players "​nought"​ and "​cross"​. There is a special fact ''​role''​ just to do that: 
- 
-<​code>​ 
-role(nought) 
-role(cross) 
-</​code>​ 
- 
-Syntax is the same as in the Prolog (or Datalog) language with the only difference, that there is no dot at the end of the sentence. All constants start with a lower-cased letter and variables are upper-cased. 
- 
-==== Fluents ==== 
- 
-Now we should define fluents that make up the game state. In a Tic-Tac-Toe game there are 29 fleunts: 
-  * 1 fluent per every cell, telling if there is a cross (''​x''​) in the cell (9 fluents) 
-  * 1 fluent per every cell, telling if there is a nought (''​o''​) in the cell (9 fluents) 
-  * 1 fluent per every cell, telling if the cell is empty (blank - ''​b''​) (9 fluents) 
-  * 1 fluent per player, telling if it is his turn to play (2 fluents). 
- 
-To define the fluents one uses the ''​base''​ facts: 
- 
-<​code>​ 
-base(cell(M,​N,​x)) :- index(M) & index(N) 
-base(cell(M,​N,​o)) :- index(M) & index(N) 
-base(cell(M,​N,​b)) :- index(M) & index(N) 
- 
-base(control(R)) :- role(R) 
- 
-index(1) 
-index(2) 
-index(3) 
-</​code>​ 
- 
-Here '':​-''​ reprenst implication ''<​-''​. ''&''​ is a logical and. In natural language the first line means: "if M and N represent cells' indexes, then there is a fluent for cell with (M, N) coordinates,​ telling there is a cross in it". If we didn't use rules, we could write all the fluents by hand: 
- 
-<​code>​ 
-base(cell(1,​1,​x)) ​   base(cell(1,​1,​o)) ​   base(cell(1,​1,​b)) 
-base(cell(1,​2,​x)) ​   base(cell(1,​2,​o)) ​   base(cell(1,​2,​b)) 
-base(cell(1,​3,​x)) ​   base(cell(1,​3,​o)) ​   base(cell(1,​3,​b)) 
-base(cell(2,​1,​x)) ​   base(cell(2,​1,​o)) ​   base(cell(2,​1,​b)) 
-base(cell(2,​2,​x)) ​   base(cell(2,​2,​o)) ​   base(cell(2,​2,​b)) 
-base(cell(2,​3,​x)) ​   base(cell(2,​3,​o)) ​   base(cell(2,​3,​b)) 
-base(cell(3,​1,​x)) ​   base(cell(3,​1,​o)) ​   base(cell(3,​1,​b)) 
-base(cell(3,​2,​x)) ​   base(cell(3,​2,​o)) ​   base(cell(3,​2,​b)) 
-base(cell(3,​3,​x)) ​   base(cell(3,​3,​o)) ​   base(cell(3,​3,​b)) 
-base(control(nought)) 
-base(control(cross)) 
-</​code>​ 
- 
- 
-Rules in GDL have a one special feature: they can recurse, but they can't loop forever. ​ [[http://​www.inf.tu-dresden.de/​content/​institutes/​ki/​cl/​study/​winter09/​ggp/​kapitel2.pdf|More details here]]. 
- 
-==== Available Actions ==== 
- 
-Now, we should model available actions, there are 20 of them here: 
- 
-  * 1 action per every cell, that the player puts the '​nought'​ in  
-  * 1 action per every cell, that the player puts the '​cross'​ in  
-  * 1 action ''​noop''​ per user, telling that player does do nothing 
- 
-Thee ''​noop''​ action exists only in order to simulate the players'​ turns. When one player makes the action then the second one has to take the ''​noop''​ action. 
- 
-In order to define an action we use the ''​input''​ predicate: 
- 
-<​code>​ 
-input(R, mark(M,N)) :- role(R) & index(M) & index(N) 
-input(R, noop) :- role(R) 
-</​code>​ 
- 
-The ''​input''​ predicates takes two arguments, first is the player, second is the action itself. 
- 
-==== Legal Moves ==== 
- 
-Knowing the available actions, we have to define when the action is a legal move with a ''​legal''​ predicate. We use the ''​true''​ predicate to check if the fluent is true at the moment.. 
- 
-<​code>​ 
-legal(W,​mark(X,​Y)) :- 
-   ​true(cell(X,​Y,​b)) & 
-   ​true(control(W)) 
- 
-legal(nought,​noop) :- 
-   ​true(control(cross)) 
- 
-legal(cross,​noop) :- 
-   ​true(control(nought)) 
-</​code>​ 
- 
-In other words - player may mark a cell only if it is empty and it is his turn. If it is not his turn, he can only perform the '​noop'​ action. 
- 
-==== Initial State ==== 
- 
-Now we have to define the initial state of the game (draw the empty board). The ''​init''​ predicate serves this purpose. For the tic-tac-toe every cell should be empty, and the ''​nought''​ player should play first. 
- 
-<​code>​ 
-init(cell(M,​N,​b)) :- index(M) & index(N) 
-init(control(nought)) 
-</​code>​ 
- 
-==== State Changes ==== 
-Now the most difficult part --- how does the state evolve in the game. We assume that the new state is created after the very move and depends only on the previous state and actions made by the players. On could notice the [[https://​en.wikipedia.org/​wiki/​Frame_problem#​Fluent_calculus_solution|frame problem occurring here]]. The etymology of the ''​frame''​ is the movie tape, where every frame differs in details from the previous one. Similarly here, we would like the next state to be exactly like the previous one except some changes made //​explicitly//​ by the players. The problem is that in our case we have also to model lack of changes :(  
- 
-To define the state evolution we use the ''​next''​ predicate, defining the fluent state in the next turn. The '​does'​ predicate check what actions were made by the players during the current turn. 
- 
-<​code>​ 
-next(cell(M,​N,​x)) :- 
-  does(cross,​mark(M,​N)) & 
-  true(cell(M,​N,​b)) 
- 
-next(cell(M,​N,​o)) :- 
-  does(nought,​mark(M,​N)) & 
-  true(cell(M,​N,​b)) 
- 
-next(cell(M,​N,​W)) :- 
-  true(cell(M,​N,​W)) & 
-  distinct(W,​b) 
- 
-next(cell(M,​N,​b)) :- 
-  does(W,​mark(J,​K)) & 
-  true(cell(M,​N,​b)) & 
-  distinct(M,​J) 
- 
-next(cell(M,​N,​b)) :- 
-  does(W,​mark(J,​K)) 
-  true(cell(M,​N,​b)) & 
-  distinct(N,​K) 
- 
-next(control(nought)) :- 
-  true(control(cross)) 
- 
-next(control(cross)) :- 
-  true(control(nought)) 
-</​code>​ 
- 
-The defined actions mean that: 
-  - if the cell is empty and the '​cross'​ player marked it then there will be a '​x'​ in this cell next turn  
-  - if the cell is empty and the '​nought'​ player marked it then there will be an '​o'​ in this cell next turn 
-  - if the cell wasn't empty (''​distinct''​ checks equality of the arguments), then it will stay the same next turn 
-  - if the cell was empty and wasn't mark this turn, it will be still empty next turn (two rules!) 
-  - if the current turn belongs to the the '​nought'​ player, next will belong to the '​cross'​ player 
-  - if the current turn belongs to the the '​cross'​ player, next will belong to the '​nought'​ player 
- 
-It's worth to note, that actions 3 and 4 are designed only to cope with the frame problem. 
-==== Game Ending Conditions ==== 
- 
-Every player knows that there are two ways to finish the game: make a line or fill all the available cells. Now we will check if there was a line made by the player. 
- 
-<​code>​ 
-line(Z) :- row(M,Z) 
-line(Z) :- column(M,Z) 
-line(Z) :- diagonal(Z) 
- 
-row(M,Z) :- 
-  true(cell(M,​1,​Z)) & 
-  true(cell(M,​2,​Z)) & 
-  true(cell(M,​3,​Z)) 
- 
-column(N,Z) :- 
-  true(cell(1,​N,​Z)) & 
-  true(cell(2,​N,​Z)) & 
-  true(cell(3,​N,​Z)) 
- 
-diagonal(Z) :- 
-  true(cell(1,​1,​Z)) & 
-  true(cell(2,​2,​Z)) & 
-  true(cell(3,​3,​Z)) ​ 
- 
-diagonal(Z) :- 
-  true(cell(1,​3,​Z)) & 
-  true(cell(2,​2,​Z)) & 
-  true(cell(3,​1,​Z)) ​ 
-</​code>​ 
- 
-The ''​true(line(naught))''​ query will tell if there is a line made of the '​o'​s on the board. 
- 
-Now we can define the ending conditions with the ''​terminal''​ predicate: 
- 
-<​code>​ 
-terminal :- line(x) 
-terminal :- line(o) 
-terminal :- ~open 
- 
-open :- true(cell(M,​N,​b)) 
-</​code>​ 
- 
-Game ends when: 
-  * there is a line of crosses 
-  * there is a line of naughts 
-  * there is no empty cell 
- 
-The '​~'​ operator means negation. Similarly to the Prolog it follows [[https://​en.wikipedia.org/​wiki/​Closed-world_assumption|the closed world assumption]],​ ie. sentence is false if can't prove otherwise. In GDL ther are additional ​ restrictions we want worry about for now (more info [[http://​www.inf.tu-dresden.de/​content/​institutes/​ki/​cl/​study/​winter09/​ggp/​kapitel2.pdf|here]]). 
- 
-==== Winning Conditions ==== 
- 
-{{ :​en:​dydaktyka:​ggp:​somuchwin.jpg?​direct&​400|}} 
-Bots don't have feelings but we still should give them prize for the good game. And we do this with the ''​goal''​ predicate: 
- 
-<​code>​ 
-goal(cross,​100) :- line(x) & ~line(o) 
-goal(cross,​50) :- ~line(x) & ~line(o) 
-goal(cross,​0) :- ~line(x) & line(o) 
- 
-goal(nought,​100) :- ~line(x) & line(o) 
-goal(nought,​50) :- ~line(x) & ~line(o) 
-goal(nought,​0) :- line(x) & ~line(o) 
-</​code>​ 
- 
-According to the rules above winner gets 100 (so much win!), in case of tie only 50, and loser gets 0. We don't what this number represent but it has to be something good like cookies. 
- 
-<WRAP center round tip 60%> 
-There is no arithmetics in GLD --- numbers are only used to define indexes, prizes, time, etc. 
-</​WRAP>​ 
- 
-==== End ==== 
- 
-That's all folks, you've just modeled a simple tic-tac-toe game. 
- 
-=== - Assignment 1 === 
- 
-Let's take an even simpler game: 
- 
-<​code>​ 
- 
-role(white) 
-role(black) 
- 
-base(p) 
-base(q) 
-base(r) 
-base(s) 
- 
-input(R,a) :- role(R) 
-input(R,b) :- role(R) 
-input(R,c) :- role(R) 
-input(R,d) :- role(R) 
- 
-init(s) 
- 
-legal(white,​a) 
-legal(white,​b) 
-legal(white,​c) 
-legal(black,​d) 
-      ​ 
-next(p) :- does(white,​a) & ~true(p) 
-next(p) :- ~does(white,​a) & true(p) 
-next(q) :- does(white,​b) & true(p) 
-next(q) :- does(white,​c) & true(r) 
-next(q) :- ~does(white,​b) & ~does(white,​c) & true(q) 
-next(r) :- does(white,​c) & true(q) 
-next(r) :- ~does(white,​c) & true(r) 
- 
-goal(white,​100) :- terminal 
-goal(white,​0) :- ~terminal 
-goal(black,​100) :- terminal 
-goal(black,​0) :- ~terminal 
- 
-terminal :- true(p) & true(q) & true(r) 
- 
-</​code> ​ 
- 
-Answer the following questions: 
-  - how many players are there? <fs xx-small>​ (correct answer: 2, why?​)</​fs>​ 
-  - how many fluents are defined? ​ <fs xx-small>​(correct answer: 4, why?​)</​fs> ​ 
-  - how many actions are there? ​ <fs xx-small>​(correct answer: 8, why?​)</​fs>​ 
-  - how many legal moves does the '​white'​ player have in the initial state of the game?  <fs xx-small>​(correct answer: 3, why?​)</​fs>​ 
-  - how many fluents are true in the initial state of the game?  <fs xx-small>​(correct answer: 1, why?​)</​fs>​ 
-  - assume in the first turn, the white player performed the '​a'​ action and the '​black'​ player action '​d'​. How many fluents are true in the second turn?  <fs xx-small>​(correct answer: 1, why?​)</​fs>​ 
-  - what is the smallest number of turns required to end the game?  <fs xx-small>​(correct answer: 4, why?​)</​fs>​ 
-  - does the game always end? (is it possible to loop the game indefinitely?​) ​ 
- 
-=== - Assignment 2 === 
- 
-Please model the '​crosses and crosses'​ game. The rules are similar to the tic-tac-toe,​ again we have nine cells, but both players use the crosses. Player loses when he first creates the line. 
- 
-=== - Assignment 3 === 
- 
-{{ :​pl:​dydaktyka:​ggp:​rock-paper-spock.jpg?​300|}} 
-Please model the "Rock Paper Scissors Lizard Spock" game. You can learn the rules from this [[https://​www.youtube.com/​watch?​v=iapcKVn7DdY|video]]. The image on the right is also a good reference. You can practice it with a colleague or like a modern man: [[http://​www.playmycode.com/​play/​game/​cainy393/​rock-paper-scissors-lizard-spock|online...]]. 
- 
-===== - Knowledge Interchange Format ===== 
- 
-Everything we learned so far is true, but the GDL has an alternative syntax used to store and transfer the games: the Knowledge Interchange Format. It's based on a lisp and uses a prefix notation. Moreover '':​-''​ operator is replaced with ''<​='',​ ''&''​ with ''​and'',​ negation ''​~''​ with ''​not''​. The variables start with the ''?''​. Below you can see an example of translation between Prolog: 
- 
-<​code>​ 
-p(a,​Y) ​                 ​ 
-~p(a,​Y) ​                 
-p(a,Y) & p(Y,​c) ​         
-q(Y) :- p(a,Y) & p(Y,​c) ​ 
-q(Y) :- p(a,Y) & p(Y,​c) ​ 
-</​code>​ 
- 
-and KIF: 
- 
-<code lisp> 
-(p a ?y) 
-(not (p a ?y)) 
-(and (p a ?y) (p ?y c)) 
-(<= (q ?y) (and (p a ?y) (p ?y c))) 
-(<= (q ?y) (p a ?y) (p ?y c)) 
-</​code>​ 
- 
-=== - Assignemnt 1 === 
- 
-For every example below, tell if the KIF version is a faithful translation of the Prolog one. 
- 
-<code lisp> 
-r(a,b) :- p(a) & q(b) 
-(<= (r a b) (and (p a) (q b))) 
-</​code>​ 
-<code lisp> ​ 
-r(a,b) :- p(a) & q(b) 
-(<= (r a b) (p a) (q b)) 
-</​code>​ 
-<code lisp> 
-r(x,y) :- p(x) & q(y) 
-(<= (r ?x ?y) (p ?x) (q ?y)) 
-</​code>​ 
-<code lisp> ​ 
-r(X,Y) :- p(X) & q(Y) 
-(<= (r ?x ?y) (p ?x) (q ?y)) 
-</​code>​ 
- 
-=== - Assignment 2 === 
- 
-Download {{:​pl:​dydaktyka:​ggp:​tictactoe.kif.zip|the KIF version of the tic-tac-toe game}}. ​ 
-What are the differences between it and our model? 
- 
-=== - Assignment 3  === 
-Read the {{:​pl:​dydaktyka:​ggp:​blocks.kif.zip|simple blocks'​ world model}}. 
-  * what are the ending contitions? 
-  * how do we count the turns? 
- 
-=== - Assignment 4 === 
- 
-Translate all the models you've created today to the KIF format. 
- 
-===== - Validation ===== 
- 
-<WRAP center round tip 60%> 
-The instructions below require an Eclipse IDE. 
-</​WRAP>​ 
- 
-Please run the Eclipse IDE and: 
-  * ''​File -> Import Project''​ 
-  * Import the git repository 
-    * If there is no such option (Eclipse is too old), one has to download the repository by hand and ignore the further instructions. 
-  * Clone from the remote URI 
-  * Fill URI: ''​https://​github.com/​ggp-org/​ggp-base.git''​ 
-  * Select the ''​master''​ branch 
-  * Select a //​reasonable//​ catalogue for the project 
-  * Select a ''​ggp-base''​ project to be imported 
- 
-==== - Adding Custom Games ==== 
- 
-In the project directory go to the ''​games/​games''​ catalogue and create there three directories ''​tic-tac-toe'',​ ''​cross-cross''​ and ''​spock''​. Every directory should contain two files: 
-  * ''<​name>​.kif''​ file with the model 
-  * ''​METADATA''​ file containing: 
- 
-<code javascript>​ 
-{ 
-  "​gameName":​ "<​game name>",  ​ 
-  "​rulesheet":​ "<​kif file name>"​ 
-} 
-</​code>​ 
- 
-==== - Games' Validation ==== 
- 
-From the Eclipse run the ''​Validator''​ app (one of the targets next to the ''​play''​ button). 
- 
-''​Validator''​ will check if the model is a valid GDL model. ​ 
- 
-=== Assignment === 
- 
-  - In the ''​Repository''​ field choose ''​Local Game Repository''​ 
-  - In the ''​Game''​ field select ''​Tic-Tac-Toe''​ 
-  - Press ''​Validate''​ --- There should be no failure 
-  - Repeat the same for your custom models 
-  - Fix your models to be playable 
- 
- 
- 
- 
-===== - Fun ===== 
- 
-Run the ''​Kiosk''​ app in the Eclipse (select from the list next to the ''​play''​ button). ​ 
- 
-=== Assignment === 
- 
-  - As an ''​Opponent''​ select ''​SimpleMonteCarloPlayer''​ 
-  - Choose a game you are familiar with  
-  - Win! 
- 
-{{ :​pl:​dydaktyka:​ggp:​have-fun.jpg?​400 |}} 
  
en/dydaktyka/ggp/gdl.txt · Last modified: 2019/06/27 15:49 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0