| 1 | //
 | 
|---|
| 2 | // opttest.cc
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.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 <iostream>
 | 
|---|
| 29 | #include <util/state/stateio.h>
 | 
|---|
| 30 | #include <math/optimize/function.h>
 | 
|---|
| 31 | #include <math/optimize/opt.h>
 | 
|---|
| 32 | #include <util/keyval/keyval.h>
 | 
|---|
| 33 | #include <math/scmat/local.h>
 | 
|---|
| 34 | #include <math/scmat/matrix.h>
 | 
|---|
| 35 | #include <math/scmat/elemop.h>
 | 
|---|
| 36 | #include <math/optimize/linkage.h>
 | 
|---|
| 37 | 
 | 
|---|
| 38 | using namespace std;
 | 
|---|
| 39 | using namespace sc;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | class Quadratic: public Function
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   private:
 | 
|---|
| 44 |     RefSCVector x0;
 | 
|---|
| 45 |     RefSCVector g0;
 | 
|---|
| 46 |     RefSymmSCMatrix h0;
 | 
|---|
| 47 |     RefSymmSCMatrix hguess;
 | 
|---|
| 48 |   public:
 | 
|---|
| 49 |     Quadratic(StateIn&);
 | 
|---|
| 50 |     Quadratic(const Ref<KeyVal>&);
 | 
|---|
| 51 |     void save_data_state(StateOut&);
 | 
|---|
| 52 |     void compute();
 | 
|---|
| 53 |     void guess_hessian(RefSymmSCMatrix&);
 | 
|---|
| 54 | };
 | 
|---|
| 55 | static ClassDesc Quadratic_cd(
 | 
|---|
| 56 |   typeid(Quadratic),"Quadratic",1,"public Function",
 | 
|---|
| 57 |   0, create<Quadratic>, create<Quadratic>);
 | 
|---|
| 58 | Quadratic::Quadratic(StateIn&s):
 | 
|---|
| 59 |   SavableState(s),
 | 
|---|
| 60 |   Function(s)
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   x0 = matrixkit_->vector(dim_);
 | 
|---|
| 63 |   x0.restore(s);
 | 
|---|
| 64 |   g0 = matrixkit_->vector(dim_);
 | 
|---|
| 65 |   g0.restore(s);
 | 
|---|
| 66 |   h0 = matrixkit_->symmmatrix(dim_);
 | 
|---|
| 67 |   h0.restore(s);
 | 
|---|
| 68 | }
 | 
|---|
| 69 | void
 | 
|---|
| 70 | Quadratic::save_data_state(StateOut&s)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |   Function::save_data_state(s);
 | 
|---|
| 73 |   x0.save(s);
 | 
|---|
| 74 |   g0.save(s);
 | 
|---|
| 75 |   h0.save(s);
 | 
|---|
| 76 | }
 | 
|---|
| 77 | Quadratic::Quadratic(const Ref<KeyVal>&keyval):
 | 
|---|
| 78 |   Function(keyval)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   set_dimension(new SCDimension(keyval->count("x0")));
 | 
|---|
| 81 |   x0 = matrixkit_->vector(dim_);
 | 
|---|
| 82 |   g0 = matrixkit_->vector(dim_);
 | 
|---|
| 83 |   h0 = matrixkit_->symmmatrix(dim_);
 | 
|---|
| 84 |   hguess = matrixkit_->symmmatrix(dim_);
 | 
|---|
| 85 |   hguess.assign(0.0);
 | 
|---|
| 86 |   Ref<SCElementOp> op(new SCElementShiftDiagonal(1.0));
 | 
|---|
| 87 |   hguess.element_op(op);
 | 
|---|
| 88 |   
 | 
|---|
| 89 |   int dim = dimension()->n();
 | 
|---|
| 90 |   for (int i=0; i<dim; i++) {
 | 
|---|
| 91 |       x0(i) = keyval->doublevalue("x0",i);
 | 
|---|
| 92 |       g0(i) = keyval->doublevalue("g0",i);
 | 
|---|
| 93 |       for (int j=0; j<=i; j++) {
 | 
|---|
| 94 |           h0(i,j) = keyval->doublevalue("h0",i,j);
 | 
|---|
| 95 |           hguess(i,j) = keyval->doublevalue("hguess",i,j);
 | 
|---|
| 96 |         }
 | 
|---|
| 97 |     }
 | 
|---|
| 98 | }
 | 
|---|
| 99 | // this computes everything, whether or not it was requested
 | 
|---|
| 100 | void
 | 
|---|
| 101 | Quadratic::compute()
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   cout << "Quadratic::compute(): entered\n";
 | 
|---|
| 104 |   
 | 
|---|
| 105 |   // compute the displacement from x0
 | 
|---|
| 106 |   RefSCVector d = x_ - x0;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // compute h * d
 | 
|---|
| 109 |   RefSCVector h0d = h0 * d;
 | 
|---|
| 110 | //   RefSCVector h0d(h0.dim());
 | 
|---|
| 111 | //   int n=h0.dim().n();
 | 
|---|
| 112 | //   for (int i=0; i<n; i++) {
 | 
|---|
| 113 | //       double tmp = 0;
 | 
|---|
| 114 | //       for (int j=0; j<n; j++) {
 | 
|---|
| 115 | //           tmp += h0(i,j) * d(j);
 | 
|---|
| 116 | //         }
 | 
|---|
| 117 | //       h0d(i) = tmp;
 | 
|---|
| 118 | //     }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // compute the value
 | 
|---|
| 121 |   value_.result_noupdate() =   d.scalar_product(g0)
 | 
|---|
| 122 |                              + 0.5 * d.scalar_product(h0d);
 | 
|---|
| 123 |   value_.computed() = 1;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   // compute the gradient
 | 
|---|
| 126 |   gradient_.result_noupdate() = g0 + h0d;
 | 
|---|
| 127 |   gradient_.computed() = 1;
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // compute the hessian
 | 
|---|
| 130 |   hessian_.result_noupdate() = h0;
 | 
|---|
| 131 |   hessian_.computed() = 1;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | void
 | 
|---|
| 134 | Quadratic::guess_hessian(RefSymmSCMatrix&gh)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   gh.assign(hguess);
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | main()
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   Ref<KeyVal> kv = new ParsedKeyVal( SRCDIR "/opttest.in");
 | 
|---|
| 142 |   Ref<KeyVal> pkv = new PrefixKeyVal(kv,"opt");
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   for (int i=0; i<pkv->count(); i++) {
 | 
|---|
| 145 |       Ref<Optimize> opt; opt << pkv->describedclassvalue(i);
 | 
|---|
| 146 |       if (opt.nonnull()) {
 | 
|---|
| 147 |           RefSCVector oldx = opt->function()->get_x();
 | 
|---|
| 148 |           opt->optimize();
 | 
|---|
| 149 |           // restore the orginal x, in case the function is used again
 | 
|---|
| 150 |           opt->function()->set_x(oldx);
 | 
|---|
| 151 |         }
 | 
|---|
| 152 |     }
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 156 | 
 | 
|---|
| 157 | // Local Variables:
 | 
|---|
| 158 | // mode: c++
 | 
|---|
| 159 | // c-file-style: "CLJ"
 | 
|---|
| 160 | // End:
 | 
|---|