ImportanceFilter.cpp
Go to the documentation of this file.
1#include "ImportanceFilter.h"
2
4{
5
6 bool
8 {
9 // Thread safety: lock the filter mutex for all mutable state access
10 std::lock_guard<std::mutex> lock(filterMutex_);
11
12 auto start = std::chrono::high_resolution_clock::now();
13 bool instances_accepted = false;
14
16 [this, &instances_accepted](armem::wm::EntityInstance& i)
17 {
18 if (!instances_accepted)
19 { //if no instance was accepted yet
20 instances_accepted = important(i); //see if another instance triggers acceptance
21 }
22 });
23
24 //set stats:
25 auto end = std::chrono::high_resolution_clock::now();
26 stats.end_time = end;
27 stats.additional_time += (end - start);
28 if (instances_accepted)
29 {
30 stats.accepted += 1;
31 }
32 else
33 {
34 stats.rejected += 1;
35 }
36
37 return instances_accepted;
38 }
39
40 void
41 SnapshotImportanceFilter::configure(const nlohmann::json& json)
42 {
43 std::lock_guard<std::mutex> lock(filterMutex_);
44 if (json.find(PARAM_THRESHOLD) != json.end())
45 {
46 this->threshold = json.at(PARAM_THRESHOLD);
47 ARMARX_INFO << VAROUT(this->threshold);
48 stats.additional_info += "Threshold for importance: ";
49 stats.additional_info += std::to_string(this->threshold);
50 }
51 if (json.find(PARAM_TYPE) != json.end())
52 {
53 auto t = json.at(PARAM_TYPE);
54 if (t == "Confidence")
55 {
56 this->type = ImportanceType::CONFIDENCE;
57 }
58 if (t == "Accesses")
59 {
60 this->type = ImportanceType::ACCESSES;
61 }
62 stats.importance_type = t;
63 ARMARX_INFO << VAROUT(stats.importance_type);
64 }
65
66 stats.start_time = std::chrono::high_resolution_clock::now();
67 stats.number_of_compared_objects = 1;
68 stats.similarity_type =
69 aron::similarity::NDArraySimilarity::Type::NONE; //information for statistics export
70 }
71
74 {
75 std::lock_guard<std::mutex> lock(filterMutex_);
76 return this->stats;
77 }
78
79 std::string
81 {
82 return this->NAME;
83 }
84
85 bool
86 SnapshotImportanceFilter::important(armem::wm::EntityInstance& i)
87 {
88 if (this->type == ImportanceType::CONFIDENCE)
89 {
90 auto c = i.metadata().confidence;
91 if (c > this->threshold)
92 {
93 //for now the whole entity snapshot is accepted if at least one instance has a confidence higher than the threshold
94 return true;
95 }
96 }
97 else if (this->type == ImportanceType::ACCESSES)
98 {
99 auto a = i.metadata().numAccessed;
100 if (a > this->threshold)
101 {
102 return true;
103 }
104 }
105 return false;
106 }
107
108
109} // namespace armarx::armem::server::ltm::processor::filter
#define VAROUT(x)
constexpr T c
bool forEachInstance(InstanceFunctionT &&func)
std::mutex filterMutex_
Mutex for thread-safe access to filter state (stats and derived class state) Derived classes should l...
Definition Filter.h:61
virtual bool accept(const armem::wm::EntitySnapshot &e, bool simulatedVersion) override
Client-side working entity instance.
Client-side working memory entity snapshot.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
float confidence
An optional confidence, may be used for things like decay.
unsigned long numAccessed
A counter how often the instance has been accessed.