Topic: AI enhancement suggestion - wood

SirVer

Joined: 2009-02-19, 15:18 UTC+1.0
Posts: 1442
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-27, 19:14 UTC+1.0

Theoretically the AI is an interface (see computer_player.cc) and creating a new one orthogonal to the existing one should be straightforward - I do not know how leaky the abstraction is though. As mentioned, the code is a mess in this part of Widelands.

Also in the game shold be something changed with the AI players. Maybe add "AI method"?

No, that is a terrible idea. Remember: options are bad. They add complexity to the code, add new less used code paths that are then not properly tested and worse of all add confusing things to choose from for the player who just wants to play. A good AI should figure most things out for itself. One dimension of choices are maybe a necessary evil (weak, normal, strong). But adding more parameters is definitively not a good idea.


Top Quote
Tibor

Topic Opener
Joined: 2009-03-23, 23:24 UTC+1.0
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-27, 20:27 UTC+1.0

My contribution to discussion :) :

first: some time ago I started working on an AI for widelands in LUA but my enthusiasm run out after like 20 hours of study and work (I was new to LUA then).

But my idea on AI (as Iintended it in LUA) was/is like this:

It should be threaded and treads should control a specific part of economy, and would mimic real life - like real company. So threads could be like
- wood_industry (would cover cutters, rangers, wood harderners)
- space_expansion (buiding military buildings)
- food_industry (from farmers throught kitchen and so on)
- raw_materials (mines, coal burners, perhaps steelworks)

So they would run more-less independently of each other...

there would be an AI governor - that would call (wake up) the threads in required time. Every thread when returning to AI governor would say when it should be waken up. For some situations even 15 seconds sleeps would be fine.

It would be nice if there was another option avaiable when setting new game (for players):
- a person (as by now)
- current AI (as by now)
- testing LUA AI (new one)

and lua would be initiated with file scripting/init-AI.lua - for every single player (Where LUA AI is set).

This would be all what is needed from the game developers, and the LUA AI option could be disabled for "production" builds.

init-LUA.lua would have some inbuild variable so the content of script knew to which player it is bound.

Even though LUA IA would not be good enough for "production" it would be very usefull for testing ideas and so on...

Also I would appreciate LUA functions like

connect_two_flags(field1,field2)
return true/false
connect_to_nearest_road(field)
return true/false
who(radius)
return {} --list of users (including NONE) who owns fields in vicinity (of radius)

of course the list would go on....

Enough for now :)


Top Quote
SirVer

Joined: 2009-02-19, 15:18 UTC+1.0
Posts: 1442
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-27, 21:38 UTC+1.0

Some comments on this:

threaded Lua coroutines lend themselves really nicely to that. They can even communicate through globals as long as we guarantee that they are not called from separate c++ threads (which we do right now).

AI governor You have this already - just run sleep(time) in your coroutine and it will be woken by widelands after this time.

  • testing LUA AI (new one) I think i mentioned this before, not sure though: but you can program this into a single map for starters, just as a regular scenario. If the AI is promising we will find a way to make it selectable from inside the game.

Lua functions most of them should be possible to be written in lua right now - here too if one becomes really useful, we could find abstractions inside of Widelands and port it to c++, but there is nothing wrong with prototyping them in Lua first.


Top Quote
Tibor

Topic Opener
Joined: 2009-03-23, 23:24 UTC+1.0
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2014-01-27, 22:06 UTC+1.0

All right, but one exemption - algorithm to create roads. It is not part of AI job, it is implemented as by now and works fine enough, so I would appreciate if some functions for road creation was made accessible from LUA

Edited: 2014-01-27, 23:48 UTC+1.0

Top Quote
SirVer

Joined: 2009-02-19, 15:18 UTC+1.0
Posts: 1442
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-28, 09:57 UTC+1.0

Well, I do not think there is an exception here. I think how the roads are build is very important for an AI and will distinguish one from another - the bigger the economy the more important become the roads in the center of it. However what you mean is probably path finding - finding a possible road from A to B. This could be exposed through the Lua interface. Maybe something like wl.map.Map.find_path(ignore_immovables) or wl.Map.find_path(start_immovable, stop_immovable, stay_or_roads). But do not expect this to be easy - the path planing inside of Widelands is quite peculiar and makes some assumptions that might not make it suitable for one-off path planning like this. In fact, writing a Lua only version might even be easier in the first place.


Top Quote
einstein13
Avatar
Joined: 2013-07-29, 00:01 UTC+2.0
Posts: 1116
Ranking
One Elder of Players
Location: Poland
Posted at: 2014-01-28, 13:19 UTC+1.0

I was thinking a lot about some algorithms of economy for AI. Most of them are connected with saving information in a memory and using it at the other time.

As I remember, the AI functions aren't parallel to the gameplay, so they are functions called by main engine(?). So the variables saved by the functions will disappear when we make a step of game.

We can do all the numeric calculations every time, but it is CPU-absorbing. And probably with big empire it will end with terrible lags. Is it good? I don't think so. In my opinion we should make a parallel threads for AI (or just one, but parallel) and make it working.

Is it possible to make it as a test (if not working, we can return to an old version)?

The road problem is a bit complex too, but I remember that AI in the Settlers II had bad implementation of using it. That was: if you have a traffic jam -> build a road connection between points. After a while AI was building a roads far away from traffic jams. So one point is to add the function of connecting the road, but second one is to think about using it wisely.


einstein13
calculations & maps packages: http://wuatek.no-ip.org/~rak/widelands/
backup website files: http://kartezjusz.ddns.net/upload/widelands/

Top Quote
teppo

Joined: 2012-01-30, 09:42 UTC+1.0
Posts: 425
Ranking
Tribe Member
Posted at: 2014-01-28, 17:50 UTC+1.0

is it possible to make a part of forum created only for AI threads?

I guess that that would bring some interesting bugs.

If you consider contributing to Widelands to be a learning opportunity, why not?

Else, I think that the AI code could be improved a lot CPU consumption wise, too.


Top Quote
einstein13
Avatar
Joined: 2013-07-29, 00:01 UTC+2.0
Posts: 1116
Ranking
One Elder of Players
Location: Poland
Posted at: 2014-01-28, 21:43 UTC+1.0

teppo wrote:

is it possible to make a part of forum created only for AI threads?

I guess that that would bring some interesting bugs.

Bugs? Only by expanding a forum? face-smile.png I can create own forum only for that, but here is ads-free and it is connected to the widelands.

If you consider contributing to Widelands to be a learning opportunity, why not?

I am learning lots of different things. Widelands is a good project with lots of good people inside. I like you all and want to give some math, algorithm and physics work for you face-smile.png I've never programmed in C++, but I know another languages and their possibilities. I know what problems have lots of programs and try to solve them. I did something last time (https://wl.widelands.org/forum/topic/1400/). Probably you can remember that. Next time (I think in a week) I will do it on .pdf file, easy to download and read. I did some math equations into this problem (wood economy problem) and only I have to have some time. I like that kind of puzzles face-smile.png

Else, I think that the AI code could be improved a lot CPU consumption wise, too.

I don't understand this sentence. Do you think that when we improve AI code, the CPU consumption would be smaller? Probably face-smile.png But also it can be worse. Now we can't remember lots of stuff and every time it have to be recalculated. It isn't good way.

Now I have one idea: some economy problems (like wood problem here mentioned) can be recalculated with some probability. It can be like:

(...AI code)

variable=random(); // the value from 0.0 to 1.0

if(variable<constant){ // constant is somewhere given

wood_economy_function();

}

(AI code...)

After that, the special function will not be recalculated every time. Some CPU time will be saved and game will be faster face-wink.png

Edited: 2014-01-28, 21:45 UTC+1.0

einstein13
calculations & maps packages: http://wuatek.no-ip.org/~rak/widelands/
backup website files: http://kartezjusz.ddns.net/upload/widelands/

Top Quote
SirVer

Joined: 2009-02-19, 15:18 UTC+1.0
Posts: 1442
Ranking
One Elder of Players
Location: Germany - Munich
Posted at: 2014-01-28, 22:25 UTC+1.0

As I remember, the AI functions aren't parallel to the gameplay, so they are functions called by main engine(?). So the variables saved by the functions will disappear when we make a step of game.

I do not understand what you want to say. That does not matter if the code is threaded (i.e. parallel) or not. Of course when the simulation progresses your assumptions are immediately outdated.

We can do all the numeric calculations every time, but it is CPU-absorbing. And probably with big empire it will end with terrible lags. Is it good? I don't think so. In my > opinion we should make a parallel threads for AI (or just one, but parallel) and make it working.

You do not want to add threaded AI to Widelands. It would be desirable of course, but it will be a major headache to add threading into the simulation since we rely on many places that stuff will only happen one at a time. Adding all the locking and mutexes will make the code even more complicated. Besides, the actual simulation is pretty cheap, so multi threaded is not needed there.

But that said, you can make the AI concurrent - i.e. by implementing it in a bunch of Lua coroutines or with some kind of abstraction in c++. This should make it easier to reason about it too.


Top Quote
einstein13
Avatar
Joined: 2013-07-29, 00:01 UTC+2.0
Posts: 1116
Ranking
One Elder of Players
Location: Poland
Posted at: 2014-01-28, 23:22 UTC+1.0

SirVer wrote:

As I remember, the AI functions aren't parallel to the gameplay, (...).

I do not understand what you want to say

You did it well:

your assumptions are immediately outdated.

But:

It would be desirable of course, but it will be a major headache to add threading into the simulation since we rely on many places that stuff will only happen one at a time.

Multi threading don't mean that all those things will happen at one time. It can be like that:

Major simulation wants to get AI response --> Call theAI-thread_1 --> Wait for answer --> do the return

AI_thread:

Wait for call --> Make computing based on the state of map & itself --> return the answer --> Wait for call

I did that in one of my project. Don't know it is the best possibility and if it is a good way to make something parallel.

I know only that making program multi-threaded is very hard (to make it clear and fast).

This is one of possible solution of the problem (outdating assumptions for AI)

Second is much more easier, but it has lots of opponents: we can build a special table as global variable where AI can hold assumptions as a numbers. But this is a global variable. It would be easy to check what new_AI is doing.

Also this idea can be done as keeping this table in the simulation and giving a pointer* to the table to the AI_function (or whatever it is).

*-Can we do it in C++? In pure C it is possible and used.

That's about the possible solution I can see here to hold some variables for AI. I'm not a professional programmer! I don't know about all of this. Only some face-wink.png I did some projects and I checked all these solutions. Can be programed and it is working. Which is the best? I don't know - please tell me.

Edited: 2014-01-28, 23:24 UTC+1.0

einstein13
calculations & maps packages: http://wuatek.no-ip.org/~rak/widelands/
backup website files: http://kartezjusz.ddns.net/upload/widelands/

Top Quote