Ejemplo n.º 1
0
/************************************
* BEST FIRST SEARCH IMPLEMENTATION *
************************************/
Bool do_best_first_search( void ) {

	static Bool fc = TRUE; /*first round*/
	static State S;

	BfsNode *first;
	int i, min = INFINITY;
	Bool start = TRUE;

	if ( fc ) {
		make_state( &S, gnum_ft_conn );
		S.max_F = gnum_ft_conn;
		fc = FALSE;
	}

	lbfs_space_head = new_BfsNode();
	lbfs_space_had = NULL;

	for ( i = 0; i < BFS_HASH_SIZE; i++ ) {
		lbfs_hash_entry[i] = NULL;
	}

	add_to_bfs_space( &ginitial_state, -1, NULL );

	while ( TRUE ) {
		if ( (first = lbfs_space_head->next) == NULL ) {
			printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
			return FALSE;
		}

		lbfs_space_head->next = first->next;
		if ( first->next ) {
			first->next->prev = lbfs_space_head;
		}

		if ( LESS( first->h, min ) ) {
			min = first->h;
			if ( start ) {
				printf("\nadvancing to distance : %4d", min);
				start = FALSE;
			} else {
				printf("\n                        %4d", min);
			}

			/*
			* modified by JC: return ealier
			*/

			if(is_solved_state( &first->S )){
				printf("\nstate have seen. return!\n");
				break;
			}
		}

		if ( first->h == 0 ) {
			break;
		}

		get_A( &(first->S) );
		for ( i = 0; i < gnum_A; i++ ) {

			/*if(g_is_strong){*/
			/*JC: dismiss invalid actions*/
				int k;	
				Bool found = FALSE;
				for(k = 0; k < gnum_IV; k++){
					if(gA[i] == gInvActs[k].act){
						found = TRUE;
						break;
					}			
				}			
			/*}*/

			if(found) continue;

			result_to_dest( &S, &(first->S), gA[i] );
			add_to_bfs_space( &S, gA[i], first );
		}

		first->next = lbfs_space_had;
		lbfs_space_had = first;
	}

	extract_plan( first );
	return TRUE;
}
Ejemplo n.º 2
0
Bool do_best_first_search( void )

{

  static Bool fc = TRUE;
  static State S;

  BfsNode *first;
  int i, min = INFINITY;
  Bool start = TRUE;

  if ( fc ) {
    make_state( &S, gnum_ft_conn );
    fc = FALSE;
  }

  lbfs_space_head = new_BfsNode();
  lbfs_space_had = NULL;

  add_to_bfs_space( &ginitial_state, -1, NULL );

  while ( TRUE ) {
    if ( (first = lbfs_space_head->next) == NULL ) {
      printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
      return FALSE;
    }

    lbfs_space_head->next = first->next;
    if ( first->next ) {
      first->next->prev = lbfs_space_head;
    }

    if ( LESS( first->h, min ) ) {
      min = first->h;
      if ( start ) {
	printf("\nadvancing to distance : %4d", min);
	start = FALSE;
	fflush(stdout);
      } else {
	printf("\n                        %4d", min);
	fflush(stdout);
      }
    }

    if ( first->h == 0 ) {
      break;
    }

    get_A( &(first->S) );
    for ( i = 0; i < gnum_A; i++ ) {
      if ( !result_to_dest( &S, NULL, first, gA[i] ) ) continue;
      add_to_bfs_space( &S, gA[i], first );
    }

    first->next = lbfs_space_had;
    lbfs_space_had = first;
  }

  extract_plan( first );
  return TRUE;

}