Code: Select all
$ifThen set X or Y
Solve MODEL using nlp minimizing OBJ;
$else
Solve MODEL using lp minimizing OBJ;
$endIf
Code: Select all
$ifThen set X or Y
Solve MODEL using nlp minimizing OBJ;
$else
Solve MODEL using lp minimizing OBJ;
$endIf
Code: Select all
$set xset 0
$if set x $set xset 1
$set yset 0
$if set y $set yset 1
$log %xset%%yset%
$ifThenE (%xset%)or(%yset%)
Solve MODEL using nlp minimizing OBJ;
$else
Solve MODEL using lp minimizing OBJ;
$endIf
Code: Select all
$set xset 0
$if set x $set xset 1
$set yset 0
$if set y $set yset 1
$log %xset%%yset%
$ifThenE (%xset%)or(%yset%)
Solve MODEL using nlp minimizing OBJ;
$else
Solve MODEL using lp minimizing OBJ;
$endIf
Code: Select all
Sets
y "Set of years in the planning horizon" /y2023*y2045/
d "Set of all days in a year" /d1*d366/
t "Set of all hours in a day" /h1*h24/;
$eval P card(y)*card(d)*card(t)
Set p period / p1*p%P% /;
Parameter E(y,d,t), cnt /0/;
loop((y,d,t), cnt=cnt+1; E(y,d,t) = cnt);
* Remove data for non leap years
set leap(y) / y2024, y2028, y2032, y2036, y2040, y2044 /;
E(y,'d366',t)$(not leap(y)) = 0;
* Now match non-zero E to p:
set ydt(y,d,t), pydtmap(p,y,d,t);
option ydt<E, pydtmap(p:ydt);
* Have parameters in p space
alias (y,yy), (d,dd), (t,tt);
Parameter Ep(p); Ep(p) = sum(pydtmap(p,y,d,t), E(y,d,t));
Variable Ep_b_res(p), oo(d);
Equation balance(p), daystart(p);
balance(p).. Ep_b_res(p) =e= sum(pydtmap(p,y,d,t), oo(d))*(Ep_b_res(p-1) + Ep(p));
daystart(p)$sum(pydtmap(p,y,d,'h1'),1).. Ep_b_res(p) =l= 2*Ep_b_res(p-1);
Code: Select all
Sets
y "Set of years in the planning horizon" /y2023*y2045/
d "Set of all days in a year" /d1*d366/
t "Set of all hours in a day" /h1*h24/;
$eval P card(y)*card(d)*card(t)
Set p period / p1*p%P% /;
Parameter E(y,d,t), cnt /0/;
loop((y,d,t), cnt=cnt+1; E(y,d,t) = cnt);
* Remove data for non leap years
set leap(y) / y2024, y2028, y2032, y2036, y2040, y2044 /;
E(y,'d366',t)$(not leap(y)) = 0;
* Now match non-zero E to p:
set ydt(y,d,t), pydtmap(p,y,d,t);
option ydt<E, pydtmap(p:ydt);
* Have parameters in p space
alias (y,yy), (d,dd), (t,tt);
Parameter Ep(p); Ep(p) = sum(pydtmap(p,y,d,t), E(y,d,t));
Variable Ep_b_res(p), oo(d);
Equation balance(p), daystart(p);
balance(p).. Ep_b_res(p) =e= sum(pydtmap(p,y,d,t), oo(d))*(Ep_b_res(p-1) + Ep(p));
daystart(p)$sum(pydtmap(p,y,d,'h1'),1).. Ep_b_res(p) =l= 2*Ep_b_res(p-1);
Code: Select all
Variables
obj objective function
Income .. obj =e= sum((h,z),sum(t,(P_ppa(t,z)/1000)*SP_ppa) + sum(t,(mp(t)*(1-pvf(t))*(P_b2g(t,z)))));
Code: Select all
Variables
obj objective function
Income .. obj =e= sum((h,z),sum(t,(P_ppa(t,z)/1000)*SP_ppa) + sum(t,(mp(t)*(1-pvf(t))*(P_b2g(t,z)))));
Code: Select all
--- MOZSAM_test.gms(165) 4 Mb[FIL:"C:\Users\ffian\Documents\support\MOZSAM_test.gms",165,0]
*** Error at line 165: division by zero (0)[LST:522]
--- MOZSAM_test.gms(171) 4 Mb 1 Error[FIL:"C:\Users\ffian\Documents\support\MOZSAM_test.gms",171,0]
*** Error at line 171: division by zero (0)[LST:523]
--- MOZSAM_test.gms(899) 6 Mb 2 Errors
Code: Select all
165 tq(c)=SAM("stax",c)/QQ0(c);
[...]
171 tim(c) = SAM("mtax",c)/SAM("ROW",c);
Code: Select all
tq(c)$QQ0(c)=SAM("stax",c)/QQ0(c);
Code: Select all
--- MOZSAM_test.gms(165) 4 Mb[FIL:"C:\Users\ffian\Documents\support\MOZSAM_test.gms",165,0]
*** Error at line 165: division by zero (0)[LST:522]
--- MOZSAM_test.gms(171) 4 Mb 1 Error[FIL:"C:\Users\ffian\Documents\support\MOZSAM_test.gms",171,0]
*** Error at line 171: division by zero (0)[LST:523]
--- MOZSAM_test.gms(899) 6 Mb 2 Errors
Code: Select all
165 tq(c)=SAM("stax",c)/QQ0(c);
[...]
171 tim(c) = SAM("mtax",c)/SAM("ROW",c);
Code: Select all
tq(c)$QQ0(c)=SAM("stax",c)/QQ0(c);
Code: Select all
i/j 1 2 3 4 5 6
1 0 1 0 0 0 1
2 0 1 0 0 0 1
3 0 1 1 0 0 1
4 1 1 1 0 0 1
5 1 0 1 0 1 0
6 1 0 1 0 1 0
7 1 0 0 0 1 0
8 0 0 0 0 1 0
9 0 0 0 0 0 0
10 0 0 0 0 0 0
Code: Select all
i/j 1 2 3 4 5 6
1 0 1 0 0 0 1
2 0 1 0 0 0 1
3 0 1 1 0 0 1
4 1 1 1 0 0 1
5 1 0 1 0 1 0
6 1 0 1 0 1 0
7 1 0 0 0 1 0
8 0 0 0 0 1 0
9 0 0 0 0 0 0
10 0 0 0 0 0 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;
)
)
)
);
* 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;
)
)
)
);
* Check the results
option randvalloop:0:1:1;
display randvalloop;
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
(gams_py46) C:\Users\myusername>pip install gamsapi[all]==46.4.0
Code: Select all
python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
Code: Select all
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\GAMS\38\apifiles\Python\gams\gams\__init__.py", line 32, in <module>
from .workspace import *
File "C:\GAMS\38\apifiles\Python\gams\gams\workspace.py", line 32, in <module>
from .database import *
File "C:\GAMS\38\apifiles\Python\gams\gams\database.py", line 26, in <module>
from gdxcc import *
ModuleNotFoundError: No module named 'gdxcc'
Code: Select all
(gams_py46) C:\Users\myusername>python -m site
Code: Select all
sys.path = [
'C:\\Users\\myusername',
'C:\\GAMS\\38\\apifiles\\Python\\gams',
'C:\\Users\\myusername\\%PYTHONPATH%',
'C:\\Anaconda3\\envs\\gams_py46\\python311.zip',
'C:\\Anaconda3\\envs\\gams_py46\\DLLs',
'C:\\Anaconda3\\envs\\gams_py46\\Lib',
'C:\\Anaconda3\\envs\\gams_py46',
'C:\\Anaconda3\\envs\\gams_py46\\Lib\\site-packages',
]
USER_BASE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python\\Python311\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True
Code: Select all
(gams_py46) C:\Users\myusername>pip install gamsapi[all]==46.4.0
Code: Select all
python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
Code: Select all
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\GAMS\38\apifiles\Python\gams\gams\__init__.py", line 32, in <module>
from .workspace import *
File "C:\GAMS\38\apifiles\Python\gams\gams\workspace.py", line 32, in <module>
from .database import *
File "C:\GAMS\38\apifiles\Python\gams\gams\database.py", line 26, in <module>
from gdxcc import *
ModuleNotFoundError: No module named 'gdxcc'
Code: Select all
(gams_py46) C:\Users\myusername>python -m site
Code: Select all
sys.path = [
'C:\\Users\\myusername',
'C:\\GAMS\\38\\apifiles\\Python\\gams',
'C:\\Users\\myusername\\%PYTHONPATH%',
'C:\\Anaconda3\\envs\\gams_py46\\python311.zip',
'C:\\Anaconda3\\envs\\gams_py46\\DLLs',
'C:\\Anaconda3\\envs\\gams_py46\\Lib',
'C:\\Anaconda3\\envs\\gams_py46',
'C:\\Anaconda3\\envs\\gams_py46\\Lib\\site-packages',
]
USER_BASE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\myusername\\AppData\\Roaming\\Python\\Python311\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True
Code: Select all
echo %PYTHONPATH%
Code: Select all
echo %PYTHONPATH%
Code: Select all
echo %PYTHONPATH%
Code: Select all
C:\GAMS\38\apifiles\Python\gams;%PYTHONPATH%
Code: Select all
C:\Users\myusername>python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
API OK -- Version 46.4.0
Code: Select all
echo %PYTHONPATH%
Code: Select all
C:\GAMS\38\apifiles\Python\gams;%PYTHONPATH%
Code: Select all
C:\Users\myusername>python -c "import gams; print(f'API OK -- Version {gams.__version__}')"
API OK -- Version 46.4.0