void testhmm(string race, int index) { cout << "Testing with replay " << index << endl; Hmm hmm; hmm.loadFromRace(race); vector<unsigned long>* replay = loadReplayData(race, index); cout << "Replay length: " << replay->size() << endl << "Rep:"; for (vector<unsigned long>::iterator it = replay->begin(); it != replay->end(); it++) { cout.width(3); cout << (*it); } for (unsigned int i = 0; i < replay->size(); i++) { unsigned int missed = 0, j; cout << endl << "Pre:"; for (j = 0; j < i; j++) { cout.width(3); cout << replay->at(j); hmm.observe(replay->at(j)); } vector<unsigned long> *seq = hmm.predictMaxSeq(replay->size() - i); for (unsigned int k = j; k < replay->size(); k++) { cout.width(3); unsigned long prediction = seq->at(k - j); if (prediction != replay->at(k)){ missed++; cout << red << prediction; } else { cout << green << prediction; } } cout << white << " (" << (1.0 - missed / (double)(replay->size() - i)) << ")" << endl; hmm.reset(); delete seq; } }
void testBuildingStats() { Hmm hmm; hmm.loadFromRace("P"); BuildingStats stats; stats.readStatsFile("Z/stats.csv"); stats.readOurStatsFile("P/stats.csv"); stats.readRepliesFile("Z/replies.csv"); int rep_state = stats.getReplyState(11); set<string> search; search.insert("Reaver"); search.insert("Observer"); int state = stats.getClosestState(search); int state2 = stats.getClosestState2(search); cout << state << " " << state2 << endl; set<string>* unitTypes = stats.decodeState(state); for (set<string>::iterator it = unitTypes->begin(); it != unitTypes->end(); it++) { cout << *it << ","; } cout << endl << endl;; set<string>* unitTypes2 = stats.decodeState(state2); for (set<string>::iterator it = unitTypes2->begin(); it != unitTypes2->end(); it++) { cout << *it << ","; } cout << endl; }
void testdata(string race, int numLines, int *trials, int *correct, int advance) { Hmm hmm; hmm.loadFromRace(race); string filename = race + "/data.csv"; ifstream infile(filename.c_str()); string line; for (int z = 0; z < numLines; z++) { vector<unsigned long>* replay = loadReplayData(race, z); for (unsigned int i = 0; i < replay->size(); i++) { unsigned int missed = 0, j; for (j = 0; j < i; j++) { hmm.observe(replay->at(j)); } vector<unsigned long> *seq = hmm.predictMaxSeq(advance); for (unsigned int k = j - 1 + advance; k < replay->size() && k < j + advance; k++) { unsigned long prediction = seq->at(k - j); if (prediction == replay->at(k)){ correct[k]++; } trials[k]++; } hmm.reset(); delete seq; } cerr << "Ran " << z << " from " << numLines << endl; } }
// construit les matrices emit et trans à l'aide de l'algorithme Baum-Welch void learn(const std::string& name, unsigned int maxIterations) { Hmm hmm; std::string output = (boost::format("seq-%1%-result") % name).str(); ifstream istrm((boost::format("seq-%1%.input") % name).str().c_str()); std:: vector < std::vector < unsigned long >* > trainingSequences; hmm.loadProbs((boost::format("seq-%1%-init") % name).str().c_str()); hmm.readSeqs(istrm, trainingSequences); hmm.baumWelch(trainingSequences, maxIterations); hmm.saveProbs(output.c_str()); }
// génére les dates des événements de sortie en fonction du HMM sur // une période [0, max] // le paramètre events est la liste des dates d'arrivée des événements d'entrée Dates generate_dates(double max, const Dates& events, Hmm& hmm) { Dates dates; double t = 0; unsigned long state = hmm.getInitState(); Dates::const_iterator ite = events.begin(); while (t < max) { unsigned long obs; if (hmm.genSeq(state, obs)) { std::string obsStr = hmm.getStr(obs); int type; std::vector < std::string > obsV; boost::split(obsV, obsStr, boost::is_any_of("|")); type = atoi(obsV[1].c_str()); if (type != IN) { if (type != END) { // ce n'est pas un état infini if (type != INF) { double value = atof(obsV[0].c_str()); if (dates.empty()) { dates.push_back(value); } else { dates.push_back(dates.back() + value); } t = dates.back(); // détermine la prochaine date d'arrivée d'un // événement d'entrée while (ite != events.end() and t >= *ite) { ++ite; } } else { // c'est un état infini if (ite != events.end()) { t = *ite; ++ite; } } } else { state = hmm.getInitState(); } } else { state = hmm.getInitState(); } } } return dates; }
int main(int argc, char* argv[]) { Hmm hmm; if (argc<3) { cerr << "USAGE: trainhmm INIT-HMM RESULT-HMM DATA [MAX-ITERATIONS]" << endl; exit(1); } hmm.loadProbs(argv[1]); const char* output = argv[2]; ifstream istrm(argv[3]); int maxIterations = 10; if (argc>4) maxIterations = atoi(argv[4]); vector<vector<unsigned long>*> trainingSequences; hmm.readSeqs(istrm, trainingSequences); hmm.baumWelch(trainingSequences, maxIterations); hmm.saveProbs(output); }
void trainhmm(string race, int numStates, int maxIterations) { Hmm hmm; hmm.makeEmitAndTransFiles(race, numStates); hmm.loadFromRace(race); ifstream istrm((race + "/data.csv").c_str()); vector<vector<unsigned long>*> trainingSequences; hmm.readSeqs(istrm, trainingSequences); hmm.baumWelch(trainingSequences, maxIterations); hmm.saveProbs(race + "/hmm"); }
virtual std::string say_hmm() { return hmm_.say(); }