1

I have some troubles to understand how to implement the following MIQP (Mixed Integer Quadratic Programming) with linear constraints in Matlab calling Gurobi.

Let me explain in a schematic way my setting.


(1) x is the unknown and it is a column vector with size 225x1.


(2) The objective function (which should be minimised wrto x) looks like

enter image description here

which can be rewritten as

enter image description here

I have a Matlab script computing alpha, Q,c (Q,c sparse) when some_known_parameters1 are given:

function [alpha, Q,c]=matrix_objective_function(some_known_parameters1)

%...

end

(3) The constraints are linear in x, include equalities and inequalities, and are written in the form enter image description here

I have a Matlab script computing Aeq,beq,Aineq,bineq (Aeq,Aineq sparse) when some_known_parameters2 is given:

function [Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)

%...

end

(4) Some components of x are restricted to be in {0,1}. I have a Matlab script producing a string of letters B (binary), C (continous) when some_known_parameters3 is given:

function type=binary_continuous(some_known_parameters3)

%...

end

Now, I need to put together (1)-(4) using Gurobi. I am struggling to understand how. I found this example but it looks very cryptic to me. Below I report some lines I have attempted to write, but they are incomplete and I would like your help to complete them.

clear 
rng default

%Define some_known_parameters1, 
 some_known_parameters2,some_known_parameters3 [...]

%1) generate alpha,Q,c,Aeq,beq,Aineq,bineq,type with Q,c,Aeq, Aineq sparse
[alpha, Q,c]=matrix_objective_function(some_known_parameters1)
[Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)
type=binary_continuous(some_known_parameters3)



%2) Set up Gurobi
clear model;
model.A=[Aineq; Aeq];
model.rhs=full([bineq(:); beq(:)]); 
model.sense=[repmat('<', size(Aineq,1),1); repmat('=', size(Aeq,1),1)];
model.Q=Q; %not sure?
model.alpha=alpha; %not sure?
model.c=c; %not sure?
model.vtype=type;
result=gurobi(model); %how do I get just the objective function here without the minimiser?

Questions:

(1) I'm not sure about

model.Q=Q; 
model.alpha=alpha; 
model.c=c;

I'm just trying to set the matrices of the objective function using the letters provided here but it gives me error. The example here seems to me doing

model.Q=Q; 
model.obj=c; 

But then how do I set alpha? Is it ignoring it because it does not change the set of solutions?

(2) How do I get as output stored in a matrix just the minimum value of the objective function without the corresponding x?

8
  • 1
    You should not expect to be able to give gurobi some black-box objective. This is something for NLP territory (where some form of differentiation is happening inside; e.g. Ipopt/Bonmin). Gurobi needs this objective in it's own native form. What form that is depends on your lib/wrapper. In low-level form usually something like 0.5 * x'Qx + q'x with Q psd (convex QP; which is probably the only one gurobi supports; ignoring SOCP generalizations). If that's giving you headaches, look for some more high-level wrapper. Gurobi's Python-API for example supports expr = QuadExpr(x*x + y+y). Commented Nov 13, 2018 at 19:49
  • @sascha thanks: my problem is very basic I guess: (a) I think my objective function can be rewritten as Q+x'Hx (I have added this to my question); (b) I still don't understand how to complete steps 3) and 4) above. Commented Nov 13, 2018 at 19:55
  • 1
    This is all explained in the docs for this low-level view. Bring your objective in this form and set obj = some_vec, objcon = some_vec and Q = some_matrix. Then (4) is just a string it seems like BBC (binary, binary, continuous). The bounds are vectors lb and ub. Commented Nov 13, 2018 at 20:02
  • 1
    vtypes is string. lb ub are vectors. 3 vars between (-1,1), then (1,3) and (2,3) will be lb=[-1,1,2] and ub=[1,3,3]. Commented Nov 14, 2018 at 5:35
  • 1
    That's a modelling thing. I recommend grabbing some integer-programming book. You can use one binary variable and replace all occurences with the term x = 1-2*binVar. x will be in {-1,1} then. Yes, this is annoying in low-level form (but there is no way out without wrappers / lib-support). But nobody should do prototyping on this level imho. Commented Nov 14, 2018 at 8:15

1 Answer 1

2

(1) You're right, there's no need to pass the constant alpha since it doesn't affect the optimal solution. Gurobi's MATLAB API only accepts sparse matrices. Furthermore model.obj is always the c vector in the problem statement:

model.Q = sparse(Q); 
model.obj = c;

(2) To get the optimal objective value, you first need to pass your model to gurobi and solve it. Then you can access it via the objval attribute:

results = gurobi(model);
val = results.objval + alpha
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.