Optimize Function Using
This example shows how to minimize a function using simulated annealing in the problem-based approach when the objective is a function file, possibly of unknown content (a "black box" function). The function to minimize,
dejong5fcn(x), is included with Global Optimization Toolbox. Plot the function.
Create a 2-D optimization variable
dejong5fcn function expects the variable to be a row vector, so specify
x as a 2-element row vector.
x = optimvar("x",1,2);
dejong5fcn as the objective function, convert the function to an optimization expression using
fun = fcn2optimexpr(@dejong5fcn,x);
Create an optimization problem with the objective function
prob = optimproblem("Objective",fun);
Set variable bounds from –50 to 50 in all components. When you specify scalar bounds, the software expands the bounds to all variables.
x.LowerBound = -50; x.UpperBound = 50;
Set a pseudorandom initial point within the bounds. The initial point is a structure with field
rng default % For reproducibility x0.x = x.LowerBound + rand(size(x.LowerBound)).*x.UpperBound;
Solve the problem, specifying the
[sol,fval] = solve(prob,x0,"Solver","simulannealbnd")
Solving problem using simulannealbnd. Optimization terminated: change in best function value less than options.FunctionTolerance.
sol = struct with fields: x: [-32.0371 -31.8792]
fval = 0.9980