Ejemplo n.º 1
0
void testOnce(void)
{
  double result;

  /* set up table */
  dtInit(5, 5, 5);

  dtAdd(0, 1, 2, 3, 0.5);
  dtAdd(0, 1, 2, 3, 0.7);

  dtAdd(0, 1, 3, -1, -10.1);
  dtAdd(0, 1, 3, -1, -10.3);

  dtAdd(1, 0, 0,  -1, 6.0);
  dtAdd(1, 0, -1, -1, 7.0);

  dtAdd(-1, 2, -1, -1, 2.7);

  /* do a few queries */
  result = dtGet(0, 1, 2, 3);
  printf("expecting: result=%lf\n", 0.7);
  printf("got:       result=%lf\n", result);

  result = dtGet(0, 1, 3, 1);
  printf("expecting: result=%lf\n", -10.3);
  printf("got:       result=%lf\n", result);

  result = dtGet(1, 0, 0, 0);
  printf("expecting: result=%lf\n", 7.0);
  printf("got:       result=%lf\n", result);

  result = dtGet(3, 2, 0, 0);
  printf("expecting: result=%lf\n", 2.7);
  printf("got:       result=%lf\n", result);

  /* clean up */
  dtDeallocate();
}
Ejemplo n.º 2
0
REAL_VALUE getImmediateReward( int action, int cur_state, int next_state,
			   int obs ) {
#if USE_DECISION_TREE
  return dtGet(action, cur_state, next_state, obs);
#else
  Imm_Reward_List temp = gImmRewardList;
  REAL_VALUE return_value = 0.0;

  assert(( action >= 0) && (action < gNumActions)
	 && (cur_state >= 0) && (cur_state < gNumStates)
	 && (next_state >= 0) && (next_state < gNumStates));

  while( temp != NULL ) {
    
    if((( temp->action == WILDCARD_SPEC )
	|| ( temp->action == action ))) {

      switch( temp->type ) {
      case ir_value:

	if( gProblemType == POMDP_problem_type ) {
	  if((( temp->next_state == WILDCARD_SPEC )
	      || ( temp->next_state == next_state))
	     && ((temp->obs == WILDCARD_SPEC)
		 || (temp->obs == obs ))
	     && ((temp->cur_state == WILDCARD_SPEC)
		 || (temp->cur_state == cur_state ))) {

	    
	    return_value = temp->rep.value;
	    
	  }  /* if we have a match */
	}  /* if POMDP */

	else {  /* then it is an MDP */
	  if((( temp->cur_state == WILDCARD_SPEC )
	      || ( temp->cur_state == cur_state))
	     && ((temp->next_state == WILDCARD_SPEC)
		 || (temp->next_state == next_state ))) {
	    
	    return_value = temp->rep.value;
	    
	  }  /* if we have a match */
	}
	     break;
    
      case ir_vector:

	if( gProblemType == POMDP_problem_type ) {
	  if((( temp->next_state == WILDCARD_SPEC )
	      || ( temp->next_state == next_state))
	     && ((temp->cur_state == WILDCARD_SPEC)
		 || (temp->cur_state == cur_state ))) {
	    
	    return_value = temp->rep.vector[obs];
	  }
	}  /* if POMDP */

	else {  /* it is an MDP */
	  if(( temp->cur_state == WILDCARD_SPEC )
	     || ( temp->cur_state == cur_state)) {
	    
	    return_value = temp->rep.vector[next_state];
	  }
	}

	break;
    
      case ir_matrix:
	if( gProblemType == POMDP_problem_type )  {
	  if(( temp->cur_state == WILDCARD_SPEC )
	     || (temp->cur_state == cur_state ))
	    return_value = getEntryMatrix( temp->rep.matrix, next_state,
					obs );
	}
	else
	  return_value = getEntryMatrix( temp->rep.matrix, cur_state,
					next_state );

	break;

      default:
	fprintf( stderr, 
		"** ERR ** Unreckognized IR_Type in getImmediateReward().\n");
	exit( -1 );
	break;
      }  /* switch */

    
    }  /* If we have a partially matching node */

    temp = temp->next;
  }  /* while */

  return( return_value );
#endif /* if USE_DECISION_TREE / else */
}  /* getImmediateReward */
Ejemplo n.º 3
0
double 
getImmediateReward( int action, int cur_state, int next_state,
		    int obs ) {
#if USE_DECISION_TREE
  double dt_return_value;
#endif
#if !USE_DECISION_TREE || CHECK_DECISION_TREE
  Imm_Reward_List temp = gImmRewardList;
  double return_value = 0.0;

  assert(( action >= 0) && (action < gNumActions)
	 && (cur_state >= 0) && (cur_state < gNumStates)
	 && (next_state >= 0) && (next_state < gNumStates));

  while( temp != NULL ) {
    
    if((( temp->action == WILDCARD_SPEC )
	|| ( temp->action == action ))) {

      switch( temp->type ) {
      case ir_value:

	if( gProblemType == POMDP_problem_type ) {
	  if((( temp->next_state == WILDCARD_SPEC )
	      || ( temp->next_state == next_state))
	     && ((temp->obs == WILDCARD_SPEC)
		 || (temp->obs == obs ))
	     && ((temp->cur_state == WILDCARD_SPEC)
		 || (temp->cur_state == cur_state ))) {

	    
	    return_value = temp->rep.value;
	    
	  }  /* if we have a match */
	}  /* if POMDP */

	else {  /* then it is an MDP */
	  if((( temp->cur_state == WILDCARD_SPEC )
	      || ( temp->cur_state == cur_state))
	     && ((temp->next_state == WILDCARD_SPEC)
		 || (temp->next_state == next_state ))) {
	    
	    return_value = temp->rep.value;
	    
	  }  /* if we have a match */
	}
	     break;
    
      case ir_vector:

	if( gProblemType == POMDP_problem_type ) {
	  if((( temp->next_state == WILDCARD_SPEC )
	      || ( temp->next_state == next_state))
	     && ((temp->cur_state == WILDCARD_SPEC)
		 || (temp->cur_state == cur_state ))) {
	    
	    return_value = temp->rep.vector[obs];
	  }
	}  /* if POMDP */

	else {  /* it is an MDP */
	  if(( temp->cur_state == WILDCARD_SPEC )
	     || ( temp->cur_state == cur_state)) {
	    
	    return_value = temp->rep.vector[next_state];
	  }
	}

	break;
    
      case ir_matrix:
	if( gProblemType == POMDP_problem_type )  {
	  if(( temp->cur_state == WILDCARD_SPEC )
	     || (temp->cur_state == cur_state ))
	    return_value = getEntryMatrix( temp->rep.matrix, next_state,
					obs );
	}
	else
	  return_value = getEntryMatrix( temp->rep.matrix, cur_state,
					next_state );

	break;

      default:
	fprintf( stderr, 
		"** ERR ** Unreckognized IR_Type in getImmediateReward().\n");
	exit( -1 );
	break;
      }  /* switch */

    
    }  /* If we have a partially matching node */

    temp = temp->next;
  }  /* while */
#endif /* if !USE_DECISION_TREE || CHECK_DECISION_TREE */


#if USE_DECISION_TREE
  dt_return_value = dtGet(action, cur_state, next_state, obs);
#if CHECK_DECISION_TREE
  if (return_value != dt_return_value) {
    fprintf(stderr,
	    "ERROR: getImmediateReward: decision-tree value and pattern match values disagree\n"
	    "  action=%d cur_state=%d next_state=%d obs=%d)\n"
	    "  decision-tree value=%g pattern match value=%g difference=%g\n",
	    action, cur_state, next_state, obs,
	    dt_return_value, return_value, fabs(dt_return_value - return_value));
    exit(EXIT_FAILURE);
  }
#endif
  return dt_return_value;
#else /* if USE_DECISION_TREE / else */
  return( return_value );
#endif /* if USE_DECISION_TREE / else */
  
}  /* getImmediateReward */