2016-10-15 53 views
0

我在索引每一個問題解決了使用linprog線性規劃問題意甲for循環:如何保存linprog解決方案的部分

from scipy.optimize import linprog 
for i in range(1,N): 
    sol[i] = linprog(coa3[N][0], A_ub = coa4[i], b_ub = chvneg[i], options=  {"disp": True}) 

我想在列表中保存(還收錄了i)函數最小化結果和顯示變量值的數組。我想我需要在for循環中添加像minfval [i] = ???和xval [i] = ???,但實際上我不知道如何從linprog提供的結果中提取這些值。有什麼建議麼?非常感謝。

回答

0

考慮閱讀docs,因爲它很清楚地解釋了linprog返回的結果。

好處是,您實際上已將這些值與您的代碼一起存儲,因爲您正在存儲整個scipy.optimize.OptimizeResult

它現在只是一個訪問它的事:

from scipy.optimize import linprog 
for i in range(1,N): 
    sol[i] = linprog(coa3[N][0], A_ub = coa4[i], b_ub = chvneg[i], options=  {"disp": True}) 

# iterate over solution-vectors 
for i in range(1,N): 
    print(sol[i].x) 

# iterate over objectives 
for i in range(1,N): 
    print(sol[i].fun) 

您也應該檢查出現場success只是爲了安全起見!

提取的文檔(以上鍊接)中包含什麼linprog的結果:

x : ndarray 
    The independent variable vector which optimizes the linear 
    programming problem. 
fun : float 
    Value of the objective function. 
slack : ndarray 
    The values of the slack variables. Each slack variable corresponds 
    to an inequality constraint. If the slack is zero, then the 
    corresponding constraint is active. 
success : bool 
    Returns True if the algorithm succeeded in finding an optimal 
    solution. 
status : int 
    An integer representing the exit status of the optimization:: 
    0 : Optimization terminated successfully 
    1 : Iteration limit reached 
    2 : Problem appears to be infeasible 
    3 : Problem appears to be unbounded 
nit : int 
    The number of iterations performed. 
message : str 
    A string descriptor of the exit status of the optimization. 
相關問題