Changeset 829
- Timestamp:
- 09/18/15 17:50:42 (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples_other/test_smo1.py
r776 r829 2 2 # 3 3 # Author: Patrick Hung (patrickh @caltech) 4 # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 4 5 # Copyright (c) 1997-2015 California Institute of Technology. 5 6 # License: 3-clause BSD. The full license text is available at: … … 12 13 13 14 from numpy import * 14 import qld15 15 import pylab 16 16 from mystic.svctools import * 17 18 # a common objective function for solving a QP problem 19 # (see http://www.mathworks.com/help/optim/ug/quadprog.html) 20 def objective(x, H, f): 21 return 0.5 * dot(dot(x,H),x) + dot(f,x) 17 22 18 23 c1 = array([[0., 0.],[1., 0.],[ 0.2, 0.2],[0.,1.]]) … … 22 27 XX = concatenate([c1,-c2]) 23 28 nx = XX.shape[0] 29 30 # quadratic and linear terms of QP 24 31 Q = KernelMatrix(XX) 25 32 b = -1 * ones(nx) … … 30 37 Beq = array([0]) 31 38 lb = zeros(nx) 32 ub = zeros(nx) + 9999939 ub = 99999 * ones(nx) 33 40 34 # first, use a general purpose IQP solver. 35 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 36 print alpha 41 from mystic.symbolic import linear_symbolic, solve, \ 42 generate_solvers as solvers, generate_constraint as constraint 43 constrain = linear_symbolic(Aeq,Beq) 44 constrain = constraint(solvers(solve(constrain,target=['x0']))) 45 46 from mystic import supressed 47 @supressed(1e-5) 48 def conserve(x): 49 return constrain(x) 50 51 #from mystic.monitors import VerboseMonitor 52 #mon = VerboseMonitor(1) 53 54 from mystic.solvers import diffev 55 alpha = diffev(objective, zip(lb,ub), args=(H,f), npop=nx*3, gtol=200, \ 56 # itermon=mon, \ 57 ftol=1e-8, bounds=zip(lb,ub), constraints=conserve, disp=1) 58 59 print 'solved x: ', alpha 60 print "constraint A*x == 0: ", inner(Aeq, alpha) 61 print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 62 37 63 38 64 # let's play. We will need to bootstrap the SMO with an initial … … 159 185 break 160 186 187 X = concatenate([c1,c2]) 161 188 y = Aeq.flatten() 162 189 p,a,b,c = f, lb, ub, 0 163 X = concatenate([c1,c2])164 190 QP_smo(Q, p, a, b, c, y, 0.01, a, X) 165 191
Note: See TracChangeset
for help on using the changeset viewer.