在 CyLP 中初始化整数变量
Initialize integer variables in CyLP
我目前正在使用 CyLP
package in Python for mixed-integer linear programming. However, I'm not able to initialize integer variables. According to the documentation,我已将 isInt = True
传递给 addVariable
方法,但它什么也没做。
我的程序的一个最小示例如下所示。 x = y = 1
时最优值应该是2,结果却不尽如人意
import cylp
from cylp.cy import CyClpSimplex
print(cylp.__version__) # 0.91.0
s = CyClpSimplex()
x = s.addVariable('x', 1, isInt = True)
y = s.addVariable('y', 1, isInt = True)
s += x >= 0.5
s += y >= 0.7
s.objective = x + y
s.optimizationDirection = 'min'
s.primal()
# Clp3002W Empty problem - 0 rows, 2 columns and 0 elements
# Clp0000I Optimal - objective value 1.2
x_opt = s.primalVariableSolution['x'][0]
y_opt = s.primalVariableSolution['y'][0]
print(x_opt, y_opt) # 0.5, 0.7
在CyLP
中还有其他方法可以初始化整型变量吗?或者我遗漏了一些关于 addVariable
方法的信息?
顺便问一下,我想知道 Clp3002W Empty problem
在 s.primal()
的输出中意味着什么。
提前致谢。
这有点意思。
Clp 是一个 Linear-Programming 求解器,而您正在尝试执行 Mixed-Integer-Programming。
您将需要使用构建在 Clp(同一个人/同一个人)之上的 Cbc。我认为,人们可以认识到其中大部分是 one-man 几十年来的项目(不直观的设计;也许)。
是的,CyLp是基于Cbc的,但是使用还是要小心!
假设您的 variable-definitions 没问题(未检查),您将需要做一些事情 like:
# model = CyClpSimplex()
# ...
cbcModel = model.getCbcModel() # Clp -> Cbc model / LP -> MIP
status = cbcModel.solve() #-> "Call CbcMain. Solve the problem
# "using the same parameters used
# "by CbcSolver."
# This deviates from cylp's docs which are sparse!
# -> preprocessing will be used and is very important!
另见 this wrapper:
if data[s.BOOL_IDX] or data[s.INT_IDX]:
# MIP
# Convert model
cbcModel = model.getCbcModel()
# cylp: /cylp/cy/CyCbcModel.pyx#L134
# Call CbcMain. Solve the problem using the same parameters used by
# CbcSolver. Equivalent to solving the model from the command line
# using cbc's binary.
cbcModel.solve()
status = cbcModel.status
else:
# LP
# cylp: /cylp/cy/CyClpSimplex.pyx
# Run CLP's initialSolve. It does a presolve and uses primal or dual
# Simplex to solve a problem.
status = model.initialSolve()
我很久以前就写过这些东西,所以我不能给你确切的细节(这些电话和评论背后的背景)。
但总的来说:很难掌握 Cbc 内部到底发生了什么(尽管我认为在改进 API 方面做了一些工作;可能 NOT 反映了虽然在 Cylp 中)。例如:以与 Cbc 可执行文件行为相同的方式从代码使用 Cbc 并非易事。
By the way, I wonder what Clp3002W Empty problem means in the output of s.primal().
Clp3002W Empty problem - 0 rows, 2 columns and 0 elements
我的解读:
您的(转换后的)模型没有任何约束。您添加了约束以强制执行 variable-bounds,但是 Clp/Cbc 已经足够先进(非常非常先进),可以将这些约束转换为 variable-bounds(不再有约束! ),在 Simplex-Routine.
中进行特殊处理
0 rows = 0 constraints
0 elements = 0 non-zero elements in your constraint-matrix of size
rows * cols = 0 * 2 = 0
我目前正在使用 CyLP
package in Python for mixed-integer linear programming. However, I'm not able to initialize integer variables. According to the documentation,我已将 isInt = True
传递给 addVariable
方法,但它什么也没做。
我的程序的一个最小示例如下所示。 x = y = 1
时最优值应该是2,结果却不尽如人意
import cylp
from cylp.cy import CyClpSimplex
print(cylp.__version__) # 0.91.0
s = CyClpSimplex()
x = s.addVariable('x', 1, isInt = True)
y = s.addVariable('y', 1, isInt = True)
s += x >= 0.5
s += y >= 0.7
s.objective = x + y
s.optimizationDirection = 'min'
s.primal()
# Clp3002W Empty problem - 0 rows, 2 columns and 0 elements
# Clp0000I Optimal - objective value 1.2
x_opt = s.primalVariableSolution['x'][0]
y_opt = s.primalVariableSolution['y'][0]
print(x_opt, y_opt) # 0.5, 0.7
在CyLP
中还有其他方法可以初始化整型变量吗?或者我遗漏了一些关于 addVariable
方法的信息?
顺便问一下,我想知道 Clp3002W Empty problem
在 s.primal()
的输出中意味着什么。
提前致谢。
这有点意思。
Clp 是一个 Linear-Programming 求解器,而您正在尝试执行 Mixed-Integer-Programming。
您将需要使用构建在 Clp(同一个人/同一个人)之上的 Cbc。我认为,人们可以认识到其中大部分是 one-man 几十年来的项目(不直观的设计;也许)。
是的,CyLp是基于Cbc的,但是使用还是要小心!
假设您的 variable-definitions 没问题(未检查),您将需要做一些事情 like:
# model = CyClpSimplex()
# ...
cbcModel = model.getCbcModel() # Clp -> Cbc model / LP -> MIP
status = cbcModel.solve() #-> "Call CbcMain. Solve the problem
# "using the same parameters used
# "by CbcSolver."
# This deviates from cylp's docs which are sparse!
# -> preprocessing will be used and is very important!
另见 this wrapper:
if data[s.BOOL_IDX] or data[s.INT_IDX]:
# MIP
# Convert model
cbcModel = model.getCbcModel()
# cylp: /cylp/cy/CyCbcModel.pyx#L134
# Call CbcMain. Solve the problem using the same parameters used by
# CbcSolver. Equivalent to solving the model from the command line
# using cbc's binary.
cbcModel.solve()
status = cbcModel.status
else:
# LP
# cylp: /cylp/cy/CyClpSimplex.pyx
# Run CLP's initialSolve. It does a presolve and uses primal or dual
# Simplex to solve a problem.
status = model.initialSolve()
我很久以前就写过这些东西,所以我不能给你确切的细节(这些电话和评论背后的背景)。
但总的来说:很难掌握 Cbc 内部到底发生了什么(尽管我认为在改进 API 方面做了一些工作;可能 NOT 反映了虽然在 Cylp 中)。例如:以与 Cbc 可执行文件行为相同的方式从代码使用 Cbc 并非易事。
By the way, I wonder what Clp3002W Empty problem means in the output of s.primal().
Clp3002W Empty problem - 0 rows, 2 columns and 0 elements
我的解读:
您的(转换后的)模型没有任何约束。您添加了约束以强制执行 variable-bounds,但是 Clp/Cbc 已经足够先进(非常非常先进),可以将这些约束转换为 variable-bounds(不再有约束! ),在 Simplex-Routine.
中进行特殊处理0 rows = 0 constraints
0 elements = 0 non-zero elements in your constraint-matrix of size
rows * cols = 0 * 2 = 0