Mathematica 的 Minimize 函数

发布于 2024-11-02 10:20:14 字数 727 浏览 1 评论 0原文

Mathematica 的 Minimize 函数是否不允许像 Mod[x,2]==0 这样的约束?我正在尝试解决 MinuteMath 与 Mathematica 的谜题:

四个不同正偶数的最小可能平均值是多少?

我的“解决方案”如下所示:

vars = Subscript[x, #] & /@ Range[4];
cond = Apply[And, Mod[#, 2] == 0 & /@ vars] && 
   (0 < Subscript[x, 1]) &&
   Apply[And, Table[Subscript[x, i] < Subscript[x, i + 1], {i, 1, 3}]];
Minimize[{Mean[vars], cond}, vars, Integers] 

但是 Minimize 返回未计算的值。附加问题:我可以使用 EvenQ 来定义约束吗?问题是,对于未定义的表达式 xEvenQ[x] 返回 False

Is it true that Mathematica's Minimize function does not allow constraints like Mod[x,2]==0? I am trying to solve a MinuteMath puzzle with Mathematica:

What is the smallest possible average of four distinct positive even integers?

My "solution" looks like this:

vars = Subscript[x, #] & /@ Range[4];
cond = Apply[And, Mod[#, 2] == 0 & /@ vars] && 
   (0 < Subscript[x, 1]) &&
   Apply[And, Table[Subscript[x, i] < Subscript[x, i + 1], {i, 1, 3}]];
Minimize[{Mean[vars], cond}, vars, Integers] 

but Minimize returns unevaluated. Additional question: Can I use EvenQ for defining the constraints? Problem is, EvenQ[x] returns False for undefined expressions x.

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

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

发布评论

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

评论(3

天煞孤星 2024-11-09 10:20:14

显然,这不需要 Mathematica,但是,在回答您的问题时,Minimize 似乎不喜欢这些模组。不过,您可以将其构建到论坛中,如下所示:

Minimize[{(2 x1 + 2 x2 + 2 x3 + 2 x4)/4,
  0 < x1 < x2 < x3 < x4}, {x1, x2, x3, x4}, Integers]

Clearly, this doens't require Mathematica but, in answer to your question, it seems that Minimize doesn't like the mods. You could build it into the forumula, though, like so:

Minimize[{(2 x1 + 2 x2 + 2 x3 + 2 x4)/4,
  0 < x1 < x2 < x3 < x4}, {x1, x2, x3, x4}, Integers]
你的心境我的脸 2024-11-09 10:20:14

对于这个问题来说显然是矫枉过正,但对于展示一些技巧很有用。

请注意:

 Exists[x, Element[x, Integers] && n x == y]

可以用作

  Mod[y,n] == 0

So:

Minimize[{(x1 + x2 + x3 + x4)/4, 0 < x1 < x2 < x3 < x4 && 
   Exists[x, Element[x, Integers] && 2 x == x1] &&
   Exists[x, Element[x, Integers] && 2 x == x2] &&
   Exists[x, Element[x, Integers] && 2 x == x3] &&
   Exists[x, Element[x, Integers] && 2 x == x4]
  },
 {x1, x2, x3, x4}, Integers]  

-> {5, {x1 -> 2, x2 -> 4, x3 -> 6, x4 -> 8}}  

的替代品,或者可能更优雅:

s = Array[x, 4];  
Minimize[{  
  Total@s,  
  Less @@ ({0} \[Union] s) &&  
   And @@ (Exists[y, Element[y, Integers] && 2 y == #] & /@ s)},
s, Integers]

--> {20, {x[1] -> 2, x[2] -> 4, x[3] -> 6, x[4] -> 8}}

A clear overkill for this problem, but useful to show some tricks.

Note that:

 Exists[x, Element[x, Integers] && n x == y]

can be used as an alternative to

  Mod[y,n] == 0

So:

Minimize[{(x1 + x2 + x3 + x4)/4, 0 < x1 < x2 < x3 < x4 && 
   Exists[x, Element[x, Integers] && 2 x == x1] &&
   Exists[x, Element[x, Integers] && 2 x == x2] &&
   Exists[x, Element[x, Integers] && 2 x == x3] &&
   Exists[x, Element[x, Integers] && 2 x == x4]
  },
 {x1, x2, x3, x4}, Integers]  

-> {5, {x1 -> 2, x2 -> 4, x3 -> 6, x4 -> 8}}  

Or perhaps more elegant:

s = Array[x, 4];  
Minimize[{  
  Total@s,  
  Less @@ ({0} \[Union] s) &&  
   And @@ (Exists[y, Element[y, Integers] && 2 y == #] & /@ s)},
s, Integers]

--> {20, {x[1] -> 2, x[2] -> 4, x[3] -> 6, x[4] -> 8}}
像极了他 2024-11-09 10:20:14

最小化对这个问题不好,它给出了错误的解决方案:

2 4 8 10

好的解决方案是:

2 4 6 12

“Select”accept Mod function 它比最小化
最小化给出了错误的解决方案。

播放该程序并看到:

Res=Select[Permutations[Range@20 {4}],Mod[ #[[1]],2]==0 &&Mod[ #[[2]],2]==0&&Mod[#[[3]],2]==0 &&Mod[#[[4]],2]==0&&Mod[(#[[1]]+#[[2]]+#[[3]]+#[[4]])/4,2]==0 &];Sort[Res,Less];Res[[1]]

Minimize is not good for this question, it gives a wrong solution:

2 4 8 10

The good solution is:

2 4 6 12

"Select" accept Mod function it's better than Minimize
Minimize gives the wrong solution.

Play this programe and see:

Res=Select[Permutations[Range@20 {4}],Mod[ #[[1]],2]==0 &&Mod[ #[[2]],2]==0&&Mod[#[[3]],2]==0 &&Mod[#[[4]],2]==0&&Mod[(#[[1]]+#[[2]]+#[[3]]+#[[4]])/4,2]==0 &];Sort[Res,Less];Res[[1]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文