| 1 | // | 
|---|
| 2 | // powell.cc | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Edward Seidl <seidl@janed.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #include <math.h> | 
|---|
| 29 |  | 
|---|
| 30 | #include <util/state/stateio.h> | 
|---|
| 31 | #include <math/optimize/update.h> | 
|---|
| 32 | #include <math/optimize/transform.h> | 
|---|
| 33 | #include <util/keyval/keyval.h> | 
|---|
| 34 |  | 
|---|
| 35 | using namespace sc; | 
|---|
| 36 |  | 
|---|
| 37 | ///////////////////////////////////////////////////////////////////////// | 
|---|
| 38 | // PowellUpdate | 
|---|
| 39 |  | 
|---|
| 40 | static ClassDesc PowellUpdate_cd( | 
|---|
| 41 | typeid(PowellUpdate),"PowellUpdate",1,"public HessianUpdate", | 
|---|
| 42 | create<PowellUpdate>, create<PowellUpdate>, create<PowellUpdate>); | 
|---|
| 43 |  | 
|---|
| 44 | PowellUpdate::PowellUpdate() | 
|---|
| 45 | { | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | PowellUpdate::PowellUpdate(const Ref<KeyVal>&keyval): | 
|---|
| 49 | HessianUpdate(keyval) | 
|---|
| 50 | { | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | PowellUpdate::PowellUpdate(StateIn&s): | 
|---|
| 54 | SavableState(s), | 
|---|
| 55 | HessianUpdate(s) | 
|---|
| 56 | { | 
|---|
| 57 | Ref<SCMatrixKit> k = SCMatrixKit::default_matrixkit(); | 
|---|
| 58 | RefSCDimension dim; | 
|---|
| 59 | dim << SavableState::restore_state(s); | 
|---|
| 60 | xprev = k->vector(dim); | 
|---|
| 61 | gprev = k->vector(dim); | 
|---|
| 62 | xprev.restore(s); | 
|---|
| 63 | gprev.restore(s); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | PowellUpdate::~PowellUpdate() | 
|---|
| 67 | { | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void | 
|---|
| 71 | PowellUpdate::save_data_state(StateOut&s) | 
|---|
| 72 | { | 
|---|
| 73 | HessianUpdate::save_data_state(s); | 
|---|
| 74 | SavableState::save_state(xprev.dim().pointer(),s); | 
|---|
| 75 | xprev.save(s); | 
|---|
| 76 | gprev.save(s); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void | 
|---|
| 80 | PowellUpdate::update(const RefSymmSCMatrix&hessian,const Ref<Function>&func, | 
|---|
| 81 | const RefSCVector&xn,const RefSCVector&gn) | 
|---|
| 82 | { | 
|---|
| 83 | RefSCVector xnew, gnew; | 
|---|
| 84 |  | 
|---|
| 85 | // the update for the inverse hessian differs from the update for the | 
|---|
| 86 | // hessian in that xdisp and gdisp are exchanged | 
|---|
| 87 | // test this...it may only be true for the Broyden family of updates | 
|---|
| 88 | if (!inverse_hessian_) { | 
|---|
| 89 | xnew = xn; | 
|---|
| 90 | gnew = gn; | 
|---|
| 91 | } else { | 
|---|
| 92 | xnew = gn; | 
|---|
| 93 | gnew = xn; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | if (xprev.nonnull()) { | 
|---|
| 97 | RefSCVector xdisp = xnew-xprev; | 
|---|
| 98 | RefSCVector gdisp = gnew-gprev-hessian*xdisp; | 
|---|
| 99 | double xdisp_xdisp = xdisp.scalar_product(xdisp); | 
|---|
| 100 | double gdisp_xdisp = gdisp.scalar_product(xdisp); | 
|---|
| 101 | hessian.accumulate( | 
|---|
| 102 | xdisp.symmetric_outer_product() * | 
|---|
| 103 | (-gdisp_xdisp/(xdisp_xdisp*xdisp_xdisp)) | 
|---|
| 104 | ); | 
|---|
| 105 | RefSCMatrix temp = | 
|---|
| 106 | (gdisp.outer_product(xdisp) + xdisp.outer_product(gdisp)) * | 
|---|
| 107 | (0.5/xdisp_xdisp); | 
|---|
| 108 | hessian.accumulate_symmetric_sum(temp); | 
|---|
| 109 | xprev.assign(xnew); | 
|---|
| 110 | gprev.assign(gnew); | 
|---|
| 111 | } else { | 
|---|
| 112 | xprev = xnew.copy(); | 
|---|
| 113 | gprev = gnew.copy(); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void | 
|---|
| 118 | PowellUpdate::apply_transform(const Ref<NonlinearTransform>& trans) | 
|---|
| 119 | { | 
|---|
| 120 | if (trans.null()) return; | 
|---|
| 121 | HessianUpdate::apply_transform(trans); | 
|---|
| 122 | trans->transform_coordinates(xprev); | 
|---|
| 123 | trans->transform_gradient(gprev); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 127 |  | 
|---|
| 128 | // Local Variables: | 
|---|
| 129 | // mode: c++ | 
|---|
| 130 | // c-file-style: "ETS" | 
|---|
| 131 | // End: | 
|---|