Problem with equation written as sequence

Problems with modeling
Post Reply
ltx
User
User
Posts: 4
Joined: 2 years ago

Problem with equation written as sequence

Post by ltx »

Hi everyone,
I had a problem with expressing the following equation
D8[RM27~_3FN]0SXF89MWNO.png
D8[RM27~_3FN]0SXF89MWNO.png (3.46 KiB) Viewed 1971 times
Here ε and η are constant parameters. z(t) and x(t) should be two variables with set t. I tried to write the equation as this: z(t+1) =e= z(t)*(1-1/η) + ε *x(t)
However, the code failed and returned error 149. I want to know how to express t+1 for my variable z(t) or I need to find the general equation for z(t)?
Thanks for your help!

Here is my first attempt

Code: Select all

sets
 
    t	periods	/0*50/

    
;
alias (t,tt);

parameters
          a	demand curve intercept	/8/
          b	demand curve slope	/0.4/
          C	marginal extraction cost	/2/
          r	discount rate	/0.05/
          Q total resource available  /80/
          d marginal cost of "backup tech" /6/
          e constant cost of GHG /0.25/
          eta residence time of GHG /100/
          epsilon emission factor /0.1/
          lambda	marginal user cost
                                        
;


variables
x(t)	quantities extracted per period
p(t)	price of extracted resource per period 
nb	    present value of net benefit
v(t)    quantities of renewable energy extracted per period
z(t)    GHG stock

;

equations
        obj_func	    objective function 
        dem_curve(t)	demand curve
        cons	        total available resource
        GHG_stock       GHG stock equation of motion 
;



obj_func.. nb =e= sum(t, exp(-r*t.val) * (a*(x(t)+v(t))- 0.5*b*(x(t)+v(t))**2 - c*x(t)-d*v(t)-e*z(t)**2)) ;
dem_curve(t)..  p(t) =e= a - (b*(x(t)+v(t)));
cons..	sum(t,x(t)) =e= Q;
GHG_stock.. z(t+1) =e= z(t)*(1-1/eta) + epsilon*x(t)


model extraction /obj_func, dem_curve, cons,GHG_stock/;
*	set lower bounds to avoid errors and ensure a meaningful solution
x.lo(t) = 0;
p.lo(t) = 0;
v.lo(t) = 0;

z.fx(t)$(ord(t)=1) =1;
solve extraction using nlp maximizing nb;




lambda(t)$(x.l(t) > 0) = exp(-r*t.val)*(a - b*x.l(t)-c);
display lambda;
And my second attempt returns error 143 that a suffix is missing

Code: Select all

sets
 
    t	periods	/0*50/

    
;
alias (t,tt);

parameters
          a	demand curve intercept	/8/
          b	demand curve slope	/0.4/
          C	marginal extraction cost	/2/
          r	discount rate	/0.05/
          Q total resource available  /80/
          d marginal cost of "backup tech" /6/
          e constant cost of GHG /0.25/
          eta residence time of GHG /100/
          epsilon emission factor /0.1/
          lambda	marginal user cost
                                    
;


variables
x(t)	quantities extracted per period
p(t)	price of extracted resource per period 
nb	    present value of net benefit
v(t)    quantities of renewable energy extracted per period
z(t)    GHG stock

;

equations
        obj_func	    objective function 
        dem_curve(t)	demand curve
        cons	        total available resource
        GHG_stock       GHG stock equation of motion 
;

obj_func.. nb =e= sum(t, exp(-r*t.val) * (a*(x(t)+v(t))- 0.5*b*(x(t)+v(t))**2 - c*x(t)-d*v(t)-e*z(t)**2)) ;
dem_curve(t)..  p(t) =e= a - (b*(x(t)+v(t)));
cons..	sum(t,x(t)) =e= Q;



model extraction /obj_func, dem_curve, cons/;



*	set lower bounds to avoid errors and ensure a meaningful solution
x.lo(t) = 0;
p.lo(t) = 0;
v.lo(t) = 0;

loop(t,
z.fx(t)$(ord(t)=1) =1;
z(t+1) = z(t)*(1-1/eta) + epsilon*x(t);
solve extraction using nlp maximizing nb;
)
lambda(t)$(x.l(t) > 0) = exp(-r*t.val)*(a - b*x.l(t)-c);
display lambda;
Last edited by ltx 2 years ago, edited 1 time in total.
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Problem with equation written as sequence

Post by bussieck »

Why don't you post what you already have. Then we might be able to help. -Michael
ltx
User
User
Posts: 4
Joined: 2 years ago

Re: Problem with equation written as sequence

Post by ltx »

Hi, here is my first attempt where returns error149

Code: Select all

sets
 
    t	periods	/0*50/

    
;
alias (t,tt);

parameters
          a	demand curve intercept	/8/
          b	demand curve slope	/0.4/
          C	marginal extraction cost	/2/
          r	discount rate	/0.05/
          Q total resource available  /80/
          d marginal cost of "backup tech" /6/
          e constant cost of GHG /0.25/
          eta residence time of GHG /100/
          epsilon emission factor /0.1/
          lambda	marginal user cost
                                         
;


variables
x(t)	quantities extracted per period
p(t)	price of extracted resource per period 
nb	    present value of net benefit
v(t)    quantities of renewable energy extracted per period
z(t)    GHG stock

;

equations
        obj_func	    objective function 
        dem_curve(t)	demand curve
        cons	        total available resource
        GHG_stock       GHG stock equation of motion 
;



obj_func.. nb =e= sum(t, exp(-r*t.val) * (a*(x(t)+v(t))- 0.5*b*(x(t)+v(t))**2 - c*x(t)-d*v(t)-e*z(t)**2)) ;
dem_curve(t)..  p(t) =e= a - (b*(x(t)+v(t)));
cons..	sum(t,x(t)) =e= Q;
GHG_stock.. z(t+1) =e= z(t)*(1-1/eta) + epsilon*x(t)


model extraction /obj_func, dem_curve, cons,GHG_stock/;
*	set lower bounds to avoid errors and ensure a meaningful solution
x.lo(t) = 0;
p.lo(t) = 0;
v.lo(t) = 0;

z.fx(t)$(ord(t)=1) =1;
solve extraction using nlp maximizing nb;


lambda(t)$(x.l(t) > 0) = exp(-r*t.val)*(a - b*x.l(t)-c);
display lambda;
I also tried to use a loop statement but it returns an error 143 says that a suffix is missing.

Code: Select all

sets
 
    t	periods	/0*50/

    
;
alias (t,tt);

parameters
          a	demand curve intercept	/8/
          b	demand curve slope	/0.4/
          C	marginal extraction cost	/2/
          r	discount rate	/0.05/
          Q total resource available  /80/
          d marginal cost of "backup tech" /6/
          e constant cost of GHG /0.25/
          eta residence time of GHG /100/
          epsilon emission factor /0.1/
          lambda	marginal user cost
                                    
;


variables
x(t)	quantities extracted per period
p(t)	price of extracted resource per period 
nb	    present value of net benefit
v(t)    quantities of renewable energy extracted per period
z(t)    GHG stock

;

equations
        obj_func	    objective function 
        dem_curve(t)	demand curve
        cons	        total available resource
        GHG_stock       GHG stock equation of motion 
;

obj_func.. nb =e= sum(t, exp(-r*t.val) * (a*(x(t)+v(t))- 0.5*b*(x(t)+v(t))**2 - c*x(t)-d*v(t)-e*z(t)**2)) ;
dem_curve(t)..  p(t) =e= a - (b*(x(t)+v(t)));
cons..	sum(t,x(t)) =e= Q;



model extraction /obj_func, dem_curve, cons/;



*	set lower bounds to avoid errors and ensure a meaningful solution
x.lo(t) = 0;
p.lo(t) = 0;
v.lo(t) = 0;

loop(t,
z.fx(t)$(ord(t)=1) =1;
z(t+1) = z(t)*(1-1/eta) + epsilon*x(t);
solve extraction using nlp maximizing nb;
)
lambda(t)$(x.l(t) > 0) = exp(-r*t.val)*(a - b*x.l(t)-c);
display lambda;
Thanks so much for your help
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Problem with equation written as sequence

Post by bussieck »

If you want this as a constraint, you have to stick to your first solution. The second does this just as data. In your first solution you need to index your equation GHG_stock over t. This is such a rookie mistake that I wonder if you covered the basics of GAMS. I recommend spending more time with the documentation and especially the tutorials.

-Michael
Post Reply