-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
206 lines (165 loc) · 4.36 KB
/
mainwindow.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
float lr = 0.01;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
int layers[] = {2,3,1};
network = new NeuralNetwork(layers,3,1);
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
network->deleteNetwork();
delete network;
delete ui;
}
QPoint MainWindow::resultToPoint(QPoint point)
{
point.setY((this->height() - point.y()));
point.setX(point.x());
return point;
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if( event->key() == Qt::Key_Z)
{
lr *=2;
}
if( event->key() == Qt::Key_S)
{
lr /=2;
}
if( event->key() == Qt::Key_Space)
{
for (int i = 0; i < 50 ; i++ ) {
int x = rand()%1001 - 500;
int y = rand()%1001 - 500;
lstPoints.push_back(QPoint(x,y));
}
repaint();
}
if( event->key() == Qt::Key_T)
{
lstAnswer.clear();
vector<float> errorAverage;
errorAverage.push_back(0);
for(QPoint p: lstPoints)
{
vector<vector<float>> inputs;
vector<float> temp;
QPoint max = getMax(lstPoints);
QPoint min = getMin(lstPoints);
temp.push_back(((float)p.x() - min.x()) / ((float)max.x() - min.x()));
inputs.push_back(temp);
temp.clear();
temp.push_back((((float)p.y() - min.y())) / ((float)max.y() - min.y()));
inputs.push_back(temp);
int goodAnswer = 0;
if(p.y() > funct(p.x()) )
{
goodAnswer = 1;
}
int giveAnswer = 0;
vector<float> networkAnswer = network->propagation(inputs);
if(networkAnswer[0] > 0.5)
{
giveAnswer = 1;
}
if(giveAnswer == goodAnswer)
{
lstAnswer.push_back(p);
}
else
{
errorAverage[0] += (networkAnswer[0] - goodAnswer);
}
}
errorAverage[0] /= lstPoints.count()- lstAnswer.count();
vector<float> err;
err.push_back((errorAverage[0]));
network->trainNetwork(err,lr);
qDebug() << errorAverage[0] << endl;
repaint();
}
qDebug()<< "------------------------------------------------------------------" << endl;
}
int MainWindow::funct(int x)
{
return 0.05*x*x;
}
QPoint MainWindow::getMax(QList<QPoint> lstPoints)
{
if (lstPoints.count() > 0)
{
int xMax = lstPoints.begin()->x();
int yMax = lstPoints.begin()->y();
for (QPoint p:lstPoints)
{
if(xMax < p.x())
{
xMax = p.x();
}
if(yMax < p.y())
{
yMax = p.y();
}
}
return QPoint(xMax,yMax);
}
else
{
return QPoint(0,0);
}
}
QPoint MainWindow::getMin(QList<QPoint> lstPoints)
{
if (lstPoints.count() > 0)
{
int xMin = lstPoints.begin()->x();
int yMin = lstPoints.begin()->y();
for (QPoint p:lstPoints)
{
if(xMin > p.x())
{
xMin = p.x();
}
if(yMin > p.y())
{
yMin = p.y();
}
}
return QPoint(xMin,yMin);
}
else
{
return QPoint(0,0);
}
}
void MainWindow::paintEvent(QPaintEvent *paintEvent)
{
QPainter * painter = new QPainter(this);
QBrush bad = QBrush(Qt::red,Qt::SolidPattern);
QBrush valid = QBrush(Qt::green,Qt::SolidPattern);
QPoint max = getMax(lstPoints);
QPoint min = getMin(lstPoints);
QPoint substract = max - min;
for(QPoint p:lstPoints)
{
painter->setBrush(bad);
for(QPoint pAnswer:lstAnswer)
{
if(p.x() == pAnswer.x() && p.y() == pAnswer.y())
{
painter->setBrush(valid);
}
}
double ratioX = geometry().width()* (p.x()-min.x()) / substract.x();
double ratioY = geometry().height()* (p.y()-min.y()) / substract.y();
painter->drawEllipse(ratioX,geometry().height()-ratioY,7,7);
}
painter->end();
delete painter;
}