Vita
lambda_f.h
Go to the documentation of this file.
1
13#if !defined(VITA_LAMBDA_F_H)
14#define VITA_LAMBDA_F_H
15
16#include <type_traits>
17
18#include "kernel/exceptions.h"
22#include "kernel/gp/team.h"
23#include "utility/discretization.h"
24
25#include "kernel/gp/detail/lambda_f.h"
26
27namespace vita
28{
29
49{
50public:
51 virtual ~basic_lambda_f() = default;
52
53 virtual value_t operator()(const dataframe::example &) const = 0;
54
55 virtual bool is_valid() const = 0;
56};
57
58// Forward declarations.
59namespace serialize
60{
61
62namespace lambda
63{
64
65template<class T = i_mep> std::unique_ptr<basic_src_lambda_f> load(
66 std::istream &, const symbol_set &);
67
68} // namespace lambda
69
70bool save(std::ostream &, const basic_src_lambda_f *);
71bool save(std::ostream &, const basic_src_lambda_f &);
72bool save(std::ostream &, const std::unique_ptr<basic_src_lambda_f> &);
73
74} // namespace serialize
75
80{
81 class_t label;
82 double sureness;
83};
84
94{
95public:
96 virtual double measure(const model_metric &, const dataframe &) const = 0;
97 virtual std::string name(const value_t &) const = 0;
98 virtual classification_result tag(const dataframe::example &) const = 0;
99
100private:
101 // *** Serialization ***
102 virtual std::string serialize_id() const = 0;
103 virtual bool save(std::ostream &) const = 0;
104
105 template<class T> friend std::unique_ptr<basic_src_lambda_f>
106 serialize::lambda::load(std::istream &, const symbol_set &);
107 friend bool serialize::save(std::ostream &, const basic_src_lambda_f *);
108};
109
110// ***********************************************************************
111// * Symbolic regression *
112// ***********************************************************************
113
117
128template<class T, bool S>
130 protected detail::reg_lambda_f_storage<T, S>
131{
132public:
133 explicit basic_reg_lambda_f(const T &);
134 basic_reg_lambda_f(std::istream &, const symbol_set &);
135
136 value_t operator()(const dataframe::example &) const final;
137
138 std::string name(const value_t &) const final;
139
140 double measure(const model_metric &, const dataframe &) const final;
141
142 bool is_valid() const final;
143
144 // *** Serialization ***
145 static const std::string SERIALIZE_ID;
146 bool save(std::ostream &) const final;
147
148private:
149 // Not useful for regression tasks and moved to private section.
150 classification_result tag(const dataframe::example &) const final;
151
152 std::string serialize_id() const final { return SERIALIZE_ID; }
153
154 value_t eval(const dataframe::example &, std::false_type) const;
155 value_t eval(const dataframe::example &, std::true_type) const;
156};
157
158// ***********************************************************************
159// * Classification *
160// ***********************************************************************
161
172{
173 mv, // majority voting
174 wta, // winner takes all
175
176 standard = wta
177};
178
182
194template<bool N>
196 protected detail::class_names<N>
197{
198public:
199 explicit basic_class_lambda_f(const dataframe &);
200
201 value_t operator()(const dataframe::example &) const final;
202
203 std::string name(const value_t &) const final;
204
205 double measure(const model_metric &, const dataframe &) const final;
206
207protected:
208 basic_class_lambda_f() = default;
209};
210
223template<class T, bool S, bool N>
225{
226public:
227 basic_dyn_slot_lambda_f(const T &, dataframe &, unsigned);
228 basic_dyn_slot_lambda_f(std::istream &, const symbol_set &);
229
230 classification_result tag(const dataframe::example &) const final;
231
232 bool is_valid() const final;
233
234 double training_accuracy() const;
235
236 // *** Serialization ***
237 static const std::string SERIALIZE_ID;
238 bool save(std::ostream &) const final;
239
240private:
241 // *** Private support methods ***
242 void fill_matrix(dataframe &, unsigned);
243 std::size_t slot(const dataframe::example &) const;
244
245 std::string serialize_id() const final { return SERIALIZE_ID; }
246
247 // *** Private data members ***
250
254 matrix<unsigned> slot_matrix_;
255
257 std::vector<class_t> slot_class_;
258
260 std::size_t dataset_size_;
261};
262
275template<class T, bool S, bool N>
277{
278public:
280 basic_gaussian_lambda_f(std::istream &, const symbol_set &);
281
282 classification_result tag(const dataframe::example &) const final;
283
284 bool is_valid() const final;
285
286 // *** Serialization ***
287 static const std::string SERIALIZE_ID;
288 bool save(std::ostream &) const final;
289
290private:
291 // *** Private support methods ***
292 void fill_vector(dataframe &);
293 bool load_(std::istream &, const symbol_set &, std::true_type);
294 bool load_(std::istream &, const symbol_set &, std::false_type);
295
296 std::string serialize_id() const final { return SERIALIZE_ID; }
297
298 // *** Private data members ***
300
301 // gauss_dist[i] = "the gaussian distribution of the i-th class if the
302 // classification problem".
303 std::vector<distribution<number>> gauss_dist_;
304};
305
316template<class T, bool S, bool N>
318{
319public:
320 basic_binary_lambda_f(const T &, dataframe &);
321 basic_binary_lambda_f(std::istream &, const symbol_set &);
322
323 classification_result tag(const dataframe::example &) const final;
324
325 bool is_valid() const final;
326
327 // *** Serialization ***
328 static const std::string SERIALIZE_ID;
329 bool save(std::ostream &) const final;
330
331private:
332 std::string serialize_id() const final { return SERIALIZE_ID; }
333
335};
336
337// ***********************************************************************
338// * Extensions to support teams *
339// ***********************************************************************
340
350template<class T, bool S, bool N, template<class, bool, bool> class L,
351 team_composition C = team_composition::standard>
353{
354public:
355 template<class... Args> team_class_lambda_f(const team<T> &, dataframe &,
356 Args &&...);
357 team_class_lambda_f(std::istream &, const symbol_set &);
358
359 classification_result tag(const dataframe::example &) const final;
360
361 bool is_valid() const final;
362
363 static const std::string SERIALIZE_ID;
364
365private:
366 bool save(std::ostream &) const final;
367 std::string serialize_id() const final;
368
369 // The components of the team never store the names of the classes. If we
370 // need the names, the master class will memorize them.
371 std::vector<L<T, S, false>> team_;
372
373 class_t classes_;
374};
375
383template<class T, bool S, bool N>
385 : public team_class_lambda_f<T, S, N, basic_dyn_slot_lambda_f>
386{
387public:
388 using basic_dyn_slot_lambda_f::team_class_lambda_f::team_class_lambda_f;
389};
390
398template<class T, bool S, bool N>
400 : public team_class_lambda_f<T, S, N, basic_gaussian_lambda_f>
401{
402public:
403 using basic_gaussian_lambda_f::team_class_lambda_f::team_class_lambda_f;
404};
405
413template<class T, bool S, bool N>
415 : public team_class_lambda_f<T, S, N, basic_binary_lambda_f>
416{
417public:
418 using basic_binary_lambda_f::team_class_lambda_f::team_class_lambda_f;
419};
420
421// ***********************************************************************
422// * Template aliases to simplify the syntax and help the user *
423// ***********************************************************************
424template<class T>
426
427template<class T>
429
430template<class T>
432
433template<class T>
435
436#include "kernel/gp/src/lambda_f.tcc"
437} // namespace vita
438
439#endif // include guard
Lambda class for Binary Classification.
Definition: lambda_f.h:318
The basic interface of a classification lambda class.
Definition: lambda_f.h:197
Lambda class for Slotted Dynamic Class Boundary Determination.
Definition: lambda_f.h:225
Lambda class for the Gaussian Distribution Classification.
Definition: lambda_f.h:277
The basic interface of a lambda function.
Definition: lambda_f.h:49
Lambda function specialized for regression tasks.
Definition: lambda_f.h:131
Extends basic_lambda_f interface adding some useful methods for symbolic regression / classification ...
Definition: lambda_f.h:94
The model_metric class choose the appropriate method considering this type.
Definition: lambda_f.h:181
The model_metric class choose the appropriate method considering this type.
Definition: lambda_f.h:116
A 2-dimensional labeled data structure with columns of potentially different types.
Definition: dataframe.h:48
There are a lot of metrics related to a model (a lambda_f) and we don't want fat classes.
Definition: model_metric.h:32
A container for the symbols used by the GP engine.
Definition: symbol_set.h:37
An helper class for extending classification schemes to teams.
Definition: lambda_f.h:353
A collection of cooperating individuals used as a member of vita::population.
Definition: team.h:54
The main namespace for the project.
std::size_t class_t
The type used as class ID in classification tasks.
Definition: dataframe.h:31
team_composition
For classification problems there are two major possibilities to combine the outputs of multiple pred...
Definition: lambda_f.h:172
std::variant< D_VOID, D_INT, D_DOUBLE, D_STRING > value_t
A variant containing the data types used by the interpreter for internal calculations / output value ...
Definition: value.h:45
Contains a class ID / confidence level pair.
Definition: lambda_f.h:80
double sureness
class ID
Definition: lambda_f.h:82
Stores a single element (row) of the dataset.
Definition: dataframe.h:194