Page 1 of 1

Logic assignment for parameters

Posted: Fri Dec 22, 2017 1:59 am
by Hefeng Zhai
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 ;

Re: Logic assignment for parameters

Posted: Fri Dec 22, 2017 5:01 am
by bussieck
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

Re: Logic assignment for parameters

Posted: Fri Dec 22, 2017 7:44 am
by Hefeng Zhai
bussieck wrote: 6 years 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

Re: Logic assignment for parameters

Posted: Tue May 08, 2018 10:57 am
by Myron Bennett
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