HELP! I have two questions.
This is a question about daily nutrient.
1.When you can only choose to eat one item a day, what do you need to eat to satisfy your nutrition?
2.When you have to eat three different foods every day, how do you choose to meet your nutritional needs?
*Saturated fat no more than 22
I tried for a long time and still can’t solve it.
I wonder what equation needs to be added to the equation to solve the problem of choosing only one item and choosing three different items.
sets
i()
j()
;
$LOAD i j
display i,j;
parameters
C(i)
N(i,j)
L(j)
; LOAD C N L
display C,N,L;
positive variables
x(i)
w
;
free variable
z
;
equations
obj
Nutrition1(j)
Nutrition2
Nutrition3
;
obj.. z =e= sum(i,C(i)*x(i));
Nutrition1(j)(ord(j)>=1 and ord(j)<=3)… sum(i,N(i,j)*x(i)) =g= L(j)$(ord(j)>=1 and ord(j)<=3) ;
Nutrition2… sum(i,N(i,‘4’)*x(i)) =l= L(‘4’);
Nutrition3… w +sum(i,n(i,‘5’)*x(i)) =g= L(‘5’);
*option limrow = 1000000;
model Ne /all/
solve Ne using lp minimizing z;
If you use better names, the model explains itself. This can be very useful when starting to write models. For example, I would expect a model about diets to have a set for the possible foods to choose from. This set could better be named
food[/code than i or j. Ditto for the set of nutrients: maybe [code]nutr
is a good name. Use good descriptions of the sets too (the trnsport.gms model from GAMSLIB does this).
Same sort of comments apply to the parameters, variables, and equations you use.
If you are struggling to get anything working, simplify the problem. Throw out the requirement that there be a limit on the number of foods to include in the diet. Now you have a linear model and you can ignore the binary variables pick() for the short term.
Thank you very much to the people who helped me.
The problem has largely been solved.
But there are still some small problems to be improved.
equations
obj ‘Cost summary’
Nutrition1(j) ‘Identify foods from the matrix that meet the nutritional requirements’
Nutrition2 ‘Upper limit of saturated fat’
Water ‘Water requirements’ Selection ‘Choose only one of the foods’
Cce ‘Connectivity constraint equation’
;
obj… z =e= sum(i,C(i)x(i));
Nutrition1(j)(ord(j)>=1 and ord(j)<=3).. sum(i,N(i,j)*x(i)) =g= L(j)(ord(j)>=1 and ord(j)<=3) ;
Nutrition2… sum(i,N(i,‘4’)x(i)) =l= L(‘4’);
Water … w +sum(i,n(i,‘5’)x(i)) =g= L(‘5’);
**Selection… sum(i, p(i)) =e= 4;
Cce(i)… x(i) =l= 50000p(i);
When sum p(i)>3, x(i) cannot make a corresponding choice corresponding to p(i).
It will keep the value in the sum p(i)=3 condition.
I don’t know how to require it to give only the optimal solution for p(i) corresponding to the condition.
I would like to thank all the people who have given me advice and instruction, thank you very much! homeworkF.gms (1.56 KB)