29 auto start = std::chrono::high_resolution_clock::now();
32 auto& acceptedSnapshots = lastAcceptedSnapshots[entityId];
34 if (acceptedSnapshots.empty())
36 acceptedSnapshots.push_back(e);
38 auto end = std::chrono::high_resolution_clock::now();
40 stats.additional_time += (end - start);
45 bool foundSimilarSnapshot =
false;
46 for (
const auto& oldSnapshot : acceptedSnapshots)
48 const float similarity = calculateSnapshotSimilarity(oldSnapshot, e);
50 if (similarity >= threshold)
52 foundSimilarSnapshot =
true;
57 if (foundSimilarSnapshot)
63 acceptedSnapshots.push_back(e);
64 if (acceptedSnapshots.size() > numSnapshots)
66 acceptedSnapshots.pop_front();
71 auto end = std::chrono::high_resolution_clock::now();
73 stats.additional_time += (end - start);
77 return !foundSimilarSnapshot;
81 SnapshotSimilarityFilter::calculateSnapshotSimilarity(
85 std::vector<aron::data::VariantPtr> oldInstances;
86 std::vector<aron::data::VariantPtr> newInstances;
91 oldInstances.push_back(i.
data());
98 newInstances.push_back(i.
data());
102 if (oldInstances.size() != newInstances.size())
107 float weightedTotal = 0.0f;
108 float totalImportance = 0.0f;
109 for (
size_t i = 0; i < newInstances.size(); i++)
112 calculateInstanceSimilarity(oldInstances[i], newInstances[i], 1.0f);
113 weightedTotal += result.similarity * result.importance;
114 totalImportance += result.importance;
117 return totalImportance > 0.0f ? weightedTotal / totalImportance : 1.0f;
120 SnapshotSimilarityFilter::SimilarityResult
121 SnapshotSimilarityFilter::calculateInstanceSimilarity(
124 float parentImportance)
126 if (!oldData && !newData)
128 return {1.0f, parentImportance};
130 if (!oldData || !newData)
132 return {0.0f, parentImportance};
135 const float importance = newData->getImportance().has_value() ?
136 parentImportance * newData->getImportance().value() : parentImportance;
139 if (importance <= 0.0f)
144 auto oldDesc = oldData->getDescriptor();
145 auto newDesc = newData->getDescriptor();
146 if (oldDesc != newDesc)
148 return {0.0f, importance};
215 const auto& oldElems = oldList->getElements();
216 const auto& newElems = newList->getElements();
218 if (oldElems.size() != newElems.size())
220 return {0.0f, importance};
223 float weightedTotal = 0.0f;
224 float totalImportance = 0.0f;
225 for (
size_t i = 0; i < newElems.size(); i++)
228 calculateInstanceSimilarity(oldElems[i], newElems[i], importance);
229 weightedTotal += result.similarity * result.importance;
230 totalImportance += result.importance;
232 const float similarity = totalImportance > 0.0f ? weightedTotal / totalImportance : 1.0f;
233 return {similarity, importance};
239 auto oldKeys = oldDict->getAllKeys();
240 auto newKeys = newDict->getAllKeys();
242 if (oldKeys.size() != newKeys.size())
244 return {0.0f, importance};
247 float weightedTotal = 0.0f;
248 float totalImportance = 0.0f;
249 for (
const auto& key : newKeys)
251 if (!oldDict->hasElement(key))
253 return {0.0f, importance};
257 calculateInstanceSimilarity(oldDict->at(key), newDict->at(key), importance);
258 weightedTotal += result.similarity * result.importance;
259 totalImportance += result.importance;
261 const float similarity = totalImportance > 0.0f ? weightedTotal / totalImportance : 1.0f;
262 return {similarity, importance};
267 return {0.0f, importance};
281 stats.additional_info +=
"Similarity threshold: ";
282 stats.additional_info += std::to_string(threshold);
290 numSnapshots = std::max<std::size_t>(1, numSnapshots);
291 stats.number_of_compared_objects =
static_cast<int>(numSnapshots);
292 stats.start_time = std::chrono::high_resolution_clock::now();
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
MemoryID getEntityID() const
const DataT & data() const
bool forEachInstance(InstanceFunctionT &&func)
std::mutex filterMutex_
Mutex for thread-safe access to filter state (stats and derived class state) Derived classes should l...
void configure(const nlohmann::json &json) override
static const constexpr char * PARAM_THRESHOLD
virtual bool accept(const armem::wm::EntitySnapshot &e, bool simulatedVersion) override
static const constexpr char * PARAM_NUM_SNAPSHOTS
std::string getName() override
FilterStatistics getFilterStatistics() override
static const constexpr char * NAME
Client-side working entity instance.
Client-side working memory entity snapshot.
static PointerType DynamicCastAndCheck(const VariantPtr &n)
#define ARMARX_INFO
The normal logging level.
std::shared_ptr< Variant > VariantPtr
float calculateSimilarity(const aron::data::NDArray &oldValue, const aron::data::NDArray &newValue)
float calculateSimilarity(const aron::data::Bool &oldValue, const aron::data::Bool &newValue)