Possible solution to suspected optimisation problem in python or R for animal emissions
Ok, I think this can be closed. Some more inspection revealed that this problem can be represented as a system of linear equations, which is then easy to solve using matrix notation. No need for any optimisation code as there is no nonlinear behaviour in this problem.
I would like to see if someone can help me with a problem on optimising an emissions model, to essentially change the input conditions to match measured outputs.
The issue is the following. I have the following model data:




Treatment group
# of animals
Treatment efficiency
Model emissions




Control
110
1
110


A
88
0.5
44


B
22
0.25
5.5




The model predicts emissions from a barn with a set number of animals in it. In this case, there are 220 animals and the predicted TOTAL emissions are 159.5 units. Each animal emits 1 unit of emissions, unless the treatment applied reduces emissions by a set fraction, as indicated in the treatment efficiency for groups A and B.
However, experimental measurements show TOTAL emissions to be 13.5 higher at 173.0
From known information, we can confirm that the number of animals is definitely 220 and treatment efficiency is not in doubt. Therefore, the only variable that can explain this is that animals are not assigned to treatment groups as they are in the model. If more animals are part of the control group and less part of treatment groups A and B, then the emissions will go up and can approach the correct measured value.
I am not very well versed in math, and too often I ran into things like this which ended up having more simple solutions. But my original model is in Excel and I don't think there is a way to do this with simple formulas. It feels like an optimisation problem to me. For example, if I add the 13.5 emissions to the control group, bringing total emissions to 123.5 for control and total emissions to the correct 173.0, then the number of animals in the control group has to become 123.5, to make sure emissions from the control group return to the max of 1 per animal. We can only get there by moving animals from groups A and B.
I do this once under the constraint that animals have to be moved proportionally to treatment efficiency. This results in the following table after 1 iteration, moving 10.8 animal from group A and 2.7 (lets assume we can split up animals for the moment):




Treatment group
# of animals
Treatment efficiency
Model emissions
Emission per animal




Control
123.5
1
123.5
1


A
77.2
0.5
38.6
0.56


B
19.3
0.25
3.825
0.28




Now, with the same number of animals, total emissions are 166.9. Closer to the observed value of 173.0. I can continue doing this and assume would approach to the right distribution of animals.
Note that my real life problem is bigger than this, with up to 5 treatment groups and I have to repeat this for about 120 different setups of animals and treatment efficiencies. Hence I would like to automate this to find a way to optimise it towards the new correct distribution of animals. I think this can be done given we add the constraints:

Constraint #1: Number of animals in treatment groups can only change proportionally.

I assume without this constraint there is an infinite number of solutions.

Constraint #2: If observed emissions are higher than modelled emissions, number of animals in the control group can't decrease.

I think that is impossible for this to even happen with constraint #1 so this constraint may be superfluous, but in my real life problem this is the case so I added it anyway. There are no cases in my problem where observed emissions are lower than modelled emissions.
I added R and python to this as those are the programming languages I know, though I haven't used it like this before for an optimisation problem so I would not really know where to start. Can anyone give any pointers?