TSP problem fixed
[charm.git] / examples / charm++ / state_space_searchengine / searchengineLib / searchEngine.h
1 /* searchEngine.h
2  *
3  *  Nov 3
4  *
5  * author: Yanhua Sun
6  */
7 #ifndef __SEARCHENGINE__
8 #define __SEARCHENGINE__
9
10 #include "charm++.h"
11 #include "cmipool.h"
12
13 class Solver;
14 class StateBase;
15 class SearchNodeMsg;
16 class CProxy_SearchConductor;
17
18 extern CProxy_SearchConductor searchEngineProxy;
19
20 typedef void (*SE_createInitialChildrenFn)(Solver *solver);
21 typedef void (*SE_createChildrenFn)( StateBase *_base , Solver* solver, bool parallel);
22 typedef int (*SE_parallelLevelFn)();
23 typedef int (*SE_searchDepthLimitFn)();
24 typedef double (*SE_lowerBoundFn)(StateBase *_base);
25
26 extern SE_lowerBoundFn   _lowerBoundFn;
27
28 void SE_register(SE_createInitialChildrenFn  f1,
29                  SE_createChildrenFn  f2,
30                  SE_parallelLevelFn   f3,
31                  SE_searchDepthLimitFn  f4,
32                  SE_lowerBoundFn f5 = NULL
33                  );
34
35 class StateBase
36 {
37 };
38
39 class Solver {
40 protected:
41     UShort parentBits;
42     unsigned int* parentPtr;
43     int searchLevel;
44 public:
45     //Solver(): searchLevel(0) {}
46     virtual StateBase *registerRootState(size_t size, unsigned int childnum, unsigned int totalNumChildren)=0;
47     virtual StateBase *registerState(size_t size, unsigned int childnum, unsigned int totalNumChildren) = 0;
48     virtual void process(StateBase *state)=0;
49     virtual void deleteState(StateBase *state) = 0;
50     virtual void setParentInfo(SearchNodeMsg *msg, int l) = 0;
51     virtual inline void reportSolution();
52     virtual void setPriority(StateBase *state, int p)=0;
53 #ifdef BRANCHBOUND
54     inline void updateCost(double c);
55 #endif
56 };
57
58 #include "searchEngine_impl.h"
59
60 #define MASK  0XFF 
61 #define ENTRYGRAIN  0.002   //2ms
62 extern void registerSE();
63 extern int se_statesize;
64
65
66 #ifdef BRANCHBOUND
67 #ifndef ADAPTIVE
68 #define SE_Register(state, f1, f2, f3, f4, f5)  \
69     void registerSE() {    \
70       SE_register(f1, f2, f3, f4, f5);   \
71     }  \
72      void createMultipleChildren(SearchGroup* myGroup, StateBase *parent, SequentialSolver* solver, bool parallel) {  \
73        StateBase *state;  \
74        double minCost = myGroup->getCost(); \
75        double lb = f5(parent); \
76         if(lb<minCost)  \
77             f2(parent, solver, false);  \
78         while((state=solver->dequeue()) != NULL) \
79         {  \
80             minCost = myGroup->getCost(); \
81             lb = f5(parent); \
82             if(lb>=minCost)  \
83             continue;  \
84             f2(state, solver, parallel); \
85         }  \
86     }
87 #else
88 #define SE_Register(state, f1, f2, f3, f4, f5)  \
89     void registerSE() {    \
90       SE_register(f1, f2, f3, f4, f5);   \
91     }  \
92     void createMultipleChildren(SearchGroup* myGroup, StateBase *parent, SequentialSolver* solver, bool parallel) {  \
93        StateBase *state;  \
94        double avgentrytime = 0;  \
95        int processed_nodes = 1; \
96        double instrument_start; \
97        double accumulate_time = 0; \
98        double minCost = myGroup->getCost(); \
99        double lb = f5(parent); \
100        instrument_start = CkWallTimer();  \
101         if(lb<minCost)  \
102             f2(parent, solver, false);  \
103         accumulate_time = avgentrytime  = CkWallTimer() - instrument_start;  \
104         while((state=solver->dequeue()) != NULL) \
105         {  \
106             minCost = myGroup->getCost(); \
107             lb = f5(parent); \
108             if(lb>=minCost)  \
109             continue;  \
110             if(processed_nodes  == 20)  \
111             {  \
112                 avgentrytime  = (CkWallTimer() - instrument_start)/20;  \
113             }  \
114             f2(state, solver, parallel); \
115             accumulate_time += avgentrytime; \
116             if(accumulate_time > ENTRYGRAIN)  \
117             {  solver-> dequeue_multiple(avgentrytime, processed_nodes);} \
118             processed_nodes++; \
119         }  \
120     }
121 #endif
122
123 #else
124 #define registerSE_DEF(state, f1, f2, f3, f4)    \
125     void registerSE() {    \
126       SE_register(f1, f2, f3, f4);   \
127     }
128 #ifndef ADAPTIVE
129 #define SE_Register(state, f1, f2, f3, f4)  \
130     registerSE_DEF(state, f1, f2, f3, f4)  \
131     void createMultipleChildren(StateBase *parent, SequentialSolver* solver, bool parallel) {  \
132        f2(parent, solver, false);  \
133        StateBase *state;  \
134        while((state=solver->dequeue()) != NULL) \
135        {  \
136             f2(state, solver, parallel); \
137        }  \
138     } 
139 #else
140 #define SE_Register(state, f1, f2, f3, f4)  \
141     registerSE_DEF(state, f1, f2, f3, f4)  \
142     void createMultipleChildren(StateBase *parent, SequentialSolver* solver, bool parallel) {  \
143        StateBase *state;  \
144        double avgentrytime = 0;  \
145        int processed_nodes = 1; \
146        double instrument_start; \
147        double accumulate_time = 0; \
148        f2(parent, solver, false);  \
149        instrument_start = CkWallTimer();\
150        accumulate_time = avgentrytime  = CkWallTimer() - instrument_start;  \
151        while((state=solver->dequeue()) != NULL) \
152        {  \
153             if(processed_nodes  == 20) \
154             {  \
155                 avgentrytime  = (CkWallTimer() - instrument_start)/20;  \
156             }  \
157             f2(state, solver, parallel); \
158             accumulate_time += avgentrytime; \
159             if(accumulate_time > ENTRYGRAIN)  \
160             {  solver-> dequeue_multiple(avgentrytime, processed_nodes);} \
161             processed_nodes++; \
162        } \
163     } 
164 #endif
165
166 #endif
167
168 #endif