Exemplo n.º 1
0
void stable_infect(unsigned int K, unsigned int U, enum objective obj_type) {

  int nThread = (int) (90 * K * K * (1 + log(G->V)));
  FILE *info = stdout;
  unsigned int V = (unsigned int) G->V, initial_number_of_seed = 0, i = 0x323;
  float Ninfected_mean[V + 1];
  float prev_obj_value = (float) (0 - LARGE_FLT);
  int delta = MAX_EDGE_WEIGHT / (K * K * 9);
  char seeds[V + 1];

  K = K < (V + 1) ? K : (V + 1);
  memset(Ninfected_mean, 0, V * sizeof *Ninfected_mean);
  memset(seeds, 0, (V + 1) * sizeof *seeds);
  seed_vertices = (int *) calloc((size_t) (V + 5), sizeof *seed_vertices);
  /*
   * Select next seed with maximum mean of infection
   */
  struct _Vertex *sorted[V + 1];
  for (i = initial_number_of_seed; i < K * K * 9; ++i) {
    // Add the seed by 1
    int new_seed_label, j;
    for (j = 0; j <= G->V; ++j) {
      G->adj_list[j]->obj = 0;
    }
    float max_ = (float) -LARGE_FLT;
    int max_2 = 0;
    int max_label = 0x12121, max_label_2 = 0x121;


    n_seed = i + 1;
    fprintf(info, "  #seed: %d\n", n_seed);
    memset(Ninfected_mean, 0, (V + 1) * sizeof *Ninfected_mean);
    fprintf(info, "new seed : ");
    for (new_seed_label = 1; new_seed_label <= V; ++new_seed_label) {
      seed_vertices[0] = new_seed_label;
      float new_ = multithread_infect("/dev/null", K, U, obj_type);
      Ninfected_mean[new_seed_label] = new_;
      G->adj_list[new_seed_label]->obj = new_;
      int j = 0x1234, s = 0;
      for (j = 0; j < nThread; ++j) {
        s += (Ninfected_ptr[j] > U ? 1 : 0);
      }
      fprintf(info, "%d:%f ", new_seed_label, new_);
      fflush(info);
      if (obj_type == P && max_2 < s) {
        max_2 = s;
        max_label_2 = new_seed_label;
      }
      if (max_ < new_) {
        max_ = new_;
        max_label = new_seed_label;
      }
    }

    memcpy(sorted + 1, G->adj_list + 1, V * sizeof *sorted);
    qsort(sorted + 1, V, sizeof *sorted, comp);
    fprintf(info, "\n-----------\n");
    for (j = V; j > V - K; --j) {
      sorted[j]->prob += delta;
      fprintf(info, "%d:%f prob %f ", sorted[j]->label, sorted[j]->obj, sorted[j]->prob/ (float) MAX_EDGE_WEIGHT);
    }
    fprintf(info, "\n");

    fputc('\n', info);
    double th = 0.0;
    if (obj_type == MEAN) {
      th = (G->V / 500 > 1 ? G->V / 500 : 1);
    } else {
      th = -LARGE_FLT;
    }
    int w = G->V / (nThread / 10);
    if (obj_type == P) {
      /*
       * If there is one selection with possible number of infected nodes
       * larger than U
       */
      max_label = max_2 > 0 ? max_label_2 : max_label;
    }
    memset(seed_vertices, 0, (G->V + 5) * sizeof *seed_vertices);
    seed_vertices[0] = max_label;
    //seeds[max_label] = 1;

    prev_obj_value = max_;
    fprintf(info, "-------\nSelected new seed: %d, prev_obj_value : %f \n",
            max_label, max_);

    print_seeds(info, seed_vertices, n_seed, 20);
    /*
     * The mean value of infected nodes
     */
    fprintf(info, "-----------\nThe mean of infected nodes : %f\n",
            multithread_infect("/dev/null", K, 0, MEAN));
    print_pdf(info, Ninfected_ptr, nThread,
              G->V, w > 1 ? w : 1);
    fprintf(info, "-------------\n");
    char cdf_file[20];
    cdf_file[0] = obj_type + '0';
    cdf_file[1] = '.';
    sprintf(cdf_file + 2, "%d.\0", U);
    sprintf(cdf_file + strlen(cdf_file), "%d\0", n_seed);
    FILE *new_info = fopen(cdf_file, "w");
    print_cdf(new_info, Ninfected_ptr, nThread, G->V, w > 1 ? w : 1);
    fclose(new_info);
  }
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    char *str_alpha_p = argv[1], *str_alpha_q = argv[2];
    char *cdf = argv[3];

    unsigned nprior = atoi(argv[4]);
    unsigned npost = atoi(argv[5]);
    unsigned ngold = atoi(argv[6]);
    /* double step_mult = atof(argv[7]); */

    gsl_set_error_handler_off();

    FILE *cdf_fh = fopen(cdf, "w");

    double alpha_p[4], alpha_q[4];
    sscanf(str_alpha_p, "%lf,%lf,%lf,%lf", &alpha_p[0], &alpha_p[1], &alpha_p[2], &alpha_p[3]);
    sscanf(str_alpha_q, "%lf,%lf,%lf,%lf", &alpha_q[0], &alpha_q[1], &alpha_q[2], &alpha_q[3]);

    double alpha_combined[] = { 
        alpha_p[0] + alpha_q[0] - 1,
        alpha_p[1] + alpha_q[1] - 1,
        alpha_p[2] + alpha_q[2] - 1,
        alpha_p[3] + alpha_q[3] - 1
    };

    unsigned ntotal = nprior + npost + ngold;
    struct wpoint *buf = (struct wpoint *)malloc(ntotal * sizeof(struct wpoint));
    struct wpoint *p_points = buf, *q_points = buf + nprior, *g_points = q_points + npost;
    struct wpoint **tmp_points = (struct wpoint **)malloc((nprior + npost) * sizeof(struct wpoint *));
    struct wpoint **int_smooth_points = (struct wpoint **)malloc(npost * sizeof(struct wpoint *));
    struct wpoint **ext_points = (struct wpoint **)malloc(nprior * sizeof(struct wpoint *));
    struct wpoint **ext_smooth_points = (struct wpoint **)malloc(nprior * sizeof(struct wpoint *));
    struct wpoint **ext_rough_points = (struct wpoint **)malloc(nprior * sizeof(struct wpoint *));
    struct wpoint **all_smooth_points = (struct wpoint **)malloc((nprior + npost) * sizeof(struct wpoint *));

    struct wpoint **pg = (struct wpoint **)malloc(ngold * sizeof(struct wpoint *));

    struct wpoint **pp, **pp2, **pe;
    struct wpoint *s, *p_end = p_points + nprior, *q_end = q_points + npost;

    gsl_rng *rand = gsl_rng_alloc(gsl_rng_taus);

    // populate P(*)
    double p_norm;
    populate_points(p_points, nprior, alpha_p, alpha_q, rand, "prior", &p_norm);

    // populate Q(*)
    double q_norm;
    populate_points(q_points, npost, alpha_q, alpha_p, rand, "post", &q_norm);

    int i;
    for (i = 0; i != 10; ++i)
    {
        populate_points(q_points, npost, alpha_q, alpha_p, rand, "post", &q_norm);
        fprintf(stderr, "q_norm = %g\n", q_norm);
    }        


    /* populate gold(*) */
    double g_norm;
    double alpha_uniform[] = { 1, 1, 1, 1 };
    populate_points(g_points, ngold, alpha_combined, alpha_uniform, rand, "gold", &g_norm);

    /* normalize */
    normalize_points(p_points, nprior, p_norm, q_norm);
    normalize_points(q_points, npost, q_norm, p_norm);
    normalize_points(g_points, ngold, g_norm, g_norm);

    unsigned n;
    
    /* gather all Q(*) points (use int_smooth_points temporarily) */
    for (s = q_points, pp = int_smooth_points; s != q_end; ++s)
        *pp++ = s;
    n = pp - int_smooth_points;

    /* collect Q(*) smooth (interior) points */
    // double ln_p_bound = smooth_threshold(int_smooth_points, step_mult, &n);
    /* for (s = q_points, pp = int_smooth_points; s != q_end; ++s) */
    /*     if (s->ln_o <= ln_p_bound) */
    /*         *pp++ = s; */
    for (s = q_points, pp = int_smooth_points; s != q_end; ++s)
        if (s->ln_d > s->ln_o)
            *pp++ = s;
    unsigned n_int_smooth_q = pp - int_smooth_points;

    /* double ma_ratio = max_to_avg_ratio(int_smooth_points,); */
    double int_mass_on_q = sum_of_weights(int_smooth_points, n_int_smooth_q);

    /* collect exterior P(*) points */
    /* for (s = p_points, pp = ext_points; s != p_end; ++s) */
    /*     if (s->ln_d > ln_p_bound) */
    /*         *pp++ = s; */
    for (s = p_points, pp = ext_smooth_points; s != p_end; ++s)
        if (s->ln_d > s->ln_o)
            *pp++ = s;
    /* unsigned n_ext_p = n = pp - ext_points; */
    /* double ln_q_bound = smooth_threshold(ext_points, step_mult, &n); */

    /* collect exterior P(*) smooth points */
    /* for (s = p_points, pp = ext_smooth_points; s != p_end; ++s) */
    /*     if (s->ln_d > ln_p_bound && s->ln_o < ln_q_bound) */
    /*         *pp++ = s; */
    unsigned n_ext_smooth_p = pp - ext_smooth_points;

    
    /* double ma2_ratio = max_to_avg_ratio(ext_smooth_points, n_ext_smooth_p); */
    double ext_mass_on_p = sum_of_weights(ext_smooth_points, n_ext_smooth_p);
    double total_mixed_mass = int_mass_on_q + ext_mass_on_p;
    /* fprintf(stderr, "Number of points unaccounted for: %u\n", n_ext_p - n_ext_smooth_p); */

    // print out numerical CDFs
    char hdr[] = "Dist\tBase\tComp\tPointIndex\tCDF\tln_d\tln_o\tBaseDist\n";
    fprintf(cdf_fh, hdr);

    print_cdf(int_smooth_points, n_int_smooth_q, total_mixed_mass, buf, "int_smooth_q", cdf_fh);
    print_cdf(ext_smooth_points, n_ext_smooth_p, total_mixed_mass, buf, "ext_smooth_p", cdf_fh);

   
    pe = int_smooth_points + n_int_smooth_q;
    for (pp = int_smooth_points, pp2 = all_smooth_points; pp != pe; ++pp, ++pp2)
        *pp2 = *pp;

    pe = ext_smooth_points + n_ext_smooth_p;
    for (pp = ext_smooth_points; pp != pe; ++pp, ++pp2)
        *pp2 = *pp;

    unsigned n_smooth = n_int_smooth_q + n_ext_smooth_p;
    print_cdf(all_smooth_points, n_smooth, total_mixed_mass, p_points, "smooth_combined", cdf_fh);

    /* all points in the P(*) sampling */
    for (s = p_points, pp = tmp_points; s != p_end; ++s)
        *pp++ = s;
    print_cdf(tmp_points, nprior, total_mixed_mass, p_points, "all_p", cdf_fh);

    /* all points in the Q(*) sampling */
    for (s = q_points, pp = tmp_points; s != q_end; ++s)
        *pp++ = s;

    print_cdf(tmp_points, npost, total_mixed_mass, q_points, "all_q", cdf_fh);


    /* print out the gold standard */
    struct wpoint *gold_end = g_points + ngold;
    for (s = g_points, pp = pg; s != gold_end; ++s)
        *pp++ = s;
    
    double gold_mass = sum_of_weights(pg, ngold);
    print_cdf(pg, ngold, gold_mass, g_points, "dgold", cdf_fh);

    /* compute component-wise marginal estimates. */
    /* double q[] = { 0.0001, 0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999, 0.9999 }; */
    /* double qv[NUCS][sizeof(q) / sizeof(double)]; */
    
    // print out marginal estimates
    /* unsigned iq; */
    /* for (dim = 0; dim != NUCS; ++dim) */
    /* { */
        /* for (iq = 0; iq != sizeof(qv[dim]) / sizeof(double); ++iq) */
        /*     printf(iq ? "\t%f" : "%f", qv[dim][iq]); */
        /* printf("\n"); */
    /* } */

    free(buf);
    free(tmp_points);
    free(int_smooth_points);
    free(ext_points);
    free(ext_smooth_points);
    free(ext_rough_points);
    free(all_smooth_points);
    free(pg);
    gsl_rng_free(rand);

    fclose(cdf_fh);

    return 0;
}