运筹系列71:CBC和CLP的python接口Cylp

博客提到Mac升级到15后,gcc的head丢失,导致编译出现诸多bug,建议使用docker镜像解决。还给出读取mps文件并求解的简单示例,以及从头开始构建的复杂示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值