x = {x1, x2, x3, x4, x5};
Maximize[{x.lista, x.lista <= 3000, ## & @@ Thread[x >= 0]}, x, Integers]
{3000, {x1 -> 0, x2 -> 0, x3 -> 5, x4 -> 4, x5 -> 0}}
If you have to use at least one of each piece:
Maximize[{x.lista, x.lista <= 3000, ## & @@ Thread[x >= 1]}, x, Integers]
{3000, {x1 -> 2, x2 -> 1, x3 -> 4, x4 -> 1, x5 -> 1}}
All solutions that use all of 3000mm:
FrobeniusSolve[lista, 3000]
{{0, 0, 0, 3, 3}, {0, 0, 5, 4, 0}, {0, 1, 2, 6, 0}, {0, 4, 3, 1,
1}, {0, 5, 0, 3, 1}, {1, 0, 1, 7, 0}, {1, 2, 5, 0, 1}, {1, 3, 2, 2,
1}, {2, 1, 4, 1, 1}, {2, 2, 1, 3, 1}, {3, 0, 3, 2, 1}, {3, 1, 0, 4,
1}, {4, 3, 0, 0, 2}, {4, 8, 0, 0, 0}, {6, 0, 1, 0, 2}, {6, 5, 1, 0,
0}, {7, 4, 0, 1, 0}, {8, 2, 2, 0, 0}, {9, 1, 1, 1, 0}, {10, 0, 0, 2, 0}}
Or use IntegerPartitions
as follows:
Map[Lookup[Counts[#], lista, 0] &, IntegerPartitions[3000, All, lista]]
{{0, 0, 0, 3, 3}, {6, 0, 1, 0, 2}, {4, 3, 0, 0, 2}, {3, 1, 0, 4,
1}, {2, 2, 1, 3, 1}, {0, 5, 0, 3, 1}, {3, 0, 3, 2, 1}, {1, 3, 2, 2,
1}, {2, 1, 4, 1, 1}, {0, 4, 3, 1, 1}, {1, 2, 5, 0, 1}, {1, 0, 1, 7,
0}, {0, 1, 2, 6, 0}, {0, 0, 5, 4, 0}, {10, 0, 0, 2, 0}, {9, 1, 1, 1,
0}, {7, 4, 0, 1, 0}, {8, 2, 2, 0, 0}, {6, 5, 1, 0, 0}, {4, 8, 0, 0, 0}}
You can also use Solve
and Reduce
:
x /. Solve[{x.lista == 3000, ## & @@ Thread[x >= 0]}, x, Integers]
Reduce[{x.lista == 3000, ## & @@ Thread[x >= 0]}, x, Integers][[All, All, -1]] /.
Or | And -> List
{{0, 0, 0, 3, 3}, {0, 0, 5, 4, 0}, {0, 1, 2, 6, 0}, {0, 4, 3, 1,
1}, {0, 5, 0, 3, 1}, {1, 0, 1, 7, 0}, {1, 2, 5, 0, 1}, {1, 3, 2, 2,
1}, {2, 1, 4, 1, 1}, {2, 2, 1, 3, 1}, {3, 0, 3, 2, 1}, {3, 1, 0, 4,
1}, {4, 3, 0, 0, 2}, {4, 8, 0, 0, 0}, {6, 0, 1, 0, 2}, {6, 5, 1, 0,
0}, {7, 4, 0, 1, 0}, {8, 2, 2, 0, 0}, {9, 1, 1, 1, 0}, {10, 0, 0, 2, 0}}
KnapsackSolve[{230, 260, 320, 350, 650}, 3000]
gives me{13, 0, 0, 0, 0}
, which only has total cost 2990. I realize large knapsack problems might be difficult, but surely the algorithm should find one of the maximal answers for this small example... any idea if I'm doing something wrong? $\endgroup$KnapsackSolve[{{230, 230}, {260, 260}, {320, 320}, {350, 350}, {650, 650}}, 3000]
), you get the solution{10, 0, 0, 2, 0}
, which has total cost (and payoff) 3000. $\endgroup$