-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDMA.cpp
More file actions
166 lines (141 loc) · 4.43 KB
/
DMA.cpp
File metadata and controls
166 lines (141 loc) · 4.43 KB
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
///////////////////////////////////////////////////////////
// DMA.cpp
// Implementation of the Class DMA
// Created on: 07-Lie-2013 20:07:30
// Original author: Povilas
///////////////////////////////////////////////////////////
#include "DMA.h"
#include "ShufleEnum.h"
#include "ShufleObjects.h"
#include <float.h>
#include <fstream>
#include "DistanceMetrics.h"
#include "DistanceMetricsEnum.h"
#include "AdditionalMethods.h"
#include <cmath>
#include <iostream>
/*! \class DMA
\brief A class of methods and attributes for DMA algorithm.
*/
DMA::DMA()
{
}
DMA::~DMA()
{
}
DMA::DMA(double eps, int maxIter, int d, int neighbours):MDS(eps, maxIter, d)
{
neighbourNumber = neighbours;
X = ObjectMatrix(AdditionalMethods::inputDataFile);
X.loadDataMatrix();
int m = X.getObjectCount();
gutman = ObjectMatrix(m);
std::vector<double> gutmanRow;
gutmanRow.resize(m, 0);
for (int i = 0; i < m; i++)
gutman.addObject(DataObject(gutmanRow));
initializeProjectionMatrix();
}
/*DMA::DMA(double eps, int maxIter, int d, int neighbours, ObjectMatrix x):MDS(eps, maxIter, d){
neighbourNumber = neighbours;
X = x;
int m = X.getObjectCount();
gutman = ObjectMatrix(m);
std::vector<double> gutmanRow;
gutmanRow.resize(m, 0);
for (int i = 0; i < m; i++)
gutman.addObject(DataObject(gutmanRow));
initializeProjectionMatrix();
}*/
int DMA::getNeighbours()
{
return neighbourNumber;
}
ObjectMatrix DMA::getProjection()
{
stressErrors.reserve(maxIteration);
int m = X.getObjectCount();
int iteration = 0;
stressErrors.push_back(DimReductionMethod::getStress());
double Epsilon = DBL_MAX;
double sum;// = 0.0;
ObjectMatrix Y_new(m), Xinit(m);
ObjectMatrix gutman;
Y_new = Y; //keeps order of the object such as it is at X matrix
Xinit = X;
while (iteration < maxIteration && Epsilon > epsilon)
{
shuffle();
gutman = getGutman(neighbourNumber); //atsizvelgiant i naujus indeksus
for (int i = 0; i < m; i++)
{
for (int j = 0; j < d; j++)
{
sum = 0.0;
for (int k = (i - neighbourNumber); k <= (i + neighbourNumber); k++)
if (k >= 0 && k < m)
if (k !=i)
sum += gutman.getObjectAt(i).getFeatureAt(k) * Y.getObjectAt(k).getFeatureAt(j); // +1 pridedamas formuojant gutmano matrica //Y u=tikrina nauj1 tvarka po sumai6ymo
else
sum += (gutman.getObjectAt(i).getFeatureAt(k) - getV(i)) * Y.getObjectAt(k).getFeatureAt(j);
Y_new.updateDataObject(shufledIndexes.at(i), j, Y.getObjectAt(i).getFeatureAt(j) + 0.5 * sum / getV(i)); //atnaujinam ne i6 eiles o tai ka k1 rodo sumai6ymas
}
}
Y = Y_new; //permetam atgal eik6mes kad nereiktu keisti stress skaiciavimo
X = Xinit;
iteration++;
stressErrors.push_back(DimReductionMethod::getStress());
Epsilon = std::fabs(stressErrors.at(iteration - 1) - stressErrors.at(iteration));
}
Y.setPrintClass(X.getStringClassAttributes());
return Y;
}
int DMA::getV(int i)
{
int k1 = i - neighbourNumber;
int k2 = i + neighbourNumber;
if (k1 < 0)
k1 = 0;
if (k2 >= m)
k2 = m - 1;
return k2 - k1;
}
void DMA::setNeighbours(int neighbours)
{
neighbourNumber = neighbours;
}
void DMA::shuffle()
{
int m = X.getObjectCount();
int j = 0;
ObjectMatrix Xshuffled(m);
ObjectMatrix Yshuffled(m);
shufledIndexes = ShufleObjects::shufleObjectMatrix(RANDOM, Y);
for (int i = 0; i < m; i++)
{
j = shufledIndexes.at(i);
Xshuffled.addObject(X.getObjectAt(j));
Yshuffled.addObject(Y.getObjectAt(j));
}
X = Xshuffled;
Y = Yshuffled;
}
/*double DMA::getStress(){
/*double sum1 = 0.0, sum2 = 0.0, stress = 0.0;
int m = X.getObjectCount();
double distX = 0.0;
double distY = 0.0;
for (int i = 0; i < m - 1; i++)
{
for (int j = i + 1; j < m; j++)
{
distX = DistanceMetrics::getDistance(X.getObjectAt(i), X.getObjectAt(j), EUCLIDEAN);
distY = DistanceMetrics::getDistance(Y.getObjectAt(i), Y.getObjectAt(j), EUCLIDEAN);
sum1 += std::pow(distX - distY, 2);
sum2 += std::pow(distX, 2);
}
}
stress = std::sqrt(sum1 / sum2);
return stress;
return MDS::getStress();
}*/