Exemplo n.º 1
0
short evaluate_black(struct board *board)
	{
	short key,oracle;
	register x;
	char **matrix;
	short i,*bpos;

	board->sp=0;

	board->intgp.j=0;
	board->intgp.k=0;

	memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));

	for(i=0;i<GROUPS;i++)
		{
		if(threat_group(board,i,BLACK))
			{
			board->intgp.tgroups[i]=YES;
			board->intgp.j++;
			}
		else board->intgp.tgroups[i]=NO;

		if(threat_group(board,i,WHITE))
			{
			board->intgp.mygroups[i]=YES;
			board->intgp.k++;
			}
		else board->intgp.mygroups[i]=NO;
		}

	claimeven(board);
	baseinverse(board);
	vertical(board);
	aftereven(board);
	lowinverse(board);
	highinverse(board);
	baseclaim(board);
	before(board);

	if(board->intgp.j==0) return YES;
	if(board->sp==0) return NO;

	matrix=(char **)allocate_matrix(board);
	build_adjacency_matrix(matrix,board);

    oracle=problem_solver(board,matrix,NO,NULL);
	free_matrix(matrix,board);

	return oracle;
	}
Exemplo n.º 2
0
void parse_and_dump(PerceptronModel mdl, FILE *fp, CoNLLCorpus corpus) {
    for (int si = 0; si < DArray_count(corpus->sentences); si++) {
        FeaturedSentence sent = DArray_get(corpus->sentences, si);

        build_adjacency_matrix(corpus, si, mdl->embedding_w, NULL);

        int *model = parse(sent);


        for (int j = 1; j < sent->length; j++) {
            Word w = (Word) DArray_get(sent->words, j);
            int p = w->parent;

            char *form = w->form;
            char *postag = w->postag;


            fprintf(fp, "%d\t%s\t%s\t%d\t%d\n", j, form, postag, p, model[j]);
        }
        fprintf(fp, "\n");
    }
}
Exemplo n.º 3
0
short ss_evaluate_white(struct board *board)
	{
    FILE *h1;
    short x,y;
	struct threat_combo tc[GROUPS];
	short threats[MAXGROUPS],thnum=0,combo,ccmb;
	short key,oracle,anythreat=0;
	char **matrix;
	short i,*bpos,px,py;

    h1=fopen(TMPFILENAME,"w");
    if(!h1) fatal_error("Cannot open temporary file");

    fprintf(h1,"Oracle analysis for yellow:\n\n");

    combo=threat_combo(board,tc);
	thnum=count_odd_threats(board,threats);
    if(thnum+combo==0)
        {
        fprintf(h1,"There are no single or double threats for yellow\n");
        fprintf(h1,"therefore nothing can be said about yellow possibilities\n");

        oracle=NO;
        goto WSS_ENDING;
        }

    fprintf(h1,"There are %d odd threats and %d double threats for yellow\n\n",
            thnum,combo);

    do
		{
        for(x=0;x<75;x++)
            fputc('*',h1);

        fprintf(h1,"\n\nCase #%d: ",anythreat+1);

        memset(board->usablegroup,0xff,GROUPS*sizeof(short));
		memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));
		memset(board->wipesq,0x00,(BOARDX+1)*(BOARDY+2)*sizeof(short));

		if(anythreat<thnum)
			{
            fprintf(h1,"odd threat at %c%d.\n\n",
                    ELX(threats[anythreat])+'a',ELY(threats[anythreat])+1);

			wipe_above(board,threats[anythreat]);
			wipe_odd(board,threats[anythreat]);

			px=ELX(threats[anythreat]);
			for(py=0;py<BOARDY;py++)
                if(board->square[ELM(px,py)]==EMPTY)
                    board->sqused[ELM(px,py)]=NO;
			}
		else
			{
            ccmb=anythreat-thnum;

			px=ELX(tc[ccmb].cross);
			x =ELX(tc[ccmb].odd);

            fprintf(h1,"double threat at %c%d.\n\n",
                    ELX(tc[ccmb].cross)+'a',ELY(tc[ccmb].cross)+1);

			for(y=0;y<BOARDY;y++)
				{
				if(board->square[ELM(px,y)]==EMPTY)
					board->sqused[ELM(px,y)]=NO;

				if(board->square[ELM( x,y)]==EMPTY)
					board->sqused[ELM( x,y)]=NO;
				}

			if(ELY(tc[ccmb].even)>ELY(tc[ccmb].odd))
				handle_even_above_odd(board,&tc[ccmb]);
			else
				handle_odd_above_even(board,&tc[ccmb]);
			}

		board->sp=0;
		board->intgp.j=0;
		board->intgp.k=0;

        fprintf(h1,"Eliminating squares: ");

        for(x=0;x<BOARDX;x++)
            for(y=0;y<BOARDY;y++)
                if(!board->sqused[ELM(x,y)])
                    fprintf(h1,"%c%d ",x+'a',y+1);

        fprintf(h1,"\n\n");

		for(i=0;i<GROUPS;i++)
			{
			if(threat_group(board,i,WHITE) &&
				!wiped_group(board,i) && board->usablegroup[i])
				{
				board->intgp.tgroups[i]=YES;
				board->intgp.j++;
				}
			else board->intgp.tgroups[i]=NO;

			if(threat_group(board,i,BLACK))
				{
				board->intgp.mygroups[i]=YES;
				board->intgp.k++;
			}
			else board->intgp.mygroups[i]=NO;
			}

        for(y=0;y<BOARDY;y++)
            for(x=0;x<BOARDX;x++)
                if(board->square[ELM(x,y)]!=EMPTY &&
                   board->sqused[ELM(x,y)]==NO)
                      fatal_error("Marked as used an useless square...");

        fprintf(h1,"There are %d group(s) in which yellow can connect four men and ",board->intgp.j);
        fprintf(h1,"%d dangerous\ngroup(s) in which red can connect four men.\n",board->intgp.k);
        fprintf(h1,"\n");

        claimeven(board);
		baseinverse(board);
		vertical(board);
		aftereven(board);
		lowinverse(board);
		highinverse(board);
		baseclaim(board);
		before(board);

        if(board->intgp.j==0)
            {
            fprintf(h1,"This implies that a win is guaranteed.\n");
            oracle=YES;
            }

        else if(board->sp==0)
            {
            fprintf(h1,"No rules can be applied to solve the aforementioned groups in which\n");
            fprintf(h1,"red can connect four men. Therefore nothing can be said about yellow.\n");
            oracle=NO;
            }

        else
			{
            fprintf(h1,"Total number of rule instances is: %d\n\n",board->sp);

            matrix=(char **)allocate_matrix(board);
			build_adjacency_matrix(matrix,board);

            oracle=problem_solver(board,matrix,YES,h1);
			free_matrix(matrix,board);
			}

        fprintf(h1,"\n");

        anythreat++;
		} while(anythreat<thnum+combo && oracle==NO);

    if(oracle==YES && anythreat<thnum+combo)
        {
        fprintf(h1,"No further cases are taken into consideration since\n");
        fprintf(h1,"this last case is sufficient to prove yellow can win\n");
        fprintf(h1,"from this position.\n");
        }

    WSS_ENDING:

    fprintf(h1,"\n");
    fclose(h1);
    return oracle;
	}
Exemplo n.º 4
0
short ss_evaluate_black(struct board *board)
	{
    FILE *h1;
    short key,oracle;
	register x;
	char **matrix;
	short i,*bpos;

    h1=fopen(TMPFILENAME,"w");
    if(!h1) fatal_error("Cannot open temporary file");

    fprintf(h1,"Oracle analysis for red:\n\n");

	board->sp=0;

	board->intgp.j=0;
	board->intgp.k=0;

	memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));

	for(i=0;i<GROUPS;i++)
		{
		if(threat_group(board,i,BLACK))
			{
			board->intgp.tgroups[i]=YES;
			board->intgp.j++;
			}
		else board->intgp.tgroups[i]=NO;

		if(threat_group(board,i,WHITE))
			{
			board->intgp.mygroups[i]=YES;
			board->intgp.k++;
			}
		else board->intgp.mygroups[i]=NO;
		}

    fprintf(h1,"There are %d groups in which red can connect four men and ",board->intgp.j);
    fprintf(h1,"%d dangerous\ngroups in which yellow can connect four men.\n",board->intgp.k);
    fprintf(h1,"\n");

	claimeven(board);
	baseinverse(board);
	vertical(board);
	aftereven(board);
	lowinverse(board);
	highinverse(board);
	baseclaim(board);
	before(board);

    if(board->intgp.j==0)
        {
        fprintf(h1,"This implies that at least a draw is guaranteed.\n");

        oracle=YES;
        goto BSS_ENDING;
        }

    if(board->sp==0)
        {
        fprintf(h1,"No rules can be applied to solve the aforementioned groups in which\n");
        fprintf(h1,"yellow can connect four men. Therefore nothing can be said about red.\n");

        oracle=NO;
        goto BSS_ENDING;
        }

    fprintf(h1,"Total number of rule instances is: %d\n\n",board->sp);

	matrix=(char **)allocate_matrix(board);
	build_adjacency_matrix(matrix,board);

    oracle=problem_solver(board,matrix,YES,h1);
	free_matrix(matrix,board);

    BSS_ENDING:

    fprintf(h1,"\n");
    fclose(h1);
    return oracle;
	}
Exemplo n.º 5
0
short evaluate_white(struct board *board)
	{
	short x,y;
	struct threat_combo tc[GROUPS];
	short threats[MAXGROUPS],thnum=0,combo,ccmb;
	short key,oracle,anythreat=0;
	char **matrix;
	short i,*bpos,px,py;

	combo=threat_combo(board,tc);
	thnum=count_odd_threats(board,threats);
	if(thnum+combo==0) return NO;

	do
		{
		memset(board->usablegroup,0xff,GROUPS*sizeof(short));
		memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));
		memset(board->wipesq,0x00,(BOARDX+1)*(BOARDY+2)*sizeof(short));

		if(anythreat<thnum)
			{
			wipe_above(board,threats[anythreat]);
			wipe_odd(board,threats[anythreat]);

			px=ELX(threats[anythreat]);
			for(py=0;py<BOARDY;py++)
                if(board->square[ELM(px,py)]==EMPTY)
                    board->sqused[ELM(px,py)]=NO;
			}
		else
			{
			ccmb=anythreat-thnum;

			px=ELX(tc[ccmb].cross);
			x =ELX(tc[ccmb].odd);

			for(y=0;y<BOARDY;y++)
				{
				if(board->square[ELM(px,y)]==EMPTY)
					board->sqused[ELM(px,y)]=NO;

				if(board->square[ELM( x,y)]==EMPTY)
					board->sqused[ELM( x,y)]=NO;
				}

			if(ELY(tc[ccmb].even)>ELY(tc[ccmb].odd))
				handle_even_above_odd(board,&tc[ccmb]);
			else
				handle_odd_above_even(board,&tc[ccmb]);
			}

		board->sp=0;
		board->intgp.j=0;
		board->intgp.k=0;

		for(i=0;i<GROUPS;i++)
			{
			if(threat_group(board,i,WHITE) &&
				!wiped_group(board,i) && board->usablegroup[i])
				{
				board->intgp.tgroups[i]=YES;
				board->intgp.j++;
				}
			else board->intgp.tgroups[i]=NO;

			if(threat_group(board,i,BLACK))
				{
				board->intgp.mygroups[i]=YES;
				board->intgp.k++;
			}
			else board->intgp.mygroups[i]=NO;
			}

        for(y=0;y<BOARDY;y++)
            for(x=0;x<BOARDX;x++)
                if(board->square[ELM(x,y)]!=EMPTY &&
                   board->sqused[ELM(x,y)]==NO)
                      fatal_error("Marked as used an useless square...");

		claimeven(board);
		baseinverse(board);
		vertical(board);
		aftereven(board);
		lowinverse(board);
		highinverse(board);
		baseclaim(board);
		before(board);

		if(board->intgp.j==0) oracle=YES;
		else if(board->sp==0) oracle=NO;
		else
			{
			matrix=(char **)allocate_matrix(board);
			build_adjacency_matrix(matrix,board);

            oracle=problem_solver(board,matrix,NO,NULL);
			free_matrix(matrix,board);
			}

		anythreat++;
		} while(anythreat<thnum+combo && oracle==NO);

	return oracle;
	}
Exemplo n.º 6
0
void debug_white(struct board *board)
	{
	short x,y,x1,y1,f,g,j;
	struct threat_combo tc[GROUPS];
	short threats[GROUPS],thnumb,anythreat=0,ccmb;
	short key,oracle,combo,pentas;
	char **matrix;
	short i,*bpos,px,py;

	if(check_early_win(board))
		{
		printf("An early win is detected! (ESC to return)\n\r");
		while(readkey()!=0x1b);
		return;
		}

    pentas=0;
	combo=threat_combo(board,tc);

	thnumb=count_odd_threats(board,threats);
	if(thnumb+combo==0 && !pentas)
		{
		printf("No decisive threats, (ESC to return)\n\r");
		while(readkey()!=0x1b);
		return;
		}

	do
		{
		memset(board->usablegroup,0xff,GROUPS*sizeof(short));
		memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));
		memset(board->wipesq,0x00,(BOARDX+1)*(BOARDY+2)*sizeof(short));

		if(anythreat<thnumb)
			{
			wipe_above(board,threats[anythreat]);
			wipe_odd(board,threats[anythreat]);

			px=ELX(threats[anythreat]);
			for(py=0;py<BOARDY;py++)
                if(board->square[ELM(px,py)]==EMPTY)
                    board->sqused[ELM(px,py)]=NO;
			}
		else if(anythreat<thnumb+combo)
			{
			ccmb=anythreat-thnumb;

			px=ELX(tc[ccmb].cross);
			x =ELX(tc[ccmb].odd);

			for(y=0;y<BOARDY;y++)
				{
				if(board->square[ELM(px,y)]==EMPTY)
					board->sqused[ELM(px,y)]=NO;

				if(board->square[ELM( x,y)]==EMPTY)
					board->sqused[ELM( x,y)]=NO;
				}

			if(ELY(tc[ccmb].even)>ELY(tc[ccmb].odd))
				handle_even_above_odd(board,&tc[ccmb]);
            else handle_odd_above_even(board,&tc[ccmb]);
            }
		else fatal_error("What am I doing?");

		board->sp=0;
		board->intgp.j=0;
		board->intgp.k=0;

		for(i=0;i<GROUPS;i++)
			{
			if(threat_group(board,i,WHITE) &&
				!wiped_group(board,i) && board->usablegroup[i])
				{
				board->intgp.tgroups[i]=YES;
				board->intgp.j++;
				}
			else board->intgp.tgroups[i]=NO;

			if(threat_group(board,i,BLACK))
				{
				board->intgp.mygroups[i]=YES;
				board->intgp.k++;
				}
			else board->intgp.mygroups[i]=NO;
			}

		claimeven(board);
		baseinverse(board);
		vertical(board);
		aftereven(board);
		lowinverse(board);
		highinverse(board);
		baseclaim(board);
		before(board);

		dump_instances(board);
		dump_file_board(board);

		if(anythreat<thnumb) dump_file_threat(board,anythreat);

		matrix=(char **)allocate_matrix(board);
		build_adjacency_matrix(matrix,board);

        oracle=problem_solver(board,matrix,NO,NULL);

		printf("\n\r");
		printf("Total sol. found : %d, total dangerous groups %d, groups solved %d\n\r",
				board->sp,board->intgp.j,board->problem_solved);


		if(anythreat<thnumb)
			{
			printf("Odd threat at %c%d, oracle spits out %d\n\r",
						ELX(threats[anythreat])+97,ELY(threats[anythreat])+1,oracle);
			}
		else if(anythreat<thnumb+combo)
			{
			printf("Double odd threat at %c%d, oracle spits out %d\n\r",
						ELX(tc[anythreat-thnumb].cross)+97,ELY(tc[anythreat-thnumb].cross)+1,oracle);
			}
		else
			{
			printf("Pentas found.\n\r",pentas);
			}


        printf("1 - 9 to see rules instances, ESC to return to game play\n");
        printf("j to dump the non adjacencies, t to test rule compatibility\n");
        printf("s to show squares available\n");

		while((key=readkey())!=0x1b)
			{
			switch(key)
				{
				case '1':
					dump_solutions(board,CLAIMEVEN);
					break;

				case '2':
					dump_solutions(board,BASEINVERSE);
					break;

				case '3':
					dump_solutions(board,VERTICAL);
					break;

				case '4':
					dump_solutions(board,AFTEREVEN);
					break;

				case '5':
					dump_solutions(board,LOWINVERSE);
					break;

				case '6':
					dump_solutions(board,HIGHINVERSE);
					break;

				case '7':
					dump_solutions(board,BASECLAIM);
					break;

				case '8':
					dump_solutions(board,BEFORE);
					break;

				case '9':
					dump_solutions(board,SPECIALBEFORE);
					break;

				case 'j':
					dump_no_adjacencies(matrix,board);
					break;

				case 't':
					test_conditions(matrix,board);
					break;

                case 's':
                    show_square_used(board);
                    break;

				}
			}

		free_matrix(matrix,board);
		anythreat++;
		} while(anythreat<thnumb+combo || pentas);

	}
Exemplo n.º 7
0
void debug_black(struct board *board)
	{
	short key,oracle;
	register x;
	char **matrix;
	short i,*bpos;


	board->sp=0;

	board->intgp.j=0;
	board->intgp.k=0;

	memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));

	for(i=0;i<GROUPS;i++)
		{
		if(threat_group(board,i,BLACK))
			{
			board->intgp.tgroups[i]=YES;
			board->intgp.j++;
			}
		else board->intgp.tgroups[i]=NO;

		if(threat_group(board,i,WHITE))
			{
			board->intgp.mygroups[i]=YES;
			board->intgp.k++;
			}
		else board->intgp.mygroups[i]=NO;
		}

	claimeven(board);
	baseinverse(board);
	vertical(board);
	aftereven(board);
	lowinverse(board);
	highinverse(board);
	baseclaim(board);
	before(board);

	dump_instances(board);
	dump_file_board(board);

	matrix=(char **)allocate_matrix(board);
	build_adjacency_matrix(matrix,board);

    oracle=problem_solver(board,matrix,NO,NULL);

	printf("\n\r");
	printf("Total sol. found : %d, total dangerous groups %d, groups solved %d\n\r",
				board->sp,board->intgp.j,board->problem_solved);
	printf("Oracle spits out %d\n\r",oracle);

	printf("Key 1 - 9 to see rules instances, ESC to return to game play\n\r");
	printf("j to dump the non adjacencies, t to test rule compatibility\n\r");

	while((key=readkey())!=0x1b)
		{
		switch(key)
			{
			case '1':
				dump_solutions(board,CLAIMEVEN);
				break;

			case '2':
				dump_solutions(board,BASEINVERSE);
				break;

			case '3':
				dump_solutions(board,VERTICAL);
				break;

			case '4':
				dump_solutions(board,AFTEREVEN);
				break;

			case '5':
				dump_solutions(board,LOWINVERSE);
				break;

			case '6':
				dump_solutions(board,HIGHINVERSE);
				break;

			case '7':
				dump_solutions(board,BASECLAIM);
				break;

			case '8':
				dump_solutions(board,BEFORE);
				break;

			case '9':
				dump_solutions(board,SPECIALBEFORE);
				break;

			case 'j':
				dump_no_adjacencies(matrix,board);
				break;

			case 't':
				test_conditions(matrix,board);
				break;
			}
		}

	free_matrix(matrix,board);
	}
Exemplo n.º 8
0
ParserTestMetric test_KernelPerceptronModel(void *mdl, const CoNLLCorpus corpus, bool use_temp_weight, FILE *gold_ofp, FILE *model_ofp) {
    ParserTestMetric metric = create_ParserTestMetric();

    PerceptronModel linear_mdl;
    KernelPerceptron kernel_mdl;
    if (kernel == KLINEAR) {
        linear_mdl = (PerceptronModel) mdl;
    } else {
        kernel_mdl = (KernelPerceptron) mdl;
    }

    for (int si = 0; si < DArray_count(corpus->sentences); si++) {
        FeaturedSentence sent = DArray_get(corpus->sentences, si);

        debug("Test sentence %d (section %d) of length %d", si, sent->section, sent->length);

        if (kernel != KLINEAR) {
                debug("Generating feature matrix for sentence %d", si);
                set_FeatureMatrix(NULL, corpus, si);
        }

        if (kernel == KLINEAR) {
            if (use_temp_weight) {
                debug("\tI will be using a weight vector (raw) of length %ld", linear_mdl->embedding_w_temp->n);
                build_adjacency_matrix(corpus, si, linear_mdl->embedding_w_temp, NULL);
            } else {
                debug("\tI will be using a weight vector (averaged) of length %ld", linear_mdl->embedding_w->n);
                build_adjacency_matrix(corpus, si, linear_mdl->embedding_w, NULL);
            }

        } else {
            debug("Generating adj. matrix for sentence %d", si);
            set_adjacency_matrix_fast(corpus, si, kernel_mdl, true);
        }


        debug("Now parsing sentence %d", si);
        int *model = parse(sent);

        (metric->total_sentence)++;
        debug("Now comparing actual arcs with model generated arcs for sentence %d (Last sentence is %d)", si, sent->length);
        for (int j = 0; j < sent->length; j++) {
            Word w = (Word) DArray_get(sent->words, j);

            w->predicted_parent = model[j + 1];

            // TODO: One file per section idea 
            if (model_ofp != NULL)
                dump_conll_word(w, true, model_ofp);

            if (gold_ofp != NULL)
                dump_conll_word(w, false, gold_ofp);

            if (w->parent == 0 && model[j + 1] == 0)
                (metric->true_root_predicted)++;

            debug("\tTrue parent of word %d (with %s:%s) is %d whereas estimated parent is %d", j + 1, w->postag, w->form, w->parent, model[j + 1]);

            int pmatch_nopunc = 0, ptotal_nopunc = 0, pmatch = 0;
            if (strcmp(w->postag, ",") != 0 && strcmp(w->postag, ":") != 0 && strcmp(w->postag, ".") != 0 && strcmp(w->postag, "``") != 0 && strcmp(w->postag, "''") != 0) {

                if (w->parent == model[j + 1]) {
                    (metric->without_punc->true_prediction)++;
                    pmatch_nopunc++;
                }

                ptotal_nopunc++;

                (metric->without_punc->total_prediction)++;
            }

            if (pmatch_nopunc == ptotal_nopunc && pmatch_nopunc != 0) {
                (metric->complete_sentence_without_punc)++;
            }


            (metric->all->total_prediction)++;

            if (w->parent == model[j + 1]) {
                pmatch++;
                (metric->all->true_prediction)++;
            }

            if (pmatch == sent->length && pmatch != 0)
                (metric->complete_sentence)++;
        }

        if (model_ofp != NULL) {
            fprintf(model_ofp, "\n");
        }

        if (gold_ofp != NULL) {
            fprintf(gold_ofp, "\n");
        }

        free(model);


        debug("Releasing feature matrix for sentence %d", si);

        free_feature_matrix(corpus, si);
    }

    return metric;
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
void balance_graph(int cycle)
{
	int i, j;

	//------------------------------------------------
 	/* do static first */	
	balance_static1(cycle);

	/* if no quests, no clustering */
	if( sv.wl_quest_count == 0 || sv.wl_quest_spread == 0 ) return;

	//--------------------------------------------------
	/* get border conflicts */
	if( !borders ) 	{
		borders = (grid_border_t**) malloc (n_grid_units*sizeof(grid_border_t*)); assert(borders);
		for(i=0;i<n_grid_units;i++) {
			borders[i] = (grid_border_t*) malloc (m_grid_units*sizeof(grid_border_t)); assert(borders[i]);
		}
	}
	total_conflicts = grid_border_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, borders);

	//--------------------------------------------------
	/* do graph assignment */
	n_nodes = n_grid_units*m_grid_units;
	allocate_memory();
	if( !(build_adjacency_matrix(cycle)) ) return;

	//-------------------------------------------	
	/* build connected components */
	build_connected_components(cycle);

	//-------------------------------------------
	/* build mappings */
	build_mappings(cycle);

	//----- ANALYZE COMPONENTS ------------------
	// Ncomp <= Nthreads,  each thread gets one component [some may get no players, for now]
	// Ncomp >  Nthreads,  distribute extra components in descending order of weight (using less loaded thread criterion)
	
	if(sv.num_threads <= nrcomp)
	{
		#ifdef DEBUG_GRAPH
		if((cycle+1) % 100 == 0)
			printf("Ncomp=%d  >=  %d=Nthreads\n", nrcomp, sv.num_threads);
		#endif

		for(i=0; i< sv.num_threads; i++)
		{
			thread_stats[i] = 0;
			for(j=0; j<n_nodes; j++)
			{
				if(!(marked[j])) continue;
				if(mappings[j] == i)   
					thread_stats[i] += grid[j%n_grid_units][j/n_grid_units].n_players;
			}
		}

		// distribute the extra components
		for(i=sv.num_threads; i<nrcomp; i++)
		{
			int least_loaded = grid_least_loaded_thread(thread_stats);

			//compsize[least_loaded] += compsize[i];
			for(j=0; j<n_nodes; j++)
			{
				if(!(marked[j])) continue;
				if(mappings[j] == i)   {
					mappings[j] = least_loaded;
					thread_stats[least_loaded] += grid[j%n_grid_units][j/n_grid_units].n_players;					
				}
			}
		}

		for(i=0;i<n_nodes;i++)
		{
			if(marked[i]) continue;			
			thread_stats[mappings[i]] += grid[i%n_grid_units][i/n_grid_units].n_players;
		}
	}
#ifndef NO_SPLIT_COMPONENTS
	else // nrcomp < sv.num_threads
	{
		int ii, ij, max, root_node, rni, rnj, node_dist_min, dist_min_i, dist_min_j;
		double r_dist, min_dist;

		// do
		//     select largest component (largest by number of players)
		//
		//     split in two components comp1 and comp2 as follows:
		//		select a root node
		//		assign root to comp1
		//		do
		//			select node X closest to root
		//			assign X to comp1
		//		until  ||comp1| - |comp2|| < eps, (eps = precision threshold)
		// until(nrcomp == num_threads)

		// do bisections
		while(sv.num_threads != nrcomp)
		{
			root_node = -1;
			max = -1;

			// select largest component
			for(i=0;i<nrcomp;i++)
				if( (max == -1) || (compsize[i] > compsize[max]) )
					max = i;
			assert( max != -1);
			assert( compsize[max] != 1 );
			compsize[nrcomp] = 0;

			// select root node as node with largest grid index sum (i+j)
			for(i=0;i<n_nodes;i++)
			{
				if(!(marked[i])) continue;				
				if(mappings[i] != max) continue;
				
				if( root_node == -1 ) {root_node = i; continue;}

				ii = i%n_grid_units;
				ij = i/n_grid_units;				
				rni = root_node%n_grid_units;
				rnj = root_node/n_grid_units;

				if( (ii + ij) > (rni + rnj) )	root_node = i;
				//if( ii > rni )	root_node = i;
			}				
			assert( root_node != -1 );

			#ifdef DEBUG_GRAPH
			if((cycle+1) % 100 == 0)
			{
				printf("root_node = %d (%d,%d)\n", root_node, root_node%n_grid_units, root_node/n_grid_units);
			}
			#endif

			#ifdef DEBUG_GRAPH
			if((cycle+1) % 100 == 0)
			{
				printf("Maxcompsize[%d] = %d   Before assigning root\n", max, compsize[max]);
			}
			#endif

			// start from root_node
			// assign N closest nodes to root_node, (where N ~= half the size of the maximum component)
			rni = root_node%n_grid_units;
			rnj = root_node/n_grid_units;
			mappings[root_node] = nrcomp;
			compsize[max]    -= grid[rni][rnj].n_players;
			compsize[nrcomp] += grid[rni][rnj].n_players;

			#ifdef DEBUG_GRAPH
			if((cycle+1) % 100 == 0)
			{
				printf("Maxcompsize[%d] = %d   After assigning root\n", max, compsize[max]);
			}
			#endif

			while( (abs(compsize[nrcomp] - compsize[max]) * 100 / (sv.n_clients) > 5) && 
//			       (abs(compsize[nrcomp] - compsize[max]) * 100 / (compsize[max] + compsize[nrcomp]) > 10) && 
			       (compsize[nrcomp] < compsize[max]) )
			{
				node_dist_min = -1;
				min_dist = 0;
				for(i=0; i<n_nodes; i++)
				{
					if(!(marked[i])) continue;
					if(mappings[i] != max) continue;
					if(node_dist_min == -1) {
						node_dist_min = i; 
						dist_min_i = node_dist_min%n_grid_units;
						dist_min_j = node_dist_min/n_grid_units;					
						min_dist = sqrt((rni-dist_min_i)*(rni-dist_min_i) + (rnj-dist_min_j)*(rnj-dist_min_j));
						continue;
					}	

					ii = i%n_grid_units;
					ij = i/n_grid_units;
					dist_min_i = node_dist_min%n_grid_units;
					dist_min_j = node_dist_min/n_grid_units;					
					r_dist = sqrt((rni-ii)*(rni-ii) + (rnj-ij)*(rnj-ij));
					
					if(r_dist < min_dist)
					{
						node_dist_min = i;
						min_dist = r_dist;
					}
				}
				assert(node_dist_min != -1);

				dist_min_i = node_dist_min%n_grid_units;
				dist_min_j = node_dist_min/n_grid_units;					
				mappings[node_dist_min] = nrcomp;
				compsize[max]    -= grid[dist_min_i][dist_min_j].n_players;
				compsize[nrcomp] += grid[dist_min_i][dist_min_j].n_players;
			}			

			nrcomp++;

			#ifdef DEBUG_GRAPH
			if((cycle+1) % 100 == 0)
			{
				printf("-----Split-----\n");
				for(i=0;i<nrcomp;i++)
				{
					printf("\tCompsize[%d]=%d\n", i, compsize[i]);
				}
				printf("\nMap after split:\n");
				for(j=m_grid_units-1; j>=0; j--)
				{
					for(i=0;i<n_grid_units;i++)
					{
						int i1 = j*n_grid_units + i;
	
						if( !(marked[i1]) ) { printf("  ---"); continue; }
				
						printf("%5d", mappings[i1]);
					}
					printf("\n");
				}
				printf("\n");
			}
			#endif
		}	
		
		// update thread_stats
		for(i = 0; i < sv.num_threads; i++)	thread_stats[i] = 0;

		for(i = 0; i < n_nodes; i++)
		{
			ii = i%n_grid_units;
			ij = i/n_grid_units;

			thread_stats[mappings[i]]      += grid[ii][ij].n_players;
		}
	}
#endif //NO_SPLIT_COMPONENTS

	//-------------------------------------------
	/* set grid unit mappings */
	for(i=0; i<n_nodes; i++)
	{
		if( !(marked[i]) ) continue;
		grid[i%n_grid_units][i/n_grid_units].tid = mappings[i];
	}
	
	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );
}