# Mass Addition two lists of values (a1;a2;a3...)(b1,b2,b3...)

Hi Guys
I have the following problem:
I have multiple 3D scanned treelogs and I want to arrange them on top of each other like a loghome wall.
The goal is to end up with a horizontal line at the top. So I have two lists of values, one with diameter on side A and one with the diameter of the treelog on side B. The two lists diameters should be added up and have the same height in the end. So I think I need to have a loop interchanging the diameter A and B of each tree until it`s lets say equal within 5percent tolerance. I would than orient the 3D scanned logs according to these values in 3D space.

I have a script to evalute the diameters of the logs and end up with diameter A and B. But don`t know exactly how to proceed from there.
How do i setup the loop? Or do I work with gates or?
My approach would be:
Mass Addition of two lists of values (a1;a2;a3ā¦)(b1,b2,b3ā¦) exchange a1 with b1,a2 with b2ā¦ until I have the two sums to be similar(within 2-5percent range).
IĀ“m not an expert so I might be doing stuff completely wrong, sorry for that.
Help would be extremely appreciated

This is my script for finding the approximate diameters of the scanned treelogs (and later Orienting them with the help of bounding boxes). At the end I have two "fictional " diameter lists to work with.
OrientTreelog_withDiameter_2.gh (8.7 MB)

basically the problem you need to solve is:

you have 2 lists of values. Their total sum is X, so target sum per list is X/2
Then keep swapping values until each sum is as close to X/2 as possible.
You could start with swapping one value that gets you closest to your end goal and then repeat the process. Probably best to script this. Attached a .gh of the first iteration

swap_value.gh (30.4 KB)

hereās one that keeps swapping until a minimum has been found

swap_values_python.gh (23.3 KB)

1 Like

@lukaskirschnick, your problem intrigued me. So I made a solution that should be able to handle what ever you throw at it.

I donāt have a ton of time to do a proper right up right now, so Iāll come back to do it later. But here is a brief description of what iām doing.

Iām using a random number generator to create a list of trees to swap. Then I swap them, and add up the sides and compare the values. I can randomly change the list of tress to swap by changing the seed.

Using Galapagos, I can test hundreds of possible seeds to either maximize or minimize the height difference. Using the annealing method we can brute force an answer that works. Since this is all simple math, you can run through thousands of variations a minute.

OrientTreelog_withDiameter_2.gh (8.7 MB)

1 Like

Thanks a lot for your solution that helped me significantly. The 5 numbers on the out are the values that have been swapped right?

Correct, the logic is to stop the swapping process when in the next iteration the same index is swapped. I tried a few combinations and it seems to converge quickly all the time.

ah okay so the last two indexes are always the same and I can exclude those.

Thanks a lot, I had thought about trying galapagos too but IĀ“m not really familiar with it. Its always so helpful to read and analyze others peoples code to learn from it

2 Likes

One last question, I have a similar problem now with the same two lists but instead of wanting two equal sums I want to be able to set two target sums and swap the values around until they are as close as possible to these target sums. Is that possible or way too complicated?

Do the two target sums add up to the total of both lists combined? And the point is a sloped wall?

Hereās a solution that has two sliders (blue groups):

• a āSeedā value for Random Reduce
• an āIndexā value for the sorted difference between the two lists

Treelog_2020Mar1a.gh (41.3 KB)

The initial lists are Entwined and paired (as ālogsā) using Flip Matrix, then grouped (as points). Reduce and Set Difference are used to randomly swap between 1 and āallā logs. Etc., etc.

Among the random swaps, you choose the sorted difference you want and that pair of lists is the result, āList Aā and āList Bā.

Nothing is special about it, my actual data comes from a way bigger file where I analyze 3D scanned treelog meshes and this is just to simplify my question.

Thank you Joseph if I understand correctly I have no control over the final result but I can play around with the index and seed slider. So I could maybe use Galapagos and these two values would be changed until they come close to my desired two sums of the MassAddition Result.

I would say that the two sliders give you complete control over the final result.

Itās true that the method could be extended to evaluate all possible combinations of the two sliders to find an optimal result but on the second slider, you would have to avoid exceeding the number of logs (as indicated by wrap = false). But it doesnāt take long at all to manually find a very small difference between the two lists, where both ends are nearly the same height.

I tried using galapagos and I easily got the maximum and minimum results after a very short time. Unfortunately I have no idea how to get specific mass addition results of for example 530 (or any other number) within 5percent tolerance.
But thats fine you helped me get an idea on how else to approach it.

Nevermind if I manually enter the desired number that works with galapagos

The white group has a ādiff_goalā slider that compares all the differences and finds the closest one, shown as āvarianceā. The Sort was a human convenience, not needed anymore and could be removed. The next step would be to add an Anemone loop to try all values of āSeedā to find the lowest variance.

Treelog_2020Mar2a.gh (42.1 KB)

The cyan group is an Anemone loop to test āSeedā values from 0 to 499. You could make the limit larger if you like but it gets way below 5 percent tolerance as is.

I had to use the āClassicā Anemone components because āFastā didnāt work, Iām not sure why? Perhaps because of text panels that report results along the way? With the āClassicā components, you can see the progress as it finds smaller and smaller āvarianceā values. Change the ādiff_goalā slider and the loop will restart.

Treelog_2020Mar2b.gh (43.0 KB)

What I didnāt do this time is to use the resulting āSeedā value to show the two lists that give the minimum varianceā¦ That would be a lot of code duplication, probably best done by clustering it. Then use it within the loop and again after the loop ends. Another twenty minutes or so would do it.

OK, did that. Here is the cluster that is used twice, once within the loop and once more after to show the resulting lists:

Treelog_2020Mar2c.gh (49.6 KB)

I havenāt taken the time to test it extensively or optimize it further, such as avoiding the unneeded Sort and trying again to use āFastā instead of the slower āClassicā Anemone components, which might work better with no text output within the loop this time.

P.S. The āClassicā Anemone components take ~60 seconds as posted but can be sped up to ~38 seconds by right-clicking Loop End and enabling āOutput after the lastā. The āFastā components (which always āOutput after the lastā) take ~19 seconds.

P.P.S. Here is the āFastā Anemone loop version, with a Data Dam added after ādiff_goalā, though itās still probably best to double-click and type a value instead of adjusting the slider, because āFastā is less forgiving about being interrupted.
Treelog_2020Mar2d.gh (52.6 KB)

Thanks Joesph that`s great, I really love all the work you put in there. There is a lot of grasshopper functions that I never used so I have to take my time and analyze what exactly is going on everywhere. But IĀ“m learning a lot