algo k_proches_voisins termine

This commit is contained in:
Felix Maurin 2019-12-09 17:05:36 +01:00
parent 849434ce13
commit f0e2fa071c

View file

@ -2,12 +2,6 @@
#include <math.hpp> #include <math.hpp>
#include <stdexcept> #include <stdexcept>
int img_dict() {
map< math::csignal, std::string > dico;
return 0
};
double distance(math::csignal v1, math::csignal v2, int n){ double distance(math::csignal v1, math::csignal v2, int n){
if (v1.size() != v2.size()) { if (v1.size() != v2.size()) {
@ -17,7 +11,7 @@ double distance(math::csignal v1, math::csignal v2, int n){
int m = v1.size(); int m = v1.size();
double d; double d;
double di; double di;
for (int i = 0 ; i<m ; ++m){ for (int i = 0 ; i<m ; ++i){
di = std::abs(v1[i] - v2[i]); di = std::abs(v1[i] - v2[i]);
di = std::pow(di, n); di = std::pow(di, n);
d = d + di; d = d + di;
@ -26,7 +20,18 @@ double distance(math::csignal v1, math::csignal v2, int n){
return d; return d;
}; };
int argmax(vector<int> v){
int arg = 0;
int max = v[0];
int n = v.size();
for(int i = 1; i < n ; ++i){
if (v[i]>max){
arg = i;
max = v[i];
};
};
return arg
};
int main(math::csignal new_vect, map< math::csignal, std::string > dico, int k){ int main(math::csignal new_vect, map< math::csignal, std::string > dico, int k){
double d; double d;
@ -36,12 +41,11 @@ int main(math::csignal new_vect, map< math::csignal, std::string > dico, int k){
int droite = 0; int droite = 0;
int gauche = 0; int gauche = 0;
int rejet = 0; int rejet = 0;
vector<int> choix; vector<int> vchoix;
for(auto& ref_vect : dico){ for(auto& ref_vect : dico){
d = distance(new_vect, ref_vect.first ); d = distance(new_vect, ref_vect.first );
if (k_min.size() < k ){ if (k_min.size() < k ){
k_min.push_back({d, ref_vect.first}); k_min.push_back({d, ref_vect.first});
sort(k_min.begin(), k_min.end());
} else if (d < k_min[k-1].first){ } else if (d < k_min[k-1].first){
k_min.push_back({d, ref_vect.first}); k_min.push_back({d, ref_vect.first});
sort(k_min.begin(), k_min.end()); sort(k_min.begin(), k_min.end());
@ -52,23 +56,35 @@ int main(math::csignal new_vect, map< math::csignal, std::string > dico, int k){
for(auto i = k_min.begin(); i != k_min.end(); i++) { for(auto i = k_min.begin(); i != k_min.end(); i++) {
if (dico[k_min[i].second].second == "avance"){ if (dico[k_min[i].second].second == "avance"){
avance = avance + 1 avance = avance + 1
} else if (dico[k_min[i].second] == "arret"){ } else if (dico[k_min[i].second].second == "arret"){
arret = arret + 1 arret = arret + 1
} else if (dico[k_min[i].second] == "droite"){ } else if (dico[k_min[i].second].second == "droite"){
arret = droite + 1 arret = droite + 1
} else if (dico[k_min[i].second] == "gauche"){ } else if (dico[k_min[i].second].second == "gauche"){
arret = gauche + 1 arret = gauche + 1
} else if (dico[k_min[i].second] == "rejet"){ } else if (dico[k_min[i].second].second == "rejet"){
arret = rejet + 1 arret = rejet + 1
} ; }
choix.push_back(avance);
choix.push_back(arret);
choix.push_back(droite);
choix.push_back(gauche);
choix.push_back(rejet);
}; };
vchoix.push_back(avance);
vchoix.push_back(arret);
vchoix.push_back(droite);
vchoix.push_back(gauche);
vchoix.push_back(rejet);
int nchoix = argmax(vchoix);
string choix ;
if (nchoix == 0){
choix = "avance"
} else if (nchoix == 1){
choix = "arret"
} else if (nchoix == 2){
choix = "droite"
} else if (nchoix == 3){
choix = "gauche"
} else if (nchoix == 4){
choix = "rejet"
}
}; };