Cardinality constraint not adhered too during MIQP problem

60 views
Skip to first unread message

Vaughn Gambeta

unread,
Jul 16, 2018, 12:50:23 AM7/16/18
to Gurobi Optimization
Hey,

I am optimizing a tracking portfolio with a subset of given assets that minimizes the tracking error. The subset of assets must adhere to the given equality constraint K. For example, I wish the subset to consist of 300 assets out of the 500 in the set with minimum error. This is a MIQP. The example below outlines the constraints, the X values must add up to 1 and the sum of the Y must equal K. The Y varariable is introduced as a binary indicator constraint to control whether or not an asset is included. 

The problem is not solved adhering to the cardinality constraint size of K. The solution below, asking for 300 assets, only produces 282 in the optimal solution. This occurs for various K, 200 -> 198 etc. 

The question is, why does the solver give me a solution without adhering to that constraint? Should it not be infeasible?

Code sample is below, output is attached.



# Create Model
m = Model('TE Portfolio')

# Add X & Y Variables & Budget Constrain
X = pd.Series(  m.addVars(ASSET_NAMES, vtype = GRB.CONTINUOUS, lb=np.zeros((n,), dtype=int), ub=np.ones((n,), dtype=int)), index=ASSET_NAMES )
Y = pd.Series( m.addVars((i for i in range(n)),vtype=GRB.BINARY, lb=0, ub=1) )
m.update()

#Budget Constraint
m.addConstr(X.sum() == 1, 'Budget')
m.update()

#Cardinality Constraint
K = 300
m.addConstr(quicksum(Y) == K, 'Cardinality')
for i in range(n):
    m.addGenConstrIndicator(Y[i], 0, X[i] == 0)
    m.update()

# Objective Minimization
OBJ = Q.dot(np.subtract(X, W_ASN)).dot(np.subtract(X, W_ASN)) 
m.setObjective(OBJ, GRB.MINIMIZE)
m.optimize()
Output.txt

Michael Winkler

unread,
Jul 17, 2018, 10:20:18 AM7/17/18
to Gurobi Optimization
Can you upload/make an example available (e.g., via dropbox) best in MPS format compressed with 7z, bzip2, or gzip.

Best,
Michael

Daniel Espinoza

unread,
Jul 17, 2018, 10:27:18 AM7/17/18
to Gurobi Optimization
Hi 

The constraint you are really modeling with that is that at most K instruments be used, since X[i] == 0 does not imply that Y[i] == 0

Best,
Daniel

Vaughn Gambeta

unread,
Jul 19, 2018, 3:49:19 PM7/19/18
to Gurobi Optimization
This is where my thinking is wrong, I thought it did imply that, thank you Daniel. 
Reply all
Reply to author
Forward
0 new messages