Page 1 of 1

HELP!Questions about linear planning

Posted: Sun Apr 02, 2023 4:40 pm
by bw0717
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(*)
;
$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;
image.png
image.png (9.23 KiB) Viewed 1629 times
GAMS.rar
(24.31 KiB) Downloaded 53 times

Re: HELP!Questions about linear planning

Posted: Mon Apr 03, 2023 11:49 pm
by dirkse
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

Re: HELP!Questions about linear planning

Posted: Tue Apr 04, 2023 4:38 am
by bw0717
I added some notes and changed some practices
Using mip, I still can't get the results I expect
GAMS.rar
(24.31 KiB) Downloaded 62 times

Re: HELP!Questions about linear planning

Posted: Wed Apr 05, 2023 10:40 am
by bw0717
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 .

w Additional water intake required
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
(1.56 KiB) Downloaded 59 times