AI evaluation system refactor proposal

billw2015

King
Joined
Jun 22, 2015
Messages
837
So having spent a fair amount of time fixing up code in the AI evaluation functions I think I have at least a basic grasp on how they are working, and as such would like to make a proposal for how they could be refactored to something hopefully better in every respect.
However I definitely don't claim a deep understanding of all the under the hood evaluation that is going on, so if I made some wrong assumptions here please correct me.
The current system (lets say for choosing what unit to build) is something like this:
Code:
total_value = 0;
if(some condition) {
    value_for_condition = evalute the condition to a single value
    total_value = total_value + value_for_condition
}
if(some other condition) {
    total_value = total_value * 2
}
etc
etc
This can be generalized to a function like:
Code:
class Condition
{
    virtual float add() { return 0; }
    virtual float mult() { return 1; }
}

class SomeCondition : Condition
{
    override float add() {
        if(some condition)
            return (evalute the condition to a single value);
        else
            return base.add();
    }
}

class SomeOtherCondition : Condition
{
    override float mult() {
        if(some other condition)
            return 2;
        else
            return base.add();
    }
}

conditions = { SomeCondition(), SomeOtherCondition() };

total_value = 0

foreach condition in conditions
{
    total_value = total_value + condition.add()
    total_value = total_value * condition.mult()
}

Obviously this is rough pseudocode, but it gets across the idea.

/edit

To expand a bit on what this improves over the current system:
- It formalizes how a selection condition is defined. This makes it a lot easier to understand what the AI is doing and how it works. Massive functions full of if statements are notorious for being hard to comprehend. Without reading every single if statement you can't be sure you understand what it is intended to do. But a for loop that does add and multiply on a set of encapsulated conditions makes it easy to be sure you know how it works (at a high level).
- It separates the logic that generates a score from conditions, from the logic of the conditions themselves, making it easier to see where one ends and the other begins
- It makes it easier to data drive this behaviour via XML
- It makes it easier to perform general optimizations, e.g. reordering the evaluations such that the fastest ones happen first, and a multiplication by 0 can skip the rest of the calculations.
 
Last edited:
Top Bottom