Optimizing Unconstrained, Constrained, and Mixed Integer Problems with Matlab’s surrogateopt

Vitality Learning
3 min readSep 12, 2024
Photo by Shahadat Rahman on Unsplash

Matlab provides powerful tools for solving optimization problems, and one such tool is the surrogateopt function. This article explains three types of optimizations—unconstrained, constrained, and mixed integer—using surrogateopt. We'll walk through the mathematical formulations and the implementation of each example.

The codes of this post are available at this GitHub page.

1. Unconstrained Optimization: Rastrigin Function

The Rastrigin function is a well-known benchmark for testing optimization algorithms due to its complexity and numerous local minima. Mathematically, it is defined as:

where n is the number of variables. For this example, we are optimizing for n=2.

Matlab Implementation:

function f = objconstrRastrigin(x)
f.Fval = 0;
for k = 1 : length(x)
f.Fval = f.Fval + x(k)^2 - 10 * cos(2 * pi * x(k));
end
f.Fval = f.Fval + 10 * length(x);
end

lb = [-5.12, -5.12];
ub = [5.12, 5.12];
trials = [0.5, -0.1];

options = optimoptions('surrogateopt', 'PlotFcn', 'surrogateoptplot', 'InitialPoints', trials);
[x, fval, exitflag] = surrogateopt(@objconstrRastrigin, lb, ub, options);

In this code, we use surrogateopt to minimize the Rastrigin function over the range [−5.12,5.12] for each variable.

2. Constrained Optimization: G06 Problem

The G06 problem is a well-known constrained optimization problem that is defined as:

Minimize:

subject to the constraints:

Matlab Implementation:

function f = objconstrG06(x)
f.Fval = (x(1) - 10)^3 + (x(2) - 20)^3;
f.Ineq = [-(x(1) - 5)^2 - (x(2) - 5)^2 + 100, ...
(x(1) - 6)^2 + (x(2) - 5)^2 - 82.81];
end

lb = [13, 0];
ub = [100, 100];
trials = [13.5, 0.5];

options = optimoptions('surrogateopt', 'PlotFcn', 'surrogateoptplot', 'InitialPoints', trials);
[x, fval, exitflag] = surrogateopt(@objconstrG06, lb, ub, options);

In this case, the function is minimized while satisfying two inequality constraints.

3. Mixed Integer Optimization

In mixed integer optimization, some of the decision variables are restricted to integer values. For this example, the goal is to minimize the function:

subject to the constraints:

Matlab Implementation:

function f = objconstrMI(x)
f.Fval = -2 * x(1) - 3 * x(2);
f.Ineq = [-4 * x(1) + 2 * x(2), ...
2 * x(1) + 2 * x(2) - 30, ...
-x(2), ...
-0.5 * x(1) + x(2) - 3.75];
end

lb = [-1, -1];
ub = [10, 15];
trials = [0, 0];

options = optimoptions('surrogateopt', 'PlotFcn', 'surrogateoptplot', 'InitialPoints', trials);
[x, fval, exitflag] = surrogateopt(@objconstrMI, lb, ub, [1 2], [], [], [], [], options);

Here, the variables x1​ and x2​ are optimized while satisfying a set of constraints. In this mixed-integer problem, some variables are required to take integer values.

--

--

Vitality Learning

We are teaching, researching and consulting parallel programming on Graphics Processing Units (GPUs) since the delivery of CUDA. We also play Matlab and Python.