Help with Randomization of Non-Overlapping Images

180 views
Skip to first unread message

Tara Miskovich

unread,
Jul 28, 2014, 4:43:33 PM7/28/14
to e-p...@googlegroups.com
Hello,

I am working on having 4 images randomly places within a rectangular region without overlapping. I wrote the code below, and it appears to be working with up to Image 3 added, but when I add in the forth image the program freezes. I am hoping for some guidance with what to try next or how to get past this. Here is the code I wrote to randomize the x and y location for each image.

Thanks!
Tara

Dim x1 As Integer
Dim x2 As Integer
Dim x3 As Integer
Dim x4 As Integer
Dim y1 As Integer
Dim y2 As Integer
Dim y3 As Integer
Dim y4 As Integer

x1 = random(36, 42)
y1 = random(38, 60)

Do
x2 = random(36, 42)
y2 = random(38, 60)
x3 = random(36, 42)
y3 = random(38, 60)
x4 = random(36, 42)
y4 = random(38, 60)


Loop While (x2 = x1 Or x2 = x1 + 1 Or x2 = x1+2 Or x2 = x1 - 1 Or x2 = x1 - 2 _
And y2 = y1 Or y2 = y1 + 1 Or y2 = y1 + 2 Or y2 = y1 + 3 Or y2 = y1 - 1 Or y2 = y1 - 2 Or y2 = y1 - 3)_
Or (x3 = x1 Or x3 = x1 + 1 Or x3 = x1+2 Or x3 = x1 - 1 Or x3 = x1 - 2 _
And y3 = y1 Or y3 = y1 + 1 Or y3 = y1 + 2 Or y3 = y1 + 3 Or y3 = y1 - 1 Or y3 = y1 - 2 Or y3 = y1 - 3)_
Or (x3 = x2 Or x3 = x2 + 1 Or x3 = x2+2 Or x3 = x2 - 1 Or x3 = x2 - 2 _
And y3 = y2 Or y3 = y2 + 1 Or y3 = y2 + 2 Or y3 = y2 + 3 Or y3 = y2 - 1 Or y3 = y2 - 2 Or y3 = y2 - 3)_
or (x4 = x1 Or x4 = x1 + 1 Or x4 = x1+2 Or x4 = x1 - 1 Or x4 = x1 - 2 _
And y4 = y1 Or y4 = y1 + 1 Or y4 = y1 + 2 Or y4 = y1 + 3 Or y4 = y1 - 1 Or y4 = y1 - 2 Or y4 = y1 - 3)_
Or (x4 = x2 Or x4 = x2 + 1 Or x4 = x2 + 2 Or x4 = x2 - 1 Or x4 = x2 - 2 _
And y4 = y2 Or y4 = y2 + 1 Or y4 = y2 + 2 Or y4 = y2 + 3 Or y4 = y2 - 1 Or y4 = y2 - 2 Or y4 = y2 - 3)_
Or (x4 = x3 Or x4 = x3 + 1 Or x4 = x3 + 2 Or x4 = x3 - 1 Or x4 = x3 - 2 _
And y4 = y3 Or y4 = y3 + 1 Or y4 = y3 + 2 Or y4 = y3 + 3 Or y4 = y3 - 1 Or y4 = y3 - 2 Or y4 = y3 - 3)

c.SetAttrib "x1", x1
c.SetAttrib "x2", x2
c.SetAttrib "x3", x3
c.SetAttrib "x4", x4
c.SetAttrib "y1", y1
c.SetAttrib "y2", y2
c.SetAttrib "y3", y3
c.SetAttrib "y4", y4

tamis...@gmail.com

unread,
Aug 1, 2014, 1:30:32 PM8/1/14
to e-p...@googlegroups.com
Does anyone have any suggestions on this? I'm stuck on this and eprime support directed me here.

Tara

Vinson, David

unread,
Aug 1, 2014, 1:54:35 PM8/1/14
to e-p...@googlegroups.com
I suspect you didn't get any replies before due the impressively complicated set of OR statements in that loop. I suspect the problem is there somewhere but imagine it world be a nightmare to track it down. My guess is the loop never exits, whether due to an error or impossible constraints.

What if instead you enumerated the possible legal combinations of positions in a nested List and then just sampled one of them per trial?

Good luck,
David V
--
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To unsubscribe from this group and stop receiving emails from it, send an email to e-prime+u...@googlegroups.com.
To post to this group, send email to e-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/e-prime/8d0ddea8-f54e-4a98-a006-cdcbed5cc8ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tamis...@gmail.com

unread,
Aug 1, 2014, 5:43:22 PM8/1/14
to e-p...@googlegroups.com
Thanks for your response David. That is what I was afraid of. Eprime support said it should work in theory, but seems as though it keeps getting stuck when I add in the forth image. I am trying to list all the combinations as well, it is just proving to be a huge task, for there are an enormous amount of possible combinations.

I was hoping that this would make coding much quicker and truly random.

Tara

David McFarlane

unread,
Aug 4, 2014, 3:01:56 PM8/4/14
to e-p...@googlegroups.com
Tara,

Wow, that Loop While statement just hurts my eyes! When something
gets that complicated, I would at least try to format it better for
readability (indentation and the "_" continuation character are your
friends here), or better yet find some way to restructure the code so
as to avoid such a long expression. E.g., if you used some arrays
instead of simple variables then you could use a loop or two to run
through all those tests that appear explicitly in your While expression.

So I do not know if something goes wrong with your code per se. But
your approach here reminds me of a bogosort (see
http://en.wikipedia.org/wiki/Bogosort ), and it may suffer from the
same problem. Even with no errors in the code, the code must find a
solution *by chance*, and *by chance* that may take many tries. That
may work well enough for small problems, but as the proportion of
solutions in the problem space gets smaller this approach takes
longer and longer to find one. In particular, perhaps for 3
coordinates your "bogosort" code could quickly find a solution, but
for 4 images it just takes a lot longer to find one *by
chance*. (PST's own "No Repeats on Consecutive Trials" examples,
download from their website, suffer the same problem.)

I do not like algorithms that work by chance, so I avoid them
whenever possible. So first let's restate your problem, and see what
we can do.

You want to find 4 unique, randomly chosen x values from a pool of 7
values, and 4 unique, randomly chosen y values from a pool of 23
values. IOW, you want to pick 4 x values randomly without
replacement from the range [36, 42], and 4 y values randomly without
replacement from the range [38, 62]. Did I get that right?

Ideally, you would follow David V's advice, make a nested List with
all possible (x, y) pairs and just pick from that (Random, without
replacement). That should involve 7 x 23 = 161 rows, which would not
be too much trouble, and Excel would make it even easier to construct
such a List. Alternatively, you might use two nested Lists, one for
all the x values and another for all the y values, each Random
(without replacement) and pick from those.

But if you like inline code, I might start by making a couple arrays
to hold my pools of x & y values:

Const xPoolSize as Integer = 7, xPool1 as Integer = 36
Const yPoolSize as Integer = 23, yPool1 as Integer = 38
Dim i as Integer
Dim xPool( 1 to xPoolSize ) as Integer
Dim yPool( 1 to yPoolSize ) as Integer
xPool(1) = xPool1
For i = 2 to xPoolSize
xPool(i) = xPool(i-1) + 1
Next i
yPool(1) = yPool1
For i = 2 to yPoolSize
yPool(i) = yPool(i-1) + 1
Next i

With a little trouble you could compact that code even better by
using a 2-dimensional array for x & y together, although then the
next step would not work. I leave that as an exercise.

Next, just shuffle these arrays. VBA/E-Basic provides a command just
for this (but only for 1-dimensional arrays, sigh):

RandomizeArray xPool
RandomizeArray yPool


You may find RandomizeArray documented in the E-Basic Help facility.

Now you may simply pick the first 4 values from the xPool & yPool
arrays to construct your coordinates.

Does that look like it will work? What have I missed?

-----
David McFarlane
E-Prime training
online: http://psychology.msu.edu/Workshops_Courses/eprime.aspx
Twitter: @EPrimeMaster (https://twitter.com/EPrimeMaster)

/----
Stock reminder: 1) I do not work for PST. 2) You may reach PST's
trained staff (and other support facilities) at
https://support.pstnet.com . 3) If you do get an answer from PST
staff, please extend the courtesy of posting their reply back here
for the sake of others.
\----
Message has been deleted

tamis...@gmail.com

unread,
Aug 4, 2014, 3:51:46 PM8/4/14
to e-p...@googlegroups.com
Hi David,

Thank you so much for your reply. I know the thing is a visual nightmare at the moment and needs a little aesthetic touch up. Sorry! 

These are incredibly helpful suggestions and getting me to think about this a lot deeper.

The only issue with the array, while it would be easier, is that 1. It seems like I could still have some over lap since my images are 2x3 % in eprime and 2. ideally I would like to still be able to use the same x coordinate for two images just so long as they don't overlap (they are far enough apart on the y).

So it's really tricky since I need to consider a range of coordinates when I place an image to insure they do not overlap. 

I like the idea of the 2 dimensional array (sounds tricky) cause that would solve #1 but not sure how to get around #2 and ensuring they don't overlap + or - 2 or 3 values.

Thanks again so much for the help!

Tara

tamis...@gmail.com

unread,
Aug 5, 2014, 9:30:23 AM8/5/14
to e-p...@googlegroups.com
Hi all, 

I just got an email from a user today who had trouble posting to the board, but his solution appears to work so far. Here it is:

(I had trouble with google groups responding – I hope you’re the Tara who emailed the eprime group – if not please ignore – sorry…)

Hi Tara,

I'm not certain this will fix the problem, but I think the loop fails due to the precedence of OR vs AND in the conditional. The sets of X's and Y's need to be grouped together. Add the parenthesis shown beloww to hopefully fix the problem:

 

Loop While ((x2 = x1 Or x2 = x1 + 1 Or x2 = x1+2 Or x2 = x1 - 1 Or x2 = x1 - 2) _

And (y2 = y1 Or y2 = y1 + 1 Or y2 = y1 + 2 Or y2 = y1 + 3 Or y2 = y1 - 1 Or y2 = y1 - 2 Or y2 = y1 - 3))_

Or ((x3 = x1 Or x3 = x1 + 1 Or x3 = x1+2 Or x3 = x1 - 1 Or x3 = x1 - 2) _

And (y3 = y1 Or y3 = y1 + 1 Or y3 = y1 + 2 Or y3 = y1 + 3 Or y3 = y1 - 1 Or y3 = y1 - 2 Or y3 = y1 - 3))_

Or ((x3 = x2 Or x3 = x2 + 1 Or x3 = x2+2 Or x3 = x2 - 1 Or x3 = x2 - 2) _

And (y3 = y2 Or y3 = y2 + 1 Or y3 = y2 + 2 Or y3 = y2 + 3 Or y3 = y2 - 1 Or y3 = y2 - 2 Or y3 = y2 - 3))_

or ((x4 = x1 Or x4 = x1 + 1 Or x4 = x1+2 Or x4 = x1 - 1 Or x4 = x1 - 2) _

And (y4 = y1 Or y4 = y1 + 1 Or y4 = y1 + 2 Or y4 = y1 + 3 Or y4 = y1 - 1 Or y4 = y1 - 2 Or y4 = y1 - 3))_

Or ((x4 = x2 Or x4 = x2 + 1 Or x4 = x2 + 2 Or x4 = x2 - 1 Or x4 = x2 - 2) _

And (y4 = y2 Or y4 = y2 + 1 Or y4 = y2 + 2 Or y4 = y2 + 3 Or y4 = y2 - 1 Or y4 = y2 - 2 Or y4 = y2 - 3))_

Or ((x4 = x3 Or x4 = x3 + 1 Or x4 = x3 + 2 Or x4 = x3 - 1 Or x4 = x3 - 2) _

And (y4 = y3 Or y4 = y3 + 1 Or y4 = y3 + 2 Or y4 = y3 + 3 Or y4 = y3 - 1 Or y4 = y3 - 2 Or y4 = y3 - 3))

 

Regards,

Bill

Reply all
Reply to author
Forward
0 new messages