Restriction of a Variable

Problems with modeling
Post Reply
Posts: 1
Joined: 1 week ago

Restriction of a Variable

Post by martinka » 1 week ago


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

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

Posts: 4
Joined: 1 week ago

Re: Restriction of a Variable

Post by arman.nedjati » 1 week ago


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.

User avatar
Posts: 198
Joined: 2 years ago

Re: Restriction of a Variable

Post by bussieck » 1 week ago

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*(1-b(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.


Post Reply