void EnsembleGenerator::add_one_state(const Ensemble& init_ensemble, Ensemble& new_ensemble) { std::priority_queue<boost::tuple<double, int, int>, Vector<boost::tuple<double, int, int> >, Comparator> bestK; // iterate over all init MultiStateModels and try to add a new state to each for(unsigned int i=0; i<init_ensemble.size(); i++) { unsigned int first_to_search = init_ensemble[i].get_last_state()+1; if(first_to_search<N_) { if(i>0 && i%100==0 && !bestK.empty()) { double curr_bestK_score = boost::get<0>(bestK.top()); std::cout << "Extending ensemble: " << i << " out of " << init_ensemble.size() << " last best " << curr_bestK_score << std::endl; } MultiStateModel new_model(init_ensemble[i]); new_model.add_state(first_to_search); // try all possible additions of a new state for(unsigned int j=first_to_search; j<N_; j++) { new_model.replace_last_state(j); double curr_score = get_score(new_model); if(curr_score < 0.0) continue; // invalid model // add to bestK if(bestK.size() <= K_ || curr_score < boost::get<0>(bestK.top())) { bestK.push(boost::make_tuple(curr_score, i, j)); if(bestK.size() > K_) bestK.pop(); } } } } // save best scoring new_ensemble.assign(bestK.size(), MultiStateModel(0)); int index = bestK.size()-1; // generate bestK new MultiStateModels while(!bestK.empty()) { double score; int ensemble_index, new_state_index; boost::tie(score, ensemble_index, new_state_index) = bestK.top(); MultiStateModel new_model(init_ensemble[ensemble_index]); new_model.add_state(new_state_index); new_model.set_score(score); new_ensemble[index] = new_model; index--; bestK.pop(); } }
transform_model* random_transform(){ transform_model* transform = new_model(); const int size = PROGRAM_SIZE; transform->program = malloc(sizeof(char) * size * 2 + 1); int i, open_brackets = 0; for(i = 0; i < size; i++){ transform->program[i] = PROGRAM_OPTIONS[rand() % PROGRAM_OPTION_COUNT]; if (transform->program[i] == '['){ open_brackets++; } else if (transform->program[i] == ']'){ if (open_brackets > 0){ open_brackets--; } else { i--; } } } for (; open_brackets > 0; open_brackets--){ transform->program[i++] = ']'; } transform->program_size = i; transform->program = realloc(transform->program, sizeof(char) * i + 1); transform->program[i] = '\0'; return transform; }
transform_model* transform_from_program(char *program){ transform_model* transform = new_model(); transform->program = strdup(program); transform->program_size = strlen(program); return transform; }
void triangulate_visitor::visit( subd_mesh_model_t& model, shape_t& shape) { if( !shape.attributes().point().has_attribute( g_P_name)) throw core::runtime_error( core::string8_t( "No P attribute found in triangulate visitor")); shape_t new_shape( shape_t::create_subd_mesh()); subd_mesh_model_t& new_model( shape_cast<subd_mesh_model_t>( new_shape)); do_visit( model, shape, new_model, new_shape); }
void MedSTC::load_model(char* model_root) { char filename[100]; int num_terms, num_topics, num_labels, num_docs; float x, C, dLambda, dGamma, dRho; sprintf(filename, "%s.other", model_root); FILE *fileptr = fopen(filename, "r"); fscanf(fileptr, "num_topics %d\n", &num_topics); fscanf(fileptr, "num_labels %d\n", &num_labels); fscanf(fileptr, "num_terms %d\n", &num_terms); fscanf(fileptr, "num_docs %d\n", &num_docs); fscanf(fileptr, "C %f\n", &C); fscanf(fileptr, "lambda %f\n", &dLambda); fscanf(fileptr, "gamma %f\n", &dGamma); fscanf(fileptr, "rho %f\n", &dRho); fclose(fileptr); new_model(num_docs, num_terms, num_topics, num_labels, C); m_dLambda = dLambda; m_dGamma = dGamma; m_dRho = dRho; sprintf(filename, "%s.beta", model_root); fileptr = fopen(filename, "r"); fscanf(fileptr, "%lf\n", &m_dPoisOffset); for ( int i=0; i<m_nK; i++) { for ( int j=0; j<m_nNumTerms; j++) { fscanf(fileptr, "%f", &x); m_dLogProbW[j][i] = exp( x ); } } fclose(fileptr); sprintf(filename, "%s.eta", model_root); fileptr = fopen(filename, "r"); fscanf(fileptr, "%lf\n", &m_dB); for ( int i=0; i<m_nK; i++) { for ( int k=0; k<m_nLabelNum; k++ ) { fscanf(fileptr, "%f", &x); m_dEta[i+k*m_nK] = x; } } fclose(fileptr); }
void MainWindow::create_actions() { // file menu new_model_action = new QAction(tr("New voxel model"), this); connect(new_model_action, SIGNAL(triggered()), this, SLOT(new_model())); open_model_action = new QAction(tr("Open voxel model"), this); connect(open_model_action, SIGNAL(triggered()), this, SLOT(open_model())); clone_model_action = new QAction(tr("Clone voxel model"), this); connect(clone_model_action, SIGNAL(triggered()), this, SLOT(clone_model())); save_action = new QAction(tr("&Save"), this); save_action->setShortcuts(QKeySequence::Save); connect(save_action, SIGNAL(triggered()), this, SLOT(save())); save_as_action = new QAction(tr("Save &As..."), this); save_as_action->setShortcuts(QKeySequence::SaveAs); connect(save_as_action, SIGNAL(triggered()), this, SLOT(save_as())); exit_action = new QAction(tr("E&xit"), this); exit_action->setShortcuts(QKeySequence::Quit); exit_action->setStatusTip(tr("Exit the application")); connect(exit_action, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); /* cut_action = new QAction(tr("Cu&t"), this); cut_action->setShortcuts(QKeySequence::Cut); cut_action->setStatusTip(tr("Cut the current selection's contents to the " "clipboard")); connect(cut_action, SIGNAL(triggered()), this, SLOT(cut())); copy_action = new QAction(tr("&Copy"), this); copy_action->setShortcuts(QKeySequence::Copy); copy_action->setStatusTip(tr("Copy the current selection's contents to the " "clipboard")); connect(copy_action, SIGNAL(triggered()), this, SLOT(copy())); paste_action = new QAction(tr("&Paste"), this); paste_action->setShortcuts(QKeySequence::Paste); paste_action->setStatusTip(tr("Paste the clipboard's contents into the current " "selection")); connect(paste_action, SIGNAL(triggered()), this, SLOT(paste()));*/ // model menu double_size_action = new QAction(tr("Double size"), this); // new_action->setShortcuts(QKeySequence::New); connect(double_size_action, SIGNAL(triggered()), this, SLOT(double_size())); half_size_action = new QAction(tr("Half size"), this); // open_action->setShortcuts(QKeySequence::Open); connect(half_size_action, SIGNAL(triggered()), this, SLOT(half_size())); optimize_action = new QAction(tr("Optimize dimensions"), this); // save_action->setShortcuts(QKeySequence::Save); connect(optimize_action, SIGNAL(triggered()), this, SLOT(optimize())); rotate_action = new QAction(tr("Rotate 90 degrees"), this); // save_action->setShortcuts(QKeySequence::Save); connect(rotate_action, SIGNAL(triggered()), this, SLOT(rotate())); }
/* * learn dictionary and find optimum code. */ int MedSTC::train(char* start, char* directory, Corpus* pC, Params *param) { m_dDeltaEll = param->DELTA_ELL; m_dLambda = param->LAMBDA; m_dRho = param->RHO; m_dGamma = m_dLambda; long runtime_start = get_runtime(); // allocate variational parameters double ***phi = (double***)malloc(sizeof(double**) * pC->num_docs); for ( int d=0; d<pC->num_docs; d++ ) { phi[d] = (double**)malloc(sizeof(double*)*pC->docs[d].length); for (int n=0; n<pC->docs[d].length; n++) { phi[d][n] = (double*)malloc(sizeof(double) * param->NTOPICS); } } double **theta = (double**)malloc(sizeof(double*)*(pC->num_docs)); for (int d=0; d<pC->num_docs; d++) { theta[d] = (double*)malloc(sizeof(double) * param->NTOPICS); } for ( int d=0; d<pC->num_docs; d++ ) { init_phi(&(pC->docs[d]), phi[d], theta[d], param); } // initialize model if (strcmp(start, "random")==0) { new_model(pC->num_docs, pC->num_terms, param->NTOPICS, param->NLABELS, param->INITIAL_C); init_param( pC ); } else { load_model(start); m_dC = param->INITIAL_C; } strcpy(m_directory, directory); char filename[100]; // run expectation maximization sprintf(filename, "%s/lhood.dat", directory); FILE* lhood_file = fopen(filename, "w"); Document *pDoc = NULL; double dobj, obj_old = 1, converged = 1; int nIt = 0; while (((converged < 0) || (converged > param->EM_CONVERGED) || (nIt <= 2)) && (nIt <= param->EM_MAX_ITER)) { dobj = 0; double dLogLoss = 0; for ( int d=0; d<pC->num_docs; d++ ) { pDoc = &(pC->docs[d]); dobj += sparse_coding( pDoc, d, param, theta[d], phi[d] ); dLogLoss += m_dLogLoss; } // m-step dict_learn(pC, theta, phi, param, false); if ( param->SUPERVISED == 1 ) { // for supervised MedLDA. char buff[512]; get_train_filename( buff, m_directory, param ); outputLowDimData( buff, pC, theta ); svmStructSolver(buff, param, m_dMu); if ( param->PRIMALSVM == 1 ) { // solve svm in the primal form for ( int d=0; d<pC->num_docs; d++ ) { loss_aug_predict( &(pC->docs[d]), theta[d] ); } } dobj += m_dsvm_primalobj; } else ; // check for convergence converged = fabs(1 - dobj / obj_old); obj_old = dobj; // output model and lhood if ( param->SUPERVISED == 1 ) { fprintf(lhood_file, "%10.10f\t%10.10f\t%5.5e\t%.5f\n", dobj-m_dsvm_primalobj, dobj, converged, dLogLoss); } else { fprintf(lhood_file, "%10.10f\t%5.5e\t%.5f\n", dobj, converged, dLogLoss); } fflush(lhood_file); if ( nIt > 0 && (nIt % LAG) == 0) { sprintf( filename, "%s/%d", directory, nIt + 1); save_model( filename, -1 ); sprintf( filename, "%s/%d.theta", directory, nIt + 1 ); save_theta( filename, theta, pC->num_docs, m_nK ); } nIt ++; } // learn the final SVM. if ( param->SUPERVISED == 0 ) { char buff[512]; get_train_filename(buff, m_directory, param); outputLowDimData(buff, pC, theta); svmStructSolver(buff, param, m_dMu); } long runtime_end = get_runtime(); double dTrainTime = ((double)runtime_end-(double)runtime_start) / 100.0; // output the final model sprintf( filename, "%s/final", directory); save_model( filename, dTrainTime ); // output the word assignments (for visualization) int nNum = 0, nAcc = 0; sprintf(filename, "%s/word-assignments.dat", directory); FILE* w_asgn_file = fopen(filename, "w"); for (int d=0; d<pC->num_docs; d++) { sparse_coding( &(pC->docs[d]), d, param, theta[d], phi[d] ); write_word_assignment(w_asgn_file, &(pC->docs[d]), phi[d]); nNum ++; pC->docs[d].predlabel = predict(theta[d]); if ( pC->docs[d].gndlabel == pC->docs[d].predlabel ) nAcc ++; } fclose(w_asgn_file); fclose(lhood_file); sprintf(filename,"%s/train.theta",directory); save_theta(filename, theta, pC->num_docs, m_nK); for (int d=0; d<pC->num_docs; d++) { free( theta[d] ); for (int n=0; n<pC->docs[d].length; n++) free( phi[d][n] ); free( phi[d] ); } free( theta ); free( phi ); return nIt; }
/** * \brief Answer to a click on "New model". * \param event This event occured. */ void bf::main_frame::on_new_model(wxCommandEvent& WXUNUSED(event)) { new_model( wxEmptyString ); } // main_frame::on_new_model()
/** * Loads a model from a file. * Returns a pointer to the bone struct containing the data loaded. */ model *load_model(const char *file_name) { FILE *fp; char buffer[BUFF_LEN], *arg_list[MAX_ARGS]; int i, line = 0, arg_count; mesh *geo; bone *b_new; model *new_mdl = NULL; /* Attempt to open the requested file. Prints error message on error. */ if((fp = fopen(file_name, "r")) == NULL) { fprintf(stderr, "ERROR(load_model): Unable to open file '%s'.\n", file_name); return NULL; } while(fgets(buffer, BUFF_LEN - 1, fp) != NULL) { line++; /* No lines should be more then the max buffer size in length. If any * lines do breach this contraint, then the file is corrupt and will * not be loaded. */ if(buffer[strlen(buffer) - 1] != '\n') { fprintf(stderr, "ERROR(load_model): Buffer overflow in file '%s' line %d\n", file_name, line); return NULL; } buffer[strlen(buffer) - 1] = '\0'; arg_count = makeargs(buffer, SEPERATOR, arg_list, MAX_ARGS); if(arg_count == 0) continue; /* If there's an m as the first character (which there should be at the * head of each model file) then we prepare the model. */ if(buffer[0] == 'm') { if(arg_count != 4) continue; /* Create a new model struct. */ new_mdl = new_model(arg_list[1], atoi(arg_list[2])); if(new_mdl == NULL) return NULL; /* Initialise all the new models variables. */ new_mdl->texture = loadTexture(arg_list[3]); printf("New model '%s' created. Loading Model...\n", arg_list[1]); } /* If we have a bone, indicated by a b as the first character. Note that * bones can only be added if a model line has been read in. */ else if(buffer[0] == 'b') { if(new_mdl == NULL) { fprintf(stderr, "ERROR(load_mdl): Attempted load bone without model decl.\n"); continue; } if(arg_count < 7) continue; printf("Loading bone '%s' into model '%s'.\n", arg_list[1], new_mdl->name); new_mdl->n_bones++; /* Allocate memory for the bone object and various chores that need * to be done to the bone to keep it healthy. */ b_new = malloc(sizeof(bone)); if(b_new == NULL) { fprintf(stderr, "ERROR(load_model): Cannot allocate memory.\n"); exit(1); } b_new->child = NULL; b_new->sibling = NULL; b_new->child_count = 0; /** * Allocate memory for the bone name and copy the string accross. */ b_new->name = malloc(strlen(arg_list[1])); strcpy(b_new->name, arg_list[1]); /* Parse translations from the strings and put them into the bone's * translation struct. */ for(i = 0; i < TRANS_SIZE; i++) b_new->rot[i] = atof(arg_list[i + 2]); b_new->length = atof(arg_list[5]); /* Attempt to load a new mesh into the current bone. */ if(strlen(arg_list[7]) > 0) { geo = load_obj(arg_list[7]); if(geo == NULL) { fprintf(stderr, "Error loading obj %s\n", arg_list[7]); exit(1); } /* Create a vertex array out of the loaded obj and free the mesh * struct that was temporarily created. */ if(strlen(arg_list[8]) > 0 && arg_list[8][0] == 'F') b_new->geometry = mesh_to_array(geo, NORM_FLAT); else b_new->geometry = mesh_to_array(geo, NORM_SMOOTH); b_new->tri_count = geo->n_elements; free_mesh(geo); } else b_new->geometry = NULL; /* If the root bone has not been set then this is obviously the root * bone, or a file format error. */ if(new_mdl->root == NULL) { if(strlen(arg_list[6]) == 0) new_mdl->root = b_new; else { fprintf(stderr, "ERROR(load_model): Error, root bone must not have parent.\n"); free_model(new_mdl); return NULL; } } else { if(!skel_add_child(arg_list[6], new_mdl->root, b_new)) { printf("Cannot add child %s.\n", b_new->name); free_model(new_mdl); return NULL; } } } /* Start of an animation. */ else if(buffer[0] == 'a') { if(new_mdl == NULL) { fprintf(stderr, "ERROR(load_mdl): Attempted load animation without model decl.\n"); continue; } if(arg_count != 2) continue; load_animation(new_mdl, atoi(arg_list[1]), fp); } } if(new_mdl != NULL) new_mdl->bone_array = skel_make_array(new_mdl->root, new_mdl->n_bones); printf("New model '%s' successfully loaded.\n", new_mdl->name); printf("%d bones loaded.\n", new_mdl->n_bones); printf("%d animations loaded.\n", new_mdl->n_anims); return new_mdl; }