24 #include <MemoryX/interface/core/EntityBase.h>
25 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
57 longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(
"LongtermMemory");
58 priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
60 std::string segmentName = getProperty<std::string>(
"LTMSegment").getValue();
63 if (!ltmInstancesSegmentPrx)
65 ARMARX_FATAL <<
"LTM segment not found or has an invalid type: " << segmentName;
68 const float agingFactor = getProperty<float>(
"AgingFactor").getValue();
69 const float pruningThreshold = getProperty<float>(
"PruningThreshold").getValue();
71 const float distanceThreshold = getProperty<float>(
"MergingThreshold").getValue();
72 const std::string distanceName = getProperty<std::string>(
"MergingDistanceType").getValue();
75 if (distanceName ==
"Mahalanobis")
79 else if (distanceName ==
"KL")
83 else if (distanceName ==
"ISD")
89 ARMARX_WARNING <<
"Unknown MergingDistanceType: " << distanceName <<
". Will use Kullback-Leibler instead.";
96 const std::string reducerName = getProperty<std::string>(
"GMMReducerAlgorithm").getValue();
97 ARMARX_INFO <<
"Using GMM reduction algorithm: " << reducerName;
99 if (reducerName ==
"West")
103 else if (reducerName ==
"Runnalls")
107 else if (reducerName ==
"Williams")
113 ARMARX_WARNING <<
"Unknown GMMReducerAlgorithm: " << reducerName <<
". Will use WestReducer instead.";
124 if (!ltmInstancesSegmentPrx || ltmInstancesSegmentPrx->getSegmentName() != segmentName)
126 ltmInstancesSegmentPrx = longtermMemoryPrx->getCustomInstancesSegment(segmentName,
true);
132 fusionMethod->setAgingFactor(factor);
137 assMethod->setThreshold(threshold);
141 const ::Ice::Current&
c)
146 posMean->x =
mean[0];
147 posMean->y =
mean[1];
148 posMean->z =
mean[2];
149 posMean->frame =
"world";
151 instance->setPosition(posMean);
152 instance->setPositionUncertainty(posDist);
153 instance->setClass(objectName, 1.);
159 const std::string clsName = newObject->getMostProbableClass();
160 ObjectInstanceList ltmObjects = ltmInstancesSegmentPrx->getObjectInstancesByClass(clsName);
162 if (ltmObjects.size() > 0)
164 for (ObjectInstanceList::const_iterator it = ltmObjects.begin(); it != ltmObjects.end(); ++it)
167 EntityBasePtr fusedEntity = fusionMethod->fuseEntity(*it, newObject);
168 ltmInstancesSegmentPrx->updateEntity((*it)->getId(), fusedEntity);
169 ARMARX_INFO <<
"Updated existing object: " << newObject->getName();
175 EntityBasePtr fusedEntity = fusionMethod->initEntity(newObject);
176 ltmInstancesSegmentPrx->addEntity(fusedEntity);
177 ARMARX_INFO <<
"Added new object: " << newObject->getName();
183 WorkingMemorySnapshotInterfacePrx snapshot = longtermMemoryPrx->openWorkingMemorySnapshot(snapshotName);
187 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
191 const std::string segmentName =
"objectInstances";
192 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
196 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
200 EntityIdList ids = segInstances->getAllEntityIds();
203 struct EntityIdComparator
205 static bool compare(
const std::string& i,
const std::string& j)
207 return (std::stoi(i) < std::stoi(j));
212 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
214 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
219 ARMARX_INFO <<
"Processing complete! Learned objects: " << ids.size();
224 GaussianMixtureDistributionBasePtr result = GaussianMixtureDistributionBasePtr();
228 clsList.push_back(className);
229 getChildClasses(className, clsList);
232 ObjectInstanceList ltmObjects = ltmInstancesSegmentPrx->getObjectInstancesByClassList(clsList);
233 ObjectInstanceList::const_iterator it = ltmObjects.begin();
235 if (it != ltmObjects.end())
240 for (++it; it != ltmObjects.end(); ++it)
242 inst = ObjectInstancePtr::dynamicCast(*it);
253 GaussianMixtureDistributionBasePtr fullGMM =
getPositionFull(objectName);
257 return gmmReducer->reduceByComponentCount(fullGMM, compCount);
261 return GaussianMixtureDistributionBasePtr();
266 DeviationType devType, const ::Ice::Current&)
268 GaussianMixtureDistributionBasePtr fullGMM =
getPositionFull(objectName);
272 return gmmReducer->reduceByMaxDeviation(fullGMM, maxDeviation, convertDeviationType(devType));
276 return GaussianMixtureDistributionBasePtr();
286 return result->getModalComponent().gaussian;
290 return NormalDistributionBasePtr();
297 return gmmToClusterList(reducedGMM);
301 DeviationType devType, const ::Ice::Current&
c)
304 return gmmToClusterList(reducedGMM);
309 DeviationMeasure CommonPlacesLearner::convertDeviationType(DeviationType devType)
316 case eDevOrientedBBox:
319 case eDevEqualSphere:
327 Cluster3DList CommonPlacesLearner::gmmToClusterList(
const GaussianMixtureDistributionBasePtr& gmm)
329 Cluster3DList result;
333 for (
int i = 0; i < gmm->size(); ++i)
336 FloatVector compMean = gmm->getComponent(i).gaussian->getMean();
337 cluster.center.x = compMean[0];
338 cluster.center.y = compMean[1];
339 cluster.center.z = compMean[2];
340 cluster.weight = gmm->getComponent(i).weight;
341 result.push_back(cluster);
346 struct Cluster3DComparator
348 static bool compare(
const Cluster3D& i,
const Cluster3D& j)
350 return (i.weight > j.weight);
358 void CommonPlacesLearner::getChildClasses(std::string className, NameList& result)
360 ObjectClassList children = priorKnowledgePrx->getObjectClassesSegment()->getChildClasses(className);
361 ARMARX_VERBOSE_S <<
"Found " << children.size() <<
" subclasses for " << className << std::endl;
363 for (ObjectClassList::const_iterator it = children.begin(); it != children.end(); ++it)
365 result.push_back((*it)->getName());