My optimization problem has ~200 variables but I would like to find a solution that only uses 5 of them. As in 195 should be zero and the other 5 can be non zero.
I have tried the following constraint, but it seems that the optimization algorithm completely ignores it, as it continues to use all 200 variables whether I include the constraint or not. Is there something I am missing or can SLSQP just not handle this?
import pandas as pd
import numpy as np
from scipy.optimize import minimize, Bounds
tmp = pd.DataFrame()
tmp.insert(loc=0,column='pred', value = np.random.random(200))
tmp['x0'] = 0
def obj(x, df = tmp):
return np.sum(-x * df['pred'].values)
def c2(x):
return -(len(x[x!=0])-5)
sol = minimize(fun=obj,x0=tmp['x0'],method='SLSQP',bounds=Bounds(-10,10),jac=False,
constraints=({'type': 'ineq', 'fun': c2}),
options={'maxiter': 1000})
When I run this it just sets everything to 10 and ignores c2.