void test_conlitron_learner_3d() { C2A_Model* P = NULL; C2A_Model* Q = NULL; readOffFile(P, "../data/cup.off"); readOffFile(Q, "../data/spoon.off"); P->ComputeRadius(); Q->ComputeRadius(); ContactSpaceR3 contactspace(P, Q, 0.05 * (P->radius + Q->radius)); std::vector<ContactSpaceSampleData> contactspace_samples = contactspace.uniform_sample(10000); std::ofstream out("space_test_3d.txt"); asciiWriter(out, contactspace_samples); DataVector w(3); w[0] = 1; w[1] = 1; w[2] = 1; MulticonlitronLearner learner(w, 0.01); /////////////////////////////////////// learner.use_approximate_dist = false; /////////////////////////////////////// tools::Profiler::Begin("learn without approximate knn"); learner.learn(contactspace_samples, 3); tools::Profiler::End("learn without approximate knn"); std::cout << learner.model.numOfHyperPlanes() << std::endl; std::cout << contactspace_samples.size() << ": " << empiricalErrorRatio(contactspace_samples, learner) << " " << errorRatioOnGrid(contactspace, learner, 50) << std::endl; delete P; delete Q; }
void TestWithPrimaryAttr() { config.sort_by_attr = 0; ModelLearner learner(schema, config); MockAttr attr(1); Tuple tuple(3); tuple.attr[0] = tuple.attr[1] = tuple.attr[2] = &attr; while (1) { learner.FeedTuple(tuple); learner.EndOfData(); if (!learner.RequireMoreIterations()) break; } std::vector<size_t> attr_vec = learner.GetOrderOfAttributes(); if (attr_vec[0] != 0 || attr_vec[1] != 2 || attr_vec[2] != 1) std::cerr << attr_vec[1] << "Model Learner w/ Primary Attr Unit Test Failed!\n"; std::unique_ptr<Model> a(learner.GetModel(0)); std::unique_ptr<Model> b(learner.GetModel(1)); std::unique_ptr<Model> c(learner.GetModel(2)); if (a->GetTargetVar() != 0 || a->GetPredictorList().size() != 0 || Check(a.get()) != 111) std::cerr << "Model Learner w/ Primary Attr Unit Test Failed!\n"; if (b->GetTargetVar() != 1 || b->GetPredictorList().size() != 1 || b->GetPredictorList()[0] != 2 || Check(b.get()) != 10) std::cerr << "Model Learner w/ Primary Attr Unit Test Failed!\n"; if (c->GetTargetVar() != 2 || c->GetPredictorList().size() != 0 || Check(c.get()) != 111) std::cerr << "Model Learner w/ Primary Attr Unit Test Failed!\n"; }
void drawSquare() { std::vector< std::vector<int> > points = {{-6, 95, 50}, {6, 95, 50}, {6, 100, 30}, {-6, 100, 30}}; rl::FidoControlSystem learner(1, {0}, {1}, 4); int currentIndex = 0; while(true) { int newIndex = (int)(5*learner.chooseBoltzmanActionDynamic({currentIndex*0.2})[0]); learner.applyReinforcementToLastAction(1 - 0.5*fabs(newIndex-currentIndex), {currentIndex*0.2}); bool isGood = true; for(int a = 0; a < 4; a++) { std::cout << int(learner.chooseBoltzmanAction({a*0.25}, 0)[0]*4) << "\n"; std::cout << "Corr: " << (a+1 > 3 ? 0 : a+1) << "\n"; if(int(learner.chooseBoltzmanAction({a*0.25}, 0)[0]*4) != a+1 > 3 ? 0 : a+1) isGood = false; } if(isGood) break; currentIndex = newIndex; } }
void run_learn_predict_test(bool& ok) { test_tool t("test/digits/learn_predict", ok); typedef recognition_problem p; reexp::lang<p> lang; reexp::data<p> data(lang, sample_dim()); setup_reg<p>(lang, data); reexp::stats<p> stats(data); reexp::learner<p> learner(lang, stats); setup_learner(learner); int rounds = 3; for (int i = 0; i < rounds; i++) { float before = stats.ndl(); if (!learner.add_exp()) break; t<<"expression added."<<checkl; float after = stats.ndl(); t<<"info "<<before<<" -> "<<after<<checkl<<checkl; } t<<checkl<<"learning done."<<checkl<<checkl; print_scan(t, lang, stats); reexp::pinfo info; setup_pinfo(info); reexp::lang_info<p> li(info, lang); t<<"vars:\n\n"<<li.drawn_vars_tostring(cvarid::x, cvarid::y); t<<"px"<<checkl<<checkl; print_pixels(t, data.var(varid::pixel)); for (int i = 0; i < rounds; i++) { t<<"exp"<<i<<checkl<<checkl; print_pixels(t, data.var(varid::digit9 + i + 1)); } print_prediction<p>(t, stats); }
int main(int argc, char *argv[]) { // prepare the random number generator srand(time(0)); // instantiate a reinforcement learner for the torso, checking for state transition at 5Hz (200ms period) Learner learner("icubSim","torso",200); // pick some random states (poses) and add them to the state space of the learner Point_d q(3,CGAL::ORIGIN); for (int i=0; i<6; i++){ std::list<double> thisState; for (int j=0; j<3; j++) thisState.push_back((double)rand()/RAND_MAX); Point_d q(thisState.size(),thisState.begin(),thisState.end()); learner.appendState(q); } //learner.print(); // send control commands and keep track of the current state as the robot moves learner.start(); // wait for the robot to relax into the attractor for the current state sleep(10); // take random actions... the learner will learn an MDP for the bodypart while (true) { learner.takeRandomAction(); //learner.print(); } learner.stop(); //printf("------------------------------------------------------------------------------\n"); //std::vector<const Learner::State*> states = learner.getStates(); //learner.deleteState(states.at(3)); //learner.print(); printf("All finished\n"); return 1; }
//----------------------------------------------------------------------------- int main() { { // Generate some data plJointDistribution orig = make_model(); generate_data(orig, "model_asia.csv", 10000); plCSVDataDescriptor dataset("model_asia.csv", orig.get_variables()); plNodeScoreBIC node_score(dataset); std::cout << "Original model: " << orig << std::endl << "BIC score of the original model on the whole dataset: " << node_score(orig) << std::endl; save("model_asia", orig); } plSymbol A("A", PL_BINARY_TYPE); // visit to Asia? plSymbol S("S", PL_BINARY_TYPE); // Smoker? plSymbol T("T", PL_BINARY_TYPE); // has Tuberculosis plSymbol L("L", PL_BINARY_TYPE); // has Lung cancer plSymbol B("B", PL_BINARY_TYPE); // has Bronchitis plSymbol O("O", PL_BINARY_TYPE); // has tuberculosis Or cancer plSymbol X("X", PL_BINARY_TYPE); // positive X-Ray plSymbol D("D", PL_BINARY_TYPE); // Dyspnoea? plVariablesConjunction variables = A^S^T^L^B^O^X^D; plCSVDataDescriptor dataset("model_asia.csv", variables); plStructureLearner learner(variables); // Learn the dependancy structure between our variables from the // dataset, using the Directed Maximum Spanning Tree algorithm. unsigned int root_index = 0; // using 'A' as the root node. std::vector<plSymbol> order; plEdgeScoreBIC edge_score(dataset); bool result = learner.DMST(edge_score, order, variables[root_index]); plJointDistribution result_dmst = learner.get_joint_distribution(dataset); // Apply the GS algorithm with BIC score on the same dataset. // Use the output of the DMST algo as a starting point. plNodeScoreBIC node_score(dataset); learner.GS(node_score); plJointDistribution result_gs = learner.get_joint_distribution(dataset); std::cout << "DMST-BIC obtained the following model: " << result_dmst << std::endl << "BIC score of the learned model on the whole dataset: " << node_score(result_dmst) << std::endl; std::cout << "DMST + GS obtained the following model: " << result_gs << std::endl << "BIC score of the learned model on the whole dataset: " << node_score(result_gs) << std::endl; save("dmst_bic", result_dmst); save("dmst-gs_bic", result_gs); // On Windows (Visual C++, MinGW) only. #if defined(WIN32) || defined(_WIN32) std::cout << "Press any key to terminate..." << std::endl; getchar(); #endif return 0; }
int main(int argc, char* argv[]) { // random seed initialization std::srand(std::time(0)); // Let us build some fake data. The input set is int, and the output // set is a double. In our fake data, input is choosen randomly in // [0..100[, and output is choosen randomly in [0,1[. Let us store // the data set in some vector. silly::DataSet basis(DATA_SIZE); for(auto& data : basis) data = {gaml::random::uniform(0,100),gaml::random::uniform(0,1)}; // Let us display the data, using the provided stl-like output iterators. auto parser = gaml::make_JSON_parser<silly::Data>(); auto outputDataStream = gaml::make_output_data_stream(std::cout, parser); auto out = gaml::make_output_iterator(outputDataStream); std::copy(basis.begin(),basis.end(),out); std::cout << std::endl << std::endl; // Now, let us train our algorithm on this database. // First create a learner silly::Learner learner; // Second get a predictor from the learner and the example dataset auto predictor = learner(basis.begin(),basis.end(), silly::input_of_data,silly::output_of_data); // lambda functions could have been used instead of input_of_data and output_of_data. // The predictor built by the learning process behaves as a function. std::cout << "Learnt constant = " << predictor(0) << std::endl; // let us now measure the empirical risk of the learnt predictor on // the basis. // We use the quadratic loss to set up an predictor evaluator. It // fits the concept gaml::concept::PredictorEvaluator. auto predictor_evaluator = gaml::risk::empirical(gaml::loss::Quadratic<double>()); // The evaluator can then evaluate our predictor on the data basis. double risk = predictor_evaluator(predictor, basis.begin(),basis.end(), silly::input_of_data,silly::output_of_data); std::cout << "Empirical risk = " << risk << std::endl; // The library also provides tools for evaluating learning // algorithms, instead of predictors. This is typically what cross // validation does. auto algo_evaluator = gaml::risk::cross_validation(gaml::loss::Quadratic<double>(), gaml::partition::kfold(6), true); double real = algo_evaluator(learner, basis.begin(),basis.end(), silly::input_of_data,silly::output_of_data); std::cout << "Real risk = " << real << std::endl; return EXIT_SUCCESS; }
void itp_solver::get_itp_core (expr_ref_vector &core) { scoped_watch _t_ (m_itp_watch); typedef obj_hashtable<expr> expr_set; expr_set B; for (unsigned i = m_first_assumption, sz = m_assumptions.size(); i < sz; ++i) { expr *a = m_assumptions.get (i); app_ref def(m); if (is_proxy(a, def)) { B.insert(def.get()); } B.insert (a); } proof_ref pr(m); pr = get_proof (); if (!m_new_unsat_core) { // old code farkas_learner learner_old; learner_old.set_split_literals(m_split_literals); learner_old.get_lemmas (pr, B, core); elim_proxies (core); simplify_bounds (core); // XXX potentially redundant } else { // new code unsat_core_learner learner(m); if (m_farkas_optimized) { if (true) // TODO: proper options { unsat_core_plugin_farkas_lemma_optimized* plugin_farkas_lemma_optimized = alloc(unsat_core_plugin_farkas_lemma_optimized, learner,m); learner.register_plugin(plugin_farkas_lemma_optimized); } else { unsat_core_plugin_farkas_lemma_bounded* plugin_farkas_lemma_bounded = alloc(unsat_core_plugin_farkas_lemma_bounded, learner,m); learner.register_plugin(plugin_farkas_lemma_bounded); } } else { unsat_core_plugin_farkas_lemma* plugin_farkas_lemma = alloc(unsat_core_plugin_farkas_lemma, learner, m_split_literals, m_farkas_a_const); learner.register_plugin(plugin_farkas_lemma); } if (m_minimize_unsat_core) { unsat_core_plugin_min_cut* plugin_min_cut = alloc(unsat_core_plugin_min_cut, learner, m); learner.register_plugin(plugin_min_cut); } else { unsat_core_plugin_lemma* plugin_lemma = alloc(unsat_core_plugin_lemma, learner); learner.register_plugin(plugin_lemma); } learner.compute_unsat_core(pr, B, core); elim_proxies (core); simplify_bounds (core); // XXX potentially redundant // // debug // expr_ref_vector core2(m); // unsat_core_learner learner2(m); // // unsat_core_plugin_farkas_lemma* plugin_farkas_lemma2 = alloc(unsat_core_plugin_farkas_lemma, learner2, m_split_literals); // learner2.register_plugin(plugin_farkas_lemma2); // unsat_core_plugin_lemma* plugin_lemma2 = alloc(unsat_core_plugin_lemma, learner2); // learner2.register_plugin(plugin_lemma2); // learner2.compute_unsat_core(pr, B, core2); // // elim_proxies (core2); // simplify_bounds (core2); // // IF_VERBOSE(2, // verbose_stream () << "Itp Core:\n" // << mk_pp (mk_and (core), m) << "\n";); // IF_VERBOSE(2, // verbose_stream () << "Itp Core2:\n" // << mk_pp (mk_and (core2), m) << "\n";); //SASSERT(mk_and (core) == mk_and (core2)); } IF_VERBOSE(2, verbose_stream () << "Itp Core:\n" << mk_pp (mk_and (core), m) << "\n";);
void test_conlitron_learner_3d_rotation_approximate_knn() { C2A_Model* P = NULL; C2A_Model* Q = NULL; readOffFile(P, "../data/cup.off"); readOffFile(Q, "../data/spoon.off"); P->ComputeRadius(); Q->ComputeRadius(); ContactSpaceSE3Euler2 contactspace(P, Q, 0.05 * (P->radius + Q->radius)); std::vector<ContactSpaceSampleData> contactspace_samples = contactspace.uniform_sample(10000); std::ofstream out("space_test_3d_rotation.txt"); asciiWriter(out, contactspace_samples); DataVector w(6); w[0] = 1; w[1] = 1; w[2] = 1; w[3] = 1; w[4] = 1; w[5] = 1; MulticonlitronLearner learner(w, 0.01); /////////////////////////////////////// learner.use_approximate_dist = true; /////////////////////////////////////// tools::Profiler::Begin("learn with approximate knn"); learner.learn(contactspace_samples, 6); tools::Profiler::End("learn with approximate knn"); std::cout << learner.model.numOfHyperPlanes() << std::endl; std::cout << contactspace_samples.size() << ": " << empiricalErrorRatio(contactspace_samples, learner) << " " << errorRatioOnGrid(contactspace, learner, 5) << std::endl; MulticonlitronLearner learner2(w, 0.01, 1, 1); /////////////////////////////////////// learner2.use_approximate_dist = true; /////////////////////////////////////// tools::Profiler::Begin("learn with approximate knn"); learner2.learn(contactspace_samples, 6); tools::Profiler::End("learn with approximate knn"); std::cout << learner2.model.numOfHyperPlanes() << std::endl; std::cout << contactspace_samples.size() << ": " << empiricalErrorRatio(contactspace_samples, learner2) << " " << errorRatioOnGrid(contactspace, learner2, 5) << std::endl; MulticonlitronLearner learner3(w, 0.01, 5, 5); /////////////////////////////////////// learner3.use_approximate_dist = true; /////////////////////////////////////// tools::Profiler::Begin("learn with approximate knn"); learner3.learn(contactspace_samples, 6); tools::Profiler::End("learn with approximate knn"); std::cout << learner3.model.numOfHyperPlanes() << std::endl; std::cout << contactspace_samples.size() << ": " << empiricalErrorRatio(contactspace_samples, learner3) << " " << errorRatioOnGrid(contactspace, learner3, 5) << std::endl; MulticonlitronLearner learner4(w, 0.01, 10, 10); /////////////////////////////////////// learner4.use_approximate_dist = true; /////////////////////////////////////// tools::Profiler::Begin("learn with approximate knn"); learner4.learn(contactspace_samples, 6); tools::Profiler::End("learn with approximate knn"); std::cout << learner4.model.numOfHyperPlanes() << std::endl; std::cout << contactspace_samples.size() << ": " << empiricalErrorRatio(contactspace_samples, learner4) << " " << errorRatioOnGrid(contactspace, learner4, 5) << std::endl; delete P; delete Q; }
int main(int argc, char *argv[]) { google::ParseCommandLineFlags(&argc, &argv, true); if(FLAGS_seed > 0) std::srand(FLAGS_seed); using Estimate = FFMEstimate<BasicParameter>; std::unique_ptr<Estimate> learner(new Estimate()); try { if(!FLAGS_model_in.empty()) { learner->load(FLAGS_model_in); } Problem tr_prob; Problem va_prob(FLAGS_validate_path, FLAGS_bias); std::vector<Problem> prob_vec; if(!FLAGS_test) { tr_prob.load_data(FLAGS_train_path, FLAGS_bias); learner->fit(tr_prob, va_prob); if(!FLAGS_model_out.empty()) learner->dump(FLAGS_model_out); } if(FLAGS_predict) { double sum_prob = 0.0; uint32_t validate_size = va_prob.all_y.size(); FILE *file = nullptr; if(validate_size > 0) { std::string pred_path = FLAGS_validate_path + ".ffm.out"; printf("start otuput predict to %s\n", pred_path.c_str()); fflush(stdout); file = open_c_file(pred_path, "w"); for(auto i = 0; i < validate_size; ++i) { auto &f = va_prob.all_f[i]; double prob = learner->predict(f, 0, false, 2); sum_prob += prob; fprintf(file, "%lf\n", prob); } printf("average prob: %lf\n", sum_prob/validate_size); learner->print_stats(); fflush(stdout); } else { std::string pred_path = FLAGS_train_path + ".ffm.out"; printf("start otuput predict to %s\n", pred_path.c_str()); fflush(stdout); file = open_c_file(pred_path, "w"); uint32_t train_size = tr_prob.all_y.size(); if(train_size == 0) tr_prob.load_data(FLAGS_train_path, FLAGS_bias); for(auto i = 0; i < train_size; ++i) { auto &f = tr_prob.all_f[i]; double prob = learner->predict(f); sum_prob += prob; fprintf(file, "%lf\n", prob); } printf("average prob: %lf\n", sum_prob/train_size); fflush(stdout); } fclose(file); } return 0; } catch (std::exception const &exc) { std::cerr << "Exception caught " << exc.what() << "\n"; } }