Merge branch 'dev' of gitlab.rezometz.org:guillaume/miniprojet into dev
This commit is contained in:
commit
8d28cdcb01
1 changed files with 74 additions and 0 deletions
74
tests/examples/k_proches_voisins
Normal file
74
tests/examples/k_proches_voisins
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include <map>
|
||||
#include <math.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
int img_dict() {
|
||||
map< math::csignal, std::string > dico;
|
||||
return 0
|
||||
};
|
||||
|
||||
double distance(math::csignal v1, math::csignal v2, int n){
|
||||
|
||||
if (v1.size() != v2.size()) {
|
||||
throw std::runtime_error("les deux vecteurs doivent être de même longueur");
|
||||
};
|
||||
|
||||
int m = v1.size();
|
||||
double d;
|
||||
double di;
|
||||
for (int i = 0 ; i<m ; ++m){
|
||||
di = std::abs(v1[i] - v2[i]);
|
||||
di = std::pow(di, n);
|
||||
d = d + di;
|
||||
};
|
||||
d = std::pow(d, 1/n);
|
||||
return d;
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(math::csignal new_vect, map< math::csignal, std::string > dico, int k){
|
||||
double d;
|
||||
vector< std::pair<double , math::csignal> > k_min;
|
||||
int avance = 0;
|
||||
int arret = 0;
|
||||
int droite = 0;
|
||||
int gauche = 0;
|
||||
int rejet = 0;
|
||||
vector<int> choix;
|
||||
for(auto& ref_vect : dico){
|
||||
d = distance(new_vect, ref_vect.first );
|
||||
if (k_min.size() < k ){
|
||||
k_min.push_back({d, ref_vect.first});
|
||||
sort(k_min.begin(), k_min.end());
|
||||
} else if (d < k_min[k-1].first){
|
||||
k_min.push_back({d, ref_vect.first});
|
||||
sort(k_min.begin(), k_min.end());
|
||||
k_min.pop_back();
|
||||
};
|
||||
|
||||
} ;
|
||||
for(auto i = k_min.begin(); i != k_min.end(); i++) {
|
||||
if (dico[k_min[i].second].second == "avance"){
|
||||
avance = avance + 1
|
||||
} else if (dico[k_min[i].second] == "arret"){
|
||||
arret = arret + 1
|
||||
} else if (dico[k_min[i].second] == "droite"){
|
||||
arret = droite + 1
|
||||
} else if (dico[k_min[i].second] == "gauche"){
|
||||
arret = gauche + 1
|
||||
} else if (dico[k_min[i].second] == "rejet"){
|
||||
arret = rejet + 1
|
||||
} ;
|
||||
|
||||
choix.push_back(avance);
|
||||
choix.push_back(arret);
|
||||
choix.push_back(droite);
|
||||
choix.push_back(gauche);
|
||||
choix.push_back(rejet);
|
||||
|
||||
|
||||
|
||||
};
|
||||
};
|
Loading…
Reference in a new issue