void update_tree (struct node*node, int old_value, int new_value) { if (node != null) { if (node->value == old_value) { node->value = new_value; } else { if (node->value > old_value) { update_tree (node->left, old_value, new_value); } else { update_tree (node->right, old_value, new_value); } } } }
void update_tree(int L, int R, int fn, int id){ if (L == R){ multiset<int>::reverse_iterator ptr=D[V[L]].rbegin(); tree[id].mxL = tree[id].mxR = tree[id].mx = *ptr; ptr++; tree[id].mx = max(-INF,tree[id].mx+(*ptr)); return ; } int M=(L+R)/2; if (fn <= M) update_tree(L,M,fn,id*2+1); else update_tree(M+1,R,fn,id*2+2); pushUp(L,R,id); }
void update_tree(int node, int left, int right, int u, int v, long long val) { lazy_update(node); if (u > right || v < left) return; if (u <= left && v >= right) { nodes[node] += val; lazy[2*node] += val; lazy[2*node+1] += val; return; } int mid = (left+right)/2; update_tree(2*node, left, mid, u, v, val); update_tree(2*node+1, mid+1, right, u, v, val); nodes[node] = max(nodes[2*node], nodes[2*node+1]); }
static void toggle_sym_value(struct menu *menu) { if (!menu->sym) return; sym_toggle_tristate_value(menu->sym); if (view_mode == FULL_VIEW) update_tree(&rootmenu, NULL); else if (view_mode == SPLIT_VIEW) { update_tree(browsed, NULL); display_list(); } else if (view_mode == SINGLE_VIEW) display_tree_part(); //fixme: keep exp/coll }
/* Change hex/int/string value in the cell */ static void renderer_edited(GtkCellRendererText * cell, const gchar * path_string, const gchar * new_text, gpointer user_data) { GtkTreePath *path = gtk_tree_path_new_from_string(path_string); GtkTreeIter iter; const char *old_def, *new_def; struct menu *menu; struct symbol *sym; if (!gtk_tree_model_get_iter(model2, &iter, path)) return; gtk_tree_model_get(model2, &iter, COL_MENU, &menu, -1); sym = menu->sym; gtk_tree_model_get(model2, &iter, COL_VALUE, &old_def, -1); new_def = new_text; sym_set_string_value(sym, new_def); config_changed = TRUE; update_tree(&rootmenu, NULL); gtk_tree_path_free(path); }
void ConnectionsDialog::_connect() { TreeItem *it = tree->get_selected(); ERR_FAIL_COND(!it); String signal=it->get_metadata(0).operator Dictionary()["name"]; NodePath dst_path=connect_dialog->get_dst_path(); Node *target = node->get_node(dst_path); ERR_FAIL_COND(!target); StringName dst_method=connect_dialog->get_dst_method(); bool defer=connect_dialog->get_deferred(); Vector<Variant> binds = connect_dialog->get_binds(); StringArray args = it->get_metadata(0).operator Dictionary()["args"]; undo_redo->create_action("Connect '"+signal+"' to '"+String(dst_method)+"'"); undo_redo->add_do_method(node,"connect",signal,target,dst_method,binds,CONNECT_PERSIST | (defer?CONNECT_DEFERRED:0)); undo_redo->add_undo_method(node,"disconnect",signal,target,dst_method); undo_redo->add_do_method(this,"update_tree"); undo_redo->add_undo_method(this,"update_tree"); undo_redo->commit_action(); if (connect_dialog->get_make_callback()) { print_line("request connect"); editor->emit_signal("script_add_function_request",target,dst_method,args); hide(); } update_tree(); }
static void ptree_periodic(struct wdgt *w) { DBG("**** building process tree root %d *****", tree_root); update_tree(mark_del); delete_tree_lines(w); synchronize(w); ptreeinfo(w, 1); }
void CreateDialog::set_base_type(const String& p_base) { set_title(vformat("Create %s Type",p_base)); if (base==p_base) return; base=p_base; if (is_inside_scene()) update_tree(); }
void CreateDialog::set_base_type(const String& p_base) { set_title("Create "+p_base+" Type"); if (base==p_base) return; base=p_base; if (is_inside_scene()) update_tree(); }
int update(int x){ int u=V.back(); int p=fa[u]; if (p) D[p].erase(D[p].find(faW[u]+tree[0].mxR)); ans.erase(ans.find(tree[0].mx)); update_tree(0,n-1,at[x],0); ans.insert(tree[0].mx); if (p) D[p].insert(faW[u]+tree[0].mxR); return p; }
/* Change the value of a symbol and update the tree */ static void change_sym_value(struct menu *menu, gint col) { struct symbol *sym = menu->sym; tristate oldval, newval; if (!sym) return; if (col == COL_NO) newval = no; else if (col == COL_MOD) newval = mod; else if (col == COL_YES) newval = yes; else return; switch (sym_get_type(sym)) { case S_BOOLEAN: case S_TRISTATE: oldval = sym_get_tristate_value(sym); if (!sym_tristate_within_range(sym, newval)) newval = yes; sym_set_tristate_value(sym, newval); config_changed = TRUE; if (view_mode == FULL_VIEW) update_tree(&rootmenu, NULL); else if (view_mode == SPLIT_VIEW) { update_tree(browsed, NULL); display_list(); } else if (view_mode == SINGLE_VIEW) display_tree_part(); //fixme: keep exp/coll break; case S_INT: case S_HEX: case S_STRING: default: break; } }
void ConnectionsDock::_connect_pressed() { TreeItem *item = tree->get_selected(); if (!item) { //no idea how this happened, but disable connect_button->set_disabled(true); return; } if (item->get_parent()==tree->get_root() || item->get_parent()->get_parent()==tree->get_root()) { //a signal - connect String signal=item->get_metadata(0).operator Dictionary()["name"]; String signalname=signal; String midname=node->get_name(); for(int i=0;i<midname.length();i++) { CharType c = midname[i]; if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_') { //all good } else if (c==' ') { c='_'; } else { midname.remove(i); i--; continue; } midname[i]=c; } connect_dialog->edit(node); connect_dialog->popup_centered_ratio(); connect_dialog->set_title(TTR("Connecting Signal:")+" "+signalname); connect_dialog->set_dst_method("_on_"+midname+"_"+signal); connect_dialog->set_dst_node(node->get_owner()?node->get_owner():node); } else { //a slot- disconnect Connection c=item->get_metadata(0); ERR_FAIL_COND(c.source!=node); //shouldn't happen but...bugcheck undo_redo->create_action(TTR("Create Subscription")); undo_redo->add_do_method(node,"disconnect",c.signal,c.target,c.method); undo_redo->add_undo_method(node,"connect",c.signal,c.target,c.method,Vector<Variant>(),c.flags); undo_redo->add_do_method(this,"update_tree"); undo_redo->add_undo_method(this,"update_tree"); undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree undo_redo->commit_action(); c.source->disconnect(c.signal,c.target,c.method); update_tree(); } }
void CreateDialog::_notification(int p_what) { if (p_what==NOTIFICATION_READY) { connect("confirmed",this,"_create"); update_tree(); } if (p_what==NOTIFICATION_DRAW) { //RID ci = get_canvas_item(); //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size())); } }
/* Update tree to be identical tree but with nodes reallocated * with probability pupdate. This avoids writing to vector slots * if unecessary. */ static obj_t update_tree(mps_ap_t ap, obj_t oldtree, unsigned d) { obj_t tree; size_t i; if (oldtree == objNULL || d == 0) return oldtree; if (rnd_double() < pupdate) { tree = mkvector(ap, width); for (i = 0; i < width; ++i) { aset(tree, i, update_tree(ap, aref(oldtree, i), d - 1)); } } else { tree = oldtree; for (i = 0; i < width; ++i) { obj_t oldsubtree = aref(oldtree, i); obj_t subtree = update_tree(ap, oldsubtree, d - 1); if (subtree != oldsubtree) { aset(tree, i, subtree); } } } return tree; }
void ConnectionsDock::_connect_pressed() { TreeItem *item = tree->get_selected(); if (!item) { connect_button->set_disabled(true); return; } if (_is_item_signal(*item)) { _open_connection_dialog(*item); } else { _disconnect(*item); update_tree(); } }
static void *gc_tree(gcthread_t thread) { unsigned i, j; mps_ap_t ap = thread->ap; obj_t leaf = pinleaf ? mktree(ap, 1, objNULL) : objNULL; for (i = 0; i < niter; ++i) { obj_t tree = mktree(ap, depth, leaf); for (j = 0 ; j < npass; ++j) { if (preuse < 1.0) tree = new_tree(ap, tree, depth); if (pupdate > 0.0) tree = update_tree(ap, tree, depth); } } return NULL; }
void update_tree (int tree_index,int curr_L,int curr_R,int L,int R,T value,Func f) { /* * Out Of Bounds Condition */ if ((curr_L > curr_R) || (curr_L > R) || (curr_R < L)) return; /* * Leaf Node Condition */ if (curr_L == curr_R) { /* * Modify Here To Get Other Update Functions */ tree[tree_index] += value; return; } /* * Recursively Update The Left Half And The Right Half */ update_tree (tree_index*2,curr_L,(curr_L+curr_R)/2,L,R,value,f); update_tree ((tree_index*2)+1,((curr_L+curr_R)/2)+1,curr_R,L,R,value,f); /* * After Completing Updating The Left And Right Subtree Update The Current Node */ tree[tree_index] = f(tree[tree_index*2],tree[(tree_index*2)+1]); }
/* Post-ConnectDialog callback for creating/editing connections. Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed. */ void ConnectionsDock::_make_or_edit_connection() { TreeItem *it = tree->get_selected(); ERR_FAIL_COND(!it); NodePath dst_path = connect_dialog->get_dst_path(); Node *target = selectedNode->get_node(dst_path); ERR_FAIL_COND(!target); Connection cToMake; cToMake.source = connect_dialog->get_source(); cToMake.target = target; cToMake.signal = connect_dialog->get_signal_name(); cToMake.method = connect_dialog->get_dst_method_name(); cToMake.binds = connect_dialog->get_binds(); bool defer = connect_dialog->get_deferred(); bool oshot = connect_dialog->get_oneshot(); cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0); //conditions to add function, must have a script and must have a method bool add_script_function = !target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cToMake.method); PoolStringArray script_function_args; if (add_script_function) { // pick up args here before "it" is deleted by update_tree script_function_args = it->get_metadata(0).operator Dictionary()["args"]; for (int i = 0; i < cToMake.binds.size(); i++) { script_function_args.append("extra_arg_" + itos(i)); } } if (connect_dialog->is_editing()) { _disconnect(*it); _connect(cToMake); } else { _connect(cToMake); } // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, // which will delete the object "it" is pointing to it = NULL; if (add_script_function) { editor->emit_signal("script_add_function_request", target, cToMake.method, script_function_args); hide(); } update_tree(); }
void ConnectionsDock::_handle_slot_menu_option(int option) { TreeItem *item = tree->get_selected(); if (!item) return; switch (option) { case EDIT: { Connection c = item->get_metadata(0); _open_connection_dialog(c); } break; case GO_TO_SCRIPT: { _go_to_script(*item); } break; case DISCONNECT: { _disconnect(*item); update_tree(); } break; } }
void ConnectionsDock::_connect() { TreeItem *it = tree->get_selected(); ERR_FAIL_COND(!it); String signal=it->get_metadata(0).operator Dictionary()["name"]; NodePath dst_path=connect_dialog->get_dst_path(); Node *target = node->get_node(dst_path); ERR_FAIL_COND(!target); StringName dst_method=connect_dialog->get_dst_method(); bool defer=connect_dialog->get_deferred(); bool oshot=connect_dialog->get_oneshot(); Vector<Variant> binds = connect_dialog->get_binds(); PoolStringArray args = it->get_metadata(0).operator Dictionary()["args"]; int flags = CONNECT_PERSIST | (defer?CONNECT_DEFERRED:0) | (oshot?CONNECT_ONESHOT:0); undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"),signal,String(dst_method))); undo_redo->add_do_method(node,"connect",signal,target,dst_method,binds,flags); undo_redo->add_undo_method(node,"disconnect",signal,target,dst_method); undo_redo->add_do_method(this,"update_tree"); undo_redo->add_undo_method(this,"update_tree"); undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree undo_redo->commit_action(); if (connect_dialog->get_make_callback()) { print_line("request connect"); editor->emit_signal("script_add_function_request",target,dst_method,args); hide(); } update_tree(); }
new_music_d::new_music_d(QWidget *parent) : QDialog(parent), ui(new Ui::new_music_d) { ui->setupUi(this); //CHECK DIR QDir dir = QDir::current(); if (!dir.exists("music")) if(!dir.mkdir("music")) QMessageBox::warning(this, tr("Dance class"), tr("Cannot create directory 'music'.")); model = new QFileSystemModel; model->setRootPath(dir.absoluteFilePath("music")); update_tree(); connect(ui->treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(check_choice(QModelIndex))); connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(add_choice(QModelIndex))); connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(create_music())); connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject())); }
void CreateDialog::_text_changed(String p_text) { update_tree(); }
void SceneTreeEditor::_subscene_option(int p_idx) { Object *obj = ObjectDB::get_instance(instance_node); if (!obj) return; Node *node = obj->cast_to<Node>(); if (!node) return; switch(p_idx) { case SCENE_MENU_EDITABLE_CHILDREN: { bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node); editable = !editable; //node->set_instance_children_editable(editable); EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node,editable); instance_menu->set_item_checked(0,editable); if (editable) { node->set_scene_instance_load_placeholder(false); instance_menu->set_item_checked(1,false); } _update_tree(); } break; case SCENE_MENU_USE_PLACEHOLDER: { bool placeholder = node->get_scene_instance_load_placeholder(); placeholder = !placeholder; //node->set_instance_children_editable(editable); if (placeholder) { EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node,false); } node->set_scene_instance_load_placeholder(placeholder); instance_menu->set_item_checked(0,false); instance_menu->set_item_checked(1,placeholder); _update_tree(); } break; case SCENE_MENU_OPEN: { emit_signal("open",node->get_filename()); } break; case SCENE_MENU_CLEAR_INHERITANCE: { clear_inherit_confirm->popup_centered_minsize(); } break; case SCENE_MENU_CLEAR_INSTANCING: { Node*root=EditorNode::get_singleton()->get_edited_scene(); if (!root) break; ERR_FAIL_COND(node->get_filename()==String()); undo_redo->create_action("Discard Instancing"); undo_redo->add_do_method(node,"set_filename",""); undo_redo->add_undo_method(node,"set_filename",node->get_filename()); _node_replace_owner(node,node,root); undo_redo->add_do_method(this,"update_tree"); undo_redo->add_undo_method(this,"update_tree"); undo_redo->commit_action(); } break; case SCENE_MENU_OPEN_INHERITED: { if (node && node->get_scene_inherited_state().is_valid()) { emit_signal("open",node->get_scene_inherited_state()->get_path()); } } break; case SCENE_MENU_CLEAR_INHERITANCE_CONFIRM: { if (node && node->get_scene_inherited_state().is_valid()) { node->set_scene_inherited_state(Ref<SceneState>()); update_tree(); EditorNode::get_singleton()->get_property_editor()->update_tree(); } } break; } }
/* ********************************************************************* Builds a new tree ******************************************************************* */ void UniformCostSearch::build(ALEState & state) { assert(p_root == NULL); p_root = new TreeNode(NULL, state, NULL, UNDEFINED, 0); update_tree(); is_built = true; }
/* * Update the tree by adding/removing entries * Does not change other nodes */ static void update_tree(struct menu *src, GtkTreeIter * dst) { struct menu *child1; GtkTreeIter iter, tmp; GtkTreeIter *child2 = &iter; gboolean valid; GtkTreeIter *sibling; struct symbol *sym; struct property *prop; struct menu *menu1, *menu2; if (src == &rootmenu) indent = 1; valid = gtk_tree_model_iter_children(model2, child2, dst); for (child1 = src->list; child1; child1 = child1->next) { prop = child1->prompt; sym = child1->sym; reparse: menu1 = child1; if (valid) gtk_tree_model_get(model2, child2, COL_MENU, &menu2, -1); else menu2 = NULL; // force adding of a first child #ifdef DEBUG printf("%*c%s | %s\n", indent, ' ', menu1 ? menu_get_prompt(menu1) : "nil", menu2 ? menu_get_prompt(menu2) : "nil"); #endif if (!menu_is_visible(child1) && !show_all) { // remove node if (gtktree_iter_find_node(dst, menu1) != NULL) { memcpy(&tmp, child2, sizeof(GtkTreeIter)); valid = gtk_tree_model_iter_next(model2, child2); gtk_tree_store_remove(tree2, &tmp); if (!valid) return; // next parent else goto reparse; // next child } else continue; } if (menu1 != menu2) { if (gtktree_iter_find_node(dst, menu1) == NULL) { // add node if (!valid && !menu2) sibling = NULL; else sibling = child2; gtk_tree_store_insert_before(tree2, child2, dst, sibling); set_node(child2, menu1, fill_row(menu1)); if (menu2 == NULL) valid = TRUE; } else { // remove node memcpy(&tmp, child2, sizeof(GtkTreeIter)); valid = gtk_tree_model_iter_next(model2, child2); gtk_tree_store_remove(tree2, &tmp); if (!valid) return; // next parent else goto reparse; // next child } } else if (sym && (sym->flags & SYMBOL_CHANGED)) { set_node(child2, menu1, fill_row(menu1)); } indent++; update_tree(child1, child2); indent--; valid = gtk_tree_model_iter_next(model2, child2); } }
void append_node(char* window, struct node *tree, int *root, struct param p) { int start = p.start; int end = p.end; uint32_t n = p.n; uint16_t l = p.l; int offset = p.offset; int index; int w_pos; int w_offset; int diff_offset; int tmp_node; int tmp_father; int cmp = 0; w_offset = (offset % n); /* The tree is empty, append the ROOT node */ if(*root == UNUSED) { *root = w_offset; tree[w_offset].pos = offset; tree[w_offset].father = UNUSED; tree[w_offset].left = UNUSED; tree[w_offset].right = UNUSED; return; } /* The tree vector is full, delete the node with the 'w_offset' index and reorder the tree */ if(tree[w_offset].father != FREE) { update_tree(tree, root, w_offset); } /* Start from the ROOT */ index = *root; while(1) { diff_offset = offset - tree[index].pos; /* Properly set the window offset */ if(offset <= n) { w_pos = tree[index].pos; } else { w_pos = n - diff_offset; } cmp = memcmp(window + start + w_pos, window + end, l); /* Found the same node, so replace the node */ if(cmp == 0) { /* The found node is the ROOT node, so set the ROOT parameters */ if(index == *root) { *root = w_offset; tree[w_offset].father = UNUSED; } /* Set the node (that is not the ROOT node) parameters */ else { tmp_father = tree[index].father; if(tree[tmp_father].left == index) { tree[tmp_father].left = w_offset; // left son } else { tree[tmp_father].right = w_offset; // right son } } /********************************************** Replace the index node with the w_offset node **********************************************/ /* Update the hierarchy: father -> son */ if(tree[index].left != UNUSED) { tmp_node = tree[index].left; // left son tree[tmp_node].father = w_offset; } if(tree[index].right != UNUSED) { tmp_node = tree[index].right; // right son tree[tmp_node].father = w_offset; } /* Overwrite the replaced node */ tree[index].father = FREE; tree[w_offset] = tree[index]; tree[w_offset].pos = offset; break; } /* The new one is smaller than the current node..."enjoy the LEFT side of the tree, Luke!" */ if(cmp > 0) { tmp_father = index; index = tree[tmp_father].left; } /* The new one is greater than the current one..."enjoy the RIGHT side of the tree, Luke!" */ else { tmp_father = index; index = tree[tmp_father].right; } /* End of the tree, append the new node and update the parameters */ if(index == UNUSED) { tree[w_offset].pos = offset; tree[w_offset].father = tmp_father; tree[w_offset].left = UNUSED; tree[w_offset].right = UNUSED; if(cmp > 0) { tree[tmp_father].left = w_offset; // left son } else { tree[tmp_father].right = w_offset; // right son } break; } } }
void on_show_debug_info1_activate(GtkMenuItem * menuitem, gpointer user_data) { show_debug = GTK_CHECK_MENU_ITEM(menuitem)->active; update_tree(&rootmenu, NULL); }
void update_tree (int L,int R,T value,Func f) { update_tree (1,0,size-1,L,R,value,f); }
// [[register]] SEXP mcmcbas(SEXP Y, SEXP X, SEXP Rweights, SEXP Rprobinit, SEXP Rmodeldim, SEXP incint, SEXP Ralpha,SEXP method, SEXP modelprior, SEXP Rupdate, SEXP Rbestmodel, SEXP plocal, SEXP BURNIN_Iterations, SEXP MCMC_Iterations, SEXP LAMBDA, SEXP DELTA, SEXP Rparents) { SEXP RXwork = PROTECT(duplicate(X)), RYwork = PROTECT(duplicate(Y)); int nProtected = 2, nUnique=0, newmodel=0; int nModels=LENGTH(Rmodeldim); // Rprintf("Allocating Space for %d Models\n", nModels) ; SEXP ANS = PROTECT(allocVector(VECSXP, 15)); ++nProtected; SEXP ANS_names = PROTECT(allocVector(STRSXP, 15)); ++nProtected; SEXP Rprobs = PROTECT(duplicate(Rprobinit)); ++nProtected; SEXP MCMCprobs= PROTECT(duplicate(Rprobinit)); ++nProtected; SEXP R2 = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP shrinkage = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP modelspace = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP modeldim = PROTECT(duplicate(Rmodeldim)); ++nProtected; SEXP counts = PROTECT(duplicate(Rmodeldim)); ++nProtected; SEXP beta = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP se = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP mse = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP modelprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP priorprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP logmarg = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP sampleprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP NumUnique = PROTECT(allocVector(INTSXP, 1)); ++nProtected; SEXP Rse_m = NULL, Rcoef_m = NULL, Rmodel_m; double *Xwork, *Ywork, *wts, *coefficients,*probs, shrinkage_m, *MCMC_probs, SSY, yty, mse_m, *se_m, MH=0.0, prior_m=1.0, *real_model, R2_m, RSquareFull, alpha, prone, denom, logmargy, postold, postnew; int nobs, p, k, i, j, m, n, l, pmodel, pmodel_old, *xdims, *model_m, *bestmodel, *varin, *varout; int mcurrent, update, n_sure; double mod, rem, problocal, *pigamma, eps, *hyper_parameters; double *XtX, *XtY, *XtXwork, *XtYwork, *SSgam, *Cov, *priorCov, *marg_probs; double lambda, delta, one=1.0; int inc=1; int *model, *modelold, bit, *modelwork, old_loc, new_loc; // char uplo[] = "U", trans[]="T"; struct Var *vars; /* Info about the model variables. */ NODEPTR tree, branch; /* get dimsensions of all variables */ nobs = LENGTH(Y); xdims = INTEGER(getAttrib(X,R_DimSymbol)); p = xdims[1]; k = LENGTH(modelprobs); update = INTEGER(Rupdate)[0]; lambda=REAL(LAMBDA)[0]; delta = REAL(DELTA)[0]; // Rprintf("delta %f lambda %f", delta, lambda); eps = DBL_EPSILON; problocal = REAL(plocal)[0]; // Rprintf("Update %i and prob.switch %f\n", update, problocal); /* Extract prior on models */ hyper_parameters = REAL(getListElement(modelprior,"hyper.parameters")); /* Rprintf("n %d p %d \n", nobs, p); */ Ywork = REAL(RYwork); Xwork = REAL(RXwork); wts = REAL(Rweights); /* Allocate other variables. */ PrecomputeData(Xwork, Ywork, wts, &XtXwork, &XtYwork, &XtX, &XtY, &yty, &SSY, p, nobs); alpha = REAL(Ralpha)[0]; vars = (struct Var *) R_alloc(p, sizeof(struct Var)); probs = REAL(Rprobs); n = sortvars(vars, probs, p); for (i =n; i <p; i++) REAL(MCMCprobs)[vars[i].index] = probs[vars[i].index]; for (i =0; i <n; i++) REAL(MCMCprobs)[vars[i].index] = 0.0; MCMC_probs = REAL(MCMCprobs); pigamma = vecalloc(p); real_model = vecalloc(n); marg_probs = vecalloc(n); modelold = ivecalloc(p); model = ivecalloc(p); modelwork= ivecalloc(p); varin= ivecalloc(p); varout= ivecalloc(p); /* create gamma gamma' matrix */ SSgam = (double *) R_alloc(n * n, sizeof(double)); Cov = (double *) R_alloc(n * n, sizeof(double)); priorCov = (double *) R_alloc(n * n, sizeof(double)); for (j=0; j < n; j++) { for (i = 0; i < n; i++) { SSgam[j*n + i] = 0.0; Cov[j*n + i] = 0.0; priorCov[j*n + i] = 0.0; if (j == i) priorCov[j*n + i] = lambda; } marg_probs[i] = 0.0; } RSquareFull = CalculateRSquareFull(XtY, XtX, XtXwork, XtYwork, Rcoef_m, Rse_m, p, nobs, yty, SSY); /* fill in the sure things */ for (i = n, n_sure = 0; i < p; i++) { model[vars[i].index] = (int) vars[i].prob; if (model[vars[i].index] == 1) ++n_sure; } GetRNGstate(); tree = make_node(-1.0); /* Rprintf("For m=0, Initialize Tree with initial Model\n"); */ m = 0; bestmodel = INTEGER(Rbestmodel); INTEGER(modeldim)[m] = n_sure; /* Rprintf("Create Tree\n"); */ branch = tree; for (i = 0; i< n; i++) { bit = bestmodel[vars[i].index]; if (bit == 1) { if (i < n-1 && branch->one == NULL) branch->one = make_node(-1.0); if (i == n-1 && branch->one == NULL) branch->one = make_node(0.0); branch = branch->one; } else { if (i < n-1 && branch->zero == NULL) branch->zero = make_node(-1.0); if (i == n-1 && branch->zero == NULL) branch->zero = make_node(0.0); branch = branch->zero; } model[vars[i].index] = bit; INTEGER(modeldim)[m] += bit; branch->where = 0; } /* Rprintf("Now get model specific calculations \n"); */ pmodel = INTEGER(modeldim)[m]; PROTECT(Rmodel_m = allocVector(INTSXP,pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } SET_ELEMENT(modelspace, m, Rmodel_m); Rcoef_m = NEW_NUMERIC(pmodel); PROTECT(Rcoef_m); Rse_m = NEW_NUMERIC(pmodel); PROTECT(Rse_m); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } R2_m = 0.0; mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; SET_ELEMENT(beta, m, Rcoef_m); SET_ELEMENT(se, m, Rse_m); REAL(R2)[m] = R2_m; REAL(mse)[m] = mse_m; gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); REAL(sampleprobs)[m] = 1.0; REAL(logmarg)[m] = logmargy; REAL(shrinkage)[m] = shrinkage_m; prior_m = compute_prior_probs(model,pmodel,p, modelprior); REAL(priorprobs)[m] = prior_m; UNPROTECT(3); old_loc = 0; pmodel_old = pmodel; nUnique=1; INTEGER(counts)[0] = 0; postold = REAL(logmarg)[m] + log(REAL(priorprobs)[m]); memcpy(modelold, model, sizeof(int)*p); /* Rprintf("model %d max logmarg %lf\n", m, REAL(logmarg)[m]); */ /* Rprintf("Now Sample the Rest of the Models \n"); */ m = 0; while (nUnique < k && m < INTEGER(BURNIN_Iterations)[0]) { memcpy(model, modelold, sizeof(int)*p); pmodel = n_sure; MH = 1.0; if (pmodel_old == n_sure || pmodel_old == n_sure + n){ MH = random_walk(model, vars, n); MH = 1.0 - problocal; } else { if (unif_rand() < problocal) { // random MH = random_switch(model, vars, n, pmodel_old, varin, varout ); } else { // Randomw walk proposal flip bit// MH = random_walk(model, vars, n); } } branch = tree; newmodel= 0; for (i = 0; i< n; i++) { bit = model[vars[i].index]; if (bit == 1) { if (branch->one != NULL) branch = branch->one; else newmodel = 1; } else { if (branch->zero != NULL) branch = branch->zero; else newmodel = 1.0; } pmodel += bit; } if (pmodel == n_sure || pmodel == n + n_sure) MH = 1.0/(1.0 - problocal); if (newmodel == 1) { new_loc = nUnique; PROTECT(Rmodel_m = allocVector(INTSXP,pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } Rcoef_m = NEW_NUMERIC(pmodel); PROTECT(Rcoef_m); Rse_m = NEW_NUMERIC(pmodel); PROTECT(Rse_m); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } R2_m = 0.0; mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; prior_m = compute_prior_probs(model,pmodel,p, modelprior); gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); postnew = logmargy + log(prior_m); } else { new_loc = branch->where; postnew = REAL(logmarg)[new_loc] + log(REAL(priorprobs)[new_loc]); } MH *= exp(postnew - postold); // Rprintf("MH new %lf old %lf\n", postnew, postold); if (unif_rand() < MH) { if (newmodel == 1) { new_loc = nUnique; insert_model_tree(tree, vars, n, model, nUnique); INTEGER(modeldim)[nUnique] = pmodel; SET_ELEMENT(modelspace, nUnique, Rmodel_m); SET_ELEMENT(beta, nUnique, Rcoef_m); SET_ELEMENT(se, nUnique, Rse_m); REAL(R2)[nUnique] = R2_m; REAL(mse)[nUnique] = mse_m; REAL(sampleprobs)[nUnique] = 1.0; REAL(logmarg)[nUnique] = logmargy; REAL(shrinkage)[nUnique] = shrinkage_m; REAL(priorprobs)[nUnique] = prior_m; UNPROTECT(3); ++nUnique; } old_loc = new_loc; postold = postnew; pmodel_old = pmodel; memcpy(modelold, model, sizeof(int)*p); } else { if (newmodel == 1) UNPROTECT(3); } INTEGER(counts)[old_loc] += 1; for (i = 0; i < n; i++) { /* store in opposite order so nth variable is first */ real_model[n-1-i] = (double) modelold[vars[i].index]; REAL(MCMCprobs)[vars[i].index] += (double) modelold[vars[i].index]; } // Update SSgam = gamma gamma^T + SSgam F77_NAME(dsyr)("U", &n, &one, &real_model[0], &inc, &SSgam[0], &n); m++; } for (i = 0; i < n; i++) { REAL(MCMCprobs)[vars[i].index] /= (double) m; } // Rprintf("\n%d \n", nUnique); // Compute marginal probabilities mcurrent = nUnique; compute_modelprobs(modelprobs, logmarg, priorprobs,mcurrent); compute_margprobs(modelspace, modeldim, modelprobs, probs, mcurrent, p); // Now sample W/O Replacement // Rprintf("NumUnique Models Accepted %d \n", nUnique); INTEGER(NumUnique)[0] = nUnique; if (nUnique < k) { update_probs(probs, vars, mcurrent, k, p); update_tree(modelspace, tree, modeldim, vars, k,p,n,mcurrent, modelwork); for (m = nUnique; m < k; m++) { for (i = n; i < p; i++) { INTEGER(modeldim)[m] += model[vars[i].index]; } branch = tree; for (i = 0; i< n; i++) { pigamma[i] = 1.0; bit = withprob(branch->prob); /* branch->done += 1; */ if (bit == 1) { for (j=0; j<=i; j++) pigamma[j] *= branch->prob; if (i < n-1 && branch->one == NULL) branch->one = make_node(vars[i+1].prob); if (i == n-1 && branch->one == NULL) branch->one = make_node(0.0); branch = branch->one; } else { for (j=0; j<=i; j++) pigamma[j] *= (1.0 - branch->prob); if (i < n-1 && branch->zero == NULL) branch->zero = make_node(vars[i+1].prob); if (i == n-1 && branch->zero == NULL) branch->zero = make_node(0.0); branch = branch->zero; } model[vars[i].index] = bit; INTEGER(modeldim)[m] += bit; } REAL(sampleprobs)[m] = pigamma[0]; pmodel = INTEGER(modeldim)[m]; /* Now subtract off the visited probability mass. */ branch=tree; for (i = 0; i < n; i++) { bit = model[vars[i].index]; prone = branch->prob; if (bit == 1) prone -= pigamma[i]; denom = 1.0 - pigamma[i]; if (denom <= 0.0) { if (denom < 0.0) { warning("neg denominator %le %le %le !!!\n", pigamma, denom, prone); if (branch->prob < 0.0 && branch->prob < 1.0) warning("non extreme %le\n", branch->prob);} denom = 0.0;} else { if (prone <= 0) prone = 0.0; if (prone > denom) { if (prone <= eps) prone = 0.0; else prone = 1.0; /* Rprintf("prone > 1 %le %le %le %le !!!\n", pigamma, denom, prone, eps);*/ } else prone = prone/denom; } if (prone > 1.0 || prone < 0.0) Rprintf("%d %d Probability > 1!!! %le %le %le %le \n", m, i, prone, branch->prob, denom, pigamma); /* if (bit == 1) pigamma /= (branch->prob); else pigamma /= (1.0 - branch->prob); if (pigamma > 1.0) pigamma = 1.0; */ branch->prob = prone; if (bit == 1) branch = branch->one; else branch = branch->zero; /* Rprintf("%d %d \n", branch->done, n - i); */ /* if (log((double) branch->done) < (n - i)*log(2.0)) { if (bit == 1) branch = branch->one; else branch = branch->zero; } else { branch->one = NULL; branch->zero = NULL; break; } */ } /* Now get model specific calculations */ PROTECT(Rmodel_m = allocVector(INTSXP, pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } SET_ELEMENT(modelspace, m, Rmodel_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } PROTECT(Rcoef_m = allocVector(REALSXP,pmodel)); PROTECT(Rse_m = allocVector(REALSXP,pmodel)); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); /* olsreg(Ywork, Xwork, coefficients, se_m, &mse_m, &pmodel, &nobs, pivot,qraux,work,residuals,effects,v,betaols); */ if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; SET_ELEMENT(beta, m, Rcoef_m); SET_ELEMENT(se, m, Rse_m); REAL(R2)[m] = R2_m; REAL(mse)[m] = mse_m; gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); REAL(logmarg)[m] = logmargy; REAL(shrinkage)[m] = shrinkage_m; REAL(priorprobs)[m] = compute_prior_probs(model,pmodel,p, modelprior); if (m > 1) { rem = modf((double) m/(double) update, &mod); if (rem == 0.0) { mcurrent = m; compute_modelprobs(modelprobs, logmarg, priorprobs,mcurrent); compute_margprobs(modelspace, modeldim, modelprobs, probs, mcurrent, p); if (update_probs(probs, vars, mcurrent, k, p) == 1) { // Rprintf("Updating Model Tree %d \n", m); update_tree(modelspace, tree, modeldim, vars, k,p,n,mcurrent, modelwork); } }} UNPROTECT(3); } } compute_modelprobs(modelprobs, logmarg, priorprobs,k); compute_margprobs(modelspace, modeldim, modelprobs, probs, k, p); SET_VECTOR_ELT(ANS, 0, Rprobs); SET_STRING_ELT(ANS_names, 0, mkChar("probne0")); SET_VECTOR_ELT(ANS, 1, modelspace); SET_STRING_ELT(ANS_names, 1, mkChar("which")); SET_VECTOR_ELT(ANS, 2, logmarg); SET_STRING_ELT(ANS_names, 2, mkChar("logmarg")); SET_VECTOR_ELT(ANS, 3, modelprobs); SET_STRING_ELT(ANS_names, 3, mkChar("postprobs")); SET_VECTOR_ELT(ANS, 4, priorprobs); SET_STRING_ELT(ANS_names, 4, mkChar("priorprobs")); SET_VECTOR_ELT(ANS, 5,sampleprobs); SET_STRING_ELT(ANS_names, 5, mkChar("sampleprobs")); SET_VECTOR_ELT(ANS, 6, mse); SET_STRING_ELT(ANS_names, 6, mkChar("mse")); SET_VECTOR_ELT(ANS, 7, beta); SET_STRING_ELT(ANS_names, 7, mkChar("mle")); SET_VECTOR_ELT(ANS, 8, se); SET_STRING_ELT(ANS_names, 8, mkChar("mle.se")); SET_VECTOR_ELT(ANS, 9, shrinkage); SET_STRING_ELT(ANS_names, 9, mkChar("shrinkage")); SET_VECTOR_ELT(ANS, 10, modeldim); SET_STRING_ELT(ANS_names, 10, mkChar("size")); SET_VECTOR_ELT(ANS, 11, R2); SET_STRING_ELT(ANS_names, 11, mkChar("R2")); SET_VECTOR_ELT(ANS, 12, counts); SET_STRING_ELT(ANS_names, 12, mkChar("freq")); SET_VECTOR_ELT(ANS, 13, MCMCprobs); SET_STRING_ELT(ANS_names, 13, mkChar("probs.MCMC")); SET_VECTOR_ELT(ANS, 14, NumUnique); SET_STRING_ELT(ANS_names, 14, mkChar("n.Unique")); setAttrib(ANS, R_NamesSymbol, ANS_names); UNPROTECT(nProtected); // Rprintf("Return\n"); PutRNGstate(); return(ANS); }
void GroupsEditor::set_current(Node* p_node) { node=p_node; update_tree(); }