主要内容

Pass Extra Parameters in Problem-Based Approach

In an optimization problem, the objective or constraint functions sometimes have parameters in addition to the independent variable. The extra parameters can be data, or can represent variables that do not change during the optimization.

To include these parameters in the problem-based approach, simply refer to workspace variables in your objective or constraint functions.

Least-Squares Problem with Passed Data

For example, suppose that you have matricesCd在里面particle.matfile, and these matrices represent data for your problem. Load the data into your workspace.

loadparticle

View the sizes of the matrices.

disp(size(C))
2000 400
disp(size(d))
2000 1

Create an optimization variablex适合形成向量的大小C*x.

x = optimvar('x',size(C,2));

Create an optimization problem to minimize the sum of squares of the terms inC*x – dsubject to the constraint thatxis nonnegative.

x.LowerBound = 0; prob = optimproblem; expr = sum((C*x - d).^2); prob.Objective = expr;

You include the dataCd仅通过在目标函数表达式中引用问题就进入问题。解决这个问题。

[sol,fval,exitflag,output] = solve(prob)
Solving problem using lsqlin. 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: [400x1 double]
fval = 22.5795
exitflag = OptimalSolution
output =struct with fields:message: 'Minimum found that satisfies the constraints....' algorithm: 'interior-point' firstorderopt: 9.9673e-07 constrviolation: 0 iterations: 9 linearsolver: 'sparse' cgiterations: [] solver: 'lsqlin'

Nonlinear Problem with Extra Parameters

使用相同的方法解决非线性问题。例如,假设您具有几个变量的目标函数,其中一些是用于优化的固定数据。

typeparameterfun
function y = parameterfun(x,a,b,c) y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-c + c*x(2)^2)*x(2)^2;

For this objective function,xis a 2-element vector, anda,b, 和c是标量参数。创建优化变量并在工作区中分配参数值。

a = 4; b = 2.1; c = 4; x = optimvar('x',2);

Create an optimization problem. Because this objective function is a rational function ofx, you can specify the objective in terms of the optimization variable. Solve the problem starting from the pointx0.x = [1/2;1/2].

概率= optimproblem;概率。目标= parameterfun(x,a,b,c); x0.x = [1/2;1/2]; [sol,fval] = solve(prob,x0)
Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol =struct with fields:x: [2x1 double]
fval = -1.0316

Ifparameterfunwere not composed of supported functions, you would convertparameterfunto an optimization expression and set the converted expression as the objective. SeeSupported Operations for Optimization Variables and ExpressionsConvert Nonlinear Function to Optimization Expression.

expr = fcn2optimexpr(@parameterfun,x,a,b,c); prob.Objective = expr; [sol,fval] = solve(prob,x0)
Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol =struct with fields:x: [2x1 double]
fval = -1.0316

Copyright 2018–2020 The MathWorks, Inc.

See Also

相关话题