Grasshopper

algorithmic modeling for Rhino

Hello guys

I would like to aks your help, I'd like to make a grasshopper program which helps to solve my problem, i don't know python/C#/VB coding that's why I ask your help.
The problem is the following (It's a bit similar to the Knapsack problem):

I have a Variable --> X
and
I Have fix numbers (can we call "pieces") 9,12,15,18

I'd like to reach the X, with the summing of these numbers and using the minimum pieces ,it can't be lower than X, but it can be higher, maximum with 3.
After this it has to found the most optimal combination which mostly use the same pieces


E.G.

X=98

The wrong solution is like = 1pcs of 18
                                      = 9pcs of 9

Sum of pieces are 10

OR

                                      = 3pcs of 18
                                      = 1pcs of 15
                                      = 1pcs of 12
                                      = 2pcs of 9

Sum of pieces are 7

The right solution in this case = 5pcs of 18
                                           = 1pcs of 9


(5*18)+(1*9)=99 it's good beacuse it's over with maximum 3 and uses the minimum pieces


Then it sends to a list like
18 : 5pcs
15 : 0pcs
12 : 0pcs
9 : 1pcs
Can somebody help me ? Or is it possible to make this ?

Thank you

Views: 2698

Attachments:

Replies to This Discussion

One method that springs to mind would be combinatorial generation. That is, for a list of values, generate all combinations/permutations of these. There's a thread on various options in GH here (I'd personally recommend the Python itertools one). So in your case, for instance: generate all combinations (using the second argument to generate only sets of three), calculate the sum of all the generated sets, and then find the sum closest to X.

Here's an quick go (didn't test, so might be buggy, looks okay though):

Edit: You might expand this to start the search with the SetSize at 1, check if this generates a result that is within some tolerance distance from X, if not, jump to 2, if not, jump to 3 etc. A less exhaustive/brute force method might be to implement Galapagos for the search, but with smaller sets I suspect that won't be an advantage (also, it comes with its own can of worms). That said, the search space and amount of possible combinations grows rapidly with these cases.

Attachments:

I used your python script and  I'm getting closer to the solution,

Here is where i'm standing now.

I set 79 as goal, my result is 81 which is good, but not the best optimalization,

Becuase it gives me  3pcs of 12   and   5 pcs of 9

the good solution would be  4pcs of 18 and 1pcs of 9

Attachments:

In that case, you might have more succes using the function combinations_with_replacement():

Edit: Also note that the script is written assuming floats, you might need to edit it for integer logics (i.e. floor/ceiling type pitfalls etc.).

i don't understand pyhton unfortunately, where can i edit floats to integer? 

In the GHPython component script editor (double-click one and it opens up, like in the screenshots).

Hello
I am wondering if its possible to ask a question here
Is their something in script
That may add another input to "mass addition"
1- the value
2- the count that mass addition operates?
Isnt it possible ?
I mean something close to Log n (x)

Thank you

There's a Logarithm component. I don't think I understand the question though, perhaps better if you create a new topic.

exactly my question isn't clear enough,

i will formulate differently.

Thank you

i am sorry to stay on for this

i tried the expression editor

Log(x[,b])

i have the 1519 invalid expression error message

Attachments:

Hey, I ran into the same error and found a solution.

So for anyone who still has this issue:

Use: Log(x,(CDbl(y))) instead of Log(x[,y])

for "x,y" input

(works also for other formats than sliders as inputs;)

Best Regards

Tim

Attachments:

A bit lost as well, there are a couple of logarithmic functions in the Python math module as well (if that was the question).

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service