-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtspsom.cpp
127 lines (101 loc) · 3.77 KB
/
tspsom.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
#include "tspsom.h"
#include "ui_tspsom.h"
#include <QVector>
#include <QVariant>
#include <QMessageBox>
#include <QtConcurrent/QtConcurrent>
TspSom::TspSom(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::TspSom)
, citiesMap()
, som()
{
ui->setupUi(this);
//Setting up the plotting area
ui->graph_qcustomplot->xAxis->setRange(0,1);
ui->graph_qcustomplot->yAxis->setRange(0,1);
//Citites graph configuration
ui->graph_qcustomplot->addGraph();
ui->graph_qcustomplot->graph(0)->setLineStyle(QCPGraph::lsNone); //Dots
ui->graph_qcustomplot->graph(0)->setPen(QPen(Qt::black)); //Color black
ui->graph_qcustomplot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssStar, 10)); //Stars
//SOM graph configuration (a graph with loops needs to be defined as QCPCurve)
//Qcustomplot takes ownership of the new curve so no direct managing is required.
somCurve = new QCPCurve(ui->graph_qcustomplot->xAxis, ui->graph_qcustomplot->yAxis);
somCurve->setPen(QPen(Qt::blue)); //color blue
somCurve->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 3)); //Filled circles
//Populate combobox
ui->initialization_comboBox->addItem("Circular", QVariant(true));
ui->initialization_comboBox->addItem("Random", QVariant(false));
//Connect qt signals and slots (more connections are formed in the uic)
QObject::connect(&som, SIGNAL(trainingIterationDone()), this, SLOT(updatePlot()));
QObject::connect(&som, SIGNAL(trainingDone(bool)), this, SLOT(enableButtons(bool)));
}
TspSom::~TspSom()
{
delete ui;
}
/**
* @brief TspSom::generateMapClicked Qt Slot that handless the "generate map" -button click.
* Generates the appropriate amount of cites.
*/
void TspSom::generateMapClicked()
{
int numberOfCities = ui->cities_spinBox->value();
citiesMap.InitializeRandom(numberOfCities);
QVector<double> x = citiesMap.getXaxis();
QVector<double> y = citiesMap.getYaxis();
ui->graph_qcustomplot->graph(0)->setData(x, y);
ui->graph_qcustomplot->replot();
}
/**
* @brief TspSom::initializeSOM_clicked Qt slot that handles the "Initialize SOM" -button click.
* Initializes the SOM according to parmeters in form.
*/
void TspSom::initializeSOM_clicked()
{
bool initMode = ui->initialization_comboBox->currentData().toBool();
int nodes = ui->nodes_spinBox->value();
if (!initMode) {
som.InitializeRandom(nodes);
}else {
som.InitializeCircular(nodes);
}
updatePlot();
}
/**
* @brief TspSom::trainClicked Qt slot that handless the "Train SOM" button click.
*/
void TspSom::trainClicked(){
//Show errormessage if citesMap or som isn't initialized.
if (citiesMap.getPoints().isEmpty() || som.getPoints().isEmpty()){
QMessageBox errorBox;
errorBox.setText("Map has to be generated and SOM need to be initialized before training!");
errorBox.setIcon(QMessageBox::Critical);
errorBox.exec();
return;
}
int iterations = ui->iterations_spinBox->value();
//Disable Ui buttons, while training is in progress
enableButtons(false);
//Initiates the training process in another thread. The future object is not used here.
QFuture<void> future = QtConcurrent::run(&som, &SOM::train, citiesMap, iterations);
}
/**
* @brief updatePlot is a slot that updates the plot in UI
*/
void TspSom::updatePlot(){
QVector<double> x = som.getXaxis();
QVector<double> y = som.getYaxis();
somCurve->setData(x, y);
ui->graph_qcustomplot->replot();
}
/**
* @brief TspSom::enableButtons slot that sets the enabled status of buttons.
* @param b
*/
void TspSom::enableButtons(bool b){
ui->generateMap_pushButton->setEnabled(b);
ui->initializeSOM_pushButton->setEnabled(b);
ui->trainSOM_pushButton->setEnabled(b);
}