Evaluator.cpp
Go to the documentation of this file.
1 
18 #include "Evaluator.h"
19 #include <iostream>
20 
22 {
23  return (std::get<0>(*first) < std::get<0>(*second));
24 }
25 
27 {
29  NBVPath = "";
30 }
31 
32 Evaluator::Evaluator(std::list<evalulatorTupleSharedPtr> objectListEntry, std::list<std::string> sceneListEntry,
33  double GainValueThresholdEntry, std::string NBVPath, std::string dbPath):
34  GainValueThreshold(GainValueThresholdEntry),NBVPath(NBVPath),DbPath(dbPath)
35  {
36  for (const std::string &it : sceneListEntry)
37  {
38  SceneMap[it].push_back("root-obj");
39  }
40 
41  std::list<evalulatorTupleSharedPtr> bufferObjectList;
42 
43  //Fusion all evaluator tuples into object tuple, so that all object are only represented once.
44  while(objectListEntry.size()>0)
45  {
46  double fValue = 0;
47  std::string objectMetaName =std::get<2>(*(*objectListEntry.begin()));
48  std::string objectName =std::get<3>(*(*objectListEntry.begin()));
49  std::string objectObservedId =std::get<4>(*(*objectListEntry.begin()));
50  fValue += std::get<0>(*(*objectListEntry.begin()));
51  //Add the object in the scene map
52  SceneMap[std::get<1>(*(*objectListEntry.begin()))].push_back(std::get<2>(*(*objectListEntry.begin())));
53  objectListEntry.pop_front();
54  bufferObjectList = objectListEntry;
55  for (const evalulatorTupleSharedPtr &tuple : objectListEntry)
56  {
57  //Search for the same object in other scene
58  if(std::get<2>(*tuple) == objectMetaName)
59  {
60  SceneMap[std::get<1>(*tuple)].push_back(std::get<2>(*tuple));
61  fValue += std::get<0>(*tuple);
62  bufferObjectList.remove(tuple);
63  }
64  }
65  //Add the tuple in the tuple list
66  ProcessedObjectList.push_back(objectTupleSharedPtr(new objectTuple(fValue,objectMetaName,objectName,objectObservedId)));
67  objectListEntry = bufferObjectList;
68  }
70 
73 }
74 
76 {
77 }
78 
79 
80 double Evaluator::CalcAverageF(std::list<objectTupleSharedPtr> list)
81 {
82  double ret = 0;
83  for (const objectTupleSharedPtr &it : list)
84  {
85  ret += std::get<0>(*it);
86  }
87  return ret/list.size();
88 }
89 
90 
91 bool Evaluator::Validation(std::list<objectTupleSharedPtr> listToValidate)
92 {
93  bool test = false;
94  //Validity of criteria : at least one object per scene
95  for (std::unordered_map<std::string,std::list<std::string>>::iterator it=SceneMap.begin(); it!=SceneMap.end(); ++it)
96  {
97  for (const objectTupleSharedPtr &it2 : listToValidate)
98  {
99  for (const std::string &it3 : it->second)
100  {
101  if(it3 == std::get<1>(*it2))
102  {
103  test = true;
104  break;
105  }
106  }
107  if(test) break;
108  }
109  if(!test) return false;
110  }
111  return true;
112 }
113 
114 bool Evaluator::Evaluate(std::list<objectTupleSharedPtr> oldList, std::list<objectTupleSharedPtr> newList)
115 {
116  if ((CalcAverageF(newList) - CalcAverageF(oldList))/CalcAverageF(newList) < GainValueThreshold)
117  {
118  return true;
119  }
120  else
121  {
122  return false;
123  }
124 
125 }
126 
127 std::list<objectTupleSharedPtr> Evaluator::getIntermediateObjects()
128 {
129  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects BEGIN");
130  objectTupleSharedPtr bufferTuple;
132  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects Weight value are now sorted");
133  std::list<objectTupleSharedPtr> bufferTupleList;
134  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects begin from filtering iteration process");
135  //remove object ad long as the gain threshold is valid and the validation true is.
136  for (int i=0; i<ObjectListSize; i++)
137  {
138  bufferTupleList = ProcessedObjectList;
139  bufferTuple = *(ProcessedObjectList.begin());
140  ProcessedObjectList.pop_front();
141 
142  if (Validation(ProcessedObjectList) == false)
143  {
144  ProcessedObjectList.push_back(bufferTuple);
145  continue;
146  }
147 
148  if(Evaluate(bufferTupleList, ProcessedObjectList) == false)
149  {
150  break;
151  }
152  }
153  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects end from filtering iteration process");
154  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects gotten list from intermediate object");
155  for (const objectTupleSharedPtr &tuple : ProcessedObjectList)
156  {
157  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects Object " <<std::get<2>(*tuple)
158  << " with observed Id " << std::get<3>(*tuple)
159  << " and value " << std::get<0>(*tuple));
160  }
161  ROS_DEBUG_STREAM("Evaluator::getIntermediateObjects END");
162  return ProcessedObjectList;
163 }
164 
166 {
167  std::string output = "<InterObj>";
168  for (const objectTupleSharedPtr &tuple : ProcessedObjectList)
169  {
170  ROS_DEBUG_STREAM("Evaluator::PublishIntermediateObjectForWorldModel Object " << std::get<2>(*tuple) << " with observed Id " << std::get<3>(*tuple)
171  << " got intermediate object weight " << std::get<0>(*tuple));
172  output += "<obj name=\""+std::get<2>(*tuple)+"\" observedId=\""+std::get<3>(*tuple)+"\">" + std::to_string(std::get<0>(*tuple)) + "</obj>";
173  }
174  output += "</InterObj>";
175 
176  std::ofstream file(NBVPath, std::ios::out | std::ios::trunc);
177  if(file)
178  {
179  file << output;
180  file.close();
181  }
182 }
183 
184 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
185  //Callback for sqlite requests.
186  int i;
187  for(i=0; i<argc; i++){
188  ROS_DEBUG("Evaluator::PublishIntermediateObjectInDomainTable %s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
189  }
190  return 0;
191 }
192 
194 {
195  std::stringstream sqlReq;
196  char *zErrMsg = 0;
197  sqlReq << "CREATE TABLE model_weight(id INT PRIMARY KEY NOT NULL,type VARCHAR(100),observedId "
198  << "VARCHAR(100), weight VARCHAR(100));";
199  const std::string& sqlReqString = sqlReq.str();
200  const char* SQLREQ = sqlReqString.c_str();
201  sqlite3 *database;
202  sqlite3_open(DbPath.c_str(), &database);
203  int sqliteReturn = sqlite3_exec(database, SQLREQ, callback, 0, &zErrMsg);
204  if( sqliteReturn != SQLITE_OK )
205  {
206  ROS_WARN_STREAM("Evaluator::PublishIntermediateObjectInDomainTable SQL error : " << zErrMsg);
207  sqlite3_free(zErrMsg);
208  }
209  else
210  {
211  ROS_DEBUG_STREAM("Evaluator::PublishIntermediateObjectInDomainTable Model weight table created.");
212  }
213 
214  std::stringstream clearReq;
215  clearReq << "DELETE FROM model_weight";
216 
217  const std::string& clearReqString = clearReq.str();
218  const char* SQLCLEAR = clearReqString.c_str();
219  sqliteReturn = sqlite3_exec(database, SQLCLEAR, callback, 0, &zErrMsg);
220  if( sqliteReturn != SQLITE_OK )
221  {
222  ROS_WARN_STREAM("Evaluator::PublishIntermediateObjectInDomainTable SQL error : " << zErrMsg);
223  sqlite3_free(zErrMsg);
224  }
225  else
226  {
227  ROS_DEBUG_STREAM("Evaluator::PublishIntermediateObjectInDomainTable table cleared.");
228  }
229 
230  std::stringstream insertReq;
231  unsigned int counter = 0;
232  for (const objectTupleSharedPtr &tuple : ProcessedObjectList)
233  {
234  insertReq << "INSERT INTO model_weight (id,type,observedId,weight)"
235  << "VALUES (" << counter <<", '"<< std::get<2>(*tuple) <<"', '"<< std::get<3>(*tuple) <<"', '"
236  << std::get<0>(*tuple) <<"');";
237  counter++;
238  }
239  const std::string& insertReqString = insertReq.str();
240  const char* SQLINSERT = insertReqString.c_str();
241  sqliteReturn = sqlite3_exec(database, SQLINSERT, callback, 0, &zErrMsg);
242  if( sqliteReturn != SQLITE_OK )
243  {
244  ROS_WARN_STREAM("Evaluator::PublishIntermediateObjectInDomainTable SQL error : " << zErrMsg);
245  sqlite3_free(zErrMsg);
246  }
247  else
248  {
249  ROS_DEBUG_STREAM("Evaluator::PublishIntermediateObjectInDomainTable Weights inserted.");
250  }
251  sqlite3_close(database);
252 }
bool Validation(std::list< objectTupleSharedPtr > listToValidate)
Definition: Evaluator.cpp:91
std::tuple< double, std::string, std::string, std::string > objectTuple
Definition: Evaluator.h:34
std::shared_ptr< objectTuple > objectTupleSharedPtr
Definition: Evaluator.h:35
std::list< objectTupleSharedPtr > ProcessedObjectList
Definition: Evaluator.h:46
std::string NBVPath
Definition: Evaluator.h:48
std::string DbPath
Definition: Evaluator.h:49
std::list< objectTupleSharedPtr > getIntermediateObjects()
Definition: Evaluator.cpp:127
void PublishIntermediateObjectForWorldModel()
Definition: Evaluator.cpp:165
std::unordered_map< std::string, std::list< std::string > > SceneMap
Definition: Evaluator.h:45
double GainValueThreshold
Definition: Evaluator.h:47
int ObjectListSize
Definition: Evaluator.h:44
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
double CalcAverageF(std::list< objectTupleSharedPtr > list)
Definition: Evaluator.cpp:80
virtual ~Evaluator()
Definition: Evaluator.cpp:75
void PublishIntermediateObjectInDomainTable()
Definition: Evaluator.cpp:193
bool compare_evaluator_tuple(const objectTupleSharedPtr &first, const objectTupleSharedPtr &second)
Definition: Evaluator.cpp:21
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
Definition: Evaluator.cpp:184
bool Evaluate(std::list< objectTupleSharedPtr > oldList, std::list< objectTupleSharedPtr > newList)
Definition: Evaluator.cpp:114
#define ROS_DEBUG(...)
std::shared_ptr< evalulatorTuple > evalulatorTupleSharedPtr
Definition: Evaluator.h:32


asr_intermediate_object_generator
Author(s): Borella Jocelyn, Meißner Pascal
autogenerated on Thu Nov 21 2019 03:53:41