-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
86 lines (68 loc) · 1.92 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "data.h"
#include "network.h"
#include <QPainter>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow) {
ui->setupUi(this);
network = new Network(vector<int>{2, 3, 2});
connect(ui->learn, &QPushButton::released, this, &MainWindow::learn);
connect(ui->draw, &QPushButton::released, this, &MainWindow::mydraw);
learningThread = std::thread{&MainWindow::learnThread, this};
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
for (int x = 0; x < colors.size(); ++x) {
for (int y = 0; y < colors[0].size(); ++y) {
painter.setPen(colors[x][y]);
painter.drawPoint(x + 20, y + 120);
}
}
for (auto& point : dataset) {
painter.setPen(qRgb((1 - point.expected) * 255, point.expected * 255, 0));
int x = int(point.inputs[0] * 500) + 20;
int y = int(point.inputs[1] * 500) + 120;
painter.drawPoint(x - 1, y);
painter.drawPoint(x + 1, y);
painter.drawPoint(x, y - 1);
painter.drawPoint(x, y + 1);
painter.drawPoint(x, y);
}
painter.end();
}
void MainWindow::learnThread() {
int counter = 100;
while (true) {
while (learning) {
network->learn();
if (counter == 100) {
mydraw();
counter = 0;
}
counter++;
}
this_thread::yield();
}
}
void MainWindow::learn() {
learning = !learning;
}
void MainWindow::mydraw() {
// threading, prevent fuzzy output
bool originalLearningStatus = learning;
learning = false;
colors = network->visualize();
update();
network->print();
// restart learning if enabled before
learning = originalLearningStatus;
}
MainWindow::~MainWindow() {
delete ui;
delete network;
}