cvxpy 示例代码

import cvxpy as cp
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

data = pd.read_csv(‘Pdata.csv‘, header=None, encoding=‘utf-8‘)
darray = data.values

c = darray[:-1, :-1]
e = darray[:-1, -1].reshape(-1, 1)
d = darray[-1, :-1].reshape(1, -1)

x = cp.Variable((6, 8))

obj = cp.Minimize(cp.sum(cp.multiply(c, x))) # c, x 同形状, 这里是矩阵的点乘

# x 的每一行求列和, 均小于e的每一行; x 的每一列求行和, 等于d的每一行, x 的每一个元素均非负

con = [cp.sum(x, axis=1, keepdims=True) <= e,
       cp.sum(x, axis=0, keepdims=True) == d,
       x >= 0]

prob = cp.Problem(obj, con)

prob.solve(solver=‘GLPK_MI‘, verbose=True)

print(‘最优值为: {0}\n最优解为{1}‘.format(prob.value, x.value))


cvxpy 示例代码

上一篇:网页尺寸规范


下一篇:PHP正则表达式入门教程