0% found this document useful (0 votes)
49 views8 pages

Integrating Rule Based AI Tools Into Mai

This document discusses the integration of rule-based AI tools, specifically Answer Set Programming (ASP), into mainstream game development using the Unity engine. It presents a framework called EmbASP that facilitates the embedding of declarative reasoning modules within Unity applications, highlighting its advantages for AI development in games. A proof-of-concept game is developed to demonstrate the feasibility of this integration, showcasing how rule-based AI can enhance gameplay through easier modifications and sophisticated planning strategies.

Uploaded by

vandyh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views8 pages

Integrating Rule Based AI Tools Into Mai

This document discusses the integration of rule-based AI tools, specifically Answer Set Programming (ASP), into mainstream game development using the Unity engine. It presents a framework called EmbASP that facilitates the embedding of declarative reasoning modules within Unity applications, highlighting its advantages for AI development in games. A proof-of-concept game is developed to demonstrate the feasibility of this integration, showcasing how rule-based AI can enhance gameplay through easier modifications and sophisticated planning strategies.

Uploaded by

vandyh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Integrating Rule-Based AI Tools

into Mainstream Game Development

Francesco Calimeri , Stefano Germano , Giovambattista Ianni ,


Francesco Pacenza(B) , Simona Perri , and Jessica Zangari

Department of Mathematics and Computer Science, University of Calabria,


Rende, Italy
{calimeri,germano,ianni,pacenza,perri,zangari}@mat.unical.it
https://www.mat.unical.it

Abstract. Rule-based declarative formalisms enjoy several advantages


when compared with imperative solutions, especially when dealing with
AI-based application development: solid theoretical bases, no need for
algorithm design or coding, explicit and easily modifiable knowledge
bases, executable declarative specifications, fast prototyping, quick error
detection, modularity. For these reasons, ways for combining declarative
paradigms, such as Answer Set Programming (ASP), with traditional
ones have been significantly studied in the recent years; there are how-
ever relevant contexts, in which this road is unexplored, such as devel-
opment of real-time games. In such a setting, the strict requirements
on reaction times, the presence of computer-human interactivity and a
generally increased impedance between the two development paradigms
make the task nontrivial. In this work we illustrate how to embed rule-
based reasoning modules into the well-known Unity game development
engine. To this end, we present an extension of EmbASP, a framework to
ease the integration of declarative formalisms with generic applications.
We prove the viability of our approach by developing a proof-of-concept
Unity game that makes use of ASP-based AI modules.

Keywords: Answer Set Programming · Artificial Intelligence


Game programming · Knowledge representation and reasoning
Logic programs · Rule-based systems · Unity

1 Context and Motivations


In the latest years, the steadily increasing availability of powerful and cheap
hardware, experienced even in the mobile scenario, fostered an equally increas-
ing deployment of applications that heavily rely on complex two- or three-
dimensional graphics, requiring computational expensive renderings. Computer-
Generated Imagery (CGI) applications, such as simulations or videogames, fall
in this category. Furthermore, several facilities, tools and frameworks have been
released with the aim of easing the development of such applications. This is
especially true for the gaming context, where developers prefer to focus on the
c Springer Nature Switzerland AG 2018
C. Benzmüller et al. (Eds.): RuleML+RR 2018, LNCS 11092, pp. 310–317, 2018.
https://doi.org/10.1007/978-3-319-99906-7_23
Integrating Rule-Based AI Tools into Mainstream Game Development 311

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.

2 EmbASP and Answer Set Programming


EmbASP3 [6] is a framework for the integration of logic formalisms in external
systems for generic applications; it consists of a general and abstract architec-
ture, which can be instantiated in a programming language of choice, and eas-
ily allows for proper specializations to different platforms and knowledge-based
solvers. The framework was originally mainly tailored on Answer Set Program-
ming (ASP), but it has been generalized for enabling the embedding of further
logic formalisms, far beyond those similar to ASP, such as PDDL. It has been
implemented in Java and in Python, and permits the embedding of rule-based
reasoning into desktop and mobile applications, making use of a wide range of
solvers.
In our showcase application the rule-based specifications have been written in
Answer Set Programming (ASP) [1,4,8]. ASP is a rule-based declarative formal-
ism for knowledge representation and reasoning, developed in the field of logic
programming and nonmonotonic reasoning. The language of ASP is based on
rules, allowing (in general) for both disjunction in rule heads and nonmonotonic
negation in the body. Also, optimization constructs such as weak constraints are
allowed. Mainstream ASP engines accept declarative specifications expressed in
the standardized input language ASP Core 2 [3].

3 Integrating Declarative Formalisms into Unity


Unity is a cross-platform game engine primarily used to develop video-games or
simulations for more than 25 different platforms like mobile, computers and con-
soles. It supports 2D and 3D graphics, drag ad drop functionality and scripting
using C# language.
Rapid development speed, a very active and powerful community, cross-
platform integration and the availability of 3D models, ready to use scripts,
shaders and other extensions, that can be easily added to a game, make Unity a
user-friendly game engine easy to learn and use also for beginners; indeed, Unity
is currently the leader of the global game development engine market, holding
the 45% share, nearly three times the size of its nearest competitor.
Among the wide range of assets offered by the Unity community, only few
of them are aimed to provide Artificial Intelligence capabilities (see footnote
2), and none of them enables developers to make use of a rule-based reasoning
module. Making rule-based reasoning available in a game development engine
such as Unity is attractive for several reasons:

1. fast prototyping and development of AI-based players, and of AI-based oppo-


nents; in particular:
– rule-based specifications are more transparent and easier to be manually
refined, compared to other approaches, such as machine learning;

3
https://www.mat.unical.it/calimeri/projects/embasp.
Integrating Rule-Based AI Tools into Mainstream Game Development 313

– modifications are easier and faster, compared to tedious retraining stages;


and,
– rule-based specifications make easier to keep AI modules independent
from the overall hard-wired game logic.
2. possibility to introduce and control sophisticated planning strategies;
3. possibility to switch from one strategy to another by simply adding or chang-
ing rules.

Nevertheless, the development of an hybrid system that integrates an imperative


graphic-tailored system like Unity with a knowledge- and rule-based evaluation
system, needs to tackle and solve some impedance issues, some of which are
discussed next.

– Modern development game engines require integration of external assets not


only at runtime level, but also at design time. Thus, the integration of rule-
based engines must be designed taking into account the possibility of invoking
and solving declarative specifications also at design time.
– Design time in game development engines follows the properties paradigm.
Properties combine traditional code with graphical design in a smooth and
intuitive way. For instance, many game variables can be changed graphically
and have immediate display on the design GUI: thus game blueprints can
include and be affected by rule-based modules. As Unity strong relies on
the notion of asset (i.e. a reusable component combining graphical elements
and code), one might think of introducing rule-based, reusable AI controllers.
This requires proper treatment, as designers expect consistent behaviors and
visualizations, both at run-time and design-time.
– Imperative and external information flows need to be synchronized. Usually,
imperative code is used to update the graphical interface, while declarative
specifications are executed asynchronously. However, thread synchronization
is one of the major weak points in most of the game engines, and, in particular,
Unity APIs are not thread safe; hence, developers have to handle all the
troubles coming when synchronizing information retrieved from the execution
of an external module.
– Arithmetic calculations need to be treated uniformly. Logic-based formal-
ism typically deal with small, discretized integer ranges, while graphical
updates and physics simulation require floating point values. Thus, infor-
mation about the state-of-world extracted from the graphic engine has to be
properly matched when sent to and from rule-based reasoning engines.

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.

specialization of the framework, as C# is the main scripting language today


adopted for the development in Unity.
In the following, we show how to employ the specialized C# libraries gener-
ated via EmbASP for making use of a rule-based reasoning module from within
actual Unity applications.

4 Rule-Based Reasoning Modules into Unity at Work


In order to give an idea of how AI declarative modules can be integrated within
applications developed in Unity via EmbASP, we developed a showcase appli-
cation. We started from a public available open-source project4 , inspired from
the original Pac-Man game, where human players control the Pac-Man moves,
and designed a new version, where the Pac-Man plays autonomously according
to moves suggested by an ASP module, instead.

Desktop Mobile Console Web …

UNITY PLAYER

UNITY GAME

Graphic Engine

Core

EmbASP4Unity

Reasoner1 … Reasonern

Fig. 1. General architecture of a Unity game relying on EmbASP

The layered architecture of the application is depicted in Fig. 1. The Unity


Player component handles the Unity facilities specifically intended for help-
ing developers in deploying an application on different platforms; it interacts
with the Unity Game component, consisting in the actual implementation of
the game, internally divided into three sub-modules: (i) the graphic engine,
that takes care of the graphical rendering; (ii) the core module, managing the
4
https://github.com/vilbeyli/Pacman.
Integrating Rule-Based AI Tools into Mainstream Game Development 315

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 ]

In contrast to the previous constraint, if a Power-Pellet is eaten, candidate


moves that make Pac-Man closer to the nearest ghost are given a lower
penalty.
The points 2 and 3 are inherently more important than the first one: Pac-
Man is primarily forced to consider the behaviour of the nearest ghost rather
than to eat Pac-Dots. Notably, by just varying the associated levels the Pac-Man
behaviour can be easily inverted.

5 Conclusions and Future Work


In this paper we presented our first working prototype, based on the EmbASP
framework, which enables a game development environment to embed rule-based
reasoning modules into their assets.
In future work we aim to tighten the integration of reasoning based modules
in several respects: first, provide a standardized mapping strategy between game
object properties and logical assertions; second, standardize how numerical val-
ues are treated along the flow between the core module and reasoning engines;
third, provide the possibility of triggering events when external reasoners com-
plete their tasks, thus providing an actual asynchronous event handling approach
and, on the other hand, allow to encode events that trigger reasoning tasks on
demand; last, but not least, we aim to introduce appropriate means to deal with
real time requirements of typical of videogames, such as the introduction of time
out killers, the configuration of default actions in case of time out, etc.
Integrating Rule-Based AI Tools into Mainstream Game Development 317

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

You might also like