Predation Equations

This is something we have been putting off for a while but I have run up against it in making the CPA prototype so we should probably start discussing it.

Problem: The CPA system requires a “predation matrix”. This means that each time step we know what quantity of compounds to transfer from species A to species B based on them predating on each other. So B might eat A a lot so it get 1% per timestep or A might occasionally eat a B so it gets 0.01% per timestep. The answer should be a real number in the set [-1,1] for each species A and B (as negative numbers mean B -> A and positive mean A -> B making the matrix upper triangular and so not so large)

Its relatively straight forward to say this should be “Number of encounters” x “Amount of compounds transferred per encounter”. The number of encounters is really not so bad (we can use the density of the two species in the patch to work it out) but it’s knowing how a fight between them would turn out which is hard.

Constraints: The big constraint here is that we need to be able to produce a result for ANY possible species that auto-evo might generate. Moreover in the later stages we need to be able to produce it for ANY possible species in the whole game (though we don’t have to solve this now). Which makes the space of possible creatures huge. Also whatever mechanism generates the matrix needs to be reasonably computationally cheap because a row of the matrix will need to be rerun each time auto-evo changes a species (because that will change it’s predation relations with all other species in it’s patch). Further the players enjoyment of the CPA system does really rest on the CPA being tuned to the actual gameplay. So if you can easily beat a species yourself then your species should easily beat it, if you lose they should lose etc. The less in sync they are the worse the experience of the CPA is going to be. Also how are agents going to fit into this? Agents have codes which are specifically targeted to different species.

Here are some ideas which have been brought up already:

  • A simulator. Build a little petri dish, put two microbes in it and let them fight it out.

Pros: This is the most accurate possible method.

Cons: Computationally intensive, probably prohibitively so (though this does rather depend on how many species are in each patch)

  • A lookup table. Basically try to split all possible creatures into n groups and then run a simulator between each of these beforehand. Then give the player a lookup table which gives accurate results.

Pros: Simulated and therefore pretty accurate, it’s as good as a simulator if the groups are size 1, if the groups are very large (everything with wings is in one group, for example) then the results will be pretty poor.

Cons: Adaptations made to a species will have no effect on its predatory success unless it moves them to a new group. So there is a problem of “useless intermediate steps” in evolving the creatures which is always a nightmare.

  • A magical algorithm which can predict the outcomes of fights.

Pros: Very computationally cheap and accurate.

Cons: It would probably take a PhD to try to work out the average outcome of fights between one species of fish and one type of bear. (what happens if the fish is faster or bigger or the bear has better eyesight etc)

  • Some sort of points based system. So you get +1 point for each pilus and +1 point for each agent. Your points determine your strength and the two strengths are used to determine the result.

Pros: Cheap and super fast

Cons: Not really very accurate, may end up with ridiculous adaptations where a cell has 4000 pilus and is an ultra predator. Again we need to be careful because whatever incentives we set up in this system auto-evo will try to satisfy. If we say being bigger will make you stronger then over time the cells will just get bigger and bigger.

We could also have a dynamic points system which looks at what is happening in the patch and then awards points differently based on that (if everyone has a lot of pilli maybe they are only worth a few points but if you are the only species with them then they are worth more etc).

  • Points based + decision tree. So maybe different behaviours matter (which is something which a simulator captures) and there should be a tree. The tree could include lots of different behaviours.

Example: If A and B fight the result is 0.1 but seeing as A is faster it is offered the chance to flee instead and this gives a result of 0.05 which is preferable.

Pros: More accurate than a simple points based system.

Cons: Priority, what if A wants to fight if B wants to flock but B wants to flee if A wants to fight. Who has to choose first? For fleeing maybe the faster microbe is offered that choice but the other one is not however with other behaviours (crawl into a ball vs fight) the creature will always have those options.

Anyway all input welcome, it’s a pretty complicated question and it’s very important because this is one of the main ways the CPA mirrors the experience the player has while swimming around and so we want it as good as we can get it.

2 Likes

I don’t think it’s enough for the compound balance to lean simply one way or the other (between predator and prey). I think we need to track whether the average compound cost of, say, fleeing a predator outweighs the average compound cost of standing and fighting, or the average compound cost of hiding, or even of simply accepting the damage. And on the predator side, we need to know if, say, chasing down fleeing prey is even, on average, worthwhile.

I think we should use a decision tree (or maybe something more like a state machine), at least for part of the system – there’s a not-too-large number of possible choices each organism might make when they encounter each other, and most of the decisions would be relatively simple:

  • Both might choose to ignore each other: I wager that this would cover the majority of interactions in practice. Sessile organisms might simply have no way to instigate an interaction (for example, a tree won’t proactively avoid or attack its predators, though it might have some special pods of nerve gas that it releases when attacked), and some calculation would tell us whether one species will even have an interest in the other.
  • One organism might choose to attempt to eat the other. We can decide if the energy balance makes it reasonable for the prey to flee, or hide, or fight back, or take other defensive measures. And then, for the predator, we can decide if it’s worth chasing, or how much energy it takes to find a hiding prey, etc.
  • Only for the cases where we expect a fight to ensue would we have to run some sort of combat sim (however complicated it might be).
  • Based on that result (how costly it is for each) we decide if it’s worth it for either – for example, if it isn’t worth it for the predator then the predator simply wouldn’t try to hunt that prey (or maybe, the rate is scaled strongly downwards to allow for occasional predation by starving, desperate predators). This way we decide what the actual behaviours would be in practice.
  • Using those behaviours and their costs, we build that predation matrix for compound transfers between species, and add the cost of predation/fleeing/etc to the list that stores average energy consumption for each species.

We probably also have to include a measure of opportunity cost – for prey, it may make sense to flee rather than hide, because you can’t eat while hiding, and the predator may be able to wait long enough to starve you out; while for predators, it makes sense to pick the easiest sources of all the nutrients you need.

1 Like

Yeah ok I think we’re thinking along similar lines.

I think sessile organisms should be allowed to be predators and have a behaviour called “trap”, like a venus fly trap or something like that.

I still think there is the problem of who chooses which sequences of behaviour are played out. So what if species A would prefer to Hide when species B is fighting but would prefer to fight if species B is rolled into a ball.

Like what if the payoffs are

Hide + Fight = 1,0
Hide + Ball = 0.5,0.5
Fight + Fight = 0,0
Fight + Ball = 1,0

If A chooses Hide first then B will choose Ball. But if B chooses ball first then A will choose fight. There’s no clear choice here.

A couple of possible solutions include running the simulation both ways, so A chooses first and then B gets it’s preferred response and then B chooses first and A gets it’s preferred response. Or having “initiative” which you can increase somehow.

For now, just for this prototype, I think I’ll work towards “You can either both fight or the faster one of you can get a reduction in this value by fleeing”. For larger, more complex, creatures this is not going to be nearly enough.

1 Like

Prisoners’ dilemma. Good point.

What we need is to find the evolutionarily stable strategy for each species in each pairing between species, and I think we need to assume that each species will contain a mix of strategies, otherwise we get prisoners’ dilemmas.

Reading up on evolutionary game theory on wikipedia, I came up with another idea: Instead of comparing potential mutations by testing each to see whether the new set of optimal behaviours is better than the current set, what if we started with the current set of behaviours a species engages in, then we graded potential mutations by testing whether they’d make those behaviours better, and then we pick the best, and generate a new set of optimal behaviours for the mutated species, and then we’re done. Essentially, a mutation that would make a nonexistent behaviour extremely good while harming all other behaviours would never happen, which we can justify because it would be extremely unlikely in nature (and if we want such things to be occasionally possible we could just throw in some random fudging to occasionally allow for more wacky mutations).

The big benefit of this idea is that we’d be able to update a species incrementally – a big pile of equations would tell us how a mutation changes the total effectiveness of a species’ behaviours; and then when we update the behaviour graph, we could avoid a full recomputation by doing an iterative, greedy refinement of the current one. It’s fast, and while it wouldn’t be optimal, evolution is also iterative, greedy, and suboptimal, so I think it’s fine.

If we break the fight sim up into another big graph of behaviours, then this same system applies to the fight sim. Same goes for all things a species does – if we can break down all the things a species might do into behaviours, and have a method to quantify how much each behaviour happens (maybe how much time an organism spends doing it, or what proportion of organisms would do it when the opportunity arrives), and how effective at it the species is, then we can model auto-evo this way.

I’m not sure I really understand. Could you maybe give an example of how this system might work?

Say the Species Blarglefart currently hunts Smoot and Smeet. When it encounters either, it spews toxins and charges in to stab with pili.

Then Blarglefart evolves, and it has a few potential mutations. Maybe one of them is a modified toxin. Maybe that toxin will make it more efficient for Blarglefart to hang back and wait for its prey to die instead of charging in. But under my proposed system, the mutation would only be chosen if it makes the current set of behaviours Blarglefart engages in to be more efficient. And it isn’t until after a mutation is chosen that makes the current set of behaviours better; that a new set of behaviours is computed. So, for example, if the modified toxin also allows it to hunt Blurp, and the effort spent hunting Blurp has a better payoff than ignoring Blurp encounters and sticking to Smoot and Smeet, then Blarglefart will start hunting Blurp too, perhaps even exclusively, but only if the mutation helped it do something it already did. We first mutate the body to better fit the behaviours, then we mutate the behaviours to better fit the body.

This differs in that we don’t recompute optimal behaviours for each potential mutation, meaning we don’t have to run multiple fight sims – indeed, if fights are modelled as decision trees, then we don’t even have separate fight sims, but just have them included in the rest of the code which goes through the behaviour tree and checks how the mutation affects it.

The final step, where a new set of behaviours is computed, can be done by incrementally modifying the decision tree that the species currently uses.


As for the datastructure: We’d have a big set of possible, simple behaviours. There would be behaviours controlling how an organism navigates around the world (for example, random walk, does it avoid anything, etc), behaviours directing how it looks for and gets food, behaviours that cover all the possible simple things organisms might do when they encounter another organism (flee, chase, release toxins, aggressive display, etc), behaviours involving shelter, water, poop, mating, marking territory, and so on. Each species would have a graph that dictates, effectively, a state machine of how a member of that species acts under different circumstances. This state machine, most simply, would be used to direct the behaviour of instances of the species in the game; but it’s also used to dictate evolutionary fitness.

Every behaviour would have some complex formulas that provide measures of how effectively a species with a particular body plan can do that behaviour. For many, it would be the energy-effectiveness, and perhaps also time-effectiveness, of the behaviour.

We’d compute the total fitness of the species by walking its entire behaviour graph, putting all that effectiveness information together, to give us a measure of how quickly the population can grow. We’d need to know how much the members of the species perform each action (how often, how long they spend, etc), but I think that’s relatively easy to do by looking up encounter rates for encounters, and using the specific characteristics for other behaviours (like sleep), and should only require one pass.

Now, while each species would only have one behaviour graph for the entire species, the different circumstances between patches will lead to separate populations spending different amounts of time doing different things – this is the driver for speciation, since a mutation which improves the hunting of, say, shore fish might reduce the fitness of the species in hunting, say, antelope.

The final step, once a mutation is chosen and the body plan of the species is changed, is to see if the mutation gives rise to any new, easy-to-develop behaviours that are better than what it already does. So we walk through the behaviour graph, and if at any point there is a potential behaviour whose benefits outweigh the potential opportunity costs of doing the other behaviours less, then we add that behaviour too, and potentially add more behaviours from that. I think it would require two passes – one forwards to see if there are any new behaviours to add to the graph, and one backwards if any were added, to update the measures of how often each is done. This second pass could just be done at the start of the next auto-evo step though.

I guess I’ve added enough detail that the main point is getting a bit lost, so I’ll reiterate it: We first mutate the body to better fit the behaviours, then we mutate the behaviours to better fit the body. This gives us speedups since we don’t need to compute a new behaviour graph for every possible mutation; and while it misses certain cases that could theoretically happen, I think it fits with how nature works, since a mutation doesn’t become dominant by making an organism very good at something it simply doesn’t do (which does not increase fitness), unless it also makes the organism better at something it does already do (which does).

Ok, sorry I am having some trouble keeping up and have a few questions.

Is each species storing data on how it deals with each other. So does it say somewhere, “if A meets B then do behaviours X, Y and Z” or does it say “when A fights with anyone do behaviours X, Y and Z”?

Either way how do we compute the effectiveness of this? How do we know how well a fight between Blarglefart and Smoot will go? We still need a way of looking at the blueprint of the species + it’s behaviours and working out who would win in a fight between any two conceivable species.

Moreover how do we compute the effectiveness of any of this? You say

“since a mutation which improves the hunting of, say, shore fish might reduce the fitness of the species in hunting, say, antelope.”

yet I don’t think we’ve really solved the problem of computing the outcomes of this yet.

I agree with you that if we have a method for computing the outcome of fights + computing the effectiveness of burrowing to make yourself invisible + computing the effectiveness of leaving scent trails for potential mates to find + etc etc then we can build all this into a coherant system. I just don’t know how to compute any of these things.

That’s the kind of example that would really help me. If you are given two species how are you going to compute the outcome of a fight between them? If there is a mutation like “make the back limbs 10% longer” are you hoping that will have a measurable effect on the outcome of a fight which involves that species? What sort of system can have that level of sensitivity and be broad enough to encompass all possible species?

Yeah, the behaviour graph would have different behaviours for encounters with every species. Many of these would be “ignore”, probably, which keeps it from growing too big on average.

As for fights, I’ll just note that even if we use a full fight sim, where we put the two creatures in a little arena etc, the things they do in there will be behvaiours that are part of the behaviour graph, though we might evaluate the effectiveness of the fight as a whole.

As for everything else, I think you get exactly what I’m saying. The key point I was making was not that there’s a way around having to figure out how to compute the effectiveness of every single behaviour, but that there’s a way to make the broad algorithm more efficient.


I think, to figure out how each the effectiveness of each behaviour varies between different bodies, we’ll probably have our work cut out for us doing them one at a time. Don’t think there’s any way around that.

For Microbe Stage, though, we’ll (almost) definitely have a lot fewer behaviours, and they’re probably simple enough that we could reduce most to a simple calculation.

For example, the effectiveness of all Microbe swimming behaviours (wandering, searching, escaping, chasing, etc) will boil down to how good (in terms of speed and energy-efficiency) it is at swimming, and at turning. Each of those is a simple function (compared to what we’d have to do for macroscopic creatures, definitely!).


As for a better example: let’s take your example, making the back legs a certain amount longer. Say we have a behaviour for kicking/punching – striking something with the ends of limbs, using the limbs and a bit of body momentum for the force. We’d probably calculate effectiveness for it by iterating down the limb, figuring out how much energy each muscle involved could impart, and then adding in some fudge factors.

I figure a lot of the behaviours will be doable like that – not through a simple equation, but through a short simulation of just as much as is necessary.

Ok. I think the system you propose is really cool and I think I understand.

Can we work on a simple problem for a bit? For example:

Two identical microbes, except for the fact that one has two pilli and the other one, neither of which have agents, both of which have four flagella, fight to the death. Who wins and how much damage does the winner take? What percentage of the losers compounds are lost to the environment and what percentage are picked up by the winning microbe?

1 Like

We might have behaviours like:

  • Offensive stab: charge in on your target with a pilus, trying to stab it. Effectiveness is determined by finding the fastest direction you can charge in, pilus-first, and seeing how quickly you can do so, and how energy-efficient that is.
  • Defensive stab: rotate a pllus towards an enemy coming from an arbitrary direction. Effectiveness depends on how quickly you can turn and how well-distributed your pili are.
  • Flee: Run away! Run awaay! Effectiveness depends on how fast you can go at fastest, and how energy-efficient it is.

In each case, effectiveness includes both measures of whether you’ll succeed at all (if you charge slower than they can flee, no dice, you won’t succeed), and whether it’s worth it (eg, if the energy costs of charging in with pili are not worth the average amount of compounds you’re likely to get (maybe the prey is tiny), then it would be better overall not to bother).

To see who wins, who dies, etc, we’d build two interlocking decision trees, one for each species. If species 1P (one pilus) charges in on species 2P (two pili), what does 2P do? Maybe it considers the fleeing behaviour but discards it because 1P is faster and can swim more efficiently (due to being one pilus lighter, but 2P doesn’t need to know that, only the numbers). Then maybe it considers the defensive stab, which will work pretty well because the two pili are pretty-well spread apart, meaning it’s not too hard to turn one to face the attacker. So 2P chooses defensive stab, and now 1P must choose a reaction. And so on and so on.

If we want to have more than just one possible reaction for each action, and instead have a distribution of reactions, then we’d have to give each possible reaction a probability score based on how effective a reaction it is. So then, maybe sometimes 2P flees, hoping 1P doesn’t have the compound reserves to chase properly, and sometimes it tries a defensive stab.

Whether or not we do that, we’d in the end have a result where either the prey is dead, or the predator gives up because it’s not worth the energy.

As for your questions, I’m not really sure how this system will answer them, it seems like those particular questions are partly orthogonal and partly to do with the specifics of certain behaviours. But maybe I just need to think about it after a full night’s sleep.