Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:31

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2019 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/tools/output_test_stream.hpp>
0011 #include <boost/test/unit_test.hpp>
0012 
0013 #include "Acts/Utilities/AnnealingUtility.hpp"
0014 
0015 #include <iostream>
0016 #include <vector>
0017 
0018 namespace Acts::Test {
0019 
0020 BOOST_AUTO_TEST_CASE(annealing_tool_singleChi2_tests) {
0021   std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
0022   AnnealingUtility::Config config;
0023   config.setOfTemperatures = temperatures;
0024   AnnealingUtility annealingTool(config);
0025 
0026   AnnealingUtility::State state;
0027 
0028   // Test weight decrease when annealing for chi2>cutOff
0029   // choose a chi2 greater than default config.cutOff (=9.),
0030   // such that it should decrease with decreasing temperature
0031   double chi2 = config.cutOff + 3.14;
0032 
0033   // cache last weight
0034   double previousWeight = 1.;
0035 
0036   std::cout << "Check weight decrease:" << std::endl;
0037   for (auto temp : temperatures) {
0038     double weight = annealingTool.getWeight(state, chi2);
0039 
0040     bool hasDecreased = weight < previousWeight;
0041 
0042     BOOST_CHECK(hasDecreased);
0043 
0044     previousWeight = weight;
0045     annealingTool.anneal(state);
0046 
0047     std::cout << "\tTemperature: " << temp << ", weight: " << weight
0048               << std::endl;
0049   }
0050 
0051   // equilibrium should be reached here
0052   BOOST_CHECK_EQUAL(state.equilibriumReached, true);
0053 
0054   // test reset
0055   state = AnnealingUtility::State();
0056 
0057   BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
0058   BOOST_CHECK_EQUAL(state.equilibriumReached, false);
0059 
0060   // Test weight increase when annealing for chi2<cutOff
0061   // choose a chi2 smaller than default config.cutOff (=9.),
0062   // such that it should increase with decreasing temperature
0063   chi2 = config.cutOff - 3.14;
0064 
0065   // cache last weight
0066   previousWeight = 0.;
0067 
0068   std::cout << "Check weight increase:" << std::endl;
0069   for (auto temp : temperatures) {
0070     double weight = annealingTool.getWeight(state, chi2);
0071 
0072     bool hasIncreased = weight > previousWeight;
0073 
0074     BOOST_CHECK(hasIncreased);
0075 
0076     previousWeight = weight;
0077     annealingTool.anneal(state);
0078 
0079     std::cout << "\tTemperature: " << temp << ", weight: " << weight
0080               << std::endl;
0081   }
0082 
0083   // reset for last test
0084   state = AnnealingUtility::State();
0085 
0086   // Test weight insensitivity when annealing for chi2==cutOff
0087   // choose a chi2 equal default config.cutOff (=9.),
0088   // such that it should be insensitive to decreasing temperature
0089   chi2 = config.cutOff;
0090 
0091   // cache last weight
0092   previousWeight = 0.5;
0093 
0094   std::cout << "Check weight insensitivity:" << std::endl;
0095   for (auto temp : temperatures) {
0096     double weight = annealingTool.getWeight(state, chi2);
0097 
0098     bool hasNotChanged = weight == previousWeight;
0099 
0100     BOOST_CHECK(hasNotChanged);
0101 
0102     previousWeight = weight;
0103     annealingTool.anneal(state);
0104 
0105     std::cout << "\tTemperature: " << temp << ", weight: " << weight
0106               << std::endl;
0107   }
0108 }
0109 
0110 BOOST_AUTO_TEST_CASE(annealing_tool_multiChi2_tests) {
0111   // vector of different chi2
0112   std::vector<double> allChi2{1.3, 4.5, 8.4,  0.4, 10.3, 12.3,
0113                               3.5, 5.8, 11.0, 1.1, 3.5,  6.7};
0114 
0115   std::vector<double> temperatures{64., 16., 4., 2., 1.5, 1.};
0116   AnnealingUtility::Config config;
0117   config.setOfTemperatures = {64., 16., 4., 2., 1.5, 1.};
0118   AnnealingUtility annealingTool(config);
0119 
0120   AnnealingUtility::State state;
0121 
0122   // Test weight decrease when annealing for chi2>cutOff
0123   // choose a chi2 greater than default config.cutOff (=9.),
0124   // such that it should decrease with decreasing temperature
0125   double chi2 = config.cutOff + 5.;
0126 
0127   // cache last weight
0128   double previousWeight = 1.;
0129 
0130   std::cout << "Check weight decrease:" << std::endl;
0131   for (auto temp : temperatures) {
0132     double weight = annealingTool.getWeight(state, chi2, allChi2);
0133 
0134     bool hasDecreased = weight < previousWeight;
0135 
0136     BOOST_CHECK(hasDecreased);
0137 
0138     previousWeight = weight;
0139     annealingTool.anneal(state);
0140 
0141     std::cout << "\tTemperature: " << temp << ", weight: " << weight
0142               << std::endl;
0143   }
0144 
0145   // equilibrium should be reached here
0146   BOOST_CHECK_EQUAL(state.equilibriumReached, true);
0147 
0148   // test reset
0149   state = AnnealingUtility::State();
0150 
0151   BOOST_CHECK_EQUAL(state.currentTemperatureIndex, 0u);
0152   BOOST_CHECK_EQUAL(state.equilibriumReached, false);
0153 
0154   // Test weight increase when annealing for chi2<cutOff
0155   // choose a chi2 smaller than default config.cutOff (=9.),
0156   // such that it should increase with decreasing temperature
0157   chi2 = 1.234;
0158 
0159   // cache last weight
0160   previousWeight = 0.;
0161 
0162   std::cout << "Check weight increase:" << std::endl;
0163   for (auto temp : temperatures) {
0164     double weight = annealingTool.getWeight(state, chi2, allChi2);
0165 
0166     bool hasIncreased = weight > previousWeight;
0167 
0168     BOOST_CHECK(hasIncreased);
0169 
0170     previousWeight = weight;
0171     annealingTool.anneal(state);
0172 
0173     std::cout << "\tTemperature: " << temp << ", weight: " << weight
0174               << std::endl;
0175   }
0176 }
0177 
0178 }  // namespace Acts::Test