NArgMin
更多信息和选项
- NArgMin 亦称为全局优化 (GO).
- NArgMin 总是尝试求给定约束条件限制下 f 的全局最小值.
- NArgMin 通常用于求给定约束条件下可能的最小值. 在不同的领域,这可能被称为最佳策略、最佳方案、最佳配置等.
- NArgMin 返回形如 {xmin,ymin,…} 的列表.
- NArgMin[…,{x,y,…}] 实际上等价于 {x,y,…}/.Last[NMinimize[…,{x,y,…},…]].
- 如果 f 和 cons 是线性的或凸的,NArgMin 给出的结果将是实数和整数值上的全局最小值;否则,有时结果可能只是局部最小值.
- 如果 NArgMin 发现无法满足约束条件,则会返回 {Indeterminate,…}.
- NArgMin 支持一种建模语言,其中目标函数 f 和约束条件 cons 是用依赖于标量或向量变量的表达式给出的. f 和 cons 通常被解析为非常有效的形式,但只要 f 和 cons 中的项给出变量的数值结果,NArgMin 通常可以找到解.
- 约束条件 cons 可以是以下表达式的任意逻辑组合:
-
lhs==rhs 等式 lhs>rhs, lhs≥rhs, lhs<rhs, lhs≤rhs 不等式 (LessEqual、…) lhsrhs, lhsrhs, lhsrhs, lhsrhs 向量不等式 (VectorLessEqual、…) {x,y,…}∈rdom 区域或域的指定 - NArgMin[{f,cons},x∈rdom] 实际上等价于 NArgMin[{f,cons&&x∈rdom},x].
- 对于 x∈rdom,可用 Indexed[x,i] 来指代不同的坐标.
- 可能的域 rdom 包括:
-
Reals 实标量变量 Integers 整数标量变量 Vectors[n,dom]
中的向量变量Matrices[{m,n},dom]
中的矩阵变量ℛ 限制在几何区域
中的向量变量 - 默认情况下,假定所有变量为实数.
- 可给出下列选项:
-
AccuracyGoal Automatic 追求多少位的准确度 EvaluationMonitor None 计算 f 时要计算的表达式 MaxIterations Automatic 使用的最大迭代次数 Method Automatic 使用的方法 PrecisionGoal Automatic 追求多少位的精度 StepMonitor None 每次迭代时要计算的表达式 WorkingPrecision MachinePrecision 内部计算使用的精度 - AccuracyGoal 和 PrecisionGoal 的设置指定了搜索最小值所在位置(最小化点)和函数的最小值时应使用的位数.
- NArgMin 会持续运行,直到 AccuracyGoal 或 PrecisionGoal 指定的目标实现为止.
- NArgMin 的方法分为两类. 第一类方法利用问题的属性,因此当方法收敛时,找到的最小值保证是全局的. 第二类启发式方法使用的方法可能包括多个局部搜索,通常通过一些随机性进行调整以锁定全局最小值. 这些方法通常能找到全局最小值,但无法保证.
- 在收敛到解时确保可以给出全局最小值的方法包括:
-
"Convex" 只使用凸方法 "MOSEK" 对于凸问题,使用商用 MOSEK 库 "Gurobi" 对于凸问题,使用商用 Gurobi 库 "Xpress" 对于凸问题,使用商用 Xpress 库 - 启发式方法包括:
-
"NelderMead" Nelder 和 Mead 的单纯形法 "DifferentialEvolution" 使用差分进化 "SimulatedAnnealing" 使用模拟退火 "RandomSearch" 使用从多个随机起始点中找到的最佳局部最小值 "Couenne" 使用 Couenne 库处理非凸混合整数非线性问题
范例
打开所有单元 关闭所有单元基本范例 (4)
NArgMin[2x ^ 2 - 3x + 5, x]NArgMin[(x y - 3) ^ 2 + 1, {x, y}]NArgMin[{x - 2y, x ^ 2 + y ^ 2 ≤ 1}, {x, y}]NArgMin[x + y, {x, y}∈Disk[]]Show[ContourPlot[x + y, {x, y}∈Disk[]], Graphics[{Red, PointSize[Large], Point[%]}]]范围 (40)
基本用法 (12)
NArgMin[{x + 2y, x^2 + 2y^2 ≤ 3, x + y == 2, x ≥ 1}, {x, y}]可用 VectorGreaterEqual 表示几个线性不等式约束条件:
NArgMin[{x + y, VectorGreaterEqual[{{x + 2y, x}, {3, -1}}]}, {x, y}]用
v>=
或 \[VectorGreaterEqual] 输入向量不等式符号 :
NArgMin[{x + y, {x + 2y, x}{3, -1}}, {x, y}]NArgMin[{x + y, x + 2y ≥ 3, x ≥ -1}, {x, y}]NArgMin[{{1, 1}.v, {{1, 2}, {1, 0}}.v{3, -1}}, v]NArgMin[{{1, 1}.x, {{1, 2}, {1, 0}}.x + {-3, 1}0}, x]NArgMin[{{1, 1}.x, {{1, 2}, {1, 0}}.x{3, -1}}, x]如果想要避免
中不想要的逐项运算,可使用 Inactive[Plus]:
NArgMin[{{1, 1}.x, {{1, 2}, {1, 0}}.x + {-3, 1}0}, x]parEqs = {a == {{1, 2}, {1, 0}}, b == {3, -1}};NArgMin[{{1, 1}.x, {parEqs, a.x + b0}}, x]VectorGreaterEqual 表示关于 "NonNegativeCone" 的锥不等式:
constraint = VectorGreaterEqual[{{{1, 2}, {1, 0}}.v, {3, -1}}, "NonNegativeCone"]如果想明确指定锥体的尺寸,可使用 {"NonNegativeCone",n}:
constraint = VectorGreaterEqual[{{{1, 2}, {1, 0}}.v, {3, -1}}, {"NonNegativeCone", 2}]NArgMin[{{1, 1}.v, constraint}, v]NArgMin[{x + y, x^2 + y^2 ≤ 9}, {x, y}]constraint = VectorGreaterEqual[{{x, y, 3}, 0}, {"NormCone", 3}]NArgMin[{x + y, constraint}, {x, y}]{a, b} = {{{1, 1, 0}, {0, 1, 1}, {1, 1, 1}}, {2, 2, -1}};用 Indexed 访问向量变量的分量,如
:
NArgMin[{Total[x], a.xb, Indexed[x, 1] ≥ 1}, x]当向量变量不明确时,用 Vectors[n,dom] 指定维度和域:
NArgMin[{Indexed[x, {1}] + 2Indexed[x, {2}], (| | |
| --------------- | --------------- |
| Indexed[x, {1}] | 1 |
| 1 | Indexed[x, {2}] |)Underscript[, {"SemidefiniteCone", 2}]0}, x]NArgMin[{Indexed[x, {1}] + Indexed[2x, {2}], (| | |
| --------------- | --------------- |
| Indexed[x, {1}] | 1 |
| 1 | Indexed[x, {2}] |)Underscript[, {"SemidefiniteCone", 2}]0}, x∈Vectors[2, Reals]]用 NonNegativeReals (
) 指定非负约束条件:
NArgMin[{{1, 1}.x, {Indexed[x, {1}], Indexed[x, {2}], 1}Underscript[, {"NormCone", 3}]0}, x∈Vectors[2, NonNegativeReals]]NArgMin[{{1, 1}.x, {Indexed[x, {1}], Indexed[x, {2}], 1}Underscript[, {"NormCone", 3}]0, x0}, x]用 NonPositiveReals (
) 指定非正约束条件:
NArgMin[{-Total[v], {1, 2}.v-3}, v∈Vectors[2, NonPositiveReals]]NArgMin[{-Total[v], {1, 2}.v-3, v0}, v]可以指定 Or 约束条件:
NArgMin[{x + y, x ^ 2 + y ^ 2 ≤ 1 || (x + 2) ^ 2 + (y + 2) ^ 2 ≤ 1}, {x, y}]//Quiet域约束条件 (4)
用 Integers 指定整数域约束条件:
NArgMin[{x + y, x + 2y ≥ 3, x ≥ -2}, {x, y∈Integers}]用 Vectors[n,Integers] 指定向量变量的整数域约束条件:
NArgMin[{Total[x], a == {{1, 2}, {1, 0}}, b == {3, -2},
a.xb}, x∈Vectors[2, Integers]]用 NonNegativeIntegers (
) 指定非负整数域约束条件:
NArgMin[{x + 2y, x^2 + 2y^2 ≤ 3, x + y == 2, x∈NonNegativeIntegers}, {x, y}]用 NonPositiveIntegers (
) 指定非正整数域约束条件:
NArgMin[{-x + y, x + 2y ≥ 3, x∈NonPositiveIntegers}, {x, y}]区域约束条件 (5)
t = RotationTransform[{{0, 0, 1}, {1, 1, 1}}];
ℛ = TransformedRegion[Ellipsoid[{0, 0, 0}, {1, 2, 3}], t];NArgMin[z, {x, y, z}∈ℛ]Graphics3D[{{Opacity[0.5], Green, GeometricTransformation[Ellipsoid[{0, 0, 0}, {1, 2, 3}], t]}, {Red, PointSize[Large], Point[%]}}]Subscript[ℛ, 1] = Disk[];
Subscript[ℛ, 2] = InfiniteLine[{{-2, 0}, {0, 2}}];NArgMin[EuclideanDistance[{x, y}, {u, v}], {{x, y}∈Subscript[ℛ, 1], {u, v}∈Subscript[ℛ, 2]}]Graphics[{{LightBlue, Subscript[ℛ, 1]}, {Green, Subscript[ℛ, 2]}, {Red, Point[Partition[%, 2]]}}]Subscript[ℛ, 1] = Triangle[{{0, 0}, {1, 0}, {0, 1}}];
Subscript[ℛ, 2][r_] = Disk[{1, 1}, {2r, r}];NArgMin[{r, {x, y}∈Subscript[ℛ, 1] && {x, y}∈Subscript[ℛ, 2][r]}, {r, x, y}]Graphics[{{LightBlue, Subscript[ℛ, 1], Subscript[ℛ, 2][First[%]]}, {Red, Point[Rest[%]]}}]Subscript[ℛ, 3][{a_, b_, r_}] = Disk[{a, b}, r];NArgMin[{r, ({0, 0} | {1, 0} | {0, 1})∈Subscript[ℛ, 3][{a, b, r}]}, {a, b, r}]Graphics[{{LightBlue, Subscript[ℛ, 3][%]}, {Red, Point[{{0, 0}, {1, 0}, {0, 1}}]}}]用 Circumsphere 直接给出同样的结果:
Circumsphere[{{0, 0}, {1, 0}, {0, 1}}]//Nℛ = Sphere[];NArgMin[x.{1, 2, 3}, x∈ℛ]线性问题 (5)
NArgMin[{x + y, 3x + 2y ≥ 7 && x + 2y ≥ 6 && x ≥ 0 && y ≥ 0}, {x, y}]NArgMin[{x - y, x + y + z == 1 / 2, x - 2z == 1, 2x - y ≥ 1}, {x, y, z}]用 Equal 一次表示几个等式约束条件:
NArgMin[{x - y, {x + y, x - y} == {1 / 2, 1}}, {x, y}]NArgMin[{x - y, x + y == 1 / 2, x - y == 1}, {x, y}]用 VectorLessEqual 一次表示几个 LessEqual 不等式约束条件:
NArgMin[{x + y, VectorLessEqual[{{x - 2y, -x + y}, {3, 2}}]}, {x, y}]NArgMin[{x + y, {x - 2y, -x + y}{3, 2}}, {x, y}]NArgMin[{x + y, {x - 2y ≤ 3, -x + y ≤ 2}}, {x, y}]用 Interval 指定变量的范围:
NArgMin[{x + y, x + 2y ≥ 3}, {x∈Interval[{-1, 2}], y∈Interval[{-1, 1}]}]凸问题 (7)
用 "NonNegativeCone" 指定形如
的线性函数:
NArgMin[{Total[x],
VectorGreaterEqual[{{{-1, -1 / 2}, {1, -1 / 2}, {0, 1}}.x, {-1 / 2, -1 / 2, -1 / 2}}, {"NonNegativeCone", 3}]}, x]NArgMin[{Total[x], {{-1, -(1/2)}, {1, -(1/2)}, {0, 1}}.x-(1/2)}, x]NArgMin[{(x - 1)^2 + y^2, x + y / 2 ≤ 1 / 2, x - y ≥ 0}, {x, y}]ContourPlot[(x - 1)^2 + y^2, {x, -1, 1}, {y, -1, 1 / 2}, ...]res = NArgMin[{(x - 1)^2 + y^2, x^2 + (y^2/2) ≤ (1/2), 2x^2 ≤ 3y}, {x, y}]Show[Plot3D[(x - 1)^2 + y^2, {x, -1, 1}, {y, 0, 1}, ...], Graphics3D[{Red, PointSize[0.03], Point[{x, y, (x - 1)^2 + y^2} /. Thread[{x, y} -> res]]}]]Subscript[ℛ, 1] = Triangle[{{1, 1}, {2, 1}, {1, 2}}];
Subscript[ℛ, 2] = Disk[];NArgMin[EuclideanDistance[x, y], {x∈Subscript[ℛ, 1], y∈Subscript[ℛ, 2]}]Graphics[{{LightBlue, Subscript[ℛ, 1], Subscript[ℛ, 2]}, {Red, Point[%]}}]res = NArgMin[{10x + 11 y, (| | |
| - | - |
| x | 1 |
| 1 | y |)Underscript[, {"SemidefiniteCone", 2}]0}, {x, y}]Show[Plot3D[10x + 11y, {x, 0, 5}, {y, 0, 5}, ...], Graphics3D[{Red, PointSize[0.05], Point[{x, y, 10x + 11y} /. Thread[{x, y} -> res]]}]]res = NArgMin[{-Log[x + y], (| | |
| ----- | ----- |
| x + y | 1 |
| 1 | x - y |)Underscript[, {"SemidefiniteCone", 2}]0, 1 ≤ x ≤ 10, -1 ≤ y ≤ 1}, {x, y}]Show[Plot3D[-Log[x + y], {x, 1, 10}, {y, -1, 1}, ...], Graphics3D[{Red, PointSize[0.05], Point[{x, y, -Log[x + y]} /. Thread[{x, y} -> res]]}]]res = NArgMin[{(x - 2)^2 + (y - 1 / 2)^2, Inactive[Norm][{x, y}, 4] ≤ 1}, {x, y}]Show[Plot3D[(x - 2)^2 + (y - 1 / 2)^2, {x, -1, 1}, {y, -1, 1}, ...], Graphics3D[{PointSize[0.05], Point[{x, y, (x - 2)^2 + (y - (1/2))^2} /. Thread[{x, y} -> res]]}]]可转化为凸问题的问题 (4)
NArgMin[{2w + 2h, h w == 1, h ≤ (1/2)w, h ≥ 0, w ≥ 0}, {h, w}]这个问题是对数-凸 (log-convex) 的,可以通过做一个变换 {hExp[],wExp[ ]} 并取对数得到凸问题来解决:
NArgMin[{Log[Exp[𝓌 + Log[2]] + Exp[𝒽 + Log[2]]], 𝒽 + 𝓌 == 0, 𝒽 - 𝓌 ≤ Log[1 / 2]}, {𝒽, 𝓌}]Exp[%]求能最小化受不等式和范数约束条件限制的拟凸函数
的点. 目标函数是拟凸的,因为它是域上一个非负函数和一个非正函数的乘积:
NArgMin[{-x y, x ≥ 0, y ≥ 0, Norm[{x - 1, y - 2}] ≤ 1}, {x, y}]pfun = ParametricConvexOptimization[0, {-x y ≤ α, x ≥ 0, y ≥ 0,
Norm[{x - 1, y - 2}] ≤ 1}, {x, y}, α, "PrimalMinimizerRules"]fun[α_ ? NumericQ] := -x * y /. pfun[α];
Plot[fun[α], {α, -4.68, 0}, PlotRange -> All]pfun[-4.68]pfun[-4.69]根据约束条件
最小化
. 目标是不凸但可以表示为凸差函数
其中
和
为凸函数:
res = NArgMin[{x ^ 2 - y ^ 2, Norm[{x, y}] <= 2}, {x, y}]Show[Plot3D[x ^ 2 - y ^ 2, {x, -2, 2}, {y, -2, 2}, ...], Graphics3D[
{PointSize[0.04], Point[{x, y, x ^ 2 - y ^ 2} /. Thread[{x, y} -> res]]}]]根据约束条件
最大化
. 约束条件
为非凸但可用一个凸差约束条件
表示,其中
为凸函数:
res = NArgMin[{x ^ 2 + y, 1 <= Norm[{x, y}] <= 2}, {x, y}]Show[Plot3D[x ^ 2 + y, {x, -2, 2}, {y, -2, 2}, ...], Graphics3D[
{PointSize[0.04], Point[{x, y, x ^ 2 + y} /. Thread[{x, y} -> res]]}]]普通问题 (3)
res = NArgMin[{x + y, Sin[2x] + Cos[3y] ≤ 1, Norm[{x, y}] ≤ 2}, {x, y}]Show[Plot3D[x + y, {x, -2, 2}, {y, -2, 2}, ...], Graphics3D[{Blue, PointSize[0.03], Point[{x, y, x + y} /. Thread[{x, y} -> res]]}]]res = NArgMin[{Sin[2x] + Cos[x], -2 ≤ x ≤ 3}, x]Plot[Sin[2x] + Cos[x], {x, -2, 3}, Epilog -> {PointSize[0.03], Red, Point[{res, Sin[2x] + Cos[x] /. x -> res}]}]res = NArgMin[{Sin[x^2 + y], Sin[2x] + Cos[3y] ≤ 1, Norm[{x^2, y}] ≤ 2}, {x, y}]Show[Plot3D[Sin[x^2 + y], {x, -2, 2}, {y, -2, 2}, ...], Graphics3D[{Blue, PointSize[0.03], Point[{x, y, Sin[x^2 + y]} /. Thread[{x, y} -> res]]}]]选项 (7)
AccuracyGoal & PrecisionGoal (2)
NArgMin[Sin[Tan[x] / 2], x, AccuracyGoal -> 9, PrecisionGoal -> 8]NArgMin[Sin[Tan[x] / 2], x, AccuracyGoal -> 20, PrecisionGoal -> 18]设置一个较高的 WorkingPrecision ,使过程收敛:
NArgMin[Sin[Tan[x] / 2], x, AccuracyGoal -> 20, PrecisionGoal -> 18, WorkingPrecision -> 40]EvaluationMonitor (1)
f[x_, y_] := (x ^ 2 + y ^ 2 - 16) ^ 2;
{sol, pts} = Reap[
NArgMin[f[x, y], {{x, -5, 5}, {y, -5, 5}}, Method -> "DifferentialEvolution", EvaluationMonitor :> Sow[{x, y}]]];ContourPlot[f[x, y], {x, -5, 5}, {y, -5, 5}, ColorFunction -> (Hue[.6(1 - #1)]&),
Epilog -> Map[Point, Cases[First[pts], x_ /; Abs[f@@x - f@@sol] ≤ .05]], Contours -> 4Range[0, 10] ^ 2]Method (2)
objective[x_] := Sin[.4 x ^ 2] + .7Cos[.6 x] x;
NArgMin[{objective[x], -9 < x < 9}, {x}, Method -> #]& /@ {"NelderMead", "DifferentialEvolution", "SimulatedAnnealing"}minSol = NArgMin[{objective[x], -10 < x < 10}, {x}, Method -> "Couenne"]minSol = NArgMin[{objective[x], -10 < x < 10}, {x}, Method -> "Couenne"]Plot[objective[x], {x, -10, 10}, Rule[...]]当速度至关重要时,请使用方法 "NelderMead" 来求解有多个变量的问题:
n = 50;AbsoluteTiming[NArgMin[Sum[i * (x[i] - i) ^ 2 + Sin[x[i]], {i, 1, n}], Table[x[i], {i, 1, n}], Method -> "NelderMead"]]StepMonitor (1)
在求经典的 Rosenbrock 函数最小值的过程中 NArgMin 采取的步骤:
pts = Reap[NArgMin[(1 - x) ^ 2 + 100(-x ^ 2 - y) ^ 2, {x, y}, Method -> "NelderMead", StepMonitor :> Sow[{x, y}]]][[2, 1]];
pts = Join[{{-1.2, 1}}, pts];ContourPlot[(1 - x) ^ 2 + 100(-x ^ 2 - y) ^ 2, {x, -1.3, 1.5}, {y, -1.5, 1.4}, Epilog -> {Arrow[pts], Point[pts]}, Contours -> Table[10 ^ -i, {i, -2, 10}], ColorFunction -> (Hue[(Log[10, #] + 10) / 12]&), ColorFunctionScaling -> False]WorkingPrecision (1)
工作精度设为
时,AccuracyGoal 和 PrecisionGoal 默认值为
:
NArgMin[Cos[x ^ 2 - 3 y] + Sin[x ^ 2 + y ^ 2], {x, y}, WorkingPrecision -> 20]应用 (14)
几何问题 (4)
求半径为 1、圆心为
和
的两个圆盘上距离最近的两个点. 令
为圆盘 1 上的一个点. 令
为圆盘 2 上的一个点. 目标是最小化受约束条件
限制的
:
res = NArgMin[{Norm[p1 - p2], c2 == {2, 1}, Norm[p1] ≤ 1, Norm[p2 - c2] ≤ 1}, {p1∈Vectors[2, Reals], p2∈Vectors[2, Reals]}]Graphics[{{Lighter[Blue], Disk[]}, {Lighter[Red], Disk[{2, 1}]}, {Green, PointSize[0.03], Point[res]}}]region = [image];constraints = Table[Norm[Inactive[Plus][pi, -c]] ≤ r, {pi, MeshCoordinates[region]}];res = NArgMin[{r, constraints}, {r, c∈Vectors[3, Reals]}]Show[Graphics3D[{Opacity[0.2], Green, Ball[res[[2]], res[[1]]]}], region]可通过 BoundingRegion 高效地找到最小包含球:
BoundingRegion[region, "MinBall"]求使得参数化为
且包围三维中的一组点的椭圆体的体积最小化的
和
:
pts = IconizedObject[«With»];constraints = Table[Norm[a.pi + b] ≤ 1, {pi, pts}];res = NArgMin[{-Log[Det[a]], constraints, aUnderscript[, {"SemidefiniteCone", 3}]0}, {a, b∈Vectors[3]}]{Σ, x0} = {Inverse[Transpose[res[[1]]].res[[1]]], -LinearSolve[res[[1]], res[[2]]]};
minVolEllipsoid = Ellipsoid[x0, Σ]Graphics3D[{{Opacity[0.3], Red, Ellipsoid[x0, Σ]}, {PointSize[0.025], Point[pts]}}]也可以使用 BoundingRegion 得到包围椭圆体(体积不一定是最小的):
fastEllipsoid = BoundingRegion[pts, "FastEllipsoid"]{Volume[minVolEllipsoid], Volume[fastEllipsoid]}求可包含
个给定半径为
的不重叠圆的最小正方形,其中
. 指定圆的数量和每个圆的半径:
n = 3;
r = BlockRandom[RandomReal[{0.5, 1.5}, n], RandomSeeding -> 123];若
为圆
的中心,则目标为最小化
. 可将目标转换为最小化
和
:
objectiveConstraint = Table[-s <= Norm[c[i], Infinity] + r[[i]] <= s, {i, n}];nonOverlapConstraint = Table[Norm[c[i] - c[j]] >= r[[i]] + r[[j]],
{i, 1, n}, {j, i + 1, n}];vars = Append[Table[Element[c[i], Vectors[2, Reals]], {i, n}], Element[s, Reals]];res = NArgMin[{s, objectiveConstraint, nonOverlapConstraint}, vars, Method -> "DifferenceOfConvex"]Graphics[{Table[Circle[res[[i]], r[[i]]], {i, n}], {FaceForm[None], EdgeForm[StandardGray], Rectangle[{-res[[-1]], -res[[-1]]}, {res[[-1]], res[[-1]]}]}}]Sum[Area[Disk[res[[i]], r[[i]]]], {i, n}] / Area[Rectangle[{-res[[-1]], -res[[-1]]}, {res[[-1]], res[[-1]]}]]数据拟合问题 (3)
对于给定矩阵 a 和向量 b,求可最小化受约束条件
限制的
的点:
{a, b} = IconizedObject[«Block»];NArgMin[{Norm[Inactive[Plus][a.x, -b]], x1}, x]将三次曲线拟合到离散数据,使数据的第一个和最后一个点位于曲线上:
data = IconizedObject[«Block»];用 DesignMatrix 构建矩阵:
a = DesignMatrix[data, {1, x, x^2, x^3}, x];
b = data[[All, 2]];pointConstraints = {a[[1]].λ == b[[1]], a[[-1]].λ == b[[-1]]};res = NArgMin[{Norm[Inactive[Plus][a.λ, -b]], pointConstraints}, λ]Show[Plot[Evaluate[{1, x, x^2, x^3}.res], {x, 0, 1}, Epilog -> {Red, PointSize[0.03], Point[data[[{1, -1}]]]}], ListPlot[data]]通过找出可最小化
的
,求对非线性离散数据的异常值不太敏感的拟合:
data = IconizedObject[«Block»];bases = Sqrt[1 + (x - #) ^ 2]& /@ Subdivide[-3, 3, 10];
input = DesignMatrix[data, bases, x, IncludeConstantBasis -> False];
output = data[[All, 2]];res = NArgMin[{Norm[Inactive[Plus][a.λ, -b], 1], a == input, b == output}, λ]Plot[Evaluate[bases.res], {x, -3, 3}, Epilog -> {PointSize[0.008], Point[data]}]Plot[{Sin[2x] + Cos[x ^ 2], Evaluate[bases.res]}, {x, -3, 3}, PlotLegends -> {"Reference", "SubscriptBox[L, 1]fit"}]分类问题 (3)
{set1, set2} = IconizedObject[«Block»];对于此分隔问题,第 1 组点必须满足
,第 2 组点必须满足
:
constraints = {set1.a + b1, set2.a + b-1};res = NArgMin[{Norm[a], constraints}, {a, b}]line = res[[1]].{x, y} + res[[2]]Show[RegionPlot[line ≤ 0, {x, 0, 1}, {y, 0, 1}], ListPlot[{set1, set2}, PlotMarkers -> {Automatic, PointSize[0.025]}]]{set1, set2} = IconizedObject[«Block»];
dataplot = ListPointPlot3D[{set1, set2}, Sequence[...]]用 DesignMatrix 构建两组点的二次多项式数据矩阵:
{data1, data2} = DesignMatrix[#, {x, x^2, y, y^2, x y}, {x, y}]& /@ {set1, set2};对于此分隔问题,第 1 组必须满足
,第 2 组必须满足
:
constraints = {data1.a + b1, data2.a + b-1};res = NArgMin[{Norm[a], constraints}, {a, b}, PrecisionGoal -> 3]poly = (res[[1]].{1, x, x^2, y, y^2, x y} + res[[2]])Show[dataplot, ContourPlot3D[poly == 0, {x, -1.5, 1.5}, {y, -1.5, 1.5}, {z, -1, 1}, Sequence[...]]]将给定的点集
分成不同的组. 可通过最小化
找出每组的中心
来完成,其中
是给定的局部核,
是给定的惩罚参数:
x = IconizedObject[«Block»];
{n, dim} = Dimensions[x];
centers = Indexed[μ, #]& /@ Range[n];核
为使得
,否则
成立的
-近邻 (
) 函数. 对于此问题,选择
:
kNN = Nearest[x -> Automatic][x, 10];
κ = SparseArray[Join@@Table[Thread[{i, kNN[[i]]}], {i, n}] -> 1, {n, n}];c1 = Table[Norm[Inactive[Plus][Indexed[x, {i}], -Indexed[μ, {i}]]], {i, n}];
c2 = Flatten[Table[κ[[i, j]]Norm[Indexed[μ, {i}] - Indexed[μ, {j}], 1], {i, 1, n}, {j, i + 1, n}]];
objective = Total[c1 ^ 2] / 2 + 10 Total[c2];vars = Table[Indexed[μ, {i}]∈Vectors[dim, Reals], {i, n}];res = NArgMin[objective, vars];对于每个数据点,都有一个对应的中心. 属于同一个组的数据将有相同的中心值:
allCenters = res;
cvals = Map[First, Gather[allCenters, Norm[#1 - #2] ≤ 0.001&]]gpts = Table[Pick[x, Map[Norm[# - ci] ≤ 0.001&, allCenters]], {ci, cvals}];
Show[ListPlot[gpts, PlotStyle -> PointSize[0.02]], ListPlot[allCenters, PlotStyle -> {PointSize[0.03], Magenta}]]图像处理 (1)
image = ImageResize[ColorConvert[ExampleData[{"TestImage", "House"}], "Grayscale"], 75]data = ImageData[image];
SeedRandom[123];
mask = RandomChoice[{0.4, 0.6} -> {0, 1}, Dimensions[data]];
corruptedData = mask * data;
Image[corruptedData]{n, m} = Dimensions[data];objective = Total[Flatten[Table[Norm[{Indexed[u, {i + 1, j}] - Indexed[u, {i, j}], Indexed[u, {i, j + 1}] - Indexed[u, {i, j}]}], {i, n - 1}, {j, m - 1}]]];knownPos = SparseArray[mask]["NonzeroPositions"];constraint = Map[Indexed[u, #] == data[[Sequence@@#]]&, knownPos];sol = NArgMin[{objective, constraint}, u∈Matrices[{n, m}, Reals]];Image[sol]设施选址问题 (1)
clientPos = {{0, 0}, {1, 0}, {3 / 2, 1}, {-1 / 2, 1}, {1 / 2, 3 / 2}, {1 / 2, 1 / 2}, {0, 3 / 2}, {1, 1}, {0, 1}, {3 / 2, 3 / 2}};
nTowers = 3;
nClients = Length[clientPos];每个基站消耗的功率与其范围成正比,由
给出. 目的是最大程度地降低功耗:
objective = Sum[Erf[r[i] ^ 1.5], {i, nTowers}];decisionConstraint = 0z1;m = 20;
distConstraint = Table[Norm[{p[j, 1], p[j, 2]} - clientPos[[i]]] -
m * (1 - Indexed[z, {i, j}]) ≤ r[j], {i, nClients}, {j, nTowers}];coverageConstraint = Total[z, {2}]1;maxCoverageConstraint = Table[0 ≤ r[j] ≤ 1, {j, nTowers}];vars = Flatten[{Table[{p[j, 1], p[j, 2], r[j]}, {j, nTowers}], z∈Matrices[{nClients, nTowers}, Integers]}];Quiet[res = NArgMin[{objective, decisionConstraint,
distConstraint, coverageConstraint, maxCoverageConstraint}, vars]]towerPos = Table[{p[i, 1], p[i, 2]}, {i, nTowers}] /. Thread[vars -> res]
towerRange = Table[r[i], {i, nTowers}] /. Thread[vars -> res]Graphics[{{Orange, PointSize[0.04], Point[towerPos]}, {Green, PointSize[0.02], Point[clientPos]}, {...}}, Frame -> True]投资组合优化 (1)
求如何在六只股票之间分配资本
,在最大程度地降低风险的同时最大化回报:
{μ, Σ} = Block[...];return = μ.w;risk = w.Σ.w;
riskConstraint = risk ≤ s;目标是最大化收益,同时在指定的风险规避参数的情况下最大程度地降低风险:
objective[α_ ? NumericQ] := -(return - α s);marketConstraints = Table[Abs[Indexed[w, i]] ^ 1.5 ≤ Indexed[t, i], {i, 6}];weightConstraints = {w0, Total[w] + 0.1Total[t] == 1};frontier = Table[res = Quiet@NArgMin[{objective[α],
riskConstraint, weightConstraints, marketConstraints}, {w, s, t∈Vectors[6, Reals]}];
{risk, return} /. w -> res[[1]], {α, Subdivide[0, 10, 100]}];randomRiskReturns = Table[{risk, return}, {w, Table[...]}];
Show[ListPlot[frontier, ...], ListPlot[randomRiskReturns, Rule[...]]]riskAversionParameter = N@{1 / 1000, 1 / 100, 1 / 2, 1, 5, 10, 15, 20};
res = Table[{α, return, Quiet@Append[w, 1 - Total[w]]} /.
w -> Quiet@NArgMin[{objective[α],
riskConstraint, weightConstraints, marketConstraints}, {w, s, t∈Vectors[6, Reals]}][[1]], {α, riskAversionParameter}];通过考虑市场成本,可以获得低风险规避参数的多样化投资组合,但是当风险规避参数较高时,由于购买的是分散程度较低的股票,市场影响成本占主导地位:
Legended[BarChart[res[[All, -1]], ...], SwatchLegend[...]]轨迹优化 (1)
找到一条可以绕过圆形障碍物并使得从起点到终点距离最小化的路径:
p = {{2, 2}, {7, 6}, {3, 5}, {4, 8}, {8, 9}, {6, 2.5}};
r = {1, 1.3, 1.5, 1, 0.7, 1.5};
{start, end} = {{0, 0}, {10, 10}};
domain = Graphics[{MapThread[Circle[#1, #2]&, {p, r}],
{PointSize[0.02], Point[{start, end}]}}, Frame -> True]路径可分解为
个不同的点. 这些点之间的距离必须小于
,其中
是要最小化的长度:
n = 30;
distanceConstraints = Table[Inactive[Norm][x[i] - x[i - 1]] <= l / n, {i, n}];objectConstraints = Table[Inactive[Norm][-p[[j]] + x[i]] >= r[[j]], {i, 1, n}, {j, 1, 6}];positionConstraints = {x[0] == start, x[n] == end};vars = Append[Table[x[i]∈Vectors[2, Reals], {i, 0, n}], l∈Reals];Short[res = NArgMin[{l, distanceConstraints, objectConstraints,
positionConstraints}, vars, Method -> "DifferenceOfConvex"], 4]Show[domain, Graphics[{Red, Point[res[[1 ;; -2]]]}]]属性和关系 (6)
NMinimize 给出最小值,并以规则形式给出使结果最小化的变量值:
NMinimize[{x.x + y, Norm[x + y] ≤ 1}, {Element[x, Vectors[2, Reals]], y}]NArgMin 给出使结果最小化的值的列表:
NArgMin[{x.x + y, Norm[x + y] ≤ 1}, {Element[x, Vectors[2, Reals]], y}]NMinValue 只给出最小值:
NMinValue[{x.x + y, Norm[x + y] ≤ 1}, {Element[x, Vectors[2, Reals]], y}]NArgMin[{x ^ 2 + y ^ 2, x + 2 y ≥ 3}, {x, y}]NArgMax[{-(x ^ 2 + y ^ 2), x + 2 y ≥ 3}, {x, y}]对于凸问题,可用 ConvexOptimization 获取解的其他属性:
NArgMin[{x ^ 2 + y ^ 2, x + 2 y ≥ 3}, {x, y}]ConvexOptimization[x ^ 2 + y ^ 2, x + 2 y ≥ 3, {x, y}, "PrimalMinimizer"]ConvexOptimization[x ^ 2 + y ^ 2, x + 2 y ≥ 3, {x, y}, "DualMaximizer"]对于含有参数的凸问题,用 ParametricConvexOptimization 给出 ParametricFunction:
pfun = ParametricConvexOptimization[x + 2 y, (x - α) ^ 2 + (y - β) ^ 2 ≤ 1, {x, y}, {α, β}, "PrimalMinimizer"]可通过计算 ParametricFunction 求得参数的值:
{pfun[0., 0.], pfun[.5, 1.], pfun[2., 1.]}用 NArgMin 定义参数化问题的函数:
fun[α_ ? NumericQ, β_ ? NumericQ] := NArgMin[{x + 2 y, (x - α) ^ 2 + (y - β) ^ 2 ≤ 1}, {x, y}];
{fun[0., 0.], fun[.5, 1.], fun[2., 1.]}testData = RandomReal[{0, 4}, {1000, 2}];
{First[AbsoluteTiming[pfun @@@testData]], First[AbsoluteTiming[fun @@@ testData]]}也可以计算 ParametricFunction 的导数:
D[pfun[α, β], {{α, β}}] /. {α -> 2., β -> 1.}对于含有参数化约束条件的凸问题,RobustConvexOptimization 将找到适用于所有可能参数值的最优值:
{rMinValue, rMin} = RobustConvexOptimization[2x + 3y, x + y ≥ α && x - y ≤ β && -1 ≤ α ≤ 1 && 0 ≤ β ≤ 1, {x, y}, {α, β}, {"PrimalMinimumValue", "PrimalMinimizerRules"}]对于特定参数值,NArgMin 也许能找到给出更小的最小值的最小化点:
minimizer0 = NArgMin[{2x + 3y, x + y ≥ α && x - y ≤ β && α == 0 && β == 0}, {x, y}]Function[{x, y}, 2x + 3 y]@@ minimizer0(Function[{x, y}, x + y ≥ α && x - y ≤ β ]@@ minimizer0) /. {α -> 1, β -> 1}fun[α_, β_] /; (-1 ≤ α ≤ 1 && 0 ≤ β ≤ 1) :=
NMinValue[{2x + 3y, x + y ≥ α && x - y ≤ β}, {x, y}];testValues = RandomVariate[UniformDistribution[{{-1, 1}, {0, 1}}], 100];
AllTrue[testValues, ((fun@@#) ≤ rMinValue)&]用 RegionNearest 算出给定区域内的最近点:
ℛ = Cone[{{-1, 0, 1}, {2, 3, 4}}, 3];
p = {3., 2., 1.};RegionNearest[ℛ, p]//RootReduce可以使用 NArgMin 进行计算:
NArgMin[Norm[{x, y, z} - p], {x, y, z}∈ℛ]Graphics3D[{ℛ, {PointSize[Large], {Green, Point[p]}, {Red, Point[%]}}}]技术笔记
-
▪
- 数值运算 ▪
- 数值最优化 ▪
- 数值非线性全局最优化示例 ▪
- 约束条件下的优化 ▪
- 无 约束条件下的优化 ▪
- 关于内部实现的一些注释:数字和相关函数
相关指南
-
▪
- 最优化 ▪
- 区域上的求解器 ▪
- 符号向量、矩阵和数组 ▪
- 凸优化
文本
Wolfram Research (2008),NArgMin,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NArgMin.html (更新于 2024 年).
CMS
Wolfram 语言. 2008. "NArgMin." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2024. https://reference.wolfram.com/language/ref/NArgMin.html.
APA
Wolfram 语言. (2008). NArgMin. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NArgMin.html 年
BibTeX
@misc{reference.wolfram_2026_nargmin, author="Wolfram Research", title="{NArgMin}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/NArgMin.html}", note=[Accessed: 25-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_nargmin, organization={Wolfram Research}, title={NArgMin}, year={2024}, url={https://reference.wolfram.com/language/ref/NArgMin.html}, note=[Accessed: 25-November-2026]}