-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSMACOF.cpp
More file actions
164 lines (142 loc) · 4.32 KB
/
SMACOF.cpp
File metadata and controls
164 lines (142 loc) · 4.32 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
///////////////////////////////////////////////////////////
// SMACOF.cpp
// Implementation of the Class SMACOF
// Created on: 07-Lie-2013 20:07:32
// Original author: Povilas
///////////////////////////////////////////////////////////
/*! \class SMACOF
\brief A class of methods and attributes for SMACOF algorithm.
*/
#include "SMACOF.h"
#include "ShufleObjects.h"
#include <string>
#include <sstream>
#include <vector>
#include "DataObject.h"
#include <float.h>
#include <cmath>
#include <iostream>
#include "AdditionalMethods.h"
#include "DistanceMetrics.h"
SMACOF::SMACOF()
{
}
SMACOF::~SMACOF()
{
}
SMACOF::SMACOF(double eps, int maxIter, int d):MDS(eps, maxIter, d)
{
X = ObjectMatrix(AdditionalMethods::inputDataFile);
X.loadDataMatrix();
readFile = true;
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();
}
SMACOF::SMACOF(double eps, int maxIter, int d, ObjectMatrix X_base, ObjectMatrix Y_base):MDS(eps, maxIter, d)
{
X = X_base;
readFile = false;
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));
Y = Y_base;
}
/*SMACOF::SMACOF(ObjectMatrix initialY, double eps, int maxIter, int d):MDS(eps, maxIter, d){
Y = initialY;
}*/
SMACOF::SMACOF(double eps, int maxIter, int d, ObjectMatrix initialX):MDS(eps, maxIter, d)
{
X = initialX;
readFile = false;
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));
// Y = Y_base;
initializeProjectionMatrix();
}
ObjectMatrix SMACOF::getProjection()
{
stressErrors.reserve(maxIteration);
int iteration = 0;
int m = X.getObjectCount();
//double str = getStress();
stressErrors.push_back(getStress());
double sum = 0.0;
double Epsilon = DBL_MAX;
// ObjectMatrix Gutman;
ObjectMatrix Y_new(m);
ObjectMatrix Y_loc = Y; // runs faster if have local copy of Y matrix
std::vector<double> Y_newRow;
Y_newRow.reserve(d);
DataObject obj1;
while (iteration < maxIteration && Epsilon > epsilon)
{
Y_new.clearDataObjects();
gutman = getGutman();
for (int i = 0; i < m; i++)
{
Y_newRow.clear();
obj1 = gutman.getObjectAt(i);
for (int j = 0; j < d; j++)
{
sum = 0.0;
for (int k = 0; k < m; k++)
sum += obj1.getFeatureAt(k) * Y_loc.getObjectAt(k).getFeatureAt(j); //get from local copy
Y_newRow.push_back(sum / (float) m);
}
Y_new.addObject(DataObject(Y_newRow), X.getObjectAt(i).getClassLabel());
}
Y = Y_new;
Y_loc = Y;
//str = getStress();
stressErrors.push_back(getStress());
iteration++;
Epsilon = std::fabs(stressErrors.at(iteration - 1) - stressErrors.at(iteration));
// std::cout << iteration << " " << Epsilon << " " << str << std::endl;
}
Y.setPrintClass(X.getStringClassAttributes());
return Y;
}
double SMACOF::getStress()
{
if (readFile) //depends if it was standalone run or was called from relative MDS method
return DimReductionMethod::getStress();
else
{
double stress = 0.0, weight= 0.0;
int m = X.getObjectCount();
double distX = 0.0;
double distY = 0.0;
double tmp;
DataObject objXi, objYi;
for (int i = 0; i < m - 1; i++)
{
objXi = X.getObjectAt(i);
objYi = Y.getObjectAt(i);
for (int j = i + 1; j < m; j++)
{
distX = DistanceMetrics::getDistance(objXi, X.getObjectAt(j), EUCLIDEAN);
distY = DistanceMetrics::getDistance(objYi, Y.getObjectAt(j), EUCLIDEAN);
tmp = distX - distY;
stress += tmp * tmp;
weight += distX * distX;
}
}
return stress / weight;
}
}
/*ObjectMatrix SMACOF::getGutmanMatrix()
{
return MDS::getGutman();
}*/