Mathematica 中的整数优化?

发布于 2024-10-26 02:13:02 字数 281 浏览 1 评论 0原文

我有一个整数规划问题。我有一根10m长的管子。我想切出尽可能多的 1.2 米的片段,然后将管道的其余部分切成 100 毫米的片段。我必须留100毫米给机器抓取。我如何在mathematica中优化它?我想我可以把它作为一个等式来解决,但如果我只是想直接得到答案。 基本上,先有尽可能多的 y,然后是 x:es。

Maximize[{x*100+y*1200, x*100+y*1200<9900},{x,y},Integers] 只是给我一个不等式图。 是的,我已经检查过 Wolfram 的说明。

I have an integer programming problem. I have a pipe, 10m long. I want to cut out as many 1.2meter pieces as I can and then cut the rest of the pipe in 100mm pieces. I have to leave 100mm for the machine to grab. How do I optimize this in mathematica? I can solve it as an equality i guess but if i just want the answer straight out.
Basically, as many y's as possible, then x:es.

Maximize[{x*100+y*1200, x*100+y*1200<9900},{x,y},Integers] just gives me an inequality plot.
And yes, I have checked instructions at wolfram.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼眸 2024-11-02 02:13:02

由于 9900 和 1200 都是 100 的倍数,因此算法就是

TotLen = 9900;
numberOf1200pieces = IntegerPart[TotLen/1200];
numberOf100pieces  = IntegerPart[(TotLen - 1200 numberOf1200pieces)/100];

Print["Number of 1200mm pieces: ", numberOf1200pieces];
Print["Number of 100mm pieces: ", numberOf100pieces];
Print["Leftover: ", 9900 - numberOf1200pieces 1200 - numberOf100pieces 100,"mm"];

Number of 1200mm pieces: 8
Number of 100mm pieces: 3
Leftover: 0mm

您也可以尝试:

Maximize[{x*100 + y*1200, x*100 + y*1200 == 9900}, {x, y}, Integers]
->{9900, {x -> 3, y -> 8}}

As 9900 and 1200 are both multiples of 100, the algorithm is just

TotLen = 9900;
numberOf1200pieces = IntegerPart[TotLen/1200];
numberOf100pieces  = IntegerPart[(TotLen - 1200 numberOf1200pieces)/100];

Print["Number of 1200mm pieces: ", numberOf1200pieces];
Print["Number of 100mm pieces: ", numberOf100pieces];
Print["Leftover: ", 9900 - numberOf1200pieces 1200 - numberOf100pieces 100,"mm"];

Number of 1200mm pieces: 8
Number of 100mm pieces: 3
Leftover: 0mm

You may also try:

Maximize[{x*100 + y*1200, x*100 + y*1200 == 9900}, {x, y}, Integers]
->{9900, {x -> 3, y -> 8}}
合约呢 2024-11-02 02:13:02

使用假设 x,y 等为 >0 等,您最终将能够使用 //N 获得一个值

Mathematica 不会假设您在现实世界中!

Use assume on x,y etc to be >0 etc and you will finally be able to get a value with //N

Mathematica doesnt assume you are in the Real world!

不语却知心 2024-11-02 02:13:02

贝利撒留的解决方案是通过Maximize实现的最简单方法。然而,它需要将不平等改为平等,这并不反映你的意图。相反,使用 v.7,我会添加第二个条件

Maximize[{x*100 + y*1200, x*100 + y*1200 <= 9900, y > x > 0}, 
         {x, y}, Integers] -> {9900, {y -> 8, x -> 3}} 

新条件 (y > x > 0) 反映了您的意图,即首先选择较大的部分更好。另请注意,我将不等式 (<) 更改为 <=,因为您的除法正好为 9900。

Belisarius's solution is the simplest method via Maximize. However, it requires changing the inequality to an equality, which does not reflect your intent. Instead, using v. 7, I'd add a second condition

Maximize[{x*100 + y*1200, x*100 + y*1200 <= 9900, y > x > 0}, 
         {x, y}, Integers] -> {9900, {y -> 8, x -> 3}} 

The new condition (y > x > 0) reflects your intent that the larger pieces be chosen first better. Also, note that I changed the inequality (<) to <= as your divisions come out to 9900 exactly.

鼻尖触碰 2024-11-02 02:13:02

为了从表面上回答这个简单的问题,而不是将其推断为优化的玩具示例,这里有一种“统一”数字的方法。

Floor[ FoldList[Mod, #, Most@#2] / #2 ] &[ 9900, {1200, 100} ]

贝利撒留暗示我的回答太天真了,我认为这可能是一种有效的方法,尽管对于更复杂的情况来说效率较低。考虑将 9950 分成长度 12、75 和 1200。

i = 9950;

While[x = Quiet@IntegerPartitions[i--, All, {12, 75, 1200}, 1]; x === {}]

x[[1]] // Tally

To answer the simple question at face value, rather than inferring it to be a toy example of optimization, here is one method of "unitizing" a number.

Floor[ FoldList[Mod, #, Most@#2] / #2 ] &[ 9900, {1200, 100} ]

Responding to belisarius' implication that my reply was too naive, I think this may be a valid, albeit inefficient method for more complicated cases. Consider splitting 9950 into lengths 12, 75, and 1200.

i = 9950;

While[x = Quiet@IntegerPartitions[i--, All, {12, 75, 1200}, 1]; x === {}]

x[[1]] // Tally
王权女流氓 2024-11-02 02:13:02
decideOrderOfProduction[itemsToProduce_] := 
 Map[#[[1]] &, Sort[itemsToProduce, #1[[2]] > #2[[2]] &]]
minimizeWaste[pipe1_, pipe2_] := {
  Maximize[{pipe1*x + pipe2*y, pipe1*x + pipe2*y <= 4900, 
    y >= x >= 0}, {x, y}, Integers]
  }
minimizeWaste[pipe_] := {
  Maximize[{pipe*x, pipe*x <= 4900, x >= 0}, x, Integers]
  }
planProduction[itemsToProduce_] := {
  productionOrder = decideOrderOfProduction[itemsToProduce];
  strategy = {};
  While[Length[productionOrder] >= 2,
   pipe1 = productionOrder[[1]];
   pipe2 = productionOrder[[2]];
   strategy = 
    Append[strategy, {pipe1, pipe2, minimizeWaste[pipe1, pipe2]}];
   productionOrder = Drop[productionOrder, 2];];
  If[Length[productionOrder] == 1,
   strategy = 
    Append[strategy, {productionOrder[[1]], Null, 
      minimizeWaste[productionOrder[[1]]]}]];
  strategy
  }


items = {{99, 1}, {200, 12}, {1200, 2}, {90, 5}, {70, 1200}};
decideOrderOfProduction[items]
planProduction[items]
{70, 200, 90, 1200, 99}

{{{70, 200, {{4900, {x -> 10, y -> 21}}}}, {90, 
   1200, {{4890, {x -> 1, y -> 4}}}}, {99, 
   Null, {{4851, {x -> 49}}}}}}

这是一个开始,但很糟糕,因为不知何故,我需要更好地考虑优先级,并且我需要完成每个管道的数量。不知何故,我认为金额也需要优先考虑。

decideOrderOfProduction[itemsToProduce_] := 
 Map[#[[1]] &, Sort[itemsToProduce, #1[[2]] > #2[[2]] &]]
minimizeWaste[pipe1_, pipe2_] := {
  Maximize[{pipe1*x + pipe2*y, pipe1*x + pipe2*y <= 4900, 
    y >= x >= 0}, {x, y}, Integers]
  }
minimizeWaste[pipe_] := {
  Maximize[{pipe*x, pipe*x <= 4900, x >= 0}, x, Integers]
  }
planProduction[itemsToProduce_] := {
  productionOrder = decideOrderOfProduction[itemsToProduce];
  strategy = {};
  While[Length[productionOrder] >= 2,
   pipe1 = productionOrder[[1]];
   pipe2 = productionOrder[[2]];
   strategy = 
    Append[strategy, {pipe1, pipe2, minimizeWaste[pipe1, pipe2]}];
   productionOrder = Drop[productionOrder, 2];];
  If[Length[productionOrder] == 1,
   strategy = 
    Append[strategy, {productionOrder[[1]], Null, 
      minimizeWaste[productionOrder[[1]]]}]];
  strategy
  }


items = {{99, 1}, {200, 12}, {1200, 2}, {90, 5}, {70, 1200}};
decideOrderOfProduction[items]
planProduction[items]
{70, 200, 90, 1200, 99}

{{{70, 200, {{4900, {x -> 10, y -> 21}}}}, {90, 
   1200, {{4890, {x -> 1, y -> 4}}}}, {99, 
   Null, {{4851, {x -> 49}}}}}}

Thats a start but it is bad because somehow I need to take into account priority better and I need to finish the amount of each pipe. Somehow I guess amount needs to be taken into account into priority as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文