Integrating Rule Based AI Tools Into Mai
Integrating Rule Based AI Tools Into Mai
design of the specific aspects of their products, such as storyline, graphic style,
gameplay; hence, they look for facilitating development environments that relieve
them from the burden of implementing technical features at lower abstraction
levels. In this respect, Unity1 is one of the most popular and widespread devel-
opment environments in the game design field. In Unity, the development time
of routine tasks is considerably shorter, and a vast range of ready-to-use solu-
tions is available. Nevertheless, a major development task, which is nowadays
scarcely automated, is the definition of some sort of AI needed, for instance,
when implementing non-human players.
Interestingly, rule-based declarative formalisms can be of great help for the
definition of such AIs: indeed, the declarative nature of rule-based knowledge
bases allow to focus on specifying desiderata, thus getting rid of the burden of
defining how to meet them; furthermore, knowledge is typically explicitly repre-
sented. Specifications written using declarative rules are definitely easier to be
modified than implicit imperative solutions: one can start by quickly defining a
first prototypical version and then iteratively rearrange it in increasingly sophis-
ticated ways. On the other hand, when used in these contexts, declarative-based
AI modules need to interact with other “non-declarative” components; i.e., one
or more logic-based reasoning modules, handled by suitable declarative systems,
must be integrated into larger, hybrid, architectures.
General ways for easing the embedding of declarative paradigms into imper-
ative, and in particular object-oriented, programming approaches, have been
significantly explored in the recent years, especially for Answer Set Program-
ming [1,4,8], for which a number of solutions have been proposed [5–7,9–11].
Nonetheless, given the general impedance between the two different paradigm
categories, there are a number of applicative contexts that still lack tools for the
integration of rule-based formalisms, such as game development. Indeed, among
the wide range of extensions offered to and by the Unity community, only few of
them are aimed to provide Artificial Intelligence capabilities2 and none of them
enables developers to make use of a rule-based reasoning module.
In this work we present a novel solution for the integration of AI declarative
modules within applications developed in Unity; our proposal is based on a
proper extension EmbASP [6], an abstract framework. In the following, we first
illustrate the requirements that must be met when integrating rule-based engines
in the context of a game development engine following the Unity paradigm. A
C# version of EmbASP, as it is the language adopted for the development in
Unity, is shortly described; we then showcase a Unity-based game where the
player is controlled by rule-based reasoning modules programmed in ASP.
1
https://unity3d.com/unity.
2
Among which:
https://assetstore.unity.com/packages/tools/ai/real-ai-112677,
https://assetstore.unity.com/packages/tools/ai/reaction-ai-66353.
312 F. Calimeri et al.
3
https://www.mat.unical.it/calimeri/projects/embasp.
Integrating Rule-Based AI Tools into Mainstream Game Development 313
As already stated before, in order to achieve our goal and develop a first work-
ing version of the hybrid system described above, we made usage of EmbASP.
EmbASP eases and guide the generation of suitable libraries for the use of spe-
cific logic-based systems on particular platforms; resulting applications manage
such systems as “black boxes”. Given that the framework can be easily imple-
mented in different object-oriented programming languages and its architecture
can be specialized for different logic languages and solvers, we defined a C#
314 F. Calimeri et al.
UNITY PLAYER
UNITY GAME
Graphic Engine
Core
EmbASP4Unity
Reasoner1 … Reasonern
logic of game; and (iii) the EmbASP4Unity module, connecting with external
reasoning engines. In order to perform AI tasks, the core sub-module inter-
acts with the EmbASP specialization for Unity. In our case, such interaction is
used to define Pac-Man moves; these are chosen according to an AI defined by
means of ASP rules. More in detail, the core module identifies the current game
state, models such information as an ASP rule-based knowledge base KB, and
invokes an underlying ASP reasoner via the Unity specialization of EmbASP.
In turn, the reasoner evaluates KB along with a ASP rule set R modelling the
AI desired behavior; the reasoning engine outputs so-called answer sets, which
encode suggested movements for the Pac-Man AI player, and these are returned
to EmbASP, that forwards them back to the core module.
As anticipated in Sect. 3, the integration of rule-based modules into Unity
requires to overcome some impedance issues; as for synchronization, we opted
for calling external solvers via a synchronous call. Positions in the game map
have been mapped to discrete cells, in contrast with the fact that sprites’ posi-
tions in the game are modeled with the resolution of one pixel. Note that Unity
continuously updates the game graphics with the aim of making the movements
of involved characters smooth and more realistic, allowing to invoke custom code
even on a per-pixel movement basis. By contrast, in a game like Pac-Man, in
which reactivity is crucial, invoking solvers with a so high frequency is expensive
and useless. This is why we opted for calling reasoning engines only when the
currently chosen Pac-Man move has been completed, i.e., when the Pac-Man
has been moved completely to the next discrete cell. Indeed, until a chosen Pac-
Man move has not been graphically executed, the system would be invoked with
the same input and it would return the same output, given that the Pac-Man
position would be mapped to the same discrete cell.
The ASP program implementing the Pac-Man AI has been designed accord-
ing the so-called “Guess/Check/Optimize” paradigm [2]: the Guessing part
defines the search space (by means of disjunctive rules); the Checking part
enforces solution admissibility (by means of integrity constraints); eventually,
the Optimizing part specifies preference criteria (by means of weak constraints).
For space reasons, we refrain from going into a complete description of the ASP
logic program; also, we are not focusing on obtaining a state-of-the-art artificial
Pac-Man player, but rather on proving the viability of our approach5 . However, it
is worth noting how the declarative approach allows to easily and quickly define
an AI and facilitates changes in the strategy by simply modifying some rules.
For instance, weak constraints in the optimizing part may guide the behaviour
of the Pac-Man player by considering different factors, along with their impor-
tance. Hence, just by focusing on the optimizing part, by means of just a few
changes we may significantly change the behaviour of the Pac-Man player, as
shown next.
5
All the development material, including logic programs, source code and a
fully playable version of the game are available at https://github.com/DeMaCS-
UNICAL/Pac-Man-Unity-EmbASP.
316 F. Calimeri et al.
We briefly recall that each weak constraint has associated a pair consisting of
a weight and a level; informally, an answer set pays, at each level, a penalty given
by the sum of the weights. Moreover, higher levels have an higher priority [3].
The optimizing part guides the Pac-Man to:
1. eat Pac-Dots, if any:
:∼ nextCell (X , Y ) , empty (X , Y ) . [1 @3 ]
The game grid is divided in cells identified by their (x, y) coordinates: the next
Pac-Man position is given by nextCell(X, Y ), while empty(X, Y ) indicates
that at coordinates (X, Y ) there is an empty cell (i.e., there is no Pac-Dot
nor Power-Pellet in it);
2. get away from the ghosts:
:∼ m i n D i s t a n c e N e x t G h o s t ( D ) , D1 =10 - D , not powerup . [ D1@4 ]
Let M be a candidate move, and D be the distance from the nearest ghost if
M would be performed: M pays a penalty inversely proportional to D, unless
Pac-Man has eaten a Power-Pellet;
3. chase the ghosts when a power pellet is eaten:
:∼ m i n D i s t a n c e N e x t G h o s t ( D ) , powerup . [ D@4 ]
References
1. Brewka, G., Eiter, T., Truszczynski, M.: Answer set programming at a glance. Com-
mun. ACM 54(12), 92–103 (2011). http://doi.acm.org/10.1145/2043174.2043195
2. Buccafurri, F., Leone, N., Rullo, P.: Strong and weak constraints in disjunctive
datalog. In: Dix, J., Furbach, U., Nerode, A. (eds.) LPNMR 1997. LNCS, vol. 1265,
pp. 2–17. Springer, Heidelberg (1997). https://doi.org/10.1007/3-540-63255-7 2
3. Calimeri, F., et al.: Asp-core-2: Input language format (2012)
4. Calimeri, F., Gebser, M., Maratea, M., Ricca, F.: Design and results of the fifth
answer set programming competition. Artif. Intell. 231, 151–181 (2016). https://
doi.org/10.1016/j.artint.2015.09.008
5. Febbraro, O., Leone, N., Grasso, G., Ricca, F.: JASP: A framework for integrating
answer set programming with java. In: Brewka, G., Eiter, T., McIlraith, S.A. (eds.)
Principles of Knowledge Representation and Reasoning: Proceedings of the Thir-
teenth International Conference, KR 2012, Rome, Italy, June 10–14, 2012. AAAI
Press (2012). http://www.aaai.org/ocs/index.php/KR/KR12/paper/view/4520
6. Fuscà, D., Germano, S., Zangari, J., Anastasio, M., Calimeri, F., Perri, S.: A frame-
work for easing the development of applications embedding answer set program-
ming. In: Proceedings of the 18th International Symposium on Principles and Prac-
tice of Declarative Programming, Edinburgh, United Kingdom, pp. 38–49 (2016)
7. Gebser, M., Kaminski, R., Kaufmann, B., Schaub, T.: Clingo = ASP + control:
Preliminary report. CoRR abs/1405.3694 (2014). http://arxiv.org/abs/1405.3694
8. Gelfond, M., Lifschitz, V.: Classical negation in logic programs and disjunctive
databases. New Gener. Comput. 9(3/4), 365–386 (1991). https://doi.org/10.1007/
BF03037169
9. Ricca, F.: The DLV java wrapper. In: de Vos, M., Provetti, A. (eds.) Proceedings
ASP03 - Answer Set Programming: Advances in Theory and Implementation, pp.
305–316. Messina, Italy, September 2003. http://CEUR-WS.org/Vol-78/
10. Schüller, P., Weinzierl, A.: Answer set application programming: a case study on
tetris. In: Vos, M.D., Eiter, T., Lierler, Y., Toni, F. (eds.) Proceedings of the
Technical Communications of the 31st International Conference on Logic Program-
ming (ICLP 2015). CEUR Workshop Proceedings, Cork, Ireland, vol. 1433. CEUR-
WS.org (2015), 31 August–4 September 2015. http://ceur-ws.org/Vol-1433/tc 17.
pdf
11. Thimm, M.: Tweety: a comprehensive collection of java libraries for logical aspects
of artificial intelligence and knowledge representation. In: Baral, C., Giacomo,
G.D., Eiter, T. (eds.) Principles of Knowledge Representation and Reasoning: Pro-
ceedings of the Fourteenth International Conference, KR 2014, Vienna, Austria.
AAAI Press, 20–24 July 2014. http://www.aaai.org/ocs/index.php/KR/KR14/
paper/view/7811