Answer Set Programming (DLV - Clingo) :connect 4 Solver
Answer Set Programming (DLV - Clingo) :connect 4 Solver
4, July 2023
ABSTRACT
Answer set programming is an approach that has recently gained a lot of popularity in solving complex
problems due to its efficient knowledge representation and reasoning. Knowledge is represented as answer
set programs, and reasoning is performed by answer set solvers. Answer set programming enables default
reasoning, which is required in commonsense reasoning. In this paper, we discuss the potential role of
answer set programming (ASP) in the context of approaches to the development of agents especially in the
realm of Computational Logic. So, we focus on the usage of DLV for solving problems in a declarative
manner particularly, in the solution of the game “connect four”. Additionally, we present the solution to
this same problem from Clingo's perspective. We address this challenge with the ASP system clingo and its
grounding and solving components by equipping them with well-defined generic interfaces facilitating the
manifold integration efforts.
KEYWORDS
Asnwer Set Programming, Clingo, Dlv, Logic, Agents,
1. INTRODUCTION
Answer Set Programming (ASP, Answer Set (Stable Model) Semantics [1] [2]), is the realization
of much theoretical work on Non-monotonic Reasoning and Artificial Intelligence applications of
Logic Programming. ASP is a novel paradigm of logic programming that has now great
acceptance in the artificial intelligence community. One of the most important reasons for its
acceptance is the existence of efficient software to compute answer sets [3] [4]. ASP provides a
simple, expressive, and efficient language that can be well suited for modelling the agent rational
component into computer science and artificial intelligence. The answer sets for a logic program
can be described as the satisfying interpretations for a set of propositional formulae.
DOI:10.5121/ijaia.2023.14403 31
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
present the implementation of our agent, in the first instance in dlv and in a second moment using
the clingo paradigm, in which we incorporate a heuristic function. Finally, we give our
conclusions and future work about this proposal.
In the last years, agent’s paradigm using logic programming has claimed a major role in defining
the trends of modern research, influencing a broad spectrum of disciplines such as Computational
Logic, Philosophy, Logic Programming, Answer Set Programming, among others. The agent
concept has recently increased its influence in the research and development of computational
logic–based systems [6]. For this reason, it is important to consider the modelling of the
reasoning module of an agent for games in general and for zero-sum games. Analyse the
possibilities of a data type so that the agent can participate by applying an appropriate strategy.
To form an effective reasoning module, the following is required:
• Objective:
• Start:
• The middle:
32
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
• Actions:
• Knowledge
o The agent needs a strategy that allows him to propose a valid move according to
the rules.
o The agent needs to prioritize his moves to win the game.
Win
Block in case the opponent has three continuous chips.
Build the game based on the strategy defined by the agent (applying
heuristics).
Define the start of the game by choosing the middle row.
For this reason, the proposed agent must consider the following:1) Objectives; 2) Environment;
3) Perceptions; 4) Actions and 5) its Knowledge. All these necessary and relevant characteristics
for the proper functioning of our agent can be summarized as follows:
Next, we present some of the rules implemented for the agent are:
1. Strategy: Set of rules for a basic level game some of the employed rules in the strategy are:
33
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
g(X,Y,r):-cell(X,Y,v), vertical win move cell(X,Z,r),
Y=Z+1,cell(X,W,r), Y=W+2, cell(X,R,r), Y=R+3. t(X,Y,r):-
cell(X,Y,v), horizontal left alignment cell(Z,Y,r),
Z=X+1,cell(W,Y,r), W=X+2.
2. Complex strategy: Set of rules for an expert level game b(X,Y,r):-cell(X,Y,v), cell(Z,Y,a),
Z=X+1, cell(W,Y,a), W=X+2, cell(R,Y,a), R=X+3.
3. Available cells: File with information about available cells: Set of available cells in every
moment. cell(0,3,v). cell(1,4,v). cell(3,2,v). cell(4,1,v). cell(5,1,v). cell(6,0,v).
4. Busy cell: File with a set of unavailable cells that may contain black or white chips.
cell(0,1,r). cell(0,2,a). cell(1,0,r). cell(1,1,r). cell(1,2,r).
cell(1,3,a). cell(2,0,a). cell(2,1,a). cell(2,2,a). cell(2,3,r).
cell(2,4,r). cell(2,5,r). cell(3,0,a). cell(3,1,a). cell(4,0,a).
5. Tokens to move: Set of chips that can be moved by the answer sets calculus provided by Dlv.
{b(6,0,a), c(1,4,r), s(6,0,r)}
Tokens to move correspond to the group of answer sets calculated by Dlv. This set allows our
agent to execute the best action. The structure is described as follow:
F(X,Y,e):- cell(X,Y,v),
...,
...,
...,
Is derived the consequent F(X,Y,e) if the exposed conditions in the predecessor are satisfied.
F can be:
The exposed conditions determinate if the empty cells make vertical, horizontal or diagonal lines
with the taken cells, such way that can serve us to block our opponent, move forward or win the
game.
For example:
We deduce g(X,Y,r) if the cells cell(X,Y,v) exist and cell(X,Z,r), cell(X,W,r), cell(X,R,r) exists
too, and are taken by black chips and form a vertical line that lead us win the game, Y=Z+1,
Y=W+2, Y=R+3.
34
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
In this way, we describe the rest of sentences to win, but considering horizontal and diagonal
lines. The sentence to block and move forward are derived for the sentence to win. For example,
the sentences for block an alignment of tree are the same statement to win, the difference is that
we ask for the white ones, not the black. The sentence to move forward in two chips alignment to
a tree chips alignment is ask for two of the predecessor cells of an empty one. The sentence to
make a move on the first line only check the existence of the cell with Y equals to zero and if is
empty.
As can be seen, choosing the paradigm based on answer set programming gives us a simple,
clear, and adequate opportunity to model complex problems in a transparent way. As shown in
the solution presented in dlv, the way in which this type of complex problem is modeled is simple
and clear [11].
In Clingo [9], unlike Dlv, the evaluation of all possible movements is done through the minimax
heuristic. The heuristic called minimax [8] is used to evaluate positions by recursively examining
all possible future moves to a certain depth and returning the best evaluated move. To find out
which is the best move among all the possible options, the heuristic function evaluates the
horizontal, vertical, and diagonal positions by looking for a specific number of consecutive pieces
of the same type in a row. If a win is found, the score is added by 100 for player 1 or subtracted
by 100 for player 2. If no win is found, the score is set to 0.
The Minimax algorithm (Schiffel & Thielscher 2009) is a method used in artificial intelligence to
make optimal decisions in two-player game situations with perfect information, such as chess,
checkers, Connect 4, among others. The algorithm works by generating a search tree that
represents all possible moves and responses to those moves, from the current state of the game to
a final state. The algorithm determines the best move a player can make at each level of the
search tree, assuming the opponent is also playing optimally. At the top level of the tree, the
35
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
algorithm considers all possible moves by the current player and simulates the opponent's optimal
response to each move. It then evaluates each of these possible outcomes to determine which is
the best option for the current player, using a heuristic evaluation function. This process is
repeated recursively for all levels of the search tree, alternating between the current player and
the opponent, until an endgame state is reached. The Minimax algorithm then returns the best
move that the current player can make at the top level of the search tree out of all possible moves
that will be evaluated.
The Connect Four game is considered a zero-sum game because each player competes to reach
the goal of connecting four tiles of the same colour in a row, column, or diagonal. If one player
manages to connect four tiles, they win the game, while the other player loses. Thus, one player's
profit comes at the other player's expense, and the sum total of wins and losses will always be
zero. It is important to note that although the game is zero-sum, this does not necessarily mean
that it is a zero-sum game, that is, that there are no external benefits or secondary effects for the
players or third parties.
Minimax works exploring all possible moves and game states, building a search tree. Each tree
node represents a game state, and each arc represents a possible move. The score at each leaf
node is the final score of the game. Scoring is assigned so that MAX players try to maximize and
MIN players try to minimize. The depth search technique is used to explore all possible moves.
However, the number of nodes in the search tree grows exponentially with the depth of the tree,
making full exploration impossible for complex games.
To solve this problem, the alpha-beta pruning technique is used, which reduces the number of
explored nodes. The alpha-beta pruning algorithm maintains two values at each tree node: alpha
and beta. Alpha represents the minimum score that MAX can achieve when exploring the left
subtree of the node, while beta represents the maximum score that MIN can achieve when
exploring the right subtree of the node.
Alpha-beta pruning works by removing subtrees that do not need to be explored. If the value of
beta at one node is less than or equal to the value of alpha at another sibling node, then the entire
right subtree of the first node can be deleted, since MIN would never choose that branch, since
MAX has a better option available. Similarly, if the value of alpha at one node is greater than or
equal to the value of beta at another sibling node, the entire left subtree of the first node can be
removed, since MAX would never choose that branch, since MIN has a better branch. option
available.
The implementation in CLingo is based on the ASP paradigm, where we must create lines of code
made up of a preposition followed by the conditions that will allow it to be fulfilled. In order to
start a connect four game we must declare the essential parts of the game, which are a board, the
players, the tiles and the cells where it is possible to shoot. In connect four we have a board that is
a 7x6 matrix where the pieces will be placed with respect to a position defined by an ordered pair
X, Y. Our board will be created with a predicate named cell that allows us to know where the
pieces can be moved. chips according to the dimensions specified above.
cell(1..7, 1..6).
The next step is to give the player a turn, which will change each time a player move is made.
36
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
We use currentPlayer(_) to indicate if the current player is the one making the move, if this
condition is met it will be given a boolean value of True and otherwise it will be False. Each
player will be recognized by chips, “X” in the case of the human player and “O” in the case of
artificial intelligence, and they will only be able to shoot in those cells evaluated as empty. To
recognize empty cells, a predicate with two conditions is used: empty(X, Y) will be true only
when there is no value within said cell and it is a cell within the range (X,Y).
Once the bases of the game have been defined, we must define the actions that can be carried out
on them. The movements are the most vital part of a connect four game, because depending on
the movement that our opponent makes, we can make a movement that can bring us closer to
victory or block the opponent. The actions that we carry out before a movement is to think of
several possible movements, evaluate which of these is the best and once decided which one to
carry out, then we proceed to carry it out. For each of these actions, a predicate is created with its
respective conditions, starting with the possible movements. Instead of making a movement over
the entire matrix, we will make it over a vector which will only move between the columns of the
X axis, leaving the axis stable. movement on one of the six rows of the Y axis, that is, an X value
will be chosen under two conditions.
This code defines the game state and implements a minimax algorithm with a heuristic function
to evaluate the position. The game state is represented by a cell(X,Y) predicate that defines the
position of each cell on the board, and the value(X,Y,P) and empty(X,Y) predicates that define
the value(color part) of a cell and if it is empty, respectively. The current player is represented by
the predicate currentPlayer(P). Possible moves are generated by the move(X) predicate, which
looks for empty cells in the bottom row and top row of any existing pieces. The predicate
evaluates the score of a position score(X, Score), which first checks if the current player can win
with move X and assigns a score of 100 or -100 accordingly. If there is no immediate win, the
algorithm calls minimax to recursively evaluate the position with a lower depth. The position
score is the maximum or minimum of the scores of the possible moves, depending on the player.
Finally, the makeMove(X) predicate updates the game state by placing a piece of the current
player in cell X. The undoMove(X) predicate undoes the move by removing the piece from cell
X. The heuristic function is defined by the heuristic(Score) predicate ), which checks for
37
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
horizontal, vertical, and diagonal gains and assigns a score of 100 or -100 accordingly. If there is
no win, the score is 0.
horizontalScore(Score) :- Score = 0.
In this presentation, two proposals for solutions to a complex problem such as connect four have
been shown.A future line of research will be to implement in both tools (Dlv and Clingo) a
problem that allows us to measure which one performs better in the model’s calculation. Finally,
we can comment that both tools allow connectivity with front ends. In our proposal, in the case of
dlv, we use java as the front-end to interact with the end user. While in the case of Clingo we use
python as the front-end part that allows interaction with the end user,
ACKNOWLEDGEMENTS
We thank the anonymous referees for their useful suggestions. The academic group of
combinatorial algorithms and learning supports this work.
38
International Journal of Artificial Intelligence and Applications (IJAIA), Vol.14, No.4, July 2023
REFERENCES
[1] M Gelfond, V Lifschitz. The stable model semantics for logic programming. ICLP/SLP 88, pp.:1070-
1080, 1988.
[2] Robert Kowalski. Computational Logic and Human Thinking: How to be Artificially Intelligent.
Cambridge University Press. 978-0-521-19482-2, 2011.
[3] Martin Gebser, Roland Kaminski, Benjamin Kaufmann, Max Ostrowski, Torsten Schaub, Sven
Thiele. A User’s Guide to gringo, clasp, clingo, and iclingo (ver. 3.x)
[4] Abeer Dyoub, Stefania Costantini and Giovanni De Gasperis. Answer set programming and agents,
Cambridge University Press: 05 November 2018.
[5] Zacarias F. “Belief revision and Updates under Nonmonotonic Reasoning: An answer set approach”.
UDALP Thesis, 2004.
[6] De Vos Marina and Vermeir Dirk. Logic Programming Agents and Game Theory. AAAI Technical
Report SS-01-01. Compilation copyright © 2001, AAAI (www.aaai.org). All rights reserved.
[7] Zacarias Fernando, Cuapa Rosalba, Contreras Meliza, Garcia Oscar. Modelling Agents with
AProlog. International Journal of Engineering and Applied Sciences (IJEAS).ISSN: 2394-3661,
Volume-5, Issue-6, June 2018.
[8] Stephan Schiffel and Michael Thielscher. Automated Theorem Proving for General Game Playing.
Proceedings of the Twenty-First International Joint Conference on Artificial Intelligence IJCAI-09,
Publisher AAAI Press. 2009.
[9] Potassco project 2000. the Potsdam Answer Set Solving Collection. Tools for Answer Set
Programming developed at the University of Potsdam. Germany https://potassco.org/
[10] Höschler Enrico. Answer set programming: Sudoku solver. Jul 10, 2018.
https://ddmler.github.io/asp/2018/07/10/answer-set-programming-sudoku-solver.html
[11] Fernando Zacarias, Rosalba Cuapa, Guillermo De Ita, J.C. Acosta and Daniel Torres. PLANNING
SOLUTIONS IN THE REAL WORLD. International Journal of Artificial Intelligence &
Applications (IJAIA), Vol. 5, No. 3, May 2014.
[12] Zacarias, Fernando and Cuapa, Rosalba and Jimenez, Luna and Vazquez, Noemi, Modelling of
Intelligent Agents Using A–Prolog (March 31, 2019). International Journal of Artificial Intelligence
and Applications (IJAIA), Vol.10, No.2, March 2019, Available at SSRN:
https://ssrn.com/abstract=3383952 or http://dx.doi.org/10.2139/ssrn.3383952
AUTHORS
Dr. Fernando Zacarias is full-time professor in the computer science department into the
Universidad Autónoma de Puebla. He has directed and participated in research projects
and development in the area of artificial intelligence and Computer science from 1995.
39