欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python二次規(guī)劃和線性規(guī)劃使用實(shí)例

 更新時(shí)間:2019年12月09日 10:56:41   投稿:yaominghui  
這篇文章主要介紹了Python二次規(guī)劃和線性規(guī)劃使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Python二次規(guī)劃和線性規(guī)劃使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

對(duì)于二次規(guī)劃(quadratic programming)和線性規(guī)劃(Linear Programming)問(wèn)題

MATLAB里是有quadprog函數(shù)可以直接用來(lái)解決二次規(guī)劃問(wèn)題的,linprog函數(shù)來(lái)解決線性規(guī)劃問(wèn)題。Python中也有很多庫(kù)用來(lái)解決,對(duì)于二次規(guī)劃有CVXOPT, CVXPY, Gurobi, MOSEK, qpOASES 和 quadprog; 對(duì)于線性規(guī)劃有Gurobi,PuLP, cvxopt。

目前發(fā)現(xiàn)quadprog進(jìn)行pip install quadprog不成功,而cvxopt成功了,就先說(shuō)cvxopt的使用。

安裝

conda install -c conda-forge cvxopt

安裝非常順利

使用

cvxopt有自己的matrix格式,因此使用前得包裝一下

對(duì)于二次規(guī)劃:

def cvxopt_solve_qp(P, q, G=None, h=None, A=None, b=None):
  P = .5 * (P + P.T) # make sure P is symmetric
  args = [cvxopt.matrix(P), cvxopt.matrix(q)]
  if G is not None:
    args.extend([cvxopt.matrix(G), cvxopt.matrix(h)])
    if A is not None:
      args.extend([cvxopt.matrix(A), cvxopt.matrix(b)])
  sol = cvxopt.solvers.qp(*args)
  if 'optimal' not in sol['status']:
    return None
  return np.array(sol['x']).reshape((P.shape[1],))

對(duì)于線性規(guī)劃:

def cvxopt_solve_lp(f, A, b):
  #args = [cvxopt.matrix(f), cvxopt.matrix(A), cvxopt.matrix(b)]
  #cvxopt.solvers.lp(*args)
  sol = cvxopt.solvers.lp(cvxopt.matrix(f), cvxopt.matrix(A), cvxopt.matrix(b))
  return np.array(sol['x']).reshape((f.shape[0],))

參考:

Quadratic Programming in Python

Linear Programming in Python with CVXOPT

cvxopt.org

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論