-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·935 lines (581 loc) · 18.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*********************************************************************
* (C) Copyright 2001 Albert Ludwigs University Freiburg
* Institute of Computer Science
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************************/
/*
* THIS SOURCE CODE IS SUPPLIED ``AS IS'' WITHOUT WARRANTY OF ANY KIND,
* AND ITS AUTHOR AND THE JOURNAL OF ARTIFICIAL INTELLIGENCE RESEARCH
* (JAIR) AND JAIR'S PUBLISHERS AND DISTRIBUTORS, DISCLAIM ANY AND ALL
* WARRANTIES, INCLUDING BUT NOT LIMITED TO ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND
* ANY WARRANTIES OR NON INFRINGEMENT. THE USER ASSUMES ALL LIABILITY AND
* RESPONSIBILITY FOR USE OF THIS SOURCE CODE, AND NEITHER THE AUTHOR NOR
* JAIR, NOR JAIR'S PUBLISHERS AND DISTRIBUTORS, WILL BE LIABLE FOR
* DAMAGES OF ANY KIND RESULTING FROM ITS USE. Without limiting the
* generality of the foregoing, neither the author, nor JAIR, nor JAIR's
* publishers and distributors, warrant that the Source Code will be
* error-free, will operate without interruption, or will meet the needs
* of the user.
*/
/*********************************************************************
* File: main.c
* Description: The main routine for the FastForward Planner.
*
* Author: Joerg Hoffmann 2000
*
*********************************************************************/
#include "ff.h"
#include "memory.h"
#include "output.h"
#include "parse.h"
#include "inst_pre.h"
#include "inst_easy.h"
#include "inst_hard.h"
#include "inst_final.h"
#include "orderings.h"
#include "relax.h"
#include "search.h"
/*
* ----------------------------- GLOBAL VARIABLES ----------------------------
*/
/*******************
* GENERAL HELPERS *
*******************/
/* used to time the different stages of the planner
*/
float gtempl_time = 0, greach_time = 0, grelev_time = 0, gconn_time = 0;
float gsearch_time = 0;
/* the command line inputs
*/
struct _command_line gcmd_line;
/* number of states that got heuristically evaluated
*/
int gevaluated_states = 0;
/* maximal depth of breadth first search
*/
int gmax_search_depth = 0;
/***********
* PARSING *
***********/
/* used for pddl parsing, flex only allows global variables
*/
int gbracket_count;
char *gproblem_name;
/* The current input line number
*/
int lineno = 1;
/* The current input filename
*/
char *gact_filename;
/* The pddl domain name
*/
char *gdomain_name = NULL;
/* loaded, uninstantiated operators
*/
PlOperator *gloaded_ops = NULL;
/* stores initials as fact_list
*/
PlNode *gorig_initial_facts = NULL;
/* not yet preprocessed goal facts
*/
PlNode *gorig_goal_facts = NULL;
/* axioms as in UCPOP before being changed to ops
*/
PlOperator *gloaded_axioms = NULL;
/* the types, as defined in the domain file
*/
TypedList *gparse_types = NULL;
/* the constants, as defined in domain file
*/
TypedList *gparse_constants = NULL;
/* the predicates and their arg types, as defined in the domain file
*/
TypedListList *gparse_predicates = NULL;
/* the objects, declared in the problem file
*/
TypedList *gparse_objects = NULL;
/* connection to instantiation ( except ops, goal, initial )
*/
/* all typed objects
*/
FactList *gorig_constant_list = NULL;
/* the predicates and their types
*/
FactList *gpredicates_and_types = NULL;
/*****************
* INSTANTIATING *
*****************/
/* global arrays of constant names,
* type names (with their constants),
* predicate names,
* predicate aritys,
* defined types of predicate args
*/
Token gconstants[MAX_CONSTANTS];
int gnum_constants = 0;
Token gtype_names[MAX_TYPES];
int gtype_consts[MAX_TYPES][MAX_TYPE];
Bool gis_member[MAX_CONSTANTS][MAX_TYPES];
int gmember_nr[MAX_CONSTANTS][MAX_TYPES];/* nr of object within a type */
int gtype_size[MAX_TYPES];
int gnum_types = 0;
Token gpredicates[MAX_PREDICATES];
int garity[MAX_PREDICATES];
int gpredicates_args_type[MAX_PREDICATES][MAX_ARITY];
int gnum_predicates = 0;
/* the domain in integer (Fact) representation
*/
Operator_pointer goperators[MAX_OPERATORS];
int gnum_operators = 0;
Fact *gfull_initial;
int gnum_full_initial = 0;
WffNode *ggoal = NULL;
/* stores inertia - information: is any occurence of the predicate
* added / deleted in the uninstantiated ops ?
*/
Bool gis_added[MAX_PREDICATES];
Bool gis_deleted[MAX_PREDICATES];
/* splitted initial state:
* initial non static facts,
* initial static facts, divided into predicates
* (will be two dimensional array, allocated directly before need)
*/
Facts *ginitial = NULL;
int gnum_initial = 0;
Fact **ginitial_predicate;
int *gnum_initial_predicate;
/* the type numbers corresponding to any unary inertia
*/
int gtype_to_predicate[MAX_PREDICATES];
int gpredicate_to_type[MAX_TYPES];
/* (ordered) numbers of types that new type is intersection of
*/
TypeArray gintersected_types[MAX_TYPES];
int gnum_intersected_types[MAX_TYPES];
/* splitted domain: hard n easy ops
*/
Operator_pointer *ghard_operators;
int gnum_hard_operators;
NormOperator_pointer *geasy_operators;
int gnum_easy_operators;
/* so called Templates for easy ops: possible inertia constrained
* instantiation constants
*/
EasyTemplate *geasy_templates;
int gnum_easy_templates;
/* first step for hard ops: create mixed operators, with conjunctive
* precondition and arbitrary effects
*/
MixedOperator *ghard_mixed_operators;
int gnum_hard_mixed_operators;
/* hard ''templates'' : pseudo actions
*/
PseudoAction_pointer *ghard_templates;
int gnum_hard_templates;
/* store the final "relevant facts"
*/
Fact grelevant_facts[MAX_RELEVANT_FACTS];
int gnum_relevant_facts = 0;
int gnum_pp_facts = 0;
/* the final actions and problem representation
*/
Action *gactions;
int gnum_actions;
State ginitial_state;
State ggoal_state;
/**********************
* CONNECTIVITY GRAPH *
**********************/
/* one ops (actions) array ...
*/
OpConn *gop_conn;
int gnum_op_conn;
/* one effects array ...
*/
EfConn *gef_conn;
int gnum_ef_conn;
/* one facts array.
*/
FtConn *gft_conn;
int gnum_ft_conn;
/*******************
* SEARCHING NEEDS *
*******************/
/* the goal state, divided into subsets
*/
State *ggoal_agenda;
int gnum_goal_agenda;
/* byproduct of fixpoint: applicable actions
*/
int *gA;
int gnum_A;
/* communication from extract 1.P. to search engines:
* 1P action choice
*/
int *gH;
int gnum_H;
/* the effects that are considered true in relaxed plan
*/
int *gin_plan_E;
int gnum_in_plan_E;
/* always stores (current) serial plan
*/
int gplan_ops[MAX_PLAN_LENGTH];
int gnum_plan_ops = 0;
/* stores the states that the current plan goes through
* ( for knowing where new agenda entry starts from )
*/
State gplan_states[MAX_PLAN_LENGTH + 1];
/*
* ----------------------------- HEADERS FOR PARSING ----------------------------
* ( fns defined in the scan-* files )
*/
void get_fct_file_name( char *filename );
void load_ops_file( char *filename );
void load_fct_file( char *filename );
/*
* ----------------------------- MAIN ROUTINE ----------------------------
*/
struct tms lstart, lend;
int main( int argc, char *argv[] )
{
/* resulting name for ops file
*/
char ops_file[MAX_LENGTH] = "";
/* same for fct file
*/
char fct_file[MAX_LENGTH] = "";
struct tms start, end;
State current_start, current_end;
int i, j;
Bool found_plan;
times ( &lstart );
/* command line treatment
*/
if ( argc == 1 || ( argc == 2 && *++argv[0] == '?' ) ) {
ff_usage();
exit( 1 );
}
if ( !process_command_line( argc, argv ) ) {
ff_usage();
exit( 1 );
}
/* make file names
*/
/* one input name missing
*/
if ( !gcmd_line.ops_file_name ||
!gcmd_line.fct_file_name ) {
fprintf(stdout, "\nff: two input files needed\n\n");
ff_usage();
exit( 1 );
}
/* add path info, complete file names will be stored in
* ops_file and fct_file
*/
sprintf(ops_file, "%s%s", gcmd_line.path, gcmd_line.ops_file_name);
sprintf(fct_file, "%s%s", gcmd_line.path, gcmd_line.fct_file_name);
/* parse the input files
*/
/* start parse & instantiation timing
*/
times( &start );
/* domain file (ops)
*/
if ( gcmd_line.display_info >= 1 ) {
printf("\nff: parsing domain file");
}
/* it is important for the pddl language to define the domain before
* reading the problem
*/
load_ops_file( ops_file );
/* problem file (facts)
*/
if ( gcmd_line.display_info >= 1 ) {
printf(" ... done.\nff: parsing problem file");
}
load_fct_file( fct_file );
if ( gcmd_line.display_info >= 1 ) {
printf(" ... done.\n\n");
}
/* This is needed to get all types.
*/
build_orig_constant_list();
/* last step of parsing: see if it's an ADL domain!
*/
if ( !make_adl_domain() ) {
printf("\nff: this is not an ADL problem!");
printf("\n can't be handled by this version.\n\n");
exit( 1 );
}
/* now instantiate operators;
*/
/**************************
* first do PREPROCESSING *
**************************/
/* start by collecting all strings and thereby encoding
* the domain in integers.
*/
encode_domain_in_integers();
/* inertia preprocessing, first step:
* - collect inertia information
* - split initial state into
* _ arrays for individual predicates
* - arrays for all static relations
* - array containing non - static relations
*/
do_inertia_preprocessing_step_1();
/* normalize all PL1 formulae in domain description:
* (goal, preconds and effect conditions)
* - simplify formula
* - expand quantifiers
* - NOTs down
*/
normalize_all_wffs();
/* translate negative preconds: introduce symmetric new predicate
* NOT-p(..) (e.g., not-in(?ob) in briefcaseworld)
*/
translate_negative_preconds();
/* split domain in easy (disjunction of conjunctive preconds)
* and hard (non DNF preconds) part, to apply
* different instantiation algorithms
*/
split_domain();
/***********************************************
* PREPROCESSING FINISHED *
* *
* NOW MULTIPLY PARAMETERS IN EFFECTIVE MANNER *
***********************************************/
build_easy_action_templates();
build_hard_action_templates();
times( &end );
TIME( gtempl_time );
times( &start );
/* perform reachability analysis in terms of relaxed
* fixpoint
*/
perform_reachability_analysis();
times( &end );
TIME( greach_time );
times( &start );
/* collect the relevant facts and build final domain
* and problem representations.
*/
collect_relevant_facts();
times( &end );
TIME( grelev_time );
times( &start );
/* now build globally accessable connectivity graph
*/
build_connectivity_graph();
times( &end );
TIME( gconn_time );
/***********************************************************
* we are finally through with preprocessing and can worry *
* bout finding a plan instead. *
***********************************************************/
times( &start );
/* another quick preprocess: approximate goal orderings and split
* goal set into sequence of smaller sets, the goal agenda
*/
compute_goal_agenda();
/* make space in plan states info, and relax
*/
for ( i = 0; i < MAX_PLAN_LENGTH + 1; i++ ) {
make_state( &(gplan_states[i]), gnum_ft_conn );
gplan_states[i].max_F = gnum_ft_conn;
}
make_state( ¤t_start, gnum_ft_conn );
current_start.max_F = gnum_ft_conn;
make_state( ¤t_end, gnum_ft_conn );
current_end.max_F = gnum_ft_conn;
initialize_relax();
source_to_dest( &(gplan_states[0]), &ginitial_state );
source_to_dest( ¤t_start, &ginitial_state );
source_to_dest( ¤t_end, &(ggoal_agenda[0]) );
for ( i = 0; i < gnum_goal_agenda; i++ ) {
if ( !do_enforced_hill_climbing( ¤t_start, ¤t_end ) ) {
break;
}
source_to_dest( ¤t_start, &(gplan_states[gnum_plan_ops]) );
if ( i < gnum_goal_agenda - 1 ) {
for ( j = 0; j < ggoal_agenda[i+1].num_F; j++ ) {
current_end.F[current_end.num_F++] = ggoal_agenda[i+1].F[j];
}
}
}
found_plan = ( i == gnum_goal_agenda ) ? TRUE : FALSE;
if ( !found_plan ) {
printf("\n\nEnforced Hill-climbing failed !");
printf("\nswitching to Best-first Search now.\n");
fflush(stdout);
found_plan = do_best_first_search();
}
times( &end );
TIME( gsearch_time );
if ( found_plan ) {
print_plan();
}
output_planner_info();
printf("\n\n");
exit( 0 );
}
/*
* ----------------------------- HELPING FUNCTIONS ----------------------------
*/
void output_planner_info( void )
{
printf( "\n\ntime spent: %7.2f seconds instantiating %d easy, %d hard action templates",
gtempl_time, gnum_easy_templates, gnum_hard_mixed_operators );
printf( "\n %7.2f seconds reachability analysis, yielding %d facts and %d actions",
greach_time, gnum_pp_facts, gnum_actions );
printf( "\n %7.2f seconds creating final representation with %d relevant facts",
grelev_time, gnum_relevant_facts );
printf( "\n %7.2f seconds building connectivity graph",
gconn_time );
printf( "\n %7.2f seconds searching, evaluating %d states, to a max depth of %d",
gsearch_time, gevaluated_states, gmax_search_depth );
printf( "\n %7.2f seconds total time",
gtempl_time + greach_time + grelev_time + gconn_time + gsearch_time );
printf("\n\n");
exit( 0 );
print_official_result();
}
FILE *out;
void print_official_result( void )
{
int i;
char name[MAX_LENGTH];
sprintf( name, "%s.soln", gcmd_line.fct_file_name );
if ( (out = fopen( name, "w")) == NULL ) {
printf("\n\nCan't open official output file!\n\n");
return;
}
times( &lend );
fprintf(out, "Time %d\n",
(int) ((lend.tms_utime - lstart.tms_utime + lend.tms_stime - lstart.tms_stime) * 10.0));
for ( i = 0; i < gnum_plan_ops; i++ ) {
print_official_op_name( gplan_ops[i] );
fprintf(out, "\n");
}
fclose( out );
}
void print_official_op_name( int index )
{
int i;
Action *a = gop_conn[index].action;
if ( a->norm_operator ||
a->pseudo_action ) {
fprintf(out, "(%s", a->name );
for ( i = 0; i < a->num_name_vars; i++ ) {
fprintf(out, " %s", gconstants[a->name_inst_table[i]]);
}
fprintf(out, ")");
}
}
void ff_usage( void )
{
printf("\nusage of ff:\n");
printf("\nOPTIONS DESCRIPTIONS\n\n");
printf("-p <str> path for operator and fact file\n");
printf("-o <str> operator file name\n");
printf("-f <str> fact file name\n\n");
printf("-i <num> run-time information level( preset: 1 )\n");
printf(" 0 only times\n");
printf(" 1 problem name, planning process infos\n");
printf(" 101 parsed problem data\n");
printf(" 102 cleaned up ADL problem\n");
printf(" 103 collected string tables\n");
printf(" 104 encoded domain\n");
printf(" 105 predicates inertia info\n");
printf(" 106 splitted initial state\n");
printf(" 107 domain with Wff s normalized\n");
printf(" 108 domain with NOT conds translated\n");
printf(" 109 splitted domain\n");
printf(" 110 cleaned up easy domain\n");
printf(" 111 unaries encoded easy domain\n");
printf(" 112 effects multiplied easy domain\n");
printf(" 113 inertia removed easy domain\n");
printf(" 114 easy action templates\n");
printf(" 115 cleaned up hard domain representation\n");
printf(" 116 mixed hard domain representation\n");
printf(" 117 final hard domain representation\n");
printf(" 118 reachability analysis results\n");
printf(" 119 facts selected as relevant\n");
printf(" 120 final domain and problem representations\n");
printf(" 121 connectivity graph\n");
printf(" 122 fixpoint result on each evaluated state\n");
printf(" 123 1P extracted on each evaluated state\n");
printf(" 124 H set collected for each evaluated state\n");
printf(" 125 False sets of goals <GAM>\n");
printf(" 126 detected ordering constraints leq_h <GAM>\n");
printf(" 127 the Goal Agenda <GAM>\n");
/* printf(" 109 reachability analysis results\n"); */
/* printf(" 110 final domain representation\n"); */
/* printf(" 111 connectivity graph\n"); */
/* printf(" 112 False sets of goals <GAM>\n"); */
/* printf(" 113 detected ordering constraints leq_h <GAM>\n"); */
/* printf(" 114 the Goal Agenda <GAM>\n"); */
/* printf(" 115 fixpoint result on each evaluated state <1Ph>\n"); */
/* printf(" 116 1P extracted on each evaluated state <1Ph>\n"); */
/* printf(" 117 H set collected for each evaluated state <1Ph>\n"); */
printf("\n-d <num> switch on debugging\n\n");
}
Bool process_command_line( int argc, char *argv[] )
{
char option;
gcmd_line.display_info = 1;
gcmd_line.debug = 0;
memset(gcmd_line.ops_file_name, 0, MAX_LENGTH);
memset(gcmd_line.fct_file_name, 0, MAX_LENGTH);
memset(gcmd_line.path, 0, MAX_LENGTH);
while ( --argc && ++argv ) {
if ( *argv[0] != '-' || strlen(*argv) != 2 ) {
return FALSE;
}
option = *++argv[0];
switch ( option ) {
default:
if ( --argc && ++argv ) {
switch ( option ) {
case 'p':
strncpy( gcmd_line.path, *argv, MAX_LENGTH );
break;
case 'o':
strncpy( gcmd_line.ops_file_name, *argv, MAX_LENGTH );
break;
case 'f':
strncpy( gcmd_line.fct_file_name, *argv, MAX_LENGTH );
break;
case 'i':
sscanf( *argv, "%d", &gcmd_line.display_info );
break;
case 'd':
sscanf( *argv, "%d", &gcmd_line.debug );
break;
default:
printf( "\nff: unknown option: %c entered\n\n", option );
return FALSE;
}
} else {
return FALSE;
}
}
}
return TRUE;
}