Hybrid Query Processing Engine for Coprocessing in Database Systems
HyPE
least_squares.cpp
Go to the documentation of this file.
00001 
00002 #include <plugins/statistical_methods/least_squares.hpp>
00003 #include <core/algorithm.hpp>
00004 
00005 #include <iostream>
00006 
00007 using namespace std;
00008 
00009 namespace hype{
00010    namespace core{
00011 
00012    Least_Squares_Method_1D::Least_Squares_Method_1D() : StatisticalMethod_Internal("Least Squares 1D"), timeestimationpolynomial_(), degree_of_polynomial_(2),polynomial_computed_(false) {
00013 
00014    }
00015 
00016    Least_Squares_Method_1D::~Least_Squares_Method_1D(){}
00017 
00018    const EstimatedTime Least_Squares_Method_1D::computeEstimation(const Tuple& input_values){
00019       if(input_values.size()!=1){
00020          std::cout << "FATAL ERROR: Least_Squares_Method_1D cannot handle input dimensions other than 1" << std::endl;
00021          exit(-1);
00022       } 
00023       //negative estimation is invalid value, allowing to distinguish between a regular estimation and a dummy value, which is returned if Algorithm is in trainingphase
00024       double returnval=-1; 
00025       if(polynomial_computed_){
00026          returnval = alglib::barycentriccalc(timeestimationpolynomial_,input_values[0]);
00027       }
00028       //cout << "Esitmated Time: " << returnval << endl;
00029       return EstimatedTime(returnval);
00030    }
00031 
00032    bool Least_Squares_Method_1D::inTrainingPhase() const throw(){
00033       return !polynomial_computed_; //if polynomial is not computed, we are in the initial trainingphase
00034    }
00035 
00036    void Least_Squares_Method_1D::retrain(){
00037       polynomial_computed_=false;
00038    }
00039    
00040    bool Least_Squares_Method_1D::recomuteApproximationFunction(Algorithm& algorithm){
00041    
00042       polynomial_computed_=true; //initial trainingphase is finished
00043 
00044       alglib::real_1d_array x; //=algorithm.;
00045       alglib::real_1d_array y;
00046 
00047       
00048       if(!quiet) cout << "recomputing Approximation function" << endl;
00049 
00050       std::vector<MeasuredTime> measured_times = algorithm.getAlgorithmStatistics().executionHistory_.getColumnMeasurements();
00051       int number_of_measurement_pairs = measured_times.size(); //= algorithm.getAlgorithmStatistics().executionHistory_.measured_times_.size();
00052       std::vector<Tuple> tuples = algorithm.getAlgorithmStatistics().executionHistory_.getColumnFeatureValues();
00053       int number_of_feature_values = tuples.size(); 
00054       
00055       assert(number_of_feature_values==number_of_measurement_pairs); //number of measured execution times = number of processed data sets
00056       
00057       std::vector<double> times;
00058       for(unsigned int i=0;i<measured_times.size();i++)
00059          times.push_back(measured_times[i].getTimeinNanoseconds());
00060 
00061       y.setcontent(number_of_measurement_pairs, &times[0] );
00062 
00063       vector<double> datasizes;
00064 
00065       for(unsigned int i=0;i<tuples.size();i++){
00066          datasizes.push_back(tuples[i][0]);
00067          //cout << tuples[i][0] << "  counter: " << i << endl;
00068       }
00069       //assgin feature values to array x
00070       x.setcontent(datasizes.size(), &datasizes[0] );
00071 
00072       alglib::ae_int_t m = degree_of_polynomial_;
00073       alglib::ae_int_t info;
00074       alglib::barycentricinterpolant p;
00075       alglib::polynomialfitreport rep;
00076       //double v;
00077 
00078    //cout << "Length Array x: " << x.length() << "   Length Array y: " << y.length() << endl;
00079       assert(x.length()==y.length());
00080 
00081     //
00082     // Fitting without individual weights
00083     //
00084     // NOTE: result is returned as barycentricinterpolant structure.
00085     //       if you want to get representation in the power basis,
00086     //       you can use barycentricbar2pow() function to convert
00087     //       from barycentric to power representation (see docs for 
00088     //       POLINT subpackage for more info).
00089     //
00090     try{
00091     alglib::polynomialfit(x, y, m, info, p, rep);
00092 
00093    }catch(alglib::ap_error e){
00094       cout << "FATAL ERROR! Least_Squares_Method_1D::recomuteApproximationFunction() " << endl
00095            << "Failed to compute polynomial!" << endl
00096            << "File: " << __FILE__ << " Line: " << __LINE__ << endl;
00097       //cout <<  << endl;
00098       exit(-1);
00099       return false;
00100    }
00101 
00102       timeestimationpolynomial_=p; //update field for estimationpolynomial
00103 
00104       return true;
00105    }
00106 
00107 // static Least_Squares_Method_1D* Least_Squares_Method_1D::create(){
00108 //    return new Least_Squares_Method_1D();
00109 // }
00110 
00111    }; //end namespace core
00112 }; //end namespace hype
00113 
00114 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines