Analysis Software
Documentation for sPHENIX simulation 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 #include <vector>
17 
18 namespace Acts {
19 namespace Test {
20 
21 BOOST_AUTO_TEST_CASE(annealing_tool_singleChi2_tests) {
22  std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
24  config.setOfTemperatures = temperatures;
25  AnnealingUtility annealingTool(config);
26 
28 
29  // Test weight decrease when annealing for chi2>cutOff
30  // choose a chi2 greater than default config.cutOff (=9.),
31  // such that it should decrease with decreasing temperature
32  double chi2 = config.cutOff + 3.14;
33 
34  // cache last weight
35  double previousWeight = 1.;
36 
37  std::cout << "Check weight decrease:" << std::endl;
38  for (auto temp : temperatures) {
39  double weight = annealingTool.getWeight(state, chi2);
40 
41  bool hasDecreased = weight < previousWeight;
42 
43  BOOST_CHECK(hasDecreased);
44 
45  previousWeight = weight;
46  annealingTool.anneal(state);
47 
48  std::cout << "\tTemperature: " << temp << ", weight: " << weight
49  << std::endl;
50  }
51 
52  // equilibrium should be reached here
53  BOOST_CHECK_EQUAL(state.equilibriumReached, true);
54 
55  // test reset
56  state = AnnealingUtility::State();
57 
58  BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
59  BOOST_CHECK_EQUAL(state.equilibriumReached, false);
60 
61  // Test weight increase when annealing for chi2<cutOff
62  // choose a chi2 smaller than default config.cutOff (=9.),
63  // such that it should increase with decreasing temperature
64  chi2 = config.cutOff - 3.14;
65 
66  // cache last weight
67  previousWeight = 0.;
68 
69  std::cout << "Check weight increase:" << std::endl;
70  for (auto temp : temperatures) {
71  double weight = annealingTool.getWeight(state, chi2);
72 
73  bool hasIncreased = weight > previousWeight;
74 
75  BOOST_CHECK(hasIncreased);
76 
77  previousWeight = weight;
78  annealingTool.anneal(state);
79 
80  std::cout << "\tTemperature: " << temp << ", weight: " << weight
81  << std::endl;
82  }
83 
84  // reset for last test
85  state = AnnealingUtility::State();
86 
87  // Test weight insensitivity when annealing for chi2==cutOff
88  // choose a chi2 equal default config.cutOff (=9.),
89  // such that it should be insensitive to decreasing temperature
90  chi2 = config.cutOff;
91 
92  // cache last weight
93  previousWeight = 0.5;
94 
95  std::cout << "Check weight insensitivity:" << std::endl;
96  for (auto temp : temperatures) {
97  double weight = annealingTool.getWeight(state, chi2);
98 
99  bool hasNotChanged = weight == previousWeight;
100 
101  BOOST_CHECK(hasNotChanged);
102 
103  previousWeight = weight;
104  annealingTool.anneal(state);
105 
106  std::cout << "\tTemperature: " << temp << ", weight: " << weight
107  << std::endl;
108  }
109 }
110 
111 BOOST_AUTO_TEST_CASE(annealing_tool_multiChi2_tests) {
112  // vector of different chi2
113  std::vector<double> allChi2{1.3, 4.5, 8.4, 0.4, 10.3, 12.3,
114  3.5, 5.8, 11.0, 1.1, 3.5, 6.7};
115 
116  std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
118  config.setOfTemperatures = {64., 16., 4., 2., 1.5, 1.};
119  AnnealingUtility annealingTool(config);
120 
122 
123  // Test weight decrease when annealing for chi2>cutOff
124  // choose a chi2 greater than default config.cutOff (=9.),
125  // such that it should decrease with decreasing temperature
126  double chi2 = config.cutOff + 5.;
127 
128  // cache last weight
129  double previousWeight = 1.;
130 
131  std::cout << "Check weight decrease:" << std::endl;
132  for (auto temp : temperatures) {
133  double weight = annealingTool.getWeight(state, chi2, allChi2);
134 
135  bool hasDecreased = weight < previousWeight;
136 
137  BOOST_CHECK(hasDecreased);
138 
139  previousWeight = weight;
140  annealingTool.anneal(state);
141 
142  std::cout << "\tTemperature: " << temp << ", weight: " << weight
143  << std::endl;
144  }
145 
146  // equilibrium should be reached here
147  BOOST_CHECK_EQUAL(state.equilibriumReached, true);
148 
149  // test reset
150  state = AnnealingUtility::State();
151 
152  BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
153  BOOST_CHECK_EQUAL(state.equilibriumReached, false);
154 
155  // Test weight increase when annealing for chi2<cutOff
156  // choose a chi2 smaller than default config.cutOff (=9.),
157  // such that it should increase with decreasing temperature
158  chi2 = 1.234;
159 
160  // cache last weight
161  previousWeight = 0.;
162 
163  std::cout << "Check weight increase:" << std::endl;
164  for (auto temp : temperatures) {
165  double weight = annealingTool.getWeight(state, chi2, allChi2);
166 
167  bool hasIncreased = weight > previousWeight;
168 
169  BOOST_CHECK(hasIncreased);
170 
171  previousWeight = weight;
172  annealingTool.anneal(state);
173 
174  std::cout << "\tTemperature: " << temp << ", weight: " << weight
175  << std::endl;
176  }
177 }
178 
179 } // namespace Test
180 } // namespace Acts