how to code if-else with variable in loop?

Problems with modeling
Post Reply
dasa
User
User
Posts: 13
Joined: 4 years ago

how to code if-else with variable in loop?

Post by dasa »

Dear All,

I hope you are doing well. I have some problem related to the model of 2 scheme electricity prices. to simplify, consider the system consists of load, PV, generator and connect to utility grid. The price of electricity is cheap if consume less than 4 and more expensive, otherwise.

Here is my code. After I run, it say a suffix is missing.
Your help would be greatly appreciated. I am looking forward to hearing from you. :)

set t /1*3/
parameters P_pv(t)/
1 3
2 4
3 2/;
parameters P_gen(t)/
1 3
2 3
3 3/;
parameters P_load(t)/
1 10
2 10
3 10/;
* electricity price
parameters C_grid1(t)/
1 1
2 1.2
3 1.1/;

parameters C_grid2(t)/
1 2.3
2 2.5
3 2.8/;
positive variable P_grid, C_grid
equation obj, c1;

obj(t).. z =e= C_grid(t)*P_grid(t);
c1(t).. P_grid(t) + P_gen(t) + P_pv(t) =e= P_load(t);
model x /all/;

loop (t,
if ( Pmg_in(t)<4,
c_grid(t)= c_grid1(t);
solve x minimizie z using lp;
else
c_grid(t)=c_grid2(t);
solve x minimize z using lp;
);
);
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: how to code if-else with variable in loop?

Post by abhosekar »

Couple of issues: The variable Pmg_in(t) is not defined.

Regarding the suffix error,
when you write c_grid(t) =... it depends on whether you want to fix the value of c_grid(t) (in which case you will write c.fx(t)) or you want to provide an initial value (in which case you will write c.l(t)). Similarly, .lo for lower bound and .up for upper bound.

short answer: as the error suggests, you need to add a suffix with something like following:

Code: Select all

c_grid.l(t)= c_grid1(t);
c_grid.l(t)=c_grid2(t);
Hope this helps.

- Atharv
Post Reply