-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradientdescent.cpp
148 lines (122 loc) · 3.99 KB
/
gradientdescent.cpp
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
#include "gradientdescent.h"
#include <QCoreApplication>
#include <QQueue>
#include <QPair>
#include <QThread>
#include <QMutex>
#include <QEventLoop>
void GDWorker::run()
{
bool queueIsEmpty = false;
double b, c;
while(!queueIsEmpty)
{
m_ctx.m1.lock();
queueIsEmpty = m_ctx.queue.isEmpty();
if (!queueIsEmpty) {
auto p = m_ctx.queue.dequeue();
b = p.first;
c = p.second;
int threadNum = 0;
if (threadNum == 0) {
QCoreApplication::processEvents();
double progress = m_ctx.tableSizeB * m_ctx.tableSizeC - m_ctx.queue.size();
progress /= m_ctx.tableSizeB * m_ctx.tableSizeC;
m_ctx.m2.lock();
emit progressChanged(progress);
m_ctx.m2.unlock();
}
if(m_ctx.m_cancelled)
{
m_ctx.m1.unlock();
break;
}
}
m_ctx.m1.unlock();
if(queueIsEmpty)
break;
auto f = [b, this](double c){
return DropGenerator::calculateError(DropGenerator::generateTheoreticalModel(TheoreticalModelParameters(m_ctx.dropType, b, c, m_ctx.precision, m_ctx.cutoffMoment)), m_ctx.experimental);
};
auto der = [f](double c){
const double h = 0.001;
return (f(c + h) - f(c)) / h;
};
double cNext;
int steps = 0;
const int maxSteps = 75;
while(steps < maxSteps)
{
double alpha = 0.1;
const double fc = f(c);
double fcn;
do
{
cNext = c -alpha*der(c);
alpha /= 2;
fcn = f(cNext);
++steps;
} while(fc <= fcn && qAbs(cNext - c) > m_ctx.gdPrecision && !(qIsInf(fc) && qIsInf(fcn)) && steps < maxSteps);
if(qAbs(cNext - c) <= m_ctx.gdPrecision || (qIsInf(fc) && qIsInf(fcn)))
break;
c = cNext;
}
c = cNext;
double error = f(c);
m_ctx.m3.lock();
if (error < m_ctx.bestError) {
m_ctx.bestParameters.c = c;
m_ctx.bestParameters.b = b;
m_ctx.bestError = error;
}
m_ctx.m3.unlock();
}
}
void GradientDescent::doWork(const QVector<QPointF> &experimental, TheoreticalModelParameters::DropType dropType, double precision, int cutoffMoment)
{
const double minB = 0.1, maxB = 3.0;
const double minC = -6.0, maxC = 0.0;
QQueue<QPair<double, double>> queue;
const int tableSizeB = 100, tableSizeC = 15;
for (int bi = 0; bi < tableSizeB; ++bi) {
for (int ci = 0; ci < tableSizeC; ++ci) {
double b = minB + bi * (maxB - minB) / tableSizeB;
double c = minC + ci * (maxC - minC) / tableSizeC;
queue.enqueue({b, c});
}
}
TheoreticalModelParameters bestParameters(dropType, 0, 0, precision, cutoffMoment);
unsigned threadCount = QThread::idealThreadCount();
GDWorker *threads[threadCount];
GDWorker::context context{QMutex(), QMutex(), QMutex(), queue, false, experimental,
dropType, precision, cutoffMoment, qInf(), bestParameters};
m_ctx = &context;
QEventLoop eventLoop;
unsigned threadsJoined = 0;
auto countJoinedThreads = [threadCount, &eventLoop, &threadsJoined]()
{
if(++threadsJoined == threadCount)
eventLoop.quit();
};
for(GDWorker*& t : threads)
{
t = new GDWorker(context);
connect(t, &GDWorker::progressChanged, this, &GradientDescent::progressChanged);
connect(t, &QThread::finished, countJoinedThreads);
t->start();
}
eventLoop.exec();
for(GDWorker* t : threads)
{
t->wait();
t->deleteLater();
}
if (context.m_cancelled)
emit cancelled();
else
emit finished(context.bestParameters);
}
void GradientDescent::cancel()
{
m_ctx->m_cancelled = true;
}