Best use of steel bar cut for mechanical study
$begingroup$
I have available a steel bar with length of 3000 mm.
I intend to get the best use of cutting out of this bar.
I need to cut this steel bar in the following lengths: 230mm, 260mm, 320mm, 350mm and 650mm.
How to get cut options "close" to the length of 3000 mm.
Example:
230 + 230 + 260 + 260 + 320 + 320 + 350 + 350 + 650;
-> 2970
I have the code below. I know I will have to use the "Nearest" function, but I am not able to apply it:
lista={230, 260, 320, 350, 650};
resultados=Join@@DeleteCases[IntegerPartitions[#,{1,[Infinity]},lista]&/@Range[3000],{}];
And@@Thread[Total/@resultados<=3000];
contagem=Map[Lookup[Counts[#],lista,0]&,resultados];
contagem//Short
In the my example, i get groups that are smaller that 3000, but wish the values that are "nearest" of the value of 3000
list-manipulation
$endgroup$
add a comment |
$begingroup$
I have available a steel bar with length of 3000 mm.
I intend to get the best use of cutting out of this bar.
I need to cut this steel bar in the following lengths: 230mm, 260mm, 320mm, 350mm and 650mm.
How to get cut options "close" to the length of 3000 mm.
Example:
230 + 230 + 260 + 260 + 320 + 320 + 350 + 350 + 650;
-> 2970
I have the code below. I know I will have to use the "Nearest" function, but I am not able to apply it:
lista={230, 260, 320, 350, 650};
resultados=Join@@DeleteCases[IntegerPartitions[#,{1,[Infinity]},lista]&/@Range[3000],{}];
And@@Thread[Total/@resultados<=3000];
contagem=Map[Lookup[Counts[#],lista,0]&,resultados];
contagem//Short
In the my example, i get groups that are smaller that 3000, but wish the values that are "nearest" of the value of 3000
list-manipulation
$endgroup$
3
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
@C.E.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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56
add a comment |
$begingroup$
I have available a steel bar with length of 3000 mm.
I intend to get the best use of cutting out of this bar.
I need to cut this steel bar in the following lengths: 230mm, 260mm, 320mm, 350mm and 650mm.
How to get cut options "close" to the length of 3000 mm.
Example:
230 + 230 + 260 + 260 + 320 + 320 + 350 + 350 + 650;
-> 2970
I have the code below. I know I will have to use the "Nearest" function, but I am not able to apply it:
lista={230, 260, 320, 350, 650};
resultados=Join@@DeleteCases[IntegerPartitions[#,{1,[Infinity]},lista]&/@Range[3000],{}];
And@@Thread[Total/@resultados<=3000];
contagem=Map[Lookup[Counts[#],lista,0]&,resultados];
contagem//Short
In the my example, i get groups that are smaller that 3000, but wish the values that are "nearest" of the value of 3000
list-manipulation
$endgroup$
I have available a steel bar with length of 3000 mm.
I intend to get the best use of cutting out of this bar.
I need to cut this steel bar in the following lengths: 230mm, 260mm, 320mm, 350mm and 650mm.
How to get cut options "close" to the length of 3000 mm.
Example:
230 + 230 + 260 + 260 + 320 + 320 + 350 + 350 + 650;
-> 2970
I have the code below. I know I will have to use the "Nearest" function, but I am not able to apply it:
lista={230, 260, 320, 350, 650};
resultados=Join@@DeleteCases[IntegerPartitions[#,{1,[Infinity]},lista]&/@Range[3000],{}];
And@@Thread[Total/@resultados<=3000];
contagem=Map[Lookup[Counts[#],lista,0]&,resultados];
contagem//Short
In the my example, i get groups that are smaller that 3000, but wish the values that are "nearest" of the value of 3000
list-manipulation
list-manipulation
edited Jan 23 at 11:37
LCarvalho
asked Jan 23 at 11:29
LCarvalhoLCarvalho
5,85642986
5,85642986
3
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
@C.E.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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56
add a comment |
3
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
@C.E.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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56
3
3
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
@C.E.
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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@C.E.
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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
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}}
$endgroup$
add a comment |
$begingroup$
lst = {230, 260, 320, 350, 650};
vars = {c1, c2, c3, c4, c5};
eq = Inner[Times, lst, vars, Plus]
(sol = FindInstance[eq == 3000 && vars > 0, vars, Integers, 10]) // Column
{c1 -> 1, c2 -> 3, c3 -> 2, c4 -> 2, c5 -> 1}
{c1 -> 2, c2 -> 1, c3 -> 4, c4 -> 1, c5 -> 1}
{c1 -> 2, c2 -> 2, c3 -> 1, c4 -> 3, c5 -> 1}
eq /. sol
{3000, 3000, 3000}
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190066%2fbest-use-of-steel-bar-cut-for-mechanical-study%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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}}
$endgroup$
add a comment |
$begingroup$
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}}
$endgroup$
add a comment |
$begingroup$
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}}
$endgroup$
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}}
edited Jan 23 at 14:09
answered Jan 23 at 11:42
kglrkglr
188k10204422
188k10204422
add a comment |
add a comment |
$begingroup$
lst = {230, 260, 320, 350, 650};
vars = {c1, c2, c3, c4, c5};
eq = Inner[Times, lst, vars, Plus]
(sol = FindInstance[eq == 3000 && vars > 0, vars, Integers, 10]) // Column
{c1 -> 1, c2 -> 3, c3 -> 2, c4 -> 2, c5 -> 1}
{c1 -> 2, c2 -> 1, c3 -> 4, c4 -> 1, c5 -> 1}
{c1 -> 2, c2 -> 2, c3 -> 1, c4 -> 3, c5 -> 1}
eq /. sol
{3000, 3000, 3000}
$endgroup$
add a comment |
$begingroup$
lst = {230, 260, 320, 350, 650};
vars = {c1, c2, c3, c4, c5};
eq = Inner[Times, lst, vars, Plus]
(sol = FindInstance[eq == 3000 && vars > 0, vars, Integers, 10]) // Column
{c1 -> 1, c2 -> 3, c3 -> 2, c4 -> 2, c5 -> 1}
{c1 -> 2, c2 -> 1, c3 -> 4, c4 -> 1, c5 -> 1}
{c1 -> 2, c2 -> 2, c3 -> 1, c4 -> 3, c5 -> 1}
eq /. sol
{3000, 3000, 3000}
$endgroup$
add a comment |
$begingroup$
lst = {230, 260, 320, 350, 650};
vars = {c1, c2, c3, c4, c5};
eq = Inner[Times, lst, vars, Plus]
(sol = FindInstance[eq == 3000 && vars > 0, vars, Integers, 10]) // Column
{c1 -> 1, c2 -> 3, c3 -> 2, c4 -> 2, c5 -> 1}
{c1 -> 2, c2 -> 1, c3 -> 4, c4 -> 1, c5 -> 1}
{c1 -> 2, c2 -> 2, c3 -> 1, c4 -> 3, c5 -> 1}
eq /. sol
{3000, 3000, 3000}
$endgroup$
lst = {230, 260, 320, 350, 650};
vars = {c1, c2, c3, c4, c5};
eq = Inner[Times, lst, vars, Plus]
(sol = FindInstance[eq == 3000 && vars > 0, vars, Integers, 10]) // Column
{c1 -> 1, c2 -> 3, c3 -> 2, c4 -> 2, c5 -> 1}
{c1 -> 2, c2 -> 1, c3 -> 4, c4 -> 1, c5 -> 1}
{c1 -> 2, c2 -> 2, c3 -> 1, c4 -> 3, c5 -> 1}
eq /. sol
{3000, 3000, 3000}
answered Jan 23 at 14:01
rmwrmw
2897
2897
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190066%2fbest-use-of-steel-bar-cut-for-mechanical-study%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
$begingroup$
This is known as the knapsack problem and there is a built-in function solve it, KnapsackSolve.
$endgroup$
– C. E.
Jan 23 at 11:34
$begingroup$
Almost this. There are several combinations possible of cut with these lengths that added are "close" of 3000
$endgroup$
– LCarvalho
Jan 23 at 11:43
$begingroup$
@C.E.
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$
– MassDefect
Jan 23 at 16:46
$begingroup$
@MassDefect I don't think you're doing anything wrong. It would appear it isn't better than that.
$endgroup$
– C. E.
Jan 23 at 16:54
$begingroup$
@C.E. Ah, okay. I'm surprised by that. I thought for sure it would be good enough and that I was doing something silly. Thanks!
$endgroup$
– MassDefect
Jan 23 at 16:56