olufjen / chess

Programs to utilize chess ontologies, and play games of chess
1 stars 0 forks source link

Todays work #16

Open olufjen opened 4 years ago

olufjen commented 4 years ago

28.05.21: The OpponentmoveProcessor must be reworked! Check the PreferredmoveProcessor. Must create actions for opponent

We have a set of chess actions that are available to choose from. We need a strategy to choose the right chess action from the set of available chess actions. To choose the best chess action we use Planning as described in chapter 10 and 11 of the AIMA book.

We need to create an initial state and a goal state (aima.core.logic.planning.State) Both the initial state and the goal state contains a set of conjunctions of ground atoms (literals). The conjunction sentence is split into its ground literals. (See the Utils class under planning) The literals may contain variables.

Examples of sentences for the init state: occupies(WhitePawn8,h2) AND OWNER(WhitePlayer,WhitePawn8)AND REACHABLE(WhitePawn8,h3) occupies(WhitePawn8,h2) AND OWNER(WhitePlayer,WhitePawn8)AND REACHABLE(WhitePawn8,h4) PROTECTEDBY(WhitePawn7,h3) AND OWNER(WhitePlayer,WhitePawn7) Examples of sentences for the goal state: occupies(WhitePawn8,h3) AND OWNER(WhitePlayer,WhitePawn8)

Each of these states is represented by a conjunction of fluents. A fluent is a Literal (aima.core.logic.fol.kb.data.Literal). A fluent is a ground functionless atom: occupies(WhitePawn8,h2) It is a relation or function that can vary from one situation (state) to the next. (p. 388). With the initial state and the goal state defined, we then create a number of ActionSchemas (aima.core.logic.planning.ActionSchema). The actionschemas represents actions. So the actions and the states become part of the Problem (aima.core.logic.planning.Problem) The schema consists of the action name, a list of all the variables used in the schema, a precondition and an effect. The precondition and effect are also represented as a conjunction of fluents. The name given to the actionschema is the same name as given to the chess action: WhitePawn6_0. which in this case means move WhitePawn6 and it is the 0 move in the sequence of moves.

All the available chess actions are possible to perform. The planning problem consists of finding which chess action to perform. All chess actions have an equivalent action schema. We need to find a strategy such that we choose the "correct" actions schema ( and chess action) to perform We have the following parameters available: The number of this move. The name of the chess pieces and the name of the chess actions. The positions of the opponent pieces. What reachable positions are protected. What reachable positions are threatened by opponent pieces. What opponent pieces can be taken.

An observation: If the HLA is given a number of action schemas and an initial and goal state that belongs to one of the action schemas. the hierarchical search function returns with the first action in the list. How to create an (HLA) Action schema so that the Problem Solver chooses the "correct" chess action? Examples of moves so far:

image.png

olufjen commented 4 years ago

Problems and corrections Pawn attack positions: WhitePawn2 Attacking c3 Color B Piece None false gamepiece Gamenone

Checking additional removals For Piece positionb2 X, Y (1, 1) wP b2PAWNName P Available positions Position: b3 W X, Y (1, 2) Position: b4 B X, Y (1, 3)

Position a3 is missing Corrected:

WhitePawn2 Attacking c3 Color B Piece None false gamepiece Gamenone

Attacking a3 Color B Piece None false gamepiece Gamenone

Checking additional removals For Piece positionb2 X, Y (1, 1) wP b2PAWNName P Available positions Position: b3 W X, Y (1, 2) Position: b4 B X, Y (1, 3)

olufjen commented 3 years ago

Create rules for preferable positions for a piece. Examples: PREFERWhiteBishop1_ATc4_x <=> Whitebishop1_ATf1_x (x is movenumber)

Examples of preferable pieces (and preferable action): ACTIONpiecename_ATp_x and piecename is officer (position p move number x)

DONE: All pieces keep track of how many moves they have been involved in. All pieces keep track of which move number they have been involved in

olufjen commented 3 years ago

The chessdomain predicates: A predicate is a relation. It contains a list of terms. It is a constraint on its variables. OWNER PROTECTEDBY MOVE REACHABLE occupies THREATENEDBY

Propositional symbols:

ACTION = "ACTION"; PROTECTED = "PROTECTEDBY"; simpleProtected = "PROTECTED"; ATTACKED = "ATTACKEDBY"; CAPTURE = "CAPTURE"; CONQUER = "CONQUER"; THREATEN = "THREATENEDBY"; NOMOV - The piece cannot be moved PREFERPOS = "PREFER"; - propositional symbol for the preferred position PREFERPIECE = "PREFERPIECE"; - propositional symbol for the preferred piece

OWNER = "OWNER"; - Only used as predicate symbol MOVE = "MOVE"; - Only used as predicate symbol - it proves that it is possible and safe to move piece x to position y REACHABLE = "REACHABLE"; - Only used as predicate symbol CANMOVE = it is possible to move piece x to position y SAFEMOVE = it is safe to move piece x to position y PAWNMOVE = This is a PAWNMOVE We must create a set of predicate symbols and propositional symbols so that we can find the best piece to move, and the best position to move it to. This must be based on the facts and the state of the game, (the percept). An example of how to create good predicates based on a story can be found in chapter 9.3 p. 330.

The following questions must be answered: Is it early in the game? How many pieces, and what pieces can be moved? How many pieces do I have available What officers do I have available How many pieces do the opponent have available? What officers do the opponent have available? Is it late in the game? - Is it an endgame?

RULES: OWNER(player,piece) AND REACHABLE(piece,pos) => CANMOVE(piece,pos): OWNER(player, piece) AND PROTECTED(piece, pos) => SAFEMOVE(piecex,pos) OWNER(player,piece) AND TYPE(piece,PAWN) =>PAWNMOVE(piece,pos) CANMOVE(piece,pos) AND SAFEMOVE(piece,pos) => MOVE(piece,pos) CANMOVE(piece,pos) AND PAWNMOVE(piece,pos) =>MOVE(piece,pos)

At present (17.12.20) are the following rules avaiable: ((OWNER(WhitePlayer,x) AND REACHABLE(x,y)) => CANMOVE(x,y)) ((OWNER(WhitePlayer,x) AND PROTECTEDBY(x,y)) => SAFEMOVE(x,y)) Changed to: ((OWNER(WhitePlayer,x) AND PROTECTEDBY(z,y)) => SAFEMOVE(x,y)) ((SAFEMOVE(p2,pos) AND CANMOVE(p2,pos)) => MOVE(p2,pos)) (((OWNER(WhitePlayer,x) AND REACHABLE(x,y)) AND PIECETYPE(x,PAWN)) => PAWNMOVE(x,y))

A possible rule: MOVE(x,y) AND .. other conditions => CHOOSE(x,y)

A more enhanced rule:

olufjen commented 3 years ago

Work to do: OBS: From latest testrun 19.12.20: SAFEMOVE(v5,v6) AND CANMOVE(v7,v6) => MOVE(v7,v6) This should be: SAFEMOVE(v5,v6) AND CANMOVE(v5,v6) => MOVE(v5,v6) CORRECTED! See change above. Forward chaining: | OWNER(WhitePlayer,v14) AND PROTECTEDBY(v15,v16) => SAFEMOVE(v14,v16) |Assert fact SAFEMOVE(WhiteBishop2,g3), {v15=WhitePawn6, v16=g3, v14=WhiteBishop2}

When creating terms for a predicate (Sentence) we must specify what terms are Constants and what terms are Variables !!!

A piece that has recently been moved must not be considered. A more efficient algorithm for choosing correct action.

To choose the best chess action as a planning problem. We need to create an initial state and a goal state (aima.core.logic.planning.State) Both the initial state and the goal state contains a set of conjunctions of ground atoms (literals). The conjunction sentence is split into its ground literals. (See the Utils class under planning) The literals may contain variables.

Examples of sentences for the init state: occupies(WhitePawn8,h2) AND OWNER(WhitePlayer,WhitePawn8)AND REACHABLE(WhitePawn8,h3) occupies(WhitePawn8,h2) AND OWNER(WhitePlayer,WhitePawn8)AND REACHABLE(WhitePawn8,h4) PROTECTEDBY(WhitePawn7,h3) AND OWNER(WhitePlayer,WhitePawn7) Examples of sentences for the goal state: occupies(WhitePawn8,h3) AND OWNER(WhitePlayer,WhitePawn8)

With the initial state and the goal state defined, we then create a number of ActionSchemas (aima.core.logic.planning.ActionSchema). The actionschemas represents actions. So the actions and the states become part of the Problem (aima.core.logic.planning.Problem) that is put to the GraphPlanAlgoritm object to solve the problem.

Parameters that determine the structure of the Problem, and the states: The number of moves so far. If the piece of the action is an officer or a pawn. If the piece of the action has been moved recently If the piece with its new position protects/reaches a centre position. If the piece with its new position can threaten/capture an opponent piece.

Procedure to build a goal state: Get the name of the action's to position. Create a FOL Sentence with the predicate occupies and name of the piece and the name of the to position. Create a FOL Sentence with predicate OWNER and name of player and name of piece. Create Literals of these two sentences and put them into a List. Create the goal state with this list of literals.

olufjen commented 3 years ago

Chess engines:

Create a chess engine based on first-order logic. Create a chess engine based on planning.

We define this as a JOB (an Act see chapter 11.2 p. 406): (A hierarchical task network planning - HTN - with a High-level action (HLA)) See also the class: aima.core.logic.planning.hierarchicalsearch. Preliminary examples of HLA's:

A plan for: To capture the opponent king A plan for: To capture an opponent piece A plan for: To occupy a certain position (with a certain piece) A plan for: "Set officers active"

A chess action has a name (the piece name) and a number. The number is part of a scheduling process. Such a chess action is a primitive action as part of a HLA plan.

An example solution from the graphplan algorithm: The fluents of Initial state. They are nodes in S0 PIECETYPE(WhitePawn5,PAWN) occupies(WhitePawn5,e2) OWNER(WhitePlayer,WhitePawn5) REACHABLE(WhitePawn5,e3) REACHABLE(WhitePawn5,e4) PROTECTEDBY(WhitePawn6,e3) PROTECTEDBY(WhitePawn4,e3) The fluents the goal state occupies(WhitePawn5,e4) OWNER(WhitePlayer,WhitePawn5) PIECETYPE(WhitePawn5,PAWN) Action(moved) PRECOND:^occupies(WhitePawn5,e2)^OWNER(WhitePlayer,WhitePawn5) EFFECT:^~occupies(WhitePawn5,e2)^occupies(WhitePawn5,e4)^OWNER(WhitePlayer,WhitePawn5) Action(moved) PRECOND:^occupies(WhitePawn5,e2)^OWNER(WhitePlayer,WhitePawn5) EFFECT:^~occupies(WhitePawn5,e2)^occupies(WhitePawn5,e4)^OWNER(WhitePlayer,WhitePawn5) Action(No-op) PRECOND:^PIECETYPE(WhitePawn5,PAWN) EFFECT:^PIECETYPE(WhitePawn5,PAWN)

When removing the reachable predicate from the preconditions of the TYPE actionschema:

The fluents of Initial state. They are nodes in S0 PIECETYPE(WhitePawn5,PAWN) occupies(WhitePawn5,e2) OWNER(WhitePlayer,WhitePawn5) REACHABLE(WhitePawn5,e3) REACHABLE(WhitePawn5,e4) PROTECTEDBY(WhitePawn6,e3) PROTECTEDBY(WhitePawn4,e3) The fluents the goal state occupies(WhitePawn5,e4) OWNER(WhitePlayer,WhitePawn5) PIECETYPE(WhitePawn5,PAWN) Action(type) PRECOND:^PIECETYPE(WhitePawn5,PAWN)^occupies(WhitePawn5,e2) EFFECT:^PIECETYPE(WhitePawn5,PAWN)^occupies(WhitePawn5,e4) Action(No-op) PRECOND:^OWNER(WhitePlayer,WhitePawn5) EFFECT:^OWNER(WhitePlayer,WhitePawn5) Action(type) PRECOND:^PIECETYPE(WhitePawn5,PAWN)^occupies(WhitePawn5,e2) EFFECT:^PIECETYPE(WhitePawn5,PAWN)^occupies(WhitePawn5,WhitePawn4)

The following file contains results from solving the planning problem for all available actions 22.12.20

https://app.zenhub.com/files/137453363/5c768e25-4e54-4d82-b34a-9714f39e94f1/download

olufjen commented 3 years ago

ActionSchemas and Problems: We have a move action schema, one for each type of piece: pawnmove,bishopmove.... Then an action schema for determining which move? This action schema creates the goal state. The goal state contains a piece type, a piece name and a new occupied position. An example is shown below:

The fluents the goal state occupies(WhitePawn6,f3) OWNER(WhitePlayer,WhitePawn6) PIECETYPE(WhitePawn6,PAWN)

Each chess action has an action name and a number. The action name is the ontology name of the piece. The action number is determined by the number of moves so far. We need a HLA (High Level Action) to determine which chess piece type, the chess piece name, and the new occupied position. (And in fact then which ChessAction to perform). A HLA is created by the getHlaAct of the PlanningProblemFactory, given a Problem. A Problem contains an initial state, a goal state, and a set of Action Schemas.

Possible solutions: We can run through all available ChessActions and create a Problem for each of these actions. OR: We can run through all available ChessActions and create an action schema for each of these chessactions, and then create a Problem with all these actions. Then we must choose which of the initial and goal states for all the created action schemas that are going to represent the initial and goal state for the problem? How to choose the "correct" initial and goal states?

One Problem may contain one or more Action Schemas. Then what is the initial state and the goal state? Create one Problem that contains all the ActionSchemas for all the available ChessActions. The solution is to choose the "correct" ChessAction. From Chapter 11.2.2 Searching for primitive solutions: For each primitive action a, provide one refinement of Act with steps {a,Act}. Act is the HLA.

olufjen commented 3 years ago

A game 18.05.21

Check the methods that find opponent pieces that can be taken. And: If my piece is threatened: Find a safe place to move to !!! In this position of the game, the f5 position is removed from available positions for WhiteBishop2! Check file preferredmovB34 and the preferredMoveProcessor. Opponent pieces that block movements for the active player must be put in a separate removed list. This is so because the opponent piece can be taken. 22.05.21 This is now solved. The prepareMove method:

game180521.PNG

olufjen commented 3 years ago

Given the following position: Find out how opponent get its available and removed positions !!

game260521.PNG

From the knowledge base, the following positions are threatened by the blackbishop1: occupies(BlackBishop1,g4) OWNER(BlackPlayer,BlackBishop1) PIECETYPE(BlackBishop1,BISHOP) THREATENEDBY(BlackBishop1,e6) THREATENEDBY(BlackBishop1,d7) THREATENEDBY(BlackBishop1,c8) THREATENEDBY(BlackBishop1,h3) THREATENEDBY(BlackBishop1,h5) THREATENEDBY(BlackBishop1,f3) THREATENEDBY(BlackBishop1,e2) THREATENEDBY(BlackBishop1,d1) THREATENEDBY(BlackBishop1,f5) And the Blackbishop1 has the following available positions: Piece position g4 X, Y (6, 3) bB g4 BISHOP ActiveName B Available positions Position: e6 W X, Y (4, 5) Position: d7 W X, Y (3, 6) Position: c8 W X, Y (2, 7) Position: h3 W X, Y (7, 2) Occupied by: WhitePawn8 Position: h5 W X, Y (7, 4) Position: f3 W X, Y (5, 2) Occupied by: WhiteKnight2 Position: e2 W X, Y (4, 1) Position: d1 W X, Y (3, 0) Occupied by: WhiteQueen Position: f5 W X, Y (5, 4) Removed positions Position: e6 W X, Y (4, 5) Position: d7 W X, Y (3, 6) Position: c8 W X, Y (2, 7) Position: e2 W X, Y (4, 1) Position: d1 W X, Y (3, 0) Occupied by: WhiteQueen

If we move Whiteknight2, then the white queen can be taken.

Similar for black queen and black rook 2: occupies(BlackRook2,f8) OWNER(BlackPlayer,BlackRook2) PIECETYPE(BlackRook2,ROOK) THREATENEDBY(BlackRook2,h8) THREATENEDBY(BlackRook2,e8) THREATENEDBY(BlackRook2,c8) THREATENEDBY(BlackRook2,b8) occupies(BlackQueen,d8) OWNER(BlackPlayer,BlackQueen) PIECETYPE(BlackQueen,QUEEN) THREATENEDBY(BlackQueen,e8) THREATENEDBY(BlackQueen,d7) THREATENEDBY(BlackQueen,c8) THREATENEDBY(BlackQueen,e7)

This is not correct. See available positions: Piece name BlackRook2 r11q1rk1/ppp11ppp/11nb1n11/111p1111/111P11b1/11NBPN1P/PP111PP1/R1BQ1RK1 Piece position f8 X, Y (5, 7) bR f8 ROOK ActiveName R Available positions Position: f1 W X, Y (5, 0) Occupied by: WhiteRook2 Position: f2 B X, Y (5, 1) Occupied by: WhitePawn6 Position: f3 W X, Y (5, 2) Occupied by: WhiteKnight2 Position: f4 B X, Y (5, 3) Position: f5 W X, Y (5, 4) Position: h8 B X, Y (7, 7) Position: f6 B X, Y (5, 5) Occupied by: BlackKnight2 Position: g8 W X, Y (6, 7) Occupied by: BlackKing Position: f7 W X, Y (5, 6) Occupied by: BlackPawn6 Position: e8 W X, Y (4, 7) Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: c8 W X, Y (2, 7) Position: b8 B X, Y (1, 7) Position: a8 W X, Y (0, 7) Occupied by: BlackRook1 Removed positions Position: f6 B X, Y (5, 5) Occupied by: BlackKnight2 Position: g8 W X, Y (6, 7) Occupied by: BlackKing Position: f7 W X, Y (5, 6) Occupied by: BlackPawn6 Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: d8 B X, Y (3, 7) Occupied by: BlackQueen Position: a8 W X, Y (0, 7) Occupied by: BlackRook1 Position: f1 W X, Y (5, 0) Occupied by: WhiteRook2 Position: f2 B X, Y (5, 1) Occupied by: WhitePawn6 Position: f3 W X, Y (5, 2) Occupied by: WhiteKnight2 Position: f4 B X, Y (5, 3) Position: f5 W X, Y (5, 4) Position: e8 W X, Y (4, 7) Position: c8 W X, Y (2, 7) Position: b8 B X, Y (1, 7)

Piece position d8 X, Y (3, 7) bQ d8 QUEEN ActiveName Q Available positions Position: d1 W X, Y (3, 0) Occupied by: WhiteQueen Position: d2 B X, Y (3, 1) Position: d3 W X, Y (3, 2) Occupied by: WhiteBishop2 Position: h8 B X, Y (7, 7) Position: d4 B X, Y (3, 3) Occupied by: WhitePawn4 Position: g8 W X, Y (6, 7) Occupied by: BlackKing Position: d5 W X, Y (3, 4) Occupied by: BlackPawn5 Position: f8 B X, Y (5, 7) Occupied by: BlackRook2 Position: d6 B X, Y (3, 5) Occupied by: BlackBishop2 Position: e8 W X, Y (4, 7) Position: d7 W X, Y (3, 6) Position: c8 W X, Y (2, 7) Position: b8 B X, Y (1, 7) Position: a8 W X, Y (0, 7) Occupied by: BlackRook1 Position: f6 B X, Y (5, 5) Occupied by: BlackKnight2 Position: e7 B X, Y (4, 6) Position: c7 B X, Y (2, 6) Occupied by: BlackPawn3 Position: b6 B X, Y (1, 5) Position: a5 B X, Y (0, 4) Position: h4 B X, Y (7, 3) Position: g5 B X, Y (6, 4) Removed positions Position: g8 W X, Y (6, 7) Occupied by: BlackKing Position: d5 W X, Y (3, 4) Occupied by: BlackPawn5 Position: f8 B X, Y (5, 7) Occupied by: BlackRook2 Position: d6 B X, Y (3, 5) Occupied by: BlackBishop2 Position: a8 W X, Y (0, 7) Occupied by: BlackRook1 Position: f6 B X, Y (5, 5) Occupied by: BlackKnight2 Position: c7 B X, Y (2, 6) Occupied by: BlackPawn3 Position: b6 B X, Y (1, 5) Position: a5 B X, Y (0, 4) Position: e8 W X, Y (4, 7) Position: c8 W X, Y (2, 7) Position: b8 B X, Y (1, 7) Position: d1 W X, Y (3, 0) Occupied by: WhiteQueen Position: d2 B X, Y (3, 1) Position: d3 W X, Y (3, 2) Occupied by: WhiteBishop2 Position: d4 B X, Y (3, 3) Occupied by: WhitePawn4