void create_integer_representation()

in grolp/gen/ff_planner/inst_pre.c [432:644]


void create_integer_representation( void )

{

  PlNode *n, *nn;
  PlOperator *o;
  Operator *tmp;
  FactList *ff;
  int type_num, i, j, sum;
  ExpNode *t;
  Effect *e;
  Literal *l;

  if ( gorig_initial_facts ) {
    sum = 0;
    for ( n = gorig_initial_facts->sons; n; n = n->next ) sum++;
    sum += gnum_constants;/* space for equalities */
    gfull_initial = ( Fact * ) calloc( sum, sizeof( Fact ) );
    gfull_fluents_initial = ( FluentValue * ) 
      calloc( sum, sizeof( FluentValue ));

    for ( n = gorig_initial_facts->sons; n; n = n->next ) {
      if ( n->connective == ATOM ) {
	make_Fact( &(gfull_initial[gnum_full_initial]), n, 0 );
	if ( gfull_initial[gnum_full_initial].predicate == 0 ) {
	  printf("\nequality in initial state! check input files.\n\n");
	  exit( 1 );
	}

	/* duplicate check!!
	 */
	for ( i = 0; i < gnum_full_initial; i++ ) {
	  if ( gfull_initial[i].predicate != gfull_initial[gnum_full_initial].predicate ) {
	    /* predicate different --> this ini fact is not a duplicate!
	     */
	    continue;
	  }
	  for ( j = 0; j < garity[gfull_initial[i].predicate]; j++ ) {
	    if ( gfull_initial[i].args[j] != gfull_initial[gnum_full_initial].args[j] ) {
	      /* arg different --> this ini fact is not a duplicate!
	       */
	      break;
	    }
	  }
	  if ( j == garity[gfull_initial[i].predicate] ) {
	    /* found a duplicate!
	     */
	    break;
	  }
	}
	if ( i < gnum_full_initial ) {
	  /* simply skip the second occurence...
	   */
	  continue;
	}

	gnum_full_initial++;
      } else {
	/* a fluent value assignment
	 */
	make_Fluent( &(gfull_fluents_initial[gnum_full_fluents_initial].fluent), 
		     n->lh->atom, 0 );
	gfull_fluents_initial[gnum_full_fluents_initial].value =
	  ( float ) strtod( n->rh->atom->item, NULL);
	gnum_full_fluents_initial++;
      }
    }
    free_PlNode( gorig_initial_facts );
  }

  /* now insert all our artificial equality constraints into initial state.
   */
  for ( i = 0; i < gnum_constants; i++ ) {
    gfull_initial[gnum_full_initial].predicate = 0;
    gfull_initial[gnum_full_initial].args[0] = i;
    gfull_initial[gnum_full_initial].args[1] = i;
    gnum_full_initial++;
  }
  /* FINITO. the rest of equality handling will fully
   * automatically be done by the rest of the machinery.
   */

  ggoal = make_Wff( gorig_goal_facts, 0 );

  if ( gparse_metric != NULL ) {
    /* no need to throw costs away, even if we're not explicitly asked to 
     * minimize them
     */
    if ( 0 && !gcost_minimizing ) {
      if ( gcmd_line.display_info ) {
	printf("\n\nno optimization required. skipping criterion.\n\n");
      }
    } else {
      gmetric = make_ExpNode( gparse_metric, 0 );
      if ( strcmp( gparse_optimization, "MINIMIZE" ) != SAME &&
	   strcmp( gparse_optimization, "minimize" ) != SAME &&
	   strcmp( gparse_optimization, "MAXIMIZE" ) != SAME &&
	   strcmp( gparse_optimization, "maximize" ) != SAME ) {
	if ( gcmd_line.display_info ) {
	  printf("\n\nunknown optimization method %s. check input files\n\n", 
		 gparse_optimization);
	}
	exit( 1 );
      }
      if ( strcmp( gparse_optimization, "MAXIMIZE" ) == SAME ||
	   strcmp( gparse_optimization, "maximize" ) == SAME ) {
	t = new_ExpNode( MINUS );
	t->son = gmetric;
	gmetric = t;
      }
    }
  }

  for ( o = gloaded_ops; o; o = o->next ) {
    tmp = new_Operator( o->name, o->number_of_real_params );
    tmp->axiom = o->axiom;

    for ( ff = o->params; ff; ff = ff->next ) {
      if ( (type_num = position_in_types_table( ff->item->next->item )) == -1 ) {
	printf("\nwarning: parameter %s of op %s has unknown or empty type %s. skipping op",
	       ff->item->item, o->name, ff->item->next->item);
	break;
      }
      if ( tmp->num_vars == MAX_VARS ) {
	printf("\ntoo many parameters! increase MAX_VARS (currently %d)\n\n",
	       MAX_VARS);
	exit( 1 );
      }
      for ( i = 0; i < tmp->num_vars; i++ ) {
	if ( tmp->var_names[i] == ff->item->item ||
	     strcmp( tmp->var_names[i], ff->item->item ) == SAME ) {
	  printf("\nwarning: operator %s parameter %s overwrites previous declaration\n\n",
		 tmp->name, ff->item->item);
	}
      }
      tmp->var_names[tmp->num_vars] = new_Token( strlen( ff->item->item ) + 1 );
      strcpy( tmp->var_names[tmp->num_vars], ff->item->item );
      tmp->var_types[tmp->num_vars++] = type_num;
    }
    if ( ff ) {
      free_Operator( tmp );
      continue;
    }

    for ( i = 0; i < tmp->num_vars; i++ ) {
      lvar_types[i] = tmp->var_types[i];
      lvar_names[i] = tmp->var_names[i];
    }

    tmp->preconds = make_Wff( o->preconds, tmp->num_vars );

    if ( o->effects ) {
      /* in make_effect, make sure that no one afects equality.
       */
      nn = o->effects->sons;
      while ( nn &&
	      (tmp->effects = make_effect( nn, tmp->num_vars )) == NULL ) {
	nn = nn->next;
      }
      if ( nn ) {
	for ( n = nn->next; n; n = n->next ) {
	  if ( (tmp->effects->prev = make_effect( n, tmp->num_vars )) == NULL ) {
	    continue;
	  }
	  tmp->effects->prev->next = tmp->effects;
	  tmp->effects = tmp->effects->prev;
	}
      }
    }

    if ( gnum_operators == MAX_OPERATORS ) {
      printf("\ntoo many operators! increase MAX_OPERATORS (currently %d)\n\n",
	     MAX_OPERATORS);
      exit( 1 );
    }
    goperators[gnum_operators++] = tmp;
  }

  if ( 0 ) {
    /* currently not in use; leads to free memory reads and
     * free memory frees (no memory leaks), which are hard to explain.
     *
     * almost no memory consumption anyway.
     */
    free_PlOperator( gloaded_ops );
  }

  /* establish gaxiom_added markers.
   * ascertain that derived predicates do not appear in effects!!
   */
  for ( i = 0; i < gnum_operators; i++ ) {
    for ( e = goperators[i]->effects; e; e = e->next ) {
      for ( l = e->effects; l; l = l->next ) {
	if ( goperators[i]->axiom ) {
	  gaxiom_added[l->fact.predicate] = TRUE;
	} 
      }
    }
  }
  for ( i = 0; i < gnum_operators; i++ ) {
    for ( e = goperators[i]->effects; e; e = e->next ) {
      for ( l = e->effects; l; l = l->next ) {
	if ( !goperators[i]->axiom &&
	     gaxiom_added[l->fact.predicate] ) {
	  printf("\nA derived predicate appears in an operator effect.");
	  printf("\nSorry, this is not allowed. Bailing out.\n\n");
	  exit( 1 );
	} 
      }
    }
  }

}