Random binary value generation

28 views
Skip to first unread message

TFaiz

unread,
Jun 23, 2016, 5:19:50 PM6/23/16
to AMPL Modeling Language
Hello,

I am trying to generate random binary values for each of the grid points in my problem. For example, for grid location (1,1) in a 5x5 grid, I have 24 grid cells as its potential assignee. I want to assign value 1 to 3 random cells, and 0 to the rest of the cells. Can I do that in AMPL?

Thank you.

Robert Fourer

unread,
Jun 25, 2016, 3:42:12 PM6/25/16
to am...@googlegroups.com
There are random functions in AMPL, such as Uniform(i,j) which returns a randomly chosen value between i and j. Using random functions you can write an AMPL script to make random assignments of many kinds. However it's not clear what particular computation you are trying to do, and especially how "grid cells" relate to "grid locations". Can you give a specific example?

(Of course nothing calculated by a computer is truly random, but some care has been taken to make AMPL's random functions return floating-point numbers that compare well to what would be expected of a random sample.)

Bob Fourer
am...@googlegroups.com

=======

Tasnim Faiz

unread,
Jun 25, 2016, 4:06:55 PM6/25/16
to am...@googlegroups.com

Thank you for your reply.

I have a 5x5 grid with 25 grid cells having coordinates (1,1), (1,2).....(5,4), (5,5).

For (1,1) grid cell, I want to generate random binary (0 or 1) parameter that indicates whether there is a connection between (1,1) and each of the other available 24 cells.

I want to generate random binary parameters B(i,j, x,y)={1,0}
Where (i,j)=(1,1) fixed
And (x,y)=(1,2)....(5,5)
1 indicates (i,j) and (x,y) are connected.
I also want give the limit of the number of connections that (1,1) can have.

Thank you.

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/xVaOcCqOJkY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Robert Fourer

unread,
Jun 26, 2016, 1:54:31 PM6/26/16
to am...@googlegroups.com
You could write something like this:

set GRID = {1..5,1..5};
param B {GRID} default 0;
param row; param col;

for {k in 1..3} {
repeat {
let row := ceil(Uniform(0,5));
let col := ceil(Uniform(0,5));
} until not (row = 1 and col = 1) and B[row,col] = 0;
let B[row,col] := 1;
}

Exactly 3 elements of B that are not (1,1) will be assigned the value 1, and the rest will be zero. You can modify this logic as necessary to fit the situation in your model.

Bob Fourer
am...@googlegroups.com

=======

Tasnim Faiz

unread,
Jun 26, 2016, 2:01:29 PM6/26/16
to am...@googlegroups.com
Thank you so much.

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/xVaOcCqOJkY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



--
Tasnim Ibn Faiz
Reply all
Reply to author
Forward
0 new messages