cvxpy 示例代码

2021/7/17 23:20:02

本文主要是介绍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 示例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程