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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAmoebaMinimizer.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkAmoebaMinimizer.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkAmoebaMinimizer);
//----------------------------------------------------------------------------
vtkAmoebaMinimizer::vtkAmoebaMinimizer()
{
this->Function = NULL;
this->FunctionArg = NULL;
this->FunctionArgDelete = NULL;
this->NumberOfParameters = 0;
this->ParameterNames = NULL;
this->ParameterValues = NULL;
this->ParameterScales = NULL;
this->FunctionValue = 0.0;
this->Tolerance = 1e-4;
this->MaxIterations = 1000;
this->Iterations = 0;
this->FunctionEvaluations = 0;
// specific to the amoeba
this->AmoebaVertices = NULL;
this->AmoebaValues = NULL;
this->AmoebaSum = NULL;
this->AmoebaNStepsNoImprovement = 0;
}
//----------------------------------------------------------------------------
vtkAmoebaMinimizer::~vtkAmoebaMinimizer()
{
this->TerminateAmoeba();
if ((this->FunctionArg) && (this->FunctionArgDelete))
{
(*this->FunctionArgDelete)(this->FunctionArg);
}
this->FunctionArg = NULL;
this->FunctionArgDelete = NULL;
this->Function = NULL;
if (this->ParameterNames)
{
for (int i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i])
{
delete [] this->ParameterNames[i];
}
}
delete [] this->ParameterNames;
this->ParameterNames = NULL;
}
if (this->ParameterValues)
{
delete [] this->ParameterValues;
this->ParameterValues = NULL;
}
if (this->ParameterScales)
{
delete [] this->ParameterScales;
this->ParameterScales = NULL;
}
this->NumberOfParameters = 0;
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "NumberOfParameters: " << this->GetNumberOfParameters() << "\n";
if (this->NumberOfParameters > 0)
{
int i;
os << indent << "ParameterValues: \n";
for (i = 0; i < this->NumberOfParameters; i++)
{
const char *name = this->GetParameterName(i);
os << indent << " ";
if (name)
{
os << name << ": ";
}
else
{
os << i << ": ";
}
os << this->GetParameterValue(i) << "\n";
}
os << indent << "ParameterScales: \n";
for (i = 0; i < this->NumberOfParameters; i++)
{
const char *name = this->GetParameterName(i);
os << indent << " ";
if (name)
{
os << name << ": ";
}
else
{
os << i << ": ";
}
os << this->GetParameterScale(i) << "\n";
}
}
os << indent << "FunctionValue: " << this->GetFunctionValue() << "\n";
os << indent << "FunctionEvaluations: " << this->GetFunctionEvaluations()
<< "\n";
os << indent << "Iterations: " << this->GetIterations() << "\n";
os << indent << "MaxIterations: " << this->GetMaxIterations() << "\n";
os << indent << "Tolerance: " << this->GetTolerance() << "\n";
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetFunction(void (*f)(void *), void *arg)
{
if ( f != this->Function || arg != this->FunctionArg )
{
// delete the current arg if there is one and a delete meth
if ((this->FunctionArg) && (this->FunctionArgDelete))
{
(*this->FunctionArgDelete)(this->FunctionArg);
}
this->Function = f;
this->FunctionArg = arg;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetFunctionArgDelete(void (*f)(void *))
{
if ( f != this->FunctionArgDelete)
{
this->FunctionArgDelete = f;
this->Modified();
}
}
//----------------------------------------------------------------------------
double vtkAmoebaMinimizer::GetParameterValue(const char *name)
{
for (int i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i] && strcmp(name,this->ParameterNames[i]) == 0)
{
return this->ParameterValues[i];
}
}
vtkErrorMacro("GetParameterValue: no parameter named " << name);
return 0.0;
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetParameterValue(const char *name, double val)
{
int i;
for (i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i] && strcmp(name,this->ParameterNames[i]) == 0)
{
break;
}
}
this->SetParameterValue(i, val);
if (!this->ParameterNames[i])
{
char *cp = new char[strlen(name)+8];
strcpy(cp,name);
this->ParameterNames[i] = cp;
}
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetParameterValue(int i, double val)
{
if (i < this->NumberOfParameters)
{
if (this->ParameterValues[i] != val)
{
this->ParameterValues[i] = val;
this->Iterations = 0; // reset to start
this->FunctionEvaluations = 0;
this->Modified();
}
return;
}
int n = this->NumberOfParameters + 1;
char **newParameterNames = new char *[n];
double *newParameterValues = new double[n];
double *newParameterScales = new double[n];
for (int j = 0; j < this->NumberOfParameters; j++)
{
newParameterNames[j] = this->ParameterNames[j];
this->ParameterNames[j] = NULL; // or else it will be deleted in Initialize
newParameterValues[j] = this->ParameterValues[j];
newParameterScales[j] = this->ParameterScales[j];
}
newParameterNames[n-1] = 0;
newParameterValues[n-1] = val;
newParameterScales[n-1] = 1.0;
this->Initialize();
this->NumberOfParameters = n;
this->ParameterNames = newParameterNames;
this->ParameterValues = newParameterValues;
this->ParameterScales = newParameterScales;
this->Iterations = 0; // reset to start
this->FunctionEvaluations = 0;
}
//----------------------------------------------------------------------------
double vtkAmoebaMinimizer::GetParameterScale(const char *name)
{
for (int i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i] && strcmp(name,this->ParameterNames[i]) == 0)
{
return this->ParameterScales[i];
}
}
vtkErrorMacro("GetParameterScale: no parameter named " << name);
return 1.0;
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetParameterScale(const char *name, double scale)
{
for (int i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i] && strcmp(name,this->ParameterNames[i]) == 0)
{
this->SetParameterScale(i, scale);
return;
}
}
vtkErrorMacro("SetParameterScale: no parameter named " << name);
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::SetParameterScale(int i, double scale)
{
if (i < 0 || i > this->NumberOfParameters)
{
vtkErrorMacro("SetParameterScale: parameter number out of range: " << i);
return;
}
if (this->ParameterScales[i] != scale)
{
this->ParameterScales[i] = scale;
this->Modified();
}
}
//----------------------------------------------------------------------------
// reset the number of parameters to zero
void vtkAmoebaMinimizer::Initialize()
{
if (this->ParameterNames)
{
for (int i = 0; i < this->NumberOfParameters; i++)
{
if (this->ParameterNames[i])
{
delete [] this->ParameterNames[i];
}
}
delete [] this->ParameterNames;
this->ParameterNames = 0;
}
if (this->ParameterValues)
{
delete [] this->ParameterValues;
this->ParameterValues = 0;
}
if (this->ParameterScales)
{
delete [] this->ParameterScales;
this->ParameterScales = 0;
}
this->NumberOfParameters = 0;
this->Iterations = 0;
this->FunctionEvaluations = 0;
this->Modified();
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::EvaluateFunction()
{
if (this->Function)
{
this->Function(this->FunctionArg);
}
this->FunctionEvaluations++;
}
//----------------------------------------------------------------------------
int vtkAmoebaMinimizer::Iterate()
{
if (this->Iterations == 0)
{
if (!this->Function)
{
vtkErrorMacro("Iterate: Function is NULL");
return 0;
}
this->InitializeAmoeba();
}
int improved = this->PerformAmoeba();
this->GetAmoebaParameterValues();
this->Iterations++;
return improved;
}
//----------------------------------------------------------------------------
void vtkAmoebaMinimizer::Minimize()
{
if (this->Iterations == 0)
{
if (!this->Function)
{
vtkErrorMacro("Minimize: Function is NULL");
return;
}
this->InitializeAmoeba();
}
for (; this->Iterations < this->MaxIterations; this->Iterations++)
{
int improved = this->PerformAmoeba();
if (!improved)
{
break;
}
}
this->GetAmoebaParameterValues();
}
/* ----------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1993,1994,1995 David MacDonald,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
/* ------------------------------------------------
This code has been modified from the original. Several macros
have been expanded, functions have been renamed to match VTK
conventions, and the formatting has been changed.
*/
#define VTK_AMOEBA_FLIP_RATIO 1.0
#define VTK_AMOEBA_CONTRACT_RATIO 0.5
#define VTK_AMOEBA_STRETCH_RATIO 2.0
/* ----------------------------- MNI Header -----------------------------------
@NAME : vtkAmoebaNumericallyClose
@INPUT : n1
n2
threshold_ratio
@OUTPUT :
@RETURNS : true if the numbers are within the threshold ratio
@DESCRIPTION: Decides if two numbers are close to each other.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
#define VTK_AMOEBA_SMALLEST 1.0e-20
static int vtkAmoebaNumericallyClose(double n1,
double n2,
double threshold_ratio )
{
double avg, diff, abs_n1, abs_n2;
diff = n1 - n2;
if( diff < 0.0 )
{
diff = -diff;
}
abs_n1 = (n1 < 0.0 ? -n1 : n1);
abs_n2 = (n2 < 0.0 ? -n2 : n2);
if( abs_n1 < VTK_AMOEBA_SMALLEST || abs_n2 < VTK_AMOEBA_SMALLEST )
{
return( abs_n1 < threshold_ratio && abs_n2 < threshold_ratio );
}
avg = (n1 + n2) / 2.0;
if( avg == 0.0 )
{
return( diff <= threshold_ratio );
}
if( avg < 0.0 )
{
avg = -avg;
}
return( (diff / avg) <= threshold_ratio );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : InitializeAmoeba
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: Initializes the amoeba structure to minimize the function.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
void vtkAmoebaMinimizer::InitializeAmoeba()
{
int i, j;
this->TerminateAmoeba();
int n_parameters = this->NumberOfParameters;
this->AmoebaNStepsNoImprovement = 0;
this->AmoebaVertices = new double *[n_parameters+1];
this->AmoebaVertices[0] = new double[n_parameters*(n_parameters+1)];
for( i = 1 ; i < n_parameters+1 ; i++)
{
this->AmoebaVertices[i] = this->AmoebaVertices[i-1] + n_parameters;
}
this->AmoebaValues = new double[n_parameters+1];
this->AmoebaSum = new double[n_parameters];
for (j = 0; j < n_parameters; j++)
{
this->AmoebaSum[j] = 0.0;
}
for( i = 0 ; i < n_parameters+1 ; i++ )
{
for( j = 0; j < n_parameters ; j++ )
{
this->AmoebaVertices[i][j] = this->ParameterValues[j];
if( i > 0 && j == i - 1 )
{
this->AmoebaVertices[i][j] =
this->ParameterValues[j] + this->ParameterScales[j];
}
this->AmoebaSum[j] += this->ParameterValues[j];
}
}
for( i = 0 ; i < n_parameters+1 ; i++ )
{
for( j = 0; j < n_parameters; j++ )
{
this->ParameterValues[j] = this->AmoebaVertices[i][j];
}
this->EvaluateFunction();
this->AmoebaValues[i] = this->FunctionValue;
}
for ( j = 0 ; j < n_parameters ; j++ )
{
this->ParameterValues[j] = this->AmoebaVertices[0][j];
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : GetAmoebaParameterValues
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: Passes back the current position of the amoeba (best value),
and returns the function value at that point.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
void vtkAmoebaMinimizer::GetAmoebaParameterValues()
{
int i, j, low;
low = 0;
for( i = 1 ; i < this->NumberOfParameters+1 ; i++ )
{
if( this->AmoebaValues[i] < this->AmoebaValues[low] )
{
low = i;
}
}
for( j = 0 ; j < this->NumberOfParameters ; j++ )
{
this->ParameterValues[j] = this->AmoebaVertices[low][j];
}
this->FunctionValue = this->AmoebaValues[low];
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : TerminateAmoeba
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: Frees the amoeba.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
void vtkAmoebaMinimizer::TerminateAmoeba()
{
if (this->AmoebaVertices)
{
delete [] this->AmoebaVertices[0];
delete [] this->AmoebaVertices;
this->AmoebaVertices = NULL;
}
if (this->AmoebaValues)
{
delete [] this->AmoebaValues;
this->AmoebaValues = NULL;
}
if (this->AmoebaSum)
{
delete [] this->AmoebaSum;
this->AmoebaSum = NULL;
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : TryAmoeba
@INPUT : sum
high
fac
@OUTPUT :
@RETURNS : value
@DESCRIPTION: Does a modification to the high vertex of the amoeba and
returns the value of the new point. If the new point is
better (smaller value), it replaces the high vertex of the
amoeba.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
double vtkAmoebaMinimizer::TryAmoeba(double sum[],
int high,
double fac )
{
int j;
double y_try, fac1, fac2;
double *parameters;
parameters = this->ParameterValues;
fac1 = (1.0 - fac) / this->NumberOfParameters;
fac2 = fac - fac1;
for( j = 0 ; j < this->NumberOfParameters ; j++ )
{
parameters[j] = (sum[j] * fac1 + this->AmoebaVertices[high][j] * fac2);
}
this->EvaluateFunction();
y_try = this->FunctionValue;
if( y_try < this->AmoebaValues[high] )
{
this->AmoebaValues[high] = y_try;
for( j = 0 ; j < this->NumberOfParameters ; j++ )
{
sum[j] += parameters[j] - this->AmoebaVertices[high][j];
this->AmoebaVertices[high][j] = parameters[j];
}
}
return( y_try );
}
#define N_STEPS_NO_IMPROVEMENT 20
/* ----------------------------- MNI Header -----------------------------------
@NAME : PerformAmoeba
@INPUT :
@OUTPUT :
@RETURNS : true if numerically significant improvement
@DESCRIPTION: Performs one iteration of an amoeba, returning true if a
numerically significant improvement has been found recently.
Even if it returns 0, you can keep calling this function,
since it may be contracting with no improvement, but will
eventually shrink small enough to get an improvment.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : 2002 David Gobbi
---------------------------------------------------------------------------- */
int vtkAmoebaMinimizer::PerformAmoeba()
{
int i, j, low, high, next_high;
double y_try, y_save;
int improvement_found;
improvement_found = 1;
if( this->AmoebaValues[0] > this->AmoebaValues[1] )
{
high = 0;
next_high = 1;
}
else
{
high = 1;
next_high = 0;
}
low = next_high;
for( i = 2 ; i < this->NumberOfParameters+1 ; i++ )
{
if( this->AmoebaValues[i] < this->AmoebaValues[low] )
{
low = i;
}
else if( this->AmoebaValues[i] > this->AmoebaValues[high] )
{
next_high = high;
high = i;
}
else if( this->AmoebaValues[i] > this->AmoebaValues[next_high] )
{
next_high = i;
}
}
if( vtkAmoebaNumericallyClose( this->AmoebaValues[low],
this->AmoebaValues[high],
this->Tolerance ) )
{
++this->AmoebaNStepsNoImprovement;
if( this->AmoebaNStepsNoImprovement >= N_STEPS_NO_IMPROVEMENT )
{
improvement_found = 0;
}
}
else
{
this->AmoebaNStepsNoImprovement = 0;
}
y_try = this->TryAmoeba( this->AmoebaSum, high, -VTK_AMOEBA_FLIP_RATIO );
if( y_try <= this->AmoebaValues[low] )
{
TryAmoeba( this->AmoebaSum, high, VTK_AMOEBA_STRETCH_RATIO );
}
else if( y_try >= this->AmoebaValues[next_high] )
{
y_save = this->AmoebaValues[high];
y_try = TryAmoeba( this->AmoebaSum, high, VTK_AMOEBA_CONTRACT_RATIO );
if( y_try >= y_save )
{
for( i = 0 ; i < this->NumberOfParameters+1 ; i++)
{
if( i != low )
{
for( j = 0 ; j < this->NumberOfParameters ; j++ )
{
this->ParameterValues[j] = (this->AmoebaVertices[i][j] +
this->AmoebaVertices[low][j]) / 2.0;
this->AmoebaVertices[i][j] = this->ParameterValues[j];
}
this->EvaluateFunction();
this->AmoebaValues[i] = this->FunctionValue;
}
}
for( j = 0 ; j < this->NumberOfParameters ; j++ )
{
this->AmoebaSum[j] = 0.0;
for( i = 0 ; i < this->NumberOfParameters+1 ; i++ )
{
this->AmoebaSum[j] += this->AmoebaVertices[i][j];
}
}
}
}
return( improvement_found );
}
|