-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_transitions.cpp
executable file
·3434 lines (2834 loc) · 87.4 KB
/
state_transitions.cpp
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 RES.LTING 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: state_transitions.c
*
* Description: computes new states. quite complicated.
*
* Author: Joerg Hoffmann 2002
*
*********************************************************************/
extern "C" {
#include "ff.h"
#include "output.h"
#include "memory.h"
#include "relax.h"
#include "state_transitions.h"
#include "repeated_states.h"
#include "search.h"
}
#include "Cachet-1.21-wmc/SAT.h"
#include "Solver.h"
// #include <set.h>
// #include <vector.h>
/***********************************************************
* LOCAL GLOBALS *
***********************************************************/
/* hitting set -- in CNF construction for noops in presence of non-unary antecedents.
*/
int *lhitting_set;
int lnum_hitting_set;
/* make space for global arrays needed here
*/
void initialize_state_transitions( void )
{
int i, j;
gclauses = ( TimedLiteral ** ) calloc( gmax_clauses, sizeof( TimedLiteral * ) );
gclause_length = ( int * ) calloc( gmax_clauses, sizeof( int ) );
for ( i = 0; i < gmax_clauses; i++ ) {
gclauses[i] = ( TimedLiteral * ) calloc( gmax_literals, sizeof( TimedLiteral ) );
gclause_length[i] = 0;
}
gnum_fixed_clauses = 0;
gnum_clauses = 0;
gcodes = ( int ** ) calloc( MAX_PLAN_LENGTH + 1, sizeof( int * ) );
/* july06: use artificial (ie new) decision vars
* saying whether a prob outcome actually occurs, + weioghted chance vars saying woith what prob.
* encode these as (gnum_ft_conn + gef_conn[ef].pef_id|pef_chance_id)
*/
for ( i = 0; i < MAX_PLAN_LENGTH + 1; i++ ) {
gcodes[i] = ( int * ) calloc( gnum_ft_conn + gnum_pef_conn, sizeof( int ) );
for ( j = 0; j < gnum_ft_conn + gnum_pef_conn; j++ ) {
gcodes[i][j] = -1;
}
}
gnum_fixed_c = 0;
/* july06: 1001 to make space for chance node codes.
*/
gcf = ( int * ) calloc( (1 + MAX_PLAN_LENGTH) * gmax_CNFU + 1001, sizeof( int ) );
gct = ( int * ) calloc( (1 + MAX_PLAN_LENGTH) * gmax_CNFU + 1001, sizeof( int ) );
/* july06: weight of dynaically created chance nodes
*/
gcweight = ( double * )
calloc( (1 + MAX_PLAN_LENGTH) * gmax_CNFU + 1001, sizeof( double ) );
lhitting_set = ( int * ) calloc( gnum_ft_conn + gnum_pef_conn, sizeof( int ) );
gdecision_stack = ( int * ) calloc( (1 + MAX_PLAN_LENGTH) * gmax_CNFU, sizeof( int ) );
/* statistics...
*/
gsum_k_clauses = ( float * ) calloc( gmax_literals + 1, sizeof( float ) );
for ( i = 0; i < gmax_literals + 1; i++ ) {
gsum_k_clauses[i] = 0;
}
ginitial_ft_weight = ( double * ) calloc( gnum_ft_conn, sizeof( double ) );
for ( i = 0; i < gnum_ft_conn; i++ ) {
if ( !gft_conn[i].weighted ) {
ginitial_ft_weight[i] = -1;
continue;
}
ginitial_ft_weight[i] = gft_conn[i].weight_a / gft_conn[i].weight_b;
}
}
/***********************************************************
* THIS HERE IS THE "MAIN" FN, CALLED DIRECTLY FROM SEARCH *
***********************************************************/
/* function that computes state transition as induced by a
* normalized ADL action under unknown literals.
* (uses CNF decision mechanism for the semantics of the latter)
*
* must take in search states as path to the source state is
* needed for computing inferred literals.
* when called from ehc, bfs_source is NULL and vice versa.
*
* implementation is largely naive, look at that again if it's
* runtime relevant --- which I doubt.
*/
Bool result_to_dest( State *dest,
EhcNode *ehc_source, BfsNode *bfs_source,
int op )
{
static Bool first_call = TRUE;
static Bool *F, *U, *Npp, *adds, *dels, *nadds, *ndels, *uadds, *udels, *unadds, *undels;
static int *check_pos, *check_neg;
State *source;
int num_cp, num_cn;
int i, j, fcount, ucount, ft, ef, k, endtime, nextstart, nextcstart;
Bool retval = TRUE;
if ( ehc_source ) {
source = &(ehc_source->S);
} else {
source = &(bfs_source->S);
}
if ( first_call ) {
F = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
U = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
Npp = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
adds = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
dels = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
nadds = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
ndels = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
uadds = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
udels = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
unadds = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
undels = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
check_pos = ( int * ) calloc( gnum_ft_conn, sizeof( int ) );
check_neg = ( int * ) calloc( gnum_ft_conn, sizeof( int ) );
for ( i = 0; i < gnum_ft_conn; i++ ) {
F[i] = FALSE;
U[i] = FALSE;
Npp[i] = FALSE;
adds[i] = FALSE;
dels[i] = FALSE;
nadds[i] = FALSE;
ndels[i] = FALSE;
uadds[i] = FALSE;
udels[i] = FALSE;
unadds[i] = FALSE;
undels[i] = FALSE;
}
first_call = FALSE;
}
/* setup direct F and U info
*/
for ( i = 0; i < source->num_F; i++ ) {
F[source->F[i]] = TRUE;
}
for ( i = 0; i < source->num_U; i++ ) {
U[source->U[i]] = TRUE;
}
/* setup known and unknown adds and dels.
*/
dest->num_F = 0;
dest->num_U = 0;
dest->num_unknown_E = 0;
for ( i = 0; i < gop_conn[op].num_E; i++ ) {
ef = gop_conn[op].E[i];
fcount = 0;
ucount = 0;
for ( j = 0; j < gef_conn[ef].num_C; j++ ) {
if ( F[gef_conn[ef].C[j]] ) fcount++;
if ( U[gef_conn[ef].C[j]] ) ucount++;
/* DEBUGGING TEST. REMOVE LATER.
*/
if ( F[gef_conn[ef].C[j]] && U[gef_conn[ef].C[j]] ) {
printf("\nsource F and U intersec non-empty?\n\n");
exit(1);
}
}
if ( fcount == gef_conn[ef].num_C ) {
if ( gef_conn[ef].eff_p >= 1 ) {
for ( j = 0; j < gef_conn[ef].num_A; j++ ) {
adds[gef_conn[ef].A[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_D; j++ ) {
dels[gef_conn[ef].D[j]] = TRUE;
}
} else {
/* july06: in any case, we need this guy in here
* so that the clauses will be done, for wmc.
*/
dest->unknown_E[dest->num_unknown_E++] = ef;
/* july06: common nondet adds/dels behave just like normal ones.
*/
for ( j = 0; j < gef_conn[ef].num_SA; j++ ) {
adds[gef_conn[ef].SA[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_SD; j++ ) {
dels[gef_conn[ef].SD[j]] = TRUE;
}
/* july06: non-common nondet adds/dels are special.
*/
for ( j = 0; j < gef_conn[ef].num_NSA; j++ ) {
nadds[gef_conn[ef].NSA[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_NSD; j++ ) {
ndels[gef_conn[ef].NSD[j]] = TRUE;
}
}
continue;
}
if ( fcount + ucount == gef_conn[ef].num_C ) {
dest->unknown_E[dest->num_unknown_E++] = ef;
if ( gef_conn[ef].eff_p >= 1 ) {
for ( j = 0; j < gef_conn[ef].num_A; j++ ) {
uadds[gef_conn[ef].A[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_D; j++ ) {
udels[gef_conn[ef].D[j]] = TRUE;
}
} else {
for ( j = 0; j < gef_conn[ef].num_SA; j++ ) {
uadds[gef_conn[ef].SA[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_SD; j++ ) {
udels[gef_conn[ef].SD[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_NSA; j++ ) {
unadds[gef_conn[ef].NSA[j]] = TRUE;
}
for ( j = 0; j < gef_conn[ef].num_NSD; j++ ) {
undels[gef_conn[ef].NSD[j]] = TRUE;
}
}
}
}
/* setup the CNF for the following SAT calls.
*/
/* EFFICIENCY: HERE, AND BELOW, ONE COULD DO SIMPLE PRE-CHECKS TO SEE
* WHETHER THERE'LL BE ANY SAT REASONING NEEDED::: ONLY IF EITHER
* OP CAN CONTRADICT ITSELF OR WHEN THERE ARE POS/NEG CHECK FACTS!!
*/
times( &end );
TIME( gsearch_time );
times( &start );
/* first, extend the current fixed clauses with the search path up to source.
* will be the same for all literals to be checked (?)
*/
endtime = extend_dynamic_clauses_base( dest,
ehc_source, bfs_source,
op,
Npp,
TRUE );
nextstart = gnum_clauses;
times( &end );
TIME( gcnf_time );
times( &start );
/* then extend the encoding, ie map the literal / time pairs
* into integers.
*
* Mar'04 NOTE: this is wasteful and completely unnecessary - we could just as well
* implement DP directly on the timed actions encoding.
* well... the int IDs help in indexing into the "assigned" array...
*
* the translation code is in table gcodes for use below in the
* single checks.
*/
extend_dynamic_clauses_base_encoding( gnum_fixed_clauses, gnum_fixed_c );
nextcstart = gnum_c;
times( &end );
TIME( genc_time );
times( &start );
/* check for possible contradictions.
*/
for ( ft = 0; ft < gnum_ft_conn; ft++ ) {
if ( adds[ft] && dels[ft] ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\naction ");
print_op_name( op );
printf(" contradicts itself. skipping it.");
}
retval = FALSE;
}
if ( adds[ft] && (udels[ft] || ndels[ft] || undels[ft]) ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\naction ");
print_op_name( op );
printf(" can delete an add. skipping it.");
print_ft_name( ft ); exit( 1 );
}
retval = FALSE;
}
if ( (uadds[ft] || nadds[ft] || unadds[ft]) && dels[ft] ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\naction ");
print_op_name( op );
printf(" can add a delete. skipping it.");
}
retval = FALSE;
}
/* july06: disabled. who gives a shit about this? it just takes time.
*/
// if ( uadds[ft] && udels[ft] ) {
// if ( gcmd_line.T && gcmd_line.debug ) {
// printf("\naction ");
// print_op_name( op );
// printf(" may contradict itself on fact ");
// print_ft_name( ft );
// printf(". checking that.");
// }
// if ( can_contradict_in( op, ft,
// dest, ehc_source, bfs_source, endtime - 1 ) ) {
// retval = FALSE;
// }
// }
}
if ( retval ) {
/* now proceed over all facts and see whether they will be true,
* false, or unknown for sure, and whether we must check if they
* become true or false.
*/
dest->num_F = 0;
dest->num_U = 0;
num_cp = 0;
num_cn = 0;
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n\n---------------------------state transition, setups");
}
for ( ft = 0; ft < gnum_ft_conn; ft++ ) {
if ( F[ft] ) {
/* this was true; note that per assumption all adds and dels
* are cap empty
*/
if ( dels[ft] ) {
/* remember, for use in CNF, that we *know* this is false
*/
Npp[ft] = TRUE;
continue;/* state-wise, false <-> do not include ft into F' or U' */
}
/* july06: this one gets deleted by some effects so it'll definitely
* be unknown at tpp.
*/
if ( ndels[ft] || undels[ft] ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n---becomes U due to nondet del ");
print_ft_name( ft );
}
dest->U[dest->num_U++] = ft;
continue;
}
/* note: order matters, ie the above are asked first!
*/
if ( udels[ft] ) {
/* might become negative; can not stay pos as then all del effs
* would need to provably stay out, and we'd know that
*/
check_neg[num_cn++] = ft;
continue;
}
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n---remains true ");
print_ft_name( ft );
}
dest->F[dest->num_F++] = ft;
continue;
}
if ( U[ft] ) {
/* this was unknown
*/
if ( adds[ft] ) {
dest->F[dest->num_F++] = ft;
continue;
}
if ( dels[ft] ) {
/* remember, for use in CNF, that we *know* this is false
*/
Npp[ft] = TRUE;
continue;
}
if ( uadds[ft] ) {
check_pos[num_cp++] = ft;
continue;
}
if ( udels[ft] ) {
check_neg[num_cn++] = ft;
continue;
}
dest->U[dest->num_U++] = ft;
continue;
}
/* this was false
*/
if ( adds[ft] ) {
dest->F[dest->num_F++] = ft;
continue;
}
/* july06: this one gets added by some effects so it'll definitely
* be unknown at tpp.
*/
if ( nadds[ft] || unadds[ft] ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n---becomes U due to nondet add ");
print_ft_name( ft );
}
dest->U[dest->num_U++] = ft;
continue;
}
/* note: order matters, ie the above are asked first!
*/
if ( uadds[ft] ) {
/* similar to above this can not stay false, so if it not
* becomes pos then it becomes U.
*/
check_pos[num_cp++] = ft;
continue;
}
/* if it's not added then we do nothing, leaving it false.
*
* we remember, though, for use in CNF, that we *know* this is false
*/
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n---remains false ");
print_ft_name( ft );
}
Npp[ft] = TRUE;
}
times( &end );
TIME( gsearch_time );
times( &start );
/* now, extend the current clauses with the step source -> dest.
* will be the same for all literals to be checked (?)
*/
endtime = extend_dynamic_clauses_base( dest,
ehc_source, bfs_source,
op,
Npp,
FALSE );
times( &end );
TIME( gcnf_time );
times( &start );
/* goes over fixed until num, so some of these have been encoded above;
* don't matter as in these cases coded remain the same !?
*/
extend_dynamic_clauses_base_encoding( nextstart, nextcstart );
gcnfs++;
times( &end );
TIME( genc_time );
times( &start );
/* hand the to-be-checked facts over to a SAT solver
*/
handle_inferred_literals( dest, ehc_source, bfs_source, op,
check_pos, num_cp,
check_neg, num_cn,
Npp, endtime );
/* DEBUGGING TEST. REMOVE LATER.
* (unknown E s computed at start already can not interfere with
* F' and U' values, can they?)
*/
if ( gcmd_line.T && gcmd_line.debug ) {
for ( i = 0; i < dest->num_unknown_E; i++ ) {
ef = dest->unknown_E[i];
/* can the effect del an F' fact?
*/
for ( j = 0; j < gef_conn[ef].num_D; j++ ) {
for ( k = 0; k < dest->num_F; k++ ) {
if ( gef_conn[ef].D[j] == dest->F[k] ) {
printf("\nunknown effect deletes F'?");
}
}
}
/* can the effect add an N' fact?
*/
for ( j = 0; j < gef_conn[ef].num_A; j++ ) {
for ( k = 0; k < dest->num_F; k++ ) {
if ( gef_conn[ef].A[j] == dest->F[k] ) {
break;
}
}
if ( k < dest->num_F ) continue;
for ( k = 0; k < dest->num_U; k++ ) {
if ( gef_conn[ef].A[j] == dest->U[k] ) {
break;
}
}
if ( k < dest->num_U ) continue;
printf("\nunknown effect adds N'?");
}
}
}
/* finally, check whether this new state stagnates, ie whether
* it is not the case that one fact can be true in it that isn't
* in source. stagnating states are cut out by making the action
* inapplicable.
*
* (NOTE: stagnation test easy to implement and set up here as
* CNF already present in gclauses!!)
*/
if ( gcmd_line.stagnating && stagnates( dest, ehc_source, bfs_source, endtime ) ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nsuccessor state stagnates! skipping action!");
}
retval = FALSE;
}
} /* if retval */
/* undo the clauses and encoding infos
*/
for ( i = gnum_fixed_clauses; i < gnum_clauses; i++ ) {
gclause_length[i] = 0;
}
gnum_clauses = gnum_fixed_clauses;
for ( i = gnum_fixed_c + 1; i <= gnum_c; i++ ) {
gcodes[gct[i]][gcf[i]] = -1;
}
/* unset direct infos
*/
for ( i = 0; i < gnum_ft_conn; i++ ) {
F[i] = FALSE;
U[i] = FALSE;
Npp[i] = FALSE;
adds[i] = FALSE;
dels[i] = FALSE;
nadds[i] = FALSE;
ndels[i] = FALSE;
uadds[i] = FALSE;
udels[i] = FALSE;
unadds[i] = FALSE;
undels[i] = FALSE;
}
return retval;
}
/**************************************************************
* CONFORMANT SEMANTICS (CALLED BY STATE TRANSITION FUNCTION) *
**************************************************************/
/* about the paths: if we get a bfs node then this is simply the sequence of
* nodes reachable via backchaining over ->father
* in an ehc node we either have a father, then we backchain til father
* is empty then continue with gplan_states;
* or we have no father but an op in which case the sequence is gplanstates
* plus our states
* or we have neither father nor op (==-1) in which case the node is
* the current iteration starting state and the sequence is exactly
* gplan_states.
*/
/* implementation largely naive. if critical, reconsider.
*/
void handle_inferred_literals( State *dest,
EhcNode *ehc_source, BfsNode *bfs_source,
int op,/* only for debugging .. */
int *check_pos, int num_cp,
int *check_neg, int num_cn,
Bool *Npp, int endtime )
{
static Bool fc = TRUE;
int i, j, m, ff;
State *source;
Bool sat;
if ( ehc_source ) {
source = &(ehc_source->S);
} else {
source = &(bfs_source->S);
}
if ( fc ) {
fc = FALSE;
}
if ( num_cp + num_cn == 0 ) {
/* naught to check --> get back.
*/
return;
}
/* proceed over the queries, check, and update dest info.
*
* NOTE: the to-be-checked facts all appear in unknown effects
* so they appear in the clauses and thus in partic in the gcodes
* table! make sure that it is so with a little debugging test.
*/
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n\n-------------------------checks, entry state");
print_state( *dest );
}
for ( i = 0; i < num_cp; i++ ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nchecking positive: ");
print_ft_name( check_pos[i] );
printf("(%d)", endtime);
}
ff = check_pos[i];
if ( gft_conn[ff].CNF ) {
m = 1;
} else {
m = -1;
ff = gft_conn[ff].negation;
}
/* DEBUGGING, REMOVE LATER
*/
if ( gcodes[endtime][ff] == -1 ) {
printf("\n\npos to-be-checked var not encoded?\n\n");
exit(1);
}
/* NEW: we don't actually insert unit clauses, but push the respective
* decisions onto the stack used in DP. that is, we initialise these
* values here!!
*/
gnum_decision_stack = 0;
gdecision_stack[gnum_decision_stack++] = m * (-1) * gcodes[endtime][ff];
if ( gcmd_line.T && gcmd_line.debug >= 4 ) {
print_encoded_clauses();
}
/* here goes the SAT solver!
*
* curr CNF unsolvable --> fact proved true at endtime
*/
/* do not count time for communicating clauses to Chaff.
*/
times( &end );
TIME( gsearch_time );
gsat_calls++;
sat = dp_CNF();
gsat_time += gDP_time;
times( &start );
if ( sat ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nsat! unknown "); print_ft_name( check_pos[i] );
}
dest->U[dest->num_U++] = check_pos[i];
} else {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nunsat! proved true");
}
dest->F[dest->num_F++] = check_pos[i];
}
} /* endfor i over cp */
/* here we could also check whether the negation (ie the artificial
* inverse) is proved true or false already --> then insert inversely
* without thinking... at the time, keep it full to see if it works.
*
*
* old note: (??) july06
* NOTE: ONE COULD ALSO COMPLETEY LEAVE THE ARTIFICIAL NEG VARS
* COMPLETELY OUT OF THE CLAUSES, AND USE -(ORGVAR) INSTEAD!!!
* KEEP ON TO-DO-LIST FOR LATER!! err... already done?!
*/
for ( i = 0; i < num_cn; i++ ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nchecking negative: ");
print_ft_name( check_neg[i] );
printf("(%d)", endtime);
}
if ( gft_conn[check_neg[i]].negation != -1 ) {
for ( j = 0; j < dest->num_F; j++ ) {
if ( dest->F[j] == gft_conn[check_neg[i]].negation ) break;
}
if ( j < dest->num_F ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nnegation proved true!");
}
continue;
}
for ( j = 0; j < dest->num_U; j++ ) {
if ( dest->U[j] == gft_conn[check_neg[i]].negation ) break;
}
if ( j < dest->num_U ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nnegation unknown!");
}
dest->U[dest->num_U++] = check_neg[i];
continue;
}
printf("\nnegation of neg check there, but neither F nor U yet??\n\n");
exit( 1 );
}
ff = check_neg[i];
if ( gft_conn[ff].CNF ) {
m = 1;
} else {
m = -1;
ff = gft_conn[ff].negation;
}
if ( gcodes[endtime][ff] == -1 ) {
printf("\n\nneg to-be-checked var not encoded?\n\n");
exit(1);
}
/* NEW: we don't actually insert unit clauses, but push the respective
* decisions onto the stack used in DP. that is, we initialise these
* values here!!
*/
gnum_decision_stack = 0;
gdecision_stack[gnum_decision_stack++] = m * gcodes[endtime][ff];
if ( gcmd_line.T && gcmd_line.debug >= 4 ) {
printf("\n");
print_encoded_clauses();
}
/* here goes the SAT solver!
* curr CNF unsolvable --> fact proved false at endtime
*/
times( &end );
TIME( gsearch_time );
gsat_calls++;
sat = dp_CNF();
gsat_time += gDP_time;
times( &start );
if ( sat ) {
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nsat! unknown");
}
dest->U[dest->num_U++] = check_neg[i];
} else {
/* false <-> noeither U nor F
*/
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\nunsat! proved false");
}
}
} /* endfor i over cn */
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n\n-------------------------checks, exit state");
print_state( *dest );
}
}
void extend_fixed_clauses_base( int low_state, int high_state )
{
static Bool fc = TRUE;
static State_pointer *path;
static int *path_op;
static Bool *Ft, *Ut, *Ftpp, *Utpp;
/* july06: for each set of alternative e's, only
* 1 set of pefclauses. store for each effect if or if not
* we already had those.
*/
static Bool *have_pefclauses_for;
int i, j, k, l, ef, num_path, t, time, ft, ff, m;
int ef1, ef2;
if ( fc ) {
path = ( State_pointer * ) calloc( MAX_PLAN_LENGTH + 1, sizeof( State_pointer ) );
path_op = ( int * ) calloc( MAX_PLAN_LENGTH + 1, sizeof( int ) );
Ft = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
Ut = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
Ftpp = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
Utpp = ( Bool * ) calloc( gnum_ft_conn, sizeof( Bool ) );
for ( i = 0; i < gnum_ft_conn; i++ ) {
Ft[i] = FALSE;
Ut[i] = FALSE;
Ftpp[i] = FALSE;
Utpp[i] = FALSE;
}
have_pefclauses_for = ( Bool * ) calloc( gnum_ef_conn, sizeof( Bool ) );
fc = FALSE;
}
gfixed_endtime = high_state;
if ( gcmd_line.T && gcmd_line.debug >= 2 ) {
printf("\n\n-----------------------fixed path");
}
num_path = 0;
for ( i = low_state; i <= high_state; i++ ) {
path_op[num_path] = gplan_ops[i];
path[num_path++] = &(gplan_states[i]);
if ( gcmd_line.T && gcmd_line.debug >= 3 ) {
printf("\n-----state %d", num_path - 1); print_state( *path[num_path - 1] );
}
if ( gcmd_line.T && gcmd_line.debug >= 2 && i < high_state ) {
printf("\n-----op "); print_op_name( gplan_ops[i] );
}
}
/* now the states leading to (including) source (and I) are in
* path[num_path + 1] .. path[MAX_PLAN_LENGTH-1].
* unknown efs leading to each state are stored in the state,
* (also in dest already) so we
* produce our CNF based on these,
* the unknowns in source-dest (separate treatment as here
* being not in F neither in U does not mean being provably
* false),
* as well as the initial implications.
*
* start with the initial implications.
*/
if ( gcmd_line.T && gcmd_line.debug ) {
printf("\n\n-----------------------adding clauses from %d",
gnum_fixed_clauses);
}
gnum_clauses = gnum_fixed_clauses;