PDA

View Full Version : AI fuzzy logic problem - how do make this into one value?



questioner1
July 19th, 2009, 01:36 PM
I am using fuzzy logic to calculate the desirability of certain strategic spots in a game.
I have 4 vars:

//in Actionscript 3.0
var mNotStrategic:Number
var mStrategic:Number
var mVeryStrategic:Numbe
var mUnbeatable:Number

//in C++:
double mNotStrategic
double mStrategic
double mVeryStrategic
double mUnbeatable

all these vars are ofcourse from 0 to 1.
and I want to calculate the desirability:
var mDesirable:Number

I want to use mDesirable in other fuzzy logic expressions.
So i don't want to defuzzify it. it has to be a number between 0 and 1.

How do i do this? I can't take the avarage of the four strategic vars, because, obviously a high mNotStrategic should cause mDesirable to be low. but if i took tha avarage, then it being VERY not strategic, would also make it very desirable.

I also want to be able to change the weight of those vars. so that the difference between mStrategic and mVeryStrategic, matters less, than the difference between mNotStrategic and mStrategic.

I hope i've made sense.

Thanks in advance.

EDIT:
these are the calculations. i dont know if you need them to answer my question. I'll just post them to make sure.


var mWeak:Number = fuzzyReverseGrade (nrShips, 1, 2);
var mStable:Number = fuzzyTriangle (nrShips, 1, 3, 5);
var mStrong:Number = fuzzyTriangle (nrShips, 4, 6, 9);
var mVeryStrong:Number = fuzzyGrade (nrShips, 7, 10);
//above calculates the value of the amount of ships.

var mClose:Number = fuzzyReverseGrade (dist, , );
var mNearby:Number = fuzzyTrapezoid (dist, , , , );
var mFar:Number = fuzzyTrapezoid (dist, , , , );
var mVeryFar:Number = fuzzyGrade (dist, , );
//the above calculates the value of the distance from the player (i haven't filled in the constants yet.)


var mNotStrategic:Number = OR(AND(mWeak,mClose), AND(mWeak,mNearby), AND(mVeryFar, OR(mWeak,mStable,notVery(mStrong))) );
//above means that a place is not strategic if it is weak and close or weak and nearby or very far and (weak,stable or kind of strong)

var mStrategic:Number = OR(AND(mStable,mNearby), AND(mWeak,mFar), AND(mStrong,mFar), AND(very(mVeryStrong),mVeryFar) );
//meaning the place is strategic if it's stable and nearby or weak and far or strong and far or verystrong and very far
var mVeryStrategic:Number = OR(AND(mStrong, OR(notVery(mClose), mNearby)), AND(mVeryStrong, OR(notVery(mNearby),vFar) );
//etc.
var mUnbeatable:Number = OR(AND(mVeryStrong,OR(mClose,very(mNearby))), AND(very(mVeryStrong), mNearby) );

Someone told me, that i might not need fuzzy logic. I wouldn't know how i would have to do something like this without fuzzy logic. But I'm open to suggestions.

TOdorus
July 20th, 2009, 12:22 AM
For starters, it's best to define everything positive. It avoids double negatives: that is very "not unstrategic". Something is strategic. Also having multiple scales (on a scale of 0 to 1 in your case) which make up the same scale (strategicness) makes no sense. Your scales exclude each other (it won't be strategic and very strategic at the same time), so they are actually items on the same scale instead of seperate scales. For example:

<= .25 = unstrategic
.25 < x < .5 = strategic
.5 < x < .9 = very strategic
>=.9 = unbeatable.
What would make more sense is something like, his offensive power, your offensive power, his defensive power, your defensive power. Those are scales which don't exclude each other. If you would combine those scales you could create another scale like for example Advantage or offensive ratio, or defensive ratio.

questioner1
July 20th, 2009, 03:55 AM
I got that technique from this book:
AI for Game developers by David M. Bourg.

Do you know what fuzzy logic is?

TOdorus
July 20th, 2009, 07:11 AM
Do you know what fuzzy logic is?

:)

Analog logic


//in Actionscript 3.0
var mNotStrategic:Number
var mStrategic:Number
var mVeryStrategic:Numbe
var mUnbeatable:Number

//in C++:
double mNotStrategic
double mStrategic
double mVeryStrategic
double mUnbeatable


Now do you know what a boolean is?


I'll repeat my point again. You variables are exclusive. You're actually using bitwise logic here. Fuzzy logic is analog: you don't express it as notstrategic, very strategic etc. You express it as .4 strategic, .7568 strategic. You've already defuzzified it.

questioner1
July 20th, 2009, 09:16 AM
"Now do you know what a boolean is?"

I didn't mean that arrogantly btw, the question if you know what fuzzy logic is. it's just that Other people answerd my question, and i later found out they didn't know what it is.
Boolean is 0 or 1, nothing in between, ofcourse i know that.

"I'll repeat my point again. You variables are exclusive."
By exclusive you mean Boolean? An example of my vars could be:

var mNotStrategic:Number = 0;
var mStrategic:Number = 0.2;
var mVeryStrategic:Number = 0.6;
var mUnbeatable:Number = 0;

Which would mean that it is between Strategic and veryStrategic, balancing more towards VeryStrategic.
I didn't made this technique up, i got it from a book: AI for gamedevelopers by David. m. bourg.
Why this is handy, i think, is because you can then go :

mUnStrategic = OR( AND(mStrong, mFar), AND(mWeak, mClose) );
mStrategic = OR( AND(mStrong, mClose), AND(mWeak, mFar) );



"You're actually using bitwise logic here. Fuzzy logic is analog: you don't express it as notstrategic, very strategic etc. You express it as .4 strategic, .7568 strategic. You've already defuzzified it."
I assume bitwise logic is boolean logic? because i dont know the term, and i've just googled it and nothing matching came up.
I understand what you're saying, but how do i solve my problem with your method? with only one var?
Because i need to say, that close and strong is good, close and weak not, but weak and far is good and strong and far not. so it's not linnear: far is better Strong is better.

BoppreH
July 20th, 2009, 10:07 AM
questioner, your variables are exclusive because you can have "mUnbeatable = 1" and "mNotStrategic = 1", but it does not make sense in the context.

Your "mX" variables should be constants defining the minimum value before the "strategic" fuzzy variable can be considered having "X" property. Example:


COLD = 0
WARM = 0.3
HOT = 0.6
temperature = 0.35 //this is the main variable. This is what will change over time

if (temperature > HOT) {
trace ("is hot")
} else if (temperature > WARM) {
trace ("is warm")
} else if (temperature > COLD) {
trace ("is cold")
}

//Now you want to know if it's warm enough:
if (temperature > WARM) {
trace ("it is warm")
// warm code
if (temperature < HOT) {
trace ("but it's not hot")
}
}

TOdorus
July 20th, 2009, 10:15 AM
"Now do you know what a boolean is?"

I didn't mean that arrogantly btw, the question if you know what fuzzy logic is. it's just that Other people answerd my question, and i later found out they didn't know what it is.
Boolean is 0 or 1, nothing in between, ofcourse i know that.

Yeah, my actual reaction was: and how is this constructive? I took it as a "dead-end" kind of reply. I've actually never used fuzzy logic in games, but it's applied in the statistics of social siences and I've got my far share of experience there.

EDIT: btw my boolean remark was to make the point, that your scales aren't actually scales, but booleans.


"
"I'll repeat my point again. You variables are exclusive."
By exclusive you mean Boolean?

Exactly. I believe the actual terminology is logical. Logic is: if this, then that. But it also means categorisation: if something is this, then it isn't that. Fuzzy logic is fuzzy, because it isn't really clear if it is either this OR that. I used the term bitwise logic, because that's how it's tested in computers (true or false needs only one bit), but it should actually be called just "logic" and fuzzy bieng an exception to the rule.

Nice how confusing terminology can be.





I understand what you're saying, but how do i solve my problem with your method? with only one var?
Because i need to say, that close and strong is good, close and weak not, but weak and far is good and strong and far not. so it's not linnear: far is better Strong is better.

so you've got distance and strength (far/close, strong/weak are also exclusive)? The stronger someone gets, the closer he wants to be so in fuzzy logic this is:

strategic = strength / distance

EDIT: this gives you an unstanderdized value btw [0, ->]

questioner1
July 20th, 2009, 11:45 AM
Yeah, my actual reaction was: and how is this constructive? I took it as a "dead-end" kind of reply. I've actually never used fuzzy logic in games, but it's applied in the statistics of social siences and I've got my far share of experience there.

EDIT: btw my boolean remark was to make the point, that your scales aren't actually scales, but booleans.
How are my values boolean? they range from 0 to 1. thats the opposite of boolean.
I don't think i know what you mean.




Exactly. I believe the actual terminology is logical. Logic is: if this, then that. But it also means categorisation: if something is this, then it isn't that. Fuzzy logic is fuzzy, because it isn't really clear if it is either this OR that. I used the term bitwise logic, because that's how it's tested in computers (true or false needs only one bit), but it should actually be called just "logic" and fuzzy bieng an exception to the rule.

Nice how confusing terminology can be. ok.... i thought u meant boolean, it's just that my vars aren't boolean, so i don't get your point.




so you've got distance and strength (far/close, strong/weak are also exclusive)? The stronger someone gets, the closer he wants to be so in fuzzy logic this is:

strategic = strength / distance

EDIT: this gives you an unstanderdized value btw [0, ->]strategic = strength/distance;
with this equation, if strength is high and distance is high, strategic is normal (lets say 1?)
if strength is low and distance is high strategic is small.
if strength is low and distance is low, then strategic is normal.
Which is wrong. like i said: if strength is low, the distance must be high for the point to be strategic.
if strength is high, the distance must be low for the point to be strategic.
only the latter is true of this function.

and also, i want a value between 0 and 1, for other fuzzy logic equations.
I don't want to defuzzify it, i simply want to use it for other fuzzy equations.


I don't understand what i'm doing wrong in your opinion.

fuzzy logic is based on having more vars for one value, so that you can then say:
mLessStrategic = OR(AND(mWeak,mClose), AND(mStrong, mFar));
mStrategic = OR(AND(mStrong,mClose), AND(mWeak,mFar));
meaning, if the point is either weak and close, or strong and far, the point is not strategic, and if either strong and close or weak and far the point IS strategic.

Again, how can you do this without the four vars?

by Boppreh:
questioner, your variables are exclusive because you can have "mUnbeatable = 1" and "mNotStrategic = 1", but it does not make sense in the context.No, thats not possible. it will never be strategic and not strategic at the same time.
only notStrategic 0.2and strategic0.6 for example. veryStrategic and Unbeatable will be both 0 in that case.
I mean. isn't the whole point of fuzzy logic, that you can ask questions like that, and that it's not boolean. if you would have one value for strategicness, and then pick a crossoverpoint that it jumps from weak to strong, that wouldn't be fuzzy anymore, that would be boolean.

TOdorus
July 20th, 2009, 12:38 PM
fuzzy logic is based on having more vars for one value, so that you can then say:
mLessStrategic = OR(AND(mWeak,mClose), AND(mStrong, mFar));
mStrategic = OR(AND(mStrong,mClose), AND(mWeak,mFar));
meaning, if the point is either weak and close, or strong and far, the point is not strategic, and if either strong and close or weak and far the point IS strategic.

Questioner really need to look up the term fuzzy logic (http://en.wikipedia.org/wiki/Fuzzy_logic). It has nothing to do with being a scale formed out of other items or not. It means that something can take up a range of values instead of being either something and not the other (categorization). You just don't categorize, you just give it a score.

With your definitions you categorize. You may define them as numbers and say that's that, they're not boolean, but you do actually use them that way, so I consider them boolean. Let's say something is unbeatable. The logical result is that it can't be unstrategic or strategic. So you're using them as a boolean, one result excludes the other. Look at BoppreH and understand it. You may understand the example, but he's trying to convey the concept of fuzzy, not how you could derive that it's warm.

A point is not unstrategic or strategic in fuzzy logic. Fuzzy logic gives it a score of strategicness. Like the temperature example of BoppreH, fuzzy is just the temperature. We, as humans, want to categorize so we can make choices. It doesn't matter to us if it is 23 or 30 degrees celcius ,it matters if we need to wear the warm weather clothes, or the hot weather clothes.


//Using numbers for variables doesn't make it fuzzy

var man:Number
var woman:Number
//
var blond:Number
var brown:Number
var ginger:Number
//
var strong:Number
var weak:Number
//
var smart:Number
var dumb:Number

glosrfc
July 20th, 2009, 12:46 PM
What makes you think that a Boolean must be "0 or 1, nothing in between"?

myVar = 0.00001;
if (myVar) {
trace("myVar is true");
}
Without strict data-typing, the above code will return a value of true!

A Boolean value represents one of two states: true or false. A Fuzzy value represents one of many states: not only true or false...but also maybe, and perhaps, and possibly, and once in a blue moon; and also frequently, often, everyday; and so on.

However Fuzzy values are still just another subset of standard Boolean values that are expanded using the AND, OR and NOT operators. SWITCH/CASE statements are also able to perform fuzzy logic as are sensible use of IF and IF/ELSE statements.

What makes something Fuzzy isn't the value(s) (which can still be 0 or 1) but the rules you use to interpret the value(s). For example, consider the following statements:
Cats have tails
Cats eat fish

Does this mean that every animal with a tail is a cat? Of course not a dog has a tail. So you can fuzzify your statements using:
if (has tail AND eats fish) = Cat

However, otters also eat fish so you have to change your rules:
if ((has tail AND eats fish) NOT (lives in water)) = Cat

But the lynx, bobcat, and Manx cats don't have a tail so again you change your rules:
if ((has tail AND eats fish) NOT (lives in water) OR (type == lynx OR type == bobcat OR type == Manx) = Cat

The values for tail, fish, water, and type can all be Boolean values of 1 or 0. What really matters are the rules you use to determine the subsets of those values.

The following might be a useful read:
http://blog.peltarion.com/2006/10/25/fuzzy-math-part-1-the-theory/

TOdorus
July 20th, 2009, 01:09 PM
What makes you think that a Boolean must be "0 or 1, nothing in between"?

It is true or false. 0 or 1. The name bitwise operator doesn't express it any more clearly then that.



ActionScript Code:

myVar = 0.00001;</p>
<p>if (myVar) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;trace("myVar is true");</p>
<p>}



Without strict data-typing, the above code will return a value of true!


Then everything is a boolean? The evaluation HAS to produce either true or false. The engineers at Adobe choose to use the true value as a default when an error occurs. They might as well have chosen false.




However Fuzzy values are still just another subset of standard Boolean values that are expanded using the AND, OR and NOT operators. SWITCH/CASE statements are also able to perform fuzzy logic as are sensible use of IF and IF/ELSE statements.

Actually that's just logic like the Greeks used it (http://en.wikipedia.org/wiki/Syllogism) . If this is known to be true then a conclusion follows. The same applies to your cats and dogs example. The main thing about fuzzy is that is analog and not digital. It has no true or false, it has a range. If you start chopping that range up you're defuzzying it, digitalising it. I do not know where the idea that fuzzy logic is using multiple statements came from, but it just isn't fuzzy.

glosrfc
July 20th, 2009, 01:31 PM
Then everything is a boolean? The evaluation HAS to produce either true or false. The engineers at Adobe choose to use the true value as a default when an error occurs. They might as well have chosen false.
And the default value has changed across different versions of the Player. The point is that questioner was under the impression that ONLY values of 0 or 1 can be Boolean whereas what we're both saying is that intermediary values can also be true or false too.



Actually that's just logic like the Greeks used it (http://en.wikipedia.org/wiki/Syllogism) . If this is known to be true then a conclusion follows. The same applies to your cats and dogs example. The main thing about fuzzy is that is analog and not digital. It has no true or false, it has a range. If you start chopping that range up you're defuzzying it, digitalising it. I do not know where the idea that fuzzy logic is using multiple statements came from, but it just isn't fuzzy.
Unfortunately, computers only deal in digital outcomes so any analogue fuzziness must ultimately be broken down into true or false results or programs simply wouldn't work. The assumption that a value falls inside a range, and therefore must be fuzzy, is wrong. At some point within that range, you determine a change of state from true or false...depending upon the rules that you apply.

For example, the water temperature may be hot or cold or warm. Your rules predetermine that anything above 40 is warm, and anything above 80 is hot. Any other temperature is cold. The temperature must ultimately fall into one of the subsets of warm, cold, or hot...fuzzy rules merely state what those points are. But someone else's rules may be different, e.g. 39.7 = warm, 63.4 = hot.

As for where multiple (presumably you mean conditional) statements fit in, I'd suggest that you read the following in conjunction with the other link I provided:
http://www.austinlinks.com/Fuzzy/overview.html

TOdorus
July 20th, 2009, 04:15 PM
And the default value has changed across different versions of the Player. The point is that questioner was under the impression that ONLY values of 0 or 1 can be Boolean whereas what we're both saying is that intermediary values can also be true or false too.

Unfortunately, computers only deal in digital outcomes so any analogue fuzziness must ultimately be broken down into true or false results or programs simply wouldn't work. The assumption that a value falls inside a range, and therefore must be fuzzy, is wrong. At some point within that range, you determine a change of state from true or false...depending upon the rules that you apply.

Fuzzy logic to me is making decision based on analog data instead of boolean data.

Ultimately it's always true or false, yes, as that's the whole point of a decision. The moment you start categorizing however, is when you reach that stage. Usually with fuzzy logic, is that people tend to categorize before the actual decision making. Like the example of your length and your bodyweight. You have 3 categories of tallness and 3 categories of heavyness. If you're in the highest heavyness category and the lowest tallness category then you're overweight. The same thing is expressed in BMI. Only there it isn't a categorisation of your weight and tallness, it's a ratio of the two. You're not overweight, ok or underweight, your attribute is a number, which doesn't judge what it is. It just expresses it. Then you look up that number to tell if you're ok, over- or underweight.

When you start categorizing, you start to defuzzify. You make the fuzzy data boolean, the analog data becomes digital data. That already is a decision as it is an evaluation. You're deciding in what category it should fall. Now you could argue that computers use bits so it's always digital, but that ignores the fact that you can do fuzzy logic with an electrical circuit. I may have chosen the wrong wording with "range" as I consider a valid range to be [<-, ->]. What I mean is that it can take any value, but as you normally standerdize it to a range of [0,1], you fit it into a range. Fuzzy logic doesn't have to be standerdized to work.

To illustrate: to me an A* algorithm is fuzzy logic. It gives every node on it's list a score and picks the lowest score. How good a node is that? It can't tell as that's fuzzy. It's expressed as a score with no limit. Then it picks the lowest score and with that it defuzzifies it, leaving the world of fuzzy logic behind to continue in the world of boolean logic.