# Imperfect A.i.

128 replies to this topic

### #121 Hamster

Hamster

GMC Member

• New Member
• 31 posts

Posted 22 September 2010 - 06:45 PM

Imperfect AI...

I just read all 6 pages on this subject and my view of an imperfect AI is a combination of previous posts.

I guess we all can agree that different types of games require different kind of (imperfect) AI.
Also, we can all agree that as an AI gets smarter, it would make less and less mistakes.

Let's say that the AI has 3 levels:
Level 1 - Perfect AI
Level 2 - Normal AI
Level 3 - Dumb AI

Platform
Considering the example of the AI trying to jump over a gap.

Let's say that the AI can jump 100px (base_jump_distance), and the gap that the AI has to jump is 105px (actual_gap_distance).

To create an imperfect AI, I would first like to introduce 2 things:
1) Estimation of distances
2) Timing

Estimation of distances:
For the AI to consider to jump the gap, it will first need to estimate if it's able to make the jump.
The AI needs to estimate two things:
1) How far can I jump? (jump_distance = base_jump_distance +- random(10)*((2*ai_level-2)/4))
2) What is the distance of the gap? (gap_distance = actual_gap_distance +- random(10)*((2*ai_level-2)/4))

(Instead of the random() function, we could also use a Gaussian Random Number for more realism)

Once the AI has both estimations, a simple check will make the AI jump (or not):

if (jump_distance >= gap_distance)
{
jump_over_gap = true
}
else
{
jump_over_gap = false
}

For the "Dumb AI", the values would range from:
jump_distance = 90px to 110px
gap_distance = 95px to 115px

For the "Normal AI", the values would range from:
jump_distance = 95px to 105px
gap_distance = 100px to 110px

For the "Perfect AI", the values would be:
jump_distance = 100px
gap_distance = 105px

As you can see, the Perfect AI would never make the jump, because his jump_distance is less than the gap_distance.
For the Normal AI, there's a change that it will try to jump (overlap: 105px - 100px = 5px)
For the Dumb AI, there's a bigger change that it will try to jump (overlap: 110px - 95px = 15px)

Timing:
For a human, if he could jump 100px and he needs to jump over a gap of 100px, timing is clearly essential.
If the player is only 1px too early in his timing, he would not make the jump. If he's 1 px too late...same story!

It would be unfair if the AI would have a perfect timing all the time...
So we will introduce timing for our AI.(You can decide if you want to base the timing in seconds of in pixels, I will use pixels in this example:)

This time, the AI needs to jump over a gap of 92px, and the algorithm above said "JUMP!".
Clearly, the AI is capable to jump over this gap...but what if he jumps to early? Or to late?

If the AI would jump within 8px from the edge, it would make it to the other side.
If the AI would start to jump 9px from the edge...bye bye!

So let's take the middle of our "jump range":
jump_range = (actual_gap_distance-base_jump_distance)/2

if (distance_to_edge < jump_range +- random(jump_range+jump_range*((ai_level-1)/4))
{
jump_now = true
}
else
{
jump_now = false
}

In this case the Perfect AI would always be in the safe range to jump, the Normal AI would jump within a range of -1px to 9px, the Dumb AI would jump within a range of -2px and 10px. Negative timing in pixels? Yes, that's when the AI is too late to jump.

I hope this post is usefull to anyone...
• 0

### #122 Medusar

Medusar

GMC Member

• GMC Member
• 1228 posts
• Version:GM:Studio

Posted 24 September 2010 - 04:09 PM

"Negative timing" is something one should be very careful with. If somebody jumps in real life, chances of him jumping too early are bigger than jumping too late. A player generally tends to jump a bit early (jumping 1px too early usually still gets him across, 1px too late doesn't). An NPC jumping too late simply looks like it doesn't even jump at all. Looks foolish, decreases realism. (Might even be called a "bug" by some players) So your system sounds like a good one, but I would add an offset to decrease the chance of the AI jumping too late.
• 0

### #123 Hamster

Hamster

GMC Member

• New Member
• 31 posts

Posted 24 September 2010 - 06:30 PM

"Negative timing" is something one should be very careful with. If somebody jumps in real life, chances of him jumping too early are bigger than jumping too late. A player generally tends to jump a bit early (jumping 1px too early usually still gets him across, 1px too late doesn't). An NPC jumping too late simply looks like it doesn't even jump at all. Looks foolish, decreases realism. (Might even be called a "bug" by some players) So your system sounds like a good one, but I would add an offset to decrease the chance of the AI jumping too late.

The "negative timing" may seem a bit weird, it will indeed look like the AI never tries to jump. On the other hand, I can't recall the number of times I pushed the jump button only a fraction too late in a platformer. I played Flashback on the PC (a long time ago), and IIRC there were several (running) jumps where you had to have a perfect timing.

We could add an additional check to the code to see if the timing is too late. If so we could play an animation of the AI trying to jump (or trying to stop in time)

• 0

### #124 mn_chaos-

mn_chaos-

GMC Member

• New Member
• 80 posts

Posted 26 November 2010 - 11:07 PM

take in example half life 2.

Obviously, in the face of honest mistakes, we should take in account that peoples aim is not perfect.
if combines had perfect aim, they would be impossible to defeat.
Valve attempted imperfect ai a bit too much when decreasing a combine soldier aiming capacity.
(i actually used some npc spawners, 10 of them, to be exact, to spawn combines with ar2s randomly. I survived against them for 5 mins with a crowbar!)

Yet they did this so that the rebels, who have a much poorer outfit of weapons and better aim, could be able to own combines in your face while fighting the combine.
They still had to make inaccurate AI for rebels, just not as bad. They still overdid the inaccurate AI for rebels, so they gave combines rediculously low health.

a combine elite (40) < rebel (70-100) < you (100+100>x"hazard suit")

so valve provides us with the most overcompensated ai i have seen in my life. The only advantage I see is that zombies have better health to accuracy ratio, zombie (100) f zombie (50-75) poison zombie (150-175) It would have been better if they faulted with too accurate ai, but changed health higher

These values are direct source from half life 2 using cheat engine.

The point is, you can't get too enthusiastic with realistic ai. I have them same problem in 2-d shooters when i calculate accuracy with rand()

lesson learned valve

Edited by mn_chaos-, 26 November 2010 - 11:09 PM.

• 0

### #125 snowyowl

snowyowl

GMC Member

• New Member
• 615 posts

Posted 02 December 2010 - 11:19 PM

I think it's important to stress the word "realistic" in "realistic flaws". When trying to determine how badly the AI should fail, watch a human play the game and try to model his behaviour. Not just add gaussian blur to everything the AI does. That feels strange in some games, as though the AI is letting me win.
• 0

### #126 Gamer3D

Gamer3D

Human* me = this;

• GMC Member
• 1589 posts
• Version:GM8.1

Posted 06 December 2010 - 03:56 AM

I think it's important to stress the word "realistic" in "realistic flaws". When trying to determine how badly the AI should fail, watch a human play the game and try to model his behaviour. Not just add gaussian blur to everything the AI does. That feels strange in some games, as though the AI is letting me win.

With that in mind, it is important to consider not only why players fail, but why they succeed.

Why players fail:
• Player is not used to the game.
• Level design requires perfect timing.
• Bots have perfect aim.
Why players succeed:
• Players learn. Players don't die to the same tricks every time.
• Players are tactical. They hide in corners to avoid being hit by surprise and put danger between them and the enemy.

• Bots have perfect vision. Most bots ignore camouflage, vision-obscuring objects (bushes), and lack of movement (Seriously. Humans RARELY notice things that stay still).
• Bots have perfect aim. Players respond to the screen with imperfect reflexes and imperfect input. Bots just aim along the vector between them and you.
• Bots have perfect reflexes. Players see, then it takes them a fraction of a second to react. Average human reaction time is 0.2 seconds.
• Bots don't learn. Players don't run into the same trap more than a few times.
• Bots don't strategize. See above.

So... Here's what to do for bots:
• Decrease chances of bots seeing you as distance increases, objects (even partially transparent ones) are placed in the way, and players stay still. The first and third may be ignored if the player has already been noticed.
• Give bots reaction time. Take the player's position 0.15 seconds before (tweaked for uncommonly good reflexes) and work based on that.
• Give bots GOOD AIM. Don't make them miss by a random amount (unless you're modelling an autistic child with an AK-47), instead making them swing their gun toward you over a period of a few steps (1 to 3 steps is usually fine). Since they are aiming based on where you WERE, make them try to predict where you WILL BE (linear prediction is fine). This will make them seem both competent and defeatable.
• Make the bots good at strategizing. Don't make them run out in front of you unless it's a group rush, stay behind cover except for when they all attack at once, close doors to stop pursuers, etc.
• Let's face it. You are not good at making bots learn. So update your game every once in a while with fixes to some of the common tactics used against bots. (For example, players in L4D2 No Mercy Rooftop Scavenge will take a charger and run people and bots clear off the roof. The developer's response should be to: A: Make the charger bots try to do the same. B: Make the survivor bots try to avoid it)

Now there are some variables to tweak in this. Some bots can be better at seeing you. Some may have better or worse reaction time. Some may be retarded (though it's not recommended).

With that advice, you should be able to make a seemingly competent, but beatable AI.

One last thing. If the bots have no premade tactic for a given situation, working as a group will give the impression that they had a strategy anyway.
• 0

### #127 csanyk

csanyk

GMC Member

• GMC Member
• 128 posts
• Version:Unknown

Posted 05 October 2012 - 06:25 PM

AI won't really be Intelligent until it can learn, discover and implement novel strategies/tactics on its own.

The best way to do this that I've been able to think of is to have the program remember moves that have been played against it by human opponents, and store them in a data structure, and then randomly choose a move when the appropriate circumstance arises again in the game and they are in the position to play that move. By keeping track of the outcomes that resulted from that move (did the AI's situation improve or get worse?) and weighting the randomization in favor of the choices that lead to more success, you can achieve an AI that learns and improves the more it plays.
• 0

### #128 hansiec

hansiec

GMC Member

• GMC Member
• 15 posts
• Version:GM8

Posted 01 December 2012 - 05:06 AM

No human makes a random choice, in the end it's always calculated on MANY MANY MANY things (personality, awareness, shock, mood, pos, ect.) so using random may seem optimal, it's not really what you should do to make an AI work as a human, instead just pile up all the variables that exist for that relevant action to do some better results.

example:

if (running_speed >= 5 and awareness >= 1 and place_free(x+running_speed,y+1) && !place_free(x,y+1) and energy >= 5){
vspeed = -energy * jump_strength / max_energy // Give out a reasonable jumping height
energy -= 5
}

if running_speed == 0{
energy+=0.05
min(energy,max_energy)
}else{
energy-=0.1
}

if energy <= 0 && place_free(x,y+1){ // no use in slowing you down when your leg's aren't propelling you forwards
if running_speed > 0
running_speed -= 1
else if running_speed < 0
running_speed += 1
}

this is a more realistic code, it slowly increases your energy if you are not running, if you run out of energy you will start run slower (until you are running no more) and you energy will start to build up more, if you jump it takes more energy, if you are not aware of the cliff, you will simply just topple down the cliff, end of story.

There is no randomized function here, but does simulate what could happen in real life to someone, it can also work for an AI system of some sort (depending on what it is.)
• 0

### #129 Broodwolf

Broodwolf

GMC Member

• GMC Member
• 4 posts
• Version:GM8

Posted 30 December 2012 - 01:57 AM

I'd like to know who has written/thought up examples of this and who knows of other examples. What ways do you introduce flaws to make the computer mind more like that of an average human's? Or is strategy from the human mind already better than A.I. could ever be?

I'm making a game, Foundation, that's in its infancy, but will address this issue as AI adaptation is a core mechanic. The player crawls through several levels of an AI-controlled ship which has gone haywire. The AI wishes to stop the player from descending, so it sends waves of bots. I want the AI to feel adaptive but not impossible to beat.

To do this, I'm going to keep track of every source of damage received and dealt by the player on whichever level is the current one. I'll track information like if the player beats the bots from a distance or up close, if he deals EMP damage or blunt damage (from electricity-weapons or brute force), and if the player kites the bots. Then, upon descending to the next level, the ship's AI will conjure up a nice counter based on the player's playstyle. So, it adapts! The actual bots themselves will be as error-free as I can code them. It doesn't make much sense to me that a machine would have difficulty aiming or would make errors of that nature.

So, I won't program in random human-like errors. Rather, I plan on tackling the problem of making AI seem adaptive by keeping track of how the player interacts with enemies and making choices to improve the enemies to better counter the player.

Now, to plug the game! If this sounds like fun to you, you can learn more and help make this game a reality at http://foundation.lefora.com/. (I even have an advanced non-combat AI dog that is in the works if you like more pacifist stuff!)

-Broodwolf
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users