Exemple #1
0
int main(int argc, char *argv[])
{
	int i;
	StudentInfo_P pMaxScore = NULL;

	StudentInfo_T info_list[4] = {
		{"Tom", 8},
		{"Jerry", 72},
		{"Moby", 90},
		{"Kirby", 89}
	};

	/*
	 * 指针数组
	 * 结构体指针数组
	 */
	StudentInfo_P pInfos[4] = {
		&info_list[0],
		&info_list[1],
		&info_list[2],
		&info_list[3]
	};

	printf("Student's Scores Info:\n");
	for (i = 0; i < 4; i++)
		printf("name %5s : score %5d\n", info_list[i].name, info_list[i].score);

	pMaxScore = get_max_score((void **)pInfos, 4, cmp_student);
	printf("Top score is %s:%d\n", pMaxScore->name, pMaxScore->score);

	return 0;
}
Exemple #2
0
 */bool AStarPath::path_search(MapCoord &start, MapCoord &goal)
{//DEBUG(0,LEVEL_DEBUGGING,"SEARCH: %d: %d,%d -> %d,%d\n",actor->get_actor_num(),start.x,start.y,goal.x,goal.y);
    astar_node *start_node = new astar_node;
    start_node->loc = start;
    start_node->to_start = 0;
    start_node->to_goal = path_cost_est(start, goal);
    start_node->score = start_node->to_start + start_node->to_goal;
    start_node->len = 0;
    push_open_node(start_node);
    const uint32 max_score = get_max_score(start_node->to_goal);
    const uint32 max_steps = 8*2*4; // walk up to four screen lengths before searching again
    while(!open_nodes.empty())
    {
        astar_node *nnode = pop_open_node(); // next closest
        if(nnode->loc == goal || nnode->len >= max_steps)
        {
	    if(nnode->loc != goal)
	        DEBUG(0,LEVEL_DEBUGGING,"out of steps, making partial path (nnode->len=%d)\n",nnode->len);
//DEBUG(0,LEVEL_DEBUGGING,"GOAL\n");
            final_node = nnode;
            create_path();
            delete_nodes();
            return(true); // reached goal - success
        }
        // check cardinal neighbors (starting at top going clockwise)
        search_node_neighbors(nnode, goal, max_score);
        // node and neighbors checked, put into closed
        closed_nodes.push_back(nnode);
    }
//DEBUG(0,LEVEL_DEBUGGING,"FAIL\n");
    delete_nodes();
    return(false); // out of open nodes - failure
}/* Return the cost of moving one step from `c1' to `c2', which is always 1. This
Exemple #3
0
static void do_classify(args_t *args)
{
    annots_reader_reset(args);
    double max_score = sqrt(args->som[0]->kdim);
    while ( annots_reader_next(args) )
    {
        double score = 0;
        switch (args->merge)
        {
            case MERGE_MIN: score = get_min_score(args, -1); break;
            case MERGE_MAX: score = get_max_score(args, -1); break;
            case MERGE_AVG: score = get_avg_score(args, -1); break;
        }
        printf("%e\n", 1.0 - score/max_score);
    }
    annots_reader_close(args);
}
Exemple #4
0
void init_motif (struct MOTIF **motif, char *fileName, float cutoff,
			 char *id)
{
    char chr,
	 *cur_id = NULL,
	 *na = NULL,
	 *buf = NULL;
    int i, 
	pos,
	found_id = 0,
	fA, fC, fG, fT;
    FILE *fp = ckopen (fileName, "r");

    buf = (char *) ckalloc (BUFSIZE);
    cur_id = (char *) ckalloc (STRSIZE);
    na = (char *) ckalloc (STRSIZE);

    while (fgets( buf, BUFSIZE, fp ))
    {
  	if (sscanf (buf, "ID %s", cur_id) == 1) {
	    if (strcmp (cur_id, id) == 0)
	    {
		found_id = 1;
    	    	*motif = ckalloc (sizeof (struct MOTIF));
    	    	(*motif)->id = (char *) ckalloc ((strlen (id) + 1) * sizeof (char));
                strcpy ((*motif)->id, id);
	    } 

	} else if ((found_id) && sscanf (buf, "NA %s", na) == 1) {
            (*motif)->name = (char *) ckalloc ((strlen (na) + 1) * sizeof (char));
            strcpy ((*motif)->name, na);  

	} else if ((found_id) && strncmp (buf, "P0", 2) == 0) {
            allocate_submat (motif);
	    pos = 0;
            while ((fgets (buf, BUFSIZE, fp)) && (buf[0] != 'X'))
            {
                if ((sscanf (buf,"%d %d %d %d %d %c", &i, &fA, &fC, &fG, &fT, &chr)) !=6)
                    fatal ("Invalid transfac matrix file format.");
                (*motif)->submat[rowA][pos] = fA;
                (*motif)->submat[rowC][pos] = fC;
                (*motif)->submat[rowG][pos] = fG;
                (*motif)->submat[rowT][pos] = fT;
		pos++;
            }

	    (*motif)->min = get_min_score ((*motif)->submat, pos);
            (*motif)->max = get_max_score ((*motif)->submat, pos);

            /* equation taken from the MOTIF program (http://motif.genome.ad.jp) */
	    (*motif)->threshold = (cutoff) * (*motif)->max + (1 - cutoff) * (*motif)->min;

	    (*motif)->len = pos;
	    (*motif)->next = NULL;

	    break;
        } 
    }

    if (!found_id)
        fatalf ("*** ERROR: did not find id number '%s' in transfac file. ***\n\n", id);

    free (cur_id);
    free (na);
    free (buf);
}
Exemple #5
0
static void do_train(args_t *args)
{
    // read training sites
    int i, igood = 0, ibad = 0, ngood = 0, nbad = 0, ntrain = 0;
    annots_reader_reset(args);
    while ( annots_reader_next(args) )
    {
        // determine which of the nfold's SOMs to train
        int isom = 0;
        if ( args->dclass == args->good_class ) 
        { 
            if ( ++igood >= args->nfold ) igood = 0; 
            isom = igood;
            ngood++;
        }
        else if ( args->dclass == args->bad_class )
        {
            if ( ++ibad >= args->nfold ) ibad = 0; 
            isom = ibad;
            nbad++;
        }
        else
            error("Could not determine the class: %d (vs %d and %d)\n", args->dclass,args->good_class,args->bad_class);

        // save the values for evaluation
        ntrain++;
        hts_expand(double, ntrain*args->mvals, args->mtrain_dat, args->train_dat);
        hts_expand(int, ntrain, args->mtrain_class, args->train_class);
        memcpy(args->train_dat+(ntrain-1)*args->mvals, args->vals, args->mvals*sizeof(double));
        args->train_class[ntrain-1] = (args->dclass==args->good_class ? 1 : 0) | isom<<1;  // store class + chunk used for training
    }
    annots_reader_close(args);

    // init maps
    if ( !args->ntrain ) args->ntrain = ngood/args->nfold;
    srandom(args->rand_seed);
    args->som = (som_t**) malloc(sizeof(som_t*)*args->nfold);
    for (i=0; i<args->nfold; i++) args->som[i] = som_init(args);

    // train
    for (i=0; i<ntrain; i++)
    {
        int is_good = args->train_class[i] & 1;
        int isom    = args->train_class[i] >> 1; 
        if ( is_good || args->train_bad ) 
            som_train_site(args->som[isom], args->train_dat+i*args->mvals, is_good);
    }

    // norm and create plots
    for (i=0; i<args->nfold; i++) 
    {
        som_norm_counts(args->som[i]);
        if ( args->prefix )
        {
            char *bname = msprintf("%s.som.%d", args->prefix,i);
            som_create_plot(args->som[i], bname);
            free(bname);
        }
    }

    // evaluate
    float *good = (float*) malloc(sizeof(float)*ngood); assert(good);
    float *bad  = (float*) malloc(sizeof(float)*nbad); assert(bad);
    igood = ibad = 0;
    double max_score = sqrt(args->som[0]->kdim);
    for (i=0; i<ntrain; i++)
    {
        double score = 0;
        int is_good = args->train_class[i] & 1;
        int isom    = args->train_class[i] >> 1;    // this vector was used for training isom-th SOM, skip
        if ( args->nfold==1 ) isom = -1;
        memcpy(args->vals, args->train_dat+i*args->mvals, args->mvals*sizeof(double));
        switch (args->merge)
        {
            case MERGE_MIN: score = get_min_score(args, isom); break;
            case MERGE_MAX: score = get_max_score(args, isom); break;
            case MERGE_AVG: score = get_avg_score(args, isom); break;
        }
        score = 1.0 - score/max_score;
        if ( is_good )
            good[igood++] = score;
        else
            bad[ibad++] = score;
    }
    qsort(good, ngood, sizeof(float), cmpfloat_desc);
    qsort(bad, nbad, sizeof(float), cmpfloat_desc);
    FILE *fp = NULL;
    if ( args->prefix ) fp = open_file(NULL,"w","%s.eval", args->prefix);
    igood = 0;
    ibad  = 0;
    float prev_score = good[0]>bad[0] ? good[0] : bad[0];
    int printed = 0;
    while ( igood<ngood || ibad<nbad )
    {
        if ( igood<ngood && good[igood]==prev_score ) { igood++; continue; }
        if ( ibad<nbad && bad[ibad]==prev_score ) { ibad++; continue; }
        if ( fp ) 
            fprintf(fp,"%e\t%f\t%f\n", prev_score, (float)igood/ngood, (float)ibad/nbad);
        if ( !printed && (float)igood/ngood > 0.9 ) 
        {
            printf("%.2f\t%.2f\t%e\t# %% of bad [1] and good [2] sites at a cutoff [3]\n", 100.*ibad/nbad,100.*igood/ngood,prev_score);
            printed = 1;
        }

        if ( igood<ngood && ibad<nbad ) prev_score = good[igood]>bad[ibad] ? good[igood] : bad[ibad];
        else if ( igood<ngood ) prev_score = good[igood];
        else prev_score = bad[ibad];
    }
    if ( !printed ) printf("%.2f\t%.2f\t%e\t# %% of bad [1] and good [2] sites at a cutoff [3]\n", 100.*ibad/nbad,100.*igood/ngood,prev_score);
    if ( fp )
    {
        if ( fclose(fp) ) error("%s.eval: fclose failed: %s\n",args->prefix,strerror(errno));
        create_eval_plot(args);
        som_write_map(args->prefix, args->som, args->nfold);
    }

    free(good);
    free(bad);
}