How to compare the two parameters Topic is solved

Problems with syntax of GAMS
Post Reply
Lucas
User
User
Posts: 16
Joined: 4 months ago

How to compare the two parameters

Post by Lucas » 2 months ago

Hi all,

How to compare the two parameters (0-1 parameters) together and find out if they are the same or not?
I wanted to use the Sameas command but I could not do it.
example compare 'a'

Code: Select all

set i/1*5/
	k(i);
parameters 
a(i) /1 1,2 0,3 1,4 0,5 0/
b(i) /1 0,2 1,3 1,4 0,5 0/
c(i) /1 1,2 0,3 1,4 0,5 0/
d(i) /1 1,2 0,3 1,4 0,5 1/
e(i) /1 1,2 1,3 1,4 0,5 1/
must to see a<>b and a=c, a<>d.


My try

Code: Select all


k(i)$(a(i))=yes;
scalar counter/0/
same/0/;
*count the number of index tha a(i) and b(i) are same 
         loop(i$,
                  counter=0;
                  
                         if( abs(a(i)-b(i))< EPS,
                                        counter= counter+1;
                         else
                                         break;

                            );

                    );
               if(counter=card(k),
                       same=1;
                 else
                 	same=0;
                 	
                  display  same;
                 );


I don't know How to use this methods for other parmeters (c,d,e,...) too? (in the loop?)
Thanks!

User avatar
Renger
Posts: 378
Joined: 3 years ago

Re: How to compare the two parameters

Post by Renger » 2 months ago

Hi Lucas

Perhaps I don't get the question correctly, but here is a simpler way for comparing two parameters

Code: Select all

parameter same;
same = sum(i, abs(a(i)-b(i)));
if(same > 0,
   display 'Parameter a is not equal to parameter b', same;);
Cheers
Renger
____________________________________
Enjoy modeling even more: The lazy economist

Lucas
User
User
Posts: 16
Joined: 4 months ago

Re: How to compare the two parameters

Post by Lucas » 2 months ago

Hi Renger :) first of all Thanks for your answer! your code is better than me!
I want to compare a parameter 'a' more than on time. How to compare 'a' with 'b' then 'a' with 'c' , 'a' with 'd' , 'a' with 'e' , without repeat the following code and change the b(i) to c(i), d(i) and e(i).

Code: Select all

parameter same;
same = sum(i, abs(a(i)-b(i)));
if(same > 0,
   display 'Parameter a is not equal to parameter b', same;);


Thanks

User avatar
dirkse
Moderator
Moderator
Posts: 75
Joined: 2 years ago
Location: Fairfax, VA

Re: How to compare the two parameters

Post by dirkse » 2 months ago

Lucas,

If you want to do this for many parameters, you could add an index and put all the parameters into a larger parameter. For example:

Code: Select all

set i/1*5/;
parameters 
a(i) /1 1,2 0,3 1,4 0,5 0/
b(i) /1 0,2 1,3 1,4 0,5 0/
c(i) /1 1,2 0,3 1,4 0,5 0/
d(i) /1 1,2 0,3 1,4 0,5 1/
e(i) /1 1,2 1,3 1,4 0,5 1/
;
set p / a, b, c, d, e /;
alias(p,p1,p2);
parameter
  data(p,i)
  diff(p1,p2)  'count positions where p1 different from p2';
data('a',i) = a(i);
data('b',i) = b(i);
data('c',i) = c(i);
data('d',i) = d(i);
data('e',i) = e(i);

* you can do the whole diff, or just the upper triangle
diff(p1,p2) = sum{i, data(p1,i) <> data(p2,i)};
* diff(p1,p2)$[ord(p1) < ord(p2)] = sum{i, data(p1,i) <> data(p2,i)};
-Steve

User avatar
Renger
Posts: 378
Joined: 3 years ago

Re: How to compare the two parameters

Post by Renger » 2 months ago

Hi Lucas

This only might be possible like this:

Code: Select all

set i/1*5/
    k(i);
parameters 
a(i) /1 1,2 0,3 1,4 0,5 0/
b(i) /1 0,2 1,3 1,4 0,5 0/
c(i) /1 1,2 0,3 1,4 0,5 0/
d(i) /1 1,2 0,3 1,4 0,5 1/
e(i) /1 1,2 1,3 1,4 0,5 1/
;


set parset /a, b, c, d, e/;

parameter allparam(parset,i)  All parameters;

allparam("a",i) = a(i);
allparam("b",i) = b(i);
allparam("b",i) = d(i);
allparam("b",i) = d(i);
allparam("b",i) = e(i);

parameter notequal(parset) Parameters not equal to a;


loop(parset, 
    if(sum(i, abs(allparam("a", i) - allparam(parset,i))) > 0,
        notequal(parset) = 1;
    );
);
display notequal;
____________________________________
Enjoy modeling even more: The lazy economist

Lucas
User
User
Posts: 16
Joined: 4 months ago

Re: How to compare the two parameters

Post by Lucas » 2 months ago

Dear Steve & Renger thanks a lot for your quick and useful answer!

Lucas
User
User
Posts: 16
Joined: 4 months ago

Re: How to compare the two parameters

Post by Lucas » 2 months ago

Hi again :D


why I had this error?

Code: Select all

**** Macro Definition error for Macro ISsame: Closing parenthesis missing

when I used the Macro facility had that error! so I checked all parenthesis,it was ok, but I had same error again! what's wrong?

Code: Select all

set i/1*5/;
parameter 
a(i) /1 1,2 0,3 1,4 0,5 0/
b(i) /1 0,2 1,3 1,4 0,5 0/
c(i) /1 1,2 0,3 1,4 0,5 0/
d(i) /1 1,2 0,3 1,4 0,5 1/
e(i) /1 1,2 1,3 1,4 0,5 1/;


parameter same;

*same = sum(i, abs(a(i)-b(i)));
*if(same > 0,
*   display 'Parameter a is not equal to parameter b', same;);
   


* Define Macro
$macro ISsame(a(j),b(j))   same = sum(i, abs(a(i)-b(i))); if(same > 0,display 'Parameter a is not equal to parameter b', same;);
                        
                           
* End of Macro definition

* Call Macro                        
ISsame(a(j),b(j) )


Thanks in advance!

User avatar
Renger
Posts: 378
Joined: 3 years ago

Re: How to compare the two parameters

Post by Renger » 1 month ago

Hi

Try this:

Code: Select all

$macro ISsame(a,b)   same = sum(j, abs(a-b));  if(same > 0,display 'Parameter a is not equal to parameter b', same); 
CHeers
Renger
____________________________________
Enjoy modeling even more: The lazy economist

Post Reply