Fleet  0.0.9
Inference in the LOT
ConstantContainer.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <vector>
5 
6 #include "Miscellaneous.h"
7 #include "Rule.h"
8 
9 using constant_t = float;
10 
11 
13 
14 
24 struct Constant {
25 
26  float value;
27  Constant() : value(0) {}
28  Constant(constant_t v) : value(v) {}
29 
30  void operator=(const constant_t v) {
31  value = v;
32  }
33  constant_t get_value() const { return value; }
34  operator constant_t() const { return value; }
35 };
36 
49 public:
50  std::vector<Constant> constants;
51  size_t constant_idx; // in evaluation, this variable stores what constant we are in
52 
53  virtual size_t count_constants() const = 0; // must implement
54  virtual std::pair<double,double> constant_proposal(Constant) const = 0; // must implmenet
55  virtual void randomize_constants() = 0;
56 
57 // ConstantContainer& operator=(const ConstantContainer& c) {
58 // constants = c.constants;
59 // constant_idx = c.constant_idx;
60 // return *this;
61 // }
62  virtual void reset_constant_index() {
63  constant_idx = 0;
64  }
65 
66  virtual Constant next_constant() {
67 
68  // now we might have too many since the size of our constants is constrained
69  if(constant_idx >= constants.size()) {
71  }
72  else {
73  return constants.at(constant_idx++);
74  }
75  }
76 
77  virtual bool operator==(const ConstantContainer& h) const {
78  auto C = count_constants();
79 
80  if(h.count_constants() != C)
81  return false;
82 
83  for(size_t i=0;i<C;i++) {
84  if(constants.at(i).get_value() != h.constants.at(i).get_value()) {
85  return false;
86  }
87  }
88  return true;
89  }
90 
91  virtual size_t hash() const {
92  // hash includes constants so they are only ever equal if constants are equal
93  auto C = count_constants();
94  size_t hsh = 1;
95  for(size_t i=0;i<C;i++) {
96  hash_combine(hsh, i, constants.at(i).get_value());
97  }
98  return hsh;
99  }
100 
101 };
102 
Constant(constant_t v)
Definition: ConstantContainer.h:28
Definition: VMSRuntimeError.h:13
virtual bool operator==(const ConstantContainer &h) const
Definition: ConstantContainer.h:77
virtual size_t hash() const
Definition: ConstantContainer.h:91
virtual size_t count_constants() const =0
Definition: ConstantContainer.h:48
virtual void reset_constant_index()
Definition: ConstantContainer.h:62
virtual Constant next_constant()
Definition: ConstantContainer.h:66
Definition: ConstantContainer.h:12
size_t constant_idx
Definition: ConstantContainer.h:51
Constant()
Definition: ConstantContainer.h:27
std::vector< Constant > constants
Definition: ConstantContainer.h:50
A Rule stores one possible expansion in the grammar, specifying a nonterminal type, an instruction that gets executed, a forma string, a number of children, and an array of types of each child. Here we "emulate" a type system using t_nonterminal to store an integer for the types. *.
constant_t get_value() const
Definition: ConstantContainer.h:33
float constant_t
Definition: ConstantContainer.h:9
Definition: ConstantContainer.h:24
void operator=(const constant_t v)
Definition: ConstantContainer.h:30
float value
Definition: ConstantContainer.h:26