How can I random the consecutive number?

Problems with modeling
Post Reply
celebpanama
User
User
Posts: 3
Joined: 1 week ago

How can I random the consecutive number?

Post by celebpanama »

I would like to random the consecutive 1 for 4 times in each column j.
My target is like, for example,

Code: Select all

i/j  1    2    3    4    5   6
1    0    1    0    0    0   1
2    0    1    0    0    0   1
3    0    1    1    0    0   1
4    1    1    1    0    0   1
5    1    0    1    0    1   0
6    1    0    1    0    1   0
7    1    0    0    0    1   0
8    0    0    0    0    1   0
9    0    0    0    0    0   0
10   0    0    0    0    0   0
Thank you for the recommendation.

Right now, I cannot fix 4 times each column

set i /r1*r10/, j /c1*c6/, iter /1*3/;

parameter
randval(i,j) Random values,
randvalloop(iter, i,j) Store the random values;

$set seed 123456
loop(iter,
randval(i, j) = uniformInt(0,1);
randvalloop(iter, i,j) = randval(i,j);
);

* Check the results
display randvalloop;
User avatar
bussieck
Moderator
Moderator
Posts: 1043
Joined: 7 years ago

Re: How can I random the consecutive number?

Post by bussieck »

Here is some GAMS code of what I think you want to accomplish. Not sure what the iter was about, but I create now iter many of such tables with random starting position of 4 consecutive ones in the columns:

Code: Select all

set i /r1*r10/, j /c1*c6/, iter /1*3/;

parameter
rowstart, cnt,
randval(i,j) Random values,
randvalloop(iter, i,j) Store the random values;

$set seed 123456
loop(iter,
  loop(j,
    rowstart = uniformInt(1,card(i)-3);
    loop(i$(ord(i)=rowstart),
      for (cnt=0 to 3,
        randvalloop(iter, i+cnt,j) = 1;
      )
    )
  )
);

* Check the results
option randvalloop:0:1:1;
display randvalloop;
-Michael
celebpanama
User
User
Posts: 3
Joined: 1 week ago

Re: How can I random the consecutive number?

Post by celebpanama »

Thank you very much.
I have some question. How GAMS can start 1 from r1 at some column and 1 end up with the last row (r10) in every iteration? As shown in the table below, 1 can start in row 1 (r1) both c2 and c6, and 1 end at row 10 (r10) in c5.

Code: Select all

      c1   c2   c3   c4   c5  c6
r1    0    1    0    0    0   1
r2    0    1    0    0    0   1
r3    0    1    1    0    0   1
r4    1    1    1    0    0   1
r5    1    0    1    0    0   0
r6    1    0    1    0    0   0
r7    1    0    0    0    1   0
r8    0    0    0    0    1   0
r9    0    0    0    0    1   0
r10   0    0    0    0    1   0
Last edited by celebpanama 1 week ago, edited 2 times in total.
User avatar
bussieck
Moderator
Moderator
Posts: 1043
Joined: 7 years ago

Re: How can I random the consecutive number?

Post by bussieck »

You can just check if no 1-sequence starts in r1 or ends in r10. If that's the case you just take first column or last column to be the start/end. -Michael

Code: Select all

set i /r1*r10/, j /c1*c6/, iter /1*3/;

parameter
rowstart, cnt,
randval(i,j) Random values,
randvalloop(iter, i,j) Store the random values;

$set seed 123456
loop(iter,
  loop(j,
    rowstart = uniformInt(1,card(i)-3);
    loop(i$(ord(i)=rowstart),
      for (cnt=0 to 3,
        randvalloop(iter, i+cnt,j) = 1;
      )
    )
  )
* No start in any column  
  if (sum(j, randvalloop(iter,'r1',j))=0, randvalloop(iter,i,'c1') = ord(i)<5);
* No end in any column  
  if (sum(j, randvalloop(iter,'r10',j))=0, randvalloop(iter,i,'c6') = ord(i)>card(i)-4);
);

* Check the results
option randvalloop:0:1:1;
display randvalloop;
celebpanama
User
User
Posts: 3
Joined: 1 week ago

Re: How can I random the consecutive number?

Post by celebpanama »

It was as I expected. Thank you very much.
celebpanama
User
User
Posts: 3
Joined: 1 week ago

Re: How can I random the consecutive number?

Post by celebpanama »

Sorry to disturb you again. It seems that the patterns in every iteration are fixed. How the same pattern baesd on above conditions will change if I click run every time and set only 1 iteration? I attempt to solve but I am not sure whether it involves in seed or anything? Thank you.

Code: Select all

set t /1*10/, i /1*6/, [b]iter /1*1/[/b];

parameter
rowstart, cnt, q,
randval(t,i) Random values,
randvalloop(iter, t,i) Store the random values

$set seed 123456

loop(iter,
  q = 6  
  loop(i,
    rowstart = uniformInt(1,card(t)-(q-1));
    loop(t$(ord(t)=rowstart),
      for (cnt=0 to (q-1),
        randvalloop(iter, t+cnt,i) = 1;
      )
    )
  )
* No start in any column  
  if (sum(i, randvalloop(iter,'1',i))=0, randvalloop(iter,t,'1') = ord(t)<(q+1));
* No end in any column  
  if (sum(i, randvalloop(iter,'10',i))=0, randvalloop(iter,t,'6') = ord(t)>card(t)-q);
  
);

* Check the results
option randvalloop:0:1:1;
display randvalloop;
Post Reply