/usr/include/ITK-4.12/itkPowellOptimizerv4.hxx is in libinsighttoolkit4-dev 4.12.2-dfsg1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkPowellOptimizerv4_hxx
#define itkPowellOptimizerv4_hxx
#include "itkPowellOptimizerv4.h"
namespace itk
{
template<typename TInternalComputationValueType>
PowellOptimizerv4<TInternalComputationValueType>
::PowellOptimizerv4():
m_SpaceDimension(0),
m_CurrentLineIteration(0),
m_MaximumIteration(100),
m_MaximumLineIteration(100),
m_CatchGetValueException(false),
m_MetricWorstPossibleValue(0),
m_StepLength(0),
m_StepTolerance(0),
m_ValueTolerance(0),
m_CurrentCost(0),
m_Stop(false)
{
m_StopConditionDescription << this->GetNameOfClass() << ": ";
}
template<typename TInternalComputationValueType>
PowellOptimizerv4<TInternalComputationValueType>
::~PowellOptimizerv4()
{}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::SetLine(const ParametersType & origin,
const vnl_vector< double > & direction)
{
const ScalesType & scales = this->GetScales();
for ( unsigned int i = 0; i < m_SpaceDimension; ++i )
{
m_LineOrigin[i] = origin[i];
if( this->GetScalesAreIdentity() )
{
m_LineDirection[i] = direction[i];
}
else
{
m_LineDirection[i] = direction[i] / scales[i];
}
}
}
template<typename TInternalComputationValueType>
double
PowellOptimizerv4<TInternalComputationValueType>
::GetLineValue(double x) const
{
ParametersType tempCoord(m_SpaceDimension);
return this->GetLineValue(x, tempCoord);
}
template<typename TInternalComputationValueType>
double
PowellOptimizerv4<TInternalComputationValueType>
::GetLineValue(double x, ParametersType & tempCoord) const
{
for ( unsigned int i = 0; i < m_SpaceDimension; i++ )
{
tempCoord[i] = this->m_LineOrigin[i] + x * this->m_LineDirection[i];
}
this->m_Metric->SetParameters(tempCoord);
itkDebugMacro(<< "x = " << x);
double val;
try
{
val = ( this->m_Metric->GetValue() );
}
catch ( ... )
{
if ( m_CatchGetValueException )
{
val = m_MetricWorstPossibleValue;
}
else
{
throw;
}
}
itkDebugMacro(<< "val = " << val);
return val;
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::SetCurrentLinePoint(double x, double fx)
{
for ( unsigned int i = 0; i < m_SpaceDimension; i++ )
{
this->m_CurrentPosition[i] = this->m_LineOrigin[i] + x * this->m_LineDirection[i];
}
this->m_Metric->SetParameters(m_CurrentPosition);
this->SetCurrentCost(fx);
this->Modified();
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::Swap(double *a, double *b) const
{
double tf;
tf = *a;
*a = *b;
*b = tf;
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::Shift(double *a, double *b, double *c, double d) const
{
*a = *b;
*b = *c;
*c = d;
}
//
// This code was implemented from the description of
// the Golden section search available in the Wikipedia
//
// http://en.wikipedia.org/wiki/Golden_section_search
//
//
// The inputs to this function are
//
// x1 and its function value f1
// x2
//
// (f2 is not yet evaluated, it will be computed inside)
// (x2 and its function value f3 are also computed inside)
//
// The outputs are the values of x2 and f2 at
// the end of the iterations.
//
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::LineBracket(double *x1, double *x2, double *x3,
double *f1, double *f2, double *f3)
{
ParametersType tempCoord(m_SpaceDimension);
this->LineBracket(x1, x2, x3, f1, f2, f3, tempCoord);
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::LineBracket(double *x1, double *x2, double *x3,
double *f1, double *f2, double *f3,
ParametersType & tempCoord)
{
//
// Compute the golden ratio as a constant to be
// used when extrapolating the bracket
//
const double goldenRatio = ( 1.0 + std::sqrt(5.0) ) / 2.0;
//
// Get the value of the function for point x2
//
*f2 = this->GetLineValue(*x2, tempCoord);
//
// Compute extrapolated point using the golden ratio
//
if ( *f2 >= *f1 )
{
this->Swap(x1, x2);
this->Swap(f1, f2);
}
// compute x3 on the side of x2
*x3 = *x1 + goldenRatio * ( *x2 - *x1 );
*f3 = this->GetLineValue(*x3, tempCoord);
// If the new point is a minimum
// then continue extrapolating in
// that direction until we find a
// value of f3 that makes f2 to be
// a minimum.
while ( *f3 < *f2 )
{
*x2 = *x3;
*f2 = *f3;
*x3 = *x1 + goldenRatio * ( *x2 - *x1 );
*f3 = this->GetLineValue(*x3, tempCoord);
}
itkDebugMacro(<< "Initial: " << *x1 << ", " << *x2 << ", " << *x3);
//
// Report the central point as the minimum
//
this->SetCurrentLinePoint(*x2, *f2);
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::BracketedLineOptimize(double ax, double bx, double cx,
double fa, double functionValueOfb, double fc,
double *extX, double *extVal)
{
ParametersType tempCoord(m_SpaceDimension);
this->BracketedLineOptimize(ax, bx, cx, fa, functionValueOfb, fc, extX, extVal, tempCoord);
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::BracketedLineOptimize(double ax, double bx, double cx,
double itkNotUsed(fa), double functionValueOfb, double itkNotUsed(fc),
double *extX, double *extVal,
ParametersType & tempCoord)
{
double x;
double v = 0.0;
double w; /* Abscissae, descr. see above */
double a;
double b;
a = ( ax < cx ? ax : cx );
b = ( ax > cx ? ax : cx );
x = bx;
w = bx;
const double goldenSectionRatio = ( 3.0 - std::sqrt(5.0) ) / 2; /* Gold
section
ratio */
const double POWELL_TINY = 1.0e-20;
double functionValueOfX; /* f(x) */
double functionValueOfV; /* f(v) */
double functionValueOfW; /* f(w) */
functionValueOfV = functionValueOfb;
functionValueOfX = functionValueOfV;
functionValueOfW = functionValueOfV;
for ( m_CurrentLineIteration = 0;
m_CurrentLineIteration < m_MaximumLineIteration;
m_CurrentLineIteration++ )
{
double middle_range = ( a + b ) / 2;
double new_step; /* Step at this iteration */
double tolerance1;
double tolerance2;
tolerance1 = m_StepTolerance * std::fabs(x) + POWELL_TINY;
tolerance2 = 2.0 * tolerance1;
if ( std::fabs(x - middle_range) <= ( tolerance2 - 0.5 * ( b - a ) )
|| 0.5 * ( b - a ) < m_StepTolerance )
{
*extX = x;
*extVal = functionValueOfX;
this->SetCurrentLinePoint(x, functionValueOfX);
itkDebugMacro(<< "x = " << *extX);
itkDebugMacro(<< "val = " << *extVal);
itkDebugMacro(<< "return 1");
return; /* Acceptable approx. is found */
}
/* Obtain the gold section step */
new_step = goldenSectionRatio * ( x < middle_range ? b - x : a - x );
/* Decide if the interpolation can be tried */
if ( std::fabs(x - w) >= tolerance1 ) /* If x and w are distinct */
{
double t;
t = ( x - w ) * ( functionValueOfX - functionValueOfV );
double q; /* ted as p/q; division operation*/
q = ( x - v ) * ( functionValueOfX - functionValueOfW );
double p; /* Interpolation step is calcula-*/
p = ( x - v ) * q - ( x - w ) * t;
q = 2 * ( q - t );
if ( q > (double)0 ) /* q was calculated with the op-*/
{
p = -p; /* posite sign; make q positive */
}
else /* and assign possible minus to */
{
q = -q; /* p */
}
/* Chec if x+p/q falls in [a,b] and not too close to a and b
and isn't too large */
if ( std::fabs(p) < std::fabs(new_step * q)
&& p > q * ( a - x + 2 * tolerance1 )
&& p < q * ( b - x - 2 * tolerance1 ) )
{
new_step = p / q; /* it is accepted */
}
/* If p/q is too large then the gold section procedure can
reduce [a,b] range to more extent */
}
/* Adjust the step to be not less than tolerance*/
if ( std::fabs(new_step) < tolerance1 )
{
if ( new_step > 0.0 )
{
new_step = tolerance1;
}
else
{
new_step = -tolerance1;
}
}
/* Obtain the next approximation to min */
/* and reduce the enveloping range */
double t = x + new_step; /* Tentative point for the min */
double functionValueOft;
functionValueOft = this->GetLineValue(t, tempCoord);
if ( functionValueOft <= functionValueOfX )
{
if ( t < x ) /* Reduce the range so that */
{
b = x; /* t would fall within it */
}
else
{
a = x;
}
/* assing the best approximation to x */
v = w;
w = x;
x = t;
functionValueOfV = functionValueOfW;
functionValueOfW = functionValueOfX;
functionValueOfX = functionValueOft;
}
else /* x remains the better approx */
{
if ( t < x ) /* Reduce the range enclosing x */
{
a = t;
}
else
{
b = t;
}
if ( functionValueOft <= functionValueOfW || Math::ExactlyEquals(w, x) )
{
v = w;
w = t;
functionValueOfV = functionValueOfW;
functionValueOfW = functionValueOft;
}
else if ( functionValueOft <= functionValueOfV || Math::ExactlyEquals(v, x) || Math::ExactlyEquals(v, w) )
{
v = t;
functionValueOfV = functionValueOft;
}
}
}
*extX = x;
*extVal = functionValueOfX;
itkDebugMacro(<< "x = " << *extX);
itkDebugMacro(<< "val = " << *extVal);
itkDebugMacro(<< "return 2");
this->SetCurrentLinePoint(x, functionValueOfX);
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::StartOptimization(bool /* doOnlyInitialization */)
{
if ( this->m_Metric.IsNull() )
{
return;
}
Superclass::StartOptimization();
m_StopConditionDescription.str("");
m_StopConditionDescription << this->GetNameOfClass() << ": ";
this->InvokeEvent( StartEvent() );
m_Stop = false;
this->SetSpaceDimension( this->m_Metric->GetNumberOfParameters() );
vnl_matrix< double > xi(m_SpaceDimension, m_SpaceDimension);
vnl_vector< double > xit(m_SpaceDimension);
xi.set_identity();
xit.fill(0);
xit[0] = 1;
ParametersType tempCoord(m_SpaceDimension);
ParametersType p(m_SpaceDimension);
ParametersType pt(m_SpaceDimension);
ParametersType ptt(m_SpaceDimension);
p = this->m_Metric->GetParameters();
pt = p;
unsigned int ibig;
double fp, del, fptt;
double ax, xx, bx;
double fa, fx, fb;
xx = 0;
this->SetLine(p, xit);
fx = this->GetLineValue(0, tempCoord);
for ( this->m_CurrentIteration = 0;
this->m_CurrentIteration <= m_MaximumIteration;
this->m_CurrentIteration++ )
{
fp = fx;
ibig = 0;
del = 0.0;
for ( unsigned int i = 0; i < m_SpaceDimension; i++ )
{
for ( unsigned int j = 0; j < m_SpaceDimension; ++j )
{
xit[j] = xi[j][i];
}
fptt = fx;
this->SetLine(p, xit);
ax = 0.0;
fa = fx;
xx = m_StepLength;
this->LineBracket(&ax, &xx, &bx, &fa, &fx, &fb, tempCoord);
this->BracketedLineOptimize(ax, xx, bx, fa, fx, fb, &xx, &fx, tempCoord);
this->SetCurrentLinePoint(xx, fx);
p = this->GetCurrentPosition();
if ( std::fabs(fptt - fx) > del )
{
del = std::fabs(fptt - fx);
ibig = i;
}
}
if ( 2.0 * std::fabs(fp - fx)
<= m_ValueTolerance * ( std::fabs(fp) + std::fabs(fx) ) )
{
m_StopConditionDescription << "Cost function values at the current parameter ("
<< fx
<< ") and at the local extrema ("
<< fp
<< ") are within Value Tolerance ("
<< m_ValueTolerance << ")";
this->InvokeEvent( EndEvent() );
return;
}
const ScalesType & scales = this->GetScales();
for ( unsigned int j = 0; j < m_SpaceDimension; ++j )
{
ptt[j] = 2.0 * p[j] - pt[j];
if( this->GetScalesAreIdentity() )
{
xit[j] = p[j] - pt[j];
}
else
{
xit[j] = ( p[j] - pt[j] ) * scales[j];
}
pt[j] = p[j];
}
this->SetLine(ptt, xit);
fptt = this->GetLineValue(0, tempCoord);
if ( fptt < fp )
{
double t = 2.0 * ( fp - 2.0 * fx + fptt )
* itk::Math::sqr(fp - fx - del)
- del *itk::Math::sqr(fp - fptt);
if ( t < 0.0 )
{
this->SetLine(p, xit);
ax = 0.0;
fa = fx;
xx = 1;
this->LineBracket(&ax, &xx, &bx, &fa, &fx, &fb, tempCoord);
this->BracketedLineOptimize(ax, xx, bx, fa, fx, fb, &xx, &fx, tempCoord);
this->SetCurrentLinePoint(xx, fx);
p = this->GetCurrentPosition();
for ( unsigned int j = 0; j < m_SpaceDimension; j++ )
{
xi[j][ibig] = xx * xit[j];
}
}
}
this->InvokeEvent( IterationEvent() );
}
m_StopConditionDescription << "Maximum number of iterations exceeded. "
<< "Number of iterations is "
<< m_MaximumIteration;
this->InvokeEvent( EndEvent() );
}
template<typename TInternalComputationValueType>
const std::string
PowellOptimizerv4<TInternalComputationValueType>
::GetStopConditionDescription() const
{
return m_StopConditionDescription.str();
}
template<typename TInternalComputationValueType>
void
PowellOptimizerv4<TInternalComputationValueType>
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Metric Worst Possible Value " << m_MetricWorstPossibleValue << std::endl;
os << indent << "Catch GetValue Exception " << m_CatchGetValueException << std::endl;
os << indent << "Space Dimension " << m_SpaceDimension << std::endl;
os << indent << "Maximum Iteration " << m_MaximumIteration << std::endl;
os << indent << "StepLength " << m_StepLength << std::endl;
os << indent << "StepTolerance " << m_StepTolerance << std::endl;
os << indent << "ValueTolerance " << m_ValueTolerance << std::endl;
os << indent << "LineOrigin " << m_LineOrigin << std::endl;
os << indent << "LineDirection " << m_LineDirection << std::endl;
os << indent << "Current Cost " << m_CurrentCost << std::endl;
os << indent << "Maximum Line Iteration " << m_MaximumLineIteration << std::endl;
os << indent << "Current Line Iteration " << m_CurrentLineIteration << std::endl;
os << indent << "Stop " << m_Stop << std::endl;
}
} // end of namespace itk
#endif
|