[英]How to set up a linear programming model (transportation problem) using python/PuLp
[英]How do I model this linear programming problem in Python?
我的任務是使用 Python 對 Dantzig Selector 進行編程,但我沒有得到任何指導,並且在線性編程或數據科學方面沒有太多經驗。 我在 LP 模塊手冊或本網站的其他問題中找不到我需要的信息。
這就是問題。 我正在尋找列向量 ˆβ。 抱歉,我的程序這部分沒有任何代碼,因為我不知道如何解決這個問題。 我嘗試了幾種方法,但都沒有正確反映問題,所以我拒絕並刪除了它們。
min||ˆβ||l1 st ||xT(y-xˆβ(||l(inf) <= δ)
^β 是一個 kx1 列向量和我正在尋找的 Dantzig 選擇器。
到目前為止,這是我的工作代碼。 數據值都只是樣本/占位符。 我已經准備好了 X、y 和一些 δ 值。 但是,我找不到合適的 LP 函數來給我 ˆβ。
import numpy as np
import random
import math
#n = no. runs = 5
n = 5
#k = no. variables = 23
k = 23
#y = vector of observations/responses (nx1, binary decisions)
y = np.array([[1],
[0],
[0],
[1],
[0]])
#X = predictor/sample matrix (nxk)
X = np.array([[1.1, 0, 0.7, 0.8, 0.9, 0.2, 0.3, 0.5, 0.2, 0.2, 1.2, 1.1, 0.5, 0.5, 0.7, 1.2, 1.3, 0.8, 0.9, 1.7, 1.2, 1.9, 0.9],
[0.3, 0.1, 0.7, 0.4, 0.9, 0.9, 0.1, 0.8, 0.1, 0.2, 1.1, 0, 0.9, 0.4, 1.4, 1.4, 0.1, 0.5, 1.8, 1.6, 1.2, 1.8, 0.3],
[0.1, 0.1, 0.3, 0.9, 0.7, 0.8, 0, 0.7, 0.8, 0.2, 1.1, 1.1, 0.5, 0.5, 0.8, 1.5, 0.2, 0.5, 1.6, 1.5, 1.2, 1.7, 0.5],
[1.2, 0.2, 0.9, 0.8, 0.6, 0.2, 0.3, 0.5, 0.3, 0.2, 1.2, 1.1, 0.5, 0, 0.7, 1.2, 1.3, 0.8, 0.9, 1.7, 1.2, 1.9, 0.9],
[0.2, 0.1, 0.6, 0, 0.5, 1.1, 0.2, 0.5, 0.9, 0.2, 1.2, 1.1, 0.8, 1.6, 0.5, 1.3, 0.2, 0.5, 1.7, 1.2, 1.2, 1.9, 0.1]])
#estimate missing data (0)
X_row_minima = np.where(X>0,X,X.max()).min(1)
X[X==0] = X_row_minima/2
#unit length normalize X
X = X/np.linalg.norm(X, ord=2, axis=1, keepdims=True)
#standardize y to zero mean
y = y - np.mean(y) / np.std(y)
#transpose X (kxn)
Xt = np.transpose(X)
#solve d0
Xty = np.matmul(Xt,y)
d0 = max(abs(Xty))
#generate 100 evenly-spaced d values
d = np.linspace(0, d0, 100)
這是我在這個網站上的第一篇文章。 與其他人相比,我為帖子中缺乏細節而道歉。
我不確定我是否有這個權利,因為我以前不必弄清楚但澤選擇器。 如果可能,我建議使用您知道期望得到答案的數據集進行測試。 據我所知,你的問題是這樣的。
minimize: sum(u)
subject to: -u_i <= beta_i for all i in len(u)
u_i >= beta_i for all i in len(u)
[X^T (y - X beta)]_j <= delta_j for all j in len(y)
[X^T (y - X beta)]_j >= -delta_j for all j in len(y)
我認為在cvxpy
編寫這個非常簡單。 如果我離開了,那么希望它能讓你朝着正確的方向前進。 使用X, y, k
:
import cvxpy as cp
beta = cp.Variable((k,1))
u = cp.Variable((k,1))
delta = 1
objective = cp.Minimize(cp.sum(u))
constraints = [beta >= -u,
beta <= u,
Xt @ (y - X @ beta) <= delta,
Xt @ (y - X @ beta) >= -delta]
prob = cp.Problem(objective, constraints)
prob.solve(verbose=True)
print("Problem status: ", prob.status)
print("Optimal value", prob.value)
print("Beta values", beta.value)
我的結果輸出:
===============================================================================
CVXPY
v1.1.15
===============================================================================
(CVXPY) Oct 12 11:00:50 PM: Your problem has 46 variables, 4 constraints, and 0 parameters.
(CVXPY) Oct 12 11:00:50 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Oct 12 11:00:50 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Oct 12 11:00:50 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Oct 12 11:00:50 PM: Compiling problem (target solver=ECOS).
(CVXPY) Oct 12 11:00:50 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> ECOS
(CVXPY) Oct 12 11:00:50 PM: Applying reduction Dcp2Cone
(CVXPY) Oct 12 11:00:50 PM: Applying reduction CvxAttr2Constr
(CVXPY) Oct 12 11:00:50 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Oct 12 11:00:50 PM: Applying reduction ECOS
(CVXPY) Oct 12 11:00:50 PM: Finished problem compilation (took 3.027e-03 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Oct 12 11:00:50 PM: Invoking solver ECOS to obtain a solution.
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -1.596e+02 +3e+02 2e-01 6e-01 1e+00 4e+00 --- --- 1 1 - | - -
1 +4.462e+00 -6.924e+00 +4e+01 1e-02 6e-02 2e-01 4e-01 0.9228 4e-02 0 0 0 | 0 0
2 +4.980e-01 -8.219e-01 +4e+00 1e-03 8e-03 3e-02 5e-02 0.9010 3e-02 0 0 0 | 0 0
3 +3.673e-02 -4.271e-02 +3e-01 9e-05 5e-04 1e-03 3e-03 0.9630 2e-02 0 0 0 | 0 0
4 +4.054e-04 -4.749e-04 +3e-03 1e-06 5e-06 1e-05 3e-05 0.9890 1e-04 0 0 0 | 0 0
5 +4.490e-06 -5.272e-06 +3e-05 1e-08 6e-08 2e-07 4e-07 0.9890 1e-04 0 0 0 | 0 0
6 +4.972e-08 -5.852e-08 +4e-07 1e-10 6e-10 2e-09 4e-09 0.9890 1e-04 0 0 0 | 0 0
7 +5.507e-10 -6.496e-10 +4e-09 1e-12 7e-12 2e-11 4e-11 0.9890 1e-04 0 0 0 | 0 0
OPTIMAL (within feastol=6.9e-12, reltol=-nan(ind), abstol=4.1e-09).
Runtime: 0.002074 seconds.
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Oct 12 11:00:50 PM: Problem status: optimal
(CVXPY) Oct 12 11:00:50 PM: Optimal value: 5.507e-10
(CVXPY) Oct 12 11:00:50 PM: Compilation took 3.027e-03 seconds
(CVXPY) Oct 12 11:00:50 PM: Solver (including time spent in interface) took 2.358e-03 seconds
Problem status: optimal
Optimal value 5.507030257315668e-10
Beta values [[ 1.43866286e-11]
[ 2.52691994e-12]
[ 1.26496527e-11]
[ 1.25889902e-11]
[ 1.32324154e-11]
[ 9.34898130e-12]
[ 4.19460380e-12]
[ 1.07283774e-11]
[ 7.54676427e-12]
[ 3.94492714e-12]
[ 1.68905798e-11]
[ 1.61644206e-11]
[ 1.09177395e-11]
[ 9.11945636e-12]
[ 1.38073390e-11]
[ 1.51133406e-11]
[ 1.59259478e-11]
[ 1.24992988e-11]
[ 1.10965990e-11]
[ 1.50106084e-11]
[ 1.66261028e-11]
[-1.92542442e-13]
[ 1.24499858e-11]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.