BARON is a sophisticated global optimizer that requires an algebraic description of the model in order to optimize it. User-supplied functions (objective, constraints) must be pre-processed to an algebraic form BARON can process. Based on this requirement, user-supplied functions must meet the following criteria:
Scalar functions
A simple objective function is written as a function of indexed (scalar) decision variables, such as:
obj = @(x) 3*x(1)^2 - 2*x(2);
If your objective is a function of one variable only (single variable optimization), you may drop the index:
obj = @(x) 3*x^2;
Alternatively, if your objective does not contain any variables, you must still write it as a function of x:
obj = @(x) -3;
Vectorized functions
The MATLAB - BARON Interface also supports vector and a subset of matrix operations. For the following examples, assume x is a 3 x 1 column vector:
a = [1;2;3];
H = eye(3);
objV = @(x) sum(a.*x); % element wise multiply summed
objD = @(x) a'*x; % equivalent to above but via inner product
objM = @(x) x'*H*x; % quadratic term
The interface implements indexing identical to MATLAB so you can use logical or position based indexing:
objI = @(x) sum(a(a>1).*x(a>1)); % logical indexing
objI2 = @(x) sum(a(1:2).*x(2:3)); % position based indexing
objI3 = @(x) sum(a(1)*x); % scalar expansion is also supported
Remember that your objective function must always result in a scalar.
MATLAB functions and anonymous functions
Both MATLAB functions and anonymous functions are supported. For example:
obj = @(x) 3*x(1)^2 - 2*x(2)^3 + 3*x(1)*x(2);
OR
function fx = objective(x)
fx = 3*x(1)^2 - 2*x(2)^3 + 3*x(1)*x(2);
end
However, both functions must be a function of one argument (x) only. If your function requires multiple parameters you can wrap it as follows:
function fx = objective(x,a,b)
fx = 3*x(1)^2 - a*x(2)^3 + b*x(1)*x(2);
end
a = 2; b = 3;
obj = @(x) objective(x,a,b); %now only a function of x
Multiline MATLAB functions, including loops, are supported. However, the function must be deterministic. No matter the value of x, the function executes the same code path.
Nonlinear constraint function
The nonlinear constraint function should return a column vector of values. For example:
nlcon = @(x) [-(x(1)*x(2));
3*x(1) + 2*x(2)];
OR
function cx = nlconstraints(x)
cx(1,1) = -(x(1)*x(2));
cx(2,1) = 3*x(1) + 2*x(2);
end
However, you may also return a matrix of values, in which case it will be internally converted to a column vector. This process assumes column-major, so ensure your nonlinear constraint bounds are set in the same order. All operations (vector, matrix, etc.) supported by the objective are also supported by the nonlinear constraint function.
MATLAB/BARON interface supported functions
Function
name
Typical usage
Function
Name
Typical usage
+
plus
a+b
prod
Product
prod(a)
-
minus
a-b
diff
Difference
diff(a)
./
rdivide
Elementwise a./b
diag
Diagonal
diag(a)
/
mrdivide
Elementwise a./b if b is a scalar
triu
Tri upper
triu(a)
.*
times
Elementwise a.*b
tril
Tri lower
tril(a)
*
mtimes
Matrix and vector products +inner and outer products
fliplr
Flip LR
fliplr(a)
.^
power
Elementwise a.^b
flipud
Flip UD
flipud(a)
^
mpower
a^b if a is square, and b is an integer
rot90
Rotate 90
rot90(a)
-
uminus
Elementwise -a
repmat
Replicate matrix
repmat(a,m,n)
.'
ctranspose
a'
reshape
Reshape
reshape(a,m,n)
'
transpose
a'
length
Length
length(a)
exp
Exponential
exp(a)
size
Size
size(a)
log
Natural Log
log(a)
ndims
# Dimensions
ndims(a)
log10
Log Base 10
log10(a)
isscalar
Is scalar
isscalar(a)
dot
Dot Product
dot(a,b)
vertcat
V concatenate
[a; b]
sqrt
Square Root
sqrt(a)
horzcat
H concatenate
[a b]
norm
Norm
norm(a) [Vector = 2 norm, Matrix = Frobenius Norm]
subsref
Subs reference
a(1), a(1:2), a(1:end), a(1,1), a(1,:), a(:)
sum
Sum
sum(a)
subsasgn
Subs assign
c(1) = a, c(1:2) = a, c(1,:) = a;
Checking the processed function
By default, baron.m will compare the result of the original function with that of the generated BARON function when both are subjected to the same random x0. This process ensures that what BARON solves is identical to what you described in MATLAB. If you wish to skip this check (for efficiency reasons), you can disable it via the option 'chkfun' in baronset.
Alternatively, you can manually inspect the resulting equations using the following code snippet (assuming you have defined fun as the objective, nlcon as the constraints, and x0 as initial guess):
x = barvec(size(x0)); % BARON Vector Variable
fval = fun(x) % Objective Function Equation
cval = nlcon(x) % Constraint Function Equation(s)
For example, consider the following code:
% Problem
a = [1;2;3];
fun = @(x) sum(a.*x);
x0 = zeros(3,1);
% Test
x = barvec(size(x0));
fval = fun(x)
The above code results in the following equation:
Scalar BARVEC Object
Eq [+]: 1*x1 + 2*x2 + 3*x3
The MATLAB/BARON interface is provided by http://www.minlp.com. It is free of charge and with no warranties.