pythonによる2次計画法
pythonで2次計画法を解くのにCVXOPTを使った。
Rのquadprogと違って、等式制約をそのまま使えるらしい。
例題はこちら↓
http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html
#!/usr/bin/python # -*- coding: utf-8 -*- from cvxopt import matrix from cvxopt import solvers Q = 2*matrix([ [2, .5], [.5, 1] ]) p = matrix([1.0, 1.0]) G = matrix([[-1.0,0.0],[0.0,-1.0]]) h = matrix([0.0,0.0]) A = matrix([1.0, 1.0], (1,2)) b = matrix(1.0) sol=solvers.qp(Q, p, G, h, A, b) print sol['x']
・Qが目的関数の2次の係数行列
・pが目的関数の1次の係数ベクトル
・G,hが不等式制約Gx≦h
・A,Bが等式制約Ax=b
結果はこちら。
pcost dcost gap pres dres 0: 1.8889e+00 7.7778e-01 1e+00 3e-16 2e+00 1: 1.8769e+00 1.8320e+00 4e-02 1e-16 6e-02 2: 1.8750e+00 1.8739e+00 1e-03 2e-16 5e-04 3: 1.8750e+00 1.8750e+00 1e-05 1e-16 5e-06 4: 1.8750e+00 1.8750e+00 1e-07 3e-16 5e-08 Optimal solution found. [ 2.50e-01] [ 7.50e-01]