gpt4 book ai didi

python - CPLEX 二次 objective-c PLEX 错误 1017 : Not available for mixed-integer problems

转载 作者:行者123 更新时间:2023-12-01 09:10:02 28 4
gpt4 key购买 nike

我正在尝试使用 cplex 求解以下线性程序:

def generate_linear_program(self):
problem = cplex.Cplex()
problem.objective.set_sense(problem.objective.sense.minimize)
for index, track in enumerate(self.tracks):
tokens = track['track'].split('_')
problem.variables.add(names = ['c' + str(tokens[1])], ub = [1.0], types = ['C'])
problem.variables.add(names = ['e' + str(index) for index, param in enumerate(self.params)],
types = ['C'] * len(self.params),
ub = [param['c'] - param['u'] * param['r'] for param in self.params],
lb = [param['c'] - param['u'] * param['r'] - param['c'] * sum(param['tracks'][track] for track in param['tracks']) for param in self.params])
problem.variables.add(names = ['l' + str(index) for index, param in enumerate(self.params)],
#obj = [1.0] * len(self.params),
types = ['C'] * len(self.params))
problem.objective.set_quadratic([0.0] * len(self.tracks) + [1.0] * len(self.params) + [0.0] * len(self.params))
# add some linear constraints here
problem.solve()

当我打电话solve()时CPLEX 提示错误消息 CPLEX Error 1017: Not available for mixed-integer problems 。如果我删除上面的二次目标,并通过取消上面注释的代码行( obj = [1.0] * len(self.params), )的注释来添加线性目标,则它可以正常工作。

堆栈跟踪:

File "/share/src/python/kmer/programming.py", line 373, in solve
problem.solve()
File "/home/user/local/cplex/lib/python/cplex/__init__.py", line 998, in solve
_proc.qpopt(self._env._e, self._lp)
File "/home/user/local/cplex/lib/python/cplex/_internal/_procedural.py", line 499, in qpopt
check_status(env, status)
File "/home/user/local/cplex/lib/python/cplex/_internal/_procedural.py", line 171, in __call__
raise CplexSolverError(error_string, env, status)
cplex.exceptions.errors.CplexSolverError: CPLEX Error 1017: Not available for mixed-integer problems.

为了更好地了解这里发生的情况,当目标是二次的时,我试图最小化一些误差项的平方和。当目标变为线性时,我将最小化这些项的绝对值之和。名称以 e 开头的变量是错误项,l s 将通过这些约束变成它们的绝对值:

    for index, params in enumerate(self.params):
problem.linear_constraints.add(
lin_expr = [cplex.SparsePair(
ind = [len(self.tracks) + len(self.params) + index, len(self.tracks) + index],
val = [1.0, 1.0],
)],
rhs = [0],
senses = ['G']
)
problem.linear_constraints.add(
lin_expr = [cplex.SparsePair(
ind = [len(self.tracks) + len(self.params) + index, len(self.tracks) + index],
val = [1.0, -1.0],
)],
rhs = [0],
senses = ['G']
)

l<index>在存在二次目标的情况下,变量实际上是无用的。

还有其他线性约束,我不能在这里包含,但它们绝对不是问题的原因,因为以下两个原因:

  1. 线性物镜存在时不会出现问题
  2. 当我使用二次目标注释掉它们时,我仍然遇到相同的错误。

我在这里缺少什么?

最佳答案

很容易错过 Cplex.variables.add 文档中的以下注释:

If types is specified, the problem type will be a MIP, even if all variables are specified to be continuous.

如果删除可选的 types来自您对 Cplex.variables.add 的调用的争论,问题应该消失。例如,代替:

problem.variables.add(names = ['c' + str(tokens[1])], ub = [1.0], types = ['C'])

用途:

problem.variables.add(names = ['c' + str(tokens[1])], ub = [1.0])

当你有一个线性目标时它起作用的原因是它被传递到 CPXmipopt ,作为方便,失败后 CPXlpoptCPXERR_NOT_FOR_MIP (错误 1017)。然而,当我们调用CPXqpopt时,这个逻辑不适用。

关于python - CPLEX 二次 objective-c PLEX 错误 1017 : Not available for mixed-integer problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com