| [0b990d] | 1 | // | 
|---|
|  | 2 | // function.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 | #ifdef __GNUC__ | 
|---|
|  | 29 | #pragma implementation | 
|---|
|  | 30 | #endif | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #include <math.h> | 
|---|
|  | 33 | #include <float.h> | 
|---|
|  | 34 | #include <string.h> | 
|---|
|  | 35 |  | 
|---|
|  | 36 | #include <util/misc/formio.h> | 
|---|
|  | 37 | #include <util/state/stateio.h> | 
|---|
|  | 38 | #include <math/scmat/matrix.h> | 
|---|
|  | 39 | #include <math/scmat/elemop.h> | 
|---|
|  | 40 | #include <util/keyval/keyval.h> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include <math/optimize/function.h> | 
|---|
|  | 43 |  | 
|---|
|  | 44 | using namespace std; | 
|---|
|  | 45 | using namespace sc; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | //////////////////////////////////////////////////////////////////////// | 
|---|
|  | 48 |  | 
|---|
|  | 49 | static ClassDesc Function_cd( | 
|---|
|  | 50 | typeid(Function),"Function",1,"virtual public SavableState", | 
|---|
|  | 51 | 0, 0, 0); | 
|---|
|  | 52 |  | 
|---|
|  | 53 | Function::Function(): | 
|---|
|  | 54 | value_(this), | 
|---|
|  | 55 | gradient_(this), | 
|---|
|  | 56 | hessian_(this) | 
|---|
|  | 57 | { | 
|---|
|  | 58 | matrixkit_ = SCMatrixKit::default_matrixkit(); | 
|---|
|  | 59 | value_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 60 | gradient_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 61 | hessian_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 | Function::Function(const Function& func): | 
|---|
|  | 65 | value_(func.value_,this), | 
|---|
|  | 66 | gradient_(func.gradient_,this), | 
|---|
|  | 67 | hessian_(func.hessian_,this) | 
|---|
|  | 68 | { | 
|---|
|  | 69 | matrixkit_ = func.matrixkit_; | 
|---|
|  | 70 | dim_ = func.dim_; | 
|---|
|  | 71 | x_ = func.x_; | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | Function::Function(const Ref<KeyVal>&kv, double funcacc, | 
|---|
|  | 75 | double gradacc, double hessacc): | 
|---|
|  | 76 | value_(this), | 
|---|
|  | 77 | gradient_(this), | 
|---|
|  | 78 | hessian_(this) | 
|---|
|  | 79 | { | 
|---|
|  | 80 | matrixkit_ << kv->describedclassvalue("matrixkit"); | 
|---|
|  | 81 |  | 
|---|
|  | 82 | if (matrixkit_.null()) matrixkit_ = SCMatrixKit::default_matrixkit(); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | KeyValValuedouble funcaccval(funcacc); | 
|---|
|  | 85 | value_.set_desired_accuracy(kv->doublevalue("value_accuracy",funcaccval)); | 
|---|
|  | 86 | if (value_.desired_accuracy() < DBL_EPSILON) | 
|---|
|  | 87 | value_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | KeyValValuedouble gradaccval(gradacc); | 
|---|
|  | 90 | gradient_.set_desired_accuracy(kv->doublevalue("gradient_accuracy", | 
|---|
|  | 91 | gradaccval)); | 
|---|
|  | 92 | if (gradient_.desired_accuracy() < DBL_EPSILON) | 
|---|
|  | 93 | gradient_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | KeyValValuedouble hessaccval(hessacc); | 
|---|
|  | 96 | hessian_.set_desired_accuracy(kv->doublevalue("hessian_accuracy", | 
|---|
|  | 97 | hessaccval)); | 
|---|
|  | 98 | if (hessian_.desired_accuracy() < DBL_EPSILON) | 
|---|
|  | 99 | hessian_.set_desired_accuracy(DBL_EPSILON); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | Function::Function(StateIn&s): | 
|---|
|  | 103 | SavableState(s), | 
|---|
|  | 104 | value_(s,this), | 
|---|
|  | 105 | gradient_(this), | 
|---|
|  | 106 | hessian_(this) | 
|---|
|  | 107 | { | 
|---|
|  | 108 | matrixkit_ = SCMatrixKit::default_matrixkit(); | 
|---|
|  | 109 | dim_ << SavableState::restore_state(s); | 
|---|
|  | 110 | x_ = matrixkit_->vector(dim_); | 
|---|
|  | 111 | x_.restore(s); | 
|---|
|  | 112 |  | 
|---|
|  | 113 | gradient_.result_noupdate() = matrixkit()->vector(dim_); | 
|---|
|  | 114 | gradient_.restore_state(s); | 
|---|
|  | 115 | gradient_.result_noupdate().restore(s); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | hessian_.result_noupdate() = matrixkit()->symmmatrix(dim_); | 
|---|
|  | 118 | hessian_.restore_state(s); | 
|---|
|  | 119 | hessian_.result_noupdate().restore(s); | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | Function::~Function() | 
|---|
|  | 123 | { | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | Function & | 
|---|
|  | 127 | Function::operator=(const Function& func) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | matrixkit_ = func.matrixkit_; | 
|---|
|  | 130 | dim_ = func.dim_; | 
|---|
|  | 131 | x_ = func.x_; | 
|---|
|  | 132 | value_ = func.value_; | 
|---|
|  | 133 | gradient_ = func.gradient_; | 
|---|
|  | 134 | hessian_ = func.hessian_; | 
|---|
|  | 135 | return *this; | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | void | 
|---|
|  | 139 | Function::save_data_state(StateOut&s) | 
|---|
|  | 140 | { | 
|---|
|  | 141 | value_.save_data_state(s); | 
|---|
|  | 142 | SavableState::save_state(dim_.pointer(),s); | 
|---|
|  | 143 | x_.save(s); | 
|---|
|  | 144 | gradient_.save_data_state(s); | 
|---|
|  | 145 | gradient_.result_noupdate().save(s); | 
|---|
|  | 146 | hessian_.save_data_state(s); | 
|---|
|  | 147 | hessian_.result_noupdate().save(s); | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
|  | 150 | Ref<SCMatrixKit> | 
|---|
|  | 151 | Function::matrixkit() const | 
|---|
|  | 152 | { | 
|---|
|  | 153 | return matrixkit_; | 
|---|
|  | 154 | } | 
|---|
|  | 155 |  | 
|---|
|  | 156 | RefSCDimension | 
|---|
|  | 157 | Function::dimension() const | 
|---|
|  | 158 | { | 
|---|
|  | 159 | return dim_; | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
|  | 162 | void | 
|---|
|  | 163 | Function::set_x(const RefSCVector&v) | 
|---|
|  | 164 | { | 
|---|
|  | 165 | x_.assign(v); | 
|---|
|  | 166 | obsolete(); | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
|  | 169 | double | 
|---|
|  | 170 | Function::value() | 
|---|
|  | 171 | { | 
|---|
|  | 172 | return value_; | 
|---|
|  | 173 | } | 
|---|
|  | 174 |  | 
|---|
|  | 175 | int | 
|---|
|  | 176 | Function::value_needed() const | 
|---|
|  | 177 | { | 
|---|
|  | 178 | return value_.needed(); | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|
|  | 181 | int | 
|---|
|  | 182 | Function::do_value(int f) | 
|---|
|  | 183 | { | 
|---|
|  | 184 | return value_.compute(f); | 
|---|
|  | 185 | } | 
|---|
|  | 186 |  | 
|---|
|  | 187 | void | 
|---|
|  | 188 | Function::set_value(double e) | 
|---|
|  | 189 | { | 
|---|
|  | 190 | value_.result_noupdate() = e; | 
|---|
|  | 191 | value_.computed() = 1; | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | void | 
|---|
|  | 195 | Function::set_desired_value_accuracy(double a) | 
|---|
|  | 196 | { | 
|---|
|  | 197 | value_.set_desired_accuracy(a); | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | void | 
|---|
|  | 201 | Function::set_actual_value_accuracy(double a) | 
|---|
|  | 202 | { | 
|---|
|  | 203 | value_.set_actual_accuracy(a); | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
|  | 206 | double | 
|---|
|  | 207 | Function::desired_value_accuracy() const | 
|---|
|  | 208 | { | 
|---|
|  | 209 | return value_.desired_accuracy(); | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 | double | 
|---|
|  | 213 | Function::actual_value_accuracy() const | 
|---|
|  | 214 | { | 
|---|
|  | 215 | return value_.actual_accuracy(); | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
|  | 218 | RefSCVector | 
|---|
|  | 219 | Function::gradient() | 
|---|
|  | 220 | { | 
|---|
|  | 221 | RefSCVector ret = gradient_.result(); | 
|---|
|  | 222 | return ret; | 
|---|
|  | 223 | } | 
|---|
|  | 224 |  | 
|---|
|  | 225 | int | 
|---|
|  | 226 | Function::gradient_needed() const | 
|---|
|  | 227 | { | 
|---|
|  | 228 | return gradient_.needed(); | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | int | 
|---|
|  | 232 | Function::do_gradient(int f) | 
|---|
|  | 233 | { | 
|---|
|  | 234 | return gradient_.compute(f); | 
|---|
|  | 235 | } | 
|---|
|  | 236 |  | 
|---|
|  | 237 | void | 
|---|
|  | 238 | Function::set_gradient(RefSCVector&g) | 
|---|
|  | 239 | { | 
|---|
|  | 240 | gradient_.result_noupdate() = g; | 
|---|
|  | 241 | gradient_.computed() = 1; | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
|  | 244 | void | 
|---|
|  | 245 | Function::set_desired_gradient_accuracy(double a) | 
|---|
|  | 246 | { | 
|---|
|  | 247 | gradient_.set_desired_accuracy(a); | 
|---|
|  | 248 | } | 
|---|
|  | 249 |  | 
|---|
|  | 250 | void | 
|---|
|  | 251 | Function::set_actual_gradient_accuracy(double a) | 
|---|
|  | 252 | { | 
|---|
|  | 253 | gradient_.set_actual_accuracy(a); | 
|---|
|  | 254 | } | 
|---|
|  | 255 |  | 
|---|
|  | 256 | double | 
|---|
|  | 257 | Function::actual_gradient_accuracy() const | 
|---|
|  | 258 | { | 
|---|
|  | 259 | return gradient_.actual_accuracy(); | 
|---|
|  | 260 | } | 
|---|
|  | 261 |  | 
|---|
|  | 262 | double | 
|---|
|  | 263 | Function::desired_gradient_accuracy() const | 
|---|
|  | 264 | { | 
|---|
|  | 265 | return gradient_.desired_accuracy(); | 
|---|
|  | 266 | } | 
|---|
|  | 267 |  | 
|---|
|  | 268 | RefSymmSCMatrix | 
|---|
|  | 269 | Function::hessian() | 
|---|
|  | 270 | { | 
|---|
|  | 271 | return hessian_.result(); | 
|---|
|  | 272 | } | 
|---|
|  | 273 |  | 
|---|
|  | 274 | int | 
|---|
|  | 275 | Function::hessian_needed() const | 
|---|
|  | 276 | { | 
|---|
|  | 277 | return hessian_.needed(); | 
|---|
|  | 278 | } | 
|---|
|  | 279 |  | 
|---|
|  | 280 | int | 
|---|
|  | 281 | Function::do_hessian(int f) | 
|---|
|  | 282 | { | 
|---|
|  | 283 | return hessian_.compute(f); | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | void | 
|---|
|  | 287 | Function::set_hessian(RefSymmSCMatrix&h) | 
|---|
|  | 288 | { | 
|---|
|  | 289 | hessian_.result_noupdate() = h; | 
|---|
|  | 290 | hessian_.computed() = 1; | 
|---|
|  | 291 | } | 
|---|
|  | 292 |  | 
|---|
|  | 293 | // the default guess hessian is the unit diagonal | 
|---|
|  | 294 | void | 
|---|
|  | 295 | Function::guess_hessian(RefSymmSCMatrix&hessian) | 
|---|
|  | 296 | { | 
|---|
|  | 297 | Ref<SCElementOp> op(new SCElementShiftDiagonal(1.0)); | 
|---|
|  | 298 | hessian.assign(0.0); | 
|---|
|  | 299 | hessian.element_op(op); | 
|---|
|  | 300 | } | 
|---|
|  | 301 |  | 
|---|
|  | 302 | RefSymmSCMatrix | 
|---|
|  | 303 | Function::inverse_hessian(RefSymmSCMatrix&hessian) | 
|---|
|  | 304 | { | 
|---|
|  | 305 | return hessian.gi(); | 
|---|
|  | 306 | } | 
|---|
|  | 307 |  | 
|---|
|  | 308 | void | 
|---|
|  | 309 | Function::set_desired_hessian_accuracy(double a) | 
|---|
|  | 310 | { | 
|---|
|  | 311 | hessian_.set_desired_accuracy(a); | 
|---|
|  | 312 | } | 
|---|
|  | 313 |  | 
|---|
|  | 314 | void | 
|---|
|  | 315 | Function::set_actual_hessian_accuracy(double a) | 
|---|
|  | 316 | { | 
|---|
|  | 317 | hessian_.set_actual_accuracy(a); | 
|---|
|  | 318 | } | 
|---|
|  | 319 |  | 
|---|
|  | 320 | double | 
|---|
|  | 321 | Function::desired_hessian_accuracy() const | 
|---|
|  | 322 | { | 
|---|
|  | 323 | return hessian_.desired_accuracy(); | 
|---|
|  | 324 | } | 
|---|
|  | 325 |  | 
|---|
|  | 326 | double | 
|---|
|  | 327 | Function::actual_hessian_accuracy() const | 
|---|
|  | 328 | { | 
|---|
|  | 329 | return hessian_.actual_accuracy(); | 
|---|
|  | 330 | } | 
|---|
|  | 331 |  | 
|---|
|  | 332 | void | 
|---|
|  | 333 | Function::print(ostream&o) const | 
|---|
|  | 334 | { | 
|---|
|  | 335 | const char *computed = " (computed)"; | 
|---|
|  | 336 | const char *notcomputed = ""; | 
|---|
|  | 337 | o << indent << "Function Parameters:\n" << incindent | 
|---|
|  | 338 | << indent << scprintf("value_accuracy    = %e (%e)%s\n", | 
|---|
|  | 339 | actual_value_accuracy(), desired_value_accuracy(), | 
|---|
|  | 340 | (value_.computed()?computed:notcomputed)) | 
|---|
|  | 341 | << indent << scprintf("gradient_accuracy = %e (%e)%s\n", | 
|---|
|  | 342 | actual_gradient_accuracy(), | 
|---|
|  | 343 | desired_gradient_accuracy(), | 
|---|
|  | 344 | (gradient_.computed()?computed:notcomputed)) | 
|---|
|  | 345 | << indent << scprintf("hessian_accuracy  = %e (%e)%s\n", | 
|---|
|  | 346 | actual_hessian_accuracy(), | 
|---|
|  | 347 | desired_hessian_accuracy(), | 
|---|
|  | 348 | (hessian_.computed()?computed:notcomputed)) | 
|---|
|  | 349 | << decindent << endl; | 
|---|
|  | 350 | } | 
|---|
|  | 351 |  | 
|---|
|  | 352 | void | 
|---|
|  | 353 | Function::set_matrixkit(const Ref<SCMatrixKit>& kit) | 
|---|
|  | 354 | { | 
|---|
|  | 355 | matrixkit_ = kit; | 
|---|
|  | 356 | } | 
|---|
|  | 357 |  | 
|---|
|  | 358 | void | 
|---|
|  | 359 | Function::set_dimension(const RefSCDimension& dim) | 
|---|
|  | 360 | { | 
|---|
|  | 361 | dim_ = dim; | 
|---|
|  | 362 | x_ = matrixkit_->vector(dim); | 
|---|
|  | 363 | x_.assign(0.0); | 
|---|
|  | 364 | gradient_ = matrixkit()->vector(dim); | 
|---|
|  | 365 | gradient_.result_noupdate().assign(0.0); | 
|---|
|  | 366 | hessian_ = matrixkit()->symmmatrix(dim); | 
|---|
|  | 367 | hessian_.result_noupdate().assign(0.0); | 
|---|
|  | 368 | } | 
|---|
|  | 369 |  | 
|---|
|  | 370 | int | 
|---|
|  | 371 | Function::value_implemented() const | 
|---|
|  | 372 | { | 
|---|
|  | 373 | return 0; | 
|---|
|  | 374 | } | 
|---|
|  | 375 |  | 
|---|
|  | 376 | int | 
|---|
|  | 377 | Function::gradient_implemented() const | 
|---|
|  | 378 | { | 
|---|
|  | 379 | return 0; | 
|---|
|  | 380 | } | 
|---|
|  | 381 |  | 
|---|
|  | 382 | int | 
|---|
|  | 383 | Function::hessian_implemented() const | 
|---|
|  | 384 | { | 
|---|
|  | 385 | return 0; | 
|---|
|  | 386 | } | 
|---|
|  | 387 |  | 
|---|
|  | 388 | Ref<NonlinearTransform> | 
|---|
|  | 389 | Function::change_coordinates() | 
|---|
|  | 390 | { | 
|---|
|  | 391 | return 0; | 
|---|
|  | 392 | } | 
|---|
|  | 393 |  | 
|---|
|  | 394 | void | 
|---|
|  | 395 | Function::do_change_coordinates(const Ref<NonlinearTransform> &t) | 
|---|
|  | 396 | { | 
|---|
|  | 397 | if (t.null()) | 
|---|
|  | 398 | return; | 
|---|
|  | 399 |  | 
|---|
|  | 400 | t->transform_coordinates(x_); | 
|---|
|  | 401 | obsolete(); | 
|---|
|  | 402 | } | 
|---|
|  | 403 |  | 
|---|
|  | 404 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 405 |  | 
|---|
|  | 406 | // Local Variables: | 
|---|
|  | 407 | // mode: c++ | 
|---|
|  | 408 | // c-file-style: "CLJ" | 
|---|
|  | 409 | // End: | 
|---|