# bayesopt with equality and inquality constraints

4 views (last 30 days)
Liao Peizhi on 19 May 2020
Commented: Alan Weiss on 21 May 2020
How to optimize the following objective funciton with constraints?
min f(x)
s.t. Ax<b
x(1)=3;
x(1)<x(2)
Here are my codes:
global A b
A=eye(2);
b=[3, 3]
xdata=[
optimizableVariable('x1', [-3 ; 6], 'type', 'real')
optimizableVariable('x2', [-1; 6], 'type', 'real')];
results=bayesopt(@fun, xdata, 'IsObjectiveDeterministic',true, 'XConstraintFcn',@xconstraint);
function objective=fun(x)
global A
objective=[x.x1, x.x2]*A*[x.x1; x.x2]+3
end
function tf=xconstraint(x)
global A b
tf1=x.x1==3
tf2=x.x1<x.x2;
tf3=A*[x.x1; x.x2]<b
tf=tf1&tf2&tf3;
end
However, matlab reports error:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To perform elementwise multiplication, use '.*'.
I was wondering how to implement the inquality constraints (in matrix form like quadprog) in bayesian optimization?
Besides, are there any ways to remove the global variables?
Thanks!

Alan Weiss on 20 May 2020
I believe that you have two errors in your formulation. As explained in Constraints in Bayesian Optimization, bayesopt attempts to pass thousands of points to your constraint function to obtain the feasibility results as logical values. So you should write your XConstraintFcn to take an arbitrary number of input variables. You can write a loop to calculate the values:
function tf = xconstraint(x,A,b)
global A b
n = size(x,1);
tf = false(n,1);
for ii = 1:n
tf(i,1) = x.x1(i) < x.x2(i);
tf(i,1) = all(A*[x.x1(i); x.x2(i)] < b) & tf(i,1);
end
end
You will notice that I have three arguments for xconstraint(x,A,b). This is to get around the problem of global variables. You call the function as @(x)xconstraint(x,A,b). For details, see Parameterizing Functions.
The other error is trying to set x1 = 3 while having it declared as a real optimizable variable. This is impossible. You need to set it as either a constant, so there is no x1 as an optimizable variable, or you need to set the lower and upper bounds equal to 3, or you need to set it as a categorical variable with value 3. If it is a real variable with unequal bounds, bayesopt will try to set it to random real values, and the Xconstraint function will return that the value is not acceptable, and your optimization will never get anywhere.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Liao Peizhi on 21 May 2020
Hi Alan,
I also notice that the size of x in constraint function is 10000. I was wondering what does this number stand for? Is it related to the 'NumSeedPoints' ? However, the default value for seed points is 4.
Besides, will it (10000) slow down the calculation and is it possible to lower this number?
Thank you again for your kind help!
Best regards,
Peizhi Liao
Alan Weiss on 21 May 2020
I tried to be as clear as I could when describing deterministic constraints in the doocumentation. bayesopt samples these constraints at a large number of points in order to find points in the feasible region. These points are pseudorandom, and this is why you cannot set x1 = 3 in this way, because that test would always return false. These points have nothing to do with seed points. The evalutaion time of these points is very low, in general, especially compared to everything else going on in the solver.
I also just noticed that I left a global statement in my answer. That should not be there. In general, you should not use global variables.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation