Difference between revisions of "Wesnoth AI Framework"

From The Battle for Wesnoth Wiki
(The [candidate_action] Tag: some wording changes)
m (Legacy Stages: minor rewordings)
Line 112: Line 112:
 
  [/stage]
 
  [/stage]
  
See the [[Lua_AI_Legacy_Methods_Howto#Using_the_Lua_Stage|legacy documentation]].
+
See the [[Lua_AI_Legacy_Methods_Howto#Using_the_Lua_Stage|Lua AI legacy documentation]].
  
 
==== Formula AI Side Formulas Stage ====
 
==== Formula AI Side Formulas Stage ====
Line 123: Line 123:
 
  [/stage]
 
  [/stage]
  
See the [[Formula_AI_Howto#Setting_up_the_Formula_AI_side_formulas_Stage|legacy documentation]].
+
See the [[Formula_AI_Howto#Setting_up_the_Formula_AI_side_formulas_Stage|Formula AI legacy documentation]].
  
 
==== Formula AI Unit Formulas Stage ====
 
==== Formula AI Unit Formulas Stage ====
Line 133: Line 133:
 
  [/stage]
 
  [/stage]
  
See the [[Formula_AI_Howto#Setting_up_the_Formula_AI_unit_formulas_Stage|legacy documentation]].
+
See the [[Formula_AI_Howto#Setting_up_the_Formula_AI_unit_formulas_Stage|Formula AI legacy documentation]].
  
 
==== Fallback stage ====
 
==== Fallback stage ====

Revision as of 18:52, 25 February 2016

Wesnoth AI Framework: A Composite AI

The Wesnoth AI framework is that of a composite AI. In other words, rather than the AI being one huge monolithic block, it is composed of a variety of different components which can be combined in a modular fashion (almost) at will.

In its default configuration, Wesnoth provides a predefined combination of these components in the RCA_AI. This configuration can be modified in a multitude of different ways. Due to the modular setup, it is not only possible to add new AI behavior to before or after the default AI moves, or replace the default AI entirely, but to insert new behavior routines in between existing components. This can be done either in addition to or as substitutions for existing components.

This page describes the AI components and their configuration in a general sense. Check out RCA_AI for a description of how these are assembled into the default AI and Wesnoth_AI for a collection of links to pages explaining how to customize and modify the existing AI or write a completely new AI.

Types of AI Components

The Wesnoth AI framework consists of the following types of components:

Stages

These are the fundamental building blocks of the AI. The AI is set up as a series of stages which are executed in the order in which they are put into the AI configuration. In other words, all actions of the first stage are executed first, then all actions of the second stage, etc.

While there are six different types of stages, only one of them, called the main_loop stage, is used in the default AI. In fact, this stage is sufficient for the vast majority of custom AI applications as well. We therefore only describe the main_loop stage and its sub-components in detail.

The Candidate Actions of the main_loop Stage

The main_loop stage consists of a number of candidate actions (CAs), each of which contains an evaluation and an execution function. For each AI move, the evaluation functions of all CAs are called, with each of them returning a score. The execution function of the CA with the highest score is then called, which results in an AI move (or in some cases, several moves) to be done. Then all CAs are evaluated again (including the one that was just executed) and the highest scoring CA is executed, until no CA returns a valid (non-zero) score any more.

Combining candidate actions designed for specific tasks such as attacks or village grabbing, it is possible to create entire AIs in a modular fashion. The CAs can be put together in whatever order and combination best suits the needs of a scenario. They do not need to be sorted in order of their evaluation scores and can even return variable scores which adapt to the situation on the map.

For a summary of the candidate actions of the default AI, click here.

Aspects and Goals

Aspects are configurable input parameters used to customize a candidate action's behavior. They are implemented as the the return values of functions that are called by the CAs of the RCA AI. They can also be called and taken into account by custom CAs, but that is not necessarily always the case. For example, some of the Micro_AIs respect some of the RCA AI aspects (such as avoid), but not all do.

The return values of the aspect functions can be defined and/or customized using both WML or Lua.

Goals are used similarly to aspects in that they also affect the behavior of the default AI. They can be used to customize the directions into which the default AI moves its units. However, both their syntax and their internal implementation (see below) are different from those of the aspects.

Check out the RCA AI page for details on which aspects and goals exist and how they are used by the default AI.

Engines

Engines assemble the configurations provided for the individual components of the AI into usable code and execute this code. In recent Wesnoth versions, you do not need to worry about the engines any more, they are always defined and active by default. It is sufficient to know that the components can be written in three different languages, C++, Lua and Formula AI (FAI). Information on how to do this is provided in the links of the Wesnoth_AI page.

It is worth noting that some stages are only set up for specific engines (see below), while the candidate actions, aspects and goals of the main_loop stage can be written in any of the three languages and can be combined arbitrarily.

AI Configuration

The Wesnoth AIs are assembled from configurations provided in several different places:

  • The default AI parameters in a variety of configuration files in the 'data/ai/' directory
  • Optional [ai] or [modify_ai] tags in era configurations
  • Optional [ai] or [modify_ai] tags in multiplayer faction configurations
  • Optional [ai] or [modify_ai] tags in scenario configurations

When the game is created, all these config snippets are merged into a single configuration stored in form of an [ai] tag. Then, aspects with the same ids are merged. During the game, whenever a [modify_ai] tag is acted upon (for example in an event), it is added into the existing AI configuration in the same way.

In the following, we describe the syntax of the [ai] tag. We first show the top-level keys and tags that can be used. Details on the use of the tags for the individual components follow after that, together with examples and links to more information for each component.

The [ai] Tag — Top-level Elements

AI configurations are set up and stored using the [ai] tag. It has the following top-level elements:

  • description="": (translatable string) This is the text displayed in the MP setup menu when choosing an AI for a computer player.
  • id="": (string) The id of the [ai] config snippet. This was originally meant to make it possible to remove [ai] configurations during a game. However, due to the merging process described above, this information is lost. The id is therefore meaningless in the current implementation of the AI.
  • version="": (string) [ Todo: Need to look into the C++ code for complete explanation ]
  • [stage]: Configuration of the stage(s) used in the AI. Several [stage] tags can be used, even several stages of the same type. They are executed in the order in which they are put into the [ai] tag.
  • [aspect]: Configuration of the aspects to be used by the AI. These are in composite aspect format.
    • Most aspects can also be provided using the simpler syntax explained at AiWML.
  • [goal]: Configuration of the goals to be used by the AI. These are in composite aspect format.
  • [engine]: This tag used to be needed for the Lua AI engine to set up the AI code. It is not needed any more. No information on its syntax is therefore provided below. If you need information on how this used to work, check out the legacy documentation.
  • formula="": (string) Formula AI code to be executed by the AI. We recommend not using this any more except for extremely simple AIs. See legacy documentation for more information.

The [ai] Tag — Stages

The [ai] tag can contain one or several stages defined in [stage] tags. If the AI configuration contains several stages, they are executed in the order in which they are put into the [ai] tag.

While there are six different types of stages available, all AI functionality possible in Wesnoth can be achieved using only a single one, the candidate action evaluation loop stage. In the default setup, this stage has id 'main_loop' and we generally simply refer to it as the main_loop stage.

Other stages exist mostly for legacy reasons from the times in the AI development when the main_loop stage did not exist yet. They are currently still functional and kept for backward compatibility reasons, but they are not maintained any more and might be removed in a future Wesnoth release cycle. We therefore recommend that all custom AI development is done using the main_loop stage only.

main_loop Stage Configuration

For the main_loop stage, the [ai] tag contains the following elements:

  • name: This must be set to 'testing_ai_default::candidate_action_evaluation_loop', so that the AI knows that this is the main_loop stage.
  • id: (string) This is the identifier of the AI. It is usually set to 'main_loop', but can take on any value. It only matters if you are planning to remove or modify the stage during a scenario.
  • engine=cpp: (string) This must be set to 'cpp', which is also the default value. It can therefore be omitted.
  • [candidate_action]: Candidate action configuration tags. See below for details.

Notes:

  • As an example, the configuration of the main_loop stage of the default AI is shown here.
  • Candidate actions can be using any of the three available engines. Just because the stage uses the cpp engine does not mean that the CAs need to do so as well. See below for an example.
  • While that should rarely ever be necessary, it is possible to include several main_loop stage definitions in the same [ai] tag.
    • This is done by putting several [stage] tags into the same [ai] tag
    • The AI engine then evaluates the CAs of the first stage in the usual way, until no CA returns a valid score any more.
    • After the first stage is finished, it moves on to the second stage and repeats the process, until all stages have finished. This ends the AI turn.
    • Obviously, this only makes sense if the sets of CAs of the different stages are not identical (although individual CAs might appear in several stages).

Recruitment Stage

There is a specialized configurable recruitment stage. However, this stage uses the recruitment functions of the old Default AI (the one used before the RCA AI existed). Anything that can be done with this stage, and more, can now also be done with the recruitment candidate action using the recruitment_instruction aspect. It should therefore never be necessary to use this stage and we do not describe it here.

Legacy Stages

Four other stages are also available. They do, however, not provide any functionality that is not also possible with the main_loop stage, only different ways of achieving the same functionality. We therefore only list them here for completeness, with brief examples and links to legacy documentation.

Note that both this documentation and the stages themselves might be removed at some point.

Lua stage

Executes Lua code by calling a function created inside a Lua engine. Whatever element is returned by the code in the Lua [engine] tag is accessible here as an upvalue. So, for example, (...):execute() calls whatever_is_returned_by_lua_engine:execute().

[stage]
    engine="lua"
    code= "(...):execute()"
[/stage]

See the Lua AI legacy documentation.

Formula AI Side Formulas Stage

This stage executes a given Formula AI side formula. Example:

[stage]
    engine=fai
    name=side_formulas
    move="write_your_formula_here"
[/stage]

See the Formula AI legacy documentation.

Formula AI Unit Formulas Stage

This stage takes no parameters and executes all Formula AI formulas which are attached to units. Example:

[stage]
    engine=fai
    name=unit_formulas
[/stage]

See the Formula AI legacy documentation.

Fallback stage

Fall back to another AI. Example:

[stage] 
    id=fallback
    name=testing_ai_default::fallback
    [ai]
       ai_algorithm=default_ai
    [/ai]
[/stage]

This example shows how to set up a stage to fall back to the old Default AI. This was useful when it was not clear yet how well the RCA AI would work and whether it would suddenly stop in the middle of the CA evaluation loop. There is really no reason to use a fallback stage any more these days, especially since it is also possible to simply add another stage to the AI configuration (without that having to be a fallback stage).

The [ai] Tag — Candidate Actions

The main_loop stage (and only this stage) can contain an assortment of candidate actions. They are evaluated and executed in the manner explained above. This section explains how to configure the [candidate_action] tag that goes into the [stage] tag of the main_loop stage.

All three engines can, in principle, be used to write custom CAs. However, only the use of Lua CAs is recommended for this purpose. Using the cpp engine has the obvious problem of requiring changes to the Wesnoth source code. Formula AI CAs are discouraged for the same reasons why we do not recommend using Formula AI stages any more.

The [candidate_action] Tag

The [candidate_action] tag can contains the following keys:

  • engine: This key is required for Wesnoth to know with which engine the CA is to be processed. Possible values are 'cpp', 'lua' and 'fai'.
  • id: The id of the CA is, in principle, optional. However, it is needed if you want to remove or change the CA after its initial definition.
  • name: The CAs of the default AI are identified by their name, making this a required key for the cpp engine. Check here for the names of the available default CAs. The key is optional for other engines.
  • max_score: The maximum score the CA evaluation function might return. This parameter is optional, but it is useful to reduce the AI move evaluation time. During each iteration through the evaluation loop, the main_loop stage only evaluates CAs with max_score larger than that of the highest-scoring CA found so far during this iteration. For that same reason, it also makes sense to order CAs by decreasing max_score in the [stage] tag, even though that is not technically necessary.
  • score: (cpp engine only) The score returned by the CA. Note that this means that cpp engine CAs always have fixed scores.

In addition, Lua CAs also contain the following keys:

  • location: File name, including path, of the AI code for the CA.
  • eval_parms, exec_parms: The parameters to be passed to the evaluation and execution functions of the AI. These need to be in form of a Lua table, without the outer curly brackets. They are passed to these functions as the second argument, cfg.

Notes:

  • There is also an old syntax for defining Lua CAs. However, this method requires the definition of a Lua engine using the [engine] tag. This is much less convenient than the current method without any advantages and will therefore be deprecated at some point in the future.
  • The current syntax was originally called external Lua CAs. However, as this has now become the standard, we drop the external and simply call them Lua CAs.
  • Formula AI CAs contain some additional keys. As we do not recommend using the fai engine any more, we do not describe this syntax here and refer to the Formula AI legacy documentation instead.

Here are examples of [candidate_action] tag uses:

Combat CA of the default AI:

[candidate_action]
    id=combat
    engine=cpp
    name=ai_default_rca::combat_phase
    max_score=100000
    score=100000
[/candidate_action]

Lua AI CA:

[candidate_action]
    engine=lua
    name=return_guardian_bob
    id=return_guardian_bob
    max_score=100010
    location="~add-ons/my_addon/lua/return_guardian.lua"
    eval_parms="id = 'Bob', return_x = 10, return_y = 15"
    exec_parms="id = 'Bob', return_x = 10, return_y = 15"
[/candidate_action]

Available Candidate Actions

  • cpp engine: The available CAs of the default AI are given here.
  • lua engine: The Micro_AIs are written using Lua CAs and can either be used as they are or as templates for creating your own CAs. They can be found here.
  • fai engine: As we said above, we do not recommend using the fai engine any more for creating custom CAs. However, if you want to do so, a few simple Formula AI CAs can be found in the legacy documentation here.

The [ai] Tag — Aspects

Aspects are read by the candidate actions of the default AI as parameters that can influence the AI behavior. Thus, for the most part, they only apply to CAs of the default AI. It is, however, possible to write Lua CAs which take them into account. For example, some of the Micro_AIs respect some of the default aspects.

A huge number of aspects is available for the default AI. They are therefore given their own page. Only the syntax and internal structure of the aspect configuration is shown here.

There are two types of syntax for defining aspects. The vast majority of aspects can be defined using a straight forward syntax of simply defining their values using keys or tags. These are usually referred to as standard, or sometimes simple, aspects. Examples are

[ai]
    time_of_day=dawn
    aggression=0.765
[/ai]

or

[ai]
    [avoid]
        terrain=W*
    [/avoid]
[ai]

For standard aspect syntax, several additional keys are available in addition to the aspect names themselves, time_of_day (as shown in the example above) turns, and ai_algorithm. These are also explained at AiWML.

By contrast, a few aspects cannot be defined in such a way and need a more complex structure. These are called composite aspects. AiWML points out when an aspect requires composite aspect syntax and how to do this. If it is not specifically stated, an aspect can be defined using standard aspect syntax.

This distinction between standard and composite aspect is only on the WML side though. Internally, the engine treats all aspects as composite and translates the standard aspect definition into a composite configuration snippet. As a result, any standard aspect can also be defined using composite syntax. That is useful, for example, if you want to remove a certain aspect at a later time. You can do that by assigning the aspect an id, which can then be used to remove the aspect.

Let's try to explain that using the aggression example from above. If we wanted to defined this using the composite aspect structure, it would look like this:

[ai]
    [aspect]
        id=aggression
        [facet]
            id=custom_aggression # Can be used to remove this facet later
            time_of_day=dawn
            value=0.765
        [/facet]
    [/aspect]
[/ai]

The most important part is that the composite aspect structure uses so-called facets, which contain the value of the aspect. If the aspect is a single scalar value, such as in this example, that value is assigned using the value key. If it is a tag, such as [avoid], the content of the tag is added inside a [value] tag. For example

[facet]
    engine=cpp
    id=custom_avoid
    name=standard_aspect
    [value]
        terrain=W*
    [/value]
[/facet]

That’s it for the most practical purposes, but there a few more parameters in the full aspect configuration:

[aspect]
    engine=cpp
    id=aggression
    invalidate_on_gamestate_change=no
    invalidate_on_minor_gamestate_change=no
    invalidate_on_tod_change_change=yes
    invalidate_on_turn_start=yes
    name=composite_aspect
    [facet]
        engine=cpp
        id=
        invalidate_on_gamestate_change=no
        invalidate_on_minor_gamestate_change=no
        invalidate_on_tod_change_change=yes
        invalidate_on_turn_start=yes
        name=standard_aspect
        time_of_day=dawn
        turns=
        value=0.765
    [/facet]
    [default]
        engine=cpp
        id=
        invalidate_on_gamestate_change=no
        invalidate_on_minor_gamestate_change=no
        invalidate_on_tod_change_change=yes
        invalidate_on_turn_start=yes
        name=standard_aspect
        time_of_day=
        turns=
        value=0.4
    [/default]
[/aspect]

[ Todo: this needs to be completed and cleaned up; are engine and name optional? ]

Thus, the full list of keys and tags inside the [aspect] tag is as follows:

  • engine:
  • id:
  • name:
  • invalidate_on_turn_start="yes": (boolean) If "yes", the value of this aspect is invalidated at the start of each AI turn.
  • invalidate_on_tod_change="yes": (boolean) If "yes", the value of this aspect is invalidated when the time of day changes (not working at the moment). This implies invalidate_on_turn_start.
  • invalidate_on_gamestate_change="no": (boolean) If "yes", the value of this aspect is invalidated on each game state change (on turn start, move, attack, recruit, etc.). This implies invalidate_on_turn_start.
  • invalidate_on_minor_gamestate_change="no": (boolean) If "yes", the value of this aspect is invalidated on each minor game state change (on set unit variable, etc). This implies invalidate_on_gamestate_change.
  • [facet] or [default]:

Some aspects can contain a significant amount of additional information. For example, for the attacks aspect, it contains a list of all possible attacks of the filtered attacker/target unit pairs. This list, originally established at the beginning of the AI turn, is not valid any more after the first attack has been performed and needs to be reset. The AI does this by 'invalidating' the aspect, after which the aspect information is read anew from the [ai] tags and the stored information (the list of attacks, in this case) is reevaluated.

Thus, information stored about aspects is reevaluated by default at each AI turn and when the time of day changes. It is not reevaluated after the game state changes (such as after an attack), which explains why 'invalidate_on_gamestate_change=yes' needs to be set explicitly for the attacks aspect.

In addition, the attacks aspect is also and exception in that it s definition does not contain a value key or [value] tag, but [filter_own] and [filter_enemy] tags.

Note: If unsure, the easiest way to see the full config for any given aspect (or any other AI component, in fact) is either in savefiles, or in-game by typing :inspect in debug mode and choosing 'ai config full' for a team.

Dynamic Lua Aspects

It is also possible define aspects dynamically in Lua. In that case, no [default] and [facet] tags are added to the configuration. They are replaced by the code key used to define the aspect. For the first example given at that link, the configuration looks like this

[aspect]
    code="local value = wesnoth.current.turn / 10.
    return value
    "
    engine=lua
    id=aggression
    invalidate_on_gamestate_change=no
    invalidate_on_minor_gamestate_change=no
    invalidate_on_tod_change_change=yes
    invalidate_on_turn_start=yes
    name=composite_aspect
[/aspect]

The [ai] Tag — Goals

Goals are essentially a special type of aspect, in that they are used to customize the behavior of the default (RCA) AI. However as described here, the default goals are not stored in configuration files. Similarly, custom goals are stored in a different format in the AI configuration than the aspects. Fortunately, this format is the same as that used for defining custom goals. See the examples given there for syntax. See also: Wesnoth_AI

The [ai] Tag — Engines

As we stated above, it is not necessary to define engines any more these days. Just for reference, we list the possible attributes of the [engine] tag here nevertheless.

  • id: (string) Optional id of the engine
  • engine=cpp: (string) The type of engine used to parse this [engine] config snippet.
  • name=cpp: (string) Name of the [engine], only relevant if engine=cpp.
  • code: If name=lua only. The Lua code to add to the engine. See the old method for settig up custom Lua CAs for details.


See also: Wesnoth_AI