Sorry. As said before, I'm not a programmer, but I do like to think about improving the AI by employing decent approximations. I'd likely prefer an average over a minimum or a maximum. iiNumDefenders / iNumCities is an underestimate as the AI will distribute the units in a better way over the cities, in a way that it gets more happiness advantages. AI_neededDefenders is an overestimate as the AI will often not own the units to get that preferred number of defenders. An average seems to be a decent approximation of the actual happiness advantage. I'm not a programmer so I didn't try to decipher all of the code. But if this causes the AI under hereditary rule to continuously create more units to fight unhappiness then that might not be such a good idea. The cost of building and maintaining a unit plus the food required to create another citizen in a city (which could have been hammers or commerce) is often larger than the advantage of the temporary extra citizen during the period that the AI is using the hereditary rule civic. At some point the investment in temporary units for temporary happiness and the cost of creating temporary citizens is just not worth it. After the hereditary rule civic has been replaced by another civic, the extra defenders are superfluous and costly and the extra citizens can't be kept happy. I typically don't use more than 3-4 units per city during hereditary rule and will often use even less units. It's nice to get some happiness bonus from the defenders in your cities, but it's typically not a good tactic to start creating units in great numbers to create happiness.