Logic assignment for parameters

Problems with syntax of GAMS
Post Reply
Hefeng Zhai
User
User
Posts: 6
Joined: 8 months ago

Logic assignment for parameters

Post by Hefeng Zhai » 8 months ago

I’m trying to use the following logic to assign parameters for Mn(n)
For n=1to 100 by 1;
Mn(1)=0, Mn(2)=0, is the initial value;

If n is odd, then Mn(n)=n-2+ Mn( (n-1)/2 ) + Mn( (n+1)/2 ) ;
If n is even, then Mn(n)=n-2+ 2*Mn( (n)/2 );

I am trying to run the following model, but I am not successful. Can anyone help me achieve this goal?

Best Regards!
Hefeng
***=============================================================================
SETS
i component /i1*i100/ ;

Scalar
Count ;
Count=1;

Parameters
Nni(i)
;

Nni('i1')=0;
Nni('i2')=0;

while( (Count<=100),

if( (mod(Count,2)=1),
Nni(i)$(ord(i)=Count)=Count-2+Nni(i)$(ord(i)=(Count-1)/2)+Nni(i)$(ord(i)=(Count+1)/2) ;
else
Nni(i)$(ord(i)=Count)= Count-2+2*Nni(i)$(ord(i)=Count/2) ;

);


Count=Count+1;

);

Display Nni ;
Mr. Hefeng Zhai
School of Electrical Engineering, Shandong University, 17923 Jingshi Road, Jinan P.r.China 250061

User avatar
bussieck
Moderator
Moderator
Posts: 115
Joined: 1 year ago

Re: Logic assignment for parameters

Post by bussieck » 8 months ago

This seems to work:

Code: Select all

set n /1*10/;
parameter Mn(n) /#n 0/;
loop(n,
  Mn(n)$(mod(n.val,2)=1) = n.val-2 + Mn(n-((n.val+1)/2)) + Mn(n-((n.val-1)/2));
  Mn(n)$(mod(n.val,2)=0) = n.val-2 + 2*Mn(n-(n.val/2));
);
display Mn;
This gives:

Code: Select all

----      7 PARAMETER Mn

1  -1.000,    2  -2.000,    3  -2.000,    4  -2.000,    5  -1.000,    7   1.000
8   2.000,    9   4.000,    10  6.000
Be aware that your recursion formula for n=1 accesses Mn(0) (Mn((1-1)/2) in the "odd" case). This is not problem for GAMS, this will evaluate just as 0. But you should think about this.

-Michael

Hefeng Zhai
User
User
Posts: 6
Joined: 8 months ago

Re: Logic assignment for parameters

Post by Hefeng Zhai » 8 months ago

bussieck wrote:
8 months ago
This seems to work:

Code: Select all

set n /1*10/;
parameter Mn(n) /#n 0/;
loop(n,
  Mn(n)$(mod(n.val,2)=1) = n.val-2 + Mn(n-((n.val+1)/2)) + Mn(n-((n.val-1)/2));
  Mn(n)$(mod(n.val,2)=0) = n.val-2 + 2*Mn(n-(n.val/2));
);
display Mn;
This gives:

Code: Select all

----      7 PARAMETER Mn

1  -1.000,    2  -2.000,    3  -2.000,    4  -2.000,    5  -1.000,    7   1.000
8   2.000,    9   4.000,    10  6.000
Be aware that your recursion formula for n=1 accesses Mn(0) (Mn((1-1)/2) in the "odd" case). This is not problem for GAMS, this will evaluate just as 0. But you should think about this.

-Michael
Dear Michael,
Thank you very much for your help and advice! :D
Best Regards
Hefeng

Myron Bennett
User
User
Posts: 4
Joined: 3 months ago

Re: Logic assignment for parameters

Post by Myron Bennett » 3 months ago

Before proceeding to the study of logical operators, I assembled models compiled in the early stages of GAMS development and used them in the examples' library.
I already had thoughts about using the help of programmers with many years of experience in these areas https://www.assignmentexpert.com/programming, since the approach I took to study has a little drawback: the initial "usability" to the package may take a certain amount of time (this is actually my initial experience).
However, modern appearance still has a developed,
fully functional Windows-like interface and therefore I hope that this problem will be easily overcome by me shortly.
Thanks for the wise advice!

Regards, Myron

Post Reply