Random with difference between outputs as constraint

Discussions, advice, bug reports and much more about the "bach" environment.
sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Random with difference between outputs as constraint

Post by sydy » Wed May 29, 2019 8:58 am

I struggle to do it and tried different ways but couldn't find a solution.

TASK
Random function with constraints executed N-times.

The function should reply to the following conditions:
1. No elements < $1
2. No elements > $2
3. No differences between randomly generated elements < $3

Condition 1. and 2. are easy.
Its the 3. condition I can't handle.

Eg. of a (sorted) list that would reply to all conditions:
$1 = 0
$2 = 30
$3 = 5

N = 4
1. 5
2. 10
3. 16
4. 25
Any help would be appreciated.

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Random with difference between outputs as constraint

Post by sydy » Wed May 29, 2019 5:41 pm

Solved it with Javascript for now. I'm not really happy about javascript. It is not fast enough.
Anyway, if somebody can make some use of it. Here's the code.
I'm still interested in any cpu cheap solution with bach...

Greatings,
m


/** generates random integer between min and max (inclusive) **/
function generateRandomBetween(min, max){
return Math.random() * (max - min + 1) + min;
}


//test logically impossible
/**
var userDefinedMin = 5
var userDefinedMax = 17
var distance = 4
**/

// test logically possible
var n = 6; //number of generated points
var userDefinedMin = 20;
var userDefinedMax = 150;
var distance = 15;
var isDataSerieValid = true;
var envelopData = new Array(n);

//init Envelop Data
for (var i = 0; i < envelopData.length; i++) envelopData = userDefinedMin;
envelopData[n-1] = userDefinedMax;


// create envelop Data
for (i = 1; i <= envelopData.length-2; i++) {
logicalMinimum = envelopData[i-1]+distance;
logicalMaximum = (userDefinedMax - ((n-i-1)*distance));
if(logicalMaximum < logicalMinimum){
isDataSerieValid = false;
}
envelopData = generateRandomBetween(logicalMinimum,logicalMaximum);

}

// print data if it is valid
if (isDataSerieValid){
print(envelopData);
} else{
print("logisch nicht möglich ein array von min "+ userDefinedMin + " bis max " + userDefinedMax + " mit Distanz "+distance+" und länge "+ n +" zu definieren");
}

danieleghisi
Site Admin
Posts: 1349
Joined: Fri Dec 03, 2010 1:25 pm

Re: Random with difference between outputs as constraint

Post by danieleghisi » Thu May 30, 2019 6:35 am

Hi!
Have you considered using bach.constraints?

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Random with difference between outputs as constraint

Post by sydy » Sat Jun 01, 2019 12:40 pm

Hey Daniele,

Thanks a lot for your reply.

Yes, I did try constraints.
Here are the two actual solutions (the older posted in Javascript was in fact not complete).
randompoints.zip
solutions_with_javascript_and_bach
(11.29 KiB) Downloaded 178 times
I must admit the bach.constraints solutions is really a brute force method and gives for now only integer solutions (I want also floats).
If I turn on @engine 0, there are no solutions, with @engine 1, the solutions are partly wrong, with @engine 2 it works but in the meantime, you go and get a coffee. Of course, there must be ways to program it better in bach. I just don't know how to handle this for now.

If you have an idea of how to make the bach solution faster I'd be glad to have it. In javascript you have – so I believe more possibilities to make loops.
For now, javascript is like a flash compared to bach.

I put both solutions as an attachment – I couldn't add the javascript to the forum without compressing it.

Best and thanks for your help,
m

danieleghisi
Site Admin
Posts: 1349
Joined: Fri Dec 03, 2010 1:25 pm

Re: Random with difference between outputs as constraint

Post by danieleghisi » Sun Jun 02, 2019 8:02 am

Hi, I may have read your post too fast, but it seems to me that this is exactly what [drunk] does, just use the maximum step as your $3, and shift it to your $1.
Collect many drunk outputs and you are done.

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Random with difference between outputs as constraint

Post by sydy » Sun Jun 02, 2019 12:03 pm

really :shock: ? No, :) unfortunately not.
drunk has a stepsize limit, not a stepsize minimum. it will randomly choose a step size inside its stepsize limit...
I want the MinimalDistance_between_each_generated_element >= $3 (In other words, each point has a lower and higher distance from the next >= $3.)
As far as my small brain understands it, this can only be done by iterating through the random generation process, comparing each new result with all the older ones.
That's roughly what my javascript does and that's also what the bach.constraint patch I patched does (although in a more brutal way)...

eg.
RandomRange: min 5, max 25
MinimalDistance between generated points(lower,higher): 4
Number of points: 3

Point 1. random between 5, 25, let's say it returns: 10
Point 2. the next point must be a random which: >=5, <=10-4, >=10+4, <=25,
so the possible ranges left for the random "selection" are: 5-6, 13-25. let's say it returns 16
Point 3. the next point must >=5, <=10-4, >=10+4, <=16-4, >=16+ 4, <=25,
so the possible ranges left for the random "selection" are: 5-6, 20-25.

There are of course data sets which are not possible for instance:
RandomRange = 5,17
MinimalDistance = 4
Number of points to be generated: 5

(I use this for the x-axis of envelopes with a specific number of points which I want to be randomly generated but without having points closer than a specific distance to avoid clicks for instance.)
Hope this explanation was clearer.

AND – Sorry for writing messages that long.

danieleghisi
Site Admin
Posts: 1349
Joined: Fri Dec 03, 2010 1:25 pm

Re: Random with difference between outputs as constraint

Post by danieleghisi » Sun Jun 02, 2019 6:49 pm

Yep, sorry. I misread your post, it couldn't have been that simple.

I still think you can solve it fairly easily though. Can this be something that goes more or less your way?

<pre><code>
----------begin_max5_patcher----------
1057.3oc0YsrjaiBEcs7WAk1NNt3gdfxprIeESkxkrEc2jxB4Bg6oyzU+uOH
PpicFjM5QKmrwt.i0gy8dtOP75pfvcUuvpCAeF72fffWWEDXlpYhf1wAgk4u
r+PdsYYg6qJKYBU3Z6uoXunLyuKW7H3IljATU.8jx78uunCbAae0IgYkj1IO
JY05GTthWI15bEhSkUmTGXJCxvt+WtZ+Sbwiakr8J6NGgnafqAwzMXRBMBif
w3DBJdMHJq4GhR2.Aeq8+yKL62pce+SDZ3OghK5PB0L2aqV07w5IZV3BtJbn
7ASiM7IYS5E7g1QHLrGBk9gSH7lASmz9Yi0u0GaRbyl1IenRnp4+KyHYxZb8
vrTZJJilfRimQJiFNkwwikywKKm2cRopDt4G5Z7KogEYFtfi597cFYeJpebj
YWuI8PnSBSBeGFYdISwjaYh7cGXmah+vjyppA6aITCeyLd3aDOBms3QA6ezO
QGoc2+zliRtPAdX3LIwr6IXyWIVmI1MUvYWUXVm+LqXqdg5G91bkRx0JKakk
f2YTP3yLYsNeu9QriIa9QJDBM5Vq8X9sJec3wtojyrJ1A8ZUn+NZU5Srq2gE
75gWOJBlrQWLEixtsjGOekfZsGN2s3qzM.z59h5xy9Ktu+Wtov0WI+DNYQxO
cUgbkTke.7E8FGTNRyANyniQHxTsGQ+AkGPA1ANLbKFwlVLN0ZxfFwO9lk2z
lM2lLxhTEPxdbv0w6nJ0TN21zxMDGNYH1ICwCSTvzhghyhpV1xEE7G5oBJ4l
1uHThUpD6WzU+REjSCIYjFRz8vP9Wf3gqCyxNKK030gt62BOyL7SiggjKXHd
jLDksHLLWxUOUV2W0WOxnPLkcnzwxS2m8K59VlYjmVxJtQX7jOsDJ9t2MxDJ
yz1.4nCuQj+PJyLtFWIwQm0l8j6bEgVFsxHIq8TzHakyoSV3u0j05RmKxlcW
ekIOHqJG9qZ.S88klDuLcKmKJpJG6YCvVeXZlm864rYkksRN.APCuskNkaLx
+FbcFetjkyMa.ysN7KWsggfMyeowrt5jbe21u6kRB9IEKX0JtvbSEmsHjcMN
8W9BT1BgSS5Qu.BMUf7AGhEnNAjrv5MQe3H2bNUWHCmFx9nVvygWL1Wq6TQB
6CPWJdmI2nOHigtQdhtQuBFS9PHMzWnm+vlDeL3yhl5xv9dPZNRBgI9lKXxb
xK45bjAGOhLqk7hiUbgpsJHJh1b9hn1yeYuV1KGggl1zPT6IQZFs.YRZdmxt
BpvSC4HefNYNxKi7Mc47G9h7MeI5tjur4Bxl+T0dkuDmcanscJle7XaqkFba
PU2f82qr8Xt1LjKrCMmwITxdl2s9XyL4RcqyJc+qmjlcZ3KI1aJIrrRCr3Du
0Yq4qFRSy6B8g2pOlaoloG+Uus5+.0hXIo.
-----------end_max5_patcher-----------
</code></pre>

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Random with difference between outputs as constraint

Post by sydy » Mon Jun 03, 2019 11:36 am

(It must be my awful English. In German I'd explain myself better) Your solution gives (not always!) the wrong results.
You can see it if you collect the extracted values, sort them and compare the differences:
The differences between the elements of the list should be >= mindist
If it's smaller than the results are wrong.

See my version with a little check routine at the end. (Try it at least 3 times or 5 if there are no wrong solutions at the beginning, they might come later).
I'm not sure you can do it the way you started it – at least not if you want always right solutions. I know the problem looks easy at the beginning but (I think) it's really much trickier... :?
But anyway, I don't want to bother you here anymore with my problem. I got a fast javascript solution now and that's not the end of the world.

Thanks for your help anyway. :)
M

<pre><code>
----------begin_max5_patcher----------
2316.3oc6b01iiZjD9yd9UzxeMSrn52.hzc5VkLmtUZurm1cyKR2FMBayLC4
rAK.uYRhx+8itaXV6YL1cCEVXknQxi.aSwS00KOUUM92uZxz4YOFWLk7Uj+K
Yxje+pISzmRchI0GOY55nGWrJpP+wllF+KYy+4oWadqx3GK0mda5lnE+Ohm4
ul2Nc65rskqhK0eWd8YMmp7W2DaD7zjzxoWen+Q9o5uRxRsTpj7WFDryUOIs
4hC0mbST4hGRRu+173Eklqefmbl20DeuY9LY.mBdBpjAhqIA9p2fRm4Q9I0W
+Ot5J0KWaotXcbQQz8wuPYHHU+IHrCqFf1TCGFthCBW5IgKm8B3Bd8Cusr1u
JKZoRUPPE2rNtLCvL9t.2Kn+.eQ150wJyxmg7u+l289W+1uk7Cu9C+KxW+1+
8+4Uu60uu5329OIu5MugbyO9g28pu9C27Mju+Uu46t48jOl9g7ekDURVEGUT
RXj0QIoj4Qo2WPlMaFoQ.qRRiWjsMsb2k580ldGPkQcUkwogZUlm5Ulu4eLb
sNp.KY0gsHnsZQbcaVEdNCQNWgJAGzV.gyD36.LOZwCy1jWEwhrLonLJcQUL
UaW07CcER9L8BUkAtdwi58Ya65uTQzmhWda0Uu56baTYYdx7skl37SdBZSl9
o37hjrzJ4NONW8lAdddf9M0JF7UOebZ0IyiVTFuj7PTA4WxyRu+IklxyHZ4x
7pXIebp8JPmSHHETsMguTYMv7DWBJv736wHtpuy4SZzVbcLTQmUVwUJok6rF
ddUeeAAPQ8wcV8YBqJ.1yUe3gvh3UsQ5p0XrprNsFm0m0QqDdfFlL9P.S8B4
e+u435XaDI8ocEil+I8+btjwaXijxJQbPEF6nYea0x.5pVyjtRbQDr8Q5xGQ
IbgWGyMwB0dPR1f4GUjkWR9GUvfT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KdHoLdQ41b880zGq+cLb55rpkrzsI0qZU
nqRj58BPZz53hMQFfn2x.W8GW8+gKhaO3
-----------end_max5_patcher-----------
</code></pre>

danieleghisi
Site Admin
Posts: 1349
Joined: Fri Dec 03, 2010 1:25 pm

Re: Random with difference between outputs as constraint

Post by danieleghisi » Tue Jun 04, 2019 9:07 pm

sydy – are you saying that you want the difference between ANY TWO extracted numbers to be >= K for some K? Because that's what your sort function at the end seem to imply... If so, why don't you just divide everything by K, extract ANY random integer (taken between, say, A/K and B/K) and then multiply the integer by K?

Again, I have the feeling that I'm missing something obvious... Sorry...

Daniele

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Random with difference between outputs as constraint

Post by sydy » Wed Jun 05, 2019 12:08 am

It's me who's sorry. I think I don't explain the problem in a clear way – I tried though...

Could you patch me what you're proposing?
I must admit, I'm not sure if I understand fully what you want to do.

But yes, the distance between any two of the randomly extracted elements should be >= K

Good night

(I might probably end up by understanding that I made an Elephant out of moustique.)

Post Reply