Granaries: Don't do things half way

Joined
Sep 11, 2009
Messages
922
Location
surrounded in the USA
So, I was arguing with a friend on the forum (hope you don't mind me calling you that!) about the optimum time to whip a granary in. I felt that the discussion was threatening to derail the thread, so I thought I'd start a new one.

Anyway, my thought was that the best time to whip the granary was when the city's food bar was filled from somewhere between 1/3 and 1/2 of the total food bar. More precisely, you want the food bar to be exactly half full the turn after the granary is completed. Since you should have a food resource filling the food bar quickly, that often means whipping earlier than you might intuit. There's also the 1-6 (depending on your speed and whether you're whipping one or two population) "gain" from shrinking the food bar when you whip.

Kossin argued that it's actually more complicated than that, and left this example as food for thought:

No, it's more complicated than that as it depends on your food surplus per turn and granary capacity.

Example:
City size 5
surplus = 4 food/t
Food to grow = 20+2x5 = 30

Let's start at half the food bin.
30/2 = 15

Food+surplus (+granary)

0. 15
1. 15+4 (+4)
2. 19+4 (+8)
3. 23+4 (+12)
4. 27+4 (+15!) = 16 food in the bin, size 6​

Now try starting at 21 food in the bin.

Food+surplus (+granary)

0. 21
1. 21+4 (+4)
2. 25+4 (+8)
3. 29+4 (+12) = 15 food in the bin, size 6
4. 15+2 = 17 food in the bin - Assuming you have only 0 food tiles for 6th citizen.

In some situations you'll be losing food+other yields if you try to fill the granary by changing tiles.

Now, as I see it, the problem with the example is that example 2 magically started out with 6 extra food but ended up with only 3 extra food. That would be a loss in my book.

Is there something that I'm missing?

By the way, I still don't understand why Kossin has the sixth citizen working a zero food tile. If you read this, Kossin, could you explain?

As a bonus, Vicawoo implied that even food surplus gained on the turn you gain another citizen gets included in the granary's storage. I got to admit that I've never noticed that before. Can anyone confirm? For example, if you build the granary when you only have one more food needed to grow and you have a food surplus of +7, do you get 6 food in your food bar on the next turn or do you get 13?
 
As a bonus, Vicawoo implied that even food surplus gained on the turn you gain another citizen gets included in the granary's storage. I got to admit that I've never noticed that before. Can anyone confirm? For example, if you build the granary when you only have one more food needed to grow and you have a food surplus of +7, do you get 6 food in your food bar on the next turn or do you get 13?

That looks right to me, based on the code. Maybe somebody can experiment....

Code:
void CvCity::doGrowth()
{
  ...

  changeFood(iDiff);
  changeFoodKept(iDiff);

  setFoodKept(range(getFoodKept(), 0
    , ((growthThreshold() * getMaxFoodKeptPercent()) / 100)));

Translation: the idea is that granary stores food is slightly misleading. What the code actually says is that cities keep food; the amount of food that they keep is determined by the sum of the iFoodKept properties of the buildings in the city.

(In the unmodded game, only the Granary/uniques have this property set).

So, on each turn we calculate iDiff (aka, the food surplus), apply it to food. Then we apply that same change to FoodKept. After we make the change to FoodKept, the value is cropped to that it falls between 0 (no food kept) and the 50% growth threshold defined by the granary.

Thus, all of the food surplus is added to "the granary" on the turn you grow, then capped by the "old" population size. Then the game reduces the food bar by the difference between the growth threshold and the kept food limit (aka - the new food level is the kept food plus the surplus) and adds a pop point.

Note that kept food doesn't go away when you grow. As far as I know, there isn't a way to exploit this.
 
Maybe somebody can experiment....

Can confirm this via much experimentation a few years back. Indeed, I even put together a simple spreadsheet that calculated the optimal granary whipping point as a function of pop size and food surplus, and was intending to include it in an article I was writing re: when to whip the granary.

Then I realised that (i) determining the optimum time to whip (edit: or finish building) a granary isn’t game breaking and (ii) I really needed to get out more. :lol:

Suffice to say that VoU and vicawoo are completely right. That the food surplus is counted toward re-growth actually means that there can be multiple optimum positions to whip / finish building a granary whilst a food bin grows, sometimes just before, sometimes right on and sometimes just after the half way point has been reached.
 
Now, as I see it, the problem with the example is that example 2 magically started out with 6 extra food but ended up with only 3 extra food. That would be a loss in my book.

Is there something that I'm missing?

There are basically 3 "efficient" ways to go about finishing a granary.

1) You finish it with chops/OF/production before hitting any of the breakpoints.

2) You finish it as late as possible to fill the granary.

3) You stunt growth slightly/or aim for another food number to get a better boost from the granary.

In most cases, you'll end up with 1). Aiming for 2) is typically best. However, there are times when you can't get the production to do the second option and then option 3 becomes best.
2>1>3

Please take a look at the saves I've WB'd.

34fd6xw.jpg


One condition: only work improved tiles or lake tile.

All 4 cities are the same. The top 2 have just finished their granary. The bottom 2 finishing just this turn.

Bottom left doesn't stagnate. Bottom right stagnates for 1 turn.

Start: size 5
TL: 15F
TR: 19F
BL: 29F
BR: 29F

End: size 6
TL: 20F
TR: 24F
BL: 18F
BR: 19F

As you can see, the sweet spot for this city configuration is 19F. However, if you can't get production in time, you can delay the granary for some time to get the second best outcome, which is the bottom right city.

For the sweet spot:
You can set up a quick spreadsheet to solve for any city configuration.

By the way, I still don't understand why Kossin has the sixth citizen working a zero food tile. If you read this, Kossin, could you explain?
I assumed worst case scenario. If that tile is food neutral of better, the comparison gets even better for the earlier growth.

Just in case, this example was referring to case 3.

As a bonus, Vicawoo implied that even food surplus gained on the turn you gain another citizen gets included in the granary's storage. I got to admit that I've never noticed that before. Can anyone confirm? For example, if you build the granary when you only have one more food needed to grow and you have a food surplus of +7, do you get 6 food in your food bar on the next turn or do you get 13?

Yes as VoU pointed out, food to the granary is added even on the turn of growth.

~~~

In the end, it doesn't matter too much unless you're doing competitive/perfectionist play. Half the bin is still an okay reminder.
 

Attachments

tl;dr

what's the perfect amount of food to complete a granary with? I assume it differs depending on whether or not you use the whip to complete it.
 
^^^
Depends on: speed, food surplus and city size. Whether you whip has no bearing, you must simply consider the correct food surplus and city size.
 
tl;dr

what's the perfect amount of food to complete a granary with? I assume it differs depending on whether or not you use the whip to complete it.

It depends on (i) the amount of food already stored in the bin at the current pop size (ii) the city’s food surplus and (iii) current pop size. As mentioned in my earlier short ;) post:

That the food surplus is counted toward re-growth actually means that there can be multiple optimum positions to whip / finish building a granary whilst a food bin grows, sometimes just before, sometimes right on and sometimes just after the half way point has been reached.

IMHO, the key point is really that made by kossin in his earlier post: unless you’re looking to play perfectly, you’re much better off simply looking to complete the build / whip somewhere around the halfway point of the food bin (although whipping can also be affected by things like what you want the overflow to be directed into eg. libraries for a creative leader.)

In other words, as I realised from several early losses at emperor back in the day, if a gamer finds themselves losing consistently at or below emperor (and even immortal), the chances are that there are far bigger issues in their game than their failure to build / whip in granaries in all their cities at precisely the optimum point in the food bar. :)
 
tl;dr

what's the perfect amount of food to complete a granary with? I assume it differs depending on whether or not you use the whip to complete it.

Gotta admit, I'm not happy with either of those answers yet - except of course for LG's point that it doesn't matter very much, in the grand scheme of things.

But considering the puzzle for its own sake....

It still looks to me as though the answer is "anywhere between 1 food less than half of a food bar, and exactly one turn of food surplus below half of a food bar."

The basic idea is that if you whip the granary with the food bar too low, then you miss out on some tile yield. If you whip late, you get extra tile yield, but you lose bonus food.

Simplified example - the city is at 0/28, and has exactly 1 hammer invested in the granary. We're working a +4 food tile and 3 2F tiles. When do we whip?

First point - when we whip doesn't change when we grow; we need 24 food to go from size 2 - 3, which at +4 food per turn requires six turns. So, starting from NOW, we can whip at 0/24 and then grow for 6 turns; or we can wait four turns before we whip (16/28) and then grow for two more turns afterwards, or any combination adding to six.

What does change is (a) how many extra turns we get on the tiles, and (b) how much food we have in the bin after we grow. We get more turns if we whip later, but we get more food if we whip sooner (up to the cap).

The usual answer to the optimization problem is that we whip at 8/28, because this takes us to 8/24. That gives us two extra turns at size 4 (four turns on tiles); on the turn we whip, the food surplus takes us to 12/24 (before the granary finishes, so we don't get the KeptFood yet). Three more turns takes us to 24/24, so we grow, and end up with 12 KeptFood.

If we whip a turn late, we get two more turns on the tiles (six total), but we are at 12/24 after we whip, 16/24 after the granary is finished, and therefore we end up with 8 KeptFood. So we would be losing ground unless those two extra tiles we work somehow generated 4 food of value (not very common).

If we whip a turn early, we hit the KeptFood cap, so don't get anything extra on the food side, while giving up two worked tiles. Clearly, that's wrong.

Run the same experiment with a 6 food tile, instead of a 4 food tile, and the same idea gives you a different answer - we still want to hit 12/24 after we whip, so we whip at 6/28 -> 6/24; after the turn we are at 12/24. Two turns later we are again at size 3 with 12 KeptFood.

The numbers here came out so neat because we just happened to have a food surplus that exactly divided the amount of food we needed to grow. With a five food tile, the math gets messier.

If we whip right away, then grow for 5 turns, we'll be at 13/26 (12 kept food plus 1 food of overflow). If we whip at 5/28, then we get the same result, but with two extra turns on the tiles. So that solution dominates.

If we whip at 10/28, that takes us to 10/24; when the granary is finished, we're at 15/24. Two more turns give us 10 KeptFood, instead of 12, plus one overflow -- so we've traded two food for two extra worked tiles. That might be OK if the tiles are good enough (again, not likely).

15/28 would be a bad whip point - that would take us to 15/24, when the granary finishes, we're at 20/24. So the final growth phase puts us at 6/26 - we've traded away 5 kept food for two additional worked tiles. That's a much worse trade than the 10/28 case.


That's the basic idea of how to work it out. However, the problem gets more complicated when you start considering tile assignments that change your surplus food. For instance, if the non food tiles are mines, or a combination of mines and farms, then it's no longer the case that you always grow in the same number of turns.
 
The optimal (latest) food level when you complete your granary is n+F, where n is the size and F is the food per turn. Why? Because if you stop growth the last turn you have 2n food, then next turn you get 2n+F food. And you need n food to fill your granary.

Everything less than that will equally fill the granary, so feel free to work your extra tile until the turn before that point. However, sometimes whipping earlier is better because you have no happiness bonus.

I had a thread about optimal granary growth strategies, but I think stop growth is going to be universally more effective with getting the most of out of non-optimal scenarios.

For example, voice had an example where he starts at 20/24 with +5 food. Stop growth grow with (4 or ) 5 food, then grow next turn, and your granary starts you with 10 out of 12 food.
 
Case 2
For example... size 5, 10 surplus -> 19
Spoiler :
NORMAL SPEED
Code:
	[B][COLOR="Red"]Food surplus[/COLOR][/B]
[B][COLOR="blue"]Size	[/COLOR][/B][B][COLOR="red"]1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20[/COLOR][/B]
[B][COLOR="Blue"]1[/COLOR][/B]	11	11	12	13	11	15	14	13	12	11	21	21	21	21	21	21	21	21	21	21	
[B][COLOR="blue"]2[/COLOR][/B]	12	13	14	15	13	17	16	15	14	13	12	23	23	23	23	23	23	23	23	23	
[B][COLOR="blue"]3[/COLOR][/B]	13	13	13	13	15	13	18	17	16	15	14	13	25	25	25	25	25	25	25	25	
[B][COLOR="blue"]4[/COLOR][/B]	14	15	15	15	17	15	20	19	18	17	16	15	14	27	27	27	27	27	27	27	
[B][COLOR="blue"]5[/COLOR][/B]	15	15	17	17	19	17	15	21	20	19	18	17	16	15	29	29	29	29	29	29	
[B][COLOR="blue"]6[/COLOR][/B]	16	17	16	19	16	19	17	23	22	21	20	19	18	17	16	31	31	31	31	31	
[B][COLOR="blue"]7[/COLOR][/B]	17	17	18	17	18	21	19	17	24	23	22	21	20	19	18	17	33	33	33	33	
[B][COLOR="blue"]8[/COLOR][/B]	18	19	20	19	20	23	21	19	26	25	24	23	22	21	20	19	18	35	35	35	
[B][COLOR="Blue"]9[/COLOR][/B]	19	19	19	21	22	19	23	21	19	27	26	25	24	23	22	21	20	19	37	37	
[B][COLOR="blue"]10[/COLOR][/B]	20	21	21	23	24	21	25	23	21	29	28	27	26	25	24	23	22	21	20	39	
[B][COLOR="blue"]11[/COLOR][/B]	21	21	23	21	21	23	27	25	23	21	30	29	28	27	26	25	24	23	22	21	
[B][COLOR="blue"]12[/COLOR][/B]	22	23	22	23	23	25	22	27	25	23	32	31	30	29	28	27	26	25	24	23	
[B][COLOR="blue"]13[/COLOR][/B]	23	23	24	25	25	27	24	29	27	25	23	33	32	31	30	29	28	27	26	25	
[B][COLOR="blue"]14[/COLOR][/B]	24	25	26	27	27	29	26	31	29	27	25	35	34	33	32	31	30	29	28	27	
[B][COLOR="blue"]15[/COLOR][/B]	25	25	25	25	29	25	28	25	31	29	27	25	36	35	34	33	32	31	30	29	
[B][COLOR="blue"]16[/COLOR][/B]	26	27	27	27	26	27	30	27	33	31	29	27	38	37	36	35	34	33	32	31	
[B][COLOR="blue"]17[/COLOR][/B]	27	27	29	29	28	29	32	29	35	33	31	29	27	39	38	37	36	35	34	33	
[B][COLOR="blue"]18[/COLOR][/B]	28	29	28	31	30	31	34	31	28	35	33	31	29	41	40	39	38	37	36	35	
[B][COLOR="blue"]19[/COLOR][/B]	29	29	30	29	32	33	29	33	30	37	35	33	31	29	42	41	40	39	38	37	
[B][COLOR="blue"]20[/COLOR][/B]	30	31	32	31	34	35	31	35	32	39	37	35	33	31	44	43	42	41	40	39

Case 3
For example... size 5, 10 surplus -> 29
Spoiler :
NORMAL SPEED
Code:
	[B][COLOR="Red"]Food surplus[/COLOR][/B]																			
[B][COLOR="Blue"]Size	[/COLOR][/B][B][COLOR="red"]1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20[/COLOR][/B]
[B][COLOR="blue"]1[/COLOR][/B]	12	13	15	17	16	21	21	21	21	21	21	21	21	21	21	21	21	21	21	21
[B][COLOR="blue"]2[/COLOR][/B]	13	13	14	15	18	17	23	23	23	23	23	23	23	23	23	23	23	23	23	23
[B][COLOR="blue"]3[/COLOR][/B]	14	15	16	17	20	19	25	25	25	25	25	25	25	25	25	25	25	25	25	25
[B][COLOR="blue"]4[/COLOR][/B]	15	15	18	19	22	21	20	27	27	27	27	27	27	27	27	27	27	27	27	27
[B][COLOR="blue"]5[/COLOR][/B]	16	17	17	21	19	23	22	29	29	29	29	29	29	29	29	29	29	29	29	29
[B][COLOR="blue"]6[/COLOR][/B]	17	17	19	19	21	25	24	23	31	31	31	31	31	31	31	31	31	31	31	31
[B][COLOR="blue"]7[/COLOR][/B]	18	19	21	21	23	27	26	25	33	33	33	33	33	33	33	33	33	33	33	33
[B][COLOR="blue"]8[/COLOR][/B]	19	19	20	23	25	23	28	27	26	35	35	35	35	35	35	35	35	35	35	35
[B][COLOR="blue"]9[/COLOR][/B]	20	21	22	25	27	25	30	29	28	37	37	37	37	37	37	37	37	37	37	37
[B][COLOR="blue"]10[/COLOR][/B]	21	21	24	23	24	27	32	31	30	29	39	39	39	39	39	39	39	39	39	39
[B][COLOR="blue"]11[/COLOR][/B]	22	23	23	25	26	29	27	33	32	31	41	41	41	41	41	41	41	41	41	41
[B][COLOR="blue"]12[/COLOR][/B]	23	23	25	27	28	31	29	35	34	33	32	43	43	43	43	43	43	43	43	43
[B][COLOR="blue"]13[/COLOR][/B]	24	25	27	29	30	33	31	37	36	35	34	45	45	45	45	45	45	45	45	45
[B][COLOR="blue"]14[/COLOR][/B]	25	25	26	27	32	29	33	31	38	37	36	35	47	47	47	47	47	47	47	47
[B][COLOR="blue"]15[/COLOR][/B]	26	27	28	29	29	31	35	33	40	39	38	37	49	49	49	49	49	49	49	49
[B][COLOR="blue"]16[/COLOR][/B]	27	27	30	31	31	33	37	35	42	41	40	39	38	51	51	51	51	51	51	51
[B][COLOR="blue"]17[/COLOR][/B]	28	29	29	33	33	35	39	37	35	43	42	41	40	53	53	53	53	53	53	53
[B][COLOR="blue"]18[/COLOR][/B]	29	29	31	31	35	37	34	39	37	45	44	43	42	41	55	55	55	55	55	55
[B][COLOR="blue"]19[/COLOR][/B]	30	31	33	33	37	39	36	41	39	47	46	45	44	43	57	57	57	57	57	57
[B][COLOR="blue"]20[/COLOR][/B]	31	31	32	35	34	35	38	43	41	39	48	47	46	45	44	59	59	59	59	59
 
For example, voice had an example where he starts at 20/24 with +5 food. Stop growth grow with (4 or ) 5 food, then grow next turn, and your granary starts you with 10 out of 12 food.

Guess I need to work through that one carefully.

Normal approach.
T4 whip the granary; after the growth phase, we're at 20/24, 0 kept food
T5 after the growth phase, we're at 25/24, 5 kept food. Pop goes up to size 3, food is at 6/26
T6 11/26, with 10 Kept food

"Restrict growth.
T4 whip the granary; again, 20/24, 0 kept food
T5 after the growth phase, we're at 25/24, 5 kept food. The restrict growth mechanic caps us at 24/24, 5 kept food.
T6 29/24, 10 kept food. So we end up at 15/26.

Interesting - 4 food ahead, one tile turn behind. Probably a good trade (once you are already in the 20/24 position).

Still not as good as whipping on T1, though (which produced 13/26 on T5, and by the same assumptions 18/26 on T6, plus an extra tile turn).
 
I've downloaded the WB saves, but haven't had the time to go through them yet, unfortunately. What's the difference between them? Timing, I suppose?
 
They simply demonstrate the case I was speaking about over 3 turns for the four cities.

Top row: Case I approach
Bottom row: Case II approach
 
Copied from another thread. The key is that overflow food gets counted twice under the right conditions...
___________________________________

I have spent some time testing this and I'm not sure this is 100% correct.

0. 15
1. 15+4 (+4)
2. 19+4 (+8)
3. 23+4 (+12)
4. 27+4 (+15!) = 16 food in the bin, size 6​

In this case, you "lost" one food because you capped out at the maximum granary storage of 15 (30 / 2 = 15) even though you could have had 16. This example is correct. The next turn would look like this:

5. 15 (granary storage) + 1 overflow -> 16/32F

The next example is where things get interesting and based on my experience, I think there is a mistake. I'm away from the game so I can't test it, but I'll tell you what I think would hapen

Now try starting at 21 food in the bin.

Food+surplus (+granary)

0. 21
1. 21+4 (+4)
2. 25+4 (+8)
3. 29+4 (+12) = 15 food in the bin, size 6
4. 15+2 = 17 food in the bin - Assuming you have only 0 food tiles for 6th citizen.

So, in this case, the granary gets +4F put into it on turns 1, 2 and 3. But wait, there's more. On turn 3, you have 29/30F @ +4F. The granary has 8F and will add 4 more this turn. You would think that the granary would only have 12F, you would get the +3F overflow and start turn 4 with 12 + 3 = 15/32.

However, those overflow food actually get counted twice, once in the granary storage and again as normal overflow food. So, on turn 4, you would have:

8 + 4 = 12 food in the granary (as expected) + 3F OF (29 + 4 - 30 = 3) for a total of 15F in the granary! Then, the +3 OF food gets counted AGAIN such that on turn 4, you'd have 18/32 F (15F in granary + 3F OF), not 15/32 as expected.

So, if you have a city that has a huge food surplus (+8 or greater), it's possible to complete the granary when the food box is 75% full (or more) and still end up with a full granary on the turn you grow.

As an example (again, not tested but based on my recollection of how it works) let's assume there is 29/30 F +8F when the granary is built. You would put +8F in the granary + 7F (29 + 8 - 30) = 15 food in the granary. We built the granary just before growing and it's full the next turn!! In this example, on the following turn you'd have 15 (granary storage) + 7 OF = 22/32 F. The +7F OF got counted twice!!

I'm not sure how the stop growth button works with respect to granaries since I've never tested it but I have tested this phenemenon where OF food is doubled (assuming the granary isn't already full, of course).

EDIT: Just tested my +8F example and I ended up with 15/32F on the next turn, not the 22/32 I was expecting. I guess I don't understand it as well as I thought... :sad:
 
For your example:
it's basically 29+8+8-30 = 15

Your mistake is in the order of things. The overflow food is added to the granary at the new size, not to the current granary count.

When you hit end turn...

1-update granary content
2-check for growth
3-update size if necessary
4-update granary for new size
 
Your mistake is in the order of things. The overflow food is added to the granary at the new size, not to the current granary count.

When you hit end turn...

1-update granary content
2-check for growth
3-update size if necessary
4-update granary for new size

I dunno - that explanation confuses the hell out of me, and I've read the code....
 
Any clue on what to make less confusing?

An example? The same generic example I've been using...

1-update granary content, for example 12 + 4 = 16 -> 15 (30/12 = 15 is the cap)
2-check for city growth, for example 26+4 = 30 (require 30 to grow)
3-city has enough food, grow 1 size to 6
4- Granary now out of 16, add overflow food to bin (I'm not actually certain of this part without reading the code)
 
Any clue on what to make less confusing?

Well, for me, matching the code more closely would help

So starting 26/30 at +4, 12 kept food

1) Update food bar: 26+4 = 30
2) Update kept food: 12+4 = 16
3) Cap kept food: min(16, .5 * 30) = 15
4) Remove full bar of food, less kept food: 30 - (30-15)
5) Add one pop.

Step #4 is equivalent to
a) remove a full bar of food (aka the "growth threshold") - that leaves the overflow from step 1;
b) then, add back the kept food from step 3

In this example, the final state is 0/32, 15 kept food.


With restricted growth enabled, steps 4/5 are replaced by a cap on the food bar.
 
This example doesn't illustrate the fact that overflow food gets counted twice. I've seen it happen all the time. In the example above, what does the code say would happen in we have 28/30 food, +6F/turn and only 6F in the granary storage?
 
Back
Top Bottom