Commit 1aec0bbb authored by Laurent Belcour's avatar Laurent Belcour

Adding a first version of the clusterizer that can provide slicing only.

Can't be used in the quadprog setting for the moment since I lok at the
dynamic type of the data. Maybe a parameter to provide (that makes a lot
of parameters to provide).
parent 8e456d84
......@@ -23,7 +23,7 @@ class arguments
// Constructor and destructor
arguments()
{
} ;
}
arguments(int argc, char** const argv)
{
std::string key ;
......@@ -53,8 +53,10 @@ class arguments
}
_map.insert(std::pair<std::string, std::string>(key, data)) ;
}
} ;
~arguments() { } ;
}
~arguments()
{
}
//! \brief is the elements in the command line ?
bool is_defined(const std::string& key) const
......@@ -67,7 +69,7 @@ class arguments
{
return false ;
}
} ;
}
//! \brief access the element stored value
std::string operator[](const std::string& key) const
{
......@@ -80,7 +82,7 @@ class arguments
//std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
return std::string() ;
}
} ;
}
//! \brief acces to the float value associated with the parameter
//! \a key.
//!
......@@ -113,6 +115,9 @@ class arguments
vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
{
vec res(size);
for(int i=0; i<size; ++i)
res[i] = default_value;
if(_map.count(key) > 0)
{
std::string s = _map.find(key)->second;
......
#include "clustering.h"
#include <iostream>
#include <limits>
#include <string>
clustering::clustering(const data* d, const arguments& args)
{
// List of the indices of the clustering
std::vector<int> indices;
vec findices = args.get_vec("cluster-dim", d->dimX(), -1.0f);
for(int i=0; i<findices.size() && findices[i] != -1.0f; ++i)
{
int id = (int)findices[i];
indices.push_back(id);
assert(id >= 0 && id < d->dimX());
}
std::cout << "<<DEBUG>> the resulting data will have " << indices.size() << " dimensions" << std::endl;
// We cannot have null cluster size of full rank.
assert(indices.size() > 0 && indices.size() < d->dimX());
// Fit the slice into the domain of definition of the data
int compressed_size = d->dimX()-indices.size();
vec compressed_vec = args.get_vec("cluster-slice", d->dimX(), std::numeric_limits<float>::max());
for(int i=0; i<d->dimX(); ++i)
{
compressed_vec[i] = std::min<double>(compressed_vec[i], d->max()[i]);
compressed_vec[i] = std::max<double>(compressed_vec[i], d->min()[i]);
}
// Set the limit of the new domain and dimensions
_nX = indices.size();
_nY = d->dimY();
_min = vec(_nX);
_max = vec(_nX);
for(int i=0; i<_nX; ++i)
{
_min[i] = d->min()[indices[i]];
_max[i] = d->max()[indices[i]];
}
for(int i=0; i<d->size(); ++i)
{
vec p = d->get(i);
bool reject = false;
for(int i=0; i<d->dimX(); ++i)
{
if(is_in<int>(indices, i) == -1 && p[i] != compressed_vec[i])
{
reject = true;
}
}
if(reject)
{
continue;
}
vec e(dimX()+dimY());
for(int i=0; i<_nX; ++i)
{
e[i] = p[indices[i]];
}
for(int i=0; i<_nY; ++i)
{
e[_nX + i] = p[d->dimX() + i];
}
_data.push_back(e);
}
std::cout << "<<INFO>> clustering left " << _data.size() << " elements" << std::endl;
}
vec clustering::get(int i) const
{
return _data[i];
}
vec clustering::operator[](int i) const
{
return _data[i];
}
int clustering::size() const
{
return _data.size();
}
vec clustering::min() const
{
return _min;
}
vec clustering::max() const
{
return _max;
}
#pragma once
#include "data.h"
#include "common.h"
#include <vector>
class clustering : public data
{
public: // methods
//! \brief constructor loading a full dimension data and clustering
//! it into a low dimension one.
clustering(const data* d, const arguments& args);
//! \brief the clustering class cannot load from a file. It requires
//! a complete object to be created.
virtual void load(const std::string& filename)
{
throw;
}
//! \brief the clustering class cannot load from a file. It requires
//! a complete object to be created.
virtual void load(const std::string& filename, const arguments& args)
{
throw;
}
//! \brief aces to data in linear order
virtual vec get(int i) const ;
//! \brief aces to data in linear order
virtual vec operator[](int i) const ;
//! \brief return the size of the data after clusterization
int size() const;
//! \brief get min input space values
virtual vec min() const ;
//! \brief get max input space values
virtual vec max() const ;
protected:
std::vector<vec> _data;
vec _min, _max;
};
......@@ -50,7 +50,7 @@ class vec : public std::vector<double>
}
return *this ;
}
// Mathematical operators
//
friend vec operator-(const vec& a)
......@@ -219,11 +219,25 @@ class vec : public std::vector<double>
out << "]" ;
return out ;
} ;
}
} ;
//! \brief locate the first index of value v in vector vec. Complexity in
//! O(n) is the worst case.
template<typename T> int is_in(std::vector<T> ve, T v)
{
int res = -1;
for(unsigned int i=0; i<ve.size(); ++i)
{
if(ve[i] == v)
return i;
}
return res;
}
#define NOT_IMPLEMENTED() \
std::cerr << "<<ERROR>> not implemented " << __FILE__ \
<< ":" << __LINE__ << std::endl; \
......
......@@ -12,8 +12,10 @@ HEADERS = args.h \
plugins_manager.h \
vertical_segment.h \
rational_function.h \
params.h
params.h \
clustering.h
SOURCES = plugins_manager.cpp \
vertical_segment.cpp \
rational_function.cpp
rational_function.cpp \
clustering.cpp
......@@ -7,6 +7,7 @@
#include "data.h"
#include "fitter.h"
#include "args.h"
#include "clustering.h"
#define USING_STATIC
......@@ -77,6 +78,13 @@ class plugins_manager
{
std::cout << "<<DEBUG>> no change was made to the parametrization" << std::endl;
}
// Check is the data has to be clusterized
if(args.is_defined("cluster-dim"))
{
clustering* cluster = new clustering(d, args);
d = cluster;
}
}
//! \brief Provide a measure of how much memory there is on the system.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment