1 #ifndef SubsetHopfieldNN_h
2 #define SubsetHopfieldNN_h
4 #include <CLHEP/Random/RandFlat.h>
6 #include "KiTrack/Subset.h"
7 #include "KiTrack/HopfieldNeuralNet.h"
99 template<
class GetQI,
class AreCompatible >
108 _limitForStable = 0.01;
111 _activationThreshold = 0.5;
115 void setTStart(
double tStart ){ _TStart = tStart; }
116 void setTInf(
double tInf ){ _TInf = tInf; }
117 void setOmega(
double omega ){ _omega = omega; }
118 void setLimitForStable(
double limitForStable ){ _limitForStable = limitForStable; }
119 void setInitStateMin(
double initStateMin ){ _initStateMin = initStateMin; }
120 void setInitStateMax(
double initStateMax ){ _initStateMax = initStateMax; }
121 void setActivationThreshold(
double activationThreshold ){ _activationThreshold = activationThreshold; }
123 double getTStart(){
return _TStart; }
124 double getTInf(){
return _TInf; }
125 double getOmega(){
return _omega; }
126 double getLimitForStable(){
return _limitForStable; }
127 double getInitStateMin(){
return _initStateMin; }
128 double getInitStateMax(){
return _initStateMax; }
129 double getActivationThreshold(){
return _activationThreshold; }
137 double _limitForStable;
138 double _initStateMin;
139 double _initStateMax;
140 double _activationThreshold;
145 template<
class T >
template<
class GetQI,
class AreCompatible >
149 unsigned nAccepted=0;
150 unsigned nRejected=0;
151 unsigned nCompWithAll=0;
152 unsigned nIncompatible=0;
155 std::vector< T > elements = this->_elements;
157 unsigned nElements = elements.size();
161 std::vector < std::vector <bool> > G;
162 G.resize( nElements );
163 for (
unsigned i=0; i<nElements; i++) G[i].resize( elements.size() );
165 std::vector < double > QI ;
166 QI.resize( nElements );
168 std::vector < double > states;
169 states.resize( nElements );
179 for (
unsigned i=0; i < nElements ; i++){
182 T elementA = elements[i];
185 QI[i] = getQI( elementA );
187 streamlog_out(DEBUG3) <<
"QI of element " << i <<
" = " << QI[i] <<
"\n";
191 states[i] = CLHEP::RandFlat::shoot ( _initStateMin , _initStateMax );
195 for (
unsigned j=i+1; j < nElements ; j++ ){
197 T elementB = elements[j];
199 if ( areCompatible( elementA , elementB ) ){
219 streamlog_out(DEBUG2) <<
"G:\n";
222 for (
unsigned i=0; i < G.size(); i++ ){
225 for (
unsigned j=0; j < G[i].size(); j++ ){
227 streamlog_out(DEBUG2) << G[i][j] <<
" ";
231 streamlog_out(DEBUG2) <<
"\n";
239 streamlog_out(DEBUG2) <<
"Incompatible ones:\n";
242 for (
unsigned i=0; i < G.size(); i++ ){
244 streamlog_out(DEBUG2) <<
"Element " << i <<
":\t";
246 for (
unsigned j=0; j < G[i].size(); j++ ){
248 if( G[i][j] ) streamlog_out(DEBUG2) << j <<
", ";
252 streamlog_out(DEBUG2) <<
"\n";
263 for(
unsigned i=0; i < elements.size(); i++ ){
266 bool isCompatibleWithAll =
true;
269 for(
unsigned j=0; j < elements.size(); j++){
272 if( (i != j) && G[i][j] ){
274 isCompatibleWithAll =
false;
282 if ( isCompatibleWithAll ){
286 this->_acceptedElements.push_back( elements[i] );
291 elements.erase( elements.begin() + i );
292 states.erase( states.begin() + i );
293 QI.erase( QI.begin() + i );
295 for(
unsigned j=0; j<G.size(); j++ ) G[j].erase( G[j].begin() + i );
296 G.erase( G.begin() + i );
310 streamlog_out( DEBUG3 ) << nCompWithAll <<
" elements are compatible with all others, " << nIncompatible
311 <<
" elements are interfering and will be checked for the best subset\n";
319 if( !elements.empty() ){
323 net.
setT ( _TStart );
327 unsigned nIterations=1;
329 streamlog_out(DEBUG1) <<
"states: ( ";
330 for (
unsigned int i=0; i< states.size(); i++) streamlog_out(DEBUG1) << states[i] <<
" ";
331 streamlog_out(DEBUG1) <<
")\n";
337 std::vector <double> newStates = net.
getStates();
339 streamlog_out(DEBUG1) <<
"states: ( ";
341 for (
unsigned int i=0; i< newStates.size(); i++) streamlog_out(DEBUG1) << newStates[i] <<
" ";
343 streamlog_out(DEBUG1) <<
")\n";
349 streamlog_out( DEBUG3 ) <<
"Hopfield Neural Network is stable after " << nIterations <<
" iterations.\n";
363 for (
unsigned i=0; i < states.size(); i++ ){
366 if ( states[i] >= _activationThreshold ){
368 this->_acceptedElements.push_back( elements[i] );
374 this->_rejectedElements.push_back( elements[i] );
384 streamlog_out( DEBUG3 ) <<
"Hopfield Neural Network accepted " << nAccepted
385 <<
" elements and rejected " << nRejected <<
" elements of all in all "
386 << nAccepted + nRejected <<
"incomaptible elements.\n";
388 streamlog_out( DEBUG3 ) <<
"So in sum " << nAccepted + nCompWithAll
389 <<
" elements survived and " << nRejected <<
" elements got rejected.\n";
void setT(double T)
Sets the temperature of the Neural Network (The HNN is cooled down in every iteration) ...
Definition: HopfieldNeuralNet.h:53
Represents a Hopfield Neural Network.
Definition: HopfieldNeuralNet.h:18
A base class for subsets.
Definition: Subset.h:19
std::vector< double > getStates()
Definition: HopfieldNeuralNet.h:71
bool doIteration()
Does one iteration of the neuronal network.
Definition: HopfieldNeuralNet.cc:165
A class to get the best subset with help of a Hopfield Neural Network.
Definition: SubsetHopfieldNN.h:17
void calculateBestSet(AreCompatible areCompatible, GetQI getQI)
Calculates the best set using a Hopfield Neural Network (see this for more info) .
Definition: SubsetHopfieldNN.h:146
void setLimitForStable(double limit)
Set the threshhold value below which the HNN is seen as "stable".
Definition: HopfieldNeuralNet.h:66
void setTInf(double TInf)
Sets the temperature at infinity.
Definition: HopfieldNeuralNet.h:59