forked from ccccourse/alg111a
-
Notifications
You must be signed in to change notification settings - Fork 0
HW1.md
derek120432 edited this page Jan 3, 2023
·
2 revisions
不是原創:看懂並改數據。
import numpy as np
import cvxpy as cp
n=5 #設置目標函數中變量個數
c=np.array([7,8,2,9,6]) #輸入目標函數的係數
a=np.array([[5,7,9,2,1],[18,4,-9,10,12],[4,7,3,8,5],[5,13,16,3,-7]]) #輸入約束條件的係數矩陣
b=np.array([250,285,211,315]) # 輸入b值
x=cp.Variable(n,integer=True)
objective=cp.Maximize(cp.sum(c*x))
constriants=[0<=x,a*x<=b]
prob=cp.Problem(objective,constriants) #求解問題
resluts=prob.solve(solver=cp.CPLEX) #這裡solver必须使用cp.CPLEX,否則計算不出來,而CPLEX需要pip intall CPLEX
print(prob.value)
print(x.value)
結果
260.0
[ 8. 20. 1. 2. 4.]