主要内容

本页采用了机器翻译。点击此处可查看英文原文。

使用优化探索器

优化探索器可帮助您通过使用不同的配置(例如不同的求解器、初始点和选项)反复求解优化问题。此示例展示了在该 App 中使用 Global Optimization Toolbox 求解器的情况,但您无需安装 Global Optimization Toolbox 即可使用该 App。

视频演示

如需查看该示例的演示,请播放视频。

创建问题

要使用优化探索器,您必须已有现成的问题,该问题可以是 OptimizationProblem 对象,也可以是优化求解器的输入,例如 fminconpatternsearch。您可以通过运行优化实时编辑器任务,或在命令行中创建一道题目,将题目导入工作区。在此示例中,请运行以下脚本,将 prob 问题导入到您的工作区中。

x = optimvar("x",LowerBound=-100,UpperBound=99);
y = optimvar("y",LowerBound=-98,UpperBound=102);
rng default % For reproducibility
offset = 30*randn(1,2); % Move optimal solution from [0,0]
f2 = @(x,y)sawtoothxy(x-offset(1),y-offset(2));
f = fcn2optimexpr(f2,x,y);
prob = optimproblem(Objective=f);
x0.x = 0;
x0.y = 0;

function f = sawtoothxy(x,y)
[t,r] = cart2pol(x,y); % Change to polar coordinates
h = cos(2*t - 1/2)/2 + cos(t) + 2;
g = (sin(r) - sin(2*r)/2 + sin(3*r)/3 - sin(4*r)/4 + 4) ...
    .*r.^2./(r+1);
f = g.*h;
end

为确保求解器能够无错误地运行该问题,请在命令行上对该问题运行求解器。

[sol,fval] = solve(prob,x0)
Solving problem using fmincon.

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.

sol = struct with fields:
    x: 9.7505
    y: 62.7420

fval = 23.5804

打开优化探索器

点击 App 库中的 App 图标,或在命令行中输入 optimizationExplorer,即可打开该 App。

Optimization Explorer in Apps gallery

选择问题和属性

在此示例中,问题是一个 OptimizationProblem 对象。导入问题对象。

Import problem object.

选择 prob

Select prob.

优化探索器显示了问题的特征。

App displaying the number of variables (2), nonlinear objective type, and bound constraints

起点指定为 x0

点击下一个。如果您知道相关属性,请指明问题涉及的属性。这些属性有助于 App 选择合适的求解器。对于此问题,请为每个属性选择

All four attribute selections are No.

点击下一个

运行优化探索器

指定以自动模式运行该 App,这意味着该 App 将在您设定的时限内,自动运行所有推荐的求解器、起始点和选项。在此示例中,将时间限制设置为 2 分钟。如果您使用的是 Parallel Computing Toolbox™,请选择使用并行,这样 App 就能并行运行,这通常比串行运行更快。与此同时,App 通常会同时运行多个求解器。

Run in auto mode for 2 minutes in parallel (if available).

点击开始会话

Optimization Explorer showing 46 seconds for the current run time and many solutions

  • 求解器配置面板显示了正在运行的求解器。

  • 问题摘要面板显示了问题类型和属性的摘要。

  • 结果摘要面板显示了返回的最佳目标函数值、当前运行时间、已启动和已完成的求解器数量以及其他结果。

  • 默认的散点图显示了目标函数的返回值与时间的对数之间的关系。

  • 结果表中列出了每种求解器配置的结果。

检查结果

查看 App 中显示的结果。首先,通过点击结果表表中的目标值列标题,按目标函数值的升序对结果表进行排序。

Sort in ascending order.

表格中的许多行,其目标函数值都接近 0。

Sorted results have many rows with the same objective value.

您可以应用滤波器,以聚焦于特定的结果。要执行此操作,请点击工具栏中查看结果部分的显示过滤器按钮。结果过滤器面板随即打开,其中显示了可用于过滤绘图和结果表中显示结果的选项。

Results Filters panel

若要仅查看前 10 条结果,请先点击“结果滤波器”窗格顶部的 Reset button 重置按钮,以显示所有结果。然后点击位于结果表顶部的显示排行榜

Results Table showing the top 10 results sorted by objective value

要查看特定配置的解,请点击该配置在列中的条目。

Solution showing x and y values

您可以查看结果表中当前所选配置的详细信息。点击表格右侧的三个垂直点进入配置界面,然后选择显示详细信息

Show details.

将打开一个选项卡,显示配置、迭代显示以及求解器停止的原因等详细信息。该选项卡还提供了相关主题的链接,以帮助您解读结果。

Details show the data associated with the solution process such as options, fval, and exitflag. Other sections show the reason the solver stopped, the iterative display, and links for further interpretation.

点击 Reset button 重置按钮,即可清除排行榜的过滤条件。

Reset Results Table

您可以选择在表格中显示哪些数据列。

Select the columns that appear in the results table.

添加或删除表列。

Dialog box with options for specifying which columns appear in the Results Table

显示结果绘图

探索各种绘图,这些绘图会根据您应用的滤波器而变化。首先,显示“解坐标”绘图。

Solution coordinates plot

该图显示了该二维问题的解的坐标,并按目标函数值进行了着色。接下来,显示求解器的箱绘图。

Solver box plots for several solvers.

重跑优化

选择一些求解器,以便手动重新运行。您可以重新安排结果表中当前选中的配置。点击表格右侧的三个垂直点进入配置界面,然后选择重新调度

Selecting the Reschedule option

从工具栏的选择求解器部分中选择其他求解器,即可运行它们。该 App 必须处于手动模式,您才能选择求解器。要切换到手动模式,请在选择求解器部分中选择手动。当您选择要运行的求解器时,它们会以选项卡的形式显示在主窗口中。

Tabs showing three scheduled configurations.

求解器配置面板显示了相同的计划配置。

Three scheduled solvers in the Solver Configurations panel.

选择要运行的求解器配置后,请点击工具栏中的运行

导出结果

您可以导出整个结果表,或者导出一个用于在表中生成当前所选结果的函数。

Export the results table or a function to call the solver for the selected table row.

导出的表格仅包含当前滤波器下的数据,并包含所有可能的列以及附加的结果详细信息。

导出的函数不包含您的优化问题或求解器输入。要使用导出的函数,请将该问题单独保存,或保存求解器的输入数据。

例如,如果您是从一个 OptimizationProblem 对象启动了优化探索器,则导出的函数代码开头如下:

function [solution,objectiveValue] = solveProblem(problem)

此导出函数要求您将原始的 OptimizationProblem 对象作为 problem 的参量传入。

如果您是从求解器输入中启动的优化探索器,则导出的函数代码以以下内容或等效内容开头:

function [solution,objectiveValue] = solveProblem(fun,A,b,Aeq,beq,lb,ub,nonlcon)
% This function accepts the problem inputs imported into the Optimization
% Explorer app and reproduces the selected result.

% Auto-generated by MATLAB on 24-Oct-2025 15:57:35

% Load data
data = load("explorerConfiguration1_1FunctionData1.mat");
x0 = data.x0;

此导出函数要求您提供原始目标函数 fun、线性约束矩阵 AbAeqbeq、边界条件 lbub,以及非线性约束函数 nonlcon。原始起始点 x0 已保存在优化探索器的会话数据中,您还需要在文件系统中保留该数据,才能运行导出的函数。

另请参阅