【发布时间】:2021-09-28 15:34:35
【问题描述】:
我用一个卷数组和一个成本数组定义了 CVXPY 问题以匹配每个卷。这个问题有 192 个变量和我定义的 3 个约束。
我的目标是最大限度地降低此问题的成本以交付特定数量并避免多次收到0, 1, 0, 1。
我当前的输出可能如下所示:
[0, 0, 1, 1, 0, 1... 0, 1, 0, 1]
理想的解决方案是避免金额。所以如果选择决定一个点是1,那么接下来的2个点应该是0。如下:
[0, 0, 1, 1, 0, 0... 0, 1, 0, 0]
我不确定如何编写这样的约束来将我的选择包含在我当前编程的问题中,如下所示:
import cvxpy as cp
import numpy as np
# Volume and cost
full_cost = [[0, data] for data in [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45,0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4]]
cost_ = np.array(full_cost)
ex = np.array([[0, 17100] for data in [i for i in range(0, 96)]])
# Minimum volume required
v_min = 300000
# Selection variable
selection = cp.Variable(shape=ex.shape, boolean=True)
# Constraints
assignment_constraint = cp.sum(selection,axis=1) == 1
volume_= cp.sum(cp.multiply(ex,selection))
volume_constraint = volume_ >= v_min
cost_constraint = cp.sum(cp.multiply(cost_, selection))
constraints = [assignment_constraint, volume_constraint, cost_constraint]
cost_ = cp.sum(cp.multiply(cost_,selection))
# Problem definition
assign_problem = cp.Problem(cp.Minimize(cost_), constraints)
assign_problem.solve(solver=cp.CPLEX, verbose=True)
# Find solution in ex variable
assignments = [np.where(r==1)[0][0] for r in selection.value]
c = [ ex[i][assignments[i]] for i in range(len(assignments)) ]
best_volume = np.sum(np.multiply(ex,selection.value))
best_cost = np.sum(np.multiply(cost_,selection.value))
print(best_cost)
print(c)
我认为约束应该基于我的选择变量,但我正在努力了解如何将其作为约束包含在内。
【问题讨论】:
-
您要排除的究竟是什么?子序列 0,1,0,1 ?描述有点混乱,因为后来你说“在 1 之后应该有两个零”,这意味着别的东西。
-
嗨,@MichalAdamaszek,很抱歉造成这种混乱。我希望防止这样的模式,如果一个选择是 1,接下来的两个选择随后是 0 或“关闭”。这将允许泵冷却。目前我的输出可能看起来像
0, 1, 0, 1,这是不可取的,因为这将是多个启动/停止 (1,0)。我的理想解决方案可能类似于0, 1, 1, 0或0, 1, 0, 0,以确保我们不会损坏电机。
标签: python mathematical-optimization linear-programming cvxpy