Ai

Olleus

Deity
Joined
Oct 30, 2005
Messages
6,478
Location
Beyond the Veil
I have been looking at the AI a bit and it seems that, although it is quite good, it lacks cohesion.
This is how I understand the CvUnitAI.cpp to work.

1) A unit is allocated 'tasks' (ie airbomb) based on its UNITAI_, chance and circumstances

2) The unit then iterates through all the squares in which the task (airbomb) can be done.

3) It then does that task on the most suitable square

4) Moves on to next unit


ATM the weakness of the AI is in the small amount of circumstances which it takes into account, having looked briefly at the code I could only see pCity->isDanger() and pCity->numPotentialAttackers(), we need a lot more.
If we could give the AI a general plan which it could stick to for a few turns, then it would also greatly improve. Maybe, when it declares war it could spot a weakly defended enemy city, and select that as its target. For the next few turns any units which are not on 'critical' duty (defending cities/key resources) are saved. When the stockpiled units gain x times the strenght of the units in the defending city, it moves onto the attack, and will only back away if it is obvious which it is loosing.

The other way to make it better would be to have feedback. In the top example, the unit has already decided that it will airbomb, even though it hasn't even looked at the different targets possible. This means that x% of the bombers will reduce the defences of a pointless tundra city, instead of attacking that nice, juicy SOD. A way of changing this would be to evaluate the effectiveness of airbomb and airstrike and only after decide which one to do.


Yet another way of improving the AI is in the order in which it does things. I propose this. It iterates through all the units once. If a unit can do an action which it rates to be 'very good' then it does it, otherwise it waits. Once it has looked through all the units once, it has another look. This time it will be satisfied with anything ranks 'Good'. Repeat.

Is this feasible?
 
I haven't really looked at the unit AI, but I'll try and comment.

Lord Olleus said:
Maybe, when it declares war it could spot a weakly defended enemy city, and select that as its target. For the next few turns any units which are not on 'critical' duty (defending cities/key resources) are saved.
This may sound good, but may fail against humans. The human would deliberately leave a city very weak, the AI would build up units, go after it, and then get torn to piece by units behind the lines. In Civ 3 the AI would usually always go for the weakest city, and as it cheated and could see the entire map, leaving an undefended but good city in a really safe place would draw the AI in to be slaughted. The case here is less severe, but I can see it being easily exploitable (the AI, for example, can't always see all the cities, so you just control it's knowledge a bit and trap it... over and over again!).

Lord Olleus said:
A way of changing this would be to evaluate the effectiveness of airbomb and airstrike and only after decide which one to do.
Sounds good. The only issue would be that to check all the possiblilites for all the actions would take considerably longer to process. I'm not sure what order of magnetude we're talking, but too many of such changes might make a significant difference to the speed at which the AI plays.

Although we could argue that since the release of the game the average computer spec has gone up, so we can take a few liberties with degrading performance...

Lord Olleus said:
Yet another way of improving the AI is in the order in which it does things. I propose this. It iterates through all the units once. If a unit can do an action which it rates to be 'very good' then it does it, otherwise it waits. Once it has looked through all the units once, it has another look. This time it will be satisfied with anything ranks 'Good'. Repeat.
I really really like this idea. The trouble, as you have identified, is that the situation often changes throughout the turn, meaning that the unit order is quite important.

I might write a bit more after I've eaten - can't think properly on empty stomach!
 
The Great Apple said:
This may sound good, but may fail against humans. The human would deliberately leave a city very weak, the AI would build up units, go after it, and then get torn to piece by units behind the lines. In Civ 3 the AI would usually always go for the weakest city, and as it cheated and could see the entire map, leaving an undefended but good city in a really safe place would draw the AI in to be slaughted. The case here is less severe, but I can see it being easily exploitable (the AI, for example, can't always see all the cities, so you just control it's knowledge a bit and trap it... over and over again!).

Hadn't thought about that. Maybe have the AI go for an important city rather than a weak city, or have it retreat as soon as it becomes unlikely that it will capture that city.

The Great Apple said:
Sounds good. The only issue would be that to check all the possiblilites for all the actions would take considerably longer to process. I'm not sure what order of magnetude we're talking, but too many of such changes might make a significant difference to the speed at which the AI plays.
Although we could argue that since the release of the game the average computer spec has gone up, so we can take a few liberties with degrading performance...
I really really like this idea. The trouble, as you have identified, is that the situation often changes throughout the turn, meaning that the unit order is quite important.

I don't think that the performance hit will be that big. I guess there's no way of knowing until we try it though. If it does end up slowing down the system than maybe we could add a slider saying how much time you are willing to let the AI spend per turn or something.
 
Lord Olleus said:
Hadn't thought about that. Maybe have the AI go for an important city rather than a weak city, or have it retreat as soon as it becomes unlikely that it will capture that city.
That would be even worse. You could keep AI stacks confused for ages by moving defenders in and out of cities.

If you were to make it sufficiently complicated it would be hard to exploit, but complicated things often don't work very well for the function they were orignally designed.
Lord Olleus said:
I don't think that the performance hit will be that big.
I'm pretty sure it'll be a drop in the ocean... however lets compare what it is doing now with what it could possibly do under the system you are proposing.

Before:

Unit --> Airbomb --> Get best plot --> Bomb

After:

Unit --> Airbomb --> Get best plot --> Airstrike --> Get best plot --> No very good missions --> <Cycle rest of units> --> No good missions --> <Cycle rest of units> --> Average mission found --> Do mission

Now I'd guess that the bit where the best plot is found would take the longest (scanning lots of plots and analysing them), so we probably have a scale-up in time of maybe 2 or 3. If something similer were run for ground units as well the the times could add up to something meaningful - espcially in the late-game of a large map.

Lord Olleus said:
If it does end up slowing down the system than maybe we could add a slider saying how much time you are willing to let the AI spend per turn or something.
To be honest the time in between turns is on the short side at the moment - the only thing that seems to take the time is animations, so we could probably get away without this. People might not even notice.
 
Even if it doesn't take years for the computer to do this, it will take years for us to write this. CvUnitAI.cpp is about 10 000 lines long. Is it really worth it?
 
We wouldn't have to re-write the whole thing. I think the improved decision making idea you are suggesting could probably be slotted into the current system quite easily.
 
We'll see what the others have to say about this before we dive in though. I have to say that I am suprised that they did not use a system like this when programming the AI in the first place, unless I missed it.
 
The Great Apple said:
That would be even worse. You could keep AI stacks confused for ages by moving defenders in and out of cities.

If you were to make it sufficiently complicated it would be hard to exploit, but complicated things often don't work very well for the function they were orignally designed.

I like that idea of giving the AI a mid term target. To prevent that the strategy is to predictable by humans we may have to add some random factors. The AI then can randomly choose one of them and go for it. Example:

create a ranking of the cites for each of the following parameters :
- culture,
- production
- defense
- ressources
- .... and whatever else

Next is to have different weighting of those single parameters by random factors (x, y, z, v, ....)
- culture * x
- production * y
- defense * z
- ressources * v
- ....

sum up those factors and go for the city with the highest one. Once a city is selected the mid term target is created and the AI tries to fullfill it. By another random factor the spontaneity of the AI is set, to cancel that goal. A higher factor means the target is followed more strictly, a lower factor the opposite.

We also can combine those random factors with the leader or civ attributes. This should add another element.

12m
 
Good idea. That way Montezuma would be suicidal on the battlefield as well as in diplomacy. Would require a hell of a lot of balancing, but if we can pull it off, it would be truly amazing.
 
I have not read any comment as im a bit short of time but the AI does war in several steps.

A it nearly constanly is in a kind of war, but most wars do not break out. Why? very often the AI selects that int wants to do war. It then switches to a preparing WARPLAN against one enemy. After 10 Turns it begins evaluating its power and the enemy power. If it is stronger than the enemy it declares. If it has not gotten stronger within 20 Turns it switches back to peacefull and starts the cycle again.

Now war itself. The AI evaluated each Area (=continent) which kind of war it wants to do. If it has a lot of troops it changes the AREA_AI to ASSAULT.
The other AREA_AIs are not to interesting.

At this step (ARIAAI_ASSAULT) it select one city and all offensive units on the continent are set to attack this city. The city is at the moment primarily selected by how far it is to the AIs lands (way/path wise).

The next step is the movement of the troops themself. This is primarily dictated by the UNIT_AI.
There are some things that seem noteworthy.
- The unitais seem to work ok. Keep in mind that the AI very often groups its offensive units so therer seldom is one lone attacker.
- The UNIT_AI is decided on built time and never changed. This means once city defender, always city defender.
- The way goups are built by the AI. Its simply if an UNITAI_ATTACK unit sees anotehr UNITAI_ATTACK unit it runs therer and attaches itself to the stack. There is a limit to the stack size dependend on the exact UNITAI of the stack and sme random numbers. Now these staks start running around and do whatever the head unit (which defined the UNITAI) would do. Only for attacking all units are considered.

what i do not yet understand at all is the way sea-war works.

I hope this small overview helps. Now i have to go back to my mages and to understand all this indepth (especially the Selection Group things and sea warfare)... or kael bites me. ;)

P.s. doing what monkey proposes should need only a change in the selection of the city to attack, so it is a very localized change.
 
Thanks Chalid - good info there.

Having read that, I still think the action filtering method based on the value of the action would work, with units with very good actions go first in the hopes that they improve the action value of the units with less good actions at present.

About city selection - I agree that the nearest city isn't optimal, but it's a pretty good approximation to what the best city is... it's usally my tactic except in very specific circumstances.

Thinking about it, always going for the nearest city is as exploitable as always going for the weakest. However I suppose there are other unitAIs to stop you from just protecting the nearest city. The pillage AI tends to wander about a bit and attack if it thinks it can win, and the naval AI likes doing flanking manouvers.

Out of interest, does anybody know the difference between the ATTACK_CITY AI, and the ATTACK AI? I guess I could look it up, but I'm feeling lazy.
 
We could use influence maps to help out with this goal. That way it keeps is out of the realm of a static formula.

Static formulas are bad. :(

Dale
 
static formulas are easy though.
 
Static formulas are predictable and easy for a human to work around. :)
 
The Civ 4 AI doesn't come with many static formulas - pretty much all of them have some potential for variablilty based on events other than the main one. Some of them don't have very much potential for variablility, but it's still there. Something to work on.

Are you planning on having an attempt at coding this LO?
 
I will start work on this in a few weeks. I am making a Air and Sea mod at the moment and then I would like to play a game.
 
Been getting quite a few new people interested in editting the AI apply to join the group since that really old thread about it was bumped in GD (this one - some interesting discussion about genetic algorithms). Please feel free to contribute anyway you like guys! I'd love to see some good AI upgrades make it into the next version.
 
Top Bottom