35 CombinatoricJigsaw::CombinatoricJigsaw(
const std::string& sname,
36 const std::string& stitle,
37 int Ncomb,
int Nobject)
38 : Jigsaw(sname, stitle, Ncomb, Nobject),
39 m_Ncomb(Ncomb), m_Nobj(Nobject)
41 m_Type = kCombinatoricJigsaw;
42 for(
int i = 0; i < m_Ncomb; i++){
43 m_ChildStates += GetNewChildState();
47 CombinatoricJigsaw::CombinatoricJigsaw()
48 : Jigsaw(), m_Ncomb(0), m_Nobj(0) {}
50 CombinatoricJigsaw::~CombinatoricJigsaw() {}
57 return MinMassesCombJigsaw::Empty();
60 void CombinatoricJigsaw::SetGroup(Group& group){
62 if(!group.IsCombinatoricGroup())
return;
63 Jigsaw::SetGroup(group);
66 CombinatoricGroup& CombinatoricJigsaw::GetGroup()
const {
67 if(!Jigsaw::GetGroup())
68 return CombinatoricGroup::Empty();
70 return static_cast<CombinatoricGroup&
>(Jigsaw::GetGroup());
73 void CombinatoricJigsaw::SetParentState(State& state){
75 if(!state.IsCombinatoricState())
return;
76 Jigsaw::SetParentState(state);
79 CombinatoricState
const& CombinatoricJigsaw::GetParentState()
const {
80 if(!Jigsaw::GetParentState())
81 return CombinatoricState::Empty();
83 return static_cast<const CombinatoricState&
>(Jigsaw::GetParentState());
86 CombinatoricState& CombinatoricJigsaw::GetChildState(
int i)
const {
87 if(!Jigsaw::GetChildState(i))
88 return CombinatoricState::Empty();
90 return static_cast<CombinatoricState&
>(Jigsaw::GetChildState(i));
93 void CombinatoricJigsaw::AddCombFrame(
const RestFrame& frame,
int i){
96 ConstRestFrameList frames =
97 frame.GetListVisibleFrames();
98 int N = frames.GetN();
99 for(
int f = 0; f < N; f++)
100 AddChildFrame(frames[f], i);
103 void CombinatoricJigsaw::AddCombFrames(
const ConstRestFrameList& frames,
int i){
104 int N = frames.GetN();
105 for(
int f = 0; f < N; f++)
106 AddCombFrame(frames[f], i);
109 void CombinatoricJigsaw::AddObjectFrame(
const RestFrame& frame,
int i){
112 ConstRestFrameList frames =
113 frame.GetListVisibleFrames()+
114 frame.GetListInvisibleFrames();
115 int N = frames.GetN();
116 for(
int f = 0; f < N; f++)
117 AddDependancyFrame(frames[f], i);
120 void CombinatoricJigsaw::AddObjectFrames(
const ConstRestFrameList& frames,
int i){
121 int N = frames.GetN();
122 for(
int f = 0; f < N; f++)
123 AddObjectFrame(frames[f], i);
126 void CombinatoricJigsaw::SetCombCharge(
const RFCharge& charge,
int i){
127 if(i < 0 || i >= m_Ncomb)
130 m_ChargeForChild[i] = charge;
133 void CombinatoricJigsaw::SetCombCharge(
int charge,
int i){
134 SetCombCharge(RFCharge(charge), i);
137 void CombinatoricJigsaw::SetCombCharge(
int charge_num,
int charge_den,
int i){
138 SetCombCharge(RFCharge(charge_num, charge_den), i);
141 void CombinatoricJigsaw::UnsetCombCharge(
int i){
142 if(m_ChargeForChild.count(i) > 0)
143 m_ChargeForChild.erase(i);
146 void CombinatoricJigsaw::SetObjectCharge(
const RFCharge& charge,
int i){
147 if(i < 0 || i >= m_Nobj)
150 m_ChargeForObject[i] = charge;
153 void CombinatoricJigsaw::SetObjectCharge(
int charge,
int i){
154 SetObjectCharge(RFCharge(charge), i);
157 void CombinatoricJigsaw::SetObjectCharge(
int charge_num,
int charge_den,
int i){
158 SetObjectCharge(RFCharge(charge_num, charge_den), i);
161 void CombinatoricJigsaw::UnsetObjectCharge(
int i){
162 if(m_ChargeForObject.count(i) > 0)
163 m_ChargeForObject.erase(i);
166 bool CombinatoricJigsaw::InitializeJigsawExecutionList(JigsawList& exec_jigsaws){
167 if(!IsSoundMind())
return false;
168 if(exec_jigsaws.Contains(*
this))
return true;
170 m_ExecuteJigsaws.Clear();
173 JigsawList group_jigsaws;
174 FillGroupJigsawDependancies(group_jigsaws);
175 group_jigsaws -= *
this;
177 int Ngroup = group_jigsaws.GetN();
178 for(
int i = Ngroup-1; i >= 0; i--){
179 Jigsaw& jigsaw = group_jigsaws[i];
180 m_DependancyJigsaws -= jigsaw;
181 if(!exec_jigsaws.Contains(jigsaw))
182 if(!jigsaw.InitializeJigsawExecutionList(exec_jigsaws))
183 return SetMind(
false);
186 while(m_DependancyJigsaws.GetN() > 0){
187 Jigsaw& jigsaw = m_DependancyJigsaws[m_DependancyJigsaws.GetN()-1];
188 if(exec_jigsaws.Contains(jigsaw)){
189 m_DependancyJigsaws -= jigsaw;
192 if(!jigsaw.DependsOnJigsaw(*
this)){
193 if(!jigsaw.InitializeJigsawExecutionList(exec_jigsaws)){
194 return SetMind(
false);
196 m_DependancyJigsaws -= jigsaw;
199 JigsawList temp_exec_jigsaws = exec_jigsaws;
200 temp_exec_jigsaws += m_ExecuteJigsaws;
201 temp_exec_jigsaws += *
this;
202 if(!jigsaw.InitializeJigsawExecutionList(temp_exec_jigsaws))
203 return SetMind(
false);
204 temp_exec_jigsaws -= *
this;
205 temp_exec_jigsaws -= exec_jigsaws;
207 m_DependancyJigsaws -= temp_exec_jigsaws;
208 m_ExecuteJigsaws += temp_exec_jigsaws;
211 exec_jigsaws += *
this;
212 exec_jigsaws += m_ExecuteJigsaws;
216 CombinatoricState& CombinatoricJigsaw::GetNewChildState(){
218 sprintf(strn,
"%d",GetNChildren());
219 std::string name =
GetName()+
"_"+std::string(strn);
220 CombinatoricState* statePtr =
new CombinatoricState(name, name);
225 bool CombinatoricJigsaw::ExecuteDependancyJigsaws(){
226 int N = m_ExecuteJigsaws.GetN();
227 for(
int i = 0; i < N; i++)
228 if(!m_ExecuteJigsaws[i].AnalyzeEvent())
233 bool CombinatoricJigsaw::AnalyzeEvent(){
234 if(!IsSoundMind() || !GetGroup())
235 return SetSpirit(
false);
237 if(!InitializeCombinatoric()){
239 m_Log <<
"Problem initializing event info" << LogEnd;
240 return SetSpirit(
false);
243 if(!LoopCombinatoric()){
245 m_Log <<
"Problem looping over combinatorics" << LogEnd;
246 return SetSpirit(
false);
249 return SetSpirit(
true);
252 bool CombinatoricJigsaw::InitializeAnalysis(){
253 if(!Jigsaw::InitializeAnalysis())
254 return SetMind(
false);
256 CombinatoricGroup& group = GetGroup();
259 m_NExclusive.clear();
262 m_NExclusiveTOT =
true;
264 for(
int i = 0; i < m_Ncomb; i++){
265 RestFrameList
const& frames = GetChildState(i).GetListFrames();
266 int Nf = frames.GetN();
269 for(
int f = 0; f < Nf; f++){
272 group.GetNElementsForFrame(frames[f], N, excl);
273 if(N < 0)
return SetMind(
false);
275 exclTOT = exclTOT && excl;
277 m_NForChild.push_back(NTOT);
278 m_NExclusive.push_back(exclTOT);
280 m_NExclusiveTOT = m_NExclusiveTOT && exclTOT;
283 return SetMind(
true);
286 bool CombinatoricJigsaw::InitializeCombinatoric(){
288 return SetSpirit(
false);
291 return SetSpirit(
false);
293 if(!GetParentState())
294 return SetSpirit(
false);
296 m_InputStates.Clear();
297 m_InputStates = GetParentState().GetElements();
299 if(m_InputStates.GetN() < m_NinputTOT){
301 m_Log <<
"Unable to execute Jigsaw. ";
302 m_Log <<
"Insufficienct number of inputs: ";
303 m_Log << m_NinputTOT <<
" (required) != ";
304 m_Log << m_InputStates.GetN() <<
" (provided)";
306 return SetSpirit(
false);
309 if(m_NExclusiveTOT &&
310 (m_InputStates.GetN() != m_NinputTOT)){
312 m_Log <<
"Unable to execute Jigsaw. ";
313 m_Log <<
"Incorrect number of exclusive inputs: ";
314 m_Log << m_NinputTOT <<
" (required) != ";
315 m_Log << m_InputStates.GetN() <<
" (provided)";
317 return SetSpirit(
false);
320 return SetSpirit(
true);
323 int CombinatoricJigsaw::GetNInputStates()
const {
324 return m_InputStates.GetN();
327 VisibleState& CombinatoricJigsaw::GetInputState(
int i)
const {
328 return m_InputStates[i];
331 int CombinatoricJigsaw::GetNinputForChild(
int i)
const {
332 if(i < 0 || i >= m_Ncomb)
334 return m_NForChild[i];
337 bool CombinatoricJigsaw::IsNinputExclForChild(
int i)
const {
338 if(i < 0 || i >= m_Ncomb)
340 return m_NExclusive[i];
343 bool CombinatoricJigsaw::IsChargeSetForChild(
int i)
const {
344 return !(m_ChargeForChild.count(i) == 0);
347 RFCharge CombinatoricJigsaw::GetChargeForChild(
int i)
const {
348 if(IsChargeSetForChild(i))
349 return m_ChargeForChild[i];
354 bool CombinatoricJigsaw::IsChargeSetForObject(
int i)
const {
355 return !(m_ChargeForObject.count(i) == 0);
358 RFCharge CombinatoricJigsaw::GetChargeForObject(
int i)
const {
359 if(IsChargeSetForObject(i))
360 return m_ChargeForObject[i];
365 bool CombinatoricJigsaw::LoopCombinatoric(){
366 int Ninput = m_InputStates.GetN();
369 for(
int i = 0; i < Ninput; i++) N_comb *= m_Ncomb;
372 double metric_min = -1;
374 for(
int c = 0; c < N_comb; c++){
376 for(
int i = 0; i < m_Ncomb; i++)
377 GetChildState(i).ClearElements();
380 for(
int i = 0; i < Ninput; i++){
381 int ihem = key%m_Ncomb;
383 GetChildState(ihem).AddElement(m_InputStates[i]);
388 for(
int i = 0; i < m_Ncomb; i++){
389 if(IsNinputExclForChild(i)){
390 if(GetChildState(i).GetNElements() != GetNinputForChild(i)){
395 if(GetChildState(i).GetNElements() < GetNinputForChild(i)){
400 if(IsChargeSetForChild(i)){
401 if(GetChildState(i).GetCharge() != GetChargeForChild(i)){
411 if(!ExecuteDependancyJigsaws())
415 for(
int i = 0; i < m_Nobj; i++){
416 if(IsChargeSetForObject(i)){
417 if(GetDependancyStates(i).GetCharge() != GetChargeForObject(i)){
428 if(!EvaluateMetric(metric))
431 if(metric < metric_min || c_min < 0){
436 if((metric < metric_min && metric >= 0.) || c_min < 0){
444 m_Log <<
"Unable to find combinatoric satisfying ";
445 m_Log <<
"requested conditions." << LogEnd;
446 return SetSpirit(
false);
450 for(
int i = 0; i < m_Ncomb; i++)
451 GetChildState(i).ClearElements();
453 for(
int i = 0; i < Ninput; i++){
454 int ihem = key%m_Ncomb;
456 GetChildState(ihem).AddElement(m_InputStates[i]);
460 if(!ExecuteDependancyJigsaws())
461 return SetSpirit(
false);
463 return SetSpirit(
true);
466 bool CombinatoricJigsaw::IsSoundBody()
const {
467 if(RFBase::IsSoundBody())
470 if(!Jigsaw::IsSoundBody())
471 return SetBody(
false);
473 for(
int i = 0; i < m_Nobj; i++){
474 if(GetDependancyFrames(i).GetN() <= 0){
476 m_Log <<
"Empty collection of object frames: " << i;
478 return SetBody(
false);
482 return SetBody(
true);
std::string GetName() const
Returns object name.
virtual void Clear()
Clears Jigsaw of all connections to other objects.
void AddDependent(RFBase *dep)
pointer to RFBase object owned by this one
virtual void Clear()
Clears Jigsaw of all connections to other objects.