# Split / partition mass addition lists

Im trying to add up the values in the list on the left and then split them every time they add up to 120 and then start a new list doing the same thing. So for the mass addition list on the right it would hit list item number 11 and then start the mass addition over from list item number 12.

See that if it fits !

kind of, it does not give me the decimals which i need.

Change int to double or try the anemone version

1 Like

limit int double.gh (7.2 KB)
@tim.stark C# is not easy to read.

knowing nothing about c# I never would have been able to figure that out, thanks for the help, works like a charm. In the interests of learning, does anyone know of a way to do it using standard components

I have an idea, but can‘t really describe it now. I will make an example for you tomorrow

Hey,

Check this out.
I have done some data-grouping with standard components there.

It does not includes the asked grouping by sum (yet). I will try it one the next free time-slot
But there is allready a grouping by ‘relative difference’ which may fit from a conceptual point of view.

Greets
Mark

I doubt about the possibility to do that without recursion. I made something with Modulo operator but it fails and it is far more complicated than the code. The magic about Grasshopper is to mix the 2.
DataTree output = new DataTree();
double sum = 0;
int path = 0;
foreach(double val in x)
{
if ((sum + val) > limit)
{
sum = 0;
path += 1;
}
sum = sum + val;
}
A = output;

vs that that fail !!!

Hadn‘t tried it out yet, but it could work.

Series (0, 200, 400 etc) -> graft
Pr larger than series,
Create set -> member index from set members and mass addition
List item (List = values, Index = member index)

Currently no time, but maybe someone can try this

Flatten output of mass addition and it works

@tim.stark
I tried your guess, but it fails.

I tried other methods too, but all fail. Its not an easy task.
My last guess would be to check all possible combinations of partional-mass-sum, which I think is the only way to do so.
But till now I haven’t found a good way to combine it back to the original list.

Its such an easy task do it with code in a loop, so I think its not worth the time for me to go on with components.

But, I’m curious to see how it can be done

try.gh (26.0 KB)
tStark.gh (13.0 KB)

Tried it by myself and it works. Currently at work. In the evening I can upload an example.

Edit:
Oh wait. I read your question one more time and I understood you wrong
I‘ll try it in the evening to find a solution without recursion

Edit 2:
Ok, I understood you right. Finally at the pc and can have a look at it. It fails unluckely sometimes if numbers are higher than 0.5*target sum. So without recursion I don’t see any solution.

I gave this a try yesterday, but it looks like you would have to:

• Generate every possible way of partitioning the list (with any number of partitions, at any position). I’m not solid on combinatorics, but this is a very big number of options.
• Mass add every list, and discard all the options which contain a list which overshoots the number limit. Then check all the options for undershooting (ie. the partition could have been further down the list)
• There should only be one option not discarded. Find it.
• Cry.

Or you could do it with recursion really easily

3 Likes