GecodeAnalyzer.cpp
Go to the documentation of this file.
1 //
2 // Created by yijiangh on 1/17/18.
3 //
4 
6 
7 #include <gecode/gist.hh>
8 
9 namespace {
10 bool writeMiniZincData(
11  const char* path,
12  const int n, const int m,
13  const std::vector<int>& A, const std::vector<int>& G, const std::vector<int>& T)
14 {
15  FILE *fp = fopen(path, "wb+");
16 
17  assert(n*n == A.size());
18  assert(n == G.size());
19  assert(n*n*m == T.size());
20 
21  if(!fp)
22  {
23  ROS_ERROR("[Gecode-ts planning] fails to output minizinc data.");
24  return false;
25  }
26 
27  fprintf(fp, "n = %d;\n\n", n);
28  fprintf(fp, "m = %d;\n\n", m);
29 
30  fprintf(fp, "G_data = [");
31  for(int i = 0; i < G.size(); i++)
32  {
33  fprintf(fp, "%d,", G[i]);
34  if(i == G.size()-1)
35  {
36  fprintf(fp, "];\n\n");
37  }
38  }
39 
40  fprintf(fp, "A_data = [");
41  for(int i = 0; i < A.size(); i++)
42  {
43  fprintf(fp, "%d,", A[i]);
44  if(i == A.size()-1)
45  {
46  fprintf(fp, "];\n\n");
47  }
48  }
49 
50  fprintf(fp, "T_data = [");
51  for(int i = 0; i < T.size(); i++)
52  {
53  fprintf(fp, "%d,", T[i]);
54  if(i == T.size()-1)
55  {
56  fprintf(fp, "];\n\n");
57  }
58  }
59 
60  if(0 == fclose(fp))
61  {
62  return true;
63  }
64  else
65  {
66  return false;
67  }
68 }
69 
70 
71 } // anon util namespace
72 
74 {
75 }
76 
78 {
79  using namespace Gecode;
80 
82 
83  Init();
84 
85  /* split layers */
86  /* label stores layer index of each dual node */
87  int layer_size = ptr_frame_->SizeOfLayer();
88 
89  layers_.clear();
90  layers_.resize(layer_size);
91  for (int i = 0; i < Nd_; i++)
92  {
94  layers_[e->Layer()].push_back(e);
95  }
96 
97  for (int l = 0; l < layer_size; l++)
98  {
99  fprintf(stderr, "Size of layer %d is %d\n", l, (int)layers_[l].size());
100  }
101 
102  /* print starting from the first layer */
103  bool bSuccess = true;
104  for (int l = 0; l < layer_size; l++)
105  {
106  if(0 == l)
107  {
108  /* set pillars as starting edges */
109  PrintPillars();
110  continue;
111  }
112 
113  int Nl = layers_[l].size();
114 
115  /* max_z_ and min_z_ in current layer */
116  min_z_ = 1e20;
117  max_z_ = -min_z_;
118 
119  for (int i = 0; i < Nl; i++)
120  {
121  WF_edge* e = layers_[l][i];
122  point u = e->pvert_->Position();
123  point v = e->ppair_->pvert_->Position();
124  min_z_ = min(min_z_, (double)min(u.z(), v.z()));
125  max_z_ = max(max_z_, (double)max(u.z(), v.z()));
126  }
127 
128  // compute input for gecode solver
129  int n = layers_[l].size();
130  int m = ptr_collision_->Divide();
131  std::vector<int> A, G, T;
132 
133  ComputeGecodeInput(layers_[l], A, G, T, CSPDataMatrixStorageType::ROW_MAJOR);
134 
135  // output minizinc data
136  std::string str_path = "/home/yijiangh/Documents/";
137  str_path = str_path + "as_minizinc_data_layer_" + std::to_string(l) + ".dzn";
138 
139  bSuccess = writeMiniZincData(str_path.c_str(), n, m, A, G, T);
140 
141  // trigger minizinc-gecode computation
142 
143  // fetch sequence back
144 
145  // for edge in sequence:
146  //{
147  // add edge in print_queue_
148  // update structure
149  // update state map
150  //}
151  }
152 
154  return bSuccess;
155 }
156 
157 void GecodeAnalyzer::ComputeGecodeInput(const std::vector<WF_edge*>& layer_e,
158  std::vector<int>& A, std::vector<int>& G, std::vector<int>& T,
160 {
161  int n = layer_e.size();
162  int m = ptr_collision_->Divide();
163 
164  A = std::vector<int>(n*n, 0);
165  G = std::vector<int>(n, 0);
166  T = std::vector<int>(n*n*m, 0);
167 
168  for(int i=0; i < n; i++)
169  {
170  WF_edge* e = layer_e[i];
171 
172  // connectivity
173  // if one of the nodes exist, grounded
174  // st node index
175  int uj = ptr_frame_->GetEndu(e->ID());
176  bool exist_uj = ptr_dualgraph_->isExistingVert(uj);
177 
178  // end node index
179  int vj = ptr_frame_->GetEndv(e->ID());
180  bool exist_vj = ptr_dualgraph_->isExistingVert(vj);
181 
182  if(exist_uj || exist_vj || e->isPillar())
183  {
184  G[i] = 1;
185  }
186 
187  // for current layer's edge, if share node = 1
188  std::vector<int> connect_vert_id(2);
189  connect_vert_id[0] = ptr_frame_->GetEndu(e->ID());
190  connect_vert_id[1] = ptr_frame_->GetEndv(e->ID());
191 
192  for(const auto id : connect_vert_id)
193  {
195 
196  while (eu != NULL)
197  {
198  if (eu->ID() == e->ID() || eu->ID() == e->ppair_->ID())
199  {
200  eu = eu->pnext_;
201  continue;
202  }
203 
204  for(int j=0; j < n; j++)
205  {
206  if(eu->ID() == layer_e[j]->ID() || eu->ID() == layer_e[j]->ppair_->ID())
207  {
208  int index_2d;
209 
210  // https://en.wikipedia.org/wiki/Row-_and_column-major_order
211  if(m_type == CSPDataMatrixStorageType::ROW_MAJOR)
212  {
213  // n1: i(n), n2: j(n);
214  index_2d = j + n * i;
215  }
216  else
217  {
218  index_2d = i + n * j;
219  }
220 
221  A[index_2d] = 1;
222  }
223  }
224 
225  eu = eu->pnext_;
226  }
227  }
228 
229  int dual_i = ptr_wholegraph_->e_dual_id(e->ID());
230 
231  for (int j = 0; j < n; j++)
232  {
233  WF_edge* ej = layer_e[j];
234  int dual_j = ptr_wholegraph_->e_dual_id(ej->ID());
235 
236  if (dual_i != dual_j)
237  {
238  std::vector<lld> tmp(3);
239  ptr_collision_->DetectCollision(e, ej, tmp);
240 
241  for (int o = 0; o < 3; o++)
242  {
243  tmp[o] |= angle_state_[dual_i][o];
244  }
245 
246  // TODO: should use tmp as a mask and impose it on angle_map_ to include previous layers' influence
247  std::vector<int> tmp_vector = ptr_collision_->ConvertCollisionMapToIntMap(tmp);
248 
249  for(int k=0; k<m; k++)
250  {
251  int index_3d;
252 
253  // https://en.wikipedia.org/wiki/Row-_and_column-major_order
254  if(m_type == CSPDataMatrixStorageType::ROW_MAJOR)
255  {
256  // n1: i(n), n2: j(n), n3: k(m)
257  index_3d = k + m * (j + n * i);
258  }
259  else
260  {
261  index_3d = i + n * (j + n * k);
262  }
263 
264  T[index_3d] = tmp_vector[k];
265  }
266  }
267  }
268 
269  } // end loop for i
270 }
271 
273 {
274  printf("***GecodeAnalyzer timer result:\n");
275  gecode_analyzer_.Print("GecodeAnalyzer:");
276 }
277 
279 {
280  using namespace Gecode;
281 }
GLdouble n
std::vector< int > ConvertCollisionMapToIntMap(const std::vector< lld > &colli_map)
const GLfloat * m
WF_edge * GetNeighborEdge(int u)
Definition: WireFrame.h:239
GLsizei const GLchar *const * path
std::vector< std::vector< WF_edge * > > layers_
void PrintPillars()
WF_edge * pnext_
Definition: WireFrame.h:165
WF_edge * GetEdge(int i)
Definition: WireFrame.h:238
bool isExistingVert(int u)
Definition: DualGraph.h:163
bool isPillar() const
Definition: WireFrame.h:125
WireFrame * ptr_frame_
Definition: SeqAnalyzer.h:132
GLsizeiptr size
vector< vector< unsigned long long > > angle_state_
Definition: SeqAnalyzer.h:151
void Stop()
Definition: Timer.cpp:21
int e_orig_id(int u)
Definition: DualGraph.h:152
int ID() const
Definition: WireFrame.h:123
WF_edge * ppair_
Definition: WireFrame.h:166
int Layer() const
Definition: WireFrame.h:124
bool DetectCollision(WF_edge *target_e, DualGraph *ptr_subgraph, std::vector< lld > &result_map)
void ComputeGecodeInput(const std::vector< WF_edge * > &layer_e, std::vector< int > &A, std::vector< int > &G, std::vector< int > &T, CSPDataMatrixStorageType m_type)
DualGraph * ptr_wholegraph_
Definition: SeqAnalyzer.h:146
DualGraph * ptr_dualgraph_
Definition: SeqAnalyzer.h:133
QuadricCollision * ptr_collision_
Definition: SeqAnalyzer.h:135
int SizeOfLayer() const
Definition: WireFrame.h:233
void Start()
Definition: Timer.cpp:15
int e_dual_id(int u)
Definition: DualGraph.h:153
int GetEndv(int i) const
Definition: WireFrame.h:245
const GLdouble * v
WF_vert * pvert_
Definition: WireFrame.h:164
void Print(char *item)
Definition: Timer.cpp:37
int GetEndu(int i) const
Definition: WireFrame.h:244
reference z()
Definition: Vec.h:212
#define ROS_ERROR(...)
point Position() const
Definition: WireFrame.h:78


choreo_task_sequence_planner
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:03:14