LearnTA  0.0.1
timed_condition_set.hh
1 
6 #pragma once
7 
8 #include <vector>
9 #include <utility>
10 
11 #include "timed_condition.hh"
12 #include "elementary_language.hh"
13 
14 namespace learnta {
19  private:
20  std::vector<TimedCondition> conditions;
21 
22  explicit TimedConditionSet(std::vector<TimedCondition> conditions) : conditions(std::move(conditions)) {}
23 
24  public:
25  TimedConditionSet() : conditions(std::vector<TimedCondition>{}) {}
26 
27  explicit TimedConditionSet(TimedCondition condition) : conditions({std::move(condition)}) {
28  }
29 
30  static TimedConditionSet bottom() {
31  return TimedConditionSet{std::vector<TimedCondition>{}};
32  }
33 
40  static TimedConditionSet reduce(std::list<ElementaryLanguage> elementaryLanguages) {
41  if (elementaryLanguages.empty()) {
42  return TimedConditionSet::bottom();
43  }
44  // Assert the equivalence of all the words
45  assert(std::all_of(elementaryLanguages.begin(), elementaryLanguages.end(), [&](const auto &elem) {
46  return elem.getWord() == elementaryLanguages.front().getWord();
47  }));
48  // Assert the simplicity
49  assert(std::all_of(elementaryLanguages.begin(), elementaryLanguages.end(), [&](const auto &elem) {
50  return elem.getTimedCondition().isSimple();
51  }));
52  std::list<std::pair<TimedCondition, int>> timedConditionsWithSize;
53  timedConditionsWithSize.resize(elementaryLanguages.size());
54  std::transform(std::make_move_iterator(elementaryLanguages.begin()),
55  std::make_move_iterator(elementaryLanguages.end()),
56  timedConditionsWithSize.begin(),
57  [&](auto &&elementary) {
58  return std::make_pair(elementary.getTimedCondition(), 1);
59  });
60  auto it = timedConditionsWithSize.begin();
61  while (it != timedConditionsWithSize.end()) {
62  auto timedCondition = it->first;
63  bool merged = false;
64  for (auto it2 = std::next(it); it2 != timedConditionsWithSize.end(); it2++) {
65  // Check if the convex hull is the exact union
66  auto convexHull = timedCondition.convexHull(it2->first);
67  if (convexHull.enumerate().size() == static_cast<std::size_t>(it->second + it2->second)) {
68  it->first = std::move(convexHull);
69  it->second += it2->second;
70  timedConditionsWithSize.erase(it2);
71  it = timedConditionsWithSize.begin();
72  merged = true;
73  break;
74  }
75  }
76  if (!merged) {
77  it++;
78  }
79  }
80  std::vector<TimedCondition> result;
81  result.resize(timedConditionsWithSize.size());
82  std::transform(std::make_move_iterator(timedConditionsWithSize.begin()),
83  std::make_move_iterator(timedConditionsWithSize.end()),
84  result.begin(),
85  [](auto &&timedConditionWithSize) {
86  return timedConditionWithSize.first;
87  });
88 
89  return TimedConditionSet{result};
90  }
91 
92  [[nodiscard]] bool empty() const {
93  return this->conditions.empty();
94  }
95 
96  [[nodiscard]] std::size_t size() const {
97  return this->conditions.size();
98  }
99 
100  [[nodiscard]] TimedCondition& front() {
101  return this->conditions.front();
102  }
103 
104  [[nodiscard]] TimedCondition& at(std::size_t i) {
105  return this->conditions.at(i);
106  }
107 
108  [[nodiscard]] const std::vector<TimedCondition> &getConditions() const {
109  return conditions;
110  }
111 
117  [[nodiscard]] std::vector<std::size_t> getStrictlyConstrainedVariables(const TimedCondition &originalCondition,
118  const size_t examinedVariableSize) const {
119  std::vector<std::size_t> result;
120  for (const auto &condition: this->conditions) {
121  std::vector<std::size_t> tmp =
122  condition.getStrictlyConstrainedVariables(originalCondition, examinedVariableSize);
123  result.reserve(result.size() + tmp.size());
124  std::move(tmp.begin(), tmp.end(), std::back_inserter(result));
125  }
126  return result;
127  }
128 
133  for (auto &condition: this->conditions) {
134  condition.removeEqualityUpperBoundAssign();
135  }
136  }
137 
138  void push_back(const TimedCondition &condition) {
139  this->conditions.push_back(condition);
140  }
141 
142  TimedCondition& back() {
143  return this->conditions.back();
144  }
145 
146  decltype(conditions.begin()) begin() {
147  return this->conditions.begin();
148  }
149 
150  decltype(conditions.end()) end() {
151  return this->conditions.end();
152  }
153  };
154 }
A set of timed conditions to represent non-convex constraints.
Definition: timed_condition_set.hh:18
void removeEqualityUpperBoundAssign()
Remove the equality upper bound.
Definition: timed_condition_set.hh:132
static TimedConditionSet reduce(std::list< ElementaryLanguage > elementaryLanguages)
Construct a timed condition set from a set of simple elementary languages.
Definition: timed_condition_set.hh:40
std::vector< std::size_t > getStrictlyConstrainedVariables(const TimedCondition &originalCondition, const size_t examinedVariableSize) const
Returns the set of variables strictly constrained compared with the original condition.
Definition: timed_condition_set.hh:117
A timed condition, a finite conjunction of inequalities of the form , where and .
Definition: timed_condition.hh:19
std::vector< std::size_t > getStrictlyConstrainedVariables(const TimedCondition &originalCondition, const size_t examinedVariableSize) const
Returns the set of variables strictly constrained compared with the original condition.
Definition: timed_condition.hh:580
void removeEqualityUpperBoundAssign()
Remove the equality upper bound.
Definition: timed_condition.hh:411
Definition: experiment_runner.hh:23