24#include <MemoryX/interface/core/EntityBase.h>
25#include <MemoryX/interface/memorytypes/MemoryEntities.h>
50 return "CommonPlacesLearner";
72 if (!ltmInstancesSegmentPrx)
74 ARMARX_FATAL <<
"LTM segment not found or has an invalid type: " << segmentName;
84 if (distanceName ==
"Mahalanobis")
88 else if (distanceName ==
"KL")
92 else if (distanceName ==
"ISD")
99 <<
". Will use Kullback-Leibler instead.";
107 ARMARX_INFO <<
"Using GMM reduction algorithm: " << reducerName;
109 if (reducerName ==
"West")
113 else if (reducerName ==
"Runnalls")
117 else if (reducerName ==
"Williams")
124 <<
". Will use WestReducer instead.";
136 const ::Ice::Current&
c)
138 if (!ltmInstancesSegmentPrx || ltmInstancesSegmentPrx->getSegmentName() != segmentName)
140 ltmInstancesSegmentPrx =
141 longtermMemoryPrx->getCustomInstancesSegment(segmentName,
true);
148 fusionMethod->setAgingFactor(factor);
154 assMethod->setThreshold(threshold);
159 const MultivariateNormalDistributionBasePtr& posDist,
160 const ::Ice::Current&
c)
163 FloatVector mean = posDist->getMean();
165 posMean->x = mean[0];
166 posMean->y = mean[1];
167 posMean->z = mean[2];
168 posMean->frame =
"world";
170 instance->setPosition(posMean);
171 instance->setPositionUncertainty(posDist);
172 instance->setClass(objectName, 1.);
178 const ::Ice::Current&
c)
180 const std::string clsName = newObject->getMostProbableClass();
181 ObjectInstanceList ltmObjects = ltmInstancesSegmentPrx->getObjectInstancesByClass(clsName);
183 if (ltmObjects.size() > 0)
185 for (ObjectInstanceList::const_iterator it = ltmObjects.begin(); it != ltmObjects.end();
189 EntityBasePtr fusedEntity = fusionMethod->fuseEntity(*it, newObject);
190 ltmInstancesSegmentPrx->updateEntity((*it)->getId(), fusedEntity);
191 ARMARX_INFO <<
"Updated existing object: " << newObject->getName();
197 EntityBasePtr fusedEntity = fusionMethod->initEntity(newObject);
198 ltmInstancesSegmentPrx->addEntity(fusedEntity);
199 ARMARX_INFO <<
"Added new object: " << newObject->getName();
205 const ::Ice::Current&
c)
207 WorkingMemorySnapshotInterfacePrx snapshot =
208 longtermMemoryPrx->openWorkingMemorySnapshot(snapshotName);
212 ARMARX_ERROR <<
"Snapshot not found in LTM: " << snapshotName;
216 const std::string segmentName =
"objectInstances";
217 PersistentEntitySegmentBasePrx segInstances = snapshot->getSegment(segmentName);
221 ARMARX_ERROR <<
"Segment not found in snapshot: " << segmentName;
225 EntityIdList ids = segInstances->getAllEntityIds();
228 struct EntityIdComparator
231 compare(
const std::string& i,
const std::string& j)
233 return (std::stoi(i) < std::stoi(j));
237 std::sort(ids.begin(), ids.end(), EntityIdComparator::compare);
239 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
241 EntityBasePtr snapEntity = segInstances->getEntityById(*it);
246 ARMARX_INFO <<
"Processing complete! Learned objects: " << ids.size();
249 GaussianMixtureDistributionBasePtr
252 GaussianMixtureDistributionBasePtr result = GaussianMixtureDistributionBasePtr();
256 clsList.push_back(className);
257 getChildClasses(className, clsList);
260 ObjectInstanceList ltmObjects =
261 ltmInstancesSegmentPrx->getObjectInstancesByClassList(clsList);
262 ObjectInstanceList::const_iterator it = ltmObjects.begin();
264 if (it != ltmObjects.end())
268 inst->getPositionAttribute()->getUncertainty());
270 for (++it; it != ltmObjects.end(); ++it)
272 inst = ObjectInstancePtr::dynamicCast(*it);
275 inst->getPositionAttribute()->getUncertainty()));
282 GaussianMixtureDistributionBasePtr
285 const ::Ice::Current&)
287 GaussianMixtureDistributionBasePtr fullGMM =
getPositionFull(objectName);
291 return gmmReducer->reduceByComponentCount(fullGMM, compCount);
295 return GaussianMixtureDistributionBasePtr();
299 GaussianMixtureDistributionBasePtr
301 Ice::Float maxDeviation,
302 DeviationType devType,
303 const ::Ice::Current&)
305 GaussianMixtureDistributionBasePtr fullGMM =
getPositionFull(objectName);
309 return gmmReducer->reduceByMaxDeviation(
310 fullGMM, maxDeviation, convertDeviationType(devType));
314 return GaussianMixtureDistributionBasePtr();
318 NormalDistributionBasePtr
320 const ::Ice::Current&)
322 GaussianMixtureDistributionBasePtr result =
327 return result->getModalComponent().gaussian;
331 return NormalDistributionBasePtr();
338 const ::Ice::Current&
c)
340 GaussianMixtureDistributionBasePtr reducedGMM =
342 return gmmToClusterList(reducedGMM);
347 Ice::Float maxDeviation,
348 DeviationType devType,
349 const ::Ice::Current&
c)
351 GaussianMixtureDistributionBasePtr reducedGMM =
353 return gmmToClusterList(reducedGMM);
359 CommonPlacesLearner::convertDeviationType(DeviationType devType)
366 case eDevOrientedBBox:
369 case eDevEqualSphere:
378 CommonPlacesLearner::gmmToClusterList(
const GaussianMixtureDistributionBasePtr& gmm)
380 Cluster3DList result;
384 for (
int i = 0; i < gmm->size(); ++i)
387 FloatVector compMean = gmm->getComponent(i).gaussian->getMean();
388 cluster.center.x = compMean[0];
389 cluster.center.y = compMean[1];
390 cluster.center.z = compMean[2];
391 cluster.weight = gmm->getComponent(i).weight;
392 result.push_back(cluster);
397 struct Cluster3DComparator
400 compare(
const Cluster3D& i,
const Cluster3D& j)
402 return (i.weight > j.weight);
406 std::sort(result.begin(), result.end(), Cluster3DComparator::compare);
412 CommonPlacesLearner::getChildClasses(std::string className, NameList& result)
414 ObjectClassList children =
415 priorKnowledgePrx->getObjectClassesSegment()->getChildClasses(className);
416 ARMARX_VERBOSE_S <<
"Found " << children.size() <<
" subclasses for " << className
419 for (ObjectClassList::const_iterator it = children.begin(); it != children.end(); ++it)
421 result.push_back((*it)->getName());
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Property< PropertyType > getProperty(const std::string &name)
The FramedPosition class.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
IceManagerPtr getIceManager() const
Returns the IceManager.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
GaussianMixtureDistributionBasePtr getPositionFull(const ::std::string &objectName, const ::Ice::Current &=Ice::emptyCurrent) override
void onInitComponent() override
Pure virtual hook for the subclass.
Cluster3DList getPositionClustersByMaxDeviation(const ::std::string &objectName, Ice::Float maxDeviation, DeviationType devType, const ::Ice::Current &=Ice::emptyCurrent) override
void setLTMSegmentName(const ::std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
void setMergingThreshold(float threshold, const ::Ice::Current &=Ice::emptyCurrent) override
void learnFromObjectMCA(const ::std::string &objectName, const MultivariateNormalDistributionBasePtr &posDist, const ::Ice::Current &=Ice::emptyCurrent) override
GaussianMixtureDistributionBasePtr getPositionReducedByComponentCount(const ::std::string &objectName, Ice::Int compCount, const ::Ice::Current &=Ice::emptyCurrent) override
void onConnectComponent() override
Pure virtual hook for the subclass.
static std::string GetDefaultName()
void learnFromObject(const ObjectInstanceBasePtr &newObject, const ::Ice::Current &=Ice::emptyCurrent) override
GaussianMixtureDistributionBasePtr getPositionReducedByMaxDeviation(const ::std::string &objectName, Ice::Float maxDeviation, DeviationType devType, const ::Ice::Current &=Ice::emptyCurrent) override
void setAgingFactor(float factor, const ::Ice::Current &=Ice::emptyCurrent) override
void onExitComponent() override
Hook for subclass.
NormalDistributionBasePtr getPositionAsGaussian(const ::std::string &objectName, const ::Ice::Current &=Ice::emptyCurrent) override
void learnFromSnapshot(const ::std::string &snapshotName, const ::Ice::Current &=Ice::emptyCurrent) override
Cluster3DList getPositionClustersByComponentCount(const ::std::string &objectName, Ice::Int compCount, const ::Ice::Current &=Ice::emptyCurrent) override
static GaussianMixtureDistributionPtr FromProbabilityMeasure(const ProbabilityMeasureBasePtr &probMeasure)
Convert or approximate given ProbabilityMeasure to a gaussian mixture.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_FATAL
The logging level for unexpected behaviour, that will lead to a seriously malfunctioning program and ...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
int compare(const T &lhs, const T &rhs)
::std::vector<::Ice::Float > FloatVector
IceInternal::Handle< FramedPosition > FramedPositionPtr
std::shared_ptr< GMMDistance > GMMDistancePtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
double distance(const Point &a, const Point &b)