我目前正在运行一个优化问题,我已将代码简化为以下内容:
1 个 90 个数字列表,范围从 0-100
1 个包含 90 个二进制数的列表
ulitamte 的目标是最大化这些列表的点积,受约束(目前只有 2 个,稍后将添加更多)
目前有两个约束:
二进制检查 - 查看所有数字是 1 还是 0
总检查 - 确保第二个列表总和为 15(因此点积是第一个列表中正好 15 个数字的总和)
运行以下代码时,我看到“LSQ 子问题中的奇异矩阵 C”错误。
我试过通过excel求解器运行同样的问题,它能够找到最佳解决方案。任何帮助将不胜感激
import numpy as np
import pandas as pd
from scipy.optimize import minimize
Selected = (42.5,40.5,36.5,36.3,32.3,32.2,31,29.2,28.7,27.8,25.1,25,23.9,23.5,23.1,22.6,21.9,21.4,21.2,21,18.4,17.3,17,16.7,16.3,16.2,16.1,15.8,14.5,14,13.1,13,12.8,12.4,12,11.4,11.3,11,10.5,10.4,10.2,9.8,9.7,9.6,9.2,9.2,9.1,8.9,8.9,8.7,8.6,8.3,8,8,7.9,7.8,7.3,7.3,7.2,7.1,6.8,6.8,6.8,6.7,6.5,6.3,6.1,6,5.9,5.6,5.6,5.4,5.3,5.2,5.1,5.1,5,4.9,4.8,4.8,4.8,4.7,4.7,4.6,4.5,4.5,4.4,4.4,4.3,4.2)
Select = (1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
data_df = pd.DataFrame(data={'Select':Select,'Selected':Selected})
def MaxFunc(range):
data_df['Select'] = range
y = data_df[data_df['Select']==1]['Selected'].sum()
return 1500 - y
def PCount(range):
data_df['Select'] = range
y = data_df[data_df['Select']==1]['Select'].count()
return 15 - y
def BinCheck(range):
Flag = 0
for x in range:
if (x != 0) & (x != 1):
Flag = 1
return Flag
con1={'type': 'eq','fun':PCount}
con2={'type': 'eq','fun':BinCheck}
constraints = [con1,con2]
Hedge = minimize(MaxFunc,Select,constraints=constraints)