Finding all possible combinations of objects

I have 4 different obejcts with different weights each. I know the individual weights of the objects, the empty weight of the box and the total weight. Yet I do not know the number of objects. How can I find all the possible combinations of objects inside?
For example
Object1: 3 grams, Object2: 5 grams, Object3: 2 grams, Object4: 7 grams,
Empty weight of the box: 600 grams
What function may I use to find all the possible combination of objects within the box given the total weight is 850 grams?

回答 (3 件)

Steven Lord
Steven Lord 2022 年 2 月 27 日

0 投票

Implement one or more of the algorithms given on the Wikipedia page abour the knapsack problem.

1 件のコメント

Torsten
Torsten 2022 年 2 月 27 日
Usually, this gives one solution, not all.

サインインしてコメントする。

Matt J
Matt J 2022 年 2 月 27 日
編集済み: Matt J 2022 年 2 月 27 日

0 投票

You can use the attached file,
w=850-600;
A=[3,5,2,7];
Nmax=ceil(w/min(A));
combinations=diophantine(A,w,0:Nmax)
combinations = 11174×4
77 2 1 1 72 5 1 1 67 8 1 1 62 11 1 1 57 14 1 1 52 17 1 1 47 20 1 1 42 23 1 1 37 26 1 1 32 29 1 1

2 件のコメント

Torsten
Torsten 2022 年 2 月 27 日
combinations=diophantine(A,w,0:Nmax)
instead of
combinations=diophantine(A,w,1:Nmax)
Matt J
Matt J 2022 年 2 月 27 日
Right!

サインインしてコメントする。

Torsten
Torsten 2022 年 2 月 27 日

0 投票

Brute force:
n1 = 83;
n2 = 50;
n3 = 125;
n4 = 35;
nsol = 0;
for i = 0:n1
for j = 0:n2
for k = 0:n3
for l = 0:n4
if 3*i + 5*j + 2*k + 7*l == 250
nsol = nsol+1;
sol(nsol,1) = i;
sol(nsol,2) = j;
sol(nsol,3) = k;
sol(nsol,4) = l;
end
end
end
end
end

質問済み:

2022 年 2 月 27 日

編集済み:

2022 年 5 月 7 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by