How to manage percent-based AI actions

I’m watching some time now about how a programmer can imitate an AI solution based on the percentage of actions for the final tactically-like games (strategy game).

Say, for example, that the AI ​​symbol has the following actions:

Attack 1: 10%
Attack 2: 9%
Magic: 4%
Move: 1%

All this is far from 100%

Now, first, having an array with 100 empty slots, the attack will have 10 slots, attack 2 9 slots in the array. By combining the random, I could perform an action. My problem here is that it is not very effective or seems not so. It is also important what should I do if I get into an empty slot. Should I calculate for each character all actions based on 100% or determine, possibly, the "default" action for all?

Or maybe there is a more efficient way to see all this? I think this percentage is the easiest way to implement AI.

+4
source share
3 answers

The best answer that I can come up with is to make a list of all the possible moves that you want the character to have, give each one a relative value, and then scale all of them to 100%.

EDIT:

For example, here are the three steps that I have. I want attack and magic to be equally likely, and run away, to be half as likely to attack or use magic:

  • attack = 20
  • magic = 20
  • flee = 10

This adds up to 50, so dividing each by this amount gives me a fractional value (multiply by 100 for a percentage):

  • attack = 0.4
  • magic = 0.4
  • flee = 0.2

Then I will make a list of cumulative values ​​from this (i.e., each record is the sum of this record and everything that came before it):

  • attack = 0.4
  • magic = 0.8
  • flee = 1

Now create a random number between 0 and 1 and find the first entry in the list that is greater than or equal to that number. This is the step you are taking.

+4
source

No, you just create thresholds. One easy way:

0 - 9 -> Attack1

10 - 18 β†’ Attack 2

19 - 22 β†’ Magic

23 β†’ Move

Something else β†’ 24-99 (you need to add up to 100)

Now create a random number and change it to 100 (so num = randomNumber% 100) to determine your action. The better the random number, the closer to the correct distribution you get. So, you take the result and see which category it falls into. You can really make it even more effective, but it's a good start.

+2
source

Well, if they do not all make up to 100, they are not percentages. It does not matter. you just need to find out the relative probability of each action. To do this, use the following formula ...

prob = value_of_action / total_value_of_all_actions 

This gives you a number from 0 to 1. If you really want a percentage, not a fraction, multiply it by 100.

here is an example:

 prob_attack = 10 / (10 + 9 + 4 + 1) = 10 / 24 = 0.4167 

This means that the attack is selected 41.67% of the time.

you can generate thresholds as indicated in other answers. And use a random number from 0 to 1 to select an action.

+1
source

All Articles