Exemple #1
0
void
rot_cells (int n1, int n2)
{
  int rn, cn;
  int nn;
  CELLREF r1, r2, c1, c2;

  if (n1 + 1 == n2 || n2 + 1 == n1)
    {
      swp_cells (n1, n2);
      return;
    }
#ifdef TEST
  if (debug & 04)
    io_error_msg ("Rot cells %u -- %u", n1, n2);
#endif
  for (rn = sort_ele.lr; rn <= sort_ele.hr; rn++)
    for (cn = sort_ele.lc; cn <= sort_ele.hc; cn++)
      {

	/* store a copy of cell # n2 */
	r2 = sort_rng.lr + (n2 * erdiff) + rn;
	c2 = sort_rng.lc + (n2 * ecdiff) + cn;
	move_cell (r2, c2, NON_ROW, NON_COL);

#ifdef TEST
	if (debug & 04)
	  io_error_msg ("Save r%uc%u", r2, c2);
#endif
	/* Copy each cell from n1 to n2-1 up one */
	for (nn = n2; nn > n1; --nn)
	  {
	    r2 = sort_rng.lr + (nn * erdiff) + rn;
	    c2 = sort_rng.lc + (nn * ecdiff) + cn;

	    r1 = sort_rng.lr + ((nn - 1) * erdiff) + rn;
	    c1 = sort_rng.lc + ((nn - 1) * ecdiff) + cn;

	    move_cell (r1, c1, r2, c2);
#ifdef TEST
	    if (debug & 04)
	      io_error_msg ("Copy r%uc%u --> r%uc%u", r1, c1, r2, c2);
#endif
	    /* push_cell(r2,c2); */
	  }

	r1 = sort_rng.lr + (nn * erdiff) + rn;
	c1 = sort_rng.lc + (nn * ecdiff) + cn;
#ifdef TEST
	if (debug & 04)
	  io_error_msg ("Restore r%uc%u", r1, c1);
#endif
	move_cell (NON_ROW, NON_COL, r1, c1);

	/* push_cell(r1,c1); */
      }
}
Exemple #2
0
void
swp_cells (int n1, int n2)
{
  int rn, cn;
  CELLREF r1, r2, c1, c2;

#ifdef TEST
  if (debug & 04)
    io_error_msg ("Swap %u<-->%u", n1, n2);
#endif
  for (rn = sort_ele.lr; rn <= sort_ele.hr; rn++)
    for (cn = sort_ele.lc; cn <= sort_ele.hc; cn++)
      {
	r1 = sort_rng.lr + (n1 * erdiff) + rn;
	r2 = sort_rng.lr + (n2 * erdiff) + rn;
	c1 = sort_rng.lc + (n1 * ecdiff) + cn;
	c2 = sort_rng.lc + (n2 * ecdiff) + cn;
#ifdef TEST
	if (debug & 04)
	  io_error_msg ("Save  r%uc%u", r1, c1);
#endif
	move_cell (r1, c1, NON_ROW, NON_COL);
#ifdef TEST
	if (debug & 04)
	  io_error_msg ("Copy r%uc%u --> r%uc%u", r2, c2, r1, c1);
#endif
	move_cell (r2, c2, r1, c1);

#ifdef TEST
	if (debug & 04)
	  io_error_msg ("Restore r%uc%u", r2, c2);
#endif
	move_cell (NON_ROW, NON_COL, r2, c2);

	/* push_cell(r1,c1);
			push_cell(r2,c2); */
      }
}
Exemple #3
0
void		next_move(int ret, t_cell board[X][X])
{
	t_cell	*cell;
	int		i;
	uint8_t	moved;

	reset_merge(board);
	i = 0;
	moved = 0;
	while ((cell = get_cell(board, i, ret)) != NULLPTR)
	{
		i++;
		if (cell->value == 0)
			continue ;
		move_cell(cell, ret, board, &moved);
	}
	if (moved)
		grid_make_random(board);
}
Exemple #4
0
int map_editor(SDL_Surface *screen) {
    int i, j;

    FILE* fichier = NULL;
    fichier = fopen("map/map_default.txt", "w+"); //opens the target file that will contain the custom map
    fprintf(fichier, "12:12\n");
    int placecursor = 5;
    int valuecursor = 1;
    int current_cell = 1;
    screen_editor(screen, placecursor);
    SDL_Event event;
    int keep = 1;
    t_tab tab = malloc(sizeof (struct s_tab));
    SDL_Rect place[100];
    SDL_Surface * cell[144] = {CELL_EMPTY}; //creates as much surfaces as possible
    t_cell_type grid[12][12];   //the grid will be used to write the cell types in the file
    for (i = 0; i <= 11; i++) {
        for (j = 0; j <= 11; j++) {
            grid[i][j] = 0;
        }
    }
    for (i = 0; i <= 100; i++) { //each surface will be firstly located in (0,0)
        place[i].x = 0;
        place[i].y = 0;
    }
    tab->x = 0;
    tab->y = 0;
    tab->sprite_type = NULL;
    tab->empty = 0;
    tab->player_set=0;

    int white_bloc = ((12 * SIZE_BLOC) - 6 * SIZE_BLOC) / 4;
    while (keep != 0) {
        tab->empty = 0;
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
        screen_editor(screen, placecursor);

        if (current_cell > 1) { //Sticks all the cells the user has defined
            for (i = 1; i <= current_cell - 1; i++) {

                SDL_BlitSurface(cell[i], NULL, screen, &place[i]);

            }
        }
        SDL_Flip(screen);
        SDL_WaitEvent(&event);

        switch (event.type) {


            case SDL_QUIT:
                return 2;
                break;

            case SDL_KEYDOWN:

                switch (event.key.keysym.sym) {

                    case SDLK_RIGHT: //move the cursor that chooses which cell type to stick
                        if (placecursor < 10 * SIZE_BLOC) {
                            placecursor = placecursor + 0.25 * white_bloc + SIZE_BLOC;
                            valuecursor++;
                        }

                        break;
                    case SDLK_LEFT:
                        if (placecursor > 5) {
                            valuecursor--;
                            placecursor = placecursor - 0.25 * white_bloc - SIZE_BLOC;
                        }

                        break;
                    case SDLK_RETURN: //validates the choice of a cell type

                        if (valuecursor == 1) {
                            tab->sprite_type = IMG_Load(IMG_MAP_STONE);
                            tab->value_sprite = 21;
                        } else if (valuecursor == 2) {
                            tab->sprite_type = IMG_Load(IMG_MAP_TREE);
                            tab->value_sprite = 22;
                        } else if (valuecursor == 3) {
                            tab->sprite_type = IMG_Load(IMG_MAP_KEY);
                            tab->value_sprite = 8;
                        } else if (valuecursor == 4) {
                            tab->sprite_type = IMG_Load(IMG_MAP_CASE);
                            tab->value_sprite = 4;
                        } else if (valuecursor == 5) {
                            tab->sprite_type = IMG_Load(IMG_MAP_CLOSED_DOOR);
                            tab->value_sprite = 10;
                        } else if (valuecursor == 6) {
                            tab->sprite_type = IMG_Load(IMG_MONSTER_DOWN);
                            tab->value_sprite = 6;
                        } else if (valuecursor == 7) {
                            tab->sprite_type = IMG_Load(IMG_PLAYER_DOWN);
                            tab->value_sprite = 3;
                            if(tab->player_set==1){break;} //check if a player is already placed
                            tab->player_set=1;
                        } else if (valuecursor == 7) {
                            tab->sprite_type = IMG_Load(IMG_PLAYER_DOWN);
                            tab->value_sprite = 3;

                        } else if (valuecursor == 8) {
                            tab->sprite_type = IMG_Load(IMG_ERASER);
                            tab->value_sprite = 0;
                            tab->empty = 1;
                        }
                        SDL_BlitSurface(tab->sprite_type, NULL, screen, &place[current_cell]);
                        SDL_Flip(screen);

                        move_cell(tab->x, tab->y, screen, tab->sprite_type, tab, placecursor, place, cell, current_cell);
                        place[current_cell].x = tab->x;
                        place[current_cell].y = tab->y;

                        if (tab->empty == 1) { //if the user wants to correct something he did wrong
                            for (i = 1; i <= current_cell - 1; i++) {
                                if (place[i].x == tab->x && place[i].y == tab->y) {
                                    cell[i] = CELL_EMPTY;
                                }
                                if(grid[place[i].x/SIZE_BLOC][place[i].y/SIZE_BLOC]==3){tab->player_set=0;}
                            }
                        } else { //or he defines a currently empty case or replaces an existing case
                            for (i = 1; i <= current_cell - 1; i++) {
                                if (place[i].x == tab->x && place[i].y == tab->y && cell[i] != CELL_EMPTY) {
                                    cell[i] = CELL_EMPTY;
                                }
                            }
                            cell[current_cell] = tab->sprite_type;
                        }
                        grid[tab->x / SIZE_BLOC][tab->y / SIZE_BLOC] = tab->value_sprite; //sets the grid cell
                        current_cell++;
                        for (i = 1; i <= current_cell - 1; i++) {
                            SDL_BlitSurface(cell[i], NULL, screen, &place[i]);
                        }
                        tab->x = 0;
                        tab->y = 0;
                        SDL_Flip(screen);


                        break;
                    case SDLK_d: // the user is done

                        for (i = 0; i <= 11; i++) { //write it all in the file
                            for (j = 0; j <= 11; j++) {
                                fprintf(fichier, "%d", grid[j][i]);
                                if (j != 11) {
                                    fprintf(fichier, " ");
                                }
                            }
                            fprintf(fichier, "\n");
                        }
                        keep = 0;

                        break;


                    default:
                        screen_editor(screen, placecursor);
                        break;
                }

                break;


        }

    }
    fclose(fichier);
    return 0;
}
int main(int argc, char *argv[])
{    
    /* definitions */
    int nocells;           /* number of cells */
    int nonets;            /* number of nets */
    int nopins;            /* number of pins */
    int noparts;           /* number of partitions */
    int totsize;           /* total net weight of the partition */
    int totcellsize;       /* total cell weight of the partition */
    int cutsize;           /* cutsize of the partition */
    int max_gain;          /* max gain of a cell */
    int max_density;       /* max density of a cell */
    int max_cweight;       /* max cell weight */
    int max_nweight;       /* max net weight */
    int bucketsize;        /* max size of a bucket array */
    int msize;             /* index to mcells */

    if (argc < 5) {
        printf("\nUsage: %s InputFileName NoParts InCount OutCount [Seed]\n", argv[0]);
	printf("\t#cells moved per phase = incount * nocells / 4\n");
	printf("\t\tUse 1, 2, 3, or 4 for incount.\n");
	printf("\t#cells moved per pass = nocells if outcount = 1,\n");
	printf("\t#cells moved per pass = nocells * noparts if outcount = 2, and\n");
	printf("\t#cells moved per pass = nocells * noparts * noparts if outcount = 3.\n");
        exit(1);
    }  /* if */

    char fname[STR_SIZE];
    sprintf(fname, "%s", argv[1]);

    noparts = atoi(argv[2]);

    int incount = atoi(argv[3]);
    int outcount = atoi(argv[4]);

    long seed;
    if (argc > 5) {
        seed = (long) atoi(argv[5]);
    } else {
        seed = (long) -1;
    }
    seed = randomize((long)  seed);
    printf("SEED = %ld fname = %s\n", seed, fname);

    read_graph_size(fname, &nocells, &nonets);
    nopins = 2 * nonets;

    /* determine what in- & out-count imply */
    int max_moved_cells = incount * nocells / 4;
    switch (outcount) {
    case 1 : outcount = nocells; break;
    case 2 : outcount = nocells * noparts; break;
    case 3 : outcount = nocells * noparts * noparts; break;
    default : break;
    }
    /* max_noiter = outcount / max_moved_cells;*/ /* do that many iterations */
    int max_noiter = outcount;

    /* alloc memory for all data structures */
    cells_t *cells = (cells_t *) calloc(nocells, sizeof(cells_t));
    assert(cells != NULL);
    cells_info_t *cells_info = (cells_info_t *) calloc(nocells, sizeof(cells_info_t));
    assert(cells_info != NULL);
    for (int i = 0; i < nocells; i++) {
        cells_info[i].mgain = (int *) calloc(noparts, sizeof(int));
        cells_info[i].partb_ptr = (bnode_ptr_t *) calloc(noparts - 1, sizeof(bnode_ptr_t));
        cells_info[i].partb_gain_inx = (int *) calloc(noparts - 1, sizeof(int));
    }

    nets_t *nets = (nets_t *) calloc(nonets, sizeof(nets_t));
    assert(nets != NULL);

    /* cells of nets */
    corn_t *cnets = (corn_t *) calloc(nopins, sizeof(corn_t));
    assert(cnets != NULL);

    /* partition buckets */
    partb_t partb[noparts][noparts - 1];  
    parts_info_t parts_info[noparts]; 

    /* population (w/ one individual!) */
    ind_t pop[MAX_POP];             
    for (int i = 0; i < MAX_POP; i++) {
        pop[i].chrom = (allele *) calloc(nocells, sizeof(allele));
        pop[i].parts = (parts_t *) calloc(noparts, sizeof(parts_t));
    }

    /* selected cell */
    selected_cell_t scell[1];     

    /* moved cells */
    mcells_t *mcells = (mcells_t *) calloc(2 * max_noiter, sizeof(mcells_t));
    assert(mcells != NULL);

    /* temp chrom */
    allele *tchrom = (allele *) calloc(nocells, sizeof(allele));
    assert(tchrom != NULL);
  
    read_graph(fname, nocells, nonets, noparts, &totsize, &totcellsize,
               &max_density, &max_cweight, &max_nweight,
               cells, nets, cnets);

    max_gain = max_density * max_nweight;
    bucketsize = 2 * max_gain + 1;

    /* alloc memory (statically if possible) */
    for (int i = 0; i < noparts; i++) {
        for (int j = 0; j < noparts - 1; ++j) {
            partb[i][j].bnode_ptr = (bnode_ptr_t *) calloc(bucketsize, sizeof(bnode_ptr_t));
        }
    }

    create_partition(nocells, noparts, totcellsize, 
                     cells, &pop[0]);

#ifdef DEBUG
    printf("Initial : Part_no min_size curr_size max_size\n");
    for (int i = 0; i < noparts; i++) {
        printf("II %d %d %d %d\n", i, pop[0].parts[i].pmin_size,
               pop[0].parts[i].pcurr_size, pop[0].parts[i].pmax_size);
    }
#endif

    init_buckets(noparts, bucketsize, partb);
    cutsize = find_cut_size(nonets, totsize, nets, &pop[0]);

#ifdef DEBUG
    printf("Totalsize = %d Initial cutsize = %d\n", totsize, cutsize);
#endif

    int gain_sum;
    int glob_inx = 0;
    int pass_no = 0;
    do {

        copy_partition(noparts, parts_info, &pop[0]);

        for (int i = 0; i < nocells; i++) {
            tchrom[i] = pop[0].chrom[i];
        }

        msize = 0;

        int noiter = 0;
        while (noiter < max_noiter) {

            compute_gains(nocells, noparts, tchrom, 
                          cells, nets, cnets, cells_info);

            create_buckets(nocells, noparts, max_gain, tchrom, partb, cells_info);

            /* max_moved_cells = nocells / 2; */
            int nlocked = 0;
            do {

                int move_possible = select_cell(noparts, scell, parts_info, cells, 
                                                partb, cells_info);

                delete_partb_nodes_of_cell(noparts, scell[0].mov_cell_no, 
                                           scell[0].from_part, partb, cells_info);

                /* lock cell */
                cells_info[scell[0].mov_cell_no].locked = True;
                if (move_possible == True) {
                    move_cell(mcells, msize, scell, tchrom);  
                    msize++;
                    update_gains(noparts, max_gain, scell, tchrom,
                                 cells, nets, cnets,
                                 partb, cells_info);
                }   /* if */
                nlocked++;

                noiter++;
            } while ((nlocked < max_moved_cells) && (noiter < max_noiter)); 

            free_nodes(noparts, bucketsize, partb);

        }   /* while */

        int max_mcells_inx;
        gain_sum = find_move_set(mcells, msize, &max_mcells_inx);

#ifdef DEBUG
        printf("gain_sum=%d max_mcells_inx=%d msize = %d\n",
               gain_sum, max_mcells_inx, msize);
#endif
        if (gain_sum > 0) {
            int cut_gain = move_cells(False, mcells, max_mcells_inx, cutsize, &glob_inx, 
                                      &pop[0], cells);
            cutsize -= cut_gain;
        }   /* if */
        pass_no++;

#ifdef DEBUG
        printf("pass_no = %d Final cutsize = %d Check cutsize = %d\n", pass_no,
               cutsize, find_cut_size(nonets, totsize, nets, &pop[0]));
#endif

    } while ((gain_sum > 0) && (cutsize > 0));

    printf("pass_no = %d Final cutsize = %d Check cutsize = %d\n", pass_no,
           cutsize, find_cut_size(nonets, totsize, nets, &pop[0]));

    free_nodes(noparts, bucketsize, partb);

#ifdef DEBUG
    printf("Final : Part_no min_size curr_size max_size\n");
    for (int i = 0; i < noparts; i++) {
        printf("FF %d %d %d %d\n", i, pop[0].parts[i].pmin_size,
               pop[0].parts[i].pcurr_size, pop[0].parts[i].pmax_size);
    }
#endif

    /* free memory for all data structures */
    cfree(cells);
    for (int i = 0; i < nocells; i++) {
        cfree(cells_info[i].mgain);
        cfree(cells_info[i].partb_ptr);
        cfree(cells_info[i].partb_gain_inx);
    }
    cfree(cells_info);

    cfree(nets);

    cfree(cnets);

    for (int i = 0; i < noparts; i++) {
        for (int j = 0; j < noparts - 1; ++j) {
            cfree(partb[i][j].bnode_ptr);
        }
    }

    for (int i = 0; i < MAX_POP; i++) {
        cfree(pop[i].chrom);
        cfree(pop[i].parts);
    }

    cfree(mcells);

    cfree(tchrom);

    return (0);
}   /* main-plm */