Table 10.3

Set Representative Value Confidence

Undesirable 12.5 0.33

Desirable 50 0.2

VeryDesirable 87.5 0.67

Plugging these values into the equation gives the desirability as a crisp

value:

(10.16)

As you can see, this method has produced a value very close to that calcu-

lated by the more accurate but costlier to calculate centroid technique (and

it would have been closer had I not estimated some of the values in the

centroid calculation) and therefore, this is the one I’d advise you use in

your games and applications.

Well, that’s it! We’ve gone from crisp values (distance to target = 200,

ammo status = 8) to fuzzy sets, to inference, and back to a crisp value rep-

resenting the desirability of using the rocket launcher (83, 62, or 60.625

depending on the defuzzification method). If this process is repeated for

each weapon type a bot is carrying, it’s a simple matter to select the one

with the highest desirability score to be the weapon the bot should use

given its current situation.

From Theory to Application: Coding a Fuzzy Logic Module

It’s now time to see exactly how the classes required to implement fuzzy

logic have been designed and how they are integrated with Raven.

The FuzzyModule Class

The FuzzyModule class is the heart of the fuzzy system. It contains a

std::map of fuzzy linguistic variables and a std::vector containing the rule

base. In addition, it has methods for adding FLVs and rules to the module

and for running the module through the process of fuzzification, inference,

and defuzzification.

Fuzzy Logic | 437

From Theory to Application: Coding a Fuzzy Logic Module

12.5 0.33 50 0.2 87.5 0.67

0.33 0.2 0.67

72.75

1.2

60.625

Desirability

Desirability

´+´+´

=

++

=

=

class FuzzyModule

{

private:

typedef std::map<std::string, FuzzyVariable*> VarMap;

public:

//a client must pass one of these values to the defuzzify method.

//This module only supports the MaxAv and centroid methods.

enum DefuzzifyType{max_av, centroid};

//when calculating the centroid of the fuzzy manifold this value is used

//to determine how many cross sections should be sampled

enum {NumSamplesToUseForCentroid = 15};

private:

//a map of all the fuzzy variables this module uses

VarMap m_Variables;

//a vector containing all the fuzzy rules

std::vector<FuzzyRule*> m_Rules;

//zeros the DOMs of the consequents of each rule. Used by Defuzzify()

inline void SetConfidencesOfConsequentsToZero();

public:

~FuzzyModule();

//creates a new "empty" fuzzy variable and returns a reference to it.

FuzzyVariable& CreateFLV(const std::string& VarName);

//adds a rule to the module

void AddRule(FuzzyTerm& antecedent, FuzzyTerm& consequence);

//this method calls the Fuzzify method of the named FLV

inline void Fuzzify(const std::string& NameOfFLV, double val);

//given a fuzzy variable and a defuzzification method this returns a

//crisp value

inline double DeFuzzify(const std::string& key, DefuzzifyType method);

};

A client will typically create an instance of this class for each AI that

requires a unique fuzzy rule set. FLVs can then be added to the module

using the

CreateFLV method. This method returns a reference to the newly

created FLV. Here’s an example of how a module is used to create the

fuzzy linguistic variables required for the weapon selection example:

FuzzyModule fm;

FuzzyVariable& DistToTarget = fm.CreateFLV("DistToTarget");

438 | Chapter 10

From Theory to Application: Coding a Fuzzy Logic Module

Start Free Trial

No credit card required