Evoplex  0.2.1
attributerange.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ATTRIBUTE_RANGE_H
18 #define ATTRIBUTE_RANGE_H
19 
20 #include <functional>
21 #include <memory>
22 #include <vector>
23 #include <QHash>
24 
25 #include "prg.h"
26 #include "value.h"
27 
28 namespace evoplex {
29 
30 class AttributeRange;
31 using AttributeRangePtr = std::shared_ptr<AttributeRange>;
32 using AttributesScope = QHash<QString, AttributeRangePtr>;
33 
39 {
40 public:
42  virtual ~AttributeRangeInterface() = default;
43 
48  virtual Value rand(PRG* prg) const = 0;
49 
56  virtual Value next(const Value& v) const = 0;
57 
64  virtual Value prev(const Value& v) const = 0;
65 };
66 
72 {
73 public:
77  enum Type {
79  // IntervalOfValues
82  Bool,
83  // SetOfValues
87  // SingleValue
92  };
93 
120  static AttributeRangePtr parse(int attrId, const QString& attrName,
121  const QString& attrRangeStr);
122 
124  ~AttributeRange() override = default;
125 
131  Value validate(const QString& valueStr) const;
132 
137  inline bool isValid() const;
141  inline int id() const;
145  inline const QString& attrName() const;
150  inline const QString& attrRangeStr() const;
154  inline Type type() const;
158  inline const Value& min() const;
162  inline const Value& max() const;
163 
164 protected:
165  const int m_id;
166  const QString m_attrName;
167  const Type m_type;
168  QString m_attrRangeStr;
169  Value m_min;
170  Value m_max;
171 
179  explicit AttributeRange(int id, const QString& attrName, Type type);
180 
181 private:
182  // assume that attrRangeStr is equal to 'int{ }' or 'double{ }'
183  static AttributeRangePtr setOfValues(QString attrRangeStr, const int id,
184  const QString& attrName);
185 
186  // assume that attrRangeStr is equal to 'int[min,max]' or 'double[min,max]'
187  static AttributeRangePtr intervalOfValues(QString attrRangeStr, const int id,
188  const QString& attrName);
189 };
190 
199 {
200 public:
207  explicit SingleValue(int id, const QString& attrName, Type type);
208  explicit SingleValue();
209 
211  ~SingleValue() override = default;
212 
213  inline Value rand(PRG*) const override;
214  inline Value next(const Value& v) const override;
215  inline Value prev(const Value& v) const override;
216 };
217 
226 {
227 public:
237  IntervalOfValues(int id, const QString& attrName, Type type,
238  const Value &min, const Value &max);
239 
241  ~IntervalOfValues() override = default;
242 
243  inline Value rand(PRG* prg) const override;
244  inline Value next(const Value& v) const override;
245  inline Value prev(const Value& v) const override;
246 
247 private:
248  std::function<Value(PRG*)> f_rand;
249  std::function<Value(const Value&)> f_next;
250  std::function<Value(const Value&)> f_prev;
251 };
252 
261 {
262 public:
271  SetOfValues(int id, const QString& attrName, Type type, Values values);
272 
274  ~SetOfValues() override = default;
275 
279  inline const Values& values() const;
280 
281  inline Value rand(PRG* prg) const override;
282  Value next(const Value& v) const override;
283  Value prev(const Value& v) const override;
284 
285 private:
286  Values m_values;
287 };
288 
289 /***********************/
290 
291 inline bool AttributeRange::isValid() const
292 { return m_type != Invalid; }
293 
294 inline int AttributeRange::id() const
295 { return m_id; }
296 
297 inline const QString& AttributeRange::attrName() const
298 { return m_attrName; }
299 
300 inline const QString& AttributeRange::attrRangeStr() const
301 { return m_attrRangeStr; }
302 
304 { return m_type; }
305 
306 inline const Value& AttributeRange::min() const
307 { return m_min; }
308 
309 inline const Value& AttributeRange::max() const
310 { return m_max; }
311 
312 /***********************/
313 
314 inline Value SingleValue::rand(PRG* prg) const
315 { return prg->bernoulli() ? m_max : m_min; }
316 
317 inline Value SingleValue::next(const Value& val) const
318 { return val; }
319 
320 inline Value SingleValue::prev(const Value& val) const
321 { return val; }
322 
323 /***********************/
324 
325 inline Value IntervalOfValues::rand(PRG* prg) const
326 { return f_rand(prg); }
327 
328 inline Value IntervalOfValues::next(const Value& val) const
329 { return f_next(val); }
330 
331 inline Value IntervalOfValues::prev(const Value& val) const
332 { return f_prev(val); }
333 
334 /***********************/
335 
336 inline Value SetOfValues::rand(PRG* prg) const {
337  if (m_values.empty()) return Value();
338  return m_values.at(prg->uniform(m_values.size()-1));
339 }
340 
341 inline const Values& SetOfValues::values() const
342 { return m_values; }
343 
344 } // evoplex
345 #endif // ATTRIBUTE_RANGE_H
Value prev(const Value &v) const override
Gets the value before v.
Definition: attributerange.h:331
A boolean.
Definition: attributerange.h:82
The IntervalOfValues class.
Definition: attributerange.h:225
A class for variant data types (tagged union).
Definition: value.h:56
T uniform(T min, T max)
Generates a random double/float [min, max).
Definition: prg.h:63
~SingleValue() override=default
Destructor.
A set of strings.
Definition: attributerange.h:86
A file path.
Definition: attributerange.h:91
A set of intergers.
Definition: attributerange.h:85
A range of real numbers.
Definition: attributerange.h:80
Type type() const
Gets the current attribute&#39;s range type.
Definition: attributerange.h:303
bool bernoulli(double p)
Bernoulli distribution.
Definition: prg.h:50
const Value & max() const
Gets the maximum value in the range.
Definition: attributerange.h:309
Value next(const Value &v) const override
Gets the value after v.
Definition: attributerange.h:317
Value rand(PRG *) const override
Gets a random value in the attribute range.
Definition: attributerange.h:314
virtual Value next(const Value &v) const =0
Gets the value after v.
~SetOfValues() override=default
Destructor.
A directory path.
Definition: attributerange.h:90
int id() const
Gets the attribute id.
Definition: attributerange.h:294
Value next(const Value &v) const override
Gets the value after v.
A common interface for the Attribute Range classes.
Definition: attributerange.h:38
const QString & attrRangeStr() const
Gets the original attribute&#39;s range string.
Definition: attributerange.h:300
virtual Value prev(const Value &v) const =0
Gets the value before v.
Value rand(PRG *prg) const override
Gets a random value in the attribute range.
Definition: attributerange.h:336
const Value & min() const
Gets the minimum value in the range.
Definition: attributerange.h:306
A string.
Definition: attributerange.h:88
The SingleValue class.
Definition: attributerange.h:198
virtual Value rand(PRG *prg) const =0
Gets a random value in the attribute range.
Value prev(const Value &v) const override
Gets the value before v.
Definition: attributerange.h:320
Value rand(PRG *prg) const override
Gets a random value in the attribute range.
Definition: attributerange.h:325
Invalid type.
Definition: attributerange.h:78
virtual ~AttributeRangeInterface()=default
Destructor.
The AttributeRange base class.
Definition: attributerange.h:71
bool isValid() const
Checks if this AttributeRange is valid.
Definition: attributerange.h:291
static AttributeRangePtr parse(int attrId, const QString &attrName, const QString &attrRangeStr)
Creates an AttributeRange object from a valid attrRangeStr string.
Value next(const Value &v) const override
Gets the value after v.
Definition: attributerange.h:328
const QString & attrName() const
Gets the attribute name.
Definition: attributerange.h:297
~AttributeRange() override=default
Destructor.
SetOfValues(int id, const QString &attrName, Type type, Values values)
Constructor.
The SetOfValues class.
Definition: attributerange.h:260
Type
An enum for the attribute range types.
Definition: attributerange.h:77
IntervalOfValues(int id, const QString &attrName, Type type, const Value &min, const Value &max)
Constructor.
A range of integers.
Definition: attributerange.h:81
Pseudo-random number generator.
Definition: prg.h:29
A set of real numbers.
Definition: attributerange.h:84
Value validate(const QString &valueStr) const
Checks if the valueStr belongs to this attribute range.
Value prev(const Value &v) const override
Gets the value before v.
~IntervalOfValues() override=default
Destructor.
const Values & values() const
Gets the vector of values.
Definition: attributerange.h:341
Definition: abstractgraph.h:29
AttributeRange(int id, const QString &attrName, Type type)
AttributeRange constructor.
A non-empty string.
Definition: attributerange.h:89