void LibSpotifyPlaybackHandler::shuffle() { random_shuffle( playQueue_.begin(), playQueue_.end() ); }
std::vector<std::vector<float>> ioStream::lireMnist(int nombre) { std::vector<float> labelsTest; std::vector<float> imagesTest; std::vector<float> labelsLearning; std::vector<float> imagesLearning; labelsLearning.reserve(50000); labelsTest.reserve(10000); imagesLearning.reserve(50000 *784); imagesTest.reserve(10000 *784); std::ifstream imagesLearningFile; std::ifstream imagesTestFile; std::ifstream labelsLearningFile; std::ifstream labelsTestFile; imagesLearningFile.open("donnees/train-images-idx3-ubyte", std::ios::in | std::ios::binary); labelsLearningFile.open("donnees/train-labels-idx1-ubyte", std::ios::in | std::ios::binary); labelsTestFile.open("donnees/t10k-labels-idx1-ubyte", std::ios::in | std::ios::binary); imagesTestFile.open("donnees/t10k-images-idx3-ubyte", std::ios::in | std::ios::binary); float c; int i; if(imagesTestFile.is_open() && labelsTestFile.is_open() && imagesLearningFile.is_open() && labelsLearningFile.is_open()) { i = 0; while(!labelsTestFile.eof()) { c = labelsTestFile.get(); if(c >= 0 && c <= 9 && i > 5) labelsTest.push_back(c); i++; } i = 0; while(!imagesTestFile.eof()) { c = imagesTestFile.get(); if(c >= 0 && c <= 255 && i > 15) imagesTest.push_back( (c/255)); i++; } i=0; while(!labelsLearningFile.eof()) { c = labelsLearningFile.get(); if(c >= 0 && c <= 9 && i > 5) labelsLearning.push_back(c); i++; } i = 0; while(!imagesLearningFile.eof()) { c = imagesLearningFile.get(); if(c >= 0 && c <= 255 && i > 15) imagesLearning.push_back((c/255 )); i++; } imagesTestFile.close(); labelsTestFile.close(); imagesLearningFile.close(); labelsLearningFile.close(); std::cout << "STEP 01 : Data read" << std::endl; } else std::cout << "STEP 01 FAILED" << std::endl; std::vector<std::vector<float>> exemples; exemples.reserve(nombre); int nombreApprentissage=0; int nombreTest=0; if(nombre>labelsTest.size() + labelsLearning.size()) nombre = labelsTest.size() + labelsLearning.size(); nombreApprentissage = (6*nombre)/7; nombreTest = nombre - nombreApprentissage; //nombreTest=0; /* for(int j=0; j<nombreApprentissage; j++) { int index=-1; while(index==-1) index = (rand()*labelsLearning.size()) / RAND_MAX -1; std::vector<float> vect; //vect.insert(vect.end(), imagesLearning.begin()+index*784, imagesLearning.begin() + (index+1) * 784); for(int a=0; a<784; a++) vect.push_back(imagesLearning.at(index*784 + a)); //vect.assign(imagesLearning.begin()+index*784, imagesLearning.begin() + (index+1) * 784); int valeur = labelsLearning.at(index); std::vector<float> sorties; for(int l=0; l<valeur; l++) sorties.push_back(0); sorties.push_back(1); for(int l=sorties.size(); l<10; l++) sorties.push_back(0); vect.insert(vect.end(), sorties.begin(), sorties.end()); imagesLearning.erase(imagesLearning.begin() + index*784, imagesLearning.begin() + (index+1)*784); labelsLearning.erase(labelsLearning.begin() + index); exemples.push_back(vect); } for(int j=0; j<nombreTest; j++) { int index=-1; while(index==-1) index = (rand()*labelsTest.size()) / RAND_MAX -1; std::vector<float> vect; //vect.insert(vect.end(), imagesTest.begin()+index*784, imagesTest.begin() + (index+1) * 784); for(int a=0; a<784; a++) vect.push_back(imagesLearning.at(index*784 + a)); //vect.assign(imagesTest.begin()+index*784, imagesTest.begin() + (index+1) * 784); int valeur = labelsTest.at(index); std::vector<float> sorties; for(int l=0; l<valeur; l++) sorties.push_back(0); sorties.push_back(1); for(int l=sorties.size(); l<10; l++) sorties.push_back(0); vect.insert(vect.end(), sorties.begin(), sorties.end());//labelsTest.at(index)); imagesTest.erase(imagesTest.begin() + index*784, imagesTest.begin() + (index+1)*784); labelsTest.erase(labelsTest.begin() + index); exemples.push_back(vect); } */ for(int j=0; j<nombreApprentissage; j++) { std::vector<float> vect; vect.assign(imagesLearning.begin()+j*784, imagesLearning.begin() + (j+1) * 784); int valeur = labelsLearning.at(j); std::vector<float> sorties; for(int l=0; l<valeur; l++) sorties.push_back(0); sorties.push_back(1); for(int l=sorties.size(); l<10; l++) sorties.push_back(0); vect.insert(vect.end(), sorties.begin(), sorties.end()); exemples.push_back(vect); } random_shuffle(exemples.begin(), exemples.end()); for(int j=0; j<nombreTest; j++) { std::vector<float> vect; vect.assign(imagesTest.begin()+j*784, imagesTest.begin() + (j+1) * 784); int valeur = labelsTest.at(j); std::vector<float> sorties; for(int l=0; l<valeur; l++) sorties.push_back(0); sorties.push_back(1); for(int l=sorties.size(); l<10; l++) sorties.push_back(0); vect.insert(vect.end(), sorties.begin(), sorties.end());//labelsTest.at(index)); exemples.push_back(vect); } std::cout << "vectors construits" << std::endl; std::cout << "quelques chiffres : " << std::endl; for(int i=0; i<3; i++) { int quelChiffre = 0; for(int j=0; j<10; j++) { quelChiffre += exemples.at(i).at(j+784) * j; } std::cout << "\nchiffre " << quelChiffre << " : " << std::endl; for(int j=0; j<28; j++) { for(int k=0; k<28; k++) { std::cout << exemples.at(i).at(j*28 + k) << "\t"; } std::cout << std::endl; } } return exemples; }
void Labeler::train(const string& trainFile, const string& devFile, const string& testFile, const string& modelFile, const string& optionFile, const string& wordEmbFile, const string& charEmbFile) { if (optionFile != "") m_options.load(optionFile); m_options.showOptions(); vector<Instance> trainInsts, devInsts, testInsts; static vector<Instance> decodeInstResults; static Instance curDecodeInst; bool bCurIterBetter = false; m_pipe.readInstances(trainFile, trainInsts, m_options.maxInstance); if (devFile != "") m_pipe.readInstances(devFile, devInsts, m_options.maxInstance); if (testFile != "") m_pipe.readInstances(testFile, testInsts, m_options.maxInstance); //Ensure that each file in m_options.testFiles exists! vector<vector<Instance> > otherInsts(m_options.testFiles.size()); for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_options.maxInstance); } //std::cout << "Training example number: " << trainInsts.size() << std::endl; //std::cout << "Dev example number: " << trainInsts.size() << std::endl; //std::cout << "Test example number: " << trainInsts.size() << std::endl; createAlphabet(trainInsts); if (!m_options.wordEmbFineTune) { addTestWordAlpha(devInsts); addTestWordAlpha(testInsts); for (int idx = 0; idx < otherInsts.size(); idx++) { addTestWordAlpha(otherInsts[idx]); } cout << "Remain words num: " << m_wordAlphabet.size() << endl; } NRMat<dtype> wordEmb; if (wordEmbFile != "") { readWordEmbeddings(wordEmbFile, wordEmb); } else { wordEmb.resize(m_wordAlphabet.size(), m_options.wordEmbSize); wordEmb.randu(1000); } NRMat<dtype> charEmb; if (charEmbFile != "") { readWordEmbeddings(charEmbFile, charEmb); } else { charEmb.resize(m_charAlphabet.size(), m_options.charEmbSize); charEmb.randu(1001); } m_classifier.setWordEmbFinetune(m_options.wordEmbFineTune); m_classifier.init(wordEmb, m_options.wordcontext, charEmb, m_options.charcontext, m_labelAlphabet.size(), m_options.charhiddenSize, m_options.rnnHiddenSize, m_options.hiddenSize); m_classifier.setDropValue(m_options.dropProb); vector<Example> trainExamples, devExamples, testExamples; initialExamples(trainInsts, trainExamples); initialExamples(devInsts, devExamples); initialExamples(testInsts, testExamples); vector<int> otherInstNums(otherInsts.size()); vector<vector<Example> > otherExamples(otherInsts.size()); for (int idx = 0; idx < otherInsts.size(); idx++) { initialExamples(otherInsts[idx], otherExamples[idx]); otherInstNums[idx] = otherExamples[idx].size(); } dtype bestDIS = 0; int inputSize = trainExamples.size(); int batchBlock = inputSize / m_options.batchSize; if (inputSize % m_options.batchSize != 0) batchBlock++; srand(0); std::vector<int> indexes; for (int i = 0; i < inputSize; ++i) indexes.push_back(i); static Metric eval, metric_dev, metric_test; static vector<Example> subExamples; int devNum = devExamples.size(), testNum = testExamples.size(); for (int iter = 0; iter < m_options.maxIter; ++iter) { std::cout << "##### Iteration " << iter << std::endl; random_shuffle(indexes.begin(), indexes.end()); eval.reset(); for (int updateIter = 0; updateIter < batchBlock; updateIter++) { subExamples.clear(); int start_pos = updateIter * m_options.batchSize; int end_pos = (updateIter + 1) * m_options.batchSize; if (end_pos > inputSize) end_pos = inputSize; for (int idy = start_pos; idy < end_pos; idy++) { subExamples.push_back(trainExamples[indexes[idy]]); } int curUpdateIter = iter * batchBlock + updateIter; dtype cost = m_classifier.process(subExamples, curUpdateIter); eval.overall_label_count += m_classifier._eval.overall_label_count; eval.correct_label_count += m_classifier._eval.correct_label_count; if ((curUpdateIter + 1) % m_options.verboseIter == 0) { //m_classifier.checkgrads(subExamples, curUpdateIter+1); std::cout << "current: " << updateIter + 1 << ", total block: " << batchBlock << std::endl; std::cout << "Cost = " << cost << ", Tag Correct(%) = " << eval.getAccuracy() << std::endl; } m_classifier.updateParams(m_options.regParameter, m_options.adaAlpha, m_options.adaEps); } if (devNum > 0) { bCurIterBetter = false; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_dev.reset(); for (int idx = 0; idx < devExamples.size(); idx++) { vector<string> result_labels; predict(devExamples[idx].m_features, result_labels, devInsts[idx].words); if (m_options.seg) devInsts[idx].SegEvaluate(result_labels, metric_dev); else devInsts[idx].Evaluate(result_labels, metric_dev); if (!m_options.outBest.empty()) { curDecodeInst.copyValuesFrom(devInsts[idx]); curDecodeInst.assignLabel(result_labels); decodeInstResults.push_back(curDecodeInst); } } metric_dev.print(); if (!m_options.outBest.empty() && metric_dev.getAccuracy() > bestDIS) { m_pipe.outputAllInstances(devFile + m_options.outBest, decodeInstResults); bCurIterBetter = true; } if (testNum > 0) { if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idx = 0; idx < testExamples.size(); idx++) { vector<string> result_labels; predict(testExamples[idx].m_features, result_labels, testInsts[idx].words); if (m_options.seg) testInsts[idx].SegEvaluate(result_labels, metric_test); else testInsts[idx].Evaluate(result_labels, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { curDecodeInst.copyValuesFrom(testInsts[idx]); curDecodeInst.assignLabel(result_labels); decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(testFile + m_options.outBest, decodeInstResults); } } for (int idx = 0; idx < otherExamples.size(); idx++) { std::cout << "processing " << m_options.testFiles[idx] << std::endl; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idy = 0; idy < otherExamples[idx].size(); idy++) { vector<string> result_labels; predict(otherExamples[idx][idy].m_features, result_labels, otherInsts[idx][idy].words); if (m_options.seg) otherInsts[idx][idy].SegEvaluate(result_labels, metric_test); else otherInsts[idx][idy].Evaluate(result_labels, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { curDecodeInst.copyValuesFrom(otherInsts[idx][idy]); curDecodeInst.assignLabel(result_labels); decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(m_options.testFiles[idx] + m_options.outBest, decodeInstResults); } } if (m_options.saveIntermediate && metric_dev.getAccuracy() > bestDIS) { std::cout << "Exceeds best previous performance of " << bestDIS << ". Saving model file.." << std::endl; bestDIS = metric_dev.getAccuracy(); writeModelFile(modelFile); } } // Clear gradients } }
int ANBSample::DartThrowing(){ //Dart-Throwing法 int trail = 0; int neighbor_range = 3; //局部邻域范围 int rand_pt_ind; //随机点索引 if(USING_ONION_ORDER){ grid.SetUpOnionOrder(neighbor_range); //开始采样 } #if ORDER_TYPE == 0 random_shuffle(dense_pt_list.begin(),dense_pt_list.end()); int id_counter = 0; vector<sample *>::iterator it; //设置点迭代器 for (it=dense_pt_list.begin();it!=dense_pt_list.end();++it){ (*it)->_ID = id_counter; //获取采样点的id } for(int i = 0 ; i < dense_pt_list.size(); i++){ rand_pt_ind = i; #elif ORDER_TYPE == 1 for(int i = 0 ; i < dense_pt_list.size(); i++){ rand_pt_ind = i; #else #endif sample *s = dense_pt_list[rand_pt_ind]; //采样点集 if(pt_list.empty()){ s->acp_ID = pt_list.size(); pt_list.push_back(s); //将采样点插入到网格中 vector<int> g_ind = GridIndex(s); grid.SetCellValue(g_ind[0],g_ind[1],g_ind[2],s->acp_ID); trail = 0; continue; } else{ vector<int>g_ind =GridIndex(s); vector<int>neighbor; if(USING_ONION_ORDER){ neighbor = grid.GetNeighbor_Onion(g_ind[0],g_ind[1],g_ind[2]); } else{ neighbor = grid.GetNeighbor(g_ind[0],g_ind[1],g_ind[2],neighbor_range, neighbor_range,neighbor_range); } int neighbor_ind; bool conflict = false; for (unsigned int nsi=0; nsi<neighbor.size(); nsi++){ neighbor_ind=neighbor[nsi]; while(true) { if(conflict_check(pt_list[neighbor_ind], s)) { conflict=true; trail++; nsi=neighbor.size(); break; } if(pt_list[neighbor_ind]->next==NULL)// arrive the end of current grid cell { break; } else { neighbor_ind=pt_list[neighbor_ind]->next->acp_ID; } }// end while(true) }//end for (unsigned nsi=0; nsi<neigbor.size(); nsi++) if (!conflict){ s->acp_ID=pt_list.size(); pt_list.push_back(s); // 采样点插入网格 std::vector<int> g_ind=GridIndex(s); if(!grid._grid[g_ind[0]][g_ind[1]][g_ind[2]].empty()) { s->next=pt_list[ grid._grid[g_ind[0]][g_ind[1]][g_ind[2]][0] ]; pt_list[ grid._grid[g_ind[0]][g_ind[1]][g_ind[2]][0] ]->pre=s; } grid.SetCellValue(g_ind[0], g_ind[1], g_ind[2], s->acp_ID); trail=0; } } } return 1; } int ANBSample::process(){ //采样过程 int grid_size[3]; grid_size[0]=grid_size[1]=grid_size[2]=1.0f/r_global; real model_width[3]={ boundingbox[1]-boundingbox[0], boundingbox[3]-boundingbox[2], boundingbox[5]-boundingbox[4]}; real cell_wid=(model_width[0])/( grid_size[0]*1.0f ); grid_size[1]=max((int)floor(model_width[1]/cell_wid),1); grid_size[2]=max((int)floor(model_width[2]/cell_wid),1); grid.CreateGrid(grid_size[0],grid_size[1],grid_size[2]); for (int i=0; i<3; i++) { grid._bin_width[i]=model_width[i]/(grid._size[i]*1.0f); } DartThrowing(); grid.CleanGrid(); grid.DeleteGrid(); return 1; }
//-------------------------------------------------- //random_list //-------------------------------------------------- // Uppgift: Slumpa ordningen av listan // Indata : Vektor som innehåller namnlistan // Utdata : Returnerar en vektor med namlistan som har slumpad ordning //-------------------------------------------------- vector <Person> random_list(vector <Person> v) { random_shuffle(v.begin(), v.end()); return v; }
// Shuffling the edge sets void MaxCutInstance::GetShuffledEdges(std::vector<std::pair<std::pair<int, int>, double> >* ret) const { *ret = all_edges_; random_shuffle(ret->begin(), ret->end()); }
int testCase_9(const string &indexFileName, const Attribute &attribute) { // Functions tested // 1. Create Index // 2. OpenIndex // 3. Insert entry // 4. Scan entries, and delete entries // 5. Scan close // 6. Insert entries again // 7. Scan entries // 8. CloseIndex // 9. DestroyIndex // NOTE: "**" signifies the new functions being tested in this test case. cout << endl << "****In Test Case 9****" << endl; RC rc; RID rid; FileHandle fileHandle; IX_ScanIterator ix_ScanIterator; int compVal; int numOfTuples; int A[30000]; int B[20000]; int count = 0; int key; //create index file rc = indexManager->createFile(indexFileName); if(rc == success) { cout << "Index Created!" << endl; } else { cout << "Failed Creating Index File..." << endl; goto error_return; } //open index file rc = indexManager->openFile(indexFileName, fileHandle); if(rc == success) { cout << "Index File Opened!" << endl; } else { cout << "Failed Opening Index File..." << endl; goto error_destroy_index; } // insert entry numOfTuples = 30000; for(int i = 0; i < numOfTuples; i++) { A[i] = i; } random_shuffle(A, A+numOfTuples); for(int i = 0; i < numOfTuples; i++) { key = A[i]; rid.pageNum = i+1; rid.slotNum = i+1; rc = indexManager->insertEntry(fileHandle, attribute, &key, rid); if(rc != success) { cout << "Failed Inserting Keys..." << endl; goto error_close_index; } } //scan compVal = 20000; rc = indexManager->scan(fileHandle, attribute, NULL, &compVal, true, true, ix_ScanIterator); if(rc == success) { cout << "Scan Opened Successfully!" << endl; } else { cout << "Failed Opening Scan..." << endl; goto error_close_index; } // Test DeleteEntry in IndexScan Iterator count = 0; while(ix_ScanIterator.getNextEntry(rid, &key) == success) { if(count % 1000 == 0) cout << rid.pageNum << " " << rid.slotNum << endl; key = A[rid.pageNum-1]; rc = indexManager->deleteEntry(fileHandle, attribute, &key, rid); if(rc != success) { cout << "Failed deleting entry in Scan..." << endl; goto error_close_scan; } count++; } cout << "Number of deleted entries: " << count << endl; if (count != 20001) { cout << "Wrong entries output...failure" << endl; goto error_close_scan; } //close scan rc = ix_ScanIterator.close(); if(rc == success) { cout << "Scan Closed Successfully!" << endl; } else { cout << "Failed Closing Scan..." << endl; goto error_close_index; } // insert entry Again numOfTuples = 20000; for(int i = 0; i < numOfTuples; i++) { B[i] = 30000+i; } random_shuffle(B, B+numOfTuples); for(int i = 0; i < numOfTuples; i++) { key = B[i]; rid.pageNum = i+30001; rid.slotNum = i+30001; rc = indexManager->insertEntry(fileHandle, attribute, &key, rid); if(rc != success) { cout << "Failed Inserting Keys..." << endl; goto error_close_index; } } //scan compVal = 35000; rc = indexManager->scan(fileHandle, attribute, NULL, &compVal, true, true, ix_ScanIterator); if(rc == success) { cout << "Scan Opened Successfully!" << endl; } else { cout << "Failed Opening Scan..." << endl; goto error_close_index; } count = 0; while(ix_ScanIterator.getNextEntry(rid, &key) == success) { if (count % 1000 == 0) cout << rid.pageNum << " " << rid.slotNum << endl; if(rid.pageNum > 30000 && B[rid.pageNum-30001] > 35000) { cout << "Wrong entries output...failure" << endl; goto error_close_scan; } count ++; } cout << "Number of scanned entries: " << count << endl; //close scan rc = ix_ScanIterator.close(); if(rc == success) { cout << "Scan Closed Successfully!" << endl; } else { cout << "Failed Closing Scan..." << endl; goto error_close_index; } //close index file file rc = indexManager->closeFile(fileHandle); if(rc == success) { cout << "Index File Closed Successfully!" << endl; } else { cout << "Failed Closing Index File..." << endl; goto error_destroy_index; } //destroy index file file rc = indexManager->destroyFile(indexFileName); if(rc == success) { cout << "Index File Destroyed Successfully!" << endl; } else { cout << "Failed Destroying Index File..." << endl; goto error_return; } g_nGradPoint += 5; g_nUndergradPoint += 5; return success; error_close_scan: //close scan ix_ScanIterator.close(); error_close_index: //close index file indexManager->closeFile(fileHandle); error_destroy_index: //destroy index file indexManager->destroyFile(indexFileName); error_return: return fail; }
void test_mgr::run() { // TODO: complete tidy-up of non-summary (verbose) results, then pull through // a command-line summary to control this. // Idea: --summary: prints short results afterwards // --verbose: prints long version as test progresses // defualt: none of these, similar to current summary = true version. const bool summary = true; std::cout << "CSV file: \"" << _args.test_case_csv_file_name << "\""; test_case_set tcs(_args.test_case_csv_file_name, _args.recover_mode); if (tcs.size()) { std::cout << " (found " << tcs.size() << " test case" << (tcs.size() != 1 ? "s" : "") << ")" << std::endl; if (tcs.ignored()) std::cout << "WARNING: " << tcs.ignored() << " test cases were ignored. (All test " "cases without auto-dequeue are ignored when recover-mode is selected.)" << std::endl; _args.print_args(); } else if(tcs.ignored()) { std::cout << " WARNING: All " << tcs.ignored() << " test case(s) were ignored. (All test " "cases without auto-dequeue are ignored when recover-mode is selected.)" << std::endl; } else std::cout << " (WARNING: This CSV file is empty or does not exist.)" << std::endl; do { unsigned u = 0; if (_args.randomize) random_shuffle(tcs.begin(), tcs.end(), _random_fn_ptr); for (test_case_set::tcl_itr tci = tcs.begin(); tci != tcs.end(); tci++, u++) { if (summary) std::cout << "Test case " << (*tci)->test_case_num() << ": \"" << (*tci)->comment() << "\"" << std::endl; else std::cout << (*tci)->str() << std::endl; if (!_args.reuse_instance || _ji_list.empty()) initialize_jrnls(); for (ji_list_citr jii=_ji_list.begin(); jii!=_ji_list.end(); jii++) (*jii)->init_tc(*tci, &_args); for (ji_list_citr jii=_ji_list.begin(); jii!=_ji_list.end(); jii++) (*jii)->run_tc(); for (ji_list_citr jii=_ji_list.begin(); jii!=_ji_list.end(); jii++) (*jii)->tc_wait_compl(); if (_args.format_chk) { for (ji_list_citr jii=_ji_list.begin(); jii!=_ji_list.end(); jii++) { jrnl_init_params::shared_ptr jpp = (*jii)->params(); std::string ja = _args.jfile_analyzer; if (ja.empty()) ja = "./jfile_chk.py"; if (!exists(ja)) { std::ostringstream oss; oss << "ERROR: Validation program \"" << ja << "\" does not exist" << std::endl; throw std::runtime_error(oss.str()); } std::ostringstream oss; oss << ja << " -b " << jpp->base_filename(); // TODO: When jfile_check.py can handle previously recovered journals for // specific tests, then remove this exclusion. if (!_args.recover_mode) { oss << " -c " << _args.test_case_csv_file_name; oss << " -t " << (*tci)->test_case_num(); } oss << " -q " << jpp->jdir(); bool res = system(oss.str().c_str()) != 0; (*tci)->set_fmt_chk_res(res, jpp->jid()); if (res) _err_flag = true; } } if (!_args.recover_mode && !_args.keep_jrnls) for (ji_list_citr jii=_ji_list.begin(); jii!=_ji_list.end(); jii++) try { mrg::journal::jdir::delete_dir((*jii)->jrnl_dir()); } catch (...) {} // TODO - work out exception strategy for failure here... print_results(*tci, summary); if ((*tci)->average().exception()) _err_flag = true; if (_abort || (!_args.repeat_flag && _signal)) break; if (_args.pause_secs && tci != tcs.end()) ::usleep(_args.pause_secs * 1000000); } } while (_args.repeat_flag && !_signal); }
void TLD::initialLearning() { learning = true; //This is just for display purposes DetectionResult *detectionResult = detectorCascade->detectionResult; detectorCascade->detect(currImg); //This is the positive patch NormalizedPatch patch; tldExtractNormalizedPatchRect(currImg, currBB, patch.values); patch.positive = 1; float initVar = tldCalcVariance(patch.values, TLD_PATCH_SIZE * TLD_PATCH_SIZE); detectorCascade->varianceFilter->minVar = initVar / 2; float *overlap = new float[detectorCascade->numWindows]; tldOverlapRect(detectorCascade->windows, detectorCascade->numWindows, currBB, overlap); //Add all bounding boxes with high overlap vector< pair<int, float> > positiveIndices; vector<int> negativeIndices; //First: Find overlapping positive and negative patches for(int i = 0; i < detectorCascade->numWindows; i++) { if(overlap[i] > 0.6) { positiveIndices.push_back(pair<int, float>(i, overlap[i])); } if(overlap[i] < 0.2) { float variance = detectionResult->variances[i]; if(!detectorCascade->varianceFilter->enabled || variance > detectorCascade->varianceFilter->minVar) //TODO: This check is unnecessary if minVar would be set before calling detect. { negativeIndices.push_back(i); } } } sort(positiveIndices.begin(), positiveIndices.end(), tldSortByOverlapDesc); vector<NormalizedPatch> patches; patches.push_back(patch); //Add first patch to patch list int numIterations = std::min<size_t>(positiveIndices.size(), 10); //Take at most 10 bounding boxes (sorted by overlap) for(int i = 0; i < numIterations; i++) { int idx = positiveIndices.at(i).first; //Learn this bounding box //TODO: Somewhere here image warping might be possible detectorCascade->ensembleClassifier->learn(&detectorCascade->windows[TLD_WINDOW_SIZE * idx], true, &detectionResult->featureVectors[detectorCascade->numTrees * idx]); } srand(1); //TODO: This is not guaranteed to affect random_shuffle random_shuffle(negativeIndices.begin(), negativeIndices.end()); //Choose 100 random patches for negative examples for(size_t i = 0; i < std::min<size_t>(100, negativeIndices.size()); i++) { int idx = negativeIndices.at(i); NormalizedPatch patch; tldExtractNormalizedPatchBB(currImg, &detectorCascade->windows[TLD_WINDOW_SIZE * idx], patch.values); patch.positive = 0; patches.push_back(patch); } detectorCascade->nnClassifier->learn(patches); delete[] overlap; }
void fct(int ii, int jj) { typedef std::map<Vertex_handle,Point_2> SM; typedef std::unordered_map<Vertex_handle,Point_2> SUM; typedef boost::unordered_map<Vertex_handle,Point_2> BUM; Dt dt; Vector_2 v(0,0); Random_points rp( 250); std::vector<Point_2> points; for(int i =0; i < ii; i++){ Point_2 p = *rp++; points.push_back(p); } dt.insert(points.begin(), points.end()); std::vector<Vertex_handle> vertices; Finite_vertices_iterator b = dt.finite_vertices_begin(), e = dt.finite_vertices_end(); for(; b!=e; ++b){ vertices.push_back(b); } random_shuffle(vertices.begin(), vertices.end()); Timer tsmc, tsumc, tbumc; Timer tsmq, tsumq, tbumq; for(int j=0; j <jj; j++){ { tsmc.start(); SM sm; for(Vertex_handle vh : vertices){ sm[vh] = vh->point(); } tsmc.stop(); tsmq.start(); for(Vertex_handle vh : vertices){ v = v + (sm[vh] - CGAL::ORIGIN); } tsmq.stop(); } { tsumc.start(); SUM sm; for(Vertex_handle vh : vertices){ sm[vh] = vh->point(); } tsumc.stop(); tsumq.start(); for(Vertex_handle vh : vertices){ v = v + (sm[vh] - CGAL::ORIGIN); } tsumq.stop(); } { tbumc.start(); BUM sm; for(Vertex_handle vh : vertices){ sm[vh] = vh->point(); } tbumc.stop(); tbumq.start(); for(Vertex_handle vh : vertices){ v = v + (sm[vh] - CGAL::ORIGIN); } tbumq.stop(); } } std::cerr << ii << " items and queries (repeated " << jj << " times)" << std::endl; std::cerr << "std::map construction : "<< tsmc.time() << " sec." << std::endl; std::cerr << "std::map queries : "<< tsmq.time() << " sec." << std::endl; std::cerr << "std::unordered_map construction : "<< tsumc.time() << " sec." << std::endl; std::cerr << "std::unordered_map queries : "<< tsumq.time() << " sec." << std::endl; std::cerr << "boost::unordered_map construction : "<< tbumc.time() << " sec." << std::endl; std::cerr << "boost::unordered_map queries : "<< tbumq.time() << " sec.\n" << std::endl; }
//************************************************************************************************* //Main part of the macro //************************************************************************************************* void CreateTrainingAndTestSamples(const string InputFilename, Double_t TestSampleFraction) { TTree* HwwTree = getTreeFromFile(InputFilename.c_str()); assert(HwwTree); MitNtupleEvent event(HwwTree); //************************************************************************************************* //Create randomized list of indices //************************************************************************************************* vector<Int_t> indices; for (Int_t i=0; i<HwwTree->GetEntries(); ++i) { indices.push_back(i); } random_shuffle(indices.begin(),indices.end()); if ( TestSampleFraction > 1.0 || TestSampleFraction < 0.0 ) { cerr << "TestSampleFraction = " << TestSampleFraction << " is not in the range [0,1]. " << endl; assert( TestSampleFraction >= 1.0 && TestSampleFraction <= 0.0 ); } cout << "Input Tree Size : " << HwwTree->GetEntries() << endl; //Remove the '.root' from end of filename size_t p; p = InputFilename.find(".root"); string tmpInputFilename = InputFilename.substr(0,p); //change 'unrandomized' to 'randomized' in the file name. p = tmpInputFilename.find("_unrandomized"); if (p != string::npos) { tmpInputFilename = tmpInputFilename.substr(0,p) + "_randomized" ; } //************************************************************************************************* //Create Randomized Sample Tree //************************************************************************************************* TFile *allSampleFile = new TFile((tmpInputFilename+".all.root").c_str(), "RECREATE"); allSampleFile->cd(); TTree *allSampleTree = HwwTree->CloneTree(0); for (Int_t n=0;n<HwwTree->GetEntries();n++) { event.GetEntry(indices[n]); allSampleTree->Fill(); } allSampleTree->Write(); cout << "All Tree Size: " << allSampleTree->GetEntries() << endl; allSampleFile->Close(); //************************************************************************************************* //Create Test Sample Tree //************************************************************************************************* //For some reason I need to make another TTree, otherwise when I try to clone it, root crashes. TTree* HwwTree2 = getTreeFromFile(InputFilename.c_str()); assert(HwwTree2); assert(HwwTree2->GetEntries() == HwwTree->GetEntries()); MitNtupleEvent event2(HwwTree2); TFile *testSampleFile = new TFile((tmpInputFilename+".test.root").c_str(), "RECREATE"); testSampleFile->cd(); Int_t testSampleSize = Int_t(TestSampleFraction * double(HwwTree->GetEntries())); TTree *testSampleTree = HwwTree2->CloneTree(0); for (Int_t n=0;n<testSampleSize;n++) { event2.GetEntry(indices[n]); event2.H_weight = event2.H_weight / (TestSampleFraction); testSampleTree->Fill(); } testSampleTree->Write(); cout << "Test Tree Size: " << testSampleTree->GetEntries() << endl; testSampleFile->Close(); //************************************************************************************************* //Create Training Sample Tree //************************************************************************************************* //For some reason I need to make another TTree, otherwise when I try to clone it, root crashes. TTree* HwwTree3 = getTreeFromFile(InputFilename.c_str()); assert(HwwTree3); assert(HwwTree3->GetEntries() == HwwTree->GetEntries()); MitNtupleEvent event3(HwwTree3); TFile *trainingSampleFile = new TFile((tmpInputFilename+".training.root").c_str(), "RECREATE"); trainingSampleFile->cd(); TTree *trainingSampleTree = HwwTree3->CloneTree(0); for (Int_t n=testSampleSize;n<HwwTree->GetEntries();n++) { event3.GetEntry(indices[n]); event3.H_weight = event3.H_weight / (1 - TestSampleFraction); trainingSampleTree->Fill(); } trainingSampleTree->Write(); cout << "Training Tree Size: " << trainingSampleTree->GetEntries() << endl; trainingSampleFile->Close(); }
int main( int argc, char *argv[] ) { if ( argc != 4 ) { cout << "Wrong number of command-line arguments" << endl << "Usage: method2 starting-power-of-2 ending-power-of-2 repetitions" << endl << "Example: method2 4 10 50000" << endl; return 1; } Timer aTimer1, aTimer2; const char * pattern = "target"; long int N = atoi( argv[ 1 ] ); N = ( long int ) pow( 2, N ); long int OUTERLIMIT = atoi( argv[ 2 ] ); OUTERLIMIT = ( long int ) pow( 2, OUTERLIMIT ); long int INNERLIMIT = atoi( argv[ 3 ] ); long double elapsedCPUtime = 0; long int pos = 0; cout << endl << "Timing results are expressed in seconds" << endl << "Resolution of the clock: " << aTimer1.getResolution() << " seconds" << endl << "Starting N: " << N << endl << "Maximum N: " << OUTERLIMIT << endl << "Repetitions for each N: " << INNERLIMIT << endl << endl << setw( 9 ) << "N" << setw( 14 ) << "T (seconds)" << setw( 14 ) << "T / logN" << setw( 14 ) << "T / N" << setw( 14 ) << "T / NlogN" << setw( 14 ) << "T / N^2" << endl << setfill( '-' ) << setw( 14 * 5 + 9 ) << "" << setfill( ' ' ) << endl; while ( N <= OUTERLIMIT ) { // edited by Mark Randles vector<int> vi; for (int i = 0; i < N; i++) vi.push_back( i ); random_shuffle(vi.begin(), vi.end()); aTimer1.start(); // Time "empty" function for ( long int j = 0; j < INNERLIMIT; j++ ) pos = flashsortTest( vi.begin(), vi.end() ); // edited by Mark Randles aTimer1.stop(); // aTimer1.display( "aTimer1 after stop()" ); // edited by Mark Randles for (int i = 0; i < N; i++) vi.push_back( i ); random_shuffle(vi.begin(), vi.end()); aTimer2.start(); // Time the real function for ( long int j = 0; j < INNERLIMIT; j++ ) pos = flashsort( vi.begin(), vi.end() ); // edited by Mark Randles aTimer2.stop(); // aTimer2.display( "aTimer2 after stop()" ); // Next line requires overload of operator- if ( aTimer1 > aTimer2 ) cout << "Invalid data for N = " << N << endl; else { elapsedCPUtime = ( aTimer2 - aTimer1 ) / INNERLIMIT; printLine( N, elapsedCPUtime ); } N += N; aTimer1.clear(); aTimer2.clear(); } return 0; }
// ------------------------------------------------------------------------- void MultiMDDAGLearner::parallelRollout(const nor_utils::Args& args, InputData* pData, const string fname, int rsize, GenericClassificationBasedPolicy* policy, PolicyResult* result, const int weakLearnerPostion) { vector<AlphaReal> policyError(_shypIter); vector<InputData*> rollouts(_shypIter,NULL); // generate rollout if (_randomNPercent>0) { vector<int> randomIndices(_shypIter); for( int si = 0; si < _shypIter; ++si ) randomIndices[si]=si; random_shuffle(randomIndices.begin(), randomIndices.end()); int ig = static_cast<int>(static_cast<float>(_shypIter * _randomNPercent) / 100.0); for( int si = 0; si < ig; ++si ) { stringstream ss(fname); // if (si>0) // { // ss << fname << "_" << si; // } else { // ss << fname; // } MDDAGLearner::parallelRollout(args, pData, ss.str(), rsize, policy, result, randomIndices[si]); InputData* rolloutTrainingData = getRolloutData( args, ss.str() ); if (_verbose) cout << "---> Rollout size("<< randomIndices[si] << ")" << rolloutTrainingData->getNumExamples() << endl; rollouts[randomIndices[si]] = rolloutTrainingData; } } else { for( int si = 0; si < _shypIter; ++si ) { stringstream ss(fname); // if (si>0) // { // ss << fname << "_" << si; // } else { // ss << fname; // } MDDAGLearner::parallelRollout(args, pData, ss.str(), rsize, policy, result, si); InputData* rolloutTrainingData = getRolloutData( args, ss.str() ); if (_verbose) cout << "---> Rollout size("<< si << ")" << rolloutTrainingData->getNumExamples() << endl; rollouts[si] = rolloutTrainingData; } } // update policy int numOfUpdatedPolicy = 0; for( int si = 0; si < _shypIter; ++si ) { if ((rollouts[si]==NULL) || (rollouts[si]->getNumExamples()<=2)) continue; policyError[si] = _policy->trainpolicy( rollouts[si], _baseLearnerName, _trainingIter, si ); if (_verbose) cout << "--> Policy error: pos: " << si << "\t error:\t" << setprecision (4) << policyError[si] << endl; numOfUpdatedPolicy++; } if (_verbose) cout << "--> Number of updated policy" << numOfUpdatedPolicy << endl << flush; //release rolouts for( int si = 0; si < _shypIter; ++si ) { if (rollouts[si]) delete rollouts[si]; } }
static void TestAlgorithms (void) { static const int c_TestNumbers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 13, 14, 15, 16, 17, 18 }; const int* first = c_TestNumbers; const int* last = first + VectorSize(c_TestNumbers); intvec_t v, buf; v.assign (first, last); PrintVector (v); cout << "swap(1,2)\n"; swap (v[0], v[1]); PrintVector (v); v.assign (first, last); cout << "copy(0,8,9)\n"; copy (v.begin(), v.begin() + 8, v.begin() + 9); PrintVector (v); v.assign (first, last); cout << "copy with back_inserter\n"; v.clear(); copy (first, last, back_inserter(v)); PrintVector (v); v.assign (first, last); cout << "copy with inserter\n"; v.clear(); copy (first, first + 5, inserter(v, v.begin())); copy (first, first + 5, inserter(v, v.begin() + 3)); PrintVector (v); v.assign (first, last); cout << "copy_n(0,8,9)\n"; copy_n (v.begin(), 8, v.begin() + 9); PrintVector (v); v.assign (first, last); cout << "copy_if(is_even)\n"; intvec_t v_even; copy_if (v, back_inserter(v_even), &is_even); PrintVector (v_even); v.assign (first, last); cout << "for_each(printint)\n{ "; for_each (v, &printint); cout << "}\n"; cout << "for_each(reverse_iterator, printint)\n{ "; for_each (v.rbegin(), v.rend(), &printint); cout << "}\n"; cout << "find(10)\n"; cout.format ("10 found at offset %zd\n", abs_distance (v.begin(), find (v, 10))); cout << "count(13)\n"; cout.format ("%zu values of 13, %zu values of 18\n", count(v,13), count(v,18)); cout << "transform(sqr)\n"; transform (v, &sqr); PrintVector (v); v.assign (first, last); cout << "replace(13,666)\n"; replace (v, 13, 666); PrintVector (v); v.assign (first, last); cout << "fill(13)\n"; fill (v, 13); PrintVector (v); v.assign (first, last); cout << "fill_n(5, 13)\n"; fill_n (v.begin(), 5, 13); PrintVector (v); v.assign (first, last); cout << "fill 64083 uint8_t(0x41) "; TestBigFill<uint8_t> (64083, 0x41); cout << "fill 64083 uint16_t(0x4142) "; TestBigFill<uint16_t> (64083, 0x4142); cout << "fill 64083 uint32_t(0x41424344) "; TestBigFill<uint32_t> (64083, 0x41424344); cout << "fill 64083 float(0.4242) "; TestBigFill<float> (64083, 0x4242f); #if HAVE_INT64_T cout << "fill 64083 uint64_t(0x4142434445464748) "; TestBigFill<uint64_t> (64083, UINT64_C(0x4142434445464748)); #else cout << "No 64bit types available on this platform\n"; #endif cout << "copy 64083 uint8_t(0x41) "; TestBigCopy<uint8_t> (64083, 0x41); cout << "copy 64083 uint16_t(0x4142) "; TestBigCopy<uint16_t> (64083, 0x4142); cout << "copy 64083 uint32_t(0x41424344) "; TestBigCopy<uint32_t> (64083, 0x41424344); cout << "copy 64083 float(0.4242) "; TestBigCopy<float> (64083, 0.4242f); #if HAVE_INT64_T cout << "copy 64083 uint64_t(0x4142434445464748) "; TestBigCopy<uint64_t> (64083, UINT64_C(0x4142434445464748)); #else cout << "No 64bit types available on this platform\n"; #endif cout << "generate(genint)\n"; generate (v, &genint); PrintVector (v); v.assign (first, last); cout << "rotate(4)\n"; rotate (v, 7); rotate (v, -3); PrintVector (v); v.assign (first, last); cout << "merge with (3,5,10,11,11,14)\n"; const int c_MergeWith[] = { 3,5,10,11,11,14 }; intvec_t vmerged; merge (v.begin(), v.end(), VectorRange(c_MergeWith), back_inserter(vmerged)); PrintVector (vmerged); v.assign (first, last); cout << "inplace_merge with (3,5,10,11,11,14)\n"; v.insert (v.end(), VectorRange(c_MergeWith)); inplace_merge (v.begin(), v.end() - VectorSize(c_MergeWith), v.end()); PrintVector (v); v.assign (first, last); cout << "remove(13)\n"; remove (v, 13); PrintVector (v); v.assign (first, last); cout << "remove (elements 3, 4, 6, 15, and 45)\n"; vector<uoff_t> toRemove; toRemove.push_back (3); toRemove.push_back (4); toRemove.push_back (6); toRemove.push_back (15); toRemove.push_back (45); typedef index_iterate<intvec_t::iterator, vector<uoff_t>::iterator> riiter_t; riiter_t rfirst = index_iterator (v.begin(), toRemove.begin()); riiter_t rlast = index_iterator (v.begin(), toRemove.end()); remove (v, rfirst, rlast); PrintVector (v); v.assign (first, last); cout << "unique\n"; unique (v); PrintVector (v); v.assign (first, last); cout << "reverse\n"; reverse (v); PrintVector (v); v.assign (first, last); cout << "lower_bound(10)\n"; PrintVector (v); cout.format ("10 begins at position %zd\n", abs_distance (v.begin(), lower_bound (v, 10))); v.assign (first, last); cout << "upper_bound(10)\n"; PrintVector (v); cout.format ("10 ends at position %zd\n", abs_distance (v.begin(), upper_bound (v, 10))); v.assign (first, last); cout << "equal_range(10)\n"; PrintVector (v); TestEqualRange (v); v.assign (first, last); cout << "sort\n"; reverse (v); PrintVector (v); random_shuffle (v); sort (v); PrintVector (v); v.assign (first, last); cout << "stable_sort\n"; reverse (v); PrintVector (v); random_shuffle (v); stable_sort (v); PrintVector (v); v.assign (first, last); cout << "is_sorted\n"; random_shuffle (v); const bool bNotSorted = is_sorted (v.begin(), v.end()); sort (v); const bool bSorted = is_sorted (v.begin(), v.end()); cout << "unsorted=" << bNotSorted << ", sorted=" << bSorted << endl; v.assign (first, last); cout << "find_first_of\n"; static const int c_FFO[] = { 10000, -34, 14, 27 }; cout.format ("found 14 at position %zd\n", abs_distance (v.begin(), find_first_of (v.begin(), v.end(), VectorRange(c_FFO)))); v.assign (first, last); static const int LC1[] = { 3, 1, 4, 1, 5, 9, 3 }; static const int LC2[] = { 3, 1, 4, 2, 8, 5, 7 }; static const int LC3[] = { 1, 2, 3, 4 }; static const int LC4[] = { 1, 2, 3, 4, 5 }; cout << "lexicographical_compare"; cout << "\nLC1 < LC2 == " << lexicographical_compare (VectorRange(LC1), VectorRange(LC2)); cout << "\nLC2 < LC2 == " << lexicographical_compare (VectorRange(LC2), VectorRange(LC2)); cout << "\nLC3 < LC4 == " << lexicographical_compare (VectorRange(LC3), VectorRange(LC4)); cout << "\nLC4 < LC1 == " << lexicographical_compare (VectorRange(LC4), VectorRange(LC1)); cout << "\nLC1 < LC4 == " << lexicographical_compare (VectorRange(LC1), VectorRange(LC4)); cout << "\nmax_element\n"; cout.format ("max element is %d\n", *max_element (v.begin(), v.end())); v.assign (first, last); cout << "min_element\n"; cout.format ("min element is %d\n", *min_element (v.begin(), v.end())); v.assign (first, last); cout << "partial_sort\n"; reverse (v); partial_sort (v.begin(), v.iat(v.size() / 2), v.end()); PrintVector (v); v.assign (first, last); cout << "partial_sort_copy\n"; reverse (v); buf.resize (v.size()); partial_sort_copy (v.begin(), v.end(), buf.begin(), buf.end()); PrintVector (buf); v.assign (first, last); cout << "partition\n"; partition (v.begin(), v.end(), &is_even); PrintVector (v); v.assign (first, last); cout << "stable_partition\n"; stable_partition (v.begin(), v.end(), &is_even); PrintVector (v); v.assign (first, last); cout << "next_permutation\n"; buf.resize (3); iota (buf.begin(), buf.end(), 1); PrintVector (buf); while (next_permutation (buf.begin(), buf.end())) PrintVector (buf); cout << "prev_permutation\n"; reverse (buf); PrintVector (buf); while (prev_permutation (buf.begin(), buf.end())) PrintVector (buf); v.assign (first, last); cout << "reverse_copy\n"; buf.resize (v.size()); reverse_copy (v.begin(), v.end(), buf.begin()); PrintVector (buf); v.assign (first, last); cout << "rotate_copy\n"; buf.resize (v.size()); rotate_copy (v.begin(), v.iat (v.size() / 3), v.end(), buf.begin()); PrintVector (buf); v.assign (first, last); static const int c_Search1[] = { 5, 6, 7, 8, 9 }, c_Search2[] = { 10, 10, 11, 14 }; cout << "search\n"; cout.format ("{5,6,7,8,9} at %zd\n", abs_distance (v.begin(), search (v.begin(), v.end(), VectorRange(c_Search1)))); cout.format ("{10,10,11,14} at %zd\n", abs_distance (v.begin(), search (v.begin(), v.end(), VectorRange(c_Search2)))); cout << "find_end\n"; cout.format ("{5,6,7,8,9} at %zd\n", abs_distance (v.begin(), find_end (v.begin(), v.end(), VectorRange(c_Search1)))); cout.format ("{10,10,11,14} at %zd\n", abs_distance (v.begin(), find_end (v.begin(), v.end(), VectorRange(c_Search2)))); cout << "search_n\n"; cout.format ("{14} at %zd\n", abs_distance (v.begin(), search_n (v.begin(), v.end(), 1, 14))); cout.format ("{13,13} at %zd\n", abs_distance (v.begin(), search_n (v.begin(), v.end(), 2, 13))); cout.format ("{10,10,10} at %zd\n", abs_distance (v.begin(), search_n (v.begin(), v.end(), 3, 10))); v.assign (first, last); cout << "includes\n"; static const int c_Includes[] = { 5, 14, 15, 18, 20 }; cout << "includes=" << includes (v.begin(), v.end(), VectorRange(c_Includes)-1); cout << ", not includes=" << includes (v.begin(), v.end(), VectorRange(c_Includes)); cout << endl; static const int c_Set1[] = { 1, 2, 3, 4, 5, 6 }, c_Set2[] = { 4, 4, 6, 7, 8 }; intvec_t::iterator setEnd; cout << "set_difference\n"; v.resize (4); setEnd = set_difference (VectorRange(c_Set1), VectorRange(c_Set2), v.begin()); PrintVector (v); assert (setEnd == v.end()); cout << "set_symmetric_difference\n"; v.resize (7); setEnd = set_symmetric_difference (VectorRange(c_Set1), VectorRange(c_Set2), v.begin()); PrintVector (v); assert (setEnd == v.end()); cout << "set_intersection\n"; v.resize (2); setEnd = set_intersection (VectorRange(c_Set1), VectorRange(c_Set2), v.begin()); PrintVector (v); assert (setEnd == v.end()); cout << "set_union\n"; v.resize (9); setEnd = set_union (VectorRange(c_Set1), VectorRange(c_Set2), v.begin()); PrintVector (v); assert (setEnd == v.end()); v.assign (first, last); }
//Shuffle Deck void Deck::shuffle() { random_shuffle(myDeck.begin(), myDeck.end()); }
LatentFactorModel* MatrixFactorization::train(MfParams* params, SparseMatrix* matrix, string path){ int k, s, n; _params = params; _matrix = matrix; _model = new LatentFactorModel(_matrix, _params->type, _params->num_factor, _params->sigma); data = (Triplet*) malloc(_matrix->length() * sizeof(Triplet)); map<pair<int, int>, double>::iterator iter; for(iter = _matrix->kv_dict()->begin(), k = 0; iter != _matrix->kv_dict()->end(); iter++, k++){ data[k].row = iter->first.first; data[k].col = iter->first.second; data[k].val = iter->second; } random_shuffle(data, data + _matrix->length()); if(_params->validate == 0){ num_train = _matrix->length(); num_validate = 0; } else{ num_validate = _matrix->length() / _params->validate; num_train = _matrix->length() - num_validate; } clock_t start, terminal; pthread_t pt; double dot, err, duration, prob; double bu_update, bi_update; double p[_params->num_factor], q[_params->num_factor], swap[_params->num_factor]; for(s = 0; s < _params->max_epoch; s++){ random_shuffle(data, data + num_train); start = clock(); for(n = 0; n < num_train; n++){ Triplet t = data[n]; memcpy(p, _model->P + (t.row * _params->num_factor), sizeof(p)); memcpy(q, _model->Q + (t.col * _params->num_factor), sizeof(q)); bu_update = _model->bu[t.row]; bi_update = _model->bi[t.col]; dot = 0.0; for(k = 0; k < _params->num_factor; k++){ dot += p[k] * q[k]; } if(_params->type == T_REGRESS){ err = t.val - (_model->mu + bu_update + bi_update + dot); } else if(_params->type == T_CLASSIFY){ prob = sigmoid(_model->mu + bu_update + bi_update + dot); err = t.val - prob; } for(k = 0; k < _params->num_factor; k++){ swap[k] = p[k] + _params->alpha * (err * q[k] - _params->lambda * p[k]); q[k] += _params->alpha * (err * p[k] - _params->lambda * q[k]); } memcpy(p, swap, sizeof(swap)); bu_update += _params->alpha * (err - _params->lambda * bu_update); bi_update += _params->alpha * (err - _params->lambda * bi_update); memcpy(_model->P + (t.row * _params->num_factor), p, sizeof(p)); memcpy(_model->Q + (t.col * _params->num_factor), q, sizeof(q)); _model->bu[t.row] = bu_update; _model->bi[t.col] = bi_update; if(_params->type == T_REGRESS){ loss_train[s] += err * err; } else if(_params->type == T_CLASSIFY){ loss_train[s] -= t.val * log(prob) + (1.0 - t.val) * log(1.0 - prob); if((prob >= 0.5 && t.val == 0.0) || (prob < 0.5 && t.val == 1.0)) clf_err_train[s] += 1.0; } } pthread_rwlock_wrlock(&MatrixFactorization::rwlock); _model->swap(); pthread_rwlock_unlock(&MatrixFactorization::rwlock); int epoch = s; // void* params[4] = {_model, &path, &epoch, &rwlock}; void* params[3] = {_model, &path, &epoch}; pthread_create(&pt, NULL, &MatrixFactorization::run_helper, params); // _model->dump("test"); terminal = clock(); duration = (double) (terminal - start) / (double) (CLOCKS_PER_SEC); if(_params->type == T_REGRESS){ loss_train[s] = sqrt(loss_train[s] / num_train); } else if(_params->type == T_CLASSIFY){ loss_train[s] = loss_train[s] / num_train; clf_err_train[s] = clf_err_train[s] / num_train; } validate(num_train, num_train + num_validate, s); cerr << setiosflags(ios::fixed); cerr << "[Iter]: " << setw(4) << setfill('0') << s << " " \ << "\t[Train]: Loss=" << setprecision(4) << loss_train[s] << " "; if(_params->type == T_CLASSIFY){ cerr << "Accuracy=" << setprecision(4) << 100.0 * (1.0 - clf_err_train[s]) << "% "; } if(_params->validate != 0){ cerr << "\t[Validate]: Loss=" << setprecision(4) << loss_validate[s] << " "; if(_params->type == T_CLASSIFY){ cerr << "Accuracy=" << setprecision(4) << 100.0 * (1.0 - clf_err_validate[s]) << "% "; } } cerr << "\t[Duration]: " << setprecision(2) << duration << " Sec." << endl; } pthread_join(pt, NULL); return _model; }
void sudokuBoard::populateBoard() { int temp[3][3]; vector<int> nums; for (int i=1; i<10; ++i) nums.push_back(i); // 1 2 3 4 5 6 7 8 9 random_shuffle ( nums.begin(), nums.end(), myrandom ); for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { temp[i][j] = nums[i*3 +j]; } } // (0,0) x current_board[0][0]=current_board[3][2]=current_board[6][1]= current_board[2][3]=current_board[5][5]=current_board[8][4]= current_board[1][6]=current_board[4][8]=current_board[7][7] = temp[0][0]; // (0,1) x current_board[1][0]=current_board[4][2]=current_board[7][1]= current_board[0][3]=current_board[3][5]=current_board[6][4]= current_board[2][6]=current_board[5][8]=current_board[8][7] = temp[0][1]; // (0,2) x current_board[2][0]=current_board[5][2]=current_board[8][1]= current_board[1][3]=current_board[4][5]=current_board[7][4]= current_board[0][6]=current_board[3][8]=current_board[6][7] = temp[0][2]; // (1,0) x current_board[0][1]=current_board[3][0]=current_board[6][2]= current_board[2][4]=current_board[5][3]=current_board[8][5]= current_board[1][7]=current_board[4][6]=current_board[7][8] = temp[1][0]; // (1,1) x current_board[1][1]=current_board[4][0]=current_board[7][2]= current_board[0][4]=current_board[3][3]=current_board[6][5]= current_board[2][7]=current_board[5][6]=current_board[8][8] = temp[1][1]; // (1,2) x current_board[2][1]=current_board[5][0]=current_board[8][2]= current_board[4][3]=current_board[7][5]=current_board[0][7]= current_board[3][6]=current_board[1][4]=current_board[6][8] = temp[1][2]; // (2,0) x current_board[0][2]=current_board[3][1]=current_board[6][0]= current_board[2][5]=current_board[5][4]=current_board[8][3]= current_board[4][7]=current_board[7][6]=current_board[1][8] = temp[2][0]; // (2,1) x current_board[1][2]=current_board[4][1]=current_board[7][0]= current_board[0][5]=current_board[3][4]=current_board[6][3]= current_board[2][8]=current_board[5][7]=current_board[8][6] = temp[2][1]; // (2,2) x current_board[2][2]=current_board[5][1]=current_board[8][0]= current_board[1][5]=current_board[4][4]=current_board[7][3]= current_board[0][8]=current_board[3][7]=current_board[6][6] = temp[2][2]; return; }
//--------------------------------------------------------------------------- __fastcall TFormMultipleChoiceQuestion::TFormMultipleChoiceQuestion( TComponent* Owner, const boost::shared_ptr<Question>& question, const bool is_test) : TFormQuestion(Owner,question,is_test), m_mcquestion(dynamic_cast<MultipleChoiceQuestion*>(question.get())) { assert(m_mcquestion && "Cast to MultipleChoiceQuestion has failed"); assert(m_mcquestion->m_answers.size() == 1 && "A multiple choice question only has one correct answer"); if (FileExists(m_mcquestion->m_filename.c_str())) { try { Image->Picture->LoadFromFile(m_mcquestion->m_filename.c_str()); } catch (...) { //Just continue... } } else { //Write a message std::vector<std::string> v; v.push_back(" "); //First empty line const int font_width = 6; const int font_height = 8; //Valid filename? //Does it contain a dot? if (std::find( m_mcquestion->m_filename.begin(), m_mcquestion->m_filename.end(), '.') != m_mcquestion->m_filename.end()) { v.push_back(" File not found: "); const std::string filename = " " + m_mcquestion->m_filename + " "; v.push_back(filename); } else { v.push_back(" (No image) "); } v.push_back(" "); //Last empty line const int width = GetMaxStringLength(v) * font_width; const int height = v.size() * font_height; Image->Picture->Graphic->Width = width; Image->Picture->Graphic->Height = height; DotMatrix(Image,0,0,v); } RichEdit->Text = m_mcquestion->m_question.c_str(); { std::vector<std::string> answers(m_mcquestion->mWrongAnswers); answers.push_back(m_mcquestion->m_answers[0]); random_shuffle(answers.begin(), answers.end() ); const int n_answers = answers.size(); RadioGroup->Items->Clear(); for (int i=0; i!=n_answers; ++i) { RadioGroup->Items->Add(answers[i].c_str()); } } }
int CommunityTypeFinder::findkMeans(){ try { error.resize(numPartitions); for (int i = 0; i < numPartitions; i++) { error[i].resize(numOTUs, 0.0); } vector<vector<double> > relativeAbundance(numSamples); vector<vector<double> > alphaMatrix; alphaMatrix.resize(numPartitions); lambdaMatrix.resize(numPartitions); for(int i=0;i<numPartitions;i++){ alphaMatrix[i].assign(numOTUs, 0); lambdaMatrix[i].assign(numOTUs, 0); } //get relative abundance for(int i=0;i<numSamples;i++){ if (m->control_pressed) { return 0; } int groupTotal = 0; relativeAbundance[i].assign(numOTUs, 0.0); for(int j=0;j<numOTUs;j++){ groupTotal += countMatrix[i][j]; } for(int j=0;j<numOTUs;j++){ relativeAbundance[i][j] = countMatrix[i][j] / (double)groupTotal; } } //randomly assign samples into partitions zMatrix.resize(numPartitions); for(int i=0;i<numPartitions;i++){ zMatrix[i].assign(numSamples, 0); } //randomize samples vector<int> temp; for (int i = 0; i < numSamples; i++) { temp.push_back(i); } random_shuffle(temp.begin(), temp.end()); //assign each partition at least one random sample int numAssignedSamples = 0; for (int i = 0; i < numPartitions; i++) { zMatrix[i][temp[numAssignedSamples]] = 1; numAssignedSamples++; } //assign rest of samples to partitions int count = 0; for(int i=numAssignedSamples;i<numSamples;i++){ zMatrix[count%numPartitions][temp[i]] = 1; count++; } double maxChange = 1; int maxIters = 1000; int iteration = 0; weights.assign(numPartitions, 0); while(maxChange > 1e-6 && iteration < maxIters){ if (m->control_pressed) { return 0; } //calcualte average relative abundance maxChange = 0.0000; for(int i=0;i<numPartitions;i++){ double normChange = 0.0; weights[i] = 0; for(int j=0;j<numSamples;j++){ weights[i] += (double)zMatrix[i][j]; } vector<double> averageRelativeAbundance(numOTUs, 0); for(int j=0;j<numOTUs;j++){ for(int k=0;k<numSamples;k++){ averageRelativeAbundance[j] += zMatrix[i][k] * relativeAbundance[k][j]; } } for(int j=0;j<numOTUs;j++){ averageRelativeAbundance[j] /= weights[i]; double difference = averageRelativeAbundance[j] - alphaMatrix[i][j]; normChange += difference * difference; alphaMatrix[i][j] = averageRelativeAbundance[j]; } normChange = sqrt(normChange); if(normChange > maxChange){ maxChange = normChange; } } //calcualte distance between each sample in partition and the average relative abundance for(int i=0;i<numSamples;i++){ if (m->control_pressed) { return 0; } double normalizationFactor = 0; vector<double> totalDistToPartition(numPartitions, 0); for(int j=0;j<numPartitions;j++){ for(int k=0;k<numOTUs;k++){ double difference = alphaMatrix[j][k] - relativeAbundance[i][k]; totalDistToPartition[j] += difference * difference; } totalDistToPartition[j] = sqrt(totalDistToPartition[j]); normalizationFactor += exp(-50.0 * totalDistToPartition[j]); } for(int j=0;j<numPartitions;j++){ zMatrix[j][i] = exp(-50.0 * totalDistToPartition[j]) / normalizationFactor; } } iteration++; // cout << "K means: " << iteration << '\t' << maxChange << endl; } // cout << "Iter:-1"; for(int i=0;i<numPartitions;i++){ weights[i] = 0.0000; for(int j=0;j<numSamples;j++){ weights[i] += zMatrix[i][j]; } // printf("\tw_%d=%.3f", i, weights[i]); } // cout << endl; for(int i=0;i<numOTUs;i++){ if (m->control_pressed) { return 0; } for(int j=0;j<numPartitions;j++){ if(alphaMatrix[j][i] > 0){ lambdaMatrix[j][i] = log(alphaMatrix[j][i]); } else{ lambdaMatrix[j][i] = -10.0; } } } return 0; } catch(exception& e){ m->errorOut(e, "CommunityTypeFinder", "kMeans"); exit(1); } }
void cGame::Shuffle() { random_shuffle(mList.begin(), mList.end()); }
void Column::update_state( vector<Cell*>& active_cells, int input_pattern_index) { update_active_duty(active_state); proximal_segment->learn(active_state); Cell* strongest_predictor = 0; if (active_state) { active_cycles++; update_input_history(input_pattern_index); // find the best predictor if (not predictive_state) { _bursting = true; int best_prediction_level = -1; vector<Cell*> tmp = cells; random_shuffle(tmp.begin(), tmp.end()); for (unsigned int i = 0; i < tmp.size(); ++i) { if (tmp[i]->get_prediction_potential() > best_prediction_level and tmp[i]->get_prediction_potential() > 0) { strongest_predictor = tmp[i]; best_prediction_level = tmp[i]->get_prediction_potential(); } } // no good predictors, just pick a random cell if (strongest_predictor == 0) { strongest_predictor = cells[cla_rand(0,cells.size()-1)]; } } } for (unsigned int i = 0; i < cells.size(); ++i) { char fire_state = false; char learn_state = false; if (active_state) { // if some cells were predicted if (predictive_state) { fire_state = cells[i]->predictive_state;; learn_state = fire_state; } // bursting else { fire_state = true; learn_state = (cells[i]==strongest_predictor); } } // just set all cells inactive else { fire_state = false; learn_state = false; } cells[i]->update_state(fire_state, learn_state, active_cells, input_pattern_index); } }
void Segmentor::train(const string& trainFile, const string& devFile, const string& testFile, const string& modelFile, const string& optionFile, const string& wordEmbFile, const string& charEmbFile, const string& bicharEmbFile) { if (optionFile != "") m_options.load(optionFile); m_options.showOptions(); vector<Instance> trainInsts, devInsts, testInsts; m_pipe.readInstances(trainFile, trainInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); if (devFile != "") m_pipe.readInstances(devFile, devInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); if (testFile != "") m_pipe.readInstances(testFile, testInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); vector<vector<Instance> > otherInsts(m_options.testFiles.size()); for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); } createAlphabet(trainInsts); addTestWordAlpha(devInsts); addTestWordAlpha(testInsts); NRMat<dtype> wordEmb, allwordEmb; if (wordEmbFile != "") { allWordAlphaEmb(wordEmbFile, allwordEmb); } else { std::cout << "must not be here, allword must be pretrained." << std::endl; } wordEmb.resize(m_classifier.fe._wordAlphabet.size(), m_options.wordEmbSize); wordEmb.randu(1000); cout << "word emb dim is " << wordEmb.ncols() << std::endl; NRMat<dtype> charEmb; if (charEmbFile != "") { readEmbeddings(m_classifier.fe._charAlphabet, charEmbFile, charEmb); } else { charEmb.resize(m_classifier.fe._charAlphabet.size(), m_options.charEmbSize); charEmb.randu(2000); } cout << "char emb dim is " << charEmb.ncols() << std::endl; NRMat<dtype> bicharEmb; if (bicharEmbFile != "") { readEmbeddings(m_classifier.fe._bicharAlphabet, bicharEmbFile, bicharEmb); } else { bicharEmb.resize(m_classifier.fe._bicharAlphabet.size(), m_options.bicharEmbSize); bicharEmb.randu(2000); } cout << "bichar emb dim is " << bicharEmb.ncols() << std::endl; NRMat<dtype> actionEmb; actionEmb.resize(m_classifier.fe._actionAlphabet.size(), m_options.actionEmbSize); actionEmb.randu(3000); cout << "action emb dim is " << actionEmb.ncols() << std::endl; NRMat<dtype> lengthEmb; lengthEmb.resize(6, m_options.lengthEmbSize); lengthEmb.randu(3000); cout << "length emb dim is " << actionEmb.ncols() << std::endl; m_classifier.init(wordEmb, allwordEmb, lengthEmb, m_options.wordNgram, m_options.wordHiddenSize, m_options.wordRNNHiddenSize, charEmb, bicharEmb, m_options.charcontext, m_options.charHiddenSize, m_options.charRNNHiddenSize, actionEmb, m_options.actionNgram, m_options.actionHiddenSize, m_options.actionRNNHiddenSize, m_options.sepHiddenSize, m_options.appHiddenSize, m_options.delta); m_classifier.setDropValue(m_options.dropProb); m_classifier.setOOVFreq(m_options.wordCutOff); m_classifier.setOOVRatio(m_options.oovRatio); m_classifier.setWordFreq(m_word_stat); vector<vector<CAction> > trainInstGoldactions; getGoldActions(trainInsts, trainInstGoldactions); double bestFmeasure = 0; int inputSize = trainInsts.size(); std::vector<int> indexes; for (int i = 0; i < inputSize; ++i) indexes.push_back(i); static Metric eval, metric_dev, metric_test; int maxIter = m_options.maxIter * (inputSize / m_options.batchSize + 1); int oneIterMaxRound = (inputSize + m_options.batchSize -1) / m_options.batchSize; std::cout << "maxIter = " << maxIter << std::endl; int devNum = devInsts.size(), testNum = testInsts.size(); static vector<vector<string> > decodeInstResults; static vector<string> curDecodeInst; static bool bCurIterBetter; static vector<vector<string> > subInstances; static vector<vector<CAction> > subInstGoldActions; for (int iter = 0; iter < maxIter; ++iter) { std::cout << "##### Iteration " << iter << std::endl; srand(iter); random_shuffle(indexes.begin(), indexes.end()); std::cout << "random: " << indexes[0] << ", " << indexes[indexes.size() - 1] << std::endl; bool bEvaluate = false; if(m_options.batchSize == 1){ eval.reset(); bEvaluate = true; for (int idy = 0; idy < inputSize; idy++) { subInstances.clear(); subInstGoldActions.clear(); subInstances.push_back(trainInsts[indexes[idy]].chars); subInstGoldActions.push_back(trainInstGoldactions[indexes[idy]]); double cost = m_classifier.train(subInstances, subInstGoldActions); eval.overall_label_count += m_classifier._eval.overall_label_count; eval.correct_label_count += m_classifier._eval.correct_label_count; if ((idy + 1) % (m_options.verboseIter*10) == 0) { std::cout << "current: " << idy + 1 << ", Cost = " << cost << ", Correct(%) = " << eval.getAccuracy() << std::endl; } m_classifier.updateParams(m_options.regParameter, m_options.adaAlpha, m_options.adaEps, m_options.clip); } std::cout << "current: " << iter + 1 << ", Correct(%) = " << eval.getAccuracy() << std::endl; } else{ if(iter == 0)eval.reset(); subInstances.clear(); subInstGoldActions.clear(); for (int idy = 0; idy < m_options.batchSize; idy++) { subInstances.push_back(trainInsts[indexes[idy]].chars); subInstGoldActions.push_back(trainInstGoldactions[indexes[idy]]); } double cost = m_classifier.train(subInstances, subInstGoldActions); eval.overall_label_count += m_classifier._eval.overall_label_count; eval.correct_label_count += m_classifier._eval.correct_label_count; if ((iter + 1) % (m_options.verboseIter) == 0) { std::cout << "current: " << iter + 1 << ", Cost = " << cost << ", Correct(%) = " << eval.getAccuracy() << std::endl; eval.reset(); bEvaluate = true; } m_classifier.updateParams(m_options.regParameter, m_options.adaAlpha, m_options.adaEps, m_options.clip); } if (bEvaluate && devNum > 0) { bCurIterBetter = false; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_dev.reset(); for (int idx = 0; idx < devInsts.size(); idx++) { predict(devInsts[idx], curDecodeInst); devInsts[idx].evaluate(curDecodeInst, metric_dev); if (!m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "dev:" << std::endl; metric_dev.print(); if (!m_options.outBest.empty() && metric_dev.getAccuracy() > bestFmeasure) { m_pipe.outputAllInstances(devFile + m_options.outBest, decodeInstResults); bCurIterBetter = true; } if (testNum > 0) { if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idx = 0; idx < testInsts.size(); idx++) { predict(testInsts[idx], curDecodeInst); testInsts[idx].evaluate(curDecodeInst, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(testFile + m_options.outBest, decodeInstResults); } } for (int idx = 0; idx < otherInsts.size(); idx++) { std::cout << "processing " << m_options.testFiles[idx] << std::endl; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idy = 0; idy < otherInsts[idx].size(); idy++) { predict(otherInsts[idx][idy], curDecodeInst); otherInsts[idx][idy].evaluate(curDecodeInst, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(m_options.testFiles[idx] + m_options.outBest, decodeInstResults); } } if (m_options.saveIntermediate && metric_dev.getAccuracy() > bestFmeasure) { std::cout << "Exceeds best previous DIS of " << bestFmeasure << ". Saving model file.." << std::endl; bestFmeasure = metric_dev.getAccuracy(); writeModelFile(modelFile); } } } }
//-------------------------------------------------------------- void soundizeMeApp::setup(){ // Smooth edges ofEnableSmoothing(); // Fixed framerate ofSetFrameRate(30); m_audio.loadSound("1_opening.mp3"); m_audio.setLoop(true); m_audio.play(); // the fft needs to be smoothed out, so we create an array of floats // for that purpose: m_nBandsToGet = 1024; int * arr = new int[m_nBandsToGet*4]; for (int i = 0; i < m_nBandsToGet*4; i++){ arr[i] = i % m_nBandsToGet; } random_shuffle(&arr[m_nBandsToGet * 0 ], &arr[m_nBandsToGet * 1]); random_shuffle(&arr[m_nBandsToGet * 1 ], &arr[m_nBandsToGet * 2]); random_shuffle(&arr[m_nBandsToGet * 2 ], &arr[m_nBandsToGet * 3]); random_shuffle(&arr[m_nBandsToGet * 3 ], &arr[m_nBandsToGet * 4]); m_fftSmoothed = new float[m_nBandsToGet]; for (int i = 0; i < m_nBandsToGet; i++){ m_fftSmoothed[i] = 0; } /* 1 sichqare X [-2,2] 2 sichqare Y [-2,2] 3 radiusi [5,30] 4 rgb setHex */ for (int i = 0; i < m_nBandsToGet/8 ; i++) { float theta = ofRandom(0,360); ofVec2f location = ofVec2f(ofRandomWidth(),ofRandomHeight()); ofVec2f velocity = ofVec2f(sin(theta),cos(theta)); ofColor color = ofColor(ofRandom(255), ofRandom(255), ofRandom(255)); velocity *= ofRandom(0.5,2); std::vector<int> FFTids; FFTids.push_back(arr[m_nBandsToGet * 0 + i]); FFTids.push_back(arr[m_nBandsToGet * 1 + i]); FFTids.push_back(arr[m_nBandsToGet * 2 + i]); FFTids.push_back(arr[m_nBandsToGet * 3 + i]); // for (int i = 0; i < 4; i++) // FFTids.push_back(int(ofRandom(m_nBandsToGet))); m_balls.push_back(Ball(location,velocity,color,20,FFTids)); } // slider1.setup("slider1", 50, 0, 100); // slider2.setup("slider2", 50, 0, 100); // slider3.setup("slider3", 50, 0, 100); // slider4.setup("slider4", 50, 0, 100); // m_gui.setup("params", 10, 10); // m_gui.add(&slider1); // m_gui.add(&slider2); // m_gui.add(&slider3); // m_gui.add(&slider4); // ofSetBackgroundAuto(false); // ofBackground(0); }
wxString Spring::WriteScriptTxt( IBattle& battle ) const { wxLogMessage(_T("0 WriteScriptTxt called ")); wxString ret; TDFWriter tdf(ret); // Start generating the script. tdf.EnterSection( _T("GAME") ); tdf.Append( _T("HostIP"), battle.GetHostIp() ); if ( battle.IsFounderMe() ) { if ( battle.GetNatType() == NAT_Hole_punching ) tdf.Append( _T("HostPort"), battle.GetMyInternalUdpSourcePort() ); else tdf.Append(_T("HostPort"), battle.GetHostPort() ); } else { tdf.Append( _T("HostPort"), battle.GetHostPort() ); if ( battle.GetNatType() == NAT_Hole_punching ) { tdf.Append( _T("SourcePort"), battle.GetMyInternalUdpSourcePort() ); } else if ( sett().GetClientPort() != 0) { tdf.Append( _T("SourcePort"), sett().GetClientPort() ); /// this allows to play with broken router by setting SourcePort to some forwarded port. } } tdf.Append( _T("IsHost"), battle.IsFounderMe() ); tdf.Append(_T("MyPlayerName"), battle.GetMe().GetNick() ); if ( !battle.IsFounderMe() ) { tdf.LeaveSection(); return ret; } /********************************************************************************** Host-only section **********************************************************************************/ tdf.AppendLineBreak(); tdf.Append(_T("ModHash"), battle.LoadMod().hash ); tdf.Append(_T("MapHash"), battle.LoadMap().hash ); tdf.Append( _T("Mapname"), battle.GetHostMapName() ); tdf.Append( _T("GameType"), battle.GetHostModName() ); tdf.AppendLineBreak(); switch ( battle.GetBattleType() ) { case BT_Played: break; case BT_Replay: { wxString path = battle.GetPlayBackFilePath(); if ( path.Find(_T("/")) != wxNOT_FOUND ) path.BeforeLast(_T('/')); tdf.Append( _T("DemoFile"), path ); tdf.AppendLineBreak(); break; } case BT_Savegame: { wxString path = battle.GetPlayBackFilePath(); if ( path.Find(_T("/")) != wxNOT_FOUND ) path.BeforeLast(_T('/')); tdf.Append( _T("Savefile"), path ); tdf.AppendLineBreak(); break; } default: wxLogDebugFunc( _T("") ); break; } long startpostype; battle.CustomBattleOptions().getSingleValue( _T("startpostype"), OptionsWrapper::EngineOption ).ToLong( &startpostype ); std::vector<StartPos> remap_positions; if ( battle.IsProxy() && ( startpostype != IBattle::ST_Pick ) && ( startpostype != IBattle::ST_Choose ) ) { std::set<int> parsedteams; unsigned int NumUsers = battle.GetNumUsers(); unsigned int NumTeams = 0; for ( unsigned int i = 0; i < NumUsers; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); if ( status.spectator ) continue; if ( parsedteams.find( status.team ) != parsedteams.end() ) continue; // skip duplicates parsedteams.insert( status.team ); NumTeams++; } MapInfo infos = battle.LoadMap().info; unsigned int nummapstartpositions = infos.positions.size(); unsigned int copysize = std::min( nummapstartpositions, NumTeams ); remap_positions = std::vector<StartPos> ( infos.positions.begin(), infos.positions.begin() + copysize ); // only add the first x positions if ( startpostype == IBattle::ST_Random ) { random_shuffle( remap_positions.begin(), remap_positions.end() ); // shuffle the positions } } if ( battle.IsProxy() ) { if ( ( startpostype == IBattle::ST_Random ) || ( startpostype == IBattle::ST_Fixed ) ) { tdf.Append( _T("startpostype"), IBattle::ST_Pick ); } else tdf.Append( _T("startpostype"), startpostype ); } else tdf.Append( _T("startpostype"), startpostype ); tdf.EnterSection( _T("mapoptions") ); OptionsWrapper::wxStringTripleVec optlistMap = battle.CustomBattleOptions().getOptions( OptionsWrapper::MapOption ); for (OptionsWrapper::wxStringTripleVec::const_iterator it = optlistMap.begin(); it != optlistMap.end(); ++it) { tdf.Append(it->first,it->second.second); } tdf.LeaveSection(); tdf.EnterSection(_T("modoptions")); OptionsWrapper::wxStringTripleVec optlistMod = battle.CustomBattleOptions().getOptions( OptionsWrapper::ModOption ); for (OptionsWrapper::wxStringTripleVec::const_iterator it = optlistMod.begin(); it != optlistMod.end(); ++it) { tdf.Append(it->first,it->second.second); } tdf.LeaveSection(); std::map<wxString,int> units = battle.RestrictedUnits(); tdf.Append( _T("NumRestrictions"), units.size()); tdf.EnterSection( _T("RESTRICT") ); int restrictcount = 0; for ( std::map<wxString, int>::iterator itor = units.begin(); itor != units.end(); itor++ ) { tdf.Append(_T("Unit") + TowxString( restrictcount ), itor->first ); tdf.Append(_T("Limit") + TowxString( restrictcount ), itor->second ); restrictcount++; } tdf.LeaveSection(); tdf.AppendLineBreak(); if ( battle.IsProxy() ) { tdf.Append( _T("NumPlayers"), battle.GetNumPlayers() -1 ); tdf.Append( _T("NumUsers"), battle.GetNumUsers() -1 ); } else { tdf.Append( _T("NumPlayers"), battle.GetNumPlayers() ); tdf.Append( _T("NumUsers"), battle.GetNumUsers() ); } tdf.AppendLineBreak(); unsigned int NumUsers = battle.GetNumUsers(); typedef std::map<int, int> ProgressiveTeamsVec; typedef ProgressiveTeamsVec::iterator ProgressiveTeamsVecIter; ProgressiveTeamsVec teams_to_sorted_teams; // original team -> progressive team int free_team = 0; std::map<User*, int> player_to_number; // player -> ordernumber srand ( time(NULL) ); for ( unsigned int i = 0; i < NumUsers; i++ ) { User& user = battle.GetUser( i ); UserBattleStatus& status = user.BattleStatus(); if ( !status.spectator ) { ProgressiveTeamsVecIter itor = teams_to_sorted_teams.find ( status.team ); if ( itor == teams_to_sorted_teams.end() ) { teams_to_sorted_teams[status.team] = free_team; free_team++; } } if ( battle.IsProxy() && ( user.GetNick() == battle.GetFounder().GetNick() ) ) continue; if ( status.IsBot() ) continue; tdf.EnterSection( _T("PLAYER") + TowxString( i ) ); tdf.Append( _T("Name"), user.GetNick() ); tdf.Append( _T("CountryCode"), user.GetCountry().Lower()); tdf.Append( _T("Spectator"), status.spectator ); tdf.Append( _T("Rank"), (int)user.GetRank() ); tdf.Append( _T("IsFromDemo"), int(status.isfromdemo) ); if ( !status.spectator ) { tdf.Append( _T("Team"), teams_to_sorted_teams[status.team] ); } else { int speccteam = 0; if ( teams_to_sorted_teams.size() != 0 ) speccteam = rand() % teams_to_sorted_teams.size(); tdf.Append( _T("Team"), speccteam ); } tdf.LeaveSection(); player_to_number[&user] = i; } if ( usync().VersionSupports( IUnitSync::USYNC_GetSkirmishAI ) ) { for ( unsigned int i = 0; i < NumUsers; i++ ) { User& user = battle.GetUser( i ); UserBattleStatus& status = user.BattleStatus(); if ( !status.IsBot() ) continue; tdf.EnterSection( _T("AI") + TowxString( i ) ); tdf.Append( _T("Name"), user.GetNick() ); // AI's nick; tdf.Append( _T("ShortName"), status.aishortname ); // AI libtype tdf.Append( _T("Version"), status.aiversion ); // AI libtype version tdf.Append( _T("Team"), teams_to_sorted_teams[status.team] ); tdf.Append( _T("IsFromDemo"), int(status.isfromdemo) ); tdf.Append( _T("Host"), player_to_number[&battle.GetUser( status.owner )] ); tdf.EnterSection( _T("Options") ); int optionmapindex = battle.CustomBattleOptions().GetAIOptionIndex( user.GetNick() ); if ( optionmapindex > 0 ) { OptionsWrapper::wxStringTripleVec optlistMod_ = battle.CustomBattleOptions().getOptions( (OptionsWrapper::GameOption)optionmapindex ); for (OptionsWrapper::wxStringTripleVec::const_iterator it = optlistMod_.begin(); it != optlistMod_.end(); ++it) { tdf.Append(it->first,it->second.second); } } tdf.LeaveSection(); tdf.LeaveSection(); player_to_number[&user] = i; } } tdf.AppendLineBreak(); std::set<int> parsedteams; wxArrayString sides = usync().GetSides( battle.GetHostModName() ); for ( unsigned int i = 0; i < NumUsers; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); if ( status.spectator ) continue; if ( parsedteams.find( status.team ) != parsedteams.end() ) continue; // skip duplicates parsedteams.insert( status.team ); tdf.EnterSection( _T("TEAM") + TowxString( teams_to_sorted_teams[status.team] ) ); if ( !usync().VersionSupports( IUnitSync::USYNC_GetSkirmishAI ) && status.IsBot() ) { tdf.Append( _T("AIDLL"), status.aishortname ); tdf.Append( _T("TeamLeader"), player_to_number[&battle.GetUser( status.owner )] ); // bot owner is the team leader } else { if ( status.IsBot() ) { tdf.Append( _T("TeamLeader"), player_to_number[&battle.GetUser( status.owner )] ); } else { tdf.Append( _T("TeamLeader"), player_to_number[&usr] ); } } if ( battle.IsProxy() ) { if ( startpostype == IBattle::ST_Pick ) { tdf.Append(_T("StartPosX"), status.pos.x ); tdf.Append(_T("StartPosZ"), status.pos.y ); } else if ( ( startpostype == IBattle::ST_Fixed ) || ( startpostype == IBattle::ST_Random ) ) { int teamnumber = teams_to_sorted_teams[status.team]; if ( teamnumber < int(remap_positions.size()) ) // don't overflow { StartPos position = remap_positions[teamnumber]; tdf.Append(_T("StartPosX"), position.x ); tdf.Append(_T("StartPosZ"), position.y ); } } } else { if ( startpostype == IBattle::ST_Pick ) { tdf.Append(_T("StartPosX"), status.pos.x ); tdf.Append(_T("StartPosZ"), status.pos.y ); } } tdf.Append( _T("AllyTeam"),status.ally ); wxString colourstring = TowxString( status.colour.Red()/255.0 ) + _T(' ') + TowxString( status.colour.Green()/255.0 ) + _T(' ') + TowxString( status.colour.Blue()/255.0 ); tdf.Append( _T("RGBColor"), colourstring); unsigned int side = status.side; if ( side < sides.GetCount() ) tdf.Append( _T("Side"), sides[side] ); tdf.Append( _T("Handicap"), status.handicap ); tdf.LeaveSection(); } tdf.AppendLineBreak(); unsigned int maxiter = std::max( NumUsers, battle.GetLastRectIdx() + 1 ); std::set<int> parsedallys; for ( unsigned int i = 0; i < maxiter; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); BattleStartRect sr = battle.GetStartRect( i ); if ( status.spectator && !sr.IsOk() ) continue; int ally = status.ally; if ( status.spectator ) ally = i; if ( parsedallys.find( ally ) != parsedallys.end() ) continue; // skip duplicates sr = battle.GetStartRect( ally ); parsedallys.insert( ally ); tdf.EnterSection( _T("ALLYTEAM") + TowxString( ally ) ); tdf.Append( _T("NumAllies"), 0 ); if ( sr.IsOk() ) { const char* old_locale = std::setlocale(LC_NUMERIC, "C"); tdf.Append( _T("StartRectLeft"), wxString::Format( _T("%.3f"), sr.left / 200.0 ) ); tdf.Append( _T("StartRectTop"), wxString::Format( _T("%.3f"), sr.top / 200.0 ) ); tdf.Append( _T("StartRectRight"), wxString::Format( _T("%.3f"), sr.right / 200.0 ) ); tdf.Append( _T("StartRectBottom"), wxString::Format( _T("%.3f"), sr.bottom / 200.0 ) ); std::setlocale(LC_NUMERIC, old_locale); } tdf.LeaveSection(); } tdf.LeaveSection(); return ret; }
void Baralho::shuffleBaralho() //yeah { random_shuffle(BaralhoPecas.begin(), BaralhoPecas.end()); }
vector<int> CRegionalMetaModel::SampleIds( const dist_pair_vector& vcDists, int nSamples ) { vector<int> vcSamples; if( true ){ //this algorithm tries to make a uniform sampling over the set. int nBuckets = 50; REAL rMin = vcDists[0].second; REAL rMax = vcDists[ vcDists.size()-1 ].second; REAL rStep = (rMax-rMin)/nBuckets; //compute the histogram. Each bucket contains the ids. vector< vector<int> > vcBuckets(nBuckets); for( int i=0; i<vcDists.size(); i++ ){ int nId = (int)( (vcDists[i].second - rMin) / rStep ); nId = min( nId, vcBuckets.size()-1 ); vcBuckets[nId].push_back( vcDists[i].first ); } //compute the max density of the buckets. int nMaxRate = 0; for( i=0; i<nBuckets; i++ ){ if( vcBuckets[i].size()>nMaxRate )nMaxRate = vcBuckets[i].size(); } //find the sampling rate at each bucket. int r1 = 0; int r2 = nMaxRate; int r = (r1+r2)/2; while( (r>r1)&&(r2>r) ){ int nSumSamples = 0; for( int i=0; i<nBuckets; i++ ){ if( r > vcBuckets[i].size() ){ nSumSamples += vcBuckets[i].size(); }else{ nSumSamples += r; } } if( nSumSamples == nSamples )break; if( nSumSamples < nSamples ){ r1 = r; }else{ r2 = r; } r = (r1 + r2 ) /2; } //now do the actual sampling. nRate -- the maximum sampling number at each bucket. int nRate = r; for( i=0; i<nBuckets; i++ ){ vector<int>::iterator pos_end = min( vcBuckets[i].begin()+nRate, vcBuckets[i].end() ); //shuffle it if the bucket has more points than the sampling rate. if( vcBuckets[i].size() > nRate )random_shuffle( vcBuckets[i].begin(), vcBuckets[i].end() ); copy( vcBuckets[i].begin(), pos_end, back_inserter(vcSamples) ); } }else{ //this is random sampling from the whole set. //copy the ids. and random_shuffle it, then copy it. vector<int> vcTmpIds( vcDists.size() ); for( int i=0 ; i<vcTmpIds.size(); i++ )vcTmpIds[i] = vcDists[i].first; if( false ){ random_shuffle( vcTmpIds.begin(), vcTmpIds.end() ); copy( vcTmpIds.begin(), vcTmpIds.begin()+nSamples, back_inserter(vcSamples) ); }else{ copy( vcTmpIds.begin(), vcTmpIds.end(), back_inserter(vcSamples) ); } } return vcSamples; }
void DeckOfCards::Shuffle() { srand(time(NULL)); random_shuffle ( deck.begin(), deck.end() ); }
/* Inserts and replaces strings in a map, in random order. */ static void test_replace (void) { const int basis = 15; enum { MAX_ELEMS = 16 }; const int max_trials = 8; int cnt; for (cnt = 0; cnt <= MAX_ELEMS; cnt++) { int insertions[MAX_ELEMS]; int trial; int i; for (i = 0; i < cnt; i++) insertions[i] = (i / 2) | random_value (i, basis); for (trial = 0; trial < max_trials; trial++) { struct string_map map; int data[MAX_ELEMS]; int n_data; /* Insert with replacement in random order. */ n_data = 0; string_map_init (&map); random_shuffle (insertions, cnt, sizeof *insertions); for (i = 0; i < cnt; i++) { const char *key = make_key (insertions[i]); const char *value = make_value (insertions[i]); int j; for (j = 0; j < n_data; j++) if ((data[j] & KEY_MASK) == (insertions[i] & KEY_MASK)) { data[j] = insertions[i]; goto found; } data[n_data++] = insertions[i]; found: if (i % 2) string_map_replace (&map, key, value); else string_map_replace_nocopy (&map, xstrdup (key), xstrdup (value)); check_string_map (&map, data, n_data); } /* Delete in original order. */ for (i = 0; i < cnt; i++) { const char *expected_value; char *value; int j; expected_value = NULL; for (j = 0; j < n_data; j++) if ((data[j] & KEY_MASK) == (insertions[i] & KEY_MASK)) { expected_value = make_value (data[j]); data[j] = data[--n_data]; break; } value = string_map_find_and_delete (&map, make_key (insertions[i])); check ((value != NULL) == (expected_value != NULL)); check (value == NULL || !strcmp (value, expected_value)); free (value); } assert (string_map_is_empty (&map)); string_map_destroy (&map); } } }
/* This function will make a new square maze of the given height and width. If this object already represents a maze it will clear all the existing data before doing so. Start with a square grid with the specified height and width. Select random walls to delete without creating a cycle, until there are no more walls that could be deleted without creating a cycle. Do not delete walls on the perimeter of the grid. The finished maze is always a tree of corridors. */ void SquareMaze::makeMaze(int width, int height) { if(width*height==0) return; WIDTH=width; HEIGHT=height; //disjoint set which will represent all cells in maze by complpetion of //makeMaze function call. Dset.clear(); Dset.addelements(width*height); //1D array Setup //============================================================================// //1d array that maps onto 2d maze. Each element is a struct containing (x,y) //coordinate on maze & which of two walls per cell it attempts to break. //(this requires the size of the array to be twice that of the maze) int dubSize = 2*width*height; deck.resize(0); //clear array deck.resize(dubSize); int w=0; int h=0; int total=0; //initialize (x,y) coordinates and calls for rightWall breaking // while (total < width*height) for(h=0; h < height ; h++) for( w=0 ; w < width ; w++) { // if(total==(width*height) ) break; deck[total].x=w; deck[total].y=h; deck[total].rightWall=true; deck[total].bottomWall=false; total++; } h=0; w=0; //initialize (x,y) coordinates and calls for bottomWall breaking for( h=0 ; h < height ; h++) for(w=0 ; w < width ; w++) { deck[total].x=w; deck[total].y=h; deck[total].rightWall=false; deck[total].bottomWall=true; total++; } //shuffle 1d mapping array for random maze creation srand(time(NULL) ); random_shuffle( deck.begin(), deck.end()); //End of 1d mapping array setup //===========================// //Clear & Setup 2d maze //============================================================================// //Clear & Resize 2d maze array MAZE.resize(0); MAZE.resize(height); for (int i = 0; i < height; ++i) { MAZE[i].resize(0); MAZE[i].resize(width); } //Erect walls & mark edges of maze for(int j=0 ; j < height; j++) for (int i=0 ; i < width ; i ++) { //Erect walls (MAZE[i][j]).Right=true; (MAZE[i][j]).Bottom=true; //Generically mark cells to not contain boundary/edge of maze (MAZE[i][j]).rightEdge=false; (MAZE[i][j]).bottomEdge=false; (MAZE[i][j]).leftEdge=false; (MAZE[i][j]).topEdge=false; //for the fewer cases of boundary/edge cells, overwrite & mark if (i == 0 ) (MAZE[i][j]).leftEdge=true; if (j == 0 ) (MAZE[i][j]).topEdge=true; if (i == (width-1) ) (MAZE[i][j]).rightEdge=true; if (j == (height-1) ) (MAZE[i][j]).bottomEdge=true; } //End of 2d Maze setup //==================// //Follow 1d array mapping onto maze & randomly break down walls w/o creating //cycles (using disjoint set to track potential cycles) //============================================================================// for(int i=0 ; i < dubSize ; i++) { int X=deck[i].x; int Y=deck[i].y; if(deck[i].rightWall) breakRight(X, Y); if(deck[i].bottomWall)breakBottom(X, Y); } //End of breaking down walls: Maze is setup. //==================// }
void Segmentor::train(const string& trainFile, const string& devFile, const string& testFile, const string& modelFile, const string& optionFile, const string& wordEmbFile) { if (optionFile != "") m_options.load(optionFile); m_options.showOptions(); vector<Instance> trainInsts, devInsts, testInsts; m_pipe.readInstances(trainFile, trainInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); if (devFile != "") m_pipe.readInstances(devFile, devInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); if (testFile != "") m_pipe.readInstances(testFile, testInsts, m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); vector<vector<Instance> > otherInsts(m_options.testFiles.size()); for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_classifier.MAX_SENTENCE_SIZE - 2, m_options.maxInstance); } createAlphabet(trainInsts); addTestWordAlpha(devInsts); addTestWordAlpha(testInsts); for (int idx = 0; idx < otherInsts.size(); idx++) { addTestWordAlpha(otherInsts[idx]); } m_classifier.init(); m_classifier.setDropValue(m_options.dropProb); vector<vector<CAction> > trainInstGoldactions; getGoldActions(trainInsts, trainInstGoldactions); double bestFmeasure = 0; int inputSize = trainInsts.size(); std::vector<int> indexes; for (int i = 0; i < inputSize; ++i) indexes.push_back(i); static Metric eval, metric_dev, metric_test; int maxIter = m_options.maxIter * (inputSize / m_options.batchSize + 1); int oneIterMaxRound = (inputSize + m_options.batchSize -1) / m_options.batchSize; std::cout << "maxIter = " << maxIter << std::endl; int devNum = devInsts.size(), testNum = testInsts.size(); static vector<vector<string> > decodeInstResults; static vector<string> curDecodeInst; static bool bCurIterBetter; static vector<vector<string> > subInstances; static vector<vector<CAction> > subInstGoldActions; for (int iter = 0; iter < maxIter; ++iter) { std::cout << "##### Iteration " << iter << std::endl; srand(iter); random_shuffle(indexes.begin(), indexes.end()); std::cout << "random: " << indexes[0] << ", " << indexes[indexes.size() - 1] << std::endl; bool bEvaluate = false; if(m_options.batchSize == 1){ eval.reset(); bEvaluate = true; for (int idy = 0; idy < inputSize; idy++) { subInstances.clear(); subInstGoldActions.clear(); subInstances.push_back(trainInsts[indexes[idy]].chars); subInstGoldActions.push_back(trainInstGoldactions[indexes[idy]]); double cost = m_classifier.train(subInstances, subInstGoldActions); eval.overall_label_count += m_classifier._eval.overall_label_count; eval.correct_label_count += m_classifier._eval.correct_label_count; if ((idy + 1) % (m_options.verboseIter*10) == 0) { std::cout << "current: " << idy + 1 << ", Cost = " << cost << ", Correct(%) = " << eval.getAccuracy() << std::endl; } m_classifier.updateParams(m_options.regParameter, m_options.adaAlpha, m_options.adaEps); } std::cout << "current: " << iter + 1 << ", Correct(%) = " << eval.getAccuracy() << std::endl; } else{ if(iter == 0)eval.reset(); subInstances.clear(); subInstGoldActions.clear(); for (int idy = 0; idy < m_options.batchSize; idy++) { subInstances.push_back(trainInsts[indexes[idy]].chars); subInstGoldActions.push_back(trainInstGoldactions[indexes[idy]]); } double cost = m_classifier.train(subInstances, subInstGoldActions); eval.overall_label_count += m_classifier._eval.overall_label_count; eval.correct_label_count += m_classifier._eval.correct_label_count; if ((iter + 1) % (m_options.verboseIter) == 0) { std::cout << "current: " << iter + 1 << ", Cost = " << cost << ", Correct(%) = " << eval.getAccuracy() << std::endl; eval.reset(); bEvaluate = true; } m_classifier.updateParams(m_options.regParameter, m_options.adaAlpha, m_options.adaEps); } if (bEvaluate && devNum > 0) { bCurIterBetter = false; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_dev.reset(); for (int idx = 0; idx < devInsts.size(); idx++) { predict(devInsts[idx], curDecodeInst); devInsts[idx].evaluate(curDecodeInst, metric_dev); if (!m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "dev:" << std::endl; metric_dev.print(); if (!m_options.outBest.empty() && metric_dev.getAccuracy() > bestFmeasure) { m_pipe.outputAllInstances(devFile + m_options.outBest, decodeInstResults); bCurIterBetter = true; } if (testNum > 0) { if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idx = 0; idx < testInsts.size(); idx++) { predict(testInsts[idx], curDecodeInst); testInsts[idx].evaluate(curDecodeInst, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(testFile + m_options.outBest, decodeInstResults); } } for (int idx = 0; idx < otherInsts.size(); idx++) { std::cout << "processing " << m_options.testFiles[idx] << std::endl; if (!m_options.outBest.empty()) decodeInstResults.clear(); metric_test.reset(); for (int idy = 0; idy < otherInsts[idx].size(); idy++) { predict(otherInsts[idx][idy], curDecodeInst); otherInsts[idx][idy].evaluate(curDecodeInst, metric_test); if (bCurIterBetter && !m_options.outBest.empty()) { decodeInstResults.push_back(curDecodeInst); } } std::cout << "test:" << std::endl; metric_test.print(); if (!m_options.outBest.empty() && bCurIterBetter) { m_pipe.outputAllInstances(m_options.testFiles[idx] + m_options.outBest, decodeInstResults); } } if (m_options.saveIntermediate && metric_dev.getAccuracy() > bestFmeasure) { std::cout << "Exceeds best previous DIS of " << bestFmeasure << ". Saving model file.." << std::endl; bestFmeasure = metric_dev.getAccuracy(); writeModelFile(modelFile); } } } }