我想根据价格找到最大的每小时交易量。 我编写的程序可以正常工作,但当我尝试添加约束时,如果一天的总和为0;第二天的交易量也必须为0,但这仅适用于连续两天,这意味着如果某天的所有交易量都为0,则第二天的所有交易量都必须为0,但第三天的交易量不能为零。代码如下:
for i in numberOfYears:
df = dfOrig[dfOrig['year'].isin([i])]
dfOut = df[['year','month','day ','weekday','Date']]
df.drop(columns=['year','month','day ','weekday','Date'], inplace = True)
df.reset_index(drop=True, inplace = True)
dfOut.reset_index(drop=True, inplace = True)
rowsCols = df.shape
rows = rowsCols[0]
cols = rowsCols[1]
A = list(df.values.flatten())
A1 = np.matrix(A)
n = len(A)
# Construct the problem.
x = cp.Variable(n)
#list of days
days = [None]*rows
for i in range(rows):
days[i] = [None]*cols
for j in range(cols):
days[i][j] = x[j]
#objective = cp.Maximize(cp.sum(A*x)) #uncomment to run on python 2.7
objective = cp.Maximize(cp.sum_entries(A1*x))
constraints = [0 <= x,
x <= 300,
cp.sum_entries(x) <= 413000 #cp.sum(x) <= 413000 #uncomment to run on python 2.7
]
for i in range(rows):
for j in range(cols):
if i ==0:
constraints += [x[j] >= 0.25*cp.pos(x[0:(cols-1)])]
else:
if i>2:
exp3= x[cols*(i-1):cols*(i-1)+(cols-1)]
exp2= x[cols*(i-2):cols*(i-2)+(cols-1)]
exp1 = x[cols*i:cols*i+(cols-1)]
constraints += [cp.sum_entries(exp1)<=cp.sum_entries(exp3)]
constraints += [x[j+cols*i] >= 0.25*cp.pos(x[cols*i:cols*i+(cols)])]
#constraints += [x[j+cols*i] >= 0.25*cp.pos(x[cols*i:cols*i+(cols-1)])]
#constraints += cp.sum(days[i])>=75
prob = cp.Problem(objective, constraints)
# The optimal objective value is returned by `prob.solve()`.
result = prob.solve(feastol=0.1)
我对声明有疑问
constraints += [cp.sum_entries(exp1)<=cp.sum_entries(exp3)]
此声明不限于连续两天