DecisionTreeReader Class Reference

#include <DecisionTreeReader.h>

List of all members.

Public Member Functions

 DecisionTreeReader ()
 ~DecisionTreeReader ()
bool CreateFromFile (string file)
double Evaluate (double *input, int size)
double Evaluate (vector< double > &input)
double EvaluateTree (int index, vector< double > &input)

Private Member Functions

bool ParseLine (ifstream &ins, DTNode *set)

Private Attributes

DTNode ** Tree
int nTree
vector< int > nNodes

Detailed Description

Definition at line 20 of file DecisionTreeReader.h.


Constructor & Destructor Documentation

DecisionTreeReader::DecisionTreeReader (  ) 

Definition at line 8 of file DecisionTreeReader.cxx.

References nNodes, nTree, and Tree.

00009 {
00010    Tree = 0;
00011    nTree = 0;
00012    nNodes.clear();
00013 }

DecisionTreeReader::~DecisionTreeReader (  ) 

Definition at line 15 of file DecisionTreeReader.cxx.

References nNodes, nTree, and Tree.

00016 {
00017    for(int i = 0; i < nTree; i++)
00018    {
00019         delete [] Tree[i];
00020    }
00021    delete [] Tree;
00022    nNodes.clear();
00023 
00024 }


Member Function Documentation

bool DecisionTreeReader::CreateFromFile ( string  file  ) 

Definition at line 26 of file DecisionTreeReader.cxx.

References nNodes, nTree, ParseLine(), and Tree.

00027 {
00028    ifstream ins(file.c_str());
00029    if(!ins.is_open()){
00030       cout<<"Unable to open file: "<<file<<endl;
00031       return false;
00032    }
00033 
00034    string dum1, dum2;
00035    int hold = 0;
00036    
00037    getline(ins, dum1);  //read and ignore first line
00038 //   cout<<dum1<<endl;
00039    ins>>dum2>>hold;     //get Number of Trees
00040 //   cout<<dum2<<hold<<endl;
00041    if(dum2 != "Classifiers:" || hold < 0){
00042      cout<<"Problem at: "<<dum2<<"  "<<hold<<endl;
00043      return false;
00044    }
00045    nTree = hold;
00046  
00047    Tree = new DTNode*[nTree]; 
00048    for(int i = 0; i < nTree; i++){  
00049        getline(ins, dum1);
00050        getline(ins, dum1);
00051        getline(ins, dum1);
00052 
00053        ins>>dum2>>hold>>dum2;  //get number of nodes
00054        if(dum2 != "nodes." || hold < 0){
00055           cout<<"Problem at (53): "<<dum2<<"  "<<hold<<endl;
00056           return false;
00057        }
00058        nNodes.push_back(hold);
00059 
00060        Tree[i] = new DTNode[hold];
00061         for(int j = 0; j < hold; j++){
00062             if(!ParseLine(ins, &Tree[i][j])) 
00063                return false;
00064             if(Tree[i][j].id != j) cout<<"Sheer problem"<<endl;
00065         }
00066    }
00067 
00068    return true;
00069 //   then a few more lines giving the variables;
00070 
00071 }

double DecisionTreeReader::Evaluate ( vector< double > &  input  ) 

Definition at line 116 of file DecisionTreeReader.cxx.

References EvaluateTree(), and nTree.

00117 {
00118 //  cout<<"Try again:  "<<nTree<<endl;
00119   double res = 0;
00120 
00121   for(int i = 0; i < nTree; i++){
00122 //     cout<<res<<endl;
00123      res += EvaluateTree(i, input);     
00124   }
00125   res /= nTree;
00126 
00127   return res;
00128 }

double DecisionTreeReader::Evaluate ( double *  input,
int  size 
)

Definition at line 106 of file DecisionTreeReader.cxx.

00107 {
00108   vector<double> out;
00109   for(int i = 0; i < size; i++){
00110      out.push_back(input[i]);
00111   }
00112 //  cout<<"here"<<endl;
00113   return Evaluate(out);
00114 }

double DecisionTreeReader::EvaluateTree ( int  index,
vector< double > &  input 
)

Definition at line 93 of file DecisionTreeReader.cxx.

References DTNode::cut, DTNode::left, DTNode::right, DTNode::score, Tree, and DTNode::var.

Referenced by Evaluate().

00094 {
00095   DTNode* pos = &Tree[index][0];
00096   while( pos->var >= 0 ) {
00097     assert( pos->var < int(input.size()) );
00098     if( input[pos->var] < pos->cut )
00099       pos = &Tree[index][pos->left];
00100     else
00101       pos = &Tree[index][pos->right];
00102   }
00103   return pos->score;
00104 }

bool DecisionTreeReader::ParseLine ( ifstream &  ins,
DTNode set 
) [private]

Definition at line 73 of file DecisionTreeReader.cxx.

References id.

Referenced by CreateFromFile().

00074 {
00075    string dum1, dum2, dum3, dum4;
00076    int id, left, right, var;
00077    float cut, score;
00078 
00079    ins>>dum1>>id>>dum2>>score>>dum3>>var>>dum3>>cut>>dum4>>left>>right;
00080    if(dum1 != "Id:" || dum2 != "Score:" || dum4 != "Daughters:")
00081       return false;
00082  
00083    set->score = score;
00084    set->id = id;
00085    set->var = var;
00086    set->cut = cut;
00087    set->left = left;
00088    set->right = right;
00089 
00090    return true;
00091 }


Member Data Documentation

Definition at line 36 of file DecisionTreeReader.h.

Referenced by CreateFromFile(), DecisionTreeReader(), and ~DecisionTreeReader().


The documentation for this class was generated from the following files:

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1