Grasshopper

algorithmic modeling for Rhino

rounding numbers in a set while keeping sum constant

rounding numbers in a set while keeping sum constant

Hello,

I am trying to round each of the numbers within the first list to an integer; however, I don't want the sum of each individual branch to change. So where this is a problem is in branch {4} (and others in my larger definition) where the sum of those numbers is 12 but when they are individually rounded to the nearest integer they happen to be rounded up so the new sum of the branch is 13.

In cases where this happens I would like to be able to add or subtract one from the largest number within the branch that needs to change to match its original sum. so in this case I would want {4}0 to be rounded to 6 and then have 1 subtracted from it but the other rounded numbers remain.

thanks,

- Michael -

Views: 752

Attachments:

Replies to This Discussion

Hi Michael,

what if the sum-total is more than 1 off? Do you want to subtract N from the largest number? Or do you want to subtract 1 from the N largest numbers? What if the sum-total is less? Add padding to the smallest or the largest number in a set?

This sort of stuff is easy to script, but you'll need to provide a very tight description of the algorithm you want to apply.

--
David Rutten
david@mcneel.com
Seattle, WA
Hi David,

I was trying to subtract the second mass addition list from the first giving me a list that is mostly zeros with the occasional -1 or 1 (on rare occasions - given the rest of the definition - that number could be -2 or 2 but no lower or higher than that) and then add that number to only the largest number within the corresponding branch of the list of integers.

as you can see in the attached image, once I get to that point, the number is added to every number in the corresponding branch instead of just one and I wasn't able to figure out how to disassemble the branching structure to get the largest number out of each branch and then reassemble it to its past structure. I thought there would be a relatively easy way to script it but since I haven't taught myself scripting yet (next thing on my list) I was just attempting to do it manually.

thanks,

- Michael -
Attachments:
I attached a ghx file with a VB script that performs rounding on a list of floating point numbers, then keeps on decrementing the largest value or incrementing the smallest value until the difference in pre and post sum-total values is less than 0.5.

--
David Rutten
david@mcneel.com
Seattle, WA
Attachments:
thanks David, this looks great! looking forward to figuring out how to write VB script myself.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service