【问题标题】:How do I prevent multiple starts/stops of my selection?如何防止多次启动/停止我的选择?
【发布时间】: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, 00, 1, 0, 0,以确保我们不会损坏电机。

标签: python mathematical-optimization linear-programming cvxpy


【解决方案1】:

如果我理解正确,您似乎想强加条件

if   x[i]==1 and x[i+1]==0   then   x[i+2]==0

对于二进制变量x。相当于

x[i+2] <= 1 - x[i] + x[i+1]

【讨论】:

  • 感谢您的回答。我有几点想问的。我会定义二进制变量吗:x = cp.Variable()?以及如何将其实现到我的选择变量中,该变量基本上控制我的输出。
  • 我想应该是名为selection的变量在我的符号中扮演x的角色。
  • 我试图用我的变量selection 替换x,如下所示:selection[i+2] &lt;= 1 - selection[i] + selection[i+1]。虽然问题是现在我被退回IndexError: Index 97 is out of bounds for axis 0 with size 96.。我正在使用固定的数组大小,所以我想知道是否有解决方法。
猜你喜欢
  • 2013-02-13
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多