예제 #1
0
static void check_propagation(test_bench_t *bench) {
  equality_queue_t *queue;
  ivector_t expl;
  uint32_t i, n;
  int32_t x, y;

  init_ivector(&expl, 10);

  queue = &bench->equeue;
  n = queue->top;
  for (i=0; i<n; i++) {
    x = queue->data[i].lhs;
    y = queue->data[i].rhs;
    if (x >= 0) {
      assert(y >= 0);

      offset_manager_explain_equality(&bench->manager, x, y, &expl);
      check_equality(bench, x, y, &expl);
      if (bench->show_details) {
	printf("Explanation for x%"PRId32" == x%"PRId32":\n", x, y);
	print_poly_def(bench->ptable, x);
	print_poly_def(bench->ptable, y);
	print_explanation(bench, &expl);
      }

      ivector_reset(&expl);
    }
  }

  delete_ivector(&expl);
}
/*
 * Run a test:
 * - show the constraint 
 * - check feasibility
 * - print the result
 */
static void run_test(int_constraint_t *cnstr) {
  ivector_t v;
  bool feasible;

  init_ivector(&v, 10);

  printf("Constraint: ");
  show_constraint(stdout, cnstr);
  show_fixed_vars(stdout, cnstr);

  feasible = int_constraint_is_feasible(cnstr, &v);
  if (feasible) {
    printf("Feasible\n");
  } else {
    printf("Not feasible\n");
    printf("conflict vars: ");
    show_varnames(stdout, v.data, v.size);
  }
  show_constraint_details(stdout, cnstr);

  if (feasible) {
    test_periods_and_phases(cnstr);
  }

  fflush(stdout);
  
  delete_ivector(&v);
}
예제 #3
0
/*
 * Initialization
 */
void init_flattener(flattener_t *flat, term_manager_t *mngr) {
  flat->terms = term_manager_get_terms(mngr);
  flat->manager = mngr;
  init_int_queue(&flat->queue, 0);
  init_int_hset(&flat->cache, 128);
  init_ivector(&flat->resu, 64);
}
/*
 * Test the period/phase computation
 */
static void test_periods_and_phases(int_constraint_t *cnstr) {
  ivector_t v;
  rational_t *p, *q;
  uint32_t i, n;
  int32_t x;

  init_ivector(&v, 10);

  n = int_constraint_num_terms(cnstr);
  for (i=0; i<n; i++) {
    ivector_reset(&v);
    int_constraint_period_of_var(cnstr, i, &v);
    x = int_constraint_get_var(cnstr, i);
    p = int_constraint_period(cnstr);
    q = int_constraint_phase(cnstr);
    printf("Variable %s: period = ", var[x].name);
    q_print(stdout, p);
    printf(", phase = ");
    q_print(stdout, q);
    printf("\n");
    printf("  antecedents: ");
    show_varnames(stdout, v.data, v.size);
    check_period_and_phase(cnstr, i, p, q);
  }

  delete_ivector(&v);
}
예제 #5
0
/*
 * Print model, including the aliased terms
 * - one line per term
 * - if model->has_alias is true, then the value of all terms in
 *   the alias table is displayed
 * - if model->has_alias is false, then this is the same as model_print
 */
void model_pp_full(yices_pp_t *printer, model_t *model) {
  evaluator_t eval;
  ivector_t v;
  term_t *a;
  uint32_t n;

  if (model->has_alias && model->alias_map != NULL) {
    init_evaluator(&eval, model);

    // collect all terms that have a value
    init_ivector(&v, 0);
    model_collect_terms(model, true, model->terms, term_to_print, &v);

    n = v.size;
    a = v.data;
    eval_pp_bool_assignments(printer, &eval, a, n);
    eval_pp_arithmetic_assignments(printer, &eval, a, n);
    eval_pp_bitvector_assignments(printer, &eval, a, n);
    eval_pp_constant_assignments(printer, &eval, a, n);
    eval_pp_tuple_assignments(printer, &eval, a, n);
    eval_pp_function_assignments(printer, &eval, a, n);
    vtbl_pp_queued_functions(printer, &model->vtbl, true);
    delete_evaluator(&eval);
    delete_ivector(&v);
  } else {
    model_pp(printer, model);
  }
}
예제 #6
0
/*
 * Check that s and c are equal
 */
static void check_equal_sets(bset_t *s, cset_t *c) {
  ivector_t aux;
  uint32_t i, n;

  n = s->card;
  for (i=0; i<n; i++) {
    if (! cset_member(c, s->data[i])) {
      goto bug;
    }
  }

  init_ivector(&aux, 10);
  cset_extract(c, &aux);
  n = aux.size;
  for (i=0; i<n; i++) {
    if (! bset_member(s, aux.data[i])) {
      goto bug;
    }
  }
  delete_ivector(&aux);
  return;

 bug:
  printf("*** BUG ***\n");
  printf(" bset: ");
  show_bset(s);
  printf("\n");
  printf(" cset: ");
  show_cset(c);
  printf("\n");
  printf("should be equal\b");
  fflush(stdout);
  exit(1);
}
예제 #7
0
/*
 * Undo assert_var/keep record poly
 * - stop on the top-most 'INCREASE_DLEVEL'
 */
static void op_stack_backtrack(test_bench_t *bench) {
  op_stack_t *stack;
  ivector_t saved_polys;
  uint32_t i;
  int32_t id;

  stack = &bench->stack;

  init_ivector(&saved_polys, 10);
  i = stack->top;
  for (;;) {
    assert(i > 0);
    i --;
    switch (stack->data[i].tag) {
    case RECORD_POLY:
      id = stack->data[i].arg.rec_id;
      ivector_push(&saved_polys, id);
      break;

    case ASSERT_EQ: // undo
    case PROPAGATE: // undo
      break;

    case INCREASE_DLEVEL:
      goto done;

    case PUSH:
    default:
      assert(false);
      break;
    }
  }

 done:
  stack->top = i;

  if (bench->conflict) {
    // check whether the conflict equality has been removed
    assert(bench->conflict_eq >= 0);
    if (i <= bench->conflict_eq) {
      bench->conflict_eq = -1;
      bench->conflict = false;
      bench->mngr_conflict = false;
      printf("---> Conflict resolved\n");
      fflush(stdout);
    }
  }


  // redo the record poly operations
  i = saved_polys.size;
  while (i > 0) {
    i --;
    id = saved_polys.data[i];
    push_record_poly(stack, id);
  }

  delete_ivector(&saved_polys);
}
예제 #8
0
void clause_db_construct(clause_db_t* db, const variable_db_t* var_db) {
  db->min_size = align(1);
  db->size = db->min_size;
  db->capacity = INITIAL_CLAUSE_DB_CAPACTIY;
  db->memory = safe_malloc(INITIAL_CLAUSE_DB_CAPACTIY);

  init_ivector(&db->clauses, 0);

  db->var_db = var_db;
}
예제 #9
0
static void test_propagate(test_bench_t *bench) {
  ivector_t expl;

  printf("[%"PRIu32"]: TEST_PROPAGATE: decision level = %"PRIu32", base level = %"PRIu32"\n", ctr, bench->decision_level, bench->base_level);
  push_propagate(&bench->stack);
  normalize_all(bench);

  if (bench->show_details) {
    printf("Active polys\n");
    print_active_polys(bench);
    printf("Assertions\n");
    print_all_equalities(bench);
    printf("Normal forms\n");
    print_normal_forms(bench);
  }

  if (bench->conflict) {
    printf("Expected result: conflict\n\n");
  } else {
    printf("Expected classes\n");
    print_expected_classes(bench);
  }

  if (offset_manager_propagate(&bench->manager)) {
    bench->mngr_conflict = false;
    printf("Propagated classes\n");
    print_infered_classes(bench);
    check_propagation(bench);
    check_all_propagated(bench);
    if (bench->conflict) {
      printf("BUG: conflict expected\n");
      fflush(stdout);
      exit(1);
    }

  } else {
    bench->mngr_conflict = true;
    printf("Conflict\n");
    init_ivector(&expl, 10);
    offset_manager_explain_conflict(&bench->manager, &expl);
    print_explanation(bench, &expl);
    check_conflict(bench, &expl);
    delete_ivector(&expl);

    if (! bench->conflict) {
      printf("BUG: conflict unexpected\n");
      fflush(stdout);
      exit(1);
    }
  }

  ctr ++;
}
예제 #10
0
/*
 * Allocate a new index i and initialize terms[i]
 */
static uint32_t type_store_alloc_index(type_store_t *store) {
  uint32_t i;

  i = store->ntypes;
  if (i == store->size) {
    extend_type_store(store);
  }
  assert(i < store->size);

  init_ivector(store->terms + i, 10);
  store->ntypes ++;

  return i;
}
예제 #11
0
static void show_cset(cset_t *c) {
  ivector_t aux;
  uint32_t i, n;

  init_ivector(&aux, 10);
  cset_extract(c, &aux);
  int_array_sort(aux.data, aux.size);
  printf("{");
  n = aux.size;
  for (i=0; i<n; i++) {
    printf(" %"PRIu32, aux.data[i]);
  }
  printf(" }");
  delete_ivector(&aux);
}
예제 #12
0
/*
 * Initialize solver:
 * - prob = problem descriptor
 * - logic/arch/parameters = for context initialization and check
 */
void init_ef_solver(ef_solver_t *solver, ef_prob_t *prob, smt_logic_t logic, context_arch_t arch) {
  uint32_t n;

  solver->prob = prob;
  solver->logic = logic;
  solver->arch = arch;
  solver->status = EF_STATUS_IDLE;
  solver->error_code = 0;

  solver->parameters = NULL;
  solver->option = EF_GEN_BY_SUBST_OPTION;
  solver->max_samples = 0;
  solver->max_iters = 0;
  solver->scan_idx = 0;

  solver->exists_context = NULL;
  solver->forall_context = NULL;
  solver->exists_model = NULL;

  n = ef_prob_num_evars(prob);
  assert(n <= UINT32_MAX/sizeof(term_t));
  solver->evalue = (term_t *) safe_malloc(n * sizeof(term_t));

  n = ef_prob_num_uvars(prob);
  assert(n <= UINT32_MAX/sizeof(term_t));
  solver->uvalue = (term_t *) safe_malloc(n * sizeof(term_t));


  solver->full_model = NULL;
  yices_init_term_vector(&solver->implicant);

  init_ivector(&solver->evalue_aux, 64);
  init_ivector(&solver->uvalue_aux, 64);
  init_ivector(&solver->all_vars, 64);
  init_ivector(&solver->all_values, 64);
}
예제 #13
0
/*
 * Print the model->map table
 */
void model_pp(yices_pp_t *printer, model_t *model) {
  ivector_t v;
  term_t *a;
  uint32_t n;

  init_ivector(&v, 0);
  model_collect_terms(model, false, model->terms, term_to_print, &v);

  n = v.size;
  a = v.data;
  model_pp_bool_assignments(printer, model, a, n);
  model_pp_arithmetic_assignments(printer, model, a, n);
  model_pp_bitvector_assignments(printer, model, a, n);
  model_pp_constant_assignments(printer, model, a, n);
  model_pp_tuple_assignments(printer, model, a, n);
  model_pp_function_assignments(printer, model, a, n);
  vtbl_pp_queued_functions(printer, &model->vtbl, true);
  delete_ivector(&v);
}
예제 #14
0
/*
 * Get explanation for t1 == t2
 */
static void test_eq_explanation(eterm_t t1, eterm_t t2) {
  ivector_t v;
  uint32_t i, n;
  int32_t x1, x2;

  init_ivector(&v, 10);

  x1 = var_of_term(t1);
  x2 = var_of_term(t2);
  offset_manager_explain_equality(&mngr, x1, x2, &v);

  printf("---> Explanation:");
  n = v.size;
  for (i=0; i<n; i++) {
    printf(" eq[%"PRId32"]", v.data[i]);
  }
  printf("\n\n");

  delete_ivector(&v);
}
예제 #15
0
파일: knnc.c 프로젝트: cran/EMV
int is_na(double *array,int* nb_col,int *miss_pos)
{
  int i;
  int count=0;
  
  init_ivector(miss_pos,nb_col,code_miss);
  
  for(i=0;i<*nb_col;i++)
    {
      if(array[i]==code_miss)
	{
	  miss_pos[count]=i;
	  count=count+1;
	}
    }
  if(count>0)
    return(1); /** one if missing's **/
  else
    return(0); /** 0 if no missing's **/
  
}
예제 #16
0
/*
 * Initialize manager m
 */
void init_epartition_manager(epartition_manager_t *m) {
    uint32_t i, n;

    n = EQABS_DEF_ESIZE;
    assert(n < EQABS_MAX_ESIZE);

    m->e_size = n;
    m->nterms = 0;
    m->label = (int32_t *) safe_malloc(n * sizeof(int32_t));
    m->next = (term_t *) safe_malloc(n * sizeof(term_t));

    // nothing stored: labels are all -1
    for (i=0; i<n; i++) {
        m->label[i] = -1;
    }

    n = EQABS_DEF_CSIZE;
    assert(n < EQABS_MAX_CSIZE);
    m->c_size = n;
    m->nclasses = 0;
    m->order = 0;
    m->root = (term_t *) safe_malloc(n * sizeof(term_t));

    n = EQABS_DEF_SCSIZE;
    assert(n < EQABS_MAX_SCSIZE);
    m->sc_size = n;
    m->subclass = (int32_t *) safe_malloc(n * sizeof(int32_t));

    // subclass[i] must be -1 for all i
    for (i=0; i<n; i++) {
        m->subclass[i] = -1;
    }

    init_ivector(&m->buffer, EQABS_BUFFER_SIZE);
    m->empty = alloc_epartition(0, 0);
}
예제 #17
0
파일: itegeppXXR.c 프로젝트: cran/HapEstXXR
void itegeppXXR(int *tog, double *lim, char **gent, double *qtrait,
int *xnp, double *likeres, char **freqres, char **hapres, char **desres)
{
  char      lino[10000], lin[10000];
  
  char* CharNull = "\0"; /* 06.11.2014/SKn */

  double    likold,
            pe, pex,                 /* 10.3. 2000 ROHDE */
	    *p2max,
            gsum;                    /* 10.3. 2000 ROHDE */
  int       i, inp, it, j, k, ki, kj, h, s, glev, non,
            ac[2],
	    drei,
	    null,
	    df = 0 /*SKn*/,
	    combinations,
	    nz,
	    iqual,
            nhap,
           *hlist,
	    **pimax,
            h1x, h2x;
  uint      iterations, h1, h2;
  bool      loop;

  // new for create design matrix (tog=0)
  double  pehh,
          *peh;
  

/* Max. 16 SNPs */
 if ( strlen(gent[0]) > 16 ) error ("Number of SNPs should smaller than 17.") ;

  np       = *xnp;
  len      =  (int) (strlen(gent[0]) + 1);

  mg       = ivector(np);
  merke    = ivector(np);
  nulmer   = ivector(np);
  ge       = ivector(np);
  hlist    = ivector(np);
  po       = uivector(len);

  geno     = cmatrix(np, len);
  max_prob = init_dvector(NULL, 0.0, np);
  prob	   = init_dvector(NULL, 0.0, np);


  hap	   = init_dvector (NULL,  0.0, Hapco);
  hc	   = init_ivector (NULL, -1,Hapco);

  po[0]=1;
  for(i=1;i<len;i++)po[i] = 2*po[i-1];
  combinations = po[len-1];


    init_dvector(hap,  0.0, Hapco);
    init_ivector (hc, -1,Hapco);

    ng = 0;

    /* read input data */
    for(inp=0;inp<np;inp++){
    drei = 0;
    null = 0;
    for (i=0; i<len-1; i++) {
    if(i < len-1 && (gent[inp][i] < 48 || gent[inp][i] > 51) ){
    Rprintf("%d %d %d\n",inp, i, gent[inp][i]);
    //Rprintf("\n Error in data person %d\n",inp+1); /* ROHDE 15.03.2000 */
    error("\n Error in data person %d\n",inp+1);;
    }
    if ( gent[inp][i] == '3' )  drei  ++;
    if ( gent[inp][i] == '0' )  null  ++;
    }
    gent[inp][len-1] = '\0';

    it = 1;
    for (i=0; i<ng; i++) {
    if ( strncmp (geno[i], gent[inp], len) == 0 ) {

    /*** a certain genotype was found more than just once ***/
    ge[inp] = i;
    mg[i] ++;
    it       = 0;
    merke[i] = drei;
    break;
    }
    }
    if (it) {
    /*** a certain genotype was encountered the first time ***/
    strcpy (geno[ng], gent[inp]);
    ge[inp] = ng;
    mg[ng]    = 1;
    merke[ng] = drei;
    nulmer[ng] = null;
    ng ++;
    }
    }   /* end while */
    People 	= np;
    Loci 	= len-1;
    /* end of reading sample data */

    nall = 2 * np;
    nstate  = init_ivector (NULL, 0, ng);
    mstate  = init_ivector (NULL, 0, ng);

    state = (uint***) calloc(ng , sizeof(uint**));
    for (i=0; i<ng; i++) {
      nz       = po[merke[i]] * po[nulmer[i]] * po[nulmer[i]];
      state[i] = uimatrix(nz, 2);
    }
    /*** sort genotypes by weights *******************************************/
    genoProb = dvector(ng);
    genoId   = ivector(ng);
    for (i=0; i<ng; i++) {
      genoId[i]   = i;
      genoProb[i] = ((double)mg[i])/((double) po[merke[i]])/pow(4.0,nulmer[i]);
    }
    sortByProb(genoProb, genoId, ng);

    glev=0;
    for(i=0;i<ng;i++)if(genoProb[i] >= SignificanceLevel)glev++;

    /*** process sorted genotypes ********************************************/

    nh = 0;

    for (i=0; i<glev; i++) {
/*    printf("\n ng: %d glev: %d  i: %d",ng,glev+1,i+1);    */
      rechap(genoId[i], 0, len-1);
/*    printf("\n %s >> %d\n",geno[genoId[i]],mg[genoId[i]]);
      for(k=0;k<16;k++)printf("%2d:%g ",hc[k],hap[k]);
      printf("\n");                                         */
    }

    for (i=glev; i<ng; i++) {
      s = 0;
/*    printf("\n ng: %d glev: %d  i: %d",ng,glev+1,i+1);    */
      for (j=0; j<nh; j++) {
        ac[0] = hc[j];
        for (k=j; k<nh; k++) {
          ac[1] = hc[k];
	  if ( compatible(geno[genoId[i]], ac) ) {
            state[genoId[i]][s][0] = j;
            state[genoId[i]][s][1] = k;
            s ++;
            if ( j != k ) {
              state[genoId[i]][s][0] = k;
              state[genoId[i]][s][1] = j;
              s ++;
	    }
	  }
        }
      }
      nstate[genoId[i]] = s;
    }
    for (i=glev; i<ng; i++) {
      addon(genoId[i]);
    }

/*  printf("\n");
    printf("\ngloop: %d ng: %d glev: %d nh: %d\n",gloop,ng,glev,nh);  */

    /*** now comes the output that does not need simulated annealing *********/

    first  = 1;           /*** start likelihood outside of annealing loops ***/

    df = nh;

    hapnew = init_dvector(NULL, 0.0, nh );
    haptmp = init_dvector(NULL, 0.0, nh );

    for (i=0; i<ng; i++)selprob(i);

    likold = likea();

    /* Continue computation of mean probabilities */

    for(i=0;i<ng;i++)
      for(j=0;j<mstate[i];j++) {
        double pp = 0.0;

        if ( nstate[i] > 1 ) {
          h          = state[i][j][0];
          hapnew[h] += (double)mg[i] / (double)mstate[i];
          h          = state[i][j][1];
          pp        += hapnew[h];
          hapnew[h] += ((double) mg[i]) / ((double) mstate[i]);
          pp        += hapnew[h];
        }
        else {
          h          = state[i][j][0];
          hapnew[h] += 2.0 * ((double) mg[i]) / ((double) mstate[i]);
          pp        += hapnew[h];
        }
      }


    non = 0;
    for (i=0; i<nh; i++) {
    if(hapnew[i]==0.0)non++;
    else hapnew[i] /= (double) nall;
    }

    for (i=0; i<nh; i++) {
    if(hapnew[i]==0.0)hapnew[i] = 0.0001/(double)non;
    else hapnew[i] *= 0.9999;
    }


	iterations = 0;
    first = 0;

    do {
      loop = 0;
      iterations ++;
/*    printf("gloop:%3d  count: %d\n",gloop,iterations);  */
      /* Recompute mean probabilities */
      for (i=0; i<nh; i++) {
        if ( fabs(hap[i] - hapnew[i]) > LoopPrecision )
          loop = 1;
        hap[i] = hapnew[i];
      }

      init_dvector(prob,   0.0, np);
      init_dvector(haptmp, 0.0, nh);
      init_dvector(hapnew, 0.0, nh);
      likold = likea();
      for (i=0; i<nh; i++)
        hapnew[i] /= (double) nall;

    } while (loop);

/*  Rprintf("\n");
    Rprintf("  Results Ensemble means: \n\n"); */
    nhap = 0;
    j    = 0;
    for (i=0; i<nh; i++)
      {
      if ( hapnew[i] >= *lim ) {
    /* 07.06.2007  S.Kn|ppel > Beschrdnken der geschdtzten Haplotypen. */
    if ( (*tog==0) &&  ((nhap+1) > 1500) ) {
     error ("Error in itegeppXXR: Too much estimated haplotypes. Increase option lim.") ;
    }
    if ( (*tog==1) &&  ((nhap+1) > 1500) ) {
     error ("Error in itegeppXXR: Too much estimated haplotypes. Increase option lim.") ;
    }


       
/*    	sprintf(lino,"\0"); 02.06.2015/SKn */  
    /*    sprintf("%s", "%s", *lino, *CharNull);*/
       sprintf(lino, "%s", CharNull);


   printHaplotype(hc[i], len, lino);
	/*
        printf("    hapnew[%8d] = %7.4f  (%7.4f)\n",
	    hc[i], hapnew[i], hap[i]);
        */
  /* sprintf(lin,"%9.6f\0", hapnew[i]); 06.11.2014/SKn */
	sprintf(lin,"%9.6f%s", hapnew[i], CharNull); /* 06.11.2014/SKn */
	strcat(lino,lin);
	strcpy(freqres[j],lino);
	j++;
        hlist[nhap++] = i;



      }
      }
      k = 0;
      htpp = init_uimatrix(NULL,0,nhap+1,nhap+1);
      for(i=0;i<nhap+1;i++)
	      for(j=i;j<nhap+1;j++)htpp[i][j]=k++;

      pgen = init_dmatrix(NULL,0.0,ng,(nhap+1)*(nhap+2)/2);

      /* start find best states after MLE   10.3.2000 ROHDE  */

   	  pimax  = imatrix(ng,10);               /* ROHDE  10.3.2000 */
          p2max = init_dvector(NULL,0.0,ng);     /* ROHDE  10.3.2000 */
	  for(i=0;i<ng;i++)max_prob[i] = 0.0;    /* ROHDE  10.3.2000 */
      for (i=0;i<ng;i++){
      for(j=0;j<10;j++)pimax[genoId[i]][j] = -1;
          iqual=1;
      for (j=0;j<nstate[genoId[i]];j++){
      pe = hapnew[state[genoId[i]][j][0]] * hapnew[state[genoId[i]][j][1]];
	  if( state[genoId[i]][j][0] != state[genoId[i]][j][1] ) pe += pe;

	  if(pe > p2max[genoId[i]]){

      if (pe > max_prob[genoId[i]]){
		p2max[genoId[i]] = max_prob[genoId[i]];
      	max_prob[genoId[i]] = pe;
      	pimax[genoId[i]][0]=j;
		for(k=1;k<10;k++)pimax[genoId[i]][k]=-1;
		iqual = 1;                 /***   ROHDE  04.09.2001 ***/
      	}

	  else{
	  if (pe == max_prob[genoId[i]] && iqual < 9){
	  	for(k=0;k<iqual;k++) if(state[genoId[i]][j][0] ==
						state[genoId[i]][pimax[genoId[i]][k]][1]) pe=0.0;
	  	if(pe > 0.0)pimax[genoId[i]][iqual++]=j;
	  	}
	    else p2max[genoId[i]] = pe;
		}
	  }

      }
      }    /* end of maximum state search */

/*    Rprintf("\n Haplotypes after MLE\n");           */
      jjx = 0;
      for(i=0;i<np;i++){
        /* sprintf(lino,"%i %s >> \0",i, geno[ge[i]]); 06.11.2014/SKn */
           sprintf(lino,"%i %s >> %s",i, geno[ge[i]], CharNull);
        for(k=0;k<10;k++){
          j = pimax[ge[i]][k];
          if(j > -1){
            if(k>0)pspace(len+3,lino);   /*** ROHDE  11.09.2001 ***/
            printHaplotype(hc[state[ge[i]][j][0]],len,lino);
            strcat(lino," <> \0");
            printHaplotype(hc[state[ge[i]][j][1]],len,lino);
            sprintf(lin,"  P>> %9.7f D>> %9.7f",
      	                 max_prob[ge[i]],max_prob[ge[i]]-p2max[ge[i]]);
            strcat(lino,lin);
          } else break;
        }
        strcpy(hapres[jjx++],lino);
      }

      /* endfind best states after MLE   10.3.2000 ROHDE  */


/*    Rprintf("\n\n       Likelihood = %f\n", likold);
      Rprintf("\n");                                      */
      /* sprintf(lino,"Likelihood = %f\0", likold); 06.11.2014/SKn */
         sprintf(lino,"Likelihood = %f%s", likold, CharNull);
     // strcpy(likeres[0],lino);
      (*likeres) = likold ;
      
/*  Sample over states for each genotype ***********************************/

      for(i=0;i<ng;i++){
      gsum = 0.0;
      for(j=0;j<nstate[genoId[i]];j++){
      h1 = state[genoId[i]][j][0];
      h2 = state[genoId[i]][j][1];
      h1x = h2x = 0;
      for(ki=1;ki<=nhap;ki++) if( h1 == hlist[ki-1] ) h1x=ki;
      for(kj=1;kj<=nhap;kj++) if( h2 == hlist[kj-1] ) h2x=kj;
      if(h1x>0 && h2x>0){
	      if(h2x < h1x){ k=h1x; h1x=h2x; h2x=k;}
	      pgen[genoId[i]][htpp[h1x-1][h2x-1]] += hapnew[h1]*hapnew[h2];
	                                     gsum += hapnew[h1]*hapnew[h2];
	}
	else{ pgen[genoId[i]][htpp[nhap][nhap]] += hapnew[h1]*hapnew[h2];
		                           gsum += hapnew[h1]*hapnew[h2];
	}
      }
      for(k=0;k<(nhap+1)*(nhap+2)/2;k++)pgen[genoId[i]][k] /= gsum;
      }

/*    for(i=0;i<ng;i++){
      Rprintf("i:%2d  %s\t",i,geno[genoId[i]]);
      for(ki=0;ki<nhap+1;ki++){
	for(kj=ki;kj<nhap+1;kj++)
               Rprintf("%4.2f ",pgen[genoId[i]][htpp[ki][kj]]);
	if(kj<ki)printf("0.000\t ");
	else	printf("%4.2f\t",pgen[genoId[i]][htpp[ki][kj]]);
      Rprintf("\t");
      }
      Rprintf("\n");
      }
*/

      jjx = 0;

      if (*tog == 1){
      for(i=0;i<np;i++){
      /*
      printf("\n%4s %s %4.2f >> ",pid[i],geno[ge[i]],qtrait[i]);
      */
      strcpy(lino,"\0");
      for(ki=0;ki<nhap;ki++){  /*  each haplotype alone   */
        for(kj=ki;kj<nhap;kj++){
          /* sprintf(lin,"%8.6f \0",pgen[ge[i]][htpp[ki][kj]]); 06.11.2014/SKn */
	           sprintf(lin,"%8.6f %s",pgen[ge[i]][htpp[ki][kj]], CharNull);
          strcat(lino,lin);
	      }

      }
      
      /* sprintf(lin,"%8.6f\0",pgen[ge[i]][htpp[nhap][nhap]]); 06.11.2014/SKn */
         sprintf(lin,"%8.6f%s",pgen[ge[i]][htpp[nhap][nhap]], CharNull);
      strcat(lino,lin);
      strcpy(desres[jjx],lino);
      jjx++;
      }
      }
      
 /* gedndert nach Klaus; Bildung Designmatrix
    16.09.2008 */
 
 /*     if(*tog == 0){
      for(i=0;i<np;i++){
      //
      //printf("\n%4s %s %4.2f >> ",id[i],geno[ge[i]],qtrait[i]);
      //
      strcpy(lino,"\0");
          pex = 0.0;
      for(j=0;j<nhap;j++){
          pe = 0.0;
      for(ki=0;ki<nhap;ki++){    // over all haplotype pairs  
      for(kj=ki;kj<nhap;kj++){
      if(ki==j && kj==j && pgen[ge[i]][htpp[ki][kj]] > 0.0)
	      pe +=2.0*pgen[ge[i]][htpp[ki][kj]];
      else if ((ki==j || kj==j) && pgen[ge[i]][htpp[ki][kj]] > 0.0)
                                    pe += pgen[ge[i]][htpp[ki][kj]];
      }
      }
      pex += pe;
      sprintf(lin,"%8.6f \0",pe);
      strcat(lino,lin);
      }
      sprintf(lin,"%8.6f\0",2.0-pex);
      strcat(lino,lin);
      strcpy(desres[jjx],lino);
      jjx++;
      }
      }
*/
/* new: nach Klaus; 17.09.2008 */
        if(*tog == 0){
        
      peh = init_dvector(NULL, 0.0, nhap+1);
      for(i=0;i<np;i++){
      /*
      printf("\n%4s %s %4.2f >> ",id[i],geno[ge[i]],qtrait[i]);
      */
      strcpy(lino,"\0");
      for(j=0;j<nhap;j++){
      gsum  = 0.0;
      /*
      for(ki=0;ki<nhap;ki++){    * over all haplotype pairs  *
      for(kj=ki;kj<nhap;kj++){
      if(ki==j && kj==j && pgen[ge[i]][htpp[ki][kj]] > 0.0)
	      pe +=2.0*pgen[ge[i]][htpp[ki][kj]];
      else if ((ki==j || kj==j) && pgen[ge[i]][htpp[ki][kj]] > 0.0)
                                    pe += pgen[ge[i]][htpp[ki][kj]];
      }
      }
      */
      for(ki=0;ki<nstate[ge[i]];ki++){
      h1 = state[ge[i]][ki][0];
      h2 = state[ge[i]][ki][1];
      h  = hlist[j];
      pex = hapnew[h1]*hapnew[h2];
      gsum += 2*pex;
      if((h == h1) && (h == h2))peh[j] += 2*pex;
      else if((h == h1) || (h == h2))peh[j] += pex;
      }  /* end nstate */
      }  /* end nhap   */
      pehh = 0.0;
      for(j=0;j<nhap;j++){
      pehh += 2*peh[j];
      /* sprintf(lin,"%8.6f \0",2*peh[j]/gsum); 06.11.2014/SKn */
      sprintf(lin,"%8.6f %s",2*peh[j]/gsum, CharNull);
      strcat(lino,lin);
      }  /* end print */
      /* sprintf(lin,"%8.6f\0",2.0-pehh/gsum); 06.11.2014/SKn */
         sprintf(lin,"%8.6f%s",2.0-pehh/gsum, CharNull);
      
      strcat(lino,lin);
      strcpy(desres[jjx],lino);
      jjx++;
      init_dvector(peh, 0.0, nhap+1);
      }  /*  end np  */
      destroy_d_array(peh);
      }

    
    destroy_c_array2(geno);
    destroy_u_array(po);
    for ( i=0;i<ng;i++) { destroy_u_array2(state[i]) ; }
    free((uint***)state);
    destroy_u_array2(htpp);
    destroy_i_array(nstate);
    destroy_i_array(mstate);
    destroy_i_array(genoId);
    destroy_i_array(mg);
    destroy_i_array(merke);
    destroy_i_array(nulmer);
    destroy_i_array(ge);
    destroy_i_array(hlist);
    destroy_d_array(prob);
    destroy_d_array(max_prob);
    destroy_d_array(hapnew);
    destroy_d_array(haptmp);
    destroy_d_array(hap);
    destroy_d_array(genoProb);
    destroy_d_array2(pgen);
    destroy_i_array(hc);
    destroy_d_array(p2max);
    destroy_i_array2(pimax);
}
예제 #18
0
static int build_instance(char *filename, smt_core_t *core) {
  int l, n, c_idx, literal, nvars, nclauses;
  char *s;
  FILE *f;
  ivector_t buffer;

  f = fopen(filename, "r");
  if (f == NULL) {
    perror(filename);
    return OPEN_ERROR;
  }

  s = fgets(line, MAX_LINE, f);
  l = 1; /* line number */

  if (s == NULL) {
    fprintf(stderr, "%s: empty file\n", filename);
    fclose(f);
    return FORMAT_ERROR;
  }

  /* skip empty and comment lines */
  while (*s == 'c' || *s == '\n') {
    s = fgets(line, MAX_LINE, f);
    l ++;

    if (s == NULL) {
      fprintf(stderr, "Format error: file %s, line %d\n", filename, l);
      fclose(f);
      return FORMAT_ERROR;
    }
  }

  /* read problem size */
  n = sscanf(s, "p cnf %d %d", &nvars, &nclauses);
  if (n != 2 || nvars < 0 || nclauses < 0) {
    fprintf(stderr, "Format error: file %s, line %d\n", filename, l);
    fclose(f);
    return FORMAT_ERROR;
  }

  /* initialize core for nvars */
  init_sat_solver(core, nvars);

  /* initialize the clause buffer */
  init_ivector(&buffer, 10);

  /* now read the clauses and translate them */
  c_idx = 0;

  while (c_idx < nclauses) {
    for (;;) {
      literal = read_literal(f, nvars);
      if (literal < 0) break;
      ivector_push(&buffer, literal);
    }

    if (literal != END_OF_CLAUSE) {
      fprintf(stderr, "Format error: file %s\n", filename);
      fclose(f);
      return FORMAT_ERROR;
    }

    add_clause(core, buffer.size, buffer.data);
    c_idx ++;
    ivector_reset(&buffer);
  }

  delete_ivector(&buffer);
  fclose(f);

  return 0;
}