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()