Exemplo n.º 1
0
FrameIDs FileConstHandle::get_root_frames() const {
  FrameIDs ret;
  RMF_FOREACH(FrameID fr, get_frames()) {
    if (get_parents(fr).empty()) ret.push_back(fr);
  }
  return ret;
}
Exemplo n.º 2
0
void
parasite_widget_tree_select_widget(ParasiteWidgetTree *widget_tree,
                                   GtkWidget *widget)
{
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget_tree));
    GList *parents = get_parents(widget, NULL);
    GList *l;
    GtkTreeIter iter, parent_iter = {0};
    gboolean found = FALSE;
    gboolean in_root = TRUE;

    for (l = parents; l != NULL; l = l->next)
    {
        GtkWidget *cur_widget = GTK_WIDGET(l->data);
        gboolean valid;
        found = FALSE;

        for (valid = gtk_tree_model_iter_children(model, &iter,
                                                  in_root ? NULL
                                                  : &parent_iter);
              valid;
              valid = gtk_tree_model_iter_next(model, &iter))
        {
            GtkWidget *iter_widget;

            gtk_tree_model_get(model, &iter,
                               WIDGET, &iter_widget,
                               -1);

            if (iter_widget == cur_widget)
            {
                parent_iter = iter;
                in_root = FALSE;
                found = TRUE;
                break;
            }
        }

        if (!found)
        {
            /* No good. Bail.. */
            break;
        }
    }

    if (found)
    {
        GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
        gtk_tree_view_expand_to_path(GTK_TREE_VIEW(widget_tree), path);
        gtk_tree_selection_select_iter(
            gtk_tree_view_get_selection(GTK_TREE_VIEW(widget_tree)),
            &iter);
        gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(widget_tree), path, NULL,
                                     FALSE, 0, 0);
    }

    g_list_free(parents);
}
Exemplo n.º 3
0
/* traverse all nodes
 * find inconsitency markers for each child depending on its parents
 *   inconsistency is found whenever:
 *   edge pa --> ch
 *        gammax[pa,t_i] == 1 # if the parent is active, then state switch from active to passive should not occur at the child
 *                      gammax[ch, t_i] == 1 & gammax[ch, t_(i+1)] == 0 not possible
 *        gammax[pa,t_i] == 0 # if the parent is passive, then state switch from passive to active should not occur at the child
 *                      gammax[ch, t_i] == 0 & gammax[ch, t_(i+1)] == 1 not possible
 *   edge pa --| ch
 *        gammax[pa,t_i] == 1 # if parent is active, then state switch from passive to active should not occur
 *                      gammax[ch, t_i] == 0 & gammax[ch, t_(i+1)] == 1 not possible
 *        gammax[pa,t_i] == 0
 *                      gammax[ch, t_i] == 1 & gammax[ch, t_(i+1)] == 0 not possible
 *
 */
int is_consistent(int *phi, int *GS, int *G, int N, int T, int R)
{
	int inc=0,
			cnt=0,
			pcnt=0;

	// initialise an array of parents, can be at most N elements
	int *parents = malloc(N*sizeof(int));

	// allocate consistency vector
	int *consvec = malloc((T*R-1)*sizeof(int));

	// for all nodes
	for(int i=0; i!=N; ++i) {

		// reset parents vector to 0
		memset(parents, 0, N*sizeof(int));

		// reset the consistency vector to all consistent
		for(int ci=0; ci!=(T*R-1); ++ci) {
			consvec[ci] = 1;
		}

		// check all parents
		pcnt = get_parents(phi, N, parents, i);

		if(pcnt>0) {
			// for each parent
			for(int j=0; j!=N; ++j) {
				if(parents[j]!=0) {
					checkC(phi, GS, i, j, N, T, R, consvec);
					// reset -14 value to 1
					for(int ci=0; ci!=(T*R-1); ci++) {
						if(consvec[ci]==-14) {
							consvec[ci] = 1;
						}
					}
				}
			} // parents end
			// increment the inconsistency count
			for(int ci=0; ci!=(T*R-1); ++ci) {
				if(consvec[ci]==0) {
					inc++;
				}
			}
		}

	}
	free(consvec);
	free(parents);
	return inc;
}
Exemplo n.º 4
0
static GList *
get_parents(GtkWidget *widget,
            GList *parents)
{
    GtkWidget *parent = gtk_widget_get_parent(widget);

    parents = g_list_prepend(parents, widget);

    if (parent != NULL)
        return get_parents(parent, parents);

    return parents;
}
/* PUBLIC */
void sb_write_clause_jmap(String_buf sb, Topform c,
			  int format,
			  I3list map)
{
  Term t;
  if (c->compressed)
    t = NULL;
  else
    t = topform_to_term(c);
  
  if (format == CL_FORM_BARE) {
    if (t == NULL)
      sb_append(sb, "clause_is_compressed");
    else
      sb_write_term(sb, t);
    sb_append(sb, ".");
  }
  else {
    if (c->id != 0) {
      sb_append_id(sb, c->id, map);
      sb_append(sb, " ");
    }

    if (t == NULL)
      sb_append(sb, "clause_is_compressed");
    else
      sb_write_term(sb, t);
    sb_append(sb, ".  ");
    if (format == CL_FORM_STD)
      sb_write_just(sb, c->justification, map);
    else {
      /* CL_FORM_PARENTS */
      Ilist parents = get_parents(c->justification, TRUE);
      Ilist p;
      sb_append(sb, "[");
      for (p = parents; p; p = p->next) {
	sb_append_id(sb, p->i, map);
	if (p->next)
	  sb_append(sb, ",");
      }
      sb_append(sb, "].");
    }
  }
  sb_append(sb, "\n");
  if (t)
    zap_term(t);
}  /* sb_write_clause_jmap */
Exemplo n.º 6
0
void train_once_KernelPerceptronModel(KernelPerceptron mdl, const CoNLLCorpus corpus, int max_rec) {
    long match = 0, total = 0;
    //size_t slen=0;

    double s_initial = dsecnd();
    int max_sv = 0;


    log_info("Total number of training instances %d", (max_rec == -1) ? DArray_count(corpus->sentences) : max_rec);

    for (int si = 0; si < ((max_rec == -1) ? DArray_count(corpus->sentences) : max_rec); si++) {

        FeaturedSentence sent = (FeaturedSentence) DArray_get(corpus->sentences, si);

        debug("Building feature matrix for sentence %d", si);
        set_FeatureMatrix(NULL, corpus, si);

        set_adjacency_matrix_fast(corpus, si, mdl, false);

        max_sv += (sent->length + 1) * sent->length - sent->length;

        int *model = parse(sent);

        //printfarch(model, sent->length);
        debug("Parsing sentence %d of length %d is done", si, sent->length);
        int *empirical = get_parents(sent);

        //printfarch(empirical, sent->length);
        int nm = nmatch(model, empirical, sent->length);

        debug("Model matches %d arcs out of %d arcs", nm, sent->length);
        if (nm != sent->length) { // root has no valid parent.
            log_info("Sentence %d (section %d) of length %d (%d arcs out of %d arcs are correct)", si, sent->section, sent->length, nm, sent->length);

            int sentence_length = sent->length;
            for (int to = 1; to <= sentence_length; to++) {

                if (model[to] != empirical[to]) {

                    update_alpha(mdl, si, model[to], to, sent, -1);

                    update_alpha(mdl, si, empirical[to], to, sent, +1);
                }


            }
        } else {
            log_info("Sentence %d (section %d) of length %d (Perfect parse)", si, sent->section, sent->length);
        }

        size_t nsuccess;
        if (budget_method == RANDOMIZED) {
            if (mdl->M > budget_target) {
                size_t nbefore = mdl->M;
                size_t nasked = nbefore - budget_target;
                nsuccess = delete_n_random_hypothesis(mdl, nasked);

                log_info("%lu vectors deleted (%lu asked). Current hypothesis set size reduced from %lu to %lu", nsuccess, nasked, nbefore, mdl->M);
            }
        }

        mdl->c++;

        free_feature_matrix(corpus, si);

        match += nm;
        total += (sent->length);


        if ((si + 1) % 1000 == 0 && si != 0) {
            log_info("Running training accuracy %lf after %d sentence.", (match * 1.) / total, si + 1);

            unsigned nsv = mdl->M;
            log_info("%u (%f of total %d) support vectors", nsv, (nsv * 1.) / max_sv, max_sv);
        }

        free(model);
        free(empirical);
    }

    unsigned nsv = mdl->M;
    log_info("Running training accuracy %lf", (match * 1.) / total);
    log_info("%u (%f of total %d) support vectors", nsv, (nsv * 1.) / max_sv, max_sv);

    if (verbosity > 0) {

        dump_support_vectors(mdl);



    }

    update_average_alpha(mdl);

    return;
}
Exemplo n.º 7
0
CubitStatus ModelEntity::remove_from_DAG(CubitBoolean recurse_flag)
{
     // This counter will be used in the recursion to test whether
     // the call is from outside or from the function itself. When
     // the call comes from outside, the counter should always be
     // zero.

  CubitBoolean this_recurse = recurse_flag;
  if (recurse_flag == CUBIT_FALSE) recurse_flag = CUBIT_TRUE;
  
  DLIList<ModelEntity*> childModEntList;
   
     // Check to see if there are no parents of this object. 
   if ( get_parents() == 0 )
   {
      if (this_recurse == CUBIT_FALSE)
      {
         // Since we are not recursing, this is a top-level entity.
         // Notify the static observers that a top-level entity is being
         // destructed.  This must be done before children are disconnected.
       CubitObservable *top_level = CAST_TO(this, CubitObservable);
       CubitObserver::notify_static_observers(top_level, TOP_LEVEL_ENTITY_DESTRUCTED);
      }

        // Go through all the children and remove their link to
        // the current object.
      
      ModelEntity* tempModEntPtr = NULL ;
      ModelEntity* childModEntPtr = NULL ;
      
      childModEntList.clean_out();
      disconnect_all_children(&childModEntList);
      
        // The following while conditional may not work...it depends on
        // what is_at_end does when you step over the end...CHECK THIS
      int i;
      for( i = 0 ; i < childModEntList.size() ; i++ )
      {
           // Get the next ModelEnti in the child list and make sure its
           // pointer to its parent is removed.
         tempModEntPtr = childModEntList.get_and_step();
          
           // Try remove() on the child ModEnt. If it comes back with
           // a success, then delete it.
         childModEntPtr = tempModEntPtr;
         
         if ( childModEntPtr->remove_from_DAG(recurse_flag) == CUBIT_SUCCESS )
         {
              // Now deactivate the child ModEnt
            childModEntPtr->deactivated(CUBIT_TRUE) ;

              // remove it from observables, just before we go to delete it
            CubitObservable *observable = CAST_TO(childModEntPtr, CubitObservable);
            if (observable) 
            {
              if( !observable->notify_observers( MODEL_ENTITY_DESTRUCTED ) )
                 return CUBIT_FAILURE;
            }
         }
      }
      
      
        // If this is the top of the recursion, then clean out all the deactivated
        // entities.
      if (this_recurse == CUBIT_FALSE)
      {
         this->deactivated(CUBIT_TRUE) ;

         // remove it from observables, just before we go to delete it
         CubitObservable *observable = CAST_TO(childModEntPtr, CubitObservable);
         if (observable) 
         {
           if( !observable->notify_observers( MODEL_ENTITY_DESTRUCTED ) )
              return CUBIT_FAILURE;

         }
         GeometryQueryTool::instance()->cleanout_deactivated_geometry() ;
      }
      
      return CUBIT_SUCCESS ;
   }
   else
   {
      return CUBIT_FAILURE ;
   }
}
Exemplo n.º 8
0
void train_once_PerceptronModel(PerceptronModel mdl, const CoNLLCorpus corpus, int max_rec) {
    long match = 0, total = 0;
    //size_t slen=0;

    log_info("Total number of training instances %d", (max_rec == -1) ? DArray_count(corpus->sentences) : max_rec);
    for (int si = 0; si < ((max_rec == -1) ? DArray_count(corpus->sentences) : max_rec); si++) {
        //log_info("Parsing sentence %d/%d", si+1, DArray_count(corpus));
        FeaturedSentence sent = (FeaturedSentence) DArray_get(corpus->sentences, si);


        start(&parser_rate);
        //debug("Building feature matrix for sentence %d of length %d", si, sent->length);
        //set_FeatureMatrix(NULL, corpus, si);


        //printfembedding(sent->feature_matrix, sent->length);

        debug("Building adjacency matrix for sentence %d of length %d", si, sent->length);
        build_adjacency_matrix(corpus, si, mdl->embedding_w, NULL);

        //printfmatrix(sent->adjacency_matrix, sent->length);

        //log_info("Adjacency matrix construction is done");


        int *model = parse(sent);
        stop(&parser_rate);
        debug("Parsing sentence %d is done", si);
        int *empirical = get_parents(sent);

        /*
        log_info("Model:");
        printfarch(model, sent->length);
        log_info("Empirical:");
        printfarch(empirical, sent->length);
         */

        int nm = nmatch(model, empirical, sent->length);
        debug("Model matches %d arcs out of %d arcs", nm, sent->length);
        if (nm != sent->length) { // root has no valid parent.

            if (corpus->disrete_patterns_parts) {

                log_info("I have discrete features");

                DArray* model_features = DArray_create(sizeof (uint32_t), 16);
                DArray* empirical_features = DArray_create(sizeof (uint32_t), 16);

                for (int fi = 1; fi < sent->length; fi++) {
                    fill_features(mdl->features->map, model_features, model[fi], fi, sent);

                    fill_features(mdl->features->map, empirical_features, empirical[fi], fi, sent);
                }

                for (int i = 0; i < DArray_count(model_features); i++) {
                    uint32_t *fidx = (uint32_t *) DArray_get(model_features, i);

                    mdl->discrete_w->data[*fidx] -= 1.0;
                    mdl->discrete_w_avg->data[*fidx] -= (mdl->c) * 1.0;
                    mdl->discrete_w_temp->data[*fidx] -= 1.0;

                }

                for (int i = 0; i < DArray_count(empirical_features); i++) {
                    uint32_t *fidx = (uint32_t *) DArray_get(empirical_features, i);

                    mdl->discrete_w->data[*fidx] += 1.0;
                    mdl->discrete_w_avg->data[*fidx] += (mdl->c) * 1.0;

                    mdl->discrete_w_temp->data[*fidx] += 1.0;
                }

                DArray_destroy(model_features);
                DArray_destroy(empirical_features);
            }

            for (int i = 1; i <= sent->length; i++) {

                if (model[i] != empirical[i]){
                    // -1 for Model arch
                    embedding_feature(sent, model[i], i, xformed_v);
                    vadd(mdl->embedding_w, xformed_v, -1.0);
                    vadd(mdl->embedding_w_temp, xformed_v, -1.0);
                    vadd(mdl->embedding_w_avg, xformed_v, -(mdl->c));

                    // +1 for Gold arc
                    embedding_feature(sent, empirical[i], i, xformed_v);

                    vadd(mdl->embedding_w, xformed_v, 1.0);
                    vadd(mdl->embedding_w_temp, xformed_v, 1.0);
                    vadd(mdl->embedding_w_avg, xformed_v, (mdl->c));
                }

                //free(real_embedding);
                //free(model_embedding);
            }
        }

        free_feature_matrix(corpus, si);

        mdl->c++;

        match += nm;
        total += (sent->length);

        if (si % 1000 == 0 && si > 0) {
            log_info("Running training accuracy %lf", (match * 1.) / total);

        }


        free(model);
        free(empirical);

        //free_sentence_structures(sent);
    }

    log_info("Running training accuracy %lf", (match * 1.) / total);

    if (corpus->disrete_patterns_parts) {
        for (int i = 0; i < mdl->n; i++) {
            //        mdl->w_avg[i] /= (numit * DArray_count(corpus));

            //mdl->w[i] -=(mdl->w_avg[i])/(mdl->c);

            mdl->discrete_w_temp->data[i] = mdl->discrete_w->data[i] - (mdl->discrete_w_avg->data[i]) / (mdl->c);
        }
    }

    //vadd(mdl->w_cont, mdl->w_cont_avg, -1./(mdl->c), SCODE_FEATURE_VECTOR_LENGTH);
    memcpy(mdl->embedding_w_temp->data, mdl->embedding_w->data, mdl->embedding_w->n * sizeof (float));
    vadd(mdl->embedding_w_temp, mdl->embedding_w_avg, -1. / (mdl->c));

    //    free(mdl->w);
    //    mdl->w = mdl->w_avg;

    //free_feature_matrix(sent);
}