如何解决当我使用“ceil”和“sign”功能时,“问题不符合DCP规则”,如何避免这种情况?
我正在尝试解决一个工作分配问题,目标是在满足能力限制的条件下,尽可能将相同的工作分配给同一个人
import cvxpy as cp
import numpy as np
c=np.array([[0,1,1],[0,[1,0],0]
])
d=np.array([[1,[2,2,2],[3,3,3],3]
])
x = cp.Variable((9,3),integer=True)
#obj = cp.Minimize(cp.sum(cp.multiply(c,x)))
x1= cp.multiply(d,x)
x1= cp.vstack((n,x1))
e = cp.diff(x1,k=1,axis=0)
f = cp.abs(e)
g = cp.sign(f)
obj=cp.Minimize(cp.sum(g))
con= [0 <= x,x <= 1,cp.sum(x,axis=1,keepdims=True)==1,cp.sum(cp.sum(cp.multiply(c,x),axis=0))==9]
prob = cp.Problem(obj,con)
prob.solve(solver='CBC')
print("Best value is :",prob.value)
print("Best solution:\n",x.value)
以下是回溯:
Traceback (most recent call last):
File "C:/Users/N000377/Documents/python/Recipe Grouping help update .py",line 33,in <module>
prob.solve(solver='CBC')
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\problems\problem.py",line 459,in solve
return solve_func(self,*args,**kwargs)
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\problems\problem.py",line 939,in _solve
solver,gp,enforce_dpp,verbose)
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\problems\problem.py",line 566,in get_problem_data
solver=solver,gp=gp,enforce_dpp=enforce_dpp)
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\problems\problem.py",line 792,in _construct_chain
enforce_dpp=enforce_dpp)
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\reductions\solvers\solving_chain.py",line 155,in construct_solving_chain
reductions = _reductions_for_problem_class(problem,candidates,gp)
File "C:\Users\N000377\AppData\Roaming\Python\Python37\site-packages\cvxpy\reductions\solvers\solving_chain.py",line 92,in _reductions_for_problem_class
"Problem does not follow DCP rules. Specifically:\n" + append)
cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
sign(abs([[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]] @ var0[1:9,0:3] + -[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]] @ var0[0:8,0:3]))
如果我想使用ceil函数也会发生同样的事情,我的想法是计算job type,或者我们可以创建一个count函数吗?
解决方法
使用 ceil() 或 sign() 函数(几乎)从来都不是一个好主意。它们不可微且不连续(当然也是非线性的)。
您可以在 MIP 模型中模拟天花板函数,如下所示:
y=ceil(x)
大约相当于
y >= x
y <= x+0.999
y integer
我通常使用 1 而不是 0.999,如果 x 已经是整数值,那么让求解器选择最佳值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。