Please correct my code Topic is solved

Problems with modeling
Post Reply
Parisa
User
User
Posts: 14
Joined: 5 years ago

Please correct my code

Post by Parisa »

Hello,
I have written the following model, but I have some problem. My problem related to defining sets K_q,a and R_q,a. When I define these sets I get some errors concerning the variable x.
I really need help.
Image

This is my code.

Code: Select all

Sets
         k       /1*25/

         q       /1*86/

         a       /1*86/
;

alias(i,k);


Variable
         o;

Binary Variables
         x(k)
         y(a,q,i)
         z(q);

Parameters
         p
         f_ev(q)
         d(q)
         f_phev(q)
         d(q);

p = 3;

$ call gdxxrw Data.xlsx par d rng=edges!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load d
$ gdxin

$ call gdxxrw Data.xlsx par f_ev rng=flows!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load f_ev
$ gdxin

$ call gdxxrw Data.xlsx par f_phev rng=flows!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load f_phev
$ gdxin


Equations
         Obj
         Const1(a,q,i)
         Const2(a,q)
         Const3(q)
         Const4;

Obj..
          o =e= sum(q,f_ev(q)*d(q)*z(q)) + sum((a,q,i),f_phev(q)*d(q)*y(a,q,i));

Const1(a,q,i)..
         y(a,q,i) =l= x(i);

Const2(a,q)..
         sum(i,y(a,q,i)) =l= 1;

Const3(q)..
         z(q) =l= sum(k,x(k));

Const4..
         sum(k,x(k)) =e= p;

model CSLM_PHEV /all/;
solve CSLM_PHEV maximizing o using MIP;
display x.l,y.l,z.l,o.l,f_ev,f_phev,d;
Attachments
var.JPG
model.JPG
model.JPG (27.47 KiB) Viewed 4145 times
Last edited by Parisa 4 years ago, edited 1 time in total.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Please correct my code

Post by Renger »

Hi
You write: "Please correct my code", but you give no indication of where there is a problem (the code is correct). You should point to where you have a problem. Don't expect people to guess your problem or compare your complete model to a picture with the equations.
Cheers
Renger
PS. Code should be inserted using the button </> not the icon with the inverted commas.
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Parisa
User
User
Posts: 14
Joined: 5 years ago

Re: Please correct my code

Post by Parisa »

This is the code by declaring set R_q, a as "i".

Code: Select all

Sets
         k       /1*25/

         q       /1*86/

         a       /1*86/

         i       /1*10/
;


Variable
         o;

Binary Variables
         x(k)
         y(a,q,i)
         z(q);

Parameters
         p
         f_ev(q)
         d(q)
         f_phev(q)
         d(q);

p = 3;

$ call gdxxrw Data.xlsx par d rng=edges!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load d
$ gdxin

$ call gdxxrw Data.xlsx par f_ev rng=flows!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load f_ev
$ gdxin

$ call gdxxrw Data.xlsx par f_phev rng=flows!A1:B86 rdim=1  cdim=0
$ gdxin Data.gdx
$ load f_phev
$ gdxin


Equations
         Obj
         Const1(a,q,i)
         Const2(a,q)
         Const3(q)
         Const4;

Obj..
          o =e= sum(q,f_ev(q)*d(q)*z(q)) + sum((a,q,i),f_phev(q)*d(q)*y(a,q,i));

Const1(a,q,i)..
         y(a,q,i) =l= x(i);

Const2(a,q)..
         sum(i,y(a,q,i)) =l= 1;

Const3(q)..
         z(q) =l= sum(i,x(i));

Const4..
         sum(k,x(k)) =e= p;

model CSLM_PHEV /all/;
solve CSLM_PHEV maximizing o using MIP;
display x.l,y.l,z.l,o.l,f_ev,f_phev,d;
In this case, GAMS gives the following error for Const1 and Const3.
Error 171:
Domain violation for set
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Please correct my code

Post by Renger »

Hi
You defined x over k and not over i: In these constraints you are using x(i) but you defined x(k)
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Parisa
User
User
Posts: 14
Joined: 5 years ago

Re: Please correct my code

Post by Parisa »

I know that, but in the problem "x" is defined over both "i" and "k". How can I fix this issue?
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Please correct my code

Post by Renger »

If k is a subset of i, you can define the set k as k(i) and then the code runs fine.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Post Reply