double Handler::optimise_single(vector<double> ¶meters, vector<vector<double> > &results, vector<vector<vector<double> > > &allResults, int& itr){ double SSE = 99999999999999.9; double tempSSE; vector<double> tempPar, seedParams; Simplex simplex; parameters.clear(); allResults.clear(); parameters = generate_seed_parameters(); for(int index = 0;index<20;index++){ seedParams.clear(); seedParams = generate_seed_parameters(); //printcon(seedParams); if(this->useMLE == false) tempPar = simplex.neldermead(&Handler::fitEpidemicsMLE, *this, seedParams, itr); else tempPar = simplex.neldermead(&Handler::fitEpidemics, *this, seedParams, itr); // Store the SSE value for this tempSSE = fitEpidemics(tempPar); if(tempSSE < SSE){ SSE = tempSSE; parameters=tempPar; } cout << "." << flush; } results = ode_solve(parameters); allResults = ode_solve_separate(parameters); //allResults.clear(); cout << endl; return SSE; }
/* Optimisation procedure. Takes a reference to parameters, combined results, component results and number of iterations taken. The function updates these arguments with the results of the Nelder-Mead optimisation procedure. */ double Handler::optimiseEpidemics(vector<double> ¶meters, vector<vector<double> > &results, vector<vector<vector<double> > > &allResults, int& itr){ double SSE = 99999999999999999.9; double tempSSE; vector<double> tempPar, seedParams; Simplex simplex; int iterations = 10001; // If no epidemics have yet been detected, use the mean of the current data as the // current model and return the corresponding SSE. if(epidemics.size() == 0){ //baseModel = base_model(temp_data); results = base_model(temp_data); SSE = calculate_SSE(results, temp_data); parameters.clear(); allResults.clear(); allResults.push_back(baseModel); return(SSE); } srand(time(NULL)); parameters = generate_seed_parameters(); // If there are epidemics to be fitted, perform X random fits and keep best fitting model for(int index=0;index<10;index++){ // Create random seed parameters seedParams.clear(); seedParams = generate_seed_parameters(); while(fitEpidemics(seedParams) != fitEpidemics(seedParams)){ seedParams.clear(); seedParams = generate_seed_parameters(); } // Get the optimised parameters from nelder mead algorithm tempPar = simplex.neldermead(&Handler::fitEpidemics, *this, seedParams, iterations); // Store the SSE value for this tempSSE = fitEpidemics(tempPar); //if(tempSSE == tempSSE) cout << "Temp RSquare: " << (1-tempSSE/(SStot(temp_data,1))) << endl; // If this SSE value is better than the previous, store it and the corresponding parameters if(tempSSE < SSE){ itr = iterations; SSE = tempSSE; parameters=tempPar; } cout << "." << flush; } // Get the combined values from these parameters, as well as a vector of // each sub-epidemic results = ode_solve(parameters); allResults.clear(); allResults = ode_solve_separate(parameters); cout << endl; return(SSE); }
double Handler::optimiseEpidemics(vector<double> ¶meters, vector<vector<double> > &results, vector<vector<vector<double> > > &allResults, int& itr){ double SSE = 99999999999.9; double tempSSE; vector<double> tempPar, seedParams; vector<vector<vector<double> > > tempAll; Simplex simplex; int iterations = 10001; // If no epidemics have yet been detected, use the mean of the current data as the // current model and return the corresponding SSE. if(epidemics.size() == 0){ results = this->baseModel; SSE = calculate_SSE(results, temp_data); parameters.clear(); allResults.clear(); allResults.push_back(results); return(SSE); } parameters = generate_seed_parameters(); // If there are epidemics to be fitted, perform 40 random fits and keep best fitting model for(int index=0;index<40;index++){ // Clear the temporary seed parameters and seed rand // Create a list of random seed parameters seedParams.clear(); seedParams = generate_seed_parameters(); // Get the optimised parameters from nelder mead algorithm if(this->useMLE == false) tempPar = simplex.neldermead(&Handler::fitEpidemicsMLE, *this, seedParams, iterations); else tempPar = simplex.neldermead(&Handler::fitEpidemics, *this, seedParams, iterations); // Store the SSE value for this tempSSE = fitEpidemics(tempPar); // If this SSE value is better than the previous, store it and the // corresponding parameters if(tempSSE < SSE){ itr = iterations; SSE = tempSSE; parameters=tempPar; } cout << "." << flush; } // Get the combined values from these parameters, as well as a vector of // each sub-epidemic results = ode_solve(parameters); tempAll = ode_solve_separate(parameters); allResults.clear(); allResults.push_back(this->baseModel); for(unsigned int x = 0;x<tempAll.size();++x){ allResults.push_back(tempAll[x]); } cout << endl; return(SSE); }