Hey,
the following model works already, but now I would like to add a restriciton to a variable, and I don't know how/where to add that restriction.
x_pump(t) should only work in between the range of 0.085 and 0.2. Meaning the pump only runs when x_solar is higher than 0.085 and lower than 0.2.
Can anyone help me to solve this problem? Thanks in advance!
Regards, Martin
Set
t time in hours /t0*t5/;
Parameter solar(t) Solarenergy per hour in kW /
t0 0.05
t1 0.085
t2 0.1
t3 0.3
t4 0.2
t5 0.07/
;
Scalar ddaily Waterdemand per day in cubic meters /3.5/;
Scalar eta efficiency from solarenergy to pump /0.5/;
Scalar costPV Cost per kW installed capacity in Euro /100/;
Variables x_solar(t);
Variables x_pump(t);
positive variable pumpflow(t)
Free Variable TotalCost,PV_Capacity;
Equations Obj,DailyWaterdemand,WaterFlow,limitSolar,solarsize;
Obj.. TotalCost =E= PV_Capacity*costPV;
DailyWaterdemand.. ddaily =E= sum(t,pumpFlow(t));
WaterFlow(t).. pumpFlow(t) =E= 5.5586*x_pump(t) + 0.2278;
limitSolar(t).. x_pump(t) =L= x_solar(t);
solarsize(t).. x_solar(t) =E= solar(t)*eta*PV_Capacity;
Model WaterPump /all/;
Solve WaterPump using lp minimizing TotalCost
Restriction of a Variable

 User
 Posts: 4
 Joined: 4 months ago
Re: Restriction of a Variable
Hi,
You mean it should work for t1 till t4 ? In this case you can use ord to restrict it.
anywhere in the formula that you wanna use that variable you will use ord to eliminate it.
WaterFlow(t)$(ord(t)>=2 and ord(t)<=5).. pumpFlow(t) =E= 5.5586*x_pump(t) + 0.2278;
limitSolar(t)$(ord(t)>=2 and ord(t)<=5).. x_pump(t) =L= x_solar(t);
I hope it helps.
You mean it should work for t1 till t4 ? In this case you can use ord to restrict it.
anywhere in the formula that you wanna use that variable you will use ord to eliminate it.
WaterFlow(t)$(ord(t)>=2 and ord(t)<=5).. pumpFlow(t) =E= 5.5586*x_pump(t) + 0.2278;
limitSolar(t)$(ord(t)>=2 and ord(t)<=5).. x_pump(t) =L= x_solar(t);
I hope it helps.
Re: Restriction of a Variable
Do you want to implement a logical constraint?
if x_solar(t)<0.085 or x_solar(t)>0.2 => x_pump(t)=0
You can easily implement this with a binary variable b(t) in a linear fashion:
x_solar(t) >= 0.085  0.085*b(t)
x_solar(t) <= 0.2 + MAXSOLAR*b(t)
So if b(t)=0 then 0.085<=x_solar(t)<=0.2 so we need to implement b(t)=1 => x_pump(t)=0
x_pump(t) <= MAXPUMP*(1b(t))
From the coefficients and ddaily you can calculate the small numbers MAXSOLAR and MAXPUMP. Because of the binary variable the model type changes from LP to MIP. Don't forget to set "option optcr=0;" to get the global optimal solution.
Michael
if x_solar(t)<0.085 or x_solar(t)>0.2 => x_pump(t)=0
You can easily implement this with a binary variable b(t) in a linear fashion:
x_solar(t) >= 0.085  0.085*b(t)
x_solar(t) <= 0.2 + MAXSOLAR*b(t)
So if b(t)=0 then 0.085<=x_solar(t)<=0.2 so we need to implement b(t)=1 => x_pump(t)=0
x_pump(t) <= MAXPUMP*(1b(t))
From the coefficients and ddaily you can calculate the small numbers MAXSOLAR and MAXPUMP. Because of the binary variable the model type changes from LP to MIP. Don't forget to set "option optcr=0;" to get the global optimal solution.
Michael
Re: Restriction of a Variable
Thanks for the helpful responses!!
Michael, your explanation was exactly what I was looking for. The linear equations seem clear to me. However I'm not sure about how to calculate MAXSOLAR and MAXPUMP. Could you explain that to me?
Thanks a lot for your support!
Martin
Michael, your explanation was exactly what I was looking for. The linear equations seem clear to me. However I'm not sure about how to calculate MAXSOLAR and MAXPUMP. Could you explain that to me?
Thanks a lot for your support!
Martin
Re: Restriction of a Variable
The scalar ddaily drives the model. Since, sum(t,pumpFlow(t)) =e= ddaily, we can derive pumpflow(t)<= ddaily. Since pumpFlow(t) =E= 5.5586*x_pump(t) + 0.2278 we can derive x_pump(t) <= (ddaily0.2278)/5.5586. There is your upper bound for MAXPUMP. I am sure you can do similar calculation for MAXSOLAR.
Michael
Michael
Re: Restriction of a Variable
Thank you Michael for your help!
I've tried to add those restrictions to my model, but I think it doesn't work right. I thought again about the binary equations and I was wondering if the value for MAXSOLAR really matters? Because to guarantee that the pump runs (x_pump(t) >= 0) the binary variable b(t) has to be 0. If b(t)=0 then the second equation (below) eliminates MAXSOLAR and x_solar(t) <= 0.2 remains. For the purpose of running the model and see some results I set MAXSOLAR=1, however the results can't be correct.
x_solar(t) >= 0.085  0.085*b(t)
x_solar(t) <= 0.2 + MAXSOLAR*b(t)
x_pump(t) <= MAXPUMP*(1b(t))
This is how my model looks like (Comment: I have changed the minimum and maximum values of the pump and the pumpflow equation):
Set t time in hours /t1*t8/
Parameters solar(t) solarenergy per hour in kW per kW installed capacity /
t1 0.356
t2 0.515
t3 0.624
t4 0.680
t5 0.702
t6 0.653
t7 0.563
t8 0.436/;
Scalars costPerkW cost per kW installed capacity /1000/
DailyWater daily water demand in village /10/
Scalars pump_min /0.2/
pump_max /0.7/;
Scalar k_pump /5/;
Variables x_pumpflow(t),x_pump(t),x_pv(t),MAXSOL,MAXPUMP;
Positive Variable x_PVcapacity;
Free Variables TotCost;
Binary Variable x_b_pump(t);
Equations TotalCost,waterdemand,waterflow,solarproduction,relPVtoPump,bin1,bin2,bin3,MaximumSolar,MaximumPump;
TotalCost.. TotCost =E= x_PVCapacity*costPerKW;
waterdemand.. DailyWater =E= sum(t,x_pumpflow(t));
waterflow(t).. x_pumpflow(t) =E= k_pump*x_pump(t);
solarproduction(t).. x_pv(t) =E= solar(t)*x_PVcapacity;
relPVtoPump(t).. x_pump(t) =L= x_pv(t);
bin1(t).. x_pv(t) =G= pump_min  pump_min * x_b_pump(t);
bin2(t).. x_pv(t) =L= pump_max + MAXSOL * x_b_pump(t);
bin3(t).. x_pump(t) =L= MAXPUMP*(1  x_b_pump(t));
MaximumPump.. MAXPUMP =L= dailywater/k_pump;
MaximumSolar.. MAXSOL =L= 1;
Model waterpump /all/;
option optcr=0;
Solve waterpump using MINLP minimizing TotCost
I'd appreciate it a lot, if you could have another look on my model! I've just started recently to work with GAMS, so it's still challenging for me to unterstand its logic and syntax.
Many thanks!
Martin
I've tried to add those restrictions to my model, but I think it doesn't work right. I thought again about the binary equations and I was wondering if the value for MAXSOLAR really matters? Because to guarantee that the pump runs (x_pump(t) >= 0) the binary variable b(t) has to be 0. If b(t)=0 then the second equation (below) eliminates MAXSOLAR and x_solar(t) <= 0.2 remains. For the purpose of running the model and see some results I set MAXSOLAR=1, however the results can't be correct.
x_solar(t) >= 0.085  0.085*b(t)
x_solar(t) <= 0.2 + MAXSOLAR*b(t)
x_pump(t) <= MAXPUMP*(1b(t))
This is how my model looks like (Comment: I have changed the minimum and maximum values of the pump and the pumpflow equation):
Set t time in hours /t1*t8/
Parameters solar(t) solarenergy per hour in kW per kW installed capacity /
t1 0.356
t2 0.515
t3 0.624
t4 0.680
t5 0.702
t6 0.653
t7 0.563
t8 0.436/;
Scalars costPerkW cost per kW installed capacity /1000/
DailyWater daily water demand in village /10/
Scalars pump_min /0.2/
pump_max /0.7/;
Scalar k_pump /5/;
Variables x_pumpflow(t),x_pump(t),x_pv(t),MAXSOL,MAXPUMP;
Positive Variable x_PVcapacity;
Free Variables TotCost;
Binary Variable x_b_pump(t);
Equations TotalCost,waterdemand,waterflow,solarproduction,relPVtoPump,bin1,bin2,bin3,MaximumSolar,MaximumPump;
TotalCost.. TotCost =E= x_PVCapacity*costPerKW;
waterdemand.. DailyWater =E= sum(t,x_pumpflow(t));
waterflow(t).. x_pumpflow(t) =E= k_pump*x_pump(t);
solarproduction(t).. x_pv(t) =E= solar(t)*x_PVcapacity;
relPVtoPump(t).. x_pump(t) =L= x_pv(t);
bin1(t).. x_pv(t) =G= pump_min  pump_min * x_b_pump(t);
bin2(t).. x_pv(t) =L= pump_max + MAXSOL * x_b_pump(t);
bin3(t).. x_pump(t) =L= MAXPUMP*(1  x_b_pump(t));
MaximumPump.. MAXPUMP =L= dailywater/k_pump;
MaximumSolar.. MAXSOL =L= 1;
Model waterpump /all/;
option optcr=0;
Solve waterpump using MINLP minimizing TotCost
I'd appreciate it a lot, if you could have another look on my model! I've just started recently to work with GAMS, so it's still challenging for me to unterstand its logic and syntax.
Many thanks!
Martin