making a decision with several variables ??

BlitzMax Forums/BlitzMax Programming/making a decision with several variables ??

slenkar(Posted 2010) [#1]
how would you make a decision on which bread shop to go to with these variables

distance to shop
money in wallet
price of bread

if the shop is close the character wont mind paying a little bit extra
(e.g. your local corner shop is more expensive than that supermarket)

if the char has lots of money in his wallet he will be more likely to spend extra

if the price of bread is high the character will go to a more distant store if its cheaper there

if the price is higher than the money in the wallet, that store is out of the question

lets say there is a list of shops

how would you get blitzmax to make that decision?


Midimaster(Posted 2010) [#2]
each factor gets a wighting and at the end you make a multiplication of all factors:

price_factor= price_wighting*price
distance_factor= distance_wighting*distance

decission= price_factor * distance_factor

if you want to add a k.o. condition, substract a value before wighting, so the factor changes to minus in case of getting too big:

money_factor= money_wighting*(money-price)

also very often used: a quadratic wighting, which lets grow the factor exponetial:

distance_factor= distance_wighting* (distance ^ 1.2 )

you have to experiment with the values to find out the formula, which covers all situations and do not overwight one situation


AndrewT(Posted 2010) [#3]
Yep, Midimaster's got it. Also, it's useful to know whether you want to maximize or minimize each variable. For instance, you want to minimize distance to shop and price of bread, but (I think?) you want to maximize money in wallet. In that, you'll want to calculate the factors for each one as Midimaster said, then create a number that increases when a) distance decreases, b) money increases, and c) price decreases. Therefore, you'll want to put distance and price in the denominator and money in the numerator. That way, when money goes up, the number itself goes up (better choice), and when distance or price go up, the number goes down (worse choice).

Money Factor / (Price Factor * Distance Factor)

Examples:

1. Shop is 10 miles away, but bread is only $0.50, and you have $20 to buy a lot of bread!

20 / (0.5 * 10) = 20 / 5 = 4

Score: 4

2. Shop is only half a mile away, but the bread is $4.00 and you've only got $5.00.

5 / (4 * 0.5) = 5 / 2 = 2.5

Score: 2.5

Then it's just a matter of sorting the stores according to their overall score.

Of course, it'll work slightly differently once you put weights on each factor to determine how important it is.


Gabriel(Posted 2010) [#4]
Read up on fuzzy logic.


slenkar(Posted 2010) [#5]
thanks, good examples
i played with fuzzy logic but i didnt know if i was doing it right, choosing shapes like triangles,


Htbaa(Posted 2010) [#6]
Fuzzy Logic would indeed be a neat way to solve this. Makes your AI agents look a lot realistic as well. You can take a look at my module (htbaapub.fuzzy) which is a BlitzMax translation of the Fuzzy Logic chapter from the book Programming Game A.I. by Example.


slenkar(Posted 2010) [#7]
yeah i tried it, but i wasnt sure which shapes to use, and what the shapes meant


Brucey(Posted 2010) [#8]
That's why it is called "fuzzy" :-)


Htbaa(Posted 2010) [#9]
It can be a bit confusing at first so my advice is to read some stuff about it, particularly the book I just mentioned. It gives a good explanation about the shapes. All the shapes in a set really do is define your terms, such as low, medium and high. Depending on what the set is for some of the shapes may overlap each other (causing a gray, or fuzzy, area).


slenkar(Posted 2010) [#10]
ok thx ill check it out