Split in groups by by sum of weights

   2297   4   0
User Avatar
Member
79 posts
Joined: Feb. 2008
Offline
I'm sure there must be an easy way to pull this off. Maybe my brain is just overworked and I can't seem to wrap my head around this one right now, but I'm guessing someone will have a great suggestion on how to accomplish this:

I have a list of primitives that has a ‘weight’ attribute per prim. I want to split them up into x number of groups, where the sum of weight in each groups are as close as possible to the same value. So let's say I have 100 prims and the sum of all their weights are 1000 units and I want to split them into 20 groups/boxes, then I want the sum of each weight attribute from each group/box to be as close as possible to 50 units. When I say groups/box, all I really need is a new attribute that has an integer value giving me the groups/box it belongs to.

What do you guys think? How should I get these prims sorted in Houdini? Any suggestions?
Thanks!
User Avatar
Member
79 posts
Joined: Feb. 2008
Offline
I thought I could use UV layout and create a grid per prim that has uv.y = 1 / nb groups and ux.x = nb_groups * weight / total weight… then however UV layout places the pieces, the uv.y / nb_groups = the group id as long as there are no rotations in the UV island which I choose in the Island Rotation Step, but uv layout still does occasionally rotate a tile 90 degrees anyway, so that ain't giving me the results I'm after.

Still looking for better approaches…
User Avatar
Member
718 posts
Joined: Sept. 2013
Offline
Hi Mathieu,

I don't think the overall weight sum is important unless you want to make the number of divisions dependent on this. The UV layout node accepts attributes for scaling and equally distributes your primitives over islands from another grid.
Edited by Konstantin Magnus - June 3, 2020 18:32:24

Attachments:
pack_values.hipnc (134.3 KB)

https://procegen.konstantinmagnus.de/ [procegen.konstantinmagnus.de]
User Avatar
Member
79 posts
Joined: Feb. 2008
Offline
I ended up writing a script that loops through the prims by decreasing value of weights. For each prim with the biggest weight, I find the group that has the less cumulative weights and add it to it. This works really well.
User Avatar
Member
504 posts
Joined: July 2005
Offline
Hi,

here is a heuristic way, which seems to work in some situations.

The input is a poly object with prim weights (> 0).

The first step is to create dummy (container) points, where each point represents one group.

Each points should have equal number of prims/weights, so probably we can start with any selection.

Now the weightsum should be different for each group.

If we take both groups, where the sums are min/max, we can try to find the weight index pair, which will

put both sums as closest to average as possible (if we remove the weight from each group and put it into the other).

Doing this multiple times should immprove the result, but this will probably not work in every situation.

If interested here is an example.
Edited by Aizatulin - June 6, 2020 15:36:37

Attachments:
Equal_Partitions_.hipnc (111.3 KB)
Equal_PartitionsA.hipnc (121.9 KB)

  • Quick Links