Example #1
0
void mark_nodes_unvisited(list_node *root)
{
  mark_nodes(root, 0);
}
Example #2
0
void solve (Problem * problem, unsigned * parcial_solution, unsigned level, unsigned last_used_block = 0) {
	unsigned tested_blocks = 0;
	MPI_Status status;
	int flag = 0;
	unsigned split_level = (problem->cells/TETROMINO_SIZE)*SPLIT_LEVEL;
	for(unsigned i = last_used_block; i<problem->block_count; i++) {
		if(problem->best_solution_price < EPSILON*problem->tree->best_posib_price) return;
		if(problem->active_blocks[i]) {
			//~ printf("added: %d\n", i);
			problem->active_blocks[i] = false;
			problem->used_blocks[problem->used_blocks_cnt++] = i;
			tested_blocks++;
			if(level != split_level || 
				(level == split_level && tested_blocks%problem->proc_cnt == (unsigned) problem->proc_id)) {
				parcial_solution[level] = i;
				
				//STAR - process information from other processes
				if(tested_blocks%10 == 9) {
					bool change = false;
					while(true) {
						MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);
						if (flag) {
							/* eceving message by blocking receive */
							double message;
							MPI_Recv(&message, 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
							printf ("proc %d recieved %f from %d\n",problem->proc_id , message, status.MPI_SOURCE);
							if(message < problem->best_solution_price) {
								problem->best_solution_price = message;
								problem->tree->best_price = message;
								problem->best_solution_src = status.MPI_SOURCE;
								change = true;
							}
						} else break;
					}
					if(change == true) {
						mark_nodes(problem->tree);
					}
				}
				//END - process information from other processes
				
				//~ if(level < ((problem->cells)/TETROMINO_SIZE)-9) {
				//~ if(level < 4) {
					//~ printf("L:%02d, working on:%d of (%d, used %d)\n", level, tested_blocks, problem->block_count, problem->used_blocks_cnt);
				//~ }
				
				if(pointless_sol(parcial_solution, level+1, problem) == false ) {
					
					unsigned blocked_blocks = 0;
					
					double price = cnt_solution_price(problem->cells, parcial_solution, level+1, problem->all_blocks);
					if(problem->best_solution_price > price) {
						problem->best_solution_price = price;
						problem->best_blocks_cnt = level+1;
						for(unsigned j = 0; j<=level; j++) {
							problem->best_solution[j] = parcial_solution[j];
						}
						problem->tree->best_price = price;
						mark_nodes(problem->tree);
						print_best_solution(problem);
						
						//START - Send solution proce to other processes
						problem->best_solution_src = problem->proc_id;
						for(int i = 0; i < problem->proc_cnt; i++) {
							if(i!=problem->proc_id) {
								MPI_Send(&price, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
							}
						}
						//END - Send solution proce to other processes
					}
					
					for(unsigned j = 0; j<TETROMINO_SIZE; j++) {
						unsigned cell = problem->all_blocks[i].positions[j];
						problem->active_cells[cell] = false;
						problem->used_cells[problem->used_cells_cnt++] = cell;
						for(unsigned k = 0; k<problem->cell_to_tetro[cell].size; k++) {
							unsigned block = problem->cell_to_tetro[cell].tetronimos[k];
							if (problem->active_blocks[block]) {
								problem->active_blocks[block] = false;
								problem->used_blocks[problem->used_blocks_cnt++] = block;
								blocked_blocks++;
							}
						}
					}
					
					if(problem->block_count > problem->used_blocks_cnt) {
						solve(problem, parcial_solution, level+1, i);
					}
					
					while(blocked_blocks--){
						unsigned block = problem->used_blocks[problem->used_blocks_cnt-1];
						problem->used_blocks_cnt--;
						problem->active_blocks[block] = true;
					}
					for(unsigned j = 0; j<TETROMINO_SIZE; j++) {
						unsigned cell = problem->used_cells[problem->used_cells_cnt-1];
						problem->used_cells_cnt--;
						problem->active_cells[cell] = true;
					}
				}
			}
		}
	}
	while(tested_blocks--) {
		unsigned block = problem->used_blocks[problem->used_blocks_cnt-1];
		problem->used_blocks_cnt--;
		problem->active_blocks[block] = true;
	}
}
Example #3
0
void mark_nodes_visited(list_node *root)
{
  mark_nodes(root, 1);
}