Parameters for terrain affinity need tweaking

Bug #1348800 reported by SirVer
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
widelands
Fix Released
Medium
Unassigned

Bug Description

This is a followup of bug 1328635.

The new terrain affinity does not let trees grow enough on some terrains.

- The Atoll.wmf map has been reported as having too little trees.
- Former desert world seems to grow too little trees.
- Blackland has a high affinity to greenland trees so the forresters sometimes plants those instead of some of the older blackland trees that should have a higher affinity to former blackland.

Related branches

SirVer (sirver)
Changed in widelands:
status: New → Incomplete
Revision history for this message
Albert Einstein (w-aaaaa) wrote :

There are three major ways to fix the bug:

1. Changing sigma values in model (0.25 and 12.5 to higher ones)
2. Changing all the terrains and trees (values for affinity)
3. Changing foresters, make them plant only best-fit trees for the ground

First one is the simpliest and it works (https://bugs.launchpad.net/widelands/+bug/1328635/comments/45)
Second one will be also good, but it has to be done by one person (with an idea how to fix)
Third one is most complex and makes foresters thinking :)

Tibor already tested first idea and has something.
Tibor: Please, tell us what values did you use?

Revision history for this message
TiborB (tiborb95) wrote :

Hi,

I believe you wanted to change these lines in logic/terrain_affinity.cc :

const double sigma_fertility = (1. - affinity.pickiness()) * 0.25 + 1e-2;
const double sigma_humidity = (1. - affinity.pickiness()) * 0.25 + 1e-2;
const double sigma_temperature = (1. - affinity.pickiness()) * 12.5 + 1e-1;

I did not do thorough tests, mainly I run atoll map, but I settled on values: 0.8, 0.3,15.0. I think that only slight modification are needed. But more tests on desert maps would be usefull to get better picture.

Personally, I think that blackland simply does not fit well into normal earth-like biomes (desert, greenland, cold lands and mountains).

Revision history for this message
SirVer (sirver) wrote :

> 3. Changing foresters, make them plant only best-fit trees for the ground

they already do that. They will find the best fitting 6 trees for the field they target and then randomly choose one of those.

Revision history for this message
Albert Einstein (w-aaaaa) wrote :

[Tibor Bamhor]
> But more tests on desert maps would be usefull to get better picture.

I think that deserts are most problematic maps. I tried to make a "good looking" of land values there, but it is impossible. Deserts in real world are so different (oasis full of water, salty sands next to the oceans, rocky mountain meadows). So probably there will be needed some terrain changes.
I mean that maybe mountain and high mountain meadows have to change into more comfortable ones?

It is very hard to make something working and a bit realistic at once.

We have to make a good map with all the biomes to test them :) It is very hard, but I will try to make one ;)

[SirVer]
> They will find the best fitting 6 trees for the field they target and then randomly choose one of those.

Great! I know that it should be a part of new affinity for trees, and it IS.
I'm not sure if 6 is small enough, but it is good for now. In desert areas some of terrains have only 1 specie of tree with best fit (f.e. beach <-> coconut palm). But don't change it! ;) I'm just thinking about all the things there...

Revision history for this message
GunChleoc (gunchleoc) wrote :

Maybe foresters could choose the best x fitting trees instead of the best 6, depending on terrain?

Revision history for this message
Albert Einstein (w-aaaaa) wrote :

I can't find the bug with those foresters' behaviour, so I will write here my solution:

> Maybe foresters could choose the best x fitting trees instead of the best 6, depending on terrain?
There is an idea. But it is a bit complicated:
[value- probability value for mean terrain and exact specie of the tree]
1. Make a sorted table of all the trees (I hope it is now)
2. Take first (the highest one) value of the best fit -> variable P_best
3. Multiply by 1/2 or another value -> 0.5*P_best
4. Just take all the trees with higher value than in point 3. -> x>0.5*P_best
5. Random choice from those trees, like it is now

Result:
If there is only one exact specie of tree (one 95%, all the others less than 30%), foresters will plant only that kind of tree.
If the terrain is a mix (lots of trees ~20%), OR the desert one (all the trees ~5%), the forester will plant all of them.

You don't have to think about connections trees <--> terrains, the forester will do that ;)

Also there is another idea, changing the choice:
1. Take all the trees to randomize
2. The probability for each tree is not equal. The probability is a "weighted choices" (is it a correct phrase?):
 - - Ex.:
fits are: {5%, 10%, 50%, 95%, 40%},
sum is: 5%+10%+50%+95%+40%=200%
probability of 1st tree is: 5/200=1/40
2nd: 10/200=1/20

The second idea make a random function more complicated and it can work bad with some terrains (with not fitted soils), because in current model the basic value of "fit" is 5%. You can't have less. Also you can't simpliy substract those 5% from all of the "fit value", because some information is lost and you can get 0.0 (which is bad in this model, better is 3e-10, but not 0.0).

First idea is- in my opinion- better and easier to understand and make code. But it needs one more value (point 3.). It needs tests- one more time. So it is not the best idea too.

Revision history for this message
Launchpad Janitor (janitor) wrote :

[Expired for widelands because there has been no activity for 60 days.]

Changed in widelands:
status: Incomplete → Expired
SirVer (sirver)
Changed in widelands:
status: Expired → Confirmed
importance: Undecided → Medium
milestone: none → build19-rc1
assignee: nobody → Kristin (ha-kripo)
Revision history for this message
TiborB (tiborb95) wrote :

one of variables that needs tweaking is pickiness for desert plants. These have too high value about 0.7, while other plants have much lower. The effect is that a palm gets very low 'match ratio' even on a terrain that is obviously suitable for it.

Revision history for this message
Teppo Mäenpää (kxq) wrote :

I tried playing the 2nd tournament map. There the outcome was that with identically placed buildings, I got about 3x the number of trees on R18 compared to a recent development snapshot.

Just looking at the display, it seemed as if the trees would have generally grown slower, and rather many never made it. Is it obvious that this is an issue with the foresters (instead of tree-to-terrain matching?

If it is obvious, then would it be worth trying to make a weighted choice between the most suitable trees (comment #3) instead of a random choice between those?

In which file(s) this particular code lurks?

Revision history for this message
TiborB (tiborb95) wrote :

you might start with logic/terrain_affinity.cc. My opinion is that more then adjustment of parameters is needed.

If you manage to come up with working improvement a lot of peoples will be thankfull

Revision history for this message
SirVer (sirver) wrote :

> Just looking at the display, it seemed as if the trees would have generally grown slower, and rather many never made it. Is it obvious that this is an issue with the foresters (instead of tree-to-terrain matching?

The forrester logic has not changed much and should only have been improved. it lives in src/logic/worker.cc in run_plant. The forrester picks a spot at random and determines the 6 best fitting tree_saplings in the world for this field as measured by terrain affinity. One of these is then picked at random.

> If it is obvious, then would it be worth trying to make a weighted choice between the most suitable trees (comment #3) instead of a random choice between those?

A weighted choice could be implemented of course, but I think tweaking the parameters is better.

src/logic/immovable.cc contains ActGrow and ActSeed which is the logic for when a tree grows. The probability of dying is fixed in each act() step and has not been changed in the one_world merge.

> you might start with logic/terrain_affinity.cc. My opinion is that more then adjustment of parameters is needed.

Could you elaborate on what changes you have in mind? I believe that the gaussion model is as flexible as the old probabiltity table we had, but needs less parameters. I think it is badly tuned though.

Revision history for this message
TiborB (tiborb95) wrote :

I looked at this some time ago, there is some systematic (mathematical) problem with poor terrains. Picking of tree type is one thing and speed of growth is second. Algorithm implies that yield of best terrain:poorest terrain is 95:5. Too big difference. More over if I tweaked the parameters to help poor soil, trees grew like crazy on good soil - not balanced.

So what I mean sole parameters change would not do - but I might be wrong of course...

Revision history for this message
SirVer (sirver) wrote :

Interesting - we are not married to the current model. The prior model was a table for each tree type that contained growth probabilities for all soil types - with the one world merge that makes too many values - too hard to maintain.

A new model was introduced that only uses 4 parameters per immovable and 3 per terrain and matches them. We can change the model, add parameters and what not, but we will need an idea to model this. Still, I would prefer some more experiments with the parameters before jumping ship on the model since it is cheaper than changing the implementation of the model.

Revision history for this message
TiborB (tiborb95) wrote :

No problem, but there should be at least some basic understanding that yield of best:poor soil would be 5:1 or so, not more

Revision history for this message
Teppo Mäenpää (kxq) wrote :

>A weighted choice could be implemented of course, but I think tweaking the parameters is better.

In the terrain my test map happened, the best tree had a suitability of 91%, 6th-best 25%. I think that a forester mostly plants the well-growing trees and only occasionally the alternatives makes sense, and made a naive trial implementation, and liked the outcome.

If you wish, I can fix the codecheck introduced complains etc., and push for review.

However, this bug is not fixed by introducing weights to plantation.

Revision history for this message
SirVer (sirver) wrote :

kxq, I think implementing this if it is not too complicated makes sense. Could you send your stuff for review?

Revision history for this message
Teppo Mäenpää (kxq) wrote :

I just added one. This has squared weights.

Revision history for this message
Teppo Mäenpää (kxq) wrote :

Does somebody know by heart, where the tree's requirements and terrain parameters reside?

Revision history for this message
SirVer (sirver) wrote :

Teppo, are you looking into this now? I wanted to fix this once and for all today too.

Changed in widelands:
assignee: Kristin (ha-kripo) → SirVer (sirver)
Revision history for this message
Teppo Mäenpää (kxq) wrote :

I could take a look. Would you know, where should I look at? This is not yet a promise to find any solutions!

Revision history for this message
SirVer (sirver) wrote :

Thanks for the offer, but for now I think this is done.

In r7358 I pushed new parameters for the different tree types.

The approach I did was this: I extracted the probability tables from b18 that describe how well a certain tree grows on a certain terrain. Then I extracted the terrain parameters and tree parameters from trunk. I reimplemented our current model function in python and then used a genetic algorithm to tweak the tree parameters until the match between the model and the truth tables from b18 was best. There cannot be a 100% agreement of course, since our model is more constraint. But the trees now behave consistent - if two terrain have similar properties, all trees will behave similar on them too.

I pushed my code here https://github.com/SirVer/wl_tree_parameter_tuning/blob/master/optimize.py

I tested this and the forrester now always plants the correct trees for the different terrains and the growth behavior looks reasonable. Further play testing of the parameters is needed of course, but I believe this is about as good as b18 was.

Changed in widelands:
status: Confirmed → Fix Committed
assignee: SirVer (sirver) → nobody
Revision history for this message
Teppo Mäenpää (kxq) wrote :

Hi, SirVer,

I tweaked with youd script a bit:

- Marginal growth areas have smaller weight. In the end of day, the forester only considers the best trees.
- Data points where that particular tree used to be most suited have additional weight
- Data points where a tree's growth rate exceeds that terrain's previous "best value" get additional, artificial weight. The idea behind this was to reduce the number of landscapes where the dominant tree changes in visually unpleasant way, and also should discourage massive tree growth in marginal areas (mountains, deserts, etc).
- Starting point of the minimization was changed, based on the "B18 best matches".

In my opinion, values tweaked like this are somewhat better, in the sense that now the lowland behavior had the B18-ish feeling in my test case. This does not guarantee that it is good everywhere.

Would you like to take values like that into trunk? I committed them to the linked branch.

Revision history for this message
SirVer (sirver) wrote :

> Would you like to take values like that into trunk? I committed them to the linked branch.

Yes, your ideas all sound very reasonable. But I'd like to propose another model. Could you run that through your test case too (you need to build the branch, I changed the model slightly too). Maybe you can apply the tweaks you did to this one too, I updated my github.

The idea here was to optimize not only the trees, but also the weights in the model and the terrains to approximate b18 as closely as possible. This optimization ran a long time (~ 2h) but converges to a small residual value of 0.7, so it should be very close to b18 in behavior.

I put the script I used to optimize here:
https://code.launchpad.net/~widelands-dev/widelands/more_trees

And the branch that contains the values is here:
https://github.com/SirVer/wl_tree_parameter_tuning

Teppo, I wait for your feedback and then we decide which way we want to put into trunk, sounds okay?

Changed in widelands:
status: Fix Committed → In Progress
Revision history for this message
Teppo Mäenpää (kxq) wrote :

> But I'd like to propose another model.
...
> I wait for your feedback

My pleasure. I am a bit busy now, might take a day or two.

Revision history for this message
Teppo Mäenpää (kxq) wrote :

For some reason, bzr refused to deliver the branch to me earlier, but today it worked again.

The "more trees" branch was very similar to B18 in the grassland-based test.

I then x-checked this with a desert map. There the more_trees branch produced 12% more trees than B18, which was not the target but still OK in my opinion. My gut feeling is that you should merge the more-trees branch, close this bug and wait for playtesting to have its say.

Revision history for this message
SirVer (sirver) wrote :

Okay, done. Thanks for testing!

Changed in widelands:
status: In Progress → Fix Committed
Revision history for this message
SirVer (sirver) wrote :

Merged in r7369.

GunChleoc (gunchleoc)
Changed in widelands:
status: Fix Committed → Fix Released
Revision history for this message
GunChleoc (gunchleoc) wrote :

Fixed in build19-rc1.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.