Belle II Software  release-06-00-14
CellularAutomaton.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingVXD/algorithms/TrackerAlgorithmBase.h>
11 
12 namespace Belle2 {
20  template<class ContainerType, class ValidatorType>
21  class CellularAutomaton final : public TrackerAlgorithmBase<ContainerType, ValidatorType> {
22  public:
25 
26 
29 
30 
32  unsigned int stopInRound = BaseClass::m_validator.nMaxIterations + 2;
33 
35  // TODO: Check where the -1 is returned, for bad cases as described above.
36  int apply(ContainerType& aNetworkContainer) override final
37  {
43  unsigned int activeCells = 1, // is set 1 because of following while loop.
44  deadCells = 0,
45  caRound = 1,
46  goodNeighbours = 0,
47  highestCellState = 0;
48 
49  // each iteration of following while loop is one CA-time-step
50  while (activeCells != 0 and caRound < stopInRound) {
51  activeCells = 0;
52 
54  // compare cells with inner neighbours:
55  for (auto* aNode : aNetworkContainer) {
56  auto& currentCell = aNode->getMetaInfo();
57  if (currentCell.isActivated() == false) { continue; }
58  goodNeighbours = 0;
59 
60  for (auto* aNeighbour : aNode->getInnerNodes()) {
61  // skip if neighbour has not the same state (NOTE if one wants to improve the versatility of the code,
62  // this should actually become a member of the cell-class, which then can add some extra
63  // stuff like checking for loops.
64  if (currentCell != aNeighbour->getMetaInfo()) continue;
65 
66  goodNeighbours++;
67  }
68  if (goodNeighbours != 0) {
69  currentCell.setStateUpgrade(true);
70  activeCells++;
71  } else {
72  // WARNING setActivationState does provoke unintended behavior, since sometimes states can not be
73  // upgraded in one round, but can in the next round!
74  /*currentCell.setActivationState(false);*/
75  deadCells++;
76  }
77  }
78 
80  for (auto* aNode : aNetworkContainer) {
81  auto& currentCell = aNode->getMetaInfo();
82  if (currentCell.isActivated() == false or currentCell.isUpgradeAllowed() == false) { continue; }
83 
84  currentCell.setStateUpgrade(false);
85  currentCell.increaseState();
86  if (currentCell.getState() > highestCellState) { highestCellState = currentCell.getState(); }
87  }
88 
90  if (BaseClass::m_validator.isValidRound(caRound) == false) {
91  break;
92  }
93 
94  caRound++;
95  } // CA main-loop
96 
97  return caRound;
98  }
99 
100 
104  unsigned int findSeeds(ContainerType& aNetworkContainer, bool strictSeeding = false) override final
105  {
106  unsigned int nSeeds = 0;
107  for (auto* aNode : aNetworkContainer) {
108  if (strictSeeding && !(aNode->getOuterNodes().empty())) continue;
109 
110  if (BaseClass::m_validator.checkSeed(aNode->getMetaInfo()) == true) {
111  aNode->getMetaInfo().setSeed(true);
112  nSeeds++;
113  }
114  }
115  return nSeeds;
116  }
117  };
119 }
The CellularAutomaton class This class serves as a functor for the algorithm itself.
unsigned int stopInRound
aborts CA after stopInRound iterations - mainly for debugging purposes:
int apply(ContainerType &aNetworkContainer) override final
actual algorithm of Cellular Automaton, returns number of rounds needed to finish or -1 if CA was abo...
unsigned int findSeeds(ContainerType &aNetworkContainer, bool strictSeeding=false) override final
checks network given for seeds, returns number of seeds found (if strictSeeding is set to true,...
base class for TrackerAlgorithms. shall allow a common base for algorithms like the cellular automato...
ValidatorType m_validator
something which checks the quality of the test so far (will be applied by the apply-function
Abstract base class for different kinds of events.