-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
1772 lines (1426 loc) · 58.6 KB
/
Main.cpp
File metadata and controls
1772 lines (1426 loc) · 58.6 KB
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
/***********************************************************************************************
Copyright (c) 2013, Martin Suda
Max-Planck-Institut für Informatik, Saarbrücken, Germany
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
#include "bb.h"
#include "output.h"
#include "Common.h"
#include "Translate.h"
#include "Invariant.h"
#include <unistd.h>
#include <cassert>
#include <signal.h>
#include <cstring>
#include <climits>
#include <cerrno>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
/* refcounted wrapper for storing layer clauses */
struct ClauseBox {
Clause data;
size_t refcnt;
size_t from, to;
ClauseBox(Clause const& cl, size_t f) : data(cl), refcnt(0), from(f), to(f) {}
ClauseBox* inc() { refcnt++; return this; }
void dec() { if (!(--refcnt)) delete this;}
bool validAt(size_t idx) {
return (from >= idx && idx >= to);
}
void kickedFrom(size_t idx) {
to = idx+1;
}
void extendedTo(size_t idx) {
from = idx;
}
};
typedef vector<ClauseBox*> Clauses;
static void pruneInvalid(Clauses &layer, size_t layer_idx) {
size_t j = 0;
for (size_t i = 0; i < layer.size(); i++)
if (layer[i]->validAt(layer_idx))
layer[j++] = layer[i];
else {
// printf("Clause no longer valid in layer %zu: ",layer_idx); printClauseNice(layer[i]->data);
layer[i]->dec();
}
layer.resize(j);
}
struct Obligation {
size_t depth;
BoolState state;
Obligation* parent;
Action *action;
Obligation(Obligation* p, Action* a) : parent(p), action(a) {}
};
typedef list<Obligation*> Obligations;
// Helper class to store more than one clause in a continuous vector
// it works like a "stream" where we always record the clause's size and then its literals
struct ClauseBuffer {
vector<size_t> clauses;
size_t num_clauses;
Action* action;
ClauseBuffer() : num_clauses(0) {}
void clear() {
num_clauses = 0;
clauses.clear();
}
};
// Helper class to store more than one binary (or unary) clause in a continuous vector
struct BinClauseBuffer {
void pushClause(Clause& cl) {
assert(cl.size());
assert(cl.size() <= 2);
if (cl.size() == 1) {
data.push_back(cl[0]);
data.push_back(cl[0]);
} else {
data.push_back(cl[0]);
data.push_back(cl[1]);
}
}
void pushClause(BinClause& cl) {
data.push_back(cl.l1);
data.push_back(cl.l2);
}
void loadClause(size_t idx, Clause& cl) {
idx *= 2;
assert(idx < data.size());
cl.clear();
cl.push_back(data[idx]);
if (data[idx] != data[idx+1])
cl.push_back(data[idx+1]);
}
size_t size() {
return data.size() >> 1;
}
void swap(BinClauseBuffer& other) {
data.swap(other.data);
}
void reserve(size_t sz) {
data.reserve(sz << 1);
}
void clear() {
data.clear();
}
void swapClauses(size_t idx1, size_t idx2) {
// printf("idx1 = %zu, idx2 = %zu, size = %zu\n",idx1,idx2,data.size());
idx1 *= 2;
assert(idx1 < data.size());
idx2 *= 2;
assert(idx2 < data.size());
size_t tmp;
tmp = data[idx1];
data[idx1] = data[idx2];
data[idx2] = tmp;
tmp = data[idx1+1];
data[idx1+1] = data[idx2+1];
data[idx2+1] = tmp;
}
void killByLast(size_t idx) {
idx *= 2;
assert(idx < data.size());
swapClauses((idx >> 1),(data.size() >> 1) -1);
data.resize(data.size()-2);
}
private:
vector<size_t> data;
//stores (data.size() / 2) many binary (or unary) clauses
//unary clauses are represented by repeating the same literal twice
};
struct SolvingContext {
size_t phase;
size_t sigsize;
BoolState start_state;
// intialized only when (gcmd_line.minimize > 1)
BoolState goal_lits; // true if the lit should be true in the goal state; used for efficient inductive minimization
BinClauseBuffer invariant;
// clause sitting primarily here:
vector< Clauses > layers_delta; // size == phase+1, i.e. phase is a valid index into the last layer
// clauses coming here from weaker layers:
// REMARK: clauses in layers_deriv could have been invalidated externally - any traversal should check for that
vector< Clauses > layers_deriv; // size == phase+1, i.e. phase is a valid index into the last layer
vector< Obligations > obligations; // size == phase
Obligations obl_grave;
// statistics
size_t oblig_processed;
size_t oblig_sat;
size_t oblig_side;
size_t oblig_unsat;
size_t oblig_subsumed;
size_t oblig_killed;
size_t cla_derived;
size_t cla_second;
size_t cla_subsumed;
size_t cla_pushed;
size_t minim_attempted;
size_t minim_litkilled;
float time_extend_sat;
float time_extend_uns;
float time_pushing;
float time_postprocessing;
size_t path_min_layer; // this one is for statistics
size_t least_affected_layer; // this one is for speeding up clause propagation (otherwise more or less the same!)
SolvingContext() : phase(0), sigsize(0),
oblig_processed(0), oblig_sat(0), oblig_side(0), oblig_unsat(0), oblig_subsumed(0), oblig_killed(0),
cla_derived(0), cla_second(0), cla_subsumed(0), cla_pushed(0),
minim_attempted(0), minim_litkilled(0),
time_extend_sat(0.0), time_extend_uns(0.0), time_pushing(0.0), time_postprocessing(0.0),
path_min_layer(1),
least_affected_layer(1)
{
}
~SolvingContext() {
printGOStat();
// clauses
for (size_t i = 0; i < layers_delta.size(); i++) {
for (size_t j = 0; j < layers_delta[i].size(); j++)
layers_delta[i][j]->dec();
for (size_t j = 0; j < layers_deriv[i].size(); j++)
layers_deriv[i][j]->dec();
}
// obligations
for (size_t i = 0; i < obligations.size(); i++)
for (Obligations::iterator j = obligations[i].begin(); j != obligations[i].end(); ++j)
delete *j;
// and the grave
for (Obligations::iterator j = obl_grave.begin(); j != obl_grave.end(); ++j)
delete *j;
}
void printStat(bool between_phases = true) {
// Obligations
{
printf("\nObligations:\n");
printf("\t%zu processed,\n",oblig_processed);
printf("\t%zu extended,\n",oblig_sat);
printf("\t%zu sidestepped,\n",oblig_side);
printf("\t%zu blocked,\n",oblig_unsat);
if (gcmd_line.obl_subsumption == 2)
printf("\t%zu subsumed (%zu extra killed).\n",oblig_subsumed,oblig_killed);
else
printf("\t%zu subsumed.\n",oblig_subsumed);
if (gcmd_line.obl_survive == 2 || gcmd_line.obl_subsumption == 2)
printf("\n\t%zu obligations in the grave.\n",obl_grave.size());
// The following are reset after the timing report:
// oblig_processed = 0;
// oblig_sat = 0;
// oblig_side = 0;
// oblig_unsat = 0;
oblig_subsumed = 0;
oblig_killed = 0;
}
// Clauses
{
size_t cla_kept = 0;
size_t cla_lensum = 0;
for (size_t i = 1; i < layers_delta.size(); i++)
for (size_t j = 0; j < layers_delta[i].size(); j++) {
cla_kept++;
cla_lensum += layers_delta[i][j]->data.size();
}
printf("\nClauses:\n");
printf("\t%zu derived,\n",cla_derived);
printf("\t%zu subsumed,\n",cla_subsumed);
printf("\t%zu pushed,\n",cla_pushed);
printf("\t%zu kept (average size %f lits ).\n",cla_kept,cla_lensum*(1.0/cla_kept));
cla_derived = 0;
cla_second = 0;
cla_subsumed = 0;
cla_pushed = 0;
}
// Minimization (if applicable)
if (gcmd_line.minimize) {
printf("\nMinimization success rate: %f lits per attempt.\n",minim_litkilled*(1.0/minim_attempted));
minim_attempted = 0;
minim_litkilled = 0;
}
// Model if (between_phases)
// Layer
{
printf("\nLayers: ");
assert(layers_delta.size() == layers_deriv.size());
for (size_t i = 0; i < layers_delta.size(); i++) {
size_t layer_lensum = 0;
for (size_t j = 0; j < layers_delta[i].size(); j++)
layer_lensum += layers_delta[i][j]->data.size();
pruneInvalid(layers_deriv[i],i);
printf("%zu+%zu",layers_delta[i].size(),layers_deriv[i].size());
if (layers_delta[i].size())
printf(" s%zu",layer_lensum/layers_delta[i].size());
else
printf(" s-");
if (i < layers_delta.size()-1)
printf(" | ");
else
printf("\n");
}
}
// Timing
{
printf("\nTiming:\n");
printf("\t%fs spent extending (%f calls per second),\n",time_extend_sat+time_extend_uns,oblig_processed/(time_extend_sat+time_extend_uns));
printf("\t%fs SAT (%f calls per second),\n",time_extend_sat,(oblig_sat+oblig_side)/time_extend_sat);
printf("\t%fs UNS (%f calls per second),\n",time_extend_uns,oblig_unsat/time_extend_uns);
printf("\t%fs spent pushing.\n",time_pushing);
if (gcmd_line.postprocess && !between_phases)
printf("\t%fs spent postprocessing the plan.\n",time_postprocessing);
time_extend_sat = 0.0;
time_extend_uns = 0.0;
time_pushing = 0.0;
oblig_processed = 0;
oblig_sat = 0;
oblig_side = 0;
oblig_unsat = 0;
}
printf("\n"); fflush(stdout);
}
void printLayers() {
for (size_t i = 0; i < layers_delta.size(); i++) {
printf("Layer %zu:\n",i);
for (size_t j = 0; j < layers_delta[i].size(); j++)
printClauseNice(layers_delta[i][j]->data);
}
}
void printGOStat() {
/*
printLayers();
*/
/*
for (size_t i = 0; i < layers_delta.size(); i++) {
printf("Layer %zu:\n",i);
for (size_t j = 0; j < layers_delta[i].size(); j++)
printClauseNice(layers_delta[i][j]->data);
if (i > 0)
for (size_t j = 0; j < layers_delta[i].size(); j++) {
bool found = false;
for (size_t k = 0; k < layers_delta[i-1].size(); k++)
if (subsumes(layers_delta[i-1][k]->data,layers_delta[i][j]->data)) {
found = true;
break;
}
if (!found) {
printf("In layer %zu, following clause not subsumed in prev layer: ",i); printClauseNice(layers_delta[i][j]->data);
}
}
}
*/
if (phase > 0) {
printf("\nGame over during phase %zu\n",phase);
printStat(false);
}
{ // Global timing
times( &gend );
printf("\nPDR took: %7.2f seconds overall.\n\n",( float ) ( ( gend.tms_utime - gstart.tms_utime + gend.tms_stime - gstart.tms_stime ) / 100.0 ));
}
}
void randomPermutation(vector<size_t> & vec, size_t size) {
vec.clear();
for (size_t i = 0; i < size; i++)
vec.push_back(i);
for (size_t i = size-1; i > 0; i--) {
size_t idx = rand() % (i+1);
size_t tmp = vec[idx];
vec[idx] = vec[i];
vec[i] = tmp;
}
}
bool isLayerState(size_t layer_idx, BoolState const& state) {
for (size_t i = 0; i < layers_delta[layer_idx].size(); i++)
if (clauseUnsatisfied(layers_delta[layer_idx][i]->data,state))
return false;
// is this neccessary?
for (size_t i = 0; i < layers_deriv[layer_idx].size(); i++)
if (clauseUnsatisfied(layers_deriv[layer_idx][i]->data,state))
return false;
return true;
}
// extend temporaries:
vector<Action *> actions; // actions dumped into vector for random access
size_t used_buffer_size;
vector<ClauseBuffer> buffers; // for every interesting action a list (in a form of a buffer) of potential contributions to the final clause
vector< vector<size_t> > action_ords; //action order separately for each layer_idx
vector<size_t> buffer_ord; // indices to traverse buffers in specific order
BoolState false_precond_lits; // false preconditions of the current action
BoolState working_state; // the currently considered potential next state
vector<size_t> lit_ord; // for traversing literals of the output clause in a specific order
Clause inv_clause; // temporary storage for current invariant clause
vector<size_t> false_clauses; // indices to layers_delta[layer_idx] pointing to clauses unsat in state
// extend output:
Action* extend_action_out;
Clause extend_clause_out; // may return more than one
vector<int> reason_histogram;
static const size_t histogram_size = 10;
struct CompareBufferSizes {
vector<ClauseBuffer> & buffers;
bool operator() (size_t i,size_t j) { return (buffers[i].num_clauses < buffers[j].num_clauses); }
CompareBufferSizes(vector<ClauseBuffer> & buffs) : buffers(buffs) {}
};
struct CompareActionScores {
vector<Action *> & actions;
bool operator() (size_t i,size_t j) { return (actions[i]->score < actions[j]->score); }
CompareActionScores(vector<Action *> & acts) : actions(acts) {}
};
char extend(size_t layer_idx, BoolState const & state, bool pushTest, bool chat = false) {
// will be set to a non-null action before returning result > 0
extend_action_out = NULL;
//printf("Extend into %zu:\n",layer_idx);
//printState(state);
/*
false clauses are those which the current state doesn't satisfy
it is typically much smaller set than the whole layers_delta[layer_idx]
it pays off to focus on false clauses first
> for the positive case, we only look at the other clauses (which the action could have made false due to a delete effect)
when the action looks plausible (all precondintions are true in current and all false_clauses true in the successor)
> for the negative case (if the default quickreason is on) only plausible actions seek reasons
among the other clauses
the "side" trick (the default resched = 2) returns an action as if it was a proper successor if it satisfies some false_clauses and does not undo validity any other clause
*/
false_clauses.clear();
for (size_t i = 0; i < layers_delta[layer_idx].size(); i++)
if (clauseUnsatisfied(layers_delta[layer_idx][i]->data,state)) {
// printf("False clause %zu: ",false_clauses.size()); printClauseNice(layers_delta[layer_idx][i]->data);
false_clauses.push_back(i);
}
// printf("Extending state into %zu; number of false clauses %zu\n",layer_idx,false_clauses.size());
// printf("The state:\n"); printStateHash(state);
assert(false_clauses.size() > 0); // there is always a false clause, otherwise <state> could already sit in layer_idx-th layer
// moreover, there is never a false clause from layers_deriv nor in invariant (that has been already checked "above")
// for implementing "side"
Action *best_action = 0;
int best_false_after = (int)false_clauses.size(); // must improve to qualify
// records preconditions of the current action
// used to skip reasons from false clauses "subsumed" by a failed precond reason
// at the same time such a clause is still considerd false with respect to "side" and its counting
false_precond_lits.clear();
false_precond_lits.resize(state.size(),false);
// for recording reasons of "interesting" actions
// interesting action is an action the reason set of which is not "SUBSUMED" by the reason set of NOOP (e.g. it must make at least one false_clase true)
// we don't need to record reasons of non-interesting actions (this tends to speedup subsequent overall-reason computation and minimization)
used_buffer_size = 0;
assert(layer_idx < action_ords.size());
vector<size_t> & actions_ord = action_ords[layer_idx];
for (size_t act_idx = 0; act_idx < actions_ord.size(); act_idx++) {
size_t action_idx = actions_ord[act_idx];
Action *a = actions[action_idx];
bool plausible = true; // as far as we see it, it could be applied and would yield a good successor (satisfying all the clauses it should)
bool interesting = false; // satisfies at least one false clause -> keep the reason set for it
a->interesting = 0;
// for "side"
bool failed_precond = false; // know explicitly whether a side condition failed
int false_after = 0; // count the number of false_clauses false in the successor
bool can_do_side;
bool just_because_side;
// give me a buffer for the current action
ClauseBuffer & buffer = buffers[used_buffer_size++];
buffer.clear();
buffer.action = a;
// printf("--- Trying action %zu:",act_idx); printAction(stdout,a);
// start preparing the successor state
working_state.clear();
for (size_t i = 0; i < state.size(); i++)
working_state.push_back(state[i]);
// adding
bool useless = true;
for (int i = 0; i < numAdds(a); i++) {
int add = getAdd(a,i);
working_state[add] = true;
if (!state[add])
useless = false;
}
// useless action cannot help reaching the goal from here
if (useless) {
used_buffer_size--; // the current buffer will get overwritten in the next round
a->score = INT_MAX; // syst2 had "(int)false_clauses.size();" here instead (not to discriminate the "here useless" too much), but it wasn't that successful
continue;
}
// test preconditions
size_t failed_preconds = 0;
for (int i = 0; i < numPreconds(a); i++) {
int precond = getPrecond(a,i);
if (!state[precond]) {
//printf("Failed precond: ");
//print_ft_name(precond);
//printf("\n");
if (pushTest)
goto next_action_1;
plausible = false;
failed_precond = true;
// record the reason
buffer.num_clauses++;
buffer.clauses.push_back(1);
buffer.clauses.push_back(precond);
failed_preconds++;
false_precond_lits[precond] = true;
}
}
// if (chat) printf("%zuF ",failed_preconds);
// deleting
for (int i = 0; i < numDels(a); i++) {
int del = getDel(a,i);
working_state[del] = false;
}
{ // first check the false clauses
size_t failed_cnt = 0;
for (size_t i = 0; i < false_clauses.size(); i++) {
Clause &cl = layers_delta[layer_idx][false_clauses[i]]->data;
if (!clauseUnsatisfied(cl,working_state))
continue;
failed_cnt++;
//printf("Failed false clause %zu\n",i);
if (pushTest)
goto next_action_1;
if (!clauseUnsatisfied(cl,false_precond_lits)) { // a better reason has been recorded already
assert(!plausible);
continue;
}
// we have a false clause
plausible = false;
false_after++;
// all literals get recorded
buffer.num_clauses++;
buffer.clauses.push_back(cl.size());
for (size_t j = 0; j < cl.size(); j++)
buffer.clauses.push_back(cl[j]);
}
if (failed_cnt < false_clauses.size()) {
interesting = true;
a->interesting = 1;
//printf("Interesting action: "); printAction(stdout,a);
} else {
used_buffer_size--; // the current buffer will get overwritten in the next round (the reasons are boring; effectively subsumed by those of NOOP)
}
}
if (plausible)
a->score = INT_MAX; // if we ever get to use the score, it will mean this action breaks something below and so can never be used successfully in this context
else
a->score = (int)buffer.num_clauses;
can_do_side = (gcmd_line.resched == 2) && !failed_precond && (false_after < best_false_after);
just_because_side = false;
if ( plausible || // normally, only if the action still seems ok, we perform the full test
(gcmd_line.quick_reason == 0) || // unless we don't want the quickreason trick (seems to harm on UNSAT problems)
(interesting && gcmd_line.quick_reason == 2) || // something in the middle (experimental)
(just_because_side = true, can_do_side) ) { // or if we still need to check whether "side" is an option ...
pruneInvalid(layers_deriv[layer_idx],layer_idx);
size_t layers_delta_size = layers_delta[layer_idx].size();
size_t layers_deriv_size = layers_deriv[layer_idx].size();
size_t invariant_size = invariant.size();
size_t false_clause_idx = 0;
for (size_t i = 0; i < layers_delta_size + layers_deriv_size + invariant_size; i++) {
// bool in_delta = false;
// bool in_deriv = false;
// bool in_inv = false;
Clause* p_cl;
if (i < layers_delta_size) {
// TODO: testing whether the test shouldn't be rather skipped
if (false_clause_idx < false_clauses.size() && i == false_clauses[false_clause_idx]) {
false_clause_idx++;
continue; // we had this one already
}
p_cl = &layers_delta[layer_idx][i]->data;
// in_delta = true;
} else if (i - layers_delta_size < layers_deriv_size) {
p_cl = &layers_deriv[layer_idx][i-layers_delta_size]->data;
// in_deriv = true;
} else {
invariant.loadClause(i - layers_delta_size - layers_deriv_size,inv_clause);
p_cl = &inv_clause;
// in_inv = true;
}
Clause &cl = *p_cl;
if (!clauseUnsatisfied(cl,working_state))
continue; // next clause
can_do_side = false;
if (just_because_side)
break;
//printf("Failed clause: "); printClauseNice(cl);
if (pushTest)
goto next_action_1;
if (!clauseUnsatisfied(cl,false_precond_lits)) { // a better reason has been recorded already
assert(!plausible);
continue;
}
// we have a false clause
plausible = false;
// all preserved negative literals get recorded
{
buffer.num_clauses++;
size_t new_cl_size = 0;
size_t new_cl_idx = buffer.clauses.size();
buffer.clauses.push_back(0); // will update when we know the real size
for (size_t j = 0; j < cl.size(); j++)
if (!state[cl[j]]) { // was already false, so was preserved; the others were deleted explicitly so cannot be part of the reason clause
new_cl_size++;
buffer.clauses.push_back(cl[j]);
//print_ft_name(cl[j]); printf(" ");
}
buffer.clauses[new_cl_idx] = new_cl_size;
}
//printf("\n");
}
//if (chat) printf(" R: %zu\n",buffer.num_clauses);
}
// all clauses sat in new state
if (plausible) {
// printf("SAT\n");
if (pushTest)
return 1;
// there is a room for heuristics when picking just one next state
// printf("Succesfully going forward!\n");
extend_action_out = a;
// syst3: bring the successful action to front
for (size_t i = act_idx; i > 0; i--)
actions_ord[i] = actions_ord[i-1];
actions_ord[0] = action_idx;
// printAction(stdout,extend_action_out);
return 1;
}
if (can_do_side &&
isLayerState(layer_idx+1,working_state)) { /* since layers_deriv are sumbsumption reduced,
there is still a risk the successors does not satisfy all the layer clauses of its parent */
// printf("Improved best to %d with state:\n",false_after);
// printStateHash(working_state);
best_false_after = false_after;
best_action = a;
}
next_action_1:
// cleanup for the action
for (int i = 0; i < numPreconds(a); i++)
false_precond_lits[getPrecond(a,i)] = false;
}
// all actions checked here !!!
if (pushTest)
return 0;
// finishing the "side" trick
if (gcmd_line.resched == 2 && best_action != 0) {
extend_action_out = best_action;
// printf("SIDE (%d)\n",best_false_after);
// printAction(stdout,extend_action_out);
return 2;
}
//printf("UNSAT\n");
// the contribution from "no-op" = clauses from current layer false in the given state
{
ClauseBuffer & buffer = buffers[used_buffer_size++];
buffer.clear();
buffer.action = NULL;
for (size_t i = 0; i < false_clauses.size(); i++) {
Clause &cl = layers_delta[layer_idx][false_clauses[i]]->data;
buffer.num_clauses++;
buffer.clauses.push_back(cl.size());
for (size_t j = 0; j < cl.size(); j++)
buffer.clauses.push_back(cl[j]);
}
}
// update the order for next time
stable_sort(actions_ord.begin(),actions_ord.end(),CompareActionScores(actions)); // low score is better
/*
printf("Uptaded actions_ord for idx %zu:\n",layer_idx);
for (size_t act_idx = 0; act_idx < actions_ord.size(); act_idx++) {
size_t action_idx = actions_ord[act_idx];
Action *a = actions[action_idx];
printf("Score: %d for ",a->score);
printAction(stdout,a);
}
*/
// just for fun - histogram of the number of reasons per action
/*
{
reason_histogram.clear();
reason_histogram.resize(histogram_size,0);
for (size_t i = 0; i < used_buffer_size; i++) {
ClauseBuffer & buffer = buffers[i];
assert(buffer.num_clauses > 0);
if (buffer.num_clauses <= histogram_size) {
reason_histogram[buffer.num_clauses-1]++;
}
}
for (size_t i = 0; i < histogram_size; i++)
printf("%3d, ",reason_histogram[i]);
printf(" histogram for idx %zu\n",layer_idx);
}
*/
// prepare the conflict clause -- abusing the workingstate for that (to represent the union being built)
working_state.clear();
working_state.resize(sigsize,false);
// resize buffer ord and sort it based on buffer sizes
randomPermutation(buffer_ord,used_buffer_size); // TODO: could skip the random and use identity permutation instead (no big deal, would it speed up?)
sort(buffer_ord.begin(),buffer_ord.end(),CompareBufferSizes(buffers)); //start with small buffers
for (size_t act_idx = 0; act_idx < buffer_ord.size(); act_idx++) {
ClauseBuffer & buffer = buffers[buffer_ord[act_idx]];
assert(buffer.num_clauses > 0);
/*
printf("%zu contributions of action %zu: ",buffer.num_clauses,buffer_ord[act_idx]);
if (buffer.action)
printAction(stdout,buffer.action);
else
printf("(NOOP)\n");
*/
size_t best_adds = sigsize+1;
size_t best_idx = 0;
size_t i = 0;
size_t sz = 0;
while (i < buffer.clauses.size()) {
sz = buffer.clauses[i];
size_t curbase = i;
size_t curadds = 0;
while (i++,sz--) {
/*
print_ft_name(buffer.clauses[i]);
printf(" ");
*/
if (!working_state[buffer.clauses[i]])
curadds++;
}
/*
printf("\n");
*/
if (curadds < best_adds) {
best_adds = curadds;
best_idx = curbase;
}
if (best_adds == 0) // no further improvement possible
break;
}
assert(best_adds <= sigsize);
// if (chat) printf("- best adds %zu\n",best_adds);
// apply the best guy
{
i = best_idx;
sz = buffer.clauses[i];
while (i++,sz--)
working_state[buffer.clauses[i]] = true;
}
}
/*
if (chat) {
size_t sz = 0;
for (size_t i = 0; i < working_state.size(); i++)
if (working_state[i])
sz++;
printf("Final size %zu\n",sz);
}
*/
/*
printf("Derived clause ");
printState(working_state);
*/
if (gcmd_line.minimize) {
minim_attempted++;
// printf("Minimizing:\n");
randomPermutation(lit_ord,sigsize); // TODO: could have better minimization heuristics (like avoiding the first action's reason first)
int goal_lits_remaining = 0;
if (gcmd_line.minimize > 1) {
for (size_t i = 0; i < sigsize; i++)
if (goal_lits[i] && working_state[i])
goal_lits_remaining++;
}
bool removed_something;
do {
removed_something = false;
for (size_t lit_idx = 0; lit_idx < sigsize; lit_idx++) {
if (working_state[lit_ord[lit_idx]]) { // could remove this guy
size_t saved = lit_ord[lit_idx];
working_state[saved] = false;
if (goal_lits[saved])
goal_lits_remaining--;
// check if we still "fit in" with the contributions
for (size_t act_idx = 0; act_idx < buffer_ord.size(); act_idx++) {
ClauseBuffer & buffer = buffers[buffer_ord[act_idx]];
size_t i = 0;
size_t sz = 0;
if (goal_lits_remaining) { // can apply the inductive argument
assert(gcmd_line.minimize > 1);
// can try the inductive reason first
if (buffer.action) {
for (int i = 0; i < numAdds(buffer.action); i++) {
int add = getAdd(buffer.action,i);
if (working_state[add])
goto all_the_standard_reasons_to_try;
}
} else { // the NOOP action itself is never a problem, but it represents all the non-interesting actions which need to be tried, and why not try them now?
for (Action* a = gactions; a; a = a->next)
if (!a->interesting)
for (int i = 0; i < numAdds(a); i++) {
int add = getAdd(a,i);
if (working_state[add])
goto all_the_standard_reasons_to_try;
}
}
// either NOOP, which always succeds inductively, or the action cannot make our current clause true anyway
goto next_action_2;
}
all_the_standard_reasons_to_try: ;
while (i < buffer.clauses.size()) {
sz = buffer.clauses[i++];
while (sz) {
if (!working_state[buffer.clauses[i]])
break;
i++, sz--;
}
if (sz)
i += sz;
else
goto next_action_2; // the current is OK
}
/*
printf("Kept "); print_ft_name(saved); printf(" also because of ");
if (buffer.action)
printAction(stdout,buffer.action);
else
printf("(NOOP)\n");
*/
// none of them is good enough - put the literal back
working_state[saved] = true;
if (goal_lits[saved])
goal_lits_remaining++;
goto next_literal;
next_action_2: ;
}
// good riddance :)
removed_something = true;
minim_litkilled++;
}
next_literal: ;
}
} while (gcmd_line.minimize > 2 && removed_something);
/*
printf("Minimized to ");
printState(working_state);