Changeset 829


Ignore:
Timestamp:
09/18/15 17:50:42 (8 months ago)
Author:
mmckerns
Message:

replacing qld with mystic.diffev in test_smo1.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples_other/test_smo1.py

    r776 r829  
    22# 
    33# Author: Patrick Hung (patrickh @caltech) 
     4# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 
    45# Copyright (c) 1997-2015 California Institute of Technology. 
    56# License: 3-clause BSD.  The full license text is available at: 
     
    1213 
    1314from numpy import * 
    14 import qld 
    1515import pylab 
    1616from 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) 
     20def objective(x, H, f): 
     21    return 0.5 * dot(dot(x,H),x) + dot(f,x) 
    1722 
    1823c1 = array([[0., 0.],[1., 0.],[ 0.2, 0.2],[0.,1.]]) 
     
    2227XX = concatenate([c1,-c2]) 
    2328nx = XX.shape[0] 
     29 
     30# quadratic and linear terms of QP 
    2431Q = KernelMatrix(XX) 
    2532b = -1 * ones(nx) 
     
    3037Beq = array([0]) 
    3138lb = zeros(nx) 
    32 ub = zeros(nx) + 99999 
     39ub = 99999 * ones(nx) 
    3340 
    34 # first, use a general purpose IQP solver. 
    35 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 
    36 print alpha 
     41from mystic.symbolic import linear_symbolic, solve, \ 
     42     generate_solvers as solvers, generate_constraint as constraint 
     43constrain = linear_symbolic(Aeq,Beq) 
     44constrain = constraint(solvers(solve(constrain,target=['x0']))) 
     45 
     46from mystic import supressed 
     47@supressed(1e-5) 
     48def conserve(x): 
     49    return constrain(x) 
     50 
     51#from mystic.monitors import VerboseMonitor 
     52#mon = VerboseMonitor(1) 
     53 
     54from mystic.solvers import diffev 
     55alpha = 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 
     59print 'solved x: ', alpha 
     60print "constraint A*x == 0: ", inner(Aeq, alpha) 
     61print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 
     62 
    3763 
    3864# let's play. We will need to bootstrap the SMO with an initial 
     
    159185        break 
    160186 
     187X = concatenate([c1,c2]) 
    161188y = Aeq.flatten() 
    162189p,a,b,c = f, lb, ub, 0 
    163 X = concatenate([c1,c2]) 
    164190QP_smo(Q, p, a, b, c, y, 0.01, a, X) 
    165191 
Note: See TracChangeset for help on using the changeset viewer.