-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
208 lines (185 loc) · 5.61 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
205
206
207
208
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QStringListModel>
#include <QDateTime>
enum PROBLEM {
PROBLEM_Multiplexer,
PROBLEM_EvenParity4,
PROBLEM_EvenParity5,
PROBLEM_EvenParity6,
PROBLEM_EvenParity7,
PROBLEM_SymbolRegression
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
_ui(new Ui::MainWindow)
{
_ui->setupUi(this);
_sngpWorker.setProblem(new ProblemMultiplexer());
_sngpWorker.reset();
_timer = new QTimer(this);
connect(_timer, SIGNAL(timeout()), this, SLOT(updateStats()));
connect(_ui->stopGoButton, SIGNAL(clicked()), this, SLOT(pauseResume()));
connect(_ui->goTimes10Button, SIGNAL(clicked()), this, SLOT(goTimes10()));
connect(_ui->goTimes100Button, SIGNAL(clicked()), this, SLOT(goTimes100()));
connect(_ui->resetButton, SIGNAL(clicked()), this, SLOT(reset()));
connect(_ui->stepButton, SIGNAL(clicked()), this, SLOT(step()));
connect(_ui->nodeListView, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(programSelected(const QModelIndex&)));
_ui->problemComboBox->addItem("Multiplexer 6", PROBLEM_Multiplexer);
_ui->problemComboBox->addItem("Even Parity 4", PROBLEM_EvenParity4);
_ui->problemComboBox->addItem("Even Parity 5", PROBLEM_EvenParity5);
_ui->problemComboBox->addItem("Even Parity 6", PROBLEM_EvenParity6);
_ui->problemComboBox->addItem("Even Parity 7", PROBLEM_EvenParity7);
_ui->problemComboBox->addItem("Symbolic Regression", PROBLEM_SymbolRegression);
connect(_ui->problemComboBox, SIGNAL(activated(int)),
this, SLOT(changeProblem(int)));
changeProblem(0);
}
MainWindow::~MainWindow()
{
delete _ui;
}
void MainWindow::reset()
{
_sngpWorker.reset();
updateStats();
}
void MainWindow::pauseResume()
{
if (_sngpWorker.isRunning()) {
_sngpWorker.pause();
_timer->stop();
} else {
_sngpWorker.setNumTimesToRun(1);
_sngpWorker.resume();
_timer->start(1000);
}
updateStats();
updateNodeList();
}
void MainWindow::goTimes(int times)
{
if (_sngpWorker.isRunning()) {
_sngpWorker.pause();
}
_sngpWorker.reset();
_sngpWorker.setNumTimesToRun(times);
_sngpWorker.resume();
_timer->start(1000);
updateStats();
updateNodeList();
}
void MainWindow::goTimes10()
{
goTimes(10);
}
void MainWindow::goTimes100()
{
goTimes(100);
}
void MainWindow::step()
{
if (!_sngpWorker.isRunning()) {
_sngpWorker.step();
updateStats();
}
}
void MainWindow::updateStats()
{
const SNodeStats& stats = _sngpWorker.getStats();
_ui->scoreLabel->setText(QString::number(stats.avgScore));
_ui->generationLabel->setText(QString::number(stats.generation));
_ui->bestScoreLabel->setText(QString::number(stats.bestScoreEver));
_ui->bestIndividualScoreLabel->setText(
QString::number(stats.bestIndividualScore));
_ui->bestIndividualScoreEverLabel->setText(
QString::number(stats.bestIndividualScoreEver));
_ui->hitsLabel->setText(QString("%1/%2").
arg(stats.hits).arg(stats.runs));
int64_t timeTakenMilliseconds = 0;
if (stats.timeTakenMilliseconds == 0) {
if (stats.startTimeMilliseconds > 0) {
timeTakenMilliseconds = QDateTime::currentMSecsSinceEpoch() -
stats.startTimeMilliseconds;
}
} else {
timeTakenMilliseconds = stats.timeTakenMilliseconds;
}
_ui->timeTakenLabel->setText(QString("%1").
arg(timeTakenMilliseconds / 1000.0, 0, 'f', 4));
if (_sngpWorker.isRunning()) {
_ui->stopGoButton->setText("Stop");
} else {
_ui->stopGoButton->setText("Go");
updateNodeList();
_timer->stop();
}
}
void MainWindow::updateNodeList()
{
QStringList nodeList;
const std::vector<SNode>& nodes = _sngpWorker.getNodes();
const std::vector<int>& values = _sngpWorker.getFitness();
if (nodes.size() > 0) {
for (int i = 0; i < (int)nodes.size(); ++i) {
const SNode& node = nodes[i];
QString nodeDetails;
int val = 0;
if ((int)values.size() > i) {
val = values[i];
}
nodeDetails = QString("%1: %2 score: %3").arg(i).
arg(node.asString()).
arg(val);
nodeList << nodeDetails;
}
QAbstractItemModel *model =
new QStringListModel(nodeList, _ui->nodeListView);
_ui->nodeListView->setModel(model);
} else {
_ui->nodeListView->reset();
}
}
void MainWindow::programSelected(const QModelIndex& index)
{
showProgram(index.row());
}
void MainWindow::changeProblem(int index)
{
Problem* p = NULL;
switch((PROBLEM)_ui->problemComboBox->itemData(index).toInt()) {
case PROBLEM_EvenParity4:
p = new ProblemEvenParity(4);
break;
case PROBLEM_EvenParity5:
p = new ProblemEvenParity(5);
break;
case PROBLEM_EvenParity6:
p = new ProblemEvenParity(6);
break;
case PROBLEM_EvenParity7:
p = new ProblemEvenParity(7);
break;
case PROBLEM_SymbolRegression:
p = new ProblemSymbolicRegression();
break;
case PROBLEM_Multiplexer:
p = new ProblemMultiplexer();
default:
break;
}
if (_sngpWorker.isRunning()) {
_sngpWorker.pause();
}
_sngpWorker.setProblem(p);
_sngpWorker.reset();
updateStats();
updateNodeList();
}
void MainWindow::showProgram(int index)
{
QString text = _sngpWorker.getProgramAsText(index);
_ui->logBrowser->setPlainText(text);
}