Mixed-Integer Surrogate Optimization, Problem-Based
This example shows how to solve an optimization problem that involves integer variables. In this example, find the pointx
that minimizes themultirosenbrock
函数超过整数值的参数,范围从–3到6个维度。这multirosenbrock
function is a poorly scaled function that is difficult to optimize. Its minimum value is 0, which is attained at the point[1,1,...,1]
. The code for themultirosenbrock
function appears at theend of this example.
Create a 10-D row vector variablex
带有边界–3的类型整数的。指定标量边界时,界限适用于所有变量组件。
x = optimvar("x",1,10,"LowerBound",-3,"UpperBound",6,“类型”,“整数”);
To usemultirosenbrock
as the objective function, convert the function to an optimization expression usingFCN2OPTIMEXPR
.
fun = fcn2optimexpr(@multirosenbrock,x);
Create an optimization problem with the objective functionmultirosenbrock
.
prob = optimproblem("Objective",fun);
Set the maximum number of function evaluations to 200.
opts = optimoptions("surrogateopt","MaxFunctionEvaluations",200);
解决这个问题。
rng(1,'twister')% For reproducibility[sol,fval] = solve(prob,“求解器”,"surrogateopt","Options",opts)
Solving problem using surrogateopt.
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.MaxFunctionEvaluations'.
sol =struct with fields:x: [1 1 1 1 1 1 1 1 1 1]
fval = 0
In this case,surrogateopt
reaches the correct solution.
Mixed-Integer Problem
Suppose that only the first six variables are integer-valued. To reformulate the problem, create a 6-D integer variablexint
and a 4-D continuous variablexcont
.
xint = optimvar("xint",1,6,"LowerBound",-3,"UpperBound",6,“类型”,“整数”); xcont = optimvar("xcont",1,4,"LowerBound",-3,"UpperBound",6);
Convertmultirosenbrock
to an optimization expression using the input[xint xcont]
.
fun2 = fcn2optimexpr (@multirosenbrock [xint xcont]);
Create and solve the problem.
prob2 = optimproblem("Objective",fun2); rng(1,'twister')% For reproducibility[sol2,fval2] = solve(prob2,“求解器”,"surrogateopt","Options",opts)
Solving problem using surrogateopt.
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.MaxFunctionEvaluations'.
sol2 =struct with fields:XCONT:[1.2133 1.4719 1.1857 1.5003] XINT:[1 1 1 1 1 1 1]
FVAL2 = 0.9736
This time the integer variables reach the correct solution, and the continuous variables are near the solution, but are not completely accurate.
Helper Function
This code creates themultirosenbrock
helper function.
functionF = multirosenbrock(x)% This function is a multidimensional generalization of Rosenbrock's% function. It operates in a vectorized manner, assuming that x is a matrix% whose rows are the individuals.Mathworks,Inc。2014年版权所有%N = size(x,2);% assumes x is a row vector or 2-D matrixifmod(N,2)% if N is odd错误('Input rows must have an even number of elements')end赔率= 1:2:n-1;evens = 2:2:n;f =零(size(x));f(:,ordds)= 1-x(:,ordds);f(::,evens)= 10*(x(:,evens)-x(:,ordds)。^2);f = sum(f。^2,2);end