// Copyright (C) 2001 Jean-Marc Valin #include "Cell.h" #include #include "ObjectParser.h" #include "misc.h" #include #ifdef HAVE_FLOAT_H #include #endif using namespace std; namespace FD { DECLARE_TYPE(Cell) //@implements VQ void Cell::recursiveSplit (const vector > &data, int level) { if (level <= 0) //if (data.size() < 50) { cout << "LEAF: " << data.size() << endl; return; } int dim; float thresh; //cerr << "aa\n"; split(data, dim, thresh); //cerr << "bb\n"; vector > firstData; vector > secondData; for (int i=0;irecursiveSplit(firstData, level-1); second->recursiveSplit(secondData, level-1); } void Cell::split(const vector > &data, int &bestDim, float &bestThreshold) { bestDim=0; int nbEqual=0; bestThreshold=0; float bestMutual = -FLT_MAX; for (int i=0;i bestMutual) { isBest=true; nbEqual=0; } if (currentMutual == bestMutual) { cerr << "randomizing at " << currentMutual << " size = " << data.size() << "\n"; nbEqual++; if (rand()%nbEqual==0) isBest=true; } if (isBest) { bestMutual=currentMutual; bestDim=i; bestThreshold=threshold; } } //cerr << "bestDim: " << bestDim << " bestThreshold: " << bestThreshold << endl; //if (some condition on bestMutual) don't perform the split //splitWithThreshold(data, bestDim, bestThreshold); } /*void Cell::findThreshold(const vector > &data, int dim, float &bestThresh, float &bestScore) { if (data.size()==0) { bestThresh=0; bestScore = 0; return; } float min_value = FLT_MAX, max_value = -FLT_MAX; int min_ind = 0, max_ind = 0; int i,k; for (i=0;i max_value) { max_value = data[i].second[dim]; max_ind=i; } if (data[i].second[dim] < min_value) { min_value = data[i].second[dim]; min_ind=i; } } bestThresh = 0; bestScore = -FLT_MAX; float thresh; float score; for (thresh = min_value; thresh < max_value; thresh += (max_value-min_value)/15.0) { int sumAi = 0, sumBi = 0; vector Ai (numberClasses, 0); vector Bi (numberClasses, 0); for (k=0;k= thresh) { sumAi++; Ai[data[k].first]++; } else { sumBi++; Bi[data[k].first]++; } } double weight = double(sumAi)/data.size(); //cerr << "weight: " << weight << " sumAi = " << sumAi << endl; score = 0.0; for (i = 0;i bestScore) { bestThresh = thresh; bestScore = score; } } }*/ /* void Cell::findThreshold(const vector > &data, int dim, float &bestThresh, float &bestScore) { float sum = 0, s2 = 0; int i,k; for (i=0;i > &data, int dim, float &thresh, float &score) { float sum = 0; int i,k; for (i=0;i Ai (numberClasses, 0); vector Bi (numberClasses, 0); for (k=0;k= thresh) { sumAi++; Ai[data[k].first]++; } else { sumBi++; Bi[data[k].first]++; } } double weight = double(sumAi)/data.size(); score = 0.0; for (i = 0;i > &data, int dim, float &thresh, float &score) { float sum = 0; int i,k; for (i=0;i scores (numberClasses, 0); for (i=0;i= thresh) { scores[data[i].first]++; } else { scores[data[i].first]--; } score = 0.0; for (i=0;i > &data, int dim, float &bestThresh, float &bestScore) { float sum = 0, s2 = 0; int i,k; for (i=0;i" << endl; out << "" << endl; out << "" << endl; if (terminal) { out << "" << endl; } else { out << "" << endl; out << "" << endl; out << "" << endl;; out << "" << endl;; } out << ">\n"; } ostream &operator << (ostream &out, const Cell &cell) { cell.printOn(out); return out; } void Cell::readFrom (istream &in) { string tag; while (1) { char ch; in >> ch; if (ch == '>') break; in >> tag; if (tag == "dimension") in >> dimension; else if (tag == "numberClasses") in >> numberClasses; else if (tag == "terminal") in >> terminal; else if (tag == "cellID") in >> cellID; else if (tag == "threshold") in >> threshold; else if (tag == "splitDimension") in >> splitDimension; else if (tag == "first") { Cell *tmp = new Cell; in >> *tmp; first = tmp; } else if (tag == "second") { Cell *tmp = new Cell; in >> *tmp; second = tmp; } else throw new ParsingException ("Cell::readFrom : unknown argument: " + tag); if (!in) throw new ParsingException ("Cell::readFrom : Parse error trying to build " + tag); in >> tag; if (tag != ">") throw new ParsingException ("Cell::readFrom : Parse error: '>' expected "); } } istream &operator >> (istream &in, Cell &cell) { if (!isValidType(in, "Cell")) return in; cell.readFrom(in); return in; } }//namespace FD