bayesopt with equality and inquality constraints

20 views (last 30 days)
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!

Answers (1)

Alan Weiss
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
  7 Comments
Alan Weiss
Alan Weiss on 29 Dec 2020
I'm sorry, but I really don't understand your issues. I suggest that you start a new question and put in the question exactly what you are trying to do.
Alan Weiss
MATLAB mathematical toolbox documentation
Federico
Federico on 29 Dec 2020
All right! I created an ad-hoc topic for my question, hope this helps.
Thank you again.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!