-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL03-SolveDebugExplain.tex
More file actions
1570 lines (1327 loc) · 49.7 KB
/
L03-SolveDebugExplain.tex
File metadata and controls
1570 lines (1327 loc) · 49.7 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
%% Source: https://github.com/tias/constraint-solving-course
%% Licensed under CC BY-NC-SA 4.0: https://creativecommons.org/licenses/by-nc-sa/4.0/
%% You may share and adapt this for non-commercial use,
%% with attribution and under the same license.
\documentclass{cons-beamer}
\begin{document}
\begin{frame}{L03: Solving, debugging and explanation techniques}
\begin{center}
~ \\
\includegraphics[height=42mm]{images/texpl_img/interaction_figure4.png} \\
Prof. Tias Guns and Dr. Dimos Tsouros \\[0.5em]
\includegraphics[width=2cm]{images/kuleuven_CMYK_logo.pdf}
\end{center}
{\footnotesize
Partly based on slides from Pierre Flener, Uppsala University.}
% https://pierre-flener.github.io/courses/M4CO/lectures.html
\end{frame}
\section{Solving the problem}
\begin{frame}{Model + \textbf{Solve}}
Declarative problem solving: We model \textbf{what} -- the solver takes care of the \textbf{how} \dots \vfill
We saw how to model a combinatorial problem in a CP modeling language...
\vfill
Now, we want to \textit{solve} it! \vfill
Combinatorial problems:
\begin{itemize}
\item Huge \red{search space}
\item \red{Exponential} growth of possible solutions
\begin{itemize}
\item For \( n \) variables with \( d \) possible values each, the \red{search space size is \( d^n \)}
\end{itemize}
\item Inference based on the constraints helps to prune infeasible solutions early, \inference{reducing the search space}
\end{itemize}
\end{frame}
\begin{frame}{Solving combinatorial problems}
Combinatorial problems: Huge \red{search space}
\vfill
% Huge Search Space without Constraints
\scriptsize
\begin{tikzpicture}[
val/.style={rectangle, fill=blue!20},
root/.style={circle, fill=red!50},
var/.style={rectangle},
level 1/.style={level distance=0.7cm, sibling distance=7cm},
level 2/.style={level distance=0.7cm, sibling distance=3.5cm},
level 3/.style={level distance=0.7cm, sibling distance=1.8cm},
level 4/.style={level distance=0.7cm, sibling distance=0.9cm},
level 5/.style={level distance=1cm, sibling distance=0.4cm},
]
% Variables listed on the side
\coordinate (V) at (-7.4,0);
\node[var] at (V) { };
\coordinate (A) at (-7.4,-0.7);
\node[var] at (A) {$A$};
\coordinate (B) at (-7.4,-1.4);
\node[var] at (B) {$B$};
\coordinate (C) at (-7.4,-2.1);
\node[var] at (C) {$C$};
\coordinate (D) at (-7.4,-2.8);
\node[var] at (D) {$D$};
\coordinate (E) at (-7.4,-3.8);
\node[var] at (E) {$E$};
\coordinate (E) at (-7.4,-4.8);
\node[var] at (E) {...};
% Root node and expanded search space with values only
\node[root] {Start}
child{node[val]{1} % A=1
child{node[val]{1} % B=1
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child{node[val]{2} % C=2
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
}
child{node[val]{2} % B=2
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child{node[val]{2} % C=2
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
}
}
child{node[val]{2} % A=2
child{node[val]{1} % B=1
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child{node[val]{2} % C=2
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
}
child{node[val]{2} % B=2
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child{node[val]{2} % C=2
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
}
};
\end{tikzpicture}
\end{frame}
\begin{frame}{Solving combinatorial problems}
\begin{transparent}{0.3}
Combinatorial problems: Huge \red{search space},
\end{transparent} \textbf{need \inference{intelligent} \search{search}!}
\vfill
% Reduced Search Space with Constraints
\scriptsize
\begin{tikzpicture}[
val/.style={rectangle, fill=blue!20},
root/.style={circle, fill=red!50},
var/.style={rectangle},
pruned/.style={rectangle, draw=red, dashed},
level 1/.style={level distance=0.7cm, sibling distance=7cm},
level 2/.style={level distance=0.7cm, sibling distance=3.5cm},
level 3/.style={level distance=0.7cm, sibling distance=1.8cm},
level 4/.style={level distance=0.7cm, sibling distance=0.9cm},
level 5/.style={level distance=1cm, sibling distance=0.4cm},
]
% Variables listed on the side
\coordinate (V) at (-7.4,0);
\node[var] at (V) { };
\coordinate (A) at (-7.4,-0.7);
\node[var] at (A) {$A$};
\coordinate (B) at (-7.4,-1.4);
\node[var] at (B) {$B$};
\coordinate (C) at (-7.4,-2.1);
\node[var] at (C) {$C$};
\coordinate (D) at (-7.4,-2.8);
\node[var] at (D) {$D$};
\coordinate (E) at (-7.4,-3.8);
\node[var] at (E) {$E$};
\coordinate (E) at (-7.4,-4.8);
\node[var] at (E) {...};
% Root node and expanded search space with values only
\node[root] {Start}
child{node[val]{1} % A=1
child{node[val]{1} % B=1
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child[pruned]{node[pruned]{2} % C=2
child[pruned]{node[pruned]{1} % D=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
child[pruned]{node[pruned]{2} % D=2
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
}
}
child{node[val]{2} % B=2
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child[pruned]{node[pruned]{2} % C=2
child[pruned]{node[pruned]{1} % D=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
child[pruned]{node[pruned]{2} % D=2
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
}
}
}
child{node[val]{2} % A=2
child{node[val]{1} % B=1
child{node[val]{1} % C=1
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
child{node[val]{2} % C=2
child{node[val]{1} % D=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
child{node[val]{2} % D=2
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % E=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % E=2
}
}
}
child[pruned]{node[pruned]{2} % B=2
child[pruned]{node[pruned]{1} % C=1
child[pruned]{node[pruned]{1} % D=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
child[pruned]{node[pruned]{2} % D=2
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
}
child[pruned]{node[pruned]{2} % C=2
child[pruned]{node[pruned]{1} % D=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
child[pruned]{node[pruned]{2} % D=2
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % E=2
}
}
}
};
% Labels for pruned branches
\node at (-3.7, -1.5) {\inference{Constraint C1}};
\node at (-0.2, -1.5) {\inference{Constraint C2}};
\node at (5.7, -0.8) {\inference{Constraint C3}};
\end{tikzpicture}
\end{frame}
\begin{frame}{Solving combinatorial problems}
Different solvers/solving technologies can be used for that. They differ in:
\begin{itemize}
\item The constraints they support (including global constraints/functions)
\item How they perform search and propagation (CP vs MIP vs PB vs SAT)
\item How they guide the search (heuristics, hyper-parameters)
\item \dots
\end{itemize}
\end{frame}
\begin{frame}{Encoding to solver-specific input}
High-level CP modeling languages have to \defined{encode} problems into a solver-specific input format.
\vfill
\begin{center}
\includegraphics[height=60mm]{images/prob2sol.png}
\end{center}
\end{frame}
\begin{frame}{Model Transformations}
\begin{columns}
% Left Column: Diagram
\begin{column}{0.7\textwidth}
\begin{tikzpicture}[scale=0.65, every node/.style={transform shape}, node distance=0.5cm]
% Model (green box)
\node[draw, fill=green!30, rounded corners, minimum width=2cm, minimum height=1cm] (model) {Model (high-level modelling language)};
% Decompose-globals (blue box)
\node[draw, fill=blue!20, below=of model, rounded corners, minimum width=3.5cm, minimum height=1cm] (decompose) {decompose globals};
% Flatten (orange box)
\node[draw, fill=blue!20, below=of decompose, rounded corners, minimum width=3.5cm, minimum height=1cm] (flatten) {flatten};
% Linearize (purple box)
\node[draw, fill=blue!20, below=of flatten, rounded corners, minimum width=3.5cm, minimum height=1cm] (linearize) {linearize};
% int2bool (cyan box)
\node[draw, fill=blue!20, below=of linearize, rounded corners, minimum width=3.5cm, minimum height=1cm] (int2bool) {int to bool};
% pb2sat (pink box)
\node[draw, fill=blue!20, below=of int2bool, rounded corners, minimum width=3.5cm, minimum height=1cm] (pb2sat) {pb to sat};
% Low-level mdoels (no fill)
\node[draw, fill=green!30, rounded corners, dotted, minimum height=1cm, minimum width=4cm, right=1cm of decompose, align=left] (smt) {SMT model};
\node[draw, fill=green!30, rounded corners, dotted, minimum height=1cm, minimum width=4cm, right=1cm of flatten, align=left] (cp) {CP model};
\node[draw, fill=green!30, rounded corners, dotted, minimum width=4cm, right=1cm of linearize, fill=green!30, rounded corners, align=left] (ilp) {ILP model};
\node[draw, fill=green!30, rounded corners, dotted, minimum height=1cm, minimum width=4cm, right=1cm of int2bool, align=left] (pb) {PB model};
\node[draw, fill=green!30, rounded corners, dotted, minimum height=1cm, minimum width=4cm, right=1cm of pb2sat, align=left] (sat) {(max)SAT model};
% Solvers (no fill)
\node[draw, minimum height=1cm, minimum width=4cm, right=1cm of smt, align=left] (smt2) {SMT solver};
\node[draw, minimum height=1cm, minimum width=4cm, right=1cm of cp, align=left] (cp2) {CP solver};
\node[draw, minimum height=1cm, minimum width=4cm, right=1cm of ilp, align=left] (ilp2) {ILP solver};
\node[draw, minimum height=1cm, minimum width=4cm, right=1cm of pb, align=left] (pb2) {PB solver};
\node[draw, minimum height=1cm, minimum width=4cm, right=1cm of sat, align=left] (sat2) {(max)SAT solver};
% Arrows
\draw[->] (model) -- (decompose);
\draw[->] (decompose) -- (flatten);
\draw[->] (flatten) -- (linearize);
\draw[->] (linearize) -- (int2bool);
\draw[->] (int2bool) -- (pb2sat);
% Connections to low-level
\draw[->] (decompose.east) -- ++(0.5,0) |- (smt.west);
\draw[->] (flatten.east) -- ++(0.5,0) |- (cp.west);
\draw[->] (linearize.east) -- ++(0.5,0) |- (ilp.west);
\draw[->] (int2bool.east) -- ++(0.5,0) |- (pb.west);
\draw[->] (pb2sat.east) -- ++(0.5,0) |- (sat.west);
% Connections to solvers
\draw[->] (smt.east) -- ++(0.5,0) |- (smt2.west);
\draw[->] (cp.east) -- ++(0.5,0) |- (cp2.west);
\draw[->] (ilp.east) -- ++(0.5,0) |- (ilp2.west);
\draw[->] (pb.east) -- ++(0.5,0) |- (pb2.west);
\draw[->] (sat.east) -- ++(0.5,0) |- (sat2.west);
\end{tikzpicture}
\end{column}
% Right Column: solver names
%\hspace{1cm}
\begin{column}{0.37\textwidth}
Exxample solvers:
\begin{itemize}
\item SMT: Z3
\item CP: Or-Tools, Choco, GCS, Minizinc (modeling lang)
\item ILP: Gurobi
\item PB: Exact
\item SAT: PySAT
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{flashcardcpmpy} % XXX does not have a preceding general slide...
\begin{frame}[fragile]{Solving in CPMpy} % [fragile] to allow \cpminline
Declarative modeling, easy solving
\footnotesize{\lstinputlisting[language=cpmpy,firstline=5,lastline=14]{models_cpmpy/t3_sudoku.py}}
\vfill
Can also specify the solver to use:
\vfill
\scriptsize{\cpminline{model.solve("choco") # use choco solver - needs pychoco package}}
\scriptsize{\cpminline{model.solve("gurobi") # use gurobi solver - needs gurobipy package}}
\vfill
\normalsize{See what solvers you have in your machine:}
\vfill
\scriptsize{\cpminline{cp.SolverLookup.solvernames()}}
\end{frame}
\end{flashcardcpmpy}
\begin{frame}[fragile]{Solving vs solution enumeration} % [fragile] to allow \verbatim
Is one solution sufficient? In many problems no!
\vfill
Finding all (or multiple) solutions by 'blocking' each found solution:
\begin{verbatim}
while solutions_found < solution_limit:
solve problem
Add constraint that forbids the exact same solution
\end{verbatim}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}{Solving vs solution enumeration -- CPMpy}
Is one solution sufficient? In many problems no!
\vfill
Finding all (or multiple) solutions by 'blocking' each found solution::
\vfill
\footnotesize
\lstinputlisting[language=cpmpy,numbers=none,firstline=16,lastline=23]{models_cpmpy/t3_sudoku.py}
\normalsize
\vfill
or just use \cpminline{model.solveAll()} $\xleftarrow{}$ It returns the amount of solutions found, accessing the solutions: \footnotesize\url{https://cpmpy.readthedocs.io/en/latest/multiple_solutions.html}
\end{frame}
\end{flashcardcpmpy}
\section{Debugging}
\begin{frame}{Debugging}
You solve the problem, but \\
you get an \textbf{error}, \\
or no error, but also \textbf{no (correct) solution}... \\
Annoying, you have a \textbf{bug}.
\vfill
\Large
How do you \textbf{debug} a model?
\begin{center}
\includegraphics[height=20mm]{images/texpl_img/debug.jpg}
\end{center}
\end{frame}
\begin{frame}{Debugging}
General advise for debugging when modeling from expert modeller\textbf{ Håkan Kjellerstrand}:
\vfill
\begin{itemize}
\item Test the model \textbf{early and often}. This makes it easier to detect problems in the model. \vfill
\item When a model is not working, \textbf{activate the constraints one by one} (e.g. comment out the other constraints) to test which constraint is the culprit:
\vspace{-1.5em}
\begin{algorithmic}
\FOR{each constraint $c$ in \textit{Constraints}}
\STATE print ``Trying:'', $c$
\STATE \textit{Solve} $c$
\ENDFOR
\end{algorithmic}
\vfill
\item \textbf{Check the domains} (see lower). The domains should be as small as possible, but not smaller. If they are too large it can take a lot of time to get a solution. If they are too small, then there will be no solution. \vfill
\end{itemize}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}{Debugging -- CPMpy}
General advise for debugging when modeling from expert modeller\textbf{ Håkan Kjellerstrand}:
\vfill
\begin{itemize}
\item Test the model \textbf{early and often}. This makes it easier to detect problems in the model. \vfill
\item When a model is not working, \textbf{activate the constraints one by one} (e.g. comment out the other constraints) to test which constraint is the culprit.
\lstinputlisting[language=cpmpy,basicstyle=\footnotesize,firstline=15,lastline=17]{models_cpmpy/t3_bugged.py} \vfill
\item \textbf{Check the domains} (see lower). The domains should be as small as possible, but not smaller. If they are too large it can take a lot of time to get a solution. If they are too small, then there will be no solution. \vfill
\end{itemize}
\end{frame}
\end{flashcardcpmpy}
\begin{frame}{Debugging}
The bug can be situated in one of three layers:
\begin{enumerate}
\item your model
\item the modeling library (CPMpy)
\item the solver
\end{enumerate}
\vfill
Ordered from most likely to least likely!
\end{frame}
\begin{frame}{Bug in the solver}
You try with the default solver (or another one) and you get an error, or not the desired solution.
\vfill
\large
Use a different solver and observe:
\normalsize
\vfill
\begin{enumerate}
\item Outcome changes! It was a (rare) solver bug. Report it to the bug tracker of the modeling library or directly to the solver developers!
\vfill
\item Outcome is the same! Not a solver error after all \dots
\end{enumerate}
\end{frame}
\begin{flashcardcpmpy} % XXX does not have a preceding general slide...
\begin{frame}{Debugging a modeling error -- CPMpy}
You get an error when you create an expression? \\ Quirks in Python/CPMpy (from last lecture):
\vfill
\begin{enumerate}
\item \textbf{Logical and/or}:
Use $\&$ and $|$, and make sure to always put the subexpressions in brackets.
\vfill
\begin{example}
write \cpminline{(x == 1) & (y == 0)} instead of \cpminline{x == 1 & y == 0}. The latter won't work.
Python will think you meant \cpminline{x == (1 & y) == 0}.
\end{example}
\vfill
\item you can write \cpminline{vars_list[other_var]} but you can’t write \cpminline{non_var_list[a_var]}. That is because the vars list knows CPMpy, and the \cpminline{non_var_list} does not. Wrap it: \cpminline{non_var_list = cp.cpm_array(non_var_list)} first.
\vfill
\item CPMpy overloads all/any/max/min/sum/abs to create expressions with them. Always use \cpminline{cp.sum(v)} instead of \cpminline{sum(v)}. You can also use directly NumPy's \cpminline{v.sum()} instead, if \cpminline{v} is a matrix or tensor.
\end{enumerate}
\end{frame}
\begin{frame}{Debugging a modeling error -- CPMpy}
You get an error when you create an expression \dots But you do not know why!
\vfill
Print the constraints you create (or the subexpressions), and check that the output matches what you wish to express!
\vfill
\begin{example}
The following: \lstinputlisting[language=cpmpy,basicstyle=\footnotesize,firstline=3,lastline=5]{models_cpmpy/t3_sum.py}
will print \cpminline{[(IV0) + (IV2) (IV1) + (IV3)]}
and you can see that it is not really a sum, but a list!
Solution: Use \cpminline{cp.sum(x)} instead!
\end{example}
\end{frame}
\end{flashcardcpmpy}
\section{Explainable Constraint Solving}
\begin{frame}{Explainable Constraint Solving}
You model the problem and you solve!
\blue{No Error!}
\vfill
But also:
\begin{itemize}
\item What if the model is \red{UNSAT}?
\item What if the solution is \red{unexpected}?
\item What if the solution is \red{not good enough}?
\end{itemize}
\vfill
There is a modeling error \dots
Or the problem constraints are too tight \dots
\vfill
\textbf{Explainable AI}: Human-Aware AI systems that interact with the users to assist in decision making
\end{frame}
\begin{frame}{Mode of interaction}
\centering
\includegraphics[height=60mm]{images/texpl_img/interaction_figure4.png}
\end{frame}
\begin{frame}{Explainable Constraint Solving}
In general, "Why $X$?" (with $X$ (part of) a solution or \red{UNSAT})
2 patterns of explanations:
\vfill
\begin{enumerate}
\item \textbf{Deductive explanation}:
How was $X$ derived? (Why I didn't get any solution?)
\vfill
\item \textbf{Counterfactual explanation}:
Why $X$ and not $Z$? (How can I make it satisfiable?)
\vfill
\end{enumerate}
\end{frame}
\begin{frame}{Running Example}
\begin{example}[Graph Colouring]
\footnotesize
Graph colouring is the problem of assigning colours to the nodes of a graph, such that no two \textbf{adjacent} nodes share the same colour.
\begin{itemize}
\item Variables are the nodes, possible values are the colours:
$\text{node}_i \in \{1, 2, \dots, \text{max\_colors}\}, \quad \forall i \in \text{Nodes}$
\item Constrain edges to have differently colored nodes (i.e., not equal values):
$\text{node}_{1} \neq \text{node}_{2}, \quad \forall (\text{node}_1, \text{node}_2) \in \text{Edges}$
\end{itemize}
\end{example}
\vspace{-0.4cm}
\footnotesize
\begin{columns}
\begin{column}{0.45\textwidth}
\begin{center}
Initial graph:
\includegraphics[height=30mm]{images/texpl_img/graph_not_coloured.png}
\end{center}
\end{column}
\begin{column}{0.1\textwidth}
\begin{center}
\begin{tikzpicture}
\draw[->, thick] (0,0) -- (1,0); % Draws an arrow
\end{tikzpicture}
\end{center}
\end{column}
\begin{column}{0.45\textwidth}
\begin{center}
Coloured graph:
\includegraphics[height=30mm]{images/texpl_img/graph_coloured.png}
\end{center}
\end{column}
\end{columns}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}{Running Example -- CPMpy}
\begin{example}[Graph Colouring]
\footnotesize
Graph colouring is the problem of assigning colours to the nodes of a graph, such that no two \textbf{adjacent} nodes share the same colour.
\footnotesize\lstinputlisting[language=cpmpy,numbers=none,firstline=15,lastline=19]{models_cpmpy/t3_graph_colouring.py}
\end{example}
\vspace{-0.4cm}
\footnotesize
\begin{columns}
\begin{column}{0.45\textwidth}
\begin{center}
Initial graph:
\includegraphics[height=30mm]{images/texpl_img/graph_not_coloured.png}
\end{center}
\end{column}
\begin{column}{0.1\textwidth}
\begin{center}
\begin{tikzpicture}
\draw[->, thick] (0,0) -- (1,0); % Draws an arrow
\end{tikzpicture}
\end{center}
\end{column}
\begin{column}{0.45\textwidth}
\begin{center}
Coloured graph:
\includegraphics[height=30mm]{images/texpl_img/graph_coloured.png}
\end{center}
\end{column}
\end{columns}
\end{frame}
\end{flashcardcpmpy}
\subsection{General case: Explaining unsatisfiabilty}
\begin{frame}{Graph Colouring: Unsatisfiable}
But what if our problem is not satisfiable?
\begin{itemize}
\item e.g. we have less colours available than needed!
\end{itemize}
\begin{example}
\texttt{m, nodes = graph\_coloring(G, max\_colors=3)}
\texttt{No solution found.}
\end{example}
\vfill
Explanation techniques can help us understand:
\begin{itemize}
\item Why is it unsatisfiable? (Deductive explanation)
\item How to fix it? (Counterfactual explanation)
\end{itemize}
\end{frame}
\subsubsection{Deductive Explanations}
\begin{frame}{Deductive Explanations}
\begin{columns}
\begin{column}{0.5\textwidth}
\begin{center}
\includegraphics[height=60mm]{images/texpl_img/explain_unsat.png}
\end{center}
\end{column}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item Find the cause!
\item Why $X$? (e.g. why is it UNSAT?)
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Deductive Explanations}
\begin{center}
Question: "Why is it unsatisfiable?"
\end{center}
\begin{columns}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item Answer: "The set of all constraints cannot be satisfied."
\end{itemize}
\begin{center}
\includegraphics[height=30mm]{images/texpl_img/allcons.png}
\end{center}
Not very useful ...
\vfill
\end{column}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item Answer: "This (small) subset of constraints cannot be satisfied together!"
\end{itemize}
\begin{center}
\includegraphics[height=30mm]{images/texpl_img/mus.png}
\end{center}
Pinpoint to a subset of constraints causing a conflict ...
\vfill
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Deductive Explanations: Graph colouring}
\begin{center}
Question: "Why is my graph colouring problem unsatisfiable?"
\end{center}
\begin{columns}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item Answer: "I cannot colour this graph with all these constraints."
\end{itemize}
\begin{center}
\includegraphics[height=30mm]{images/texpl_img/graph_all_cons_red.png}
\end{center}
Not very useful ...
\vfill
\end{column}
\begin{column}{0.5\textwidth}
\begin{itemize}
\item Answer: "These constraints prevent me from finding a solution!"
\end{itemize}
\begin{center}
\includegraphics[height=30mm]{images/texpl_img/graph_mus_red.png}
\end{center}