主要内容

使用 particleswarm 优化函数(基于问题)

此示例说明当目标是函数文件(可能是“黑盒”函数,即内容未知)时,如何在基于问题的方法中使用粒子群最小化函数。运行此示例时,将包含要最小化的函数 dejong5fcn(x)

dejong5fcn

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

创建一个二维优化变量 xdejong5fcn 函数期望变量是行向量,因此将 x 指定为二元素行向量。

x = optimvar("x",1,2);

要使用 dejong5fcn 作为目标函数,请使用 fcn2optimexpr 将该函数转换为优化表达式。

fun = fcn2optimexpr(@dejong5fcn,x);

用目标函数 fun 创建一个优化问题。

prob = optimproblem("Objective",fun);

在所有分量中,将变量边界设置为从 - 50 到 50。指定标量边界时,软件会将边界扩展到所有变量。

x.LowerBound = -50;
x.UpperBound = 50;

求解问题,指定 particleswarm 求解器。

rng default % For reproducibility
[sol,fval] = solve(prob,"Solver","particleswarm")
Solving problem using particleswarm.
Optimization ended: relative change in the objective value 
over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
sol = struct with fields:
    x: [-31.9751 -31.9719]

fval = 
0.9980

另请参阅

| |

主题