\(x_1, ..., x_k >= 0\) (realistic diet intake can not be negative)
\(x_1e_1 + x_2 e_2 + ... + x_k e_k >= E_{lower}\), total energy above the lower limit
\(x_1e_1 + x_2 e_2 + ... + x_k e_k <= E_{upper}\), total energy below the upper limit
\(x_1p_1 + x_2 p_2 + ... + x_k p_k >= P_{lower}\), total protein below the upper limit
\(x_1p_1 + x_2 p_2 + ... + x_k e_k <= P_{upper}\), total protein below the upper limit
And so on.
Solve the optimization problem
This setting is a quadratic program (QP). It is an optimization problem with quadratic objective, and inequality constraints. We do not have equality constraints in this setting.
With R, there are various software to find a solution:
CPLEX (chosen one)
nloptr in nloptr package (non-linear optimization),
constrOptim in stats package, which relies on optim function,
The CPLEX solver aims to solve the optimisation problem:
\[min \frac{1}{2} x' Q x + c'x\] subject to
\[Ax \leq b\]
\[lb \leq x \leq ub\]
The program in R looks like this
library(Rcplex)# the values needs to be availableresult_obj <-Rcplex(cvec = cvec,Amat = Amat,bvec = bvec,Qmat = Qmat,lb = lb,ub = ub,objsense ="min",sense = sense)# the result is saved in result_obj, to see the resultsresult_obj
1
cvec: vector, current diet values (multiplied by -2)
2
Amat: matrix, individual food contribution to total nutrition and environmental impact
3
bvec: vector, constraint values for nutrition and environmental impact for total diet
4
Qmat: matrix
5
lb, ub: lower and upper bound for target diet
6
objsense, sense: E, L, and R instead of ==, <= and >=