O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Let’s build on our example and add some member sets to DistToTarget.
FuzzyModule fm;
FuzzyVariable& DistToTarget = fm.CreateFLV("DistToTarget");
FzSet Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close",
0,
25,
150);
FzSet Target_Medium = DistToTarget.AddTriangularSet("Target_Medium",
25,
50,
300);
FzSet Target_Far = DistToTarget.AddRightShoulderSet("Target_Far",
150,
300,
500);
These few lines of code create the FLV shown in Figure 10.26.
Notice how an instance of a
FzSet is returned by each of the set addition
methods. This is a proxy class that mimics the functionality of a concrete
FuzzySet. The concrete instances themselves are contained within
FuzzyVariable::m_MemberSets. These proxies are used as operands when
constructing a fuzzy rule base.
Designing Classes for Building Fuzzy Rules
This is undoubtedly the gnarliest part of coding a fuzzy system. As you’ve
learned, each fuzzy rule takes the form:
IF antecedent THEN consequent
where the antecedent and consequent can be single fuzzy sets or composite
sets that are the results of operations. To be flexible the fuzzy module must
be able to handle rules using not only the AND operator but also the OR
Fuzzy Logic | 445
From Theory to Application: Coding a Fuzzy Logic Module
Figure 10.26
and NOT operators and fuzzy hedges such as VERY and FAIRLY. In other
words, the module should be able to cope with rules like the following:
IF a1 and a2 THEN c1
IF VERY(a1) AND (a2 OR a3) THEN c1
IF [(a1 AND a2) OR (NOT(a3) AND VERY(a4)) ] THEN [c1 AND c2]
Within the final rule observe how the OR operator is operating on the result
of (a1 AND a2) and the result of (NOT(a3) AND VERY(a4)). In turn, the
AND in the second term is operating on the results of NOT(a3) and
VERY(a4). If that isn’t complex enough, the rule has two consequents
ANDed together. Obviously this example is well over the top and it’s
extremely unlikely a game AI programmer will ever require a rule such as
this (although it wouldn’t be too uncommon a sight in many fuzzy expert
systems), but it illustrates my point well — any operator class worth its salt
must be able to handle individual operands and compositions of operands
and operators identically. This is clearly another area where the composite
design pattern comes to the rescue.
To reiterate: The idea behind the composite pattern is to design a com-
mon interface for both composite and atomic objects to implement; when a
request is made of the composite it will forward it to one or more of its
children (see Chapter 9 if you require a more detailed explanation of the
composite pattern). In fuzzy rules, operands (fuzzy sets) are atomic objects,
and operators (AND, OR, VERY, etc.) are composites. Therefore, a class is
required that defines a common interface for both these types of objects to
implement. That class is called
FuzzyTerm and it looks like this:
class FuzzyTerm
{
public:
virtual ~FuzzyTerm(){}
//all terms must implement a virtual constructor
virtual FuzzyTerm* Clone()const = 0;
//retrieves the degree of membership of the term
virtual double GetDOM()const=0;
//clears the degree of membership of the term
virtual void ClearDOM()=0;
// method for updating the DOM of a consequent when a rule fires
virtual void ORwithDOM(double val)=0;
};
Because any type of fuzzy operation on one or more sets results in a com
-
posite fuzzy set, this small interface is adequate to define objects for use in
the construction of fuzzy rules. Figure 10.27 shows the relationship
446 | Chapter 10
From Theory to Application: Coding a Fuzzy Logic Module

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required