Code: Select all
embeddedCode Python:
var_x = list(gams.get('x'))
print(var_x)
endEmbeddedCode
Code: Select all
embeddedCode Connect:
- GAMSReader: # read variable x from GAMS into the Connect database
symbols:
- name: x
- Projection: # turn variable x into parameter x_new with level values only
name: x.l(i,j)
newName: x_new(i,j)
asParameter: True
- CSVWriter: # write x_new to CSV
file: x_level.csv
name: x_new
endembeddedCode
Code: Select all
embeddedCode Python:
var_x = list(gams.get('x'))
print(var_x)
endEmbeddedCode
Code: Select all
embeddedCode Connect:
- GAMSReader: # read variable x from GAMS into the Connect database
symbols:
- name: x
- Projection: # turn variable x into parameter x_new with level values only
name: x.l(i,j)
newName: x_new(i,j)
asParameter: True
- CSVWriter: # write x_new to CSV
file: x_level.csv
name: x_new
endembeddedCode
Code: Select all
set i 'departments' / i1*i5 /
j '8 hour slice' / j1*j8 /
t 'one week in hours' / t1*t168 /;
parameter d(i,t) 'demand'; d(i,t) = UniformInt(1,10);
parameter d8(i,t) 'demand for the next 8 hours';
d8(i,t) = sum(j, d(i,t++(ord(j)-1)));
display d,d8;
Code: Select all
set i 'departments' / i1*i5 /
j '8 hour slice' / j1*j8 /
t 'one week in hours' / t1*t168 /;
parameter d(i,t) 'demand'; d(i,t) = UniformInt(1,10);
parameter d8(i,t) 'demand for the next 8 hours';
d8(i,t) = sum(j, d(i,t++(ord(j)-1)));
display d,d8;
Code: Select all
c1 c2 c3 c4 c5 c6
r1 0 1 0 0 0 1
r2 0 1 0 0 0 1
r3 0 1 1 0 0 1
r4 1 1 1 0 0 1
r5 1 0 1 0 0 0
r6 1 0 1 0 0 0
r7 1 0 0 0 1 0
r8 0 0 0 0 1 0
r9 0 0 0 0 1 0
r10 0 0 0 0 1 0
Code: Select all
c1 c2 c3 c4 c5 c6
r1 0 1 0 0 0 1
r2 0 1 0 0 0 1
r3 0 1 1 0 0 1
r4 1 1 1 0 0 1
r5 1 0 1 0 0 0
r6 1 0 1 0 0 0
r7 1 0 0 0 1 0
r8 0 0 0 0 1 0
r9 0 0 0 0 1 0
r10 0 0 0 0 1 0
Code: Select all
set i /r1*r10/, j /c1*c6/, iter /1*3/;
parameter
rowstart, cnt,
randval(i,j) Random values,
randvalloop(iter, i,j) Store the random values;
$set seed 123456
loop(iter,
loop(j,
rowstart = uniformInt(1,card(i)-3);
loop(i$(ord(i)=rowstart),
for (cnt=0 to 3,
randvalloop(iter, i+cnt,j) = 1;
)
)
)
* No start in any column
if (sum(j, randvalloop(iter,'r1',j))=0, randvalloop(iter,i,'c1') = ord(i)<5);
* No end in any column
if (sum(j, randvalloop(iter,'r10',j))=0, randvalloop(iter,i,'c6') = ord(i)>card(i)-4);
);
* Check the results
option randvalloop:0:1:1;
display randvalloop;
Code: Select all
set i /r1*r10/, j /c1*c6/, iter /1*3/;
parameter
rowstart, cnt,
randval(i,j) Random values,
randvalloop(iter, i,j) Store the random values;
$set seed 123456
loop(iter,
loop(j,
rowstart = uniformInt(1,card(i)-3);
loop(i$(ord(i)=rowstart),
for (cnt=0 to 3,
randvalloop(iter, i+cnt,j) = 1;
)
)
)
* No start in any column
if (sum(j, randvalloop(iter,'r1',j))=0, randvalloop(iter,i,'c1') = ord(i)<5);
* No end in any column
if (sum(j, randvalloop(iter,'r10',j))=0, randvalloop(iter,i,'c6') = ord(i)>card(i)-4);
);
* Check the results
option randvalloop:0:1:1;
display randvalloop;
Code: Select all
set t /1*10/, i /1*6/, [b]iter /1*1/[/b];
parameter
rowstart, cnt, q,
randval(t,i) Random values,
randvalloop(iter, t,i) Store the random values
$set seed 123456
loop(iter,
q = 6
loop(i,
rowstart = uniformInt(1,card(t)-(q-1));
loop(t$(ord(t)=rowstart),
for (cnt=0 to (q-1),
randvalloop(iter, t+cnt,i) = 1;
)
)
)
* No start in any column
if (sum(i, randvalloop(iter,'1',i))=0, randvalloop(iter,t,'1') = ord(t)<(q+1));
* No end in any column
if (sum(i, randvalloop(iter,'10',i))=0, randvalloop(iter,t,'6') = ord(t)>card(t)-q);
);
* Check the results
option randvalloop:0:1:1;
display randvalloop;
Code: Select all
set t /1*10/, i /1*6/, [b]iter /1*1/[/b];
parameter
rowstart, cnt, q,
randval(t,i) Random values,
randvalloop(iter, t,i) Store the random values
$set seed 123456
loop(iter,
q = 6
loop(i,
rowstart = uniformInt(1,card(t)-(q-1));
loop(t$(ord(t)=rowstart),
for (cnt=0 to (q-1),
randvalloop(iter, t+cnt,i) = 1;
)
)
)
* No start in any column
if (sum(i, randvalloop(iter,'1',i))=0, randvalloop(iter,t,'1') = ord(t)<(q+1));
* No end in any column
if (sum(i, randvalloop(iter,'10',i))=0, randvalloop(iter,t,'6') = ord(t)>card(t)-q);
);
* Check the results
option randvalloop:0:1:1;
display randvalloop;
Code: Select all
Error 59 Endogenous prod smin smax require model type "dnlp"
Error 256 equation eq2.. VAR prod smin smax
Code: Select all
i1 i2 i3 i4 i5
f1.t1 7 5 6 0 6
f1.t2 8 4 6 1 6
f1.t3 6 7 3 2 4
f2.t1 8 7 5 3 1
f2.t2 0 5 5 7 0
f2.t3 0 8 0 5 9
Code: Select all
eq2(f,i).. last(i) =e= smax((t),EM(f,t,i));
Code: Select all
Error 59 Endogenous prod smin smax require model type "dnlp"
Error 256 equation eq2.. VAR prod smin smax
Code: Select all
i1 i2 i3 i4 i5
f1.t1 7 5 6 0 6
f1.t2 8 4 6 1 6
f1.t3 6 7 3 2 4
f2.t1 8 7 5 3 1
f2.t2 0 5 5 7 0
f2.t3 0 8 0 5 9
Code: Select all
eq2(f,i).. last(i) =e= smax((t),EM(f,t,i));
Can you help me reformulate a non-linear constraint of a mathematical optimization model using the max operator like x(i) = max(j, y(i,j)) into linear constraints with additional binary variables?
ChatGPT even gives you some GAMS codeSure, I can help with that. To reformulate a non-linear constraint using the max operator into linear constraints with additional binary variables, you can use a common technique called "big M" method.
Here's how you can do it:
Let's say you have a non-linear constraint of the form:
x(i) = max_{j} (y(i,j))
You can reformulate it using the following linear constraints:
1. Introduce a binary variable z(i,j) for each pair of indices i and j.
2. Add a constraint ensuring that z(i,j) = 1 if and only if y(i,j) is greater than or equal to all other values of y(i,.):
y(i,j) - y(i,k) <= M (1 - z(i,j)), for all j, k where j <> k
3. Add a constraint enforcing that z(i,j) = 1 if and only if x(i) = y(i,j):
x(i) - y(i,j) <= M (1 - z(i,j)), for all j
4. Add a constraint ensuring that x(i) is no less than any y(i,j):
x(i) - y(i,j) >= 0, for all j
In these constraints, M is a sufficiently large constant, which acts as a "big M" to ensure that the binary variables z(i,j) and the auxiliary variables do not violate the logic of the original non-linear constraint.
Keep in mind that the value of M should be chosen carefully to ensure that it is larger than the maximum possible value of y(i,j) in your problem. If it is too large, it may lead to numerical instability or suboptimal solutions.
This reformulation allows you to model the non-linear constraint using only linear constraints and binary variables.
Code: Select all
Sets
i /i1*iN/
j /j1*jM/;
Parameters
M /1000/; // Adjust this value based on the problem
Variables
x(i) // Your decision variable
y(i,j) // Your original variable
z(i,j) binary; // Binary variable for reformulation
Binary Variables z;
Equations
non_linear_constraint1(i,j)
non_linear_constraint2(i,j)
non_linear_constraint3(i);
non_linear_constraint1(i,j)..
y(i,j) - y(i,k) - M * (1 - z(i,j)) =L= 0
$(j <> k);
non_linear_constraint2(i,j)..
x(i) - y(i,j) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint3(i)..
x(i) - y(i,j) =G= 0;
Model reformulated_constraint /all/;
Code: Select all
$eolcom //
$set N 10
$set M 15
Sets
i /i1*i%N%/
j /j1*j%M%/;
Parameters
M /1000/; // Adjust this value based on the problem
Variables
x(i) // Your decision variable
y(i,j) // Your original variable
z(i,j) binary; // Binary variable for reformulation
Binary Variables z;
alias (j,k);
Equations
non_linear_constraint1(i,j,k)
non_linear_constraint2(i,j)
non_linear_constraint3(i,j);
non_linear_constraint1(i,j,k)$(not sameas(j,k))..
y(i,j) - y(i,k) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint2(i,j)..
x(i) - y(i,j) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint3(i,j)..
x(i) - y(i,j) =G= 0;
Model reformulated_constraint /all/;
Can you help me reformulate a non-linear constraint of a mathematical optimization model using the max operator like x(i) = max(j, y(i,j)) into linear constraints with additional binary variables?
ChatGPT even gives you some GAMS codeSure, I can help with that. To reformulate a non-linear constraint using the max operator into linear constraints with additional binary variables, you can use a common technique called "big M" method.
Here's how you can do it:
Let's say you have a non-linear constraint of the form:
x(i) = max_{j} (y(i,j))
You can reformulate it using the following linear constraints:
1. Introduce a binary variable z(i,j) for each pair of indices i and j.
2. Add a constraint ensuring that z(i,j) = 1 if and only if y(i,j) is greater than or equal to all other values of y(i,.):
y(i,j) - y(i,k) <= M (1 - z(i,j)), for all j, k where j <> k
3. Add a constraint enforcing that z(i,j) = 1 if and only if x(i) = y(i,j):
x(i) - y(i,j) <= M (1 - z(i,j)), for all j
4. Add a constraint ensuring that x(i) is no less than any y(i,j):
x(i) - y(i,j) >= 0, for all j
In these constraints, M is a sufficiently large constant, which acts as a "big M" to ensure that the binary variables z(i,j) and the auxiliary variables do not violate the logic of the original non-linear constraint.
Keep in mind that the value of M should be chosen carefully to ensure that it is larger than the maximum possible value of y(i,j) in your problem. If it is too large, it may lead to numerical instability or suboptimal solutions.
This reformulation allows you to model the non-linear constraint using only linear constraints and binary variables.
Code: Select all
Sets
i /i1*iN/
j /j1*jM/;
Parameters
M /1000/; // Adjust this value based on the problem
Variables
x(i) // Your decision variable
y(i,j) // Your original variable
z(i,j) binary; // Binary variable for reformulation
Binary Variables z;
Equations
non_linear_constraint1(i,j)
non_linear_constraint2(i,j)
non_linear_constraint3(i);
non_linear_constraint1(i,j)..
y(i,j) - y(i,k) - M * (1 - z(i,j)) =L= 0
$(j <> k);
non_linear_constraint2(i,j)..
x(i) - y(i,j) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint3(i)..
x(i) - y(i,j) =G= 0;
Model reformulated_constraint /all/;
Code: Select all
$eolcom //
$set N 10
$set M 15
Sets
i /i1*i%N%/
j /j1*j%M%/;
Parameters
M /1000/; // Adjust this value based on the problem
Variables
x(i) // Your decision variable
y(i,j) // Your original variable
z(i,j) binary; // Binary variable for reformulation
Binary Variables z;
alias (j,k);
Equations
non_linear_constraint1(i,j,k)
non_linear_constraint2(i,j)
non_linear_constraint3(i,j);
non_linear_constraint1(i,j,k)$(not sameas(j,k))..
y(i,j) - y(i,k) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint2(i,j)..
x(i) - y(i,j) - M * (1 - z(i,j)) =L= 0;
non_linear_constraint3(i,j)..
x(i) - y(i,j) =G= 0;
Model reformulated_constraint /all/;
Code: Select all
Sets
i /i1*i43/
;
parameters
c(i)
ac(i)
uc(i)
k(i)
s(i)
d(i)
rdp(i)
rup(i)
ndf(i)
adf(i)
ca(i)
m(i)
de(i)
cp(i)
f(i);
$call gdxxrw tez.xlsx par=c rng=c!a1:b43 rdim=1 cdim=0
$gdxin tez.gdx
$load c
$gdxIn
$call gdxxrw tez.xlsx par=ac rng=ac! rdim=1 cdim=0
$gdxin tez.gdx
$load ac
$gdxIn
$call gdxxrw tez.xlsx par=uc rng=uc! rdim=1 cdim=0
$gdxin tez.gdx
$load uc
$gdxIn
$call gdxxrw tez.xlsx par=k rng=k! rdim=1 cdim=0
$gdxin tez.gdx
$load k
$gdxIn
$call gdxxrw tez.xlsx par=s rng=s! rdim=1 cdim=0
$gdxin tez.gdx
$load s
$gdxIn
$call gdxxrw tez.xlsx par=d rng=d! rdim=1 cdim=0
$gdxin tez.gdx
$load d
$gdxIn
$call gdxxrw tez.xlsx par=rdp rng=rdp! rdim=1 cdim=0
$gdxin tez.gdx
$load rdp
$gdxIn
$call gdxxrw tez.xlsx par=rup rng=rup! rdim=1 cdim=0
$gdxin tez.gdx
$load rup
$gdxIn
$call gdxxrw tez.xlsx par=ndf rng=ndf! rdim=1 cdim=0
$gdxin tez.gdx
$load ndf
$gdxIn
$call gdxxrw tez.xlsx par=adf rng=adf! rdim=1 cdim=0
$gdxin tez.gdx
$load adf
$gdxIn
$call gdxxrw tez.xlsx par=ca rng=ca! rdim=1 cdim=0
$gdxin tez.gdx
$load ca
$gdxIn
$call gdxxrw tez.xlsx par=m rng=m! rdim=1 cdim=0
$gdxin tez.gdx
$load m
$gdxIn
$call gdxxrw tez.xlsx par=de rng=de! rdim=1 cdim=0
$gdxin tez.gdx
$load de
$gdxIn
$call gdxxrw tez.xlsx par=cp rng=cp! rdim=1 cdim=0
$gdxin tez.gdx
$load cp
$gdxIn
$call gdxxrw tez.xlsx par=f rng=f! rdim=1 cdim=0
$gdxin tez.gdx
$load f
$gdxIn
display
c
ac
uc
k
s
d
rdp
rup
ndf
adf
ca
m
de
f
cp
;
Parameters
km /10600/
me /14.8/
hamprotein /888/
rd /1052/
ru /336/
ndff /3074/
adff /2014/
kalsiyum /22.4/
fosfor /16.6/
;
Scalar
BM/1000000000/
z1pis /8.316/
z1nis /6.585/
z2pis /131.704/
z2nis /166.325/
z3pis /6.585/
z3nis /8.316/
;
Variables
z,amac1, amac2, amac3;
Positive variable
z1,z2,z3,lamda;
Integer Variable
x(i) 'satın alımı yapılacak i. yem miktarı'
e(i) 'stoktan kullanılacak i. yem miktarı'
;
Binary Variable
xc(i)
;
Equations
amac
h11
h21
h31
h1
h2
h3
bulanik1
bulanik2
bulanik3
kisit1
kisit2
kisit3
kisit4
kisit5
kisit6
kisit7
kisit8
kisit9
kisit10
kisit11
kisit12(i)
kisit13
kisit14
kisit15
kisit16
kisit17
kisit18
kisit19
kisit20
kisit21
kisit22
kisit23
kisit24
kisit25
kisit26
kisit27
kisit28
kisit29
kisit30
kisit31
kisit32
kisit33
kisit34(i)
kisit35(i)
kisit36
kisit37
kisit38
;
amac.. z =e= lamda;
h11.. lamda =l= amac1;
h21.. lamda =l= amac2;
h31.. lamda =l= amac3;
h1.. amac1 =e= (z1-z1nis)/(z1pis-z1nis);
h2.. amac2 =e= (z2nis-z2)/(z2nis-z2pis);
h3.. amac3 =e= ((z3nis-z3) / (z3nis-z3pis));
bulanik1.. z1 =e= sum(i,(c(i)-ac(i))*x(i));
bulanik2.. z2 =e= sum(i, c(i)*x(i)) + sum(i, e(i)*s(i));
bulanik3.. z3 =e= sum(i, (uc(i)-c(i))*x(i));
kisit1.. sum(i, e(i)*k(i)) + sum(i, x(i)*k(i)) =e= km;
kisit2.. sum(i, e(i)*m(i)) + sum(i, x(i)*m(i)) =g= me;
kisit3.. sum(i, e(i)*cp(i)) + sum(i, x(i)*cp(i)) =g= hamprotein;
kisit4.. sum(i, e(i)*rdp(i)) + sum(i, x(i)*rdp(i)) =g= rd;
kisit5.. sum(i, e(i)*rup(i)) + sum(i, x(i)*rup(i)) =g= ru;
kisit6.. sum(i, e(i)*ca(i)) + sum(i, x(i)*ca(i)) =g= kalsiyum;
kisit7.. sum(i, e(i)*f(i)) + sum(i, x(i)*f(i)) =g= fosfor;
kisit8.. sum(i, e(i)*ndf(i)) + sum(i, x(i)*ndf(i)) =g= ndff;
kisit9.. sum(i, e(i)*adf(i)) + sum(i, x(i)*adf(i)) =g= adff;
kisit10.. (sum(i, ca(i)*x(i))+sum(i, ca(i)*e(i)))-(sum(i, f(i)*x(i))+sum(i, f(i)*e(i))) =g= 0;
kisit11.. (sum(i, ca(i)*x(i))+sum(i, ca(i)*e(i)))-2*(sum(i, f(i)*x(i))+sum(i, f(i)*e(i))) =l= 0;
kisit12(i).. e(i) =l= d(i);
kisit13.. sum(i, x('i1'))+sum(i, e('i1')) =l= 40;
kisit14.. sum(i,x('i2'))+sum(i, e('i2')) =l= 40;
kisit15.. sum(i,x('i3'))+sum(i, e('i3')) =l= 60;
kisit16.. sum(i,x('i4'))+sum(i, e('i4')) =l= 70;
kisit17.. sum(i,x('i5'))+sum(i, e('i5')) =l= 10;
kisit18.. sum(i,x('i6'))+sum(i, e('i6')) =l= 10;
kisit19.. sum(i,x('i7'))+sum(i, e('i7')) =l= 30;
kisit20.. sum(i,x('i8'))+sum(i, e('i8')) =l= 20;
kisit21.. sum(i,x('i9'))+sum(i, e('i9')) =l= 30;
kisit22.. sum(i,x('i10'))+sum(i, e('i10')) =l= 40;
kisit23.. sum(i,x('i11'))+sum(i, e('i11')) =l= 10;
kisit24.. sum(i,x('i13'))+sum(i, e('i13')) =l= 6;
kisit25.. sum(i,x('i17'))+sum(i, e('i17')) =l= 7;
kisit26.. sum(i,x('i18'))+sum(i, e('i18')) =l= 1;
kisit27.. sum(i,x('i19'))+sum(i, e('i19')) =l= 40;
kisit28.. sum(i,x('i20'))+sum(i, e('i20')) =l= 15;
kisit29.. sum(i,x('i21'))+sum(i, e('i21')) =l= 25;
kisit30.. sum(i,x('i25'))+sum(i, e('i25')) =l= 20;
kisit31.. sum(i,x('i27'))+sum(i, e('i27')) =l= 10;
kisit32.. sum(i,x('i28'))+sum(i, e('i28')) =l= 40;
kisit33.. sum(i,x('i29'))+sum(i, e('i29')) =l= 30;
kisit34(i).. d(i)-e(i) =l= (1-xc(i))*BM;
kisit35(i).. x(i) =l= xc(i)*BM;
kisit36.. (x('i16')+e('i16')+x('i17')+e('i17')+x('i19')+e('i19')+x('i27')+e('i27')+x('i29')+e('i29')+x('i31')+e('i31')) =g= 0.4*(sum(i, x(i))+sum(i,e(i)));
kisit37.. (x('i16')+e('i16')+x('i17')+e('i17')+x('i19')+e('i19')+x('i27')+e('i27')+x('i29')+e('i29')+x('i31')+e('i31')) =l= 0.6*(sum(i, x(i))+sum(i,e(i)));
kisit38.. sum(i,x('i16'))+sum(i, e('i16')) =l= 0;
Model deneme / all /;
option LP= Cplex;
solve deneme using rmip maximizing z;
Code: Select all
Sets
i /i1*i43/
;
parameters
c(i)
ac(i)
uc(i)
k(i)
s(i)
d(i)
rdp(i)
rup(i)
ndf(i)
adf(i)
ca(i)
m(i)
de(i)
cp(i)
f(i);
$call gdxxrw tez.xlsx par=c rng=c!a1:b43 rdim=1 cdim=0
$gdxin tez.gdx
$load c
$gdxIn
$call gdxxrw tez.xlsx par=ac rng=ac! rdim=1 cdim=0
$gdxin tez.gdx
$load ac
$gdxIn
$call gdxxrw tez.xlsx par=uc rng=uc! rdim=1 cdim=0
$gdxin tez.gdx
$load uc
$gdxIn
$call gdxxrw tez.xlsx par=k rng=k! rdim=1 cdim=0
$gdxin tez.gdx
$load k
$gdxIn
$call gdxxrw tez.xlsx par=s rng=s! rdim=1 cdim=0
$gdxin tez.gdx
$load s
$gdxIn
$call gdxxrw tez.xlsx par=d rng=d! rdim=1 cdim=0
$gdxin tez.gdx
$load d
$gdxIn
$call gdxxrw tez.xlsx par=rdp rng=rdp! rdim=1 cdim=0
$gdxin tez.gdx
$load rdp
$gdxIn
$call gdxxrw tez.xlsx par=rup rng=rup! rdim=1 cdim=0
$gdxin tez.gdx
$load rup
$gdxIn
$call gdxxrw tez.xlsx par=ndf rng=ndf! rdim=1 cdim=0
$gdxin tez.gdx
$load ndf
$gdxIn
$call gdxxrw tez.xlsx par=adf rng=adf! rdim=1 cdim=0
$gdxin tez.gdx
$load adf
$gdxIn
$call gdxxrw tez.xlsx par=ca rng=ca! rdim=1 cdim=0
$gdxin tez.gdx
$load ca
$gdxIn
$call gdxxrw tez.xlsx par=m rng=m! rdim=1 cdim=0
$gdxin tez.gdx
$load m
$gdxIn
$call gdxxrw tez.xlsx par=de rng=de! rdim=1 cdim=0
$gdxin tez.gdx
$load de
$gdxIn
$call gdxxrw tez.xlsx par=cp rng=cp! rdim=1 cdim=0
$gdxin tez.gdx
$load cp
$gdxIn
$call gdxxrw tez.xlsx par=f rng=f! rdim=1 cdim=0
$gdxin tez.gdx
$load f
$gdxIn
display
c
ac
uc
k
s
d
rdp
rup
ndf
adf
ca
m
de
f
cp
;
Parameters
km /10600/
me /14.8/
hamprotein /888/
rd /1052/
ru /336/
ndff /3074/
adff /2014/
kalsiyum /22.4/
fosfor /16.6/
;
Scalar
BM/1000000000/
z1pis /8.316/
z1nis /6.585/
z2pis /131.704/
z2nis /166.325/
z3pis /6.585/
z3nis /8.316/
;
Variables
z,amac1, amac2, amac3;
Positive variable
z1,z2,z3,lamda;
Integer Variable
x(i) 'satın alımı yapılacak i. yem miktarı'
e(i) 'stoktan kullanılacak i. yem miktarı'
;
Binary Variable
xc(i)
;
Equations
amac
h11
h21
h31
h1
h2
h3
bulanik1
bulanik2
bulanik3
kisit1
kisit2
kisit3
kisit4
kisit5
kisit6
kisit7
kisit8
kisit9
kisit10
kisit11
kisit12(i)
kisit13
kisit14
kisit15
kisit16
kisit17
kisit18
kisit19
kisit20
kisit21
kisit22
kisit23
kisit24
kisit25
kisit26
kisit27
kisit28
kisit29
kisit30
kisit31
kisit32
kisit33
kisit34(i)
kisit35(i)
kisit36
kisit37
kisit38
;
amac.. z =e= lamda;
h11.. lamda =l= amac1;
h21.. lamda =l= amac2;
h31.. lamda =l= amac3;
h1.. amac1 =e= (z1-z1nis)/(z1pis-z1nis);
h2.. amac2 =e= (z2nis-z2)/(z2nis-z2pis);
h3.. amac3 =e= ((z3nis-z3) / (z3nis-z3pis));
bulanik1.. z1 =e= sum(i,(c(i)-ac(i))*x(i));
bulanik2.. z2 =e= sum(i, c(i)*x(i)) + sum(i, e(i)*s(i));
bulanik3.. z3 =e= sum(i, (uc(i)-c(i))*x(i));
kisit1.. sum(i, e(i)*k(i)) + sum(i, x(i)*k(i)) =e= km;
kisit2.. sum(i, e(i)*m(i)) + sum(i, x(i)*m(i)) =g= me;
kisit3.. sum(i, e(i)*cp(i)) + sum(i, x(i)*cp(i)) =g= hamprotein;
kisit4.. sum(i, e(i)*rdp(i)) + sum(i, x(i)*rdp(i)) =g= rd;
kisit5.. sum(i, e(i)*rup(i)) + sum(i, x(i)*rup(i)) =g= ru;
kisit6.. sum(i, e(i)*ca(i)) + sum(i, x(i)*ca(i)) =g= kalsiyum;
kisit7.. sum(i, e(i)*f(i)) + sum(i, x(i)*f(i)) =g= fosfor;
kisit8.. sum(i, e(i)*ndf(i)) + sum(i, x(i)*ndf(i)) =g= ndff;
kisit9.. sum(i, e(i)*adf(i)) + sum(i, x(i)*adf(i)) =g= adff;
kisit10.. (sum(i, ca(i)*x(i))+sum(i, ca(i)*e(i)))-(sum(i, f(i)*x(i))+sum(i, f(i)*e(i))) =g= 0;
kisit11.. (sum(i, ca(i)*x(i))+sum(i, ca(i)*e(i)))-2*(sum(i, f(i)*x(i))+sum(i, f(i)*e(i))) =l= 0;
kisit12(i).. e(i) =l= d(i);
kisit13.. sum(i, x('i1'))+sum(i, e('i1')) =l= 40;
kisit14.. sum(i,x('i2'))+sum(i, e('i2')) =l= 40;
kisit15.. sum(i,x('i3'))+sum(i, e('i3')) =l= 60;
kisit16.. sum(i,x('i4'))+sum(i, e('i4')) =l= 70;
kisit17.. sum(i,x('i5'))+sum(i, e('i5')) =l= 10;
kisit18.. sum(i,x('i6'))+sum(i, e('i6')) =l= 10;
kisit19.. sum(i,x('i7'))+sum(i, e('i7')) =l= 30;
kisit20.. sum(i,x('i8'))+sum(i, e('i8')) =l= 20;
kisit21.. sum(i,x('i9'))+sum(i, e('i9')) =l= 30;
kisit22.. sum(i,x('i10'))+sum(i, e('i10')) =l= 40;
kisit23.. sum(i,x('i11'))+sum(i, e('i11')) =l= 10;
kisit24.. sum(i,x('i13'))+sum(i, e('i13')) =l= 6;
kisit25.. sum(i,x('i17'))+sum(i, e('i17')) =l= 7;
kisit26.. sum(i,x('i18'))+sum(i, e('i18')) =l= 1;
kisit27.. sum(i,x('i19'))+sum(i, e('i19')) =l= 40;
kisit28.. sum(i,x('i20'))+sum(i, e('i20')) =l= 15;
kisit29.. sum(i,x('i21'))+sum(i, e('i21')) =l= 25;
kisit30.. sum(i,x('i25'))+sum(i, e('i25')) =l= 20;
kisit31.. sum(i,x('i27'))+sum(i, e('i27')) =l= 10;
kisit32.. sum(i,x('i28'))+sum(i, e('i28')) =l= 40;
kisit33.. sum(i,x('i29'))+sum(i, e('i29')) =l= 30;
kisit34(i).. d(i)-e(i) =l= (1-xc(i))*BM;
kisit35(i).. x(i) =l= xc(i)*BM;
kisit36.. (x('i16')+e('i16')+x('i17')+e('i17')+x('i19')+e('i19')+x('i27')+e('i27')+x('i29')+e('i29')+x('i31')+e('i31')) =g= 0.4*(sum(i, x(i))+sum(i,e(i)));
kisit37.. (x('i16')+e('i16')+x('i17')+e('i17')+x('i19')+e('i19')+x('i27')+e('i27')+x('i29')+e('i29')+x('i31')+e('i31')) =l= 0.6*(sum(i, x(i))+sum(i,e(i)));
kisit38.. sum(i,x('i16'))+sum(i, e('i16')) =l= 0;
Model deneme / all /;
option LP= Cplex;
solve deneme using rmip maximizing z;
Code: Select all
$GDXIN inputs.gdx
$LOAD g=Data1
$gdxIn
Code: Select all
$GDXIN inputs.gdx
$LOAD g=Data1
$gdxIn
Attachments
Sample data.xlsx (35.6 KiB)Attachments
Sample data.xlsx (35.6 KiB)Code: Select all
Parameter p(*,*);
$onEmbeddedCode Connect:
- ExcelReader:
file: Sample data.xlsx
symbols:
- name: p
range: Sheet1!A1
#rowDimension: 1 # default: 1
#columnDimension: 1 # default: 1
#valueSubstitutions: {0: .nan} # drop 0 values (e.g. for writing a GDX file without zeroes)
#- GDXWriter: # write to GDX
# file: data.gdx
- GAMSWriter: # write to GAMS
writeAll: True
$offEmbeddedCode
display p;
Code: Select all
Parameter p(*,*);
$onEmbeddedCode Connect:
- ExcelReader:
file: Sample data.xlsx
symbols:
- name: p
range: Sheet1!A1
#rowDimension: 1 # default: 1
#columnDimension: 1 # default: 1
#valueSubstitutions: {0: .nan} # drop 0 values (e.g. for writing a GDX file without zeroes)
#- GDXWriter: # write to GDX
# file: data.gdx
- GAMSWriter: # write to GAMS
writeAll: True
$offEmbeddedCode
display p;
Code: Select all
parameter elastc(*,*);
parameter ELASTY(*,*);
parameter Elastnw(*,*);
parameter agg(*,*);
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: elastc
range: sheet5!A1
rowDimension: 1
columnDimension: 1
- name: ELASTY
range: sheet1!A1
rowDimension: 1
columnDimension: 1
- GAMSWriter:
writeAll: True
$offEmbeddedCode
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: Elastnw
range: sheet2!A1
rowDimension: 2
columnDimension: 0
- GAMSWriter:
writeAll: True
$offEmbeddedCode
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: agg
range: sheet4!A1
rowDimension: 1
columnDimension: 1
- GAMSWriter:
writeAll: True
$offEmbeddedCode
display elastc, ELASTY, Elastnw, agg
Code: Select all
parameter elastc(*,*);
parameter ELASTY(*,*);
parameter Elastnw(*,*);
parameter agg(*,*);
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: elastc
range: sheet5!A1
rowDimension: 1
columnDimension: 1
- name: ELASTY
range: sheet1!A1
rowDimension: 1
columnDimension: 1
- GAMSWriter:
writeAll: True
$offEmbeddedCode
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: Elastnw
range: sheet2!A1
rowDimension: 2
columnDimension: 0
- GAMSWriter:
writeAll: True
$offEmbeddedCode
$onEmbeddedCode Connect:
- ExcelReader:
file: test.xlsx
symbols:
- name: agg
range: sheet4!A1
rowDimension: 1
columnDimension: 1
- GAMSWriter:
writeAll: True
$offEmbeddedCode
display elastc, ELASTY, Elastnw, agg