-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasgl.lisp
1571 lines (1419 loc) · 59 KB
/
asgl.lisp
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
;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
;;; ASGL an abstract argumentation solver in ECL and GECODE.
;;; Copyright (C) 2015 Kilian Sprotte
;;; 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 3 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, see <http://www.gnu.org/licenses/>.
(defpackage :asgl
(:use :cl :early :alexandria :gecode :af-constraints))
(in-package :asgl)
#+nil(declaim (optimize (debug 3) (safety 3) (speed 0)))
(declaim (optimize (debug 0) (safety 1) (speed 3) (space 0)))
(eval-when (:compile-toplevel :execute)
(cover:annotate t))
(defvar *use-gist* nil)
(defvar *intval* nil)
(defvar *intvar* nil)
(defvar *seed* 2015)
(defvar *decay* 1.0)
(defvar *constr* 0)
(defvar *use-sat* nil)
(defvar *sat-force-freeze* t)
(defun make-dfs-engine-or-gist (space)
(if (not *use-gist*)
(make-dfs-engine space)
(make-dfs-engine
;; go interactive. When user is done, we just continue with
;; normal search. Space is returned unchanged.
(dfs-search-gist space))))
(defun make-bab-engine-or-gist (space)
(if (not *use-gist*)
(make-bab-engine space)
(make-bab-engine
;; go interactive. When user is done, we just continue with
;; normal search. Space is returned unchanged.
(bab-search-gist space))))
(defun adopt-keywords (list)
;; (check-type list list)
(mapcar (lambda (x)
(if (char= #\- (char x 0))
(intern (string-upcase (subseq x 1)) "KEYWORD")
x))
list))
(defun parse-problem (string)
;; (check-type string string)
(let ((pos (position #\- string)))
(values
(intern (string-upcase (subseq string 0 pos)) "KEYWORD")
(intern (string-upcase (subseq string (1+ pos))) "KEYWORD"))))
(defun parse-problem* (string)
(multiple-value-bind (task semantic)
(parse-problem string)
(values (make-task task)
(make-semantic semantic))))
(defun task-semantic-format-as-cli-argument (task semantic)
(format nil "~A-~A"
(etypecase task (ee-task "EE") (se-task "SE")
(ds-task "DS") (dc-task "DC"))
(etypecase semantic (complete "CO") (preferred "PR")
(stable "ST") (grounded "GR"))))
(deftype input () '(or string pathname vector cons))
(defgeneric make-initial-space (graph task semantic))
(defgeneric constrain-space (space semantic task graph))
(defgeneric constrain-arg-if-needed (space semantic task))
(defgeneric constrain-arg (space semantic task))
(defgeneric branch-space (space task semantic))
(defgeneric make-search-engine (space task semantic vector))
(defgeneric make-driver (semantic task))
(defgeneric drive-search-and-print (task engine))
(defgeneric drive-search-and-collect (task engine))
(defgeneric translate-problem (task semantic))
(defclass task () ())
(defclass semantic () ())
(defclass complete (semantic) ())
(defclass grounded (complete) ())
(defclass preferred (complete) ())
(defclass stable (preferred) ())
(defclass extension-task (task) ())
(defclass ee-task (extension-task) ())
(defclass se-task (extension-task) ())
(defmethod (setf task-hash) (value (task task))
;; (check-type value hash-table)
;; nothing to do here
)
(defclass decision-task (task)
((hash :accessor task-hash :initform nil)
(arg-name :reader task-arg-name :initarg :arg-name)
(inferred-on-no-solution :reader inferred-on-no-solution)))
(defclass dc-task (decision-task)
((inferred-on-no-solution :initform nil)))
(defclass ds-task (decision-task)
((inferred-on-no-solution :initform t)))
(defmethod task-arg ((task decision-task))
(or (gethash (task-arg-name task) (task-hash task))
(error "task-arg-name ~S not found in task-hash ~S containing~%~S"
(task-arg-name task) (task-hash task)
(hash-table-alist (task-hash task)))))
(defun prepare-space (input task semantic)
;; (check-type input input)
;; (check-type task task)
;; (check-type semantic semantic)
(multiple-value-bind (graph vector hash)
(with-timing (read-graph-input input))
(setf (task-hash task) hash)
(log* 1 "input AF consisting of ~A arguments and ~A attacks"
(order graph) (size graph))
(log* 2 "indegrees: ~A" (summary (indegrees graph)))
(log* 2 "outdegrees: ~A" (summary (outdegrees graph)))
(let ((space (make-initial-space graph task semantic)))
(with-post-env-setup (space)
(constrain-space space semantic task graph)
(constrain-arg-if-needed space semantic task))
(branch-space space task semantic)
(values space vector))))
;;; problem
(defgeneric name (object)
(:documentation "Retrieve name of object."))
(defmethod name ((object asgl::task)) (type-of object))
(defmethod name ((object asgl::semantic)) (type-of object))
(defclass problem ()
((graph :reader problem-graph :initarg :graph)
(query-argument :reader problem-query-argument :initarg :query-argument)
(query-argument-name :reader problem-query-argument-name :initarg :query-argument-name)
(argument-names :reader problem-argument-names :initarg :argument-names)
(task :reader problem-task :initarg :task)
(semantic :reader problem-semantic :initarg :semantic)))
(defmethod print-object ((problem problem) stream)
(print-unreadable-object (problem stream :type t)
(format stream "~A~@[ (arg ~A)~] of AF with ~r argument~:P, ~r attack~:P"
(asgl::task-semantic-format-as-cli-argument
(problem-task problem)
(problem-semantic problem))
(problem-query-argument-name problem)
(asgl::order (problem-graph problem))
(asgl::size (problem-graph problem)))))
(defclass problem-with-result-printed (problem)
())
(defun make-problem (&key graph-input task-semantic query-argument-name
(class 'problem-with-result-printed))
"problem constructor."
(typecase graph-input
((or string pathname)
(unless (probe-file graph-input)
(error "graph-input ~S does not exist" graph-input))))
(multiple-value-bind (graph vector hash)
(asgl::read-graph-input graph-input)
(when query-argument-name
(unless (gethash query-argument-name hash)
(error "argument ~S is not part of AF" query-argument-name)))
(multiple-value-bind (task semantic)
(asgl::parse-problem* (string task-semantic))
(if (typep task 'asgl::decision-task)
(or query-argument-name
(error "no argument given for ~S" task))
(or (not query-argument-name)
(error "argument given for ~S. expected none." task)))
(make-instance class
:graph graph
:query-argument (gethash query-argument-name hash)
:query-argument-name query-argument-name
:argument-names vector
:task task
:semantic semantic))))
(defclass driver () ())
(defclass search-all-driver (driver)
())
(defclass search-one-driver (driver)
())
(defclass search-one-decision-driver (driver)
((inferred-on-no-solution
:reader inferred-on-no-solution
:initarg :inferred-on-no-solution)))
(defmethod print-object ((driver search-one-decision-driver) stream)
(print-unreadable-object (driver stream :identity nil :type t)
(format stream "inferred-on-no-solution ~A"
(inferred-on-no-solution driver))))
(defmethod make-driver (semantic (task ee-task))
;; (check-type semantic semantic)
(make-instance 'search-all-driver))
(defmethod make-driver (semantic (task se-task))
;; (check-type semantic semantic)
(make-instance 'search-one-driver))
(defmethod make-driver (semantic (task dc-task))
;; (check-type semantic semantic)
(make-instance 'search-one-decision-driver
:inferred-on-no-solution nil))
(defmethod make-driver (semantic (task ds-task))
;; (check-type semantic semantic)
(make-instance 'search-one-decision-driver
:inferred-on-no-solution t))
(defmethod make-driver ((semantic grounded) (task decision-task))
(make-instance 'search-one-decision-driver
:inferred-on-no-solution t))
(defun solve (input task semantic drive-fn)
;; (check-type input input)
;; (check-type task task)
;; (check-type semantic semantic)
;; (check-type drive-fn function)
#+fobj-leak-checks(gecode::pool-start)
(multiple-value-bind (space vector)
(with-timing (prepare-space input task semantic))
(let ((engine (with-timing (make-search-engine space task semantic vector)))
(driver (with-timing (make-driver semantic task))))
(log* 1 "driver: ~A" driver)
(log* 1 "engine: ~A" engine)
(log* 1 "STARTING SEARCH")
(multiple-value-prog1
(with-timing (funcall drive-fn driver engine))
#+fobj-leak-checks
(gecode::pool-check (format nil "~S ~S ~S ~S"
input task semantic drive-fn))))))
;;; ================================================================================
;;; sketches
;;; ================================================================================
(defun sat-print-in (solver argument-names &optional vector)
(let ((vector (or vector (make-array (length argument-names))))
(stream *standard-output*))
(sat:save-solution solver vector)
(write-string "[" stream)
(loop with first-time = t
for i below (length argument-names)
for in = (aref vector i)
do (when in
(if first-time
(setq first-time nil)
(write-string "," stream))
(princ (aref argument-names i) stream)))
(write-string "]" stream)
nil))
(defun sat-collect-in (solver argument-names &optional vector)
(let ((vector (or vector (make-array (length argument-names))))
(stream *standard-output*))
(sat:save-solution solver vector)
(loop for i below (length argument-names)
for in = (aref vector i)
when in
collect (aref argument-names i))))
(defun sat-block-solution (solver vector)
(sat:with-added-clause (solver)
(dotimes (i (length vector))
(if (aref vector i)
(sat:add-literals solver :negative i)
(sat:add-literals solver :positive i)))))
(defmacro with-array-output-helper ((stream) &body body)
(once-only
(stream)
(with-gensyms (first-time)
`(macrolet ((maybe-print-comma ()
',`(progn
(if ,first-time
(write-string " " ,stream)
(write-string ", " ,stream))
(when ,first-time
(setq ,first-time nil)))))
(let ((,first-time t))
(write-line "[" ,stream)
,@body
(write-line "]" ,stream))))))
(defmacro with-space ((space-var form) &body body)
`(let ((,space-var ,form))
(unwind-protect
(progn ,@body)
(when ,space-var
(delete-space ,space-var)))))
(defmacro with-leak-checks ((msg) &body body)
#-fobj-leak-checks(declare (ignore msg))
`(progn
#+fobj-leak-checks(gecode::pool-start)
(multiple-value-prog1
(progn ,@body)
#+fobj-leak-checks
(gecode::pool-check ,msg))))
(defmacro defun-leak-checks (name args &body body)
`(defun ,name ,args
(with-leak-checks (,(princ-to-string name))
,@body)))
(defun branch* (space intvar intval)
(labels ((random-needed-p ()
(or (eql intvar :rnd)
(eql intval :rnd)
(eql intvar :degree-max-rnd)))
(b (var random-gen)
(let*-heap ((val (ecase intval
(:min (int-val-min))
(:max (int-val-max))
(:rnd (int-val-rnd random-gen)))))
(branch space var val))))
(let*-heap ((random-gen (case (random-needed-p)
((t) (rnd *seed*)))))
(case intvar
(:degree-max-activity-max
(let*-heap ((degree-max (int-var-degree-max))
(activity-max (int-var-activity-max *decay*))
(var (tiebreak degree-max activity-max)))
(b var random-gen)))
(:degree-max-rnd
(let*-heap ((degree-max (int-var-degree-max))
(rrr (int-var-rnd random-gen))
(var (tiebreak degree-max rrr)))
(b var random-gen)))
(t (let*-heap ((var (ecase intvar
(:degree-max (int-var-degree-max))
(:activity-max (int-var-activity-max *decay*))
(:afc-max (int-var-afc-max *decay*))
(:rnd (int-var-rnd random-gen))
(:none (int-var-none)))))
(b var random-gen)))))))
(defun-leak-checks solve-se-gr (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(with-space (space (make-bool-space (order graph)))
(do-parents (x parents graph)
(if (member x parents)
(gecode:post-ored-vars-imp-neg-var space (remove x parents) x)
(gecode:post-ored-vars-eql-neg-var space parents x)))
(assert (eql :solved (space-status space)))
(if print
(progn
(space-print-in space argument-names)
(terpri))
(values (space-collect-in space argument-names)
t)))))
(defun-leak-checks solve-dc-gr (graph-input argument-name &key print)
(multiple-value-bind (graph argument-names hash-table)
(read-graph-input graph-input)
(declare (ignore argument-names))
(with-space (space (make-bool-space (order graph)))
(post-must-be-false space (gethash argument-name hash-table))
(with-post-env-setup (space)
(constrain-complete graph))
(ecase (space-status space)
(:solved
(if print
(write-line "NO")
nil))
(:failed
(if print
(write-line "YES")
t))))))
(defun-leak-checks solve-ee-gr (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(with-space (space (make-bool-space (order graph)))
(with-post-env-setup (space)
(constrain-complete graph))
(assert (eql :solved (space-status space)))
(if print
(progn
(write-char #\[)
(space-print-in space argument-names)
(write-line "]"))
(list (space-collect-in space argument-names))))))
(defun-leak-checks solve-ee-co (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(let ((space (make-bool-space (order graph))))
(case *constr*
(3 (with-post-env-setup (space)
(constrain-complete graph)))
(t (let* ((n (order graph))
(vars-in (gecode:space-vars-as-vector space))
(vars-out (gecode:make-boolvar-array space n)))
(case *constr*
(0 (do-parents (i parents graph)
(dolist (j parents)
(gecode:assert-nand space (aref vars-in i) (aref vars-in j)))
(gecode:assert-nand space (aref vars-in i) (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))
(1 (do-parents (i parents graph)
(dolist (j parents)
(gecode:assert-nand space (aref vars-in i) (aref vars-in j)))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))
(2 (do-parents (i parents graph)
(gecode:assert-nand space (aref vars-in i) (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))))))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(multiple-value-prog1
(if print
(with-array-output-helper (*standard-output*)
(loop
(with-space (space (dfs-next engine))
(if space
(progn
(maybe-print-comma)
(space-print-in space argument-names)
(terpri))
(return)))))
(let (result)
(loop
(with-space (space (dfs-next engine))
(if space
(push (space-collect-in space argument-names) result)
(return))))
(nreverse result)))
(delete-dfs engine))))))
(defun-leak-checks solve-ee-st (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(let* ((space (make-bool-space (order graph)))
(vars-in (gecode:space-vars-as-vector space)))
(do-parents (x parents graph)
;; es ginge auch nur
;; (gecode:post-ored-vars-eql-neg-var space parents x)
(if (member x parents)
(progn
(gecode:assert-false space (aref vars-in x))
(gecode:vector-indices-bot-eql-const
space vars-in (remove x parents) :bot-or t))
(gecode:post-ored-vars-eql-neg-var space parents x)))
(branch* space (or *intvar* :degree-max) (or *intval* :max))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(multiple-value-prog1
(if print
(with-array-output-helper (*standard-output*)
(loop
(with-space (space (dfs-next engine))
(if space
(progn
(maybe-print-comma)
(space-print-in space argument-names)
(terpri))
(return)))))
(let (result)
(loop
(with-space (space (dfs-next engine))
(if space
(push (space-collect-in space argument-names) result)
(return))))
(nreverse result)))
(delete-dfs engine))))))
(defun solve-ee-st-sat (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(sat:with-solver (solver)
(do-parents (x parents graph)
(sat:with-added-clause (solver)
(sat:add-literals solver :positive x)
(dolist (parent parents)
(sat:add-literals solver :positive parent)))
(dolist (parent parents)
(sat:with-added-clause (solver)
(sat:add-literals solver :negative x parent))))
(let ((solution-vector (make-array (length argument-names))))
(dotimes (i (length argument-names))
(sat:freeze-literal solver i))
(if print
(with-array-output-helper (*standard-output*)
(loop
(if (sat:satisfiablep solver)
(progn
(maybe-print-comma)
(sat-print-in solver argument-names solution-vector)
(sat-block-solution solver solution-vector)
(terpri))
(return))))
(let (result)
(loop
(if (sat:satisfiablep solver)
(push (prog1
(sat-collect-in solver argument-names solution-vector)
(sat-block-solution solver solution-vector))
result)
(return)))
(nreverse result)))))))
(defun-leak-checks solve-se-co (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(let ((space (make-bool-space (order graph))))
(with-post-env-setup (space)
(constrain-complete graph))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if print
(progn
(space-print-in space argument-names)
(terpri))
(values (space-collect-in space argument-names)
t))
(delete-dfs engine)))))))
(defun-leak-checks solve-se-st (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(let* ((space (make-bool-space (order graph)))
(vars-in (gecode:space-vars-as-vector space)))
(do-parents (x parents graph)
;; es ginge auch nur
;; (gecode:post-ored-vars-eql-neg-var space parents x)
(if (member x parents)
(progn
(gecode:assert-false space (aref vars-in x))
(gecode:vector-indices-bot-eql-const
space vars-in (remove x parents) :bot-or t))
(gecode:post-ored-vars-eql-neg-var space parents x)))
(branch* space (or *intvar* :degree-max) (or *intval* :max))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if space
(if print
(progn
(space-print-in space argument-names)
(terpri))
(values (space-collect-in space argument-names)
t))
(if print
(write-line "NO")
(values nil nil)))
(delete-dfs engine)))))))
(defun solve-se-st-sat (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(sat:with-solver (solver)
(do-parents (x parents graph)
(sat:with-added-clause (solver)
(sat:add-literals solver :positive x)
(dolist (parent parents)
(sat:add-literals solver :positive parent)))
(dolist (parent parents)
(sat:with-added-clause (solver)
(sat:add-literals solver :negative x parent))))
(when *sat-force-freeze*
(dotimes (i (length argument-names))
(sat:freeze-literal solver i)))
(if (sat:satisfiablep solver)
(if print
(progn
(sat-print-in solver argument-names)
(terpri))
(values (sat-collect-in solver argument-names)
t))
(if print
(write-line "NO")
(values nil nil))))))
(defun-leak-checks solve-se-pr (graph-input &key print)
(multiple-value-bind (graph argument-names)
(read-graph-input graph-input)
(let ((space (make-pr-bab-space (order graph))))
(case *constr*
(3 (with-post-env-setup (space)
(constrain-complete graph)))
(t (let* ((n (order graph))
(vars-in (gecode:space-vars-as-vector space))
(vars-out (gecode:make-boolvar-array space n)))
(case *constr*
(0 (do-parents (i parents graph)
(dolist (j parents)
(gecode:assert-nand space (aref vars-in i) (aref vars-in j)))
(gecode:assert-nand space (aref vars-in i) (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))
(1 (do-parents (i parents graph)
(dolist (j parents)
(gecode:assert-nand space (aref vars-in i) (aref vars-in j)))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))
(2 (do-parents (i parents graph)
(gecode:assert-nand space (aref vars-in i) (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-in parents :bot-or (aref vars-out i))
(gecode:vector-indices-bot-eql-var
space vars-out parents :bot-and (aref vars-in i))))))))
(branch* space (or *intvar* :degree-max) (or *intval* :max))
(let ((engine (gecode:make-bab-engine space)))
(delete-space space)
(with-space (space (bab-best engine))
(multiple-value-prog1
(if print
(progn
(space-print-in space argument-names)
(terpri))
(values (space-collect-in space argument-names)
t))
(delete-bab engine)))))))
(defun-leak-checks solve-dc-st (graph-input argument-name &key print)
(multiple-value-bind (graph argument-names hash-table)
(read-graph-input graph-input)
(declare (ignore argument-names))
(let ((space (make-bool-space (order graph))))
(post-must-be-true space (gethash argument-name hash-table))
(with-post-env-setup (space)
(constrain-complete graph)
(constrain-stable graph))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if space
(if print
(write-line "YES")
t)
(if print
(write-line "NO")
nil))
(delete-dfs engine)))))))
(defun-leak-checks solve-ds-st (graph-input argument-name &key print)
(multiple-value-bind (graph argument-names hash-table)
(read-graph-input graph-input)
(declare (ignore argument-names))
(let ((space (make-bool-space (order graph))))
(post-must-be-false space (gethash argument-name hash-table))
(with-post-env-setup (space)
(constrain-complete graph)
(constrain-stable graph))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if (not space)
(if print
(write-line "YES")
t)
(if print
(write-line "NO")
nil))
(delete-dfs engine)))))))
(defun-leak-checks solve-dc-co (graph-input argument-name &key print)
(multiple-value-bind (graph argument-names hash-table)
(read-graph-input graph-input)
(declare (ignore argument-names))
(let ((space (make-bool-space (order graph))))
(post-must-be-true space (gethash argument-name hash-table))
(with-post-env-setup (space)
(constrain-complete graph))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if space
(if print
(write-line "YES")
t)
(if print
(write-line "NO")
nil))
(delete-dfs engine)))))))
(defun-leak-checks solve-ds-co (graph-input argument-name &key print)
(multiple-value-bind (graph argument-names hash-table)
(read-graph-input graph-input)
(declare (ignore argument-names))
(let ((space (make-bool-space (order graph))))
(post-must-be-false space (gethash argument-name hash-table))
(with-post-env-setup (space)
(constrain-complete graph))
(branch* space (or *intvar* :degree-max) (or *intval* :min))
(let ((engine (gecode:make-dfs-engine space)))
(delete-space space)
(with-space (space (dfs-next engine))
(multiple-value-prog1
(if (not space)
(if print
(write-line "YES")
t)
(if print
(write-line "NO")
nil))
(delete-dfs engine)))))))
(defun solve-ee-pr (graph-input &key print)
(let* ((ee-co (solve-ee-co graph-input :print nil))
(ee-pr (remove-duplicates (sort ee-co #'< :key #'length)
:test #'subsetp)))
(if print
(with-array-output-helper (*standard-output*)
(dolist (ext ee-pr)
(maybe-print-comma)
(format t "[~{~A~^,~}]~%" ext)))
ee-pr)))
(defun solve-ds-pr (graph-input argument-name &key print)
(let* ((ee-co (solve-ee-co graph-input :print nil))
(ee-pr (remove-duplicates (sort ee-co #'< :key #'length)
:test #'subsetp)))
(if (every (lambda (ext) (member argument-name ext :test #'equal))
ee-pr)
(if print
(write-line "YES")
t)
(if print
(write-line "NO")
nil))))
(defun solve-ds-gr (graph-input argument-name &key print)
(solve-dc-gr graph-input argument-name :print print))
(defun solve-dc-pr (graph-input argument-name &key print)
(solve-dc-co graph-input argument-name :print print))
;;; ////////////////////////////////////////////////////////////////////////////////
;;; END sketches
;;; ////////////////////////////////////////////////////////////////////////////////
(defun typep* (obj type)
(eql type (type-of obj)))
(defun print-answer (input arg-name task semantic)
(cond
((and (typep* task 'se-task)
(typep* semantic 'grounded))
(solve-se-gr input :print t))
((and (typep* task 'ee-task)
(typep* semantic 'grounded))
(solve-ee-gr input :print t))
((and (typep* task 'se-task)
(typep* semantic 'stable))
(if *use-sat*
(solve-se-st-sat input :print t)
(solve-se-st input :print t)))
((and (typep* task 'se-task)
(typep* semantic 'complete))
(solve-se-co input :print t))
((and (typep* task 'ee-task)
(typep* semantic 'complete))
(solve-ee-co input :print t))
((and (typep* task 'ee-task)
(typep* semantic 'stable))
(if *use-sat*
(solve-ee-st-sat input :print t)
(solve-ee-st input :print t)))
((and (typep* task 'se-task)
(typep* semantic 'preferred))
(solve-se-pr input :print t))
((and (typep* task 'dc-task)
(typep* semantic 'stable))
(solve-dc-st input arg-name :print t))
((and (typep* task 'ds-task)
(typep* semantic 'stable))
(solve-ds-st input arg-name :print t))
((and (typep* task 'dc-task)
(typep* semantic 'complete))
(solve-dc-co input arg-name :print t))
((and (typep* task 'ds-task)
(typep* semantic 'complete))
(solve-ds-co input arg-name :print t))
((and (typep* task 'dc-task)
(typep* semantic 'grounded))
(solve-dc-gr input arg-name :print t))
((and (typep* task 'ds-task)
(typep* semantic 'grounded))
(solve-ds-gr input arg-name :print t))
((and (typep* task 'dc-task)
(typep* semantic 'preferred))
(solve-dc-pr input arg-name :print t))
((and (typep* task 'ee-task)
(typep* semantic 'preferred))
(solve-ee-pr input :print t))
((and (typep* task 'ds-task)
(typep* semantic 'preferred))
(solve-ds-pr input arg-name :print t))
(t (solve input task semantic
#'drive-search-and-print))))
(defun collect-answer (input arg-name task semantic)
(cond
((and (typep* task 'se-task)
(typep* semantic 'grounded))
(solve-se-gr input :print nil))
((and (typep* task 'ee-task)
(typep* semantic 'grounded))
(solve-ee-gr input :print nil))
((and (typep* task 'se-task)
(typep* semantic 'stable))
(if *use-sat*
(solve-se-st-sat input :print nil)
(solve-se-st input :print nil)))
((and (typep* task 'se-task)
(typep* semantic 'complete))
(solve-se-co input :print nil))
((and (typep* task 'ee-task)
(typep* semantic 'complete))
(solve-ee-co input :print nil))
((and (typep* task 'ee-task)
(typep* semantic 'stable))
(if *use-sat*
(solve-ee-st-sat input :print nil)
(solve-ee-st input :print nil)))
((and (typep* task 'se-task)
(typep* semantic 'preferred))
(solve-se-pr input :print nil))
((and (typep* task 'dc-task)
(typep* semantic 'stable))
(solve-dc-st input arg-name :print nil))
((and (typep* task 'ds-task)
(typep* semantic 'stable))
(solve-ds-st input arg-name :print nil))
((and (typep* task 'dc-task)
(typep* semantic 'complete))
(solve-dc-co input arg-name :print nil))
((and (typep* task 'ds-task)
(typep* semantic 'complete))
(solve-ds-co input arg-name :print nil))
((and (typep* task 'dc-task)
(typep* semantic 'grounded))
(solve-dc-gr input arg-name :print nil))
((and (typep* task 'ds-task)
(typep* semantic 'grounded))
(solve-ds-gr input arg-name :print nil))
((and (typep* task 'dc-task)
(typep* semantic 'preferred))
(solve-dc-pr input arg-name :print nil))
((and (typep* task 'ee-task)
(typep* semantic 'preferred))
(solve-ee-pr input :print nil))
((and (typep* task 'ds-task)
(typep* semantic 'preferred))
(solve-ds-pr input arg-name :print nil))
(t (solve input task semantic
#'drive-search-and-collect))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun make-semantic (semantic)
(ecase semantic
(:co (make-instance 'complete))
(:gr (make-instance 'grounded))
(:st (make-instance 'stable))
(:pr (make-instance 'preferred))))
(defun make-task (task &optional arg)
;; (check-type arg (or null string non-negative-fixnum))
(ecase task
(:ee (make-instance 'ee-task))
(:se (make-instance 'se-task))
(:dc (make-instance 'dc-task :arg-name arg))
(:ds (make-instance 'ds-task :arg-name arg)))))
(defmethod make-initial-space (graph (task task) (semantic semantic))
;; (check-type graph graph)
(log* 1 "creating initial bool-space")
(make-bool-space (order graph)))
(defmethod make-initial-space (graph (task se-task) (semantic preferred))
;; (check-type graph graph)
(log* 1 "creating initial pr-bab-space")
(make-pr-bab-space (order graph)))
(defmethod make-initial-space (graph (task ee-task) (semantic preferred))
;; (check-type graph graph)
(log* 1 "creating initial pr-bab-space")
(make-pr-bab-space (order graph)))
(defmethod constrain-space (space (semantic complete) task graph)
;; (check-type space SI:FOREIGN-DATA)
;; (check-type task task)
;; (check-type graph graph)
(log* 1 "constrain-complete")
(constrain-complete graph))
(defmethod constrain-space :after (space (semantic stable) task graph)
;; (check-type space SI:FOREIGN-DATA)
;; (check-type task task)
;; (check-type graph graph)
(log* 1 "constrain-stable")
(constrain-stable graph))
(defmethod constrain-arg-if-needed (space semantic task)
;; (check-type space SI:FOREIGN-DATA)
;; (check-type semantic semantic)
;; (check-type task task)
;; nothing to do here
)
(defmethod constrain-arg-if-needed (space semantic (task decision-task))
;; (check-type space SI:FOREIGN-DATA)
;; (check-type semantic semantic)
;; (check-type task task)
(constrain-arg space semantic task))
(defmethod constrain-arg (space (semantic grounded) (task decision-task))
;; (check-type space SI:FOREIGN-DATA)
;; (check-type semantic semantic)
;; (check-type task task)
(log* 1 "constrain arg not to be in")
(log* 3 "task arg is ~S" (task-arg task))
(post-must-be-false space (task-arg task)))
(defmethod constrain-arg (space semantic (task ds-task))
;; (check-type space SI:FOREIGN-DATA)
;; (check-type semantic semantic)
;; (check-type task task)
(log* 1 "constrain arg not to be in")
(log* 3 "task arg is ~S" (task-arg task))
(post-must-be-false space (task-arg task)))
(defmethod constrain-arg (space semantic (task dc-task))
;; (check-type space SI:FOREIGN-DATA)
;; (check-type semantic semantic)
;; (check-type task task)
(log* 1 "constrain arg to be in")
(log* 3 "task arg is ~S" (task-arg task))
(post-must-be-true space (task-arg task)))
(defmacro branch-with-logging (space &body body)
`(let*-heap (,@body)