EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AnnealingUtilityTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AnnealingUtilityTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
14 
15 #include <iostream>
16 
17 namespace Acts {
18 namespace Test {
19 
20 BOOST_AUTO_TEST_CASE(annealing_tool_singleChi2_tests) {
21  std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
22  AnnealingUtility::Config config(temperatures);
23  AnnealingUtility annealingTool(config);
24 
26 
27  // Test weight decrease when annealing for chi2>cutOff
28  // choose a chi2 greater than default config.cutOff (=9.),
29  // such that it should decrease with decreasing temperature
30  double chi2 = config.cutOff + 3.14;
31 
32  // cache last weight
33  double previousWeight = 1.;
34 
35  std::cout << "Check weight decrease:" << std::endl;
36  for (auto temp : temperatures) {
37  double weight = annealingTool.getWeight(state, chi2);
38 
39  bool hasDecreased = weight < previousWeight;
40 
41  BOOST_CHECK(hasDecreased);
42 
43  previousWeight = weight;
44  annealingTool.anneal(state);
45 
46  std::cout << "\tTemperature: " << temp << ", weight: " << weight
47  << std::endl;
48  }
49 
50  // equilibrium should be reached here
51  BOOST_CHECK_EQUAL(state.equilibriumReached, true);
52 
53  // test reset
54  state = AnnealingUtility::State();
55 
56  BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
57  BOOST_CHECK_EQUAL(state.equilibriumReached, false);
58 
59  // Test weight increase when annealing for chi2<cutOff
60  // choose a chi2 smaller than default config.cutOff (=9.),
61  // such that it should increase with decreasing temperature
62  chi2 = config.cutOff - 3.14;
63 
64  // cache last weight
65  previousWeight = 0.;
66 
67  std::cout << "Check weight increase:" << std::endl;
68  for (auto temp : temperatures) {
69  double weight = annealingTool.getWeight(state, chi2);
70 
71  bool hasIncreased = weight > previousWeight;
72 
73  BOOST_CHECK(hasIncreased);
74 
75  previousWeight = weight;
76  annealingTool.anneal(state);
77 
78  std::cout << "\tTemperature: " << temp << ", weight: " << weight
79  << std::endl;
80  }
81 
82  // reset for last test
83  state = AnnealingUtility::State();
84 
85  // Test weight insensitivity when annealing for chi2==cutOff
86  // choose a chi2 equal default config.cutOff (=9.),
87  // such that it should be insensitive to decreasing temperature
88  chi2 = config.cutOff;
89 
90  // cache last weight
91  previousWeight = 0.5;
92 
93  std::cout << "Check weight insensitivity:" << std::endl;
94  for (auto temp : temperatures) {
95  double weight = annealingTool.getWeight(state, chi2);
96 
97  bool hasNotChanged = weight == previousWeight;
98 
99  BOOST_CHECK(hasNotChanged);
100 
101  previousWeight = weight;
102  annealingTool.anneal(state);
103 
104  std::cout << "\tTemperature: " << temp << ", weight: " << weight
105  << std::endl;
106  }
107 }
108 
109 BOOST_AUTO_TEST_CASE(annealing_tool_multiChi2_tests) {
110  // vector of different chi2
111  std::vector<double> allChi2{1.3, 4.5, 8.4, 0.4, 10.3, 12.3,
112  3.5, 5.8, 11.0, 1.1, 3.5, 6.7};
113 
114  std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
115  AnnealingUtility::Config config(temperatures);
116  AnnealingUtility annealingTool(config);
117 
119 
120  // Test weight decrease when annealing for chi2>cutOff
121  // choose a chi2 greater than default config.cutOff (=9.),
122  // such that it should decrease with decreasing temperature
123  double chi2 = config.cutOff + 5.;
124 
125  // cache last weight
126  double previousWeight = 1.;
127 
128  std::cout << "Check weight decrease:" << std::endl;
129  for (auto temp : temperatures) {
130  double weight = annealingTool.getWeight(state, chi2, allChi2);
131 
132  bool hasDecreased = weight < previousWeight;
133 
134  BOOST_CHECK(hasDecreased);
135 
136  previousWeight = weight;
137  annealingTool.anneal(state);
138 
139  std::cout << "\tTemperature: " << temp << ", weight: " << weight
140  << std::endl;
141  }
142 
143  // equilibrium should be reached here
144  BOOST_CHECK_EQUAL(state.equilibriumReached, true);
145 
146  // test reset
147  state = AnnealingUtility::State();
148 
149  BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
150  BOOST_CHECK_EQUAL(state.equilibriumReached, false);
151 
152  // Test weight increase when annealing for chi2<cutOff
153  // choose a chi2 smaller than default config.cutOff (=9.),
154  // such that it should increase with decreasing temperature
155  chi2 = 1.234;
156 
157  // cache last weight
158  previousWeight = 0.;
159 
160  std::cout << "Check weight increase:" << std::endl;
161  for (auto temp : temperatures) {
162  double weight = annealingTool.getWeight(state, chi2, allChi2);
163 
164  bool hasIncreased = weight > previousWeight;
165 
166  BOOST_CHECK(hasIncreased);
167 
168  previousWeight = weight;
169  annealingTool.anneal(state);
170 
171  std::cout << "\tTemperature: " << temp << ", weight: " << weight
172  << std::endl;
173  }
174 }
175 
176 } // namespace Test
177 } // namespace Acts