Find & change in parameter Topic is solved

Problems with syntax of GAMS
Post Reply
Lucas
User
User
Posts: 23
Joined: 4 years ago

Find & change in parameter

Post by Lucas »

Hi!
How to find the third one and changed it to 4?(without loop ?Is it possible?)
Generally How may I get the nth repetitive element (1,2or 3) from set A and change it?Is there anyway to do that?

Code: Select all

Set A/1*9/;
Parameter c(A);
*Suppose p is  c(A)=121311332
 c(A) /1 1,2 2,3 1,4 3,5 1,6 1,7 3,8 3,9 2/;
 *===========
 *(E.g.  121311332 changed to 121341332
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Find & change in parameter

Post by bussieck »

You can build up a set pos(p,x,a) that gives you the location a (in your vector) for the xth occurrence of the number p and with that you easily access the location of the ith location of a value. If you change c then the set pos becomes outdated and needs to be recalculated. So I you you just access a single ith occurrence of a value to just change it then this is not much of a help over the trivial solution (implemented in macro change). This pos-solution assumes that the values of c come from small set (I used an alias to A). Anyway, here is the code.

-Michael

Code: Select all

Set A/1*9/;
Parameter c(A) /1 1,2 2,3 1,4 3,5 1,6 1,7 3,8 3,9 2/;
alias (a,p,x);
set pos(p,x,a);
parameter n(p); n(p)=0;
loop((a,p,x)$(sameas('1',x) and c(a)=p.val),
  pos(p,x+n(p),a) = yes;
  n(p) = n(p)+1;
);
option pos:0:0:1, c:0;
display pos;
display c;
c(a)$pos('1','3',a) = 4;
display c;
scalar cnt;
$macro change(v,p,nv) cnt=0; loop(a, cnt=cnt+(c(a)=v); if (cnt=p, c(a)=nv; break;)) 
* Undo previous change
change(4,1,1);
display c;
* Redo previous change
change(1,3,4);
display c;
Lucas
User
User
Posts: 23
Joined: 4 years ago

Re: Find & change in parameter

Post by Lucas »

Hi @Michael could you pleas tell me what's the cnt is?

why I got Erro?
" 158 * Undo previous change
159 cnt=0; loop(a, cnt=cnt+(c(a)=4); if (cnt=1, c(a)=1; break;));
**** $140,36"

which version of GAMS has break?
Macro command need to be written in a single line, is it Right?

Thanks
Post Reply