1 #ifndef LloydtypeCFCF_H
2 #define LloydtypeCFCF_H
4 #include "../base/algorithm.h"
5 #include "../base/inputsetter.h"
6 #include "../base/measuresetter.h"
7 #include "../base/proxygenerator.h"
8 #include "../base/dissimilaritymeasure.h"
9 #include "../base/solutionprovider.h"
10 #include "../datastructure/doublesolution.h"
11 #include "../exception/invalidruntimeconfigurationexception.h"
12 #include "../base/euclideanspaceprovider.h"
60 LloydtypeCF(std::vector<T>
const& seeds, std::vector<T*>
const* data = NULL,
66 LloydtypeCF(std::vector<T*>
const& seeds, std::vector<T*>
const* data = NULL,
81 virtual void setInput(std::vector<T*>
const*);
103 espace(s==NULL?NULL:s->clone()), measure(m==NULL?NULL:m->clone()), maxiterations(i)
109 unsigned int i) : seeding(seeds), input(data), espace(s==NULL?NULL:s->clone()),
110 measure(m==NULL?NULL:m->clone()), maxiterations(i)
116 unsigned int i) : input(data), espace(s==NULL?NULL:s->clone()),
117 measure(m==NULL?NULL:m->clone()), maxiterations(i)
123 Algorithm(rhs), input(rhs.input), seeding(rhs.seeding),
124 espace(rhs.espace==NULL?NULL:rhs.espace->clone()),
125 measure(rhs.measure==NULL?NULL:rhs.measure->clone()), maxiterations(rhs.maxiterations)
131 Algorithm::operator=(rhs);
134 delete this->generator;
135 this->generator = pg;
136 delete this->measure;
138 this->input = rhs.
input;
146 delete this->measure;
152 if (this->input==NULL)
154 if (this->measure==NULL)
156 if (this->espace==NULL)
162 unsigned int k = this->seeding.size();
167 T nullVector = espace->nullVector();
168 double nullVectorSq = nullVector * nullVector;
169 std::vector<CFREntry<T>> clusters(k,
CFREntry<T>(0, nullVector, nullVectorSq, nullVector));
170 for(
size_t i = 0; i < k; ++i)
171 clusters[i].representative = seeding[i];
174 unsigned int iterations = 0;
175 std::vector<size_t> assignment(input->size(), -1);
182 for(
size_t i = 0; i < input->size(); ++i)
184 double minDist = std::numeric_limits<double>::infinity();
186 for(
size_t j = 0; j < k; ++j)
188 double tmpDist = measure->dissimilarity(*(*input)[i], clusters[j].representative);
189 if(tmpDist < minDist)
197 if(assignment[i] != minIndex)
199 if(assignment[i] != -1)
200 clusters[assignment[i]].remove(*(*input)[i]);
201 clusters[minIndex].insert(*(*input)[i]);
202 assignment[i] = minIndex;
209 if (this->maxiterations > 0 && iterations > this->maxiterations)
224 for(
size_t i = 0; i < k; ++i)
225 clusters[i].representative = clusters[i].cog();
232 std::vector<T> proxies;
234 for(
size_t i = 0; i < k; ++i)
235 proxies[i] = clusters[i].representative;
238 std::vector<std::vector<T*>> partitions(k, std::vector<T*>());
239 for(
size_t i = 0; i < input->size(); ++i)
240 partitions[assignment[i]].push_back((*input)[i]);
243 for(
size_t i = 0; i < k; ++i)
244 solution->
costs += clusters[i].kMeansCost(clusters[i].cog());
258 this->seeding = seeds;
263 this->seeding.clear();
264 unsigned int size = seeds.size();
265 for (
unsigned int i=0; i<size; i++)
266 this->seeding.push_back(*seeds[i]);
271 this->measure = m==NULL?NULL:m->
clone();
276 this->espace = m==NULL?NULL:m->
clone();
Data structure for partitions and proxies.
LloydtypeCF specific DoubleSolution.
void setEuclideanSpaceProvider(EuclideanSpaceProvider< T > const *)
DissimilarityMeasure< T > * measure
virtual void setInput(std::vector< T * > const *)
LloydtypeCF< T > & operator=(const LloydtypeCF< T > &)
void setSeeding(std::vector< T > const &)
LloydtypeCF(std::vector< T * > const *data=NULL, EuclideanSpaceProvider< T > const *s=NULL, DissimilarityMeasure< T > *m=NULL, unsigned int maxIterations=0)
Constructor initializing input, seeding the centers and configuring the algorithm.
Abstract base class for mechanisms that compute a proxy or representative object for a given set of o...
virtual DissimilarityMeasure< T > * clone() const =0
Clustering feature with representation point.
virtual void setMeasure(DissimilarityMeasure< T > const *)
Abstract base class for algorithms.
virtual LloydtypeCFSolution< T > * compute()
Interface to propagate the ability to set a DissimilarityMeasure.
unsigned int maxiterations
static LloydtypeCF< T > * toLloydtypeCF(Algorithm *a)
does a dynamic cast of the given Algorithm to LloydtypeCF
std::vector< std::vector< T > > proxysets
Abstract base class for dissimilarity measurement.
EuclideanSpaceProvider< T > * espace
std::vector< T * > const * input
Indicates that a computation entered an invalid configuration state.
Lloyd type algorithm using clustering features.
virtual EuclideanSpaceProvider< V > * clone() const =0
std::vector< std::vector< std::vector< T * > > > partitions