Skip to content

Commit dbc7995

Browse files
committed
added namespace
1 parent 191baa2 commit dbc7995

File tree

11 files changed

+1317
-1448
lines changed

11 files changed

+1317
-1448
lines changed

src/MLearning.cpp

Lines changed: 490 additions & 575 deletions
Large diffs are not rendered by default.

src/MLearning.h

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,80 +31,81 @@
3131
#include "propts.h"
3232
#include "structs.h"
3333

34+
namespace prlearn {
35+
36+
class MLearning {
37+
public:
38+
MLearning();
39+
MLearning(const MLearning& other);
40+
MLearning& operator=(const MLearning& other) = default;
41+
42+
MLearning(MLearning&&) = default;
43+
MLearning& operator=(MLearning&&) = default;
44+
45+
void addSample(size_t dimen, // dimensions
46+
const double* f_var, const double* t_var, // doubles
47+
size_t label, // edge chosen, edge taken
48+
size_t dest, double value, // cost
49+
const std::vector<MLearning>& clouds, // other points
50+
bool minimization, const double delta,
51+
const propts_t& options
52+
);
53+
54+
void update(const std::vector<MLearning>& clouds, bool minimization);
55+
56+
std::pair<double, double> lookup(size_t label, const double* f_var, size_t dimen) const;
57+
58+
void print(std::ostream& s, size_t tabs, std::map<size_t, size_t>& edge_map, const std::vector<MLearning>& clouds) const;
3459

35-
class MLearning {
36-
public:
37-
MLearning();
38-
MLearning(const MLearning& other);
39-
MLearning& operator = (const MLearning& other) = default;
40-
41-
MLearning(MLearning&&) = default;
42-
MLearning& operator = (MLearning&&) = default;
43-
44-
void addSample( size_t dimen, // dimensions
45-
const double* f_var, const double* t_var, // doubles
46-
size_t label,// edge chosen, edge taken
47-
size_t dest, double value, // cost
48-
const std::vector<MLearning>& clouds, // other points
49-
bool minimization, const double delta,
50-
const propts_t& options
51-
);
52-
53-
void update(const std::vector<MLearning>& clouds, bool minimization);
54-
55-
std::pair<double,double> lookup(size_t label, const double* f_var, size_t dimen) const;
56-
57-
void print(std::ostream& s, size_t tabs, std::map<size_t, size_t>& edge_map, const std::vector<MLearning>& clouds) const;
58-
5960
protected:
60-
61-
std::unique_ptr<size_t[]> findIntersection(const double* point) const;
62-
61+
62+
std::unique_ptr<size_t[] > findIntersection(const double* point) const;
63+
6364
struct interesect_t {
6465
size_t _size = 0;
6566
size_t _cloud = std::numeric_limits<size_t>::max();
66-
std::unique_ptr<size_t[]> _nodes = nullptr;
67-
std::unique_ptr<std::pair<qvar_t,qvar_t>[]> _variance = nullptr;
68-
std::unique_ptr<std::pair<qvar_t,qvar_t>[]> _old = nullptr;
67+
std::unique_ptr<size_t[] > _nodes = nullptr;
68+
std::unique_ptr<std::pair<qvar_t, qvar_t>[] > _variance = nullptr;
69+
std::unique_ptr<std::pair<qvar_t, qvar_t>[] > _old = nullptr;
6970

7071
interesect_t() = default;
7172
interesect_t(interesect_t&&) = default;
7273
interesect_t& operator=(interesect_t&&) = default;
7374
interesect_t(const interesect_t& other, size_t dimen);
7475
bool operator<(const interesect_t& other) const;
75-
bool operator != (const interesect_t& other) const;
76+
bool operator!=(const interesect_t& other) const;
7677
};
77-
78+
7879
struct data_t {
7980
avg_t _lmid, _hmid, _mid;
8081
splitfilter_t _splitfilter;
8182
};
82-
83+
8384
struct node_t {
8485
simple_split_t _split;
8586
qvar_t _q;
8687
qvar_t _old;
8788
size_t _parent;
88-
std::vector<interesect_t> _samples;
89-
std::unique_ptr<data_t[]> _data = nullptr;
89+
std::vector<interesect_t> _samples;
90+
std::unique_ptr<data_t[] > _data = nullptr;
9091
node_t() = default;
9192
node_t(const node_t& other, size_t dimen);
9293
node_t(node_t&& other) noexcept = default;
93-
node_t& operator=(node_t&& other) noexcept = default;
94-
94+
node_t& operator=(node_t&& other) noexcept = default;
95+
9596
size_t find_node(const std::vector<node_t>& nodes, const double * point, const size_t id) const;
9697
void update(size_t id, bool minimize, const std::vector<MLearning>& clouds, std::vector<node_t>& nodes, size_t dimen, bool allowSplit, const double delta, const propts_t& options);
97-
std::pair<qvar_t,qvar_t> aggregate_samples(const std::vector<MLearning>& clouds, size_t dimen, bool minimize, std::pair<qvar_t,qvar_t>* tmpq, double discount);
98+
std::pair<qvar_t, qvar_t> aggregate_samples(const std::vector<MLearning>& clouds, size_t dimen, bool minimize, std::pair<qvar_t, qvar_t>* tmpq, double discount);
9899
void print(std::ostream& s, size_t tabs, const std::vector<node_t>& nodes) const;
99100
void tighten_samples(const std::vector<MLearning>& clouds, size_t cloud);
100101
void add_sample(size_t dest, const double* f_var, const double* point, double value, size_t dimen, const std::vector<MLearning>& clouds);
101102
static void update_parents(std::vector<node_t>& nodes, size_t next, bool minimize);
102103
};
103-
104+
104105
size_t _dimen = 0;
105106
std::vector<el_t> _mapping;
106107
std::vector<node_t> _nodes;
107-
};
108-
108+
};
109+
}
109110
#endif /* MLEARNING_H */
110111

src/QLearning.h

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,57 @@
3030
#include <numeric>
3131
#include <cmath>
3232
#include <limits>
33+
namespace prlearn {
3334

34-
template<typename Regressor>
35-
class QLearning
36-
{
37-
public:
38-
QLearning() = default;
39-
QLearning(const QLearning& other) = default;
40-
QLearning& operator = (const QLearning& other) = default;
41-
42-
QLearning(QLearning&&) = default;
43-
QLearning& operator = (QLearning&&) = default;
35+
template<typename Regressor>
36+
class QLearning {
37+
public:
38+
QLearning() = default;
39+
QLearning(const QLearning& other) = default;
40+
QLearning& operator=(const QLearning& other) = default;
4441

45-
46-
void addSample( size_t dimen, // dimensions
47-
const double* f_val, const double* t_val, // source, destination-states
48-
size_t label, // action chosen,
49-
size_t dest, double value, // destination, cost
50-
const std::vector<QLearning>& clouds, // other points
51-
bool minimization, const double delta, const propts_t& options);
52-
53-
void print(std::ostream& s, size_t tabs, std::map<size_t, size_t>& label_map, const std::vector<QLearning<Regressor>>& ) const
54-
{
55-
_regressor.print(s, tabs, label_map);
56-
}
57-
58-
std::pair<double,double> lookup(size_t label, const double* f_var, size_t dimen) const
59-
{
60-
auto res = _regressor.lookup(label, f_var, dimen);
61-
return std::make_pair(res._avg, res._cnt);
62-
}
42+
QLearning(QLearning&&) = default;
43+
QLearning& operator=(QLearning&&) = default;
44+
45+
46+
void addSample(size_t dimen, // dimensions
47+
const double* f_val, const double* t_val, // source, destination-states
48+
size_t label, // action chosen,
49+
size_t dest, double value, // destination, cost
50+
const std::vector<QLearning>& clouds, // other points
51+
bool minimization, const double delta, const propts_t& options);
52+
53+
void print(std::ostream& s, size_t tabs, std::map<size_t, size_t>& label_map, const std::vector<QLearning<Regressor>>&) const {
54+
_regressor.print(s, tabs, label_map);
55+
}
56+
57+
std::pair<double, double> lookup(size_t label, const double* f_var, size_t dimen) const {
58+
auto res = _regressor.lookup(label, f_var, dimen);
59+
return std::make_pair(res._avg, res._cnt);
60+
}
6361

6462
protected:
6563
Regressor _regressor;
66-
};
64+
};
6765

68-
template<typename Regressor>
69-
void QLearning<Regressor>::addSample( size_t dimen, // dimensions
70-
const double* f_var, const double* t_var, // doubles
71-
size_t label, size_t dest, double value, // cost
72-
const std::vector<QLearning<Regressor>>& clouds, // other points
73-
bool minimization, const double delta, const propts_t& options)
74-
{
75-
// The ALPHA part of Q-learning is handled inside the regressors
76-
auto toDone = 0.0;
66+
template<typename Regressor>
67+
void QLearning<Regressor>::addSample(size_t dimen, // dimensions
68+
const double* f_var, const double* t_var, // doubles
69+
size_t label, size_t dest, double value, // cost
70+
const std::vector<QLearning<Regressor>>&clouds, // other points
71+
bool minimization, const double delta, const propts_t& options) {
72+
// The ALPHA part of Q-learning is handled inside the regressors
73+
auto toDone = 0.0;
7774

78-
if(dest != 0 && options._discount != 0)
79-
toDone = clouds[dest]._regressor.getBestQ(t_var, minimization); // 0 is a special sink-node.
80-
auto nval = value;
81-
// if future is not a weird number, then add it (discounted)
82-
if(!std::isinf(toDone) && !std::isnan(toDone))
83-
{
84-
nval = value + (options._discount * toDone);
75+
if (dest != 0 && options._discount != 0)
76+
toDone = clouds[dest]._regressor.getBestQ(t_var, minimization); // 0 is a special sink-node.
77+
auto nval = value;
78+
// if future is not a weird number, then add it (discounted)
79+
if (!std::isinf(toDone) && !std::isnan(toDone)) {
80+
nval = value + (options._discount * toDone);
81+
}
82+
_regressor.update(label, f_var, dimen, nval, delta, options);
8583
}
86-
_regressor.update(label, f_var, dimen, nval, delta, options);
8784
}
8885

8986
#endif /* QLEARNING_H */

0 commit comments

Comments
 (0)