long fit_panel::on_cmd_run_fit(FXObject*, FXSelector, void* ptr) { struct fit_parameters* fps = fit_parameters_new(); for(unsigned j = 0; j < m_parameters.size(); j++) { if(m_parameters[j].selected) { fit_parameters_add(fps, &m_parameters[j].fp); } } if(fps->number > 0) { run_fit(fps); for(unsigned k = 0; k < m_parameters.size(); k++) { param_info& p = m_parameters[k]; FXString s = FXStringFormat("%g", m_fit->get_parameter_value(k)); p.text_field->setText(s); p.is_dirty = true; } m_canvas->update_limits(); } fit_parameters_free(fps); if (m_results_target) { m_results_target->notify_change(); } return 1; }
// collect and avg sample to be passed onto LSQ estimator after all requisite orientations are done void AccelCalibrator::new_sample(const Vector3f& delta_velocity, float dt) { if (_status != ACCEL_CAL_COLLECTING_SAMPLE) { return; } if (_samples_collected >= _conf_num_samples) { set_status(ACCEL_CAL_FAILED); return; } _sample_buffer[_samples_collected].delta_velocity += delta_velocity; _sample_buffer[_samples_collected].delta_time += dt; _last_samp_frag_collected_ms = AP_HAL::millis(); if (_sample_buffer[_samples_collected].delta_time > _conf_sample_time) { Vector3f sample = _sample_buffer[_samples_collected].delta_velocity/_sample_buffer[_samples_collected].delta_time; if (!accept_sample(sample)) { set_status(ACCEL_CAL_FAILED); return; } _samples_collected++; if (_samples_collected >= _conf_num_samples) { run_fit(MAX_ITERATIONS, _fitness); if (_fitness < _conf_tolerance && accept_result()) { set_status(ACCEL_CAL_SUCCESS); } else { set_status(ACCEL_CAL_FAILED); } } else { set_status(ACCEL_CAL_WAITING_FOR_ORIENTATION); } } }
int main(int argc,char *argv[]) { int c; bool writeOutput = false; std::string outFileName = ""; std::vector<std::string> years; std::string inYears = ""; int numIterations = 1; int outYear = 0; bool useSeededValues = false; std::string seededLocation = ""; bool zeroSRS = false; /*____________________________Parse Command Line___________________________*/ while ((c = getopt(argc, argv, "y:Y:o:i:S:zh")) != -1) { switch (c) { case 'y': inYears.assign(optarg); boost::split(years, inYears, boost::is_any_of(",")); break; case 'Y': outYear = atoi(optarg); break; case 'o': outFileName.assign(optarg); writeOutput = true; break; case 'i': numIterations = atoi(optarg); break; case 'S': useSeededValues = true; seededLocation.assign(optarg); break; case 'z': zeroSRS = true; break; case 'h': printOptions(); return 0; default: // not an option break; } } //ensure years and outYear are set if (years.empty()) { std::cout << "You must set the input years using the -y switch and a comma-separated list of years" << std::endl; printOptions(); return 0; } if (outYear == 0) { std::cout << "You must set the output year using the -Y switch" << std::endl; printOptions(); return 0; } char *homePath, path[256]; homePath = getenv("HOME"); //read in the necessary constants sprintf(path, "%s/cpp/NCAA_C/constants/waverage_standard_deviations.d", homePath); ConstantStandardDeviations *stdDevs = ConstantStandardDeviations::Instance(); stdDevs->initialize(path); sprintf(path, "%s/cpp/NCAA_C/constants/season_info.d", homePath); ConstantSeasonInfo *seasonInfo = ConstantSeasonInfo::Instance(); seasonInfo->initialize(path); sprintf(path, "%s/cpp/NCAA_C/constants/waverage_functions.d", homePath); ConstantWAverageFunctions *functions = ConstantWAverageFunctions::Instance(); functions->initialize(path); sprintf(path, "%s/cpp/NCAA_C/constants/team_neutral_ratios.d", homePath); ConstantTeamNeutralRatios *ratios = ConstantTeamNeutralRatios::Instance(); ratios->initialize(path); sprintf(path, "%s/cpp/NCAA_C/constants/srs_additions.d", homePath); ConstantSRSadditions *additions = ConstantSRSadditions::Instance(); additions->initialize(path); //read in the game and waverage info for (std::string &year : years) { sprintf(path, "%s/cpp/NCAA_C/teams/%s/", homePath, year.c_str()); std::cout << "Reading in games and waverages for " << year << std::endl; readTeamsFromDir(path); readTeamsFromDir(path, "waverages"); } std::unordered_map<std::string, Team*> teams = Team::getTeams(); std::unordered_map<std::string, TeamGame*> games; Team *opp; TeamWAverage *wa1, *wa2; int totalGames = 0; for (auto &team : teams) { games = team.second->getGamesByDate(); for (auto &game : games) { if (team.first < game.second->getOpp()) continue; //look at each game only once opp = Team::findTeam(game.second->getOpp()); if (!opp) continue; if (game.second->getDate() >= seasonInfo->get(team.second->getYear(), "tournament start")) continue; if (game.second->getDate().month().as_number() == 11) continue; totalGames++; wa1 = team.second->WAverageOnDate(game.second->getDate()); wa2 = opp->WAverageOnDate(game.second->getDate()); std::string loc = game.second->getLoc(); std::string oppLoc = game.second->getOppLoc(); std::unordered_map<std::string, double> predictions1 = functions->predictStats(wa1, wa2, outYear); std::unordered_map<std::string, double> predictions2 = functions->predictStats(wa2, wa1, outYear); win.push_back(game.second->getWin()); oor.push_back((predictions1["oor.p"] * wa1->getValue("oor.p") / ratios->get(outYear,loc,"oor.p") - predictions2["oor.p"] * wa2->getValue("oor.p") / ratios->get(outYear,oppLoc,"oor.p")) / stdDevs->get(outYear,"oor.p")); oefg.push_back((predictions1["oefg.p"] * wa1->getValue("oefg.p") / ratios->get(outYear,loc,"oefg.p") - predictions2["oefg.p"] * wa2->getValue("oefg.p") / ratios->get(outYear,oppLoc,"oefg.p")) / stdDevs->get(outYear,"oefg.p")); oftmr.push_back((predictions1["oftmr.p"] * wa1->getValue("oftmr.p") / ratios->get(outYear,loc,"oftmr.p") - predictions2["oftmr.p"] * wa2->getValue("oftmr.p") / ratios->get(outYear,oppLoc,"oftmr.p")) / stdDevs->get(outYear,"oftmr.p")); //This guy is reversed because turnovers are bad. oto.push_back((-predictions1["oto.p"] * wa1->getValue("oto.p") / ratios->get(outYear,loc,"oto.p") + predictions2["oto.p"] * wa2->getValue("oto.p") / ratios->get(outYear,oppLoc,"oto.p")) / stdDevs->get(outYear,"oto.p")); srs.push_back((wa1->getSrs() - wa2->getSrs()) / stdDevs->get(outYear,"srs")); //removed additions } } std::cout << "Total number of games to analyze: " << totalGames << std::endl; std::vector<double> temp_ary, temp_ary2; std::vector<double> fcn_mins; std::vector< std::vector<double>*> params_ary; if (!useSeededValues) { //random number generator, using Mersenne Twister method //the 0 means we use a unique seed each time TRandom3 rand(0); for (int i = 0; i < numIterations; i++) { double randoms[5]; rand.RndmArray(5, randoms); if (zeroSRS) temp_ary = run_fit(randoms[0], randoms[1], randoms[2], randoms[3], 0); else temp_ary = run_fit(randoms[0], randoms[1], randoms[2], randoms[3], randoms[4]); double temp_fcn_min = temp_ary.back(); double temp_fcn_min2; double temp_norm = 0; int num_times_through = 0; for (int k = 0; k < 100; k++) { num_times_through++; temp_ary2 = run_fit(temp_ary[0], temp_ary[1], temp_ary[2], temp_ary[3], temp_ary[4]); temp_fcn_min2 = temp_ary2.back(); if (temp_fcn_min2 == temp_fcn_min && temp_ary == temp_ary2) break; temp_ary = temp_ary2; temp_fcn_min = temp_fcn_min2; } fcn_mins.push_back(temp_fcn_min); params_ary.push_back(new std::vector<double>()); for (int ii = 0; ii < temp_ary.size(); ii++) params_ary.back()->push_back(temp_ary[ii]); int indexOfMin = indexOfVectorMin(fcn_mins); std::cout << " " << i << "\t" << doubleFormatter(fcn_mins.back(), 4) << "\t" << doubleFormatter(fcn_mins[indexOfMin], 4) << "\t" << indexOfMin << "\r"; fflush(stdout); // std::cout << " \t\t" << i << "\t" << doubleFormatter(fcn_mins.back(), 4) << "\t" << // doubleFormatter(fcn_mins[indexOfMin], 4) << "\t" << indexOfMin << std::endl; // temp_norm = 0; // for (int ii = 0; ii < 5; ii++) // temp_norm += fabs(temp_ary[ii]); // for (int ii = 0; ii < 5; ii++){ // std::cout << " " << ii << "\t" << doubleFormatter(temp_ary[ii],4) << std::endl; // } } } else{ sprintf(path, "%s/cpp/NCAA_C/%s", homePath, seededLocation.c_str()); ConstantGameFunction *gameFunction = ConstantGameFunction::Instance(); gameFunction->initialize(path); std::vector<int> keys = gameFunction->getKeys(); for (int &year : keys){ std::vector<double> initialValuesVec = gameFunction->getWeights(year); temp_ary = run_fit(initialValuesVec[0],initialValuesVec[1],initialValuesVec[2], initialValuesVec[3],initialValuesVec[4]); double temp_fcn_min = temp_ary.back(); double temp_fcn_min2; double temp_norm = 0; int num_times_through = 0; for (int k = 0; k < 100; k++) { num_times_through++; temp_ary2 = run_fit(temp_ary[0], temp_ary[1], temp_ary[2], temp_ary[3], temp_ary[4]); temp_fcn_min2 = temp_ary2.back(); if (temp_fcn_min2 == temp_fcn_min && temp_ary == temp_ary2) break; temp_ary = temp_ary2; temp_fcn_min = temp_fcn_min2; } fcn_mins.push_back(temp_fcn_min); params_ary.push_back(new std::vector<double>()); for (int ii = 0; ii < temp_ary.size(); ii++) params_ary.back()->push_back(temp_ary[ii]); int indexOfMin = indexOfVectorMin(fcn_mins); std::cout << " " << year << "\t" << doubleFormatter(fcn_mins.back(), 4) << "\t" << doubleFormatter(fcn_mins[indexOfMin], 4) << "\t" << indexOfMin << std::endl; } } std::cout << "\n\n\nFinal Results"; int indexOfMin = indexOfVectorMin(fcn_mins); double norm = 0; for (int i = 0; i < 5; i++) norm += fabs(params_ary[indexOfMin]->at(i)); std::cout << "Minimum = " << doubleFormatter(fcn_mins[indexOfMin],4) << std::endl; for (int i = 0; i < 5; i++) std::cout << i << "\t" << doubleFormatter(params_ary[indexOfMin]->at(i) / norm, 4) << std::endl; if (writeOutput) { sprintf(path, "%s/cpp/NCAA_C/constants/%s", homePath, outFileName.c_str()); std::ofstream outFile(path, std::ios::app); outFile << outYear; for (int i = 0; i < 5; i++) outFile << "," << doubleFormatter(params_ary[indexOfMin]->at(i) / norm, 4); outFile << "," << doubleFormatter(params_ary[indexOfMin]->at(5), 4); outFile << std::endl; outFile.close(); } return 0; }
int main(int argc,char *argv[]) { int c; std::vector<std::string> years; std::string inYears = ""; int numIterations = 1; std::string outFileName = ""; bool writeOutput = false; int outYear = 0; /*____________________________Parse Command Line___________________________*/ while ((c = getopt(argc, argv, "y:Y:i:o:h")) != -1) { switch (c) { case 'y': inYears.assign(optarg); boost::split(years, inYears, boost::is_any_of(",")); break; case 'Y': outYear = atoi(optarg); break; case 'i': numIterations = atoi(optarg); break; case 'o': outFileName.assign(optarg); writeOutput = true; break; case 'h': printOptions(); return 0; default: // not an option break; } } //ensure years and outYear are set if (years.empty()) { std::cout << "You must set the input years using the -y switch and a comma-separated list of years" << std::endl; printOptions(); return 0; } if (outYear == 0) { std::cout << "You must set the output year using the -Y switch" << std::endl; printOptions(); return 0; } char *homePath, path[256]; homePath = getenv("HOME"); //read in the necessary constants sprintf(path, "%s/cpp/NCAA_C/constants/season_info.d", homePath); ConstantSeasonInfo *seasonInfo = ConstantSeasonInfo::Instance(); seasonInfo->initialize(path); //read in the game and waverage info for (std::string &year : years) { sprintf(path, "%s/cpp/NCAA_C/teams/%s/", homePath, year.c_str()); std::cout << "Reading in games and waverages for " << year << std::endl; readTeamsFromDir(path); readTeamsFromDir(path, "waverages"); } std::unordered_map<std::string, Team*> teams = Team::getTeams(); std::unordered_map<std::string, TeamGame*> games; Team *opp; TeamWAverage *wa1, *wa2; int totalGames = 0; std::cout << "Processing Games" << std::endl; for (auto &team : teams) { games = team.second->getGamesByDate(); for (auto &game : games) { if (team.first < game.second->getOpp()) continue; //look at each game only once opp = Team::findTeam(game.second->getOpp()); if (!opp) continue; if (game.second->getDate() >= seasonInfo->get(team.second->getYear(), "tournament start")) continue; if (game.second->getDate().month().as_number() == 11) continue; totalGames++; wa1 = team.second->WAverageOnDate(game.second->getDate()); wa2 = opp->WAverageOnDate(game.second->getDate()); int loc = 0; if (game.second->getLoc() == "home") loc = 1; else if (game.second->getLoc() == "away") loc = -1; win.push_back(game.second->getWin()); location.push_back(loc); srs.push_back((wa1->getSrs() - wa2->getSrs())); } } std::cout << "Total number of games to analyze: " << totalGames << std::endl; std::vector<double> temp_ary, temp_ary2; std::vector<double> fcn_min; std::vector< std::vector<double>*> params_ary; //random number generator, using Mersenne Twister method //the 0 means we use a unique seed each time TRandom3 rand(0); for (int i = 0; i < numIterations; i++){ temp_ary = run_fit(rand.Rndm()*40 - 20);//random number between -20 and 20 double temp_fcn_min = temp_ary.back(); double temp_fcn_min2; int num_times_through = 0; for (int k = 0; k < 100; k++){ num_times_through++; temp_ary2 = run_fit(temp_ary[0]); temp_fcn_min2 = temp_ary2.back(); if (temp_fcn_min2 == temp_fcn_min && temp_ary == temp_ary2) break; temp_ary = temp_ary2; temp_fcn_min = temp_fcn_min2; } fcn_min.push_back(temp_fcn_min); params_ary.push_back(new std::vector<double>()); for (int ii = 0; ii < temp_ary.size(); ii++) params_ary.back()->push_back(temp_ary[ii]); int indexOfMin = indexOfVectorMin(fcn_min); std::cout << " " << i << "\t" << doubleFormatter(fcn_min.back(), 4) << "\t" << doubleFormatter(fcn_min[indexOfMin], 4) << "\t" << indexOfMin << "\r"; fflush(stdout); } int indexOfMin = indexOfVectorMin(fcn_min); std::cout << indexOfMin << "\t" << fcn_min[indexOfMin] << "\t" << params_ary[indexOfMin]->at(0) << std::endl; if (writeOutput) { sprintf(path, "%s/cpp/NCAA_C/constants/%s", homePath, outFileName.c_str()); std::ofstream outFile(path, std::ios::app); outFile << outYear << "," << doubleFormatter(params_ary[indexOfMin]->at(0), 2) << "," << doubleFormatter(params_ary[indexOfMin]->at(1), 4) << std::endl; outFile.close(); } return 0; }