-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelTester.h
152 lines (134 loc) · 3.15 KB
/
ModelTester.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <vector>
#include <string>
#include "Hypothesis.h"
#include <thread>
struct InferenceResults
{
std::vector<std::string> Models;
std::vector<double> Scores;
int BestModelIdx;
std::string BestModel;
};
template<class DataClass>
class ModelTester
{
public:
ModelTester()
{
Nthreads = 1;
}
ModelTester(int n)
{
Nthreads = n;
}
template<class T>
void AddHypothesis(T guess)
{
std::unique_ptr<T> p = std::make_unique<T>(guess);
Suppositions.push_back(std::move(p));
}
InferenceResults BeginTest(const std::vector<DataClass> & Data, int resolution)
{
int N = Suppositions.size();
if (N < 2)
{
std::cout << "Cannot perform test with fewer than 2 hypotheses" << std::endl;
exit(5);
}
Scores = std::vector(N,0.0);
Threads.resize(Nthreads-1);
Names.resize(N);
std::vector<int> count(Nthreads,0);
int allocated = 0;
int idx = 0;
while (allocated < N)
{
++count[idx % Nthreads];
++allocated;
++idx;
}
int t = 0;
for (int i = 0; i < Nthreads -1; ++i)
{
Threads[i] = std::thread(&ModelTester::ChunkedScoreLauncher,this,Data,resolution,t,count[i]);
t+=count[i];
}
// Threads[Nthreads-1]= std::thread(&ModelTester::ChunkedScoreLauncher,this,Data,resolution,t,count[Nthreads-1]);
ChunkedScoreLauncher(Data,resolution,t,count[Nthreads-1]);
int joined = 1;
while (joined < Nthreads)
{
for (int n = 0; n < Nthreads-1; ++n)
{
if (Threads[n].joinable())
{
Threads[n].join();
++joined;
}
}
}
double bestScore;
int bestHyp;
for (int i = 0; i <N; ++i)
{
if (i==0 || Scores[i] > bestScore)
{
bestScore = Scores[i];
bestHyp = i;
}
}
for (int i = 0; i < Suppositions.size(); ++i)
{
Scores[i] -= bestScore;
}
InferenceResults output;
output.Models = Names;
output.Scores = Scores;
output.BestModel = Names[bestHyp];
output.BestModelIdx = bestHyp;
return output;
}
std::vector<double> FitModel(int modelIdx,const std::vector<DataClass> & Data)
{
auto start = Suppositions[modelIdx]->paramMidPoint();
if (start.size() > 0)
{
return Suppositions[modelIdx]->FindMaximum(Data,start,100000);
}
else
{
return {};
}
}
int Verbosity = 0;
private:
std::vector<std::unique_ptr<Hypothesis<DataClass>>> Suppositions;
std::vector<double> Scores;
std::vector<std::thread> Threads;
std::vector<std::string> Names;
int Nthreads;
void ChunkedScoreLauncher(const std::vector<DataClass> & Data, int resolution,int start, int size)
{
int end = std::min(start+size,(int)Suppositions.size());
if (Verbosity >0)
{
std::cout << "Testing " << Suppositions.size() << " models" << std::endl;
}
for (int i = start; i < end; ++i)
{
if (Verbosity >0)
{
std::cout << "\tBeginning Test on " << Suppositions[i]->Identifier << std::endl;
}
Suppositions[i]->Verbosity = Verbosity;
double S = Suppositions[i]->Score(Data,resolution);
Scores[i] = S;
Names[i] =Suppositions[i]->Identifier;
if (Verbosity>0)
{
std::cout << "\t\tScored: " << S << std::endl;
}
}
}
};