Ultra 0.9.4
Loading...
Searching...
No Matches
model_measurements.tcc
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_MODEL_MEASUREMENTS_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(ULTRA_MODEL_MEASUREMENTS_TCC)
18#define ULTRA_MODEL_MEASUREMENTS_TCC
19
20template<Fitness F>
21model_measurements<F>::model_measurements(const F &f, double a)
22 : fitness(f), accuracy(a)
23{
24 Expects(in_0_1(a));
25}
26
30template<Fitness F>
31bool model_measurements<F>::empty() const noexcept
32{
33 return !fitness.has_value() && !accuracy.has_value();
34}
35
44template<Fitness F>
45auto operator<=>(const model_measurements<F> &lhs,
46 const model_measurements<F> &rhs) noexcept
47{
48 if (lhs.fitness <= rhs.fitness && lhs.accuracy <= rhs.accuracy
49 && (lhs.fitness < rhs.fitness || lhs.accuracy < rhs.accuracy))
50 return std::partial_ordering::less;
51 if (lhs.fitness >= rhs.fitness && lhs.accuracy >= rhs.accuracy
52 && (lhs.fitness > rhs.fitness || lhs.accuracy > rhs.accuracy))
53 return std::partial_ordering::greater;
54 if (lhs == rhs)
55 return std::partial_ordering::equivalent;
56
57 return std::partial_ordering::unordered; // {12, 50} <=> {10, 60}
58}
59
69template<Fitness F>
70bool model_measurements<F>::load(std::istream &is)
71{
72 model_measurements tmp;
73
74 unsigned has_fitness;
75 if (!(is >> has_fitness))
76 return false;
77 if (has_fitness)
78 {
79 if (F tmp_fitness; !ultra::load(is, &tmp_fitness))
80 return false;
81 else
82 tmp.fitness = tmp_fitness;
83 }
84
85 unsigned has_accuracy;
86 if (!(is >> has_accuracy))
87 return false;
88 if (has_accuracy)
89 {
90 if (double tmp_accuracy; !load_float_from_stream(is, &tmp_accuracy))
91 return false;
92 else
93 tmp.accuracy = tmp_accuracy;
94 }
95
96 *this = tmp;
97
98 return true;
99}
100
107template<Fitness F>
108bool model_measurements<F>::save(std::ostream &os) const
109{
110 if (fitness.has_value())
111 {
112 os << "1 ";
113 if (!ultra::save(os, *fitness))
114 return false;
115 }
116 else
117 os << "0";
118
119 os << '\n';
120
121 if (accuracy.has_value())
122 {
123 os << "1 ";
124 if (!save_float_to_stream(os, *accuracy))
125 return false;
126 }
127 else
128 os << "0";
129
130 os << '\n';
131
132 return os.good();
133}
134
135#endif // include guard
double accuracy(const P &p, const dataframe &d)
Definition metrics.h:47
auto operator<=>(const model_measurements< F > &lhs, const model_measurements< F > &rhs) noexcept
Definition model_measurements.tcc:45
bool save(std::ostream &out, F f)
Definition fitness.h:151