Problems with modeling
bw0717
User
Posts: 4
Joined: 2 months ago

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.

\$CALL GDXXRW.EXE D:\GAMS\test.xlsx @D:\GAMS\homework.txt output=D:\GAMS\homework.gdx
\$GDXIN D:\GAMS\homework.gdx

sets
i(*)
j(*)
;
display i,j;
parameters
C(i)
N(i,j)
L(j)
;
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;
image.png (9.23 KiB) Viewed 487 times
GAMS.rar
Last edited by bw0717 1 month ago, edited 1 time in total.
dirkse
Moderator
Posts: 204
Joined: 6 years ago
Location: Fairfax, VA

### Re: HELP！Questions about linear planning

Hello,

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

Code: Select all

``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.

HTH,

-Steve
bw0717
User
Posts: 4
Joined: 2 months ago

### Re: HELP！Questions about linear planning

I added some notes and changed some practices
Using mip, I still can't get the results I expect
GAMS.rar
bw0717
User
Posts: 4
Joined: 2 months ago

### Re: HELP！Questions about linear planning

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= 50000*p(i);

---- VAR x Foods variable

LOWER LEVEL UPPER MARGINAL

1 . 210.315 +INF .
2 . . +INF 39.236
3 . 2.523 +INF .
4 . . +INF 7.599
5 . 4502.807 +INF .

LOWER LEVEL UPPER MARGINAL

---- VAR w . . +INF EPS
---- VAR z -INF 721.831 +INF .

z The purchase cost of each food item is in NT dollars

---- VAR p binary variables of i

LOWER LEVEL UPPER MARGINAL

1 . 1.000 1.000 EPS
2 . 1.000 1.000 EPS
3 . 1.000 1.000 EPS
4 . . 1.000 EPS
5 . 1.000 1.000 EPS

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