运筹系列71:CBC和CLP的python接口Cylp
Posted IE06
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运筹系列71:CBC和CLP的python接口Cylp相关的知识,希望对你有一定的参考价值。
1. 介绍
mac升级15之后把gcc的head给搞掉了,编译一堆bug,建议直接用docker镜像。
1. 示例
来个简单的例子(读取mps文件并求解)
>>> from cylp.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.initialSolve()
'optimal'
再来个复杂的例子(从头开始构建)
import numpy as np
from CyLP.cy import CyClpSimplex
from CyLP.py.modeling.CyLPModel import CyLPArray
s = CyClpSimplex()
# Add variables
x = s.addVariable('x', 3)
y = s.addVariable('y', 2)
# Create coefficients and bounds
A = np.matrix([[1., 2., 0],[1., 0, 1.]])
B = np.matrix([[1., 0, 0], [0, 0, 1.]])
D = np.matrix([[1., 2.],[0, 1]])
a = CyLPArray([5, 2.5])
b = CyLPArray([4.2, 3])
x_u= CyLPArray([2., 3.5])
# Add constraints
s += A * x <= a
s += 2 <= B * x + D * y <= b
s += y >= 0
s += 1.1 <= x[1:3] <= x_u
# Set the objective function
c = CyLPArray([1., -2., 3.])
s.objective = c * x + 2 * y.sum()
# Solve using primal Simplex
s.primal()
print s.primalVariableSolution['x']
以上是关于运筹系列71:CBC和CLP的python接口Cylp的主要内容,如果未能解决你的问题,请参考以下文章