习题4.4代码
import cvxpy as cp
import numpy as np
import pandas as pd
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='cm'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
x = cp.Variable(2, integer=True)
c = np.array([[0, 5], [6, 2], [1, 1]])
b = np.array([15, 24, 5]).T
p = np.array([2, 1])
prob = cp.Problem(cp.Maximize(p @ x), [c@x <= b, x >= 0])
prob.solve(solver='GLPK_MI')
print(f'最优解为:{x.value}'), print(f'最优值为:{prob.value}')