Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsSimulatedAnnealingManager.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 //for random number generation
20 #include "vnl/vnl_random.h"
21 #include "vnl/vnl_math.h"
22 
24 : m_Permutation( nullptr )
25 {
26 }
27 
29 {
30 }
31 
32 
33 bool mitk::ConnectomicsSimulatedAnnealingManager::AcceptChange( double costBefore, double costAfter, double temperature )
34 {
35  if( costAfter <= costBefore )
36  {// if cost is lower after
37  return true;
38  }
39 
40  //the random number generators
41  vnl_random rng( (unsigned int) rand() );
42 
43  //randomly generate threshold
44  const double threshold = rng.drand64( 0.0 , 1.0);
45 
46  //the likelihood of acceptance
47  double likelihood = std::exp( - ( costAfter - costBefore ) / temperature );
48 
49  if( threshold < likelihood )
50  {
51  return true;
52  }
53 
54  return false;
55 }
56 
58 {
59  m_Permutation = permutation;
60 }
61 
63  double temperature,
64  double stepSize
65  )
66 {
67  if( m_Permutation.IsNull() )
68  {
69  MBI_ERROR << "Trying to run simulated annealing on empty permutation.";
70  return;
71  }
72 
73  if( !m_Permutation->HasCostFunction() )
74  {
75  MBI_ERROR << "Trying to run simulated annealing on empty cost function.";
76  return;
77  }
78 
79  // Initialize the associated permutation
80  m_Permutation->Initialize();
81 
82 
83  for( double currentTemperature( temperature );
84  currentTemperature > 0.00001;
85  currentTemperature = currentTemperature / stepSize )
86  {
87  // Run Permutations at the current temperature
88  m_Permutation->Permutate( currentTemperature );
89 
90  }
91 
92  // Clean up result
93  m_Permutation->CleanUp();
94 
95 }
itk::SmartPointer< Self > Pointer
void SetPermutation(mitk::ConnectomicsSimulatedAnnealingPermutationBase::Pointer permutation)
bool AcceptChange(double costBefore, double costAfter, double temperature)
#define MBI_ERROR
Definition: mbilog.h:223