我有一个二进制MIP优化问题,其中我想选择最好的候选人,给出一个预算和一些其他属性的限制。对于每个候选人,我都有一个样本向量,说明他们可能的效用是从单独的贝叶斯分析中得到的。候选人之间是相互关联的。所以,我不想仅仅根据他们的平均效用来选择最好的候选人,我想对那些过于相关的候选对进行负向加权。有点类似于金融中的均值-方差优化投资组合。
下面是带有玩具示例数据的代码
import numpy as np
import pandas as pd
import cvxpy as cvx
N = 256
N_SAMPLES = 2**10
N_AGENCIES = 16
static_data = {'style':np.random.randint(4, size=N), 'agency':np.random.randint(N_AGENCIES, size=N), 'hourly_rate':np.random.normal(160, 32, size=N).astype(np.int)}
df = pd.DataFrame(static_data)
#df.head()
S = np.random.randn(N,N)
cov = S.dot(S.T)
cov = np.divide(np.divide(cov, np.sqrt(np.diag(cov).reshape(-1,1))), np.sqrt(np.diag(cov).reshape(1,-1)))
mu = static_data['hourly_rate']
mu = (mu - mu.mean())/mu.std()
samples = np.random.multivariate_normal(mu, cov, size=N_SAMPLES)
#samples.shape
HOURLY_BUDGET = 1000
REQUIRED_STYLES = np.array([3, 2, 1, 4])
MAX_PER_AGENCY = 2
M_AGENCIES = np.eye(N_AGENCIES)[df['agency']].astype(np.int)
M_STYLES = np.eye(4)[df['style']].astype(np.int)
### Optimization in cvxPy
selection = cvx.Variable(N, boolean=True)
utility = samples@selection
total_utility = cvx.sum(utility)
avg_utility = total_utility / utility.size
utility_over_avg = utility - avg_utility
risk = cvx.norm(utility_over_avg, 2)
constraint_number = cvx.sum(selection)==np.sum(REQUIRED_STYLES)
constraint_styles = selection@M_STYLES == REQUIRED_STYLES
constraint_agencies = cvx.max( selection@M_AGENCIES ) <= MAX_PER_AGENCY
constraint_budget = cvx.sum( selection*df['hourly_rate'].values ) <= HOURLY_BUDGET
constraints = [constraint_number, constraint_styles, constraint_agencies, constraint_budget]
alpha_value = 0.01
alpha = cvx.Parameter(pos=True, value=alpha_value)
prob = cvx.Problem(cvx.Maximize(total_utility - risk*alpha), constraints=constraints)
prob.solve(solver='ECOS_BB')
print( 'solverName', prob.solver_stats.solver_name )
print( 'solverStatus', prob.status )
selected_flags = selection.value.round().astype(np.bool)
print( selected_flags.dot(M_STYLES).astype(np.int), selected_flags.dot(M_AGENCIES).max(), selected_flags.dot(df['hourly_rate']) )
print( df.loc[selected_flags] )这在cvxpy中有效,我得到了一个解决方案。然而,随着我将alpha_value增加到一个更高的数目来惩罚相关性,那么ECOS_BB需要很长的时间。在我的实际代码中,无论是解决问题还是返回不可行的问题,都需要超过24小时。在实际代码中,alpha_value的较低值在2到5分钟内解决。实际数据有8000个样本,约150种候选数据,以及相同的约束设置。
如果我尝试使用solver='CBC‘,那么它将立即失败,并发出错误消息。
SolverError: Either candidate conic solvers (['CBC']) do not support the cones output by the problem (SOC), or there are not enough constraints in the problem.几个问题:
总的来说,
发布于 2020-01-01 10:47:01
让我试着回答你的个人问题。
x'Qx是二进制的,x可以线性化)。总之:这一切都需要一些工作,也许不值得付出努力。(2)是的,如果去掉风险,模型就变成线性的。CBC可以解决这个线性model.https://stackoverflow.com/questions/59551185
复制相似问题