BARON compatible functions
BARON is a sophisticated global optimizer that requires an algebraic description of the model in order to optimize it. Therefore, user-supplied functions (e.g., objective and constraints) must be pre-processed to an algebraic form that 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 only one variable (i.e., 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 a 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 that is 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, 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 only one argument (x). If your function requires multiple parameters you can wrap it like this:
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, but 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, which will be internally converted to a column vector. This process assumes column major, so you must ensure that your nonlinear constraint bounds are set in the same order. All operations (vector, matrix, etc.,) that are 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 and that of the generated BARON function when both are subject to the same random x0. This process ensures that the problem BARON solves is identical to the one 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 at The Optimization Firm. The interface is provided free of charge and without warranties.
EST. 2001
The Optimization Firm, LLC
Terms of Service | Privacy Policy