#include #include #include #include"models.h" /* * Model */ Model::Model(){ Data T1("Model"),T2("Experience"); m_data = T1; exp_data = T2; CostFunction* f; m_cost = f; } Model::Model(const Model& other): m_data(other.m_data),exp_data(other.exp_data),m_cost(other.m_cost){} Model::Model(Data T){ Data T1("Model"); m_data = T1; exp_data = T; CostFunction* f; m_cost = f; } //Model::Model(Data T1, Data T2, CostFunction* f): m_data(T1), exp_data(T2), m_cost(f){} void Model::setModelData(Data T1){ m_data = T1; } void Model::setExpData(Data T2){ exp_data = T2; } void Model::setCost(CostFunction* f){ m_cost = f; } /* * Linear Approximation model */ //Constructor & Destructor LinearApprox::LinearApprox(): param({0.,0.}){ Data T1("LinearApprox"),T2("ExperimentalData"); m_data = T1; exp_data = T2; m_cost = new Khi2(m_data.getData(),exp_data.getData()); } LinearApprox::LinearApprox(const LinearApprox& other): param(other.param){ m_data = other.m_data; exp_data = other.exp_data; m_cost = other.m_cost; } LinearApprox::LinearApprox(Data T): param({0.,0.}){ exp_data = T; std::vector> V1(exp_data.getCard(), std::vector (2,0.)); for(int i=0;i par): param(par){ exp_data = T; std::vector> V1(exp_data.getCard(), std::vector (2,0.)); for(int i=0;i> V1(exp_data.getData()); for(int i=0;i a){ param = a; std::vector> n_val = m_data.getData(); for(int i=0;i LinearApprox::getParam() const { return param; } std::vector LinearApprox::getNeighbor(double ampl) const { double d_o=0,d_s=0; d_o = ampl*(2.*rand()/(RAND_MAX+1.)-1.); d_s = ampl*(2.*rand()/(RAND_MAX+1.)-1.); std::vector n_param = {param[0]+d_o,param[1]+d_s}; return n_param; } double LinearApprox::getCost(){ delete m_cost; m_cost = new Khi2(exp_data.getData(),m_data.getData()); return m_cost->get(); } //Export model parameters void LinearApprox::exportModel() const { FILE * fich = fopen((exp_data.getName()+".linear_approx.data").c_str(), "w"); fprintf(fich, "y = %lf *x + %lf", param[1], param[0]); } void LinearApprox::displayModel() const { char buffer[32]; memset(buffer, 0, sizeof(buffer)); snprintf(buffer, sizeof(buffer), "%g", m_cost->get()); std::string cost(buffer); std::vector commandsForGnuplot = {"set title \""+m_data.getName()+"\"","set key bmargin","plot '"+exp_data.getName()+".temp' using 1:2 w p pt 1 lc -1 title 'Experimental Data'"," replot '"+exp_data.getName()+".temp' using 3:4 w l dt 2 lc 6 title 'Linear approximation with khi2="+cost+"'"}; FILE * temp = fopen((exp_data.getName()+".temp").c_str(), "w"); FILE * gnuplotPipe = popen ("gnuplot -persistent", "w"); for (int i=0; i < m_data.getCard(); i++){ fprintf(temp, "%lf %lf %lf %lf \n", exp_data.getData()[i][0], exp_data.getData()[i][1], m_data.getData()[i][0], m_data.getData()[i][1]); } for (std::string command : commandsForGnuplot){ fprintf(gnuplotPipe,"%s \n", command.c_str()); } } //Set and get parameters Data LinearApprox::getExpData() const { return exp_data; } void LinearApprox::setSlope(double a){ param[1] = a; std::vector> n_val = m_data.getData(); for(int i=0;i> n_val = m_data.getData(); for(int i=0;i a, double x){ double y=0,tmp=0; for(int i=0;i a(3,0.); param = a; } PolynomialApprox::PolynomialApprox(const PolynomialApprox& other):param(other.param){ m_data = other.m_data; exp_data = other.exp_data; m_cost = other.m_cost; } PolynomialApprox::PolynomialApprox(int n){ std::vector a(n+1,0.); param = a; Data T1("PolynomialApprox"),T2("ExperimentalData"); m_data = T1; exp_data = T2; m_cost = new Khi2(m_data.getData(),exp_data.getData()); } PolynomialApprox::PolynomialApprox(Data T, int n){ std::vector a(n+1,0.); param = a; exp_data = T; std::vector> V1(exp_data.getCard(), std::vector (2,0.)); for(int i=0;i a):param(a){ exp_data = T; std::vector> PolDat, ExpDat = exp_data.getData(); for(int i=0;i> V1(exp_data.getData()); for(int i=0;i a){ param = a; std::vector> n_val = m_data.getData(); for(int i=0;i PolynomialApprox::getParam() const { return param; } std::vector PolynomialApprox::getNeighbor(double ampl) const { double d_a=0; std::vector n_param; for(int i=0;iget(); } //Export model parameters void PolynomialApprox::exportModel() const { FILE * fich = fopen((exp_data.getName()+".polynomial_approx.data").c_str(), "w"); fprintf(fich, "Polynom of degree %li with parameters : \n",param.size()); for(int i=0;iget()); std::string cost(buffer); std::vector commandsForGnuplot = {"set title \""+m_data.getName()+"\"","set key bmargin","plot '"+exp_data.getName()+".temp' using 1:2 w p pt 1 lc -1 title 'Experimental Data'"," replot '"+exp_data.getName()+".temp' using 3:4 w l dt 2 lc 6 title 'Polynomial approximation with khi2="+cost+"'"}; FILE * temp = fopen((exp_data.getName()+".temp").c_str(), "w"); FILE * gnuplotPipe = popen ("gnuplot -persistent", "w"); for (int i=0; i < m_data.getCard(); i++){ fprintf(temp, "%lf %lf %lf %lf \n", exp_data.getData()[i][0], exp_data.getData()[i][1], m_data.getData()[i][0], m_data.getData()[i][1]); } for (std::string command : commandsForGnuplot){ fprintf(gnuplotPipe,"%s \n", command.c_str()); } } //Set and get parameters Data PolynomialApprox::getExpData() const { return exp_data; } /* * Traveling SalesPerson problem */ std::vector vdtovi(std::vector Vd){ std::vector Vi; for(int i=0;i vitovd(std::vector Vi){ std::vector Vd; for(int i=0;i o(exp_data.getCard(),0); for(int i=0;i o(exp_data.getCard(),0); std::vector> V1(exp_data.getCard(), std::vector (2,0.)); for(int i=0;i o){ exp_data = T; order = o; std::vector> V1(exp_data.getCard(), std::vector (2,0.)); for(int i=0;i o(exp_data.getCard(), 0); for(int i=0;i> T2(m_data.getData()); for(int i=0;i a){ order = vdtovi(a); std::vector> n_val(m_data.getData()); for(int i=0;i TSP::getParam() const { return vitovd(order); } /* std::vector TSP::getNeighbor(double ampl) const { //This method take 2 random cities and swap them std::vector n_order_i(order); for(int i=0;i n_order_d(vitovd(n_order_i)); return n_order_d; } */ /* std::vector TSP::getNeighbor(double ampl) const { //This method take one random city and put it at the end of the path std::vector n_order_i(order); for(int i=0;i n_order_ii(n_order_i); for(int j=ind;j<(order.size()-1);j++) n_order_i[j] = n_order_ii[j+1]; n_order_i[order.size()-1] = n_order_ii[ind]; } } std::vector n_order_d(vitovd(n_order_i)); return n_order_d; } */ std::vector TSP::getNeighbor(double ampl) const { //This method take a subset of the path and shuffle it std::vector n_order_i(order); std::vector tmp; int set_length = floor(10.*ampl+1.); int ind_set = 0, ind_new = 0; ind_set = rand()%(order.size()-set_length+1); for(int i=0;i n_order_d(vitovd(n_order_i)); return n_order_d; } double TSP::getCost(){ delete m_cost; m_cost = new Distance(exp_data.getData(),order); return m_cost->get(); } //Export model parameters void TSP::exportModel() const { FILE * fich = fopen((exp_data.getName()+".TSP.data").c_str(), "w"); fprintf(fich, "The Salesperson should go through thoses %li cities in this order to get distance %lf : \n",order.size(),m_cost->get()); for(int i=0;iget()); std::string cost(buffer); std::vector commandsForGnuplot = {"set title \""+m_data.getName()+"\"","set key bmargin","plot '"+exp_data.getName()+".temp' using 1:2 w p pt 1 lc -1 title 'Experimental Data'"," replot '"+exp_data.getName()+".temp' using 3:4 w l dt 2 lc 6 title 'Short path estimation with distance="+cost+"'"}; FILE * temp = fopen((exp_data.getName()+".temp").c_str(), "w"); FILE * gnuplotPipe = popen ("gnuplot -persistent", "w"); for (int i=0; i < m_data.getCard(); i++){ fprintf(temp, "%lf %lf %lf %lf \n", exp_data.getData()[i][0], exp_data.getData()[i][1], m_data.getData()[i][0], m_data.getData()[i][1]); } for (std::string command : commandsForGnuplot){ fprintf(gnuplotPipe,"%s \n", command.c_str()); } } //Set and get parameters Data TSP::getExpData() const { return exp_data; } void TSP::setOrder(std::vector o){ order = o; std::vector> n_val(m_data.getData()); for(int i=0;i TSP::getOrder() const { return order; }