-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL01-Introduction.tex
More file actions
1529 lines (1390 loc) · 63.4 KB
/
L01-Introduction.tex
File metadata and controls
1529 lines (1390 loc) · 63.4 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}
% custom columns for array package:
\newcolumntype{C}{>{\centering\arraybackslash}p{65mm}} % comb opti examples
\newcolumntype{D}{>{\centering\arraybackslash}p{11mm}} % doctor rostering
\newcolumntype{G}{>{\centering}m{9mm}} % genotypes
\newcolumntype{H}{>{\centering}m{4mm}} % haplotypes
% commands for BIBD, AED:
\newcommand{\VarSem}{sample} \newcommand{\BlkSem}{growth}
\newcommand{\Variety}{grain} \newcommand{\Block}{\textnormal{plot}}
\newcommand{\VarBlk}{grown in} \newcommand{\BlkVar}{grows}
\newcommand{\BlkVarAlt}{grow}
\newcommand{\VarOne}{barley} \newcommand{\BlkOne}{\Block1}
\newcommand{\VarTwo}{corn} \newcommand{\BlkTwo}{\Block2}
\newcommand{\VarThree}{millet} \newcommand{\BlkThree}{\Block3}
\newcommand{\VarFour}{oats} \newcommand{\BlkFour}{\Block4}
\newcommand{\VarFive}{rye} \newcommand{\BlkFive}{\Block5}
\newcommand{\VarSix}{spelt} \newcommand{\BlkSix}{\Block6}
\newcommand{\VarSeven}{wheat} \newcommand{\BlkSeven}{\Block7}
% commands for doctor rostering
\newcommand{\appt}{\defined{appt}}
\newcommand{\call}{\search{call}}
\newcommand{\none}{\relaxation{none}}
\newcommand{\oper}{\inference{oper}}
\begin{document}
\begin{frame}{L01: Intro to Model+Solve and Combinatorial Optimisation}
\begin{center}
~ \\
\includegraphics[height=38mm]{images/CO_examples} \\
~ \\
Prof. Tias Guns and Dr Dimos Tsouros \\[0.5em]
\includegraphics[width=2cm]{images/kuleuven_CMYK_logo.pdf}
\end{center}
{\footnotesize
Based on slides from Pierre Flener, Uppsala University + slides from Guido Tack \& Chris Beck.}
% https://pierre-flener.github.io/courses/M4CO/lectures.html
\end{frame}
\begin{frame}
\begin{example}[From: Steven S. Skiena's The Algorithm Design Manual]
\small
Imagine you are a highly-in-
demand actor, who has been presented with offers to star in n different movie
projects under development. Each offer comes specified with the first and last day
of filming. To take the job, you must commit to being available throughout this
entire period. Thus, you cannot simultaneously accept two jobs whose intervals
overlap.
$ $
For an artist such as yourself, the criteria for job acceptance is clear: you want
to make as much money as possible. Because each of these films pays the same fee
per film, this implies you seek the largest possible set of jobs (intervals) such that no two of them conflict with each other.
\begin{table}[h!]
\centering
\tiny
\begin{tabular}{| m{5cm} | m{2cm} | m{2cm} |}
\hline
\textbf{Title} & \textbf{Start} & \textbf{End} \\ \hline
Tarjan of the Jungle & 4 & 13 \\ \hline
The Four Volume Problem & 17 & 27 \\ \hline
The President's Algorist & 1 & 10 \\ \hline
Steiner's Tree & 12 & 18 \\ \hline
Process Terminated & 23 & 30 \\ \hline
Halting State & 9 & 16 \\ \hline
Programming Challenges & 19 & 25 \\ \hline
Discrete Mathematics & 2 & 7 \\ \hline
Calculated Bets & 26 & 31 \\ \hline
\end{tabular}
\end{table}
\end{example}
How would you solve this problem?
\end{frame}
\begin{frame}
\begin{example}[From: Steven S. Skiena's The Algorithm Design Manual]
\small
Imagine you are a highly-in-
demand actor, who has been presented with offers to star in n different movie
projects under development. Each offer comes specified with the first and last day
of filming. To take the job, you must commit to being available throughout this
entire period. Thus, you cannot simultaneously accept two jobs whose intervals
overlap.
$ $
For an artist such as yourself, the criteria for job acceptance is clear: you want
to make as much money as possible. Because each of these films pays the same fee
per film, this implies you seek the largest possible set of jobs (intervals) such that no two of them conflict with each other.
\begin{table}[h!]
\centering
\tiny
\begin{tabular}{| m{5cm} | m{2cm} | m{2cm} |}
\hline
\textbf{Title} & \textbf{Start} & \textbf{End} \\
\hline
... & ... & ... \\
\hline
\end{tabular}
\end{table}
\end{example}
How would you solve this problem?
\begin{enumerate}
\item Trial and error on paper
\item Write a custom search algorithm
\item Reuse an existing, generic problem solving paradigm \alt<2>{\textbf{$\Leftarrow$ This Course}}{}
\end{enumerate}
\end{frame}
% based on slide from Chris Beck
\begin{frame}{Model-and-Solve}
\centering
\includegraphics[height=68mm]{images/prob2def}
\end{frame}
\begin{frame}{Model-and-Solve}
\centering
\includegraphics[height=68mm]{images/prob2sol}
\end{frame}
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\section{Combinatorial Optimisation Problems}
\begin{frame}{Combinatorial Optimisation}
\begin{center}
~ \\
\includegraphics[height=48mm]{images/CO_examples} \\
~ \\
\visible<2>{Combinatorial Optimisation is a science of \alert{service}: \\
to scientists, to engineers, to artists, and to society.}
\end{center}
\end{frame}
\begin{frame}
\begin{example}[Agricultural experiment design]
\begin{table} \small
\begin{tabular}{r|c|c|c|c|c|c|c|}
\multicolumn{1}{r}{} & \multicolumn{1}{c}{\BlkOne}
& \multicolumn{1}{c}{\BlkTwo} & \multicolumn{1}{c}{\BlkThree}
& \multicolumn{1}{c}{\BlkFour} & \multicolumn{1}{c}{\BlkFive}
& \multicolumn{1}{c}{\BlkSix} & \multicolumn{1}{c}{\BlkSeven} \\
\cline{2-8}
\VarOne & \alt<2>{\tick}{} & \alt<2>{\tick}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{--}{} \\ \cline{2-8}
\VarTwo & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} \\ \cline{2-8}
\VarThree & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{\tick}{} \\ \cline{2-8}
\VarFour & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} \\ \cline{2-8}
\VarFive & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{\tick}{} \\ \cline{2-8}
\VarSix & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} \\ \cline{2-8}
\VarSeven & \alt<2>{--}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{--}{} & \alt<2>{\tick}{} & \alt<2>{\tick}{} & \alt<2>{--}{} \\
\cline{2-8}
\end{tabular}
\end{table}
\textbf{Constraints} to be \textbf{satisfied}:
\begin{enumerate}
\item Equal \VarSem\ size: Every \Variety\ is \VarBlk\ 3 \Block s.
\item Equal \BlkSem\ load: Every \Block\ \BlkVar\ 3 \Variety s.
\item Balance: Every \Variety\ pair is \VarBlk\ 1 common \Block.
\end{enumerate}
\textbf{Instance}: $7$ \Block s, $7$ \Variety s, $3$ \Variety
s/\Block, $3$ \Block s/\Variety, balance~$1$.
\end{example}
\end{frame}
\begin{frame}
\begin{example}[Doctor rostering]
\begin{table}
\begin{tabular}{l|D|D|D|D|D|D|D|}
\multicolumn{1}{l}{} & \multicolumn{1}{c}{Mon} & \multicolumn{1}{c}{Tue} & \multicolumn{1}{c}{Wed} & \multicolumn{1}{c}{Thu} & \multicolumn{1}{c}{Fri} & \multicolumn{1}{c}{Sat} & \multicolumn{1}{c}{Sun} \\
\cline{2-8}
Doctor A & \alt<2>{\call}{} & \alt<2>{\none}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\none}{} \\ \cline{2-8}
Doctor B & \alt<2>{\appt}{} & \alt<2>{\call}{} & \alt<2>{\none}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\none}{} & \alt<2>{\call}{} \\ \cline{2-8}
Doctor C & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\call}{} & \alt<2>{\appt}{} & \alt<2>{\appt}{} & \alt<2>{\call}{} & \alt<2>{\none}{} \\ \cline{2-8}
Doctor D & \alt<2>{\appt}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\call}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\none}{} \\ \cline{2-8}
Doctor E & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\oper}{} & \alt<2>{\none}{} & \alt<2>{\call}{} & \alt<2>{\none}{} & \alt<2>{\none}{} \\ \cline{2-8}
\end{tabular}
\end{table}\vspace{-7pt}
\textbf{Constraints} to be \textbf{satisfied}: \\[+5pt]
\vfill
\begin{minipage}[c]{0.49\textwidth}
\begin{enumerate}
\item \#\search{on-call doctors} / day $=$ 1
\item \#\inference{operating doctors} / weekday $\leq$ 2
\item \#\inference{operating doctors} / week $\geq$ 7
\item \#\defined{appointed doctors} / week $\geq$ 4
\item \relaxation{day off} after \inference{operation day}
\item \dots
\end{enumerate}
\end{minipage}
\hfill
\begin{minipage}[c]{0.50\textwidth}
\visible<2>{\includegraphics[width=63mm]{images/physicians}}
\end{minipage} \\[+5pt]
\textbf{Objective function} to be \textbf{minimised}:
Cost: \dots
\end{example}
\end{frame}
\begin{frame}
\begin{example}[Vehicle routing: parcel delivery]
\structured{Given} a depot with parcels for clients and a vehicle
fleet, \\ \structured{find} which vehicle visits which client when.
\\ [+1mm]
\textbf{Constraints} to be \textbf{satisfied}: \\
\begin{minipage}[c]{0.59\textwidth}
\begin{enumerate}
\item All parcels are delivered on time. \vfill
\item No vehicle is overloaded. \vfill
\item Driver regulations are respected. \vfill
\item \dots
\end{enumerate}
\end{minipage}
\hfill
\begin{minipage}[c]{0.40\textwidth}
\includegraphics[width=40mm]{images/VRP}
\end{minipage} \vfill
\textbf{Objective function} to be \textbf{minimised}:
\begin{itemize}
\item Cost: the total fuel consumption and driver salary.
\end{itemize}
\end{example}\vfill
\begin{example}[Travelling salesperson: optimisation TSP]
\structured{Given} a map and cities, \\ \structured{find} a
\textbf{shortest} route visiting each city once and returning to
the starting city.
\end{example}
\end{frame}
\begin{frame}{Applications in Air Traffic Management}
\begin{tabular}{CC}
\structured{Demand vs capacity} \newline
\includegraphics[height=25mm]{images/routes}
&
\structured{Airspace sectorisation} \newline
\includegraphics[height=25mm]{images/sectorLondon}
\\[+8mm]
\structured{Contingency planning} \newline\newline
\tiny
\begin{tabular}{|l|c|c|}
\hline
Flow & Time Span & Hourly Rate \\
\hline
From: Arlanda & 00:00 -- 09:00 & 3 \\
To: west, south & 09:00 -- 18:00 & 5 \\
& 18:00 -- 24:00 & 2 \\
\hline
From: Arlanda & 00:00 -- 12:00 & 4 \\
To: east, north & 12:00 -- 24:00 & 3 \\
\hline
\dots & \dots & \dots \\
\hline
\end{tabular}
&
\structured{Workload balancing} \newline
\includegraphics[height=25mm]{images/atm}
\end{tabular}
\end{frame}
\begin{frame}
\begin{example}[Air-traffic demand-capacity balancing]
Reroute flights, in height and speed, so as to balance the
workload of air traffic controllers in a multi-sector airspace:
\vfill
\begin{center}
\includegraphics[height=58mm]{images/routes}
\end{center}
\end{example}
\end{frame}
\begin{frame}
\begin{example}[Airspace sectorisation]
\begin{tabular}{p{60mm}p{60mm}}
\structured{Given} an airspace split into $c$ cells,
a targeted number $s$ of sectors, and~flight schedules. &
\structured{Find} a colouring of the $c$ cells into~$s$
connected convex sectors, with minimal imbalance of the
workloads of their air traffic controllers. \\
\includegraphics[height=40mm]{images/hexLondon} \hfill &
\includegraphics[height=40mm]{images/sectorLondon}
\end{tabular}
\\ There are $s^c$ possible colourings, but very few optimally
satisfy the constraints: is \alert{intelligent} search necessary?
\end{example}
\end{frame}
\begin{frame}{Applications in Biology and Medicine}
\begin{tabular}{CC}
\structured{Phylogenetic supertree} \newline
\includegraphics[height=28mm]{images/seabirds}
&
\structured{Haplotype inference} \newline
\includegraphics[height=25mm]{images/genotype-haplotype}
\\[+8mm]
\structured{Medical image analysis} \newline
\includegraphics[width=50mm]{images/medical}
&
\structured{Doctor rostering}\invisible{gy} \newline
\includegraphics[width=50mm]{images/physicians}
\end{tabular}
\end{frame}
\begin{frame}
\begin{example}[Given several phylogentic trees, what supertree is maximally consistent with shared species in the trees?]
\centering
\includegraphics[width=100mm]{images/seabirds.pdf}
\end{example}
\end{frame}
\begin{frame}
\begin{example}[Haplotype inference by pure parsimony]
\structured{Given} $n$ child genotypes, with homo- and
heterozygous sites:
\begin{center} \footnotesize
\begin{tabular}{|G|G|G|G|G|G|}
\hline
\multicolumn{6}{|c|}{$\cdots$} \tabularnewline \hline
A & \blue{C} / \red{G} & T & C & \blue{A} / \red{T} & C \tabularnewline \hline
\multicolumn{6}{|c|}{$\cdots$} \tabularnewline \hline
A / T & G & T & C / G & A & C \tabularnewline \hline
\multicolumn{6}{|c|}{$\cdots$} \tabularnewline \hline
\end{tabular}
\end{center}
\structured{find} a minimal set of (at most $2 \cdot n$) parent
haplotypes:
\begin{center} \footnotesize
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\multicolumn{6}{|c|}{$\cdots$} \\ \hline
A & \blue{C} & T & C & \red{T} & C \\ \hline
\multicolumn{6}{|c|}{$\cdots$} \\ \hline
A & \red{G} & T & C & \blue{A} & C \\ \hline
\multicolumn{6}{|c|}{$\cdots$} \\ \hline
T & G & T & G & A & C \\ \hline
\multicolumn{6}{|c|}{$\cdots$} \\ \hline
\end{tabular}
\end{center}
\structured{so that} each given genotype conflates (is the merge of) 2 found
haplotypes.
\end{example}
\end{frame}
\begin{frame}{Applications in Programming and Testing}
\begin{tabular}{CC}
\structured{Robot programming} \newline
\includegraphics[width=50mm]{images/ABB-FRIDA}
&
\structured{Sensor-net configuration} \newline
\includegraphics[width=50mm]{images/WSN}
\\[+8mm]
\structured{Compiler design} \newline
\includegraphics[height=30mm]{images/instruction-scheduling}
&
\structured{Base-station testing}
\includegraphics[height=30mm]{images/baseStation}
\end{tabular}
\end{frame}
\begin{frame}{Other Application Areas}
\begin{tabular}{CC}
\structured{School timetabling} \newline
\includegraphics[height=30mm]{images/timetable}
&
\structured{Sports tournament design} \newline
\includegraphics[height=5mm]{images/SEH} \newline
\includegraphics[height=25mm]{images/handball}
\\[+8mm]
\structured{Security: SQL injection} \newline
\includegraphics[height=24mm]{images/sqlinjection.png}
&
\structured{Container packing} \newline
\includegraphics[height=27mm]{images/binPacking}
% &
% \structured{Financial designs} \newline
% \includegraphics[height=27mm]{images/WallStreet}
\end{tabular}
\end{frame}
\section{Search and constraint solving paradigms}
\begin{frame}
Let's reconsider:
\begin{example}[Agricultural experiment design]
\begin{table} \small
\begin{tabular}{r|c|c|c|c|c|c|c|}
\multicolumn{1}{r}{} & \multicolumn{1}{c}{\BlkOne}
& \multicolumn{1}{c}{\BlkTwo} & \multicolumn{1}{c}{\BlkThree}
& \multicolumn{1}{c}{\BlkFour} & \multicolumn{1}{c}{\BlkFive}
& \multicolumn{1}{c}{\BlkSix} & \multicolumn{1}{c}{\BlkSeven} \\
\cline{2-8}
\VarOne & {\tick}{} & {\tick}{} & {\tick}{} & {--}{} & {--}{} & {--}{} & {--}{} \\ \cline{2-8}
\VarTwo & {\tick}{} & {--}{} & {--}{} & {\tick}{} & {\tick}{} & {--}{} & {--}{} \\ \cline{2-8}
\VarThree & {\tick}{} & {--}{} & {--}{} & {--}{} & {--}{} & {\tick}{} & {\tick}{} \\ \cline{2-8}
\VarFour & {--}{} & {\tick}{} & {--}{} & {\tick}{} & {--}{} & {\tick}{} & {--}{} \\ \cline{2-8}
\VarFive & {--}{} & {\tick}{} & {--}{} & {--}{} & {\tick}{} & {--}{} & {\tick}{} \\ \cline{2-8}
\VarSix & {--}{} & {--}{} & {\tick}{} & {\tick}{} & {--}{} & {--}{} & {\tick}{} \\ \cline{2-8}
\VarSeven & {--}{} & {--}{} & {\tick}{} & {--}{} & {\tick}{} & {\tick}{} & {--}{} \\
\cline{2-8}
\end{tabular}
\end{table}
\textbf{Constraints} to be \textbf{satisfied}:
\begin{enumerate}
\item Equal \VarSem\ size: Every \Variety\ is \VarBlk\ 3 \Block s.
\item Equal \BlkSem\ load: Every \Block\ \BlkVar\ 3 \Variety s.
\item Balance: Every \Variety\ pair is \VarBlk\ 1 common \Block.
\end{enumerate}
\textbf{Instance}: $7$ \Block s, $7$ \Variety s, $3$ \Variety
s/\Block, $3$ \Block s/\Variety, balance~$1$.
\end{example}
Could you compute a solution for 50 grains and 20 plots? for 1000 grains?
\end{frame}
\begin{frame}{P $\overset{?}{=}$ NP \hfill (Cook, 1971; Levin, 1973)}
This is one of the seven
\href{https://www.claymath.org/millennium/p-vs-np}{Millennium Prize}
problems of the Clay Mathematics Institute (Massachusetts, USA),
each worth 1~million~US\$. \vfill
Informally:
\begin{itemize}
\item P = class of problems that need \alert{no} search to be solved \\
NP = class of problems that \alert{might} need search to solve
\item P = class of problems with easy-to-\alert{compute} solutions \\
NP = class of problems with easy-to-\alert{check} solutions
\end{itemize}
Thus: Can search always be avoided (P $=$ NP), \\ or is search
sometimes necessary (P $\neq$ NP)? \vfill
Problems that are solvable in polynomial time (in the input size)
are considered \defined{tractable}, aka \defined{easy}. \\ Problems
needing super-polynomial time are considered \defined{intractable},
aka \defined{hard}.
\end{frame}
\begin{frame}[label={longest}]{NP Completeness: Examples}
Given a digraph $(V,E)$:
\begin{examples}
\begin{itemize}
\item Finding a \defined{shortest path} takes
$\Oh{V \cdot E}$ time and is thus in P.
\item Determining the existence of a simple path (which has
distinct vertices), from a given single source, that has
\emph{at least} a given number $\ell$ of edges \\ is
NP-complete. Hence finding a \defined{longest path}
seems hard: \\ increase $\ell$ starting from a trivial lower
bound, until answer is `no'.
\end{itemize}
\end{examples}
\begin{examples}
\begin{itemize}
\item Finding an \defined{Euler tour} (which visits each
\emph{edge} once) takes $\Oh{E}$ time \\ and is thus in P.
\item Determining the existence of a \defined{Hamiltonian cycle}
(which visits each \emph{vertex} once) is NP-complete.
\end{itemize}
\end{examples}
\end{frame}
\begin{frame}{NP Completeness: More Examples}
\begin{examples}
\begin{itemize}
\item \defined{$n$-SAT}: Determining the satisfiability of a
conjunction of disjunctions of~$n$~Boolean literals is in P for
$n=2$ but NP-complete for $n=3$.
\item \defined{SAT}: Determining the satisfiability of a formula
over Boolean literals is NP-complete.
\item \defined{Clique}: Determining the existence of a clique
(complete subgraph) of a given size in a graph is NP-complete.
\item \defined{Vertex Cover}: Determining the existence of a
vertex cover (a vertex subset with at least one endpoint for all
edges) of a given size in a graph is NP-complete.
\item \defined{Subset Sum}: Determining the existence of a subset,
of a given set, that has a given sum is NP-complete.
\end{itemize}
\end{examples}
\end{frame}
\begin{frame}
Search spaces are often larger than the universe! \vfill
\includegraphics[height=50mm]{images/burningLaptop} \\
Many important real-life problems are NP-hard or worse: their
real-life instances can only be solved exactly and fast enough by
\alert{intelligent} search, unless $\text{P} =
\text{NP}$.
$ $ \\
\alert{NP-hardness is not where the fun ends, its where it begins!}
\end{frame}
\begin{frame}
\begin{example}[Optimisation TSP over $n$ cities]
A brute-force algorithm evaluates all $n!$ candidate routes:
\begin{itemize}
\item A computer of today evaluates $10^6$ routes / second:
\begin{center}\footnotesize
\begin{tabular}{lr}
$n$ & time \\
\hline
$11$ & $40$ seconds \\
$14$ & $1$ day \\
$18$ & $203$ years \\
$20$ & $77$k years
\end{tabular}
\end{center}
\item Planck time is shortest useful interval:
$\approx 5.4 \cdot 10^{-44}$~second; \\ a Planck computer would
evaluate $1.8 \cdot 10^{43}$ routes / second:
\begin{center}\footnotesize
\begin{tabular}{lr}
$n$ & time \\
\hline
$37$ & $0.7$ seconds \\
$41$ & $20$ days \\
$48$ & $1.5 ~\cdot$ age of universe
\end{tabular}
\end{center}
\end{itemize}
The \underline{dynamic program} by Bellman-Held-Karp ``only'' takes
$\Oh{n^2 \cdot 2^n}$ time: \\ a computer of today takes a day for
$n=27$, a year for $n=35$, the age of the universe for $n=67$, and
beats the $\Oh{n!}$ algo on
Planck computer for $n \geq 44$.
\end{example}
\end{frame}
\begin{frame}{Intelligent Search upon NP-Hardness}
\alert{Do not give up but try to stay ahead of the curve: \\ there
is an instance size until which an \textbf{exact} algorithm is
fast enough!}
\begin{tikzpicture} \centering
\begin{semilogyaxis}[height=45mm, width=8cm, clip=false, axis lines=left,
xlabel={$n$}, x label style={at={(1.02,0.07)}},
xmin=10, xmax=50, xtick={10,14,27,35,44,48},
ylabel={time (s): log scale!},
ymin=0.1, ytickten={0,5,8,13,18}]
\draw[very thin,color=gray] (10,86400) -- (50,86400)
node[right] {\footnotesize 1 day};
\draw[very thin,color=gray] (10,31536000) -- (50,31536000)
node[right] {\footnotesize 1 year};
\draw[very thin,color=gray] (10,4.35*10^17) -- (50,4.35*10^17)
node[right] {\footnotesize age of universe}; % 13.8 billion years
\addplot[OliveGreen,very thick,domain=10:25,samples=16] {x!/(10^6)}
node[above] {$n!$ (today)};
\addplot[Orange,very thick,domain=36:49,samples=14] {x!/(1.8*10^43)}
node[above] {$n!$ (Planck)};
\addplot[Orchid,very thick,domain=10:48,samples=39] {(x^2)*(2^x)/(10^6)}
node[right] {$n^2 \cdot 2^n$ (today)};
\end{semilogyaxis}
\end{tikzpicture} \\
\href{https://www.math.uwaterloo.ca/tsp/concorde}{Concorde TSP Solver} beats the
\defined{Bellman-Held-Karp} exact algo: it uses local search \&
approximation algos, but sometimes proves exactness of its optima.
The largest instance solved exactly, in $136$ CPU years in 2006, has $n=85900$.
\end{frame}
\begin{frame}
A \defined{declarative problem solving paradigm} offers languages, methods, and tools for: \vfill
\begin{itemize}
\item[what:] \textbf{Modelling} combinatorial problems in a \alert{declarative} language.
\end{itemize}\vfill
and / or \vfill
\begin{itemize}
\item[how:] \textbf{Solving} combinatorial problems \inference{intelli}\relaxation{gently}: \vfill
\begin{itemize}
\item \search{Search}: Explore the space of possible assignments. \vfill
\item \inference{Inference}: Reduce the space to feasible (partial) assignments. \vfill
\item \relaxation{Relaxation}: Exploit solutions to problems with fewer or simplified constraints. \vfill
\end{itemize}
\end{itemize}
A \defined{solver} is a program that takes a model and data as input \\
and tries to solve that problem instance.\vfill
\textit{The ideas in this course extend to continuous optimisation, stochastic optimisation, planning and more}
\end{frame}
\begin{frame}
\begin{examples}[Declarative problem solving paradigms]
General-purpose solvers, taking model and data as input:
\begin{itemize}
\item SAT: Boolean satisfiability
\item PB: Pseudo-Boolean Optimisation (0-1 linear constraints)
\item SMT/OMT: SAT (resp.\ Optimisation) Modulo Theories
\item MIP: Mixed Integer (Linear) Programming
\item CP: Constraint programming
\item \dots
\end{itemize}
\end{examples}
\begin{examples}[Solving methodologies]
Methodologies (typically without separated concept of 'model' and 'solver'):
\begin{itemize}
\item Dynamic programming (DP)
\item Greedy and Approximation algorithms
\item Local search (LS)
\item \dots
\end{itemize}
\end{examples}
\end{frame}
\begin{frame}
\includegraphics[height=70mm]{images/encode_search_decode.png}
\end{frame}
\begin{frame}
\includegraphics[height=70mm]{images/encode_search_decode_example.png}
\end{frame}
\section{What: Declarative Modelling}
\begin{frame}{What vs How}
\begin{example}
Consider the \textbf{problem} of sorting an array $A$ of $n$
numbers into an array $S$ \\ of increasing-or-equal numbers.
\\[+8pt]
A \textbf{formal specification} is:
\[
\text{sort}(A,S) \equiv
\text{permutation}(A,S) \land \text{increasing}(S)
\]
saying that $S$ must be a permutation of $A$ in increasing order.
\\[+8pt]
Seen as a generate-and-test \textbf{algorithm}, it takes $\Oh{n!}$ time, \\
% since there are $n!$ permutations of $A$,
but it can be refined into the existing $\Oh{n \log n}$ algorithms.
\\[+8pt]
\end{example}
A \defined{specification} is a \textbf{declarative} description of \textbf{what} problem is to be solved. \\
An \defined{algorithm} is an \textbf{imperative} description of \textbf{how} to solve the problem (fast).
\end{frame}
\begin{frame}{Modelling vs Programming}
\begin{center}
\begin{tikzpicture}
[->,>=stealth',level/.style={level distance=20mm,sibling distance=50mm}]
\tikzset{nod/.style = {font=\bfseries}}
\node[nod]{problem}
child{
node[nod]{specification}
child{
node[nod]{model}
child{
node[nod]{program}
edge from parent node[left] {automatic!}
}
edge from parent node[above left] {what? (declarative)}
}
child{
node[nod]{algorithm}
child{
node[nod]{program}
edge from parent node[right] {manual!}
}
edge from parent node[above right] {how? (imperative)}
}
};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}
\begin{definitions}
A \defined{combinatorial optimisation problem} consists of:
\begin{itemize}
\item \textbf{Decision variables}: the unknowns for which values have to be found
\item \textbf{Domains}: for each variable what its allowed values are
\item \textbf{Constraints}: relations between decision variables that must be satisfied
\item optionally an \textbf{Objective function}: a mathematical function over the decision variables that must be minimized or maximized.
\end{itemize}
\end{definitions}
\begin{definitions}
An \defined{assignment} maps each decision variable to a value within its domain; it is:
\begin{itemize}
\item \defined{feasible} if all the constraints are satisfied;
\item \defined{optimal} if the objective function takes an optimal value.
\end{itemize}
The \defined{search space} consists of all possible assignments. \\
A \defined{solution} to a \defined{satisfaction problem} is a feasible assignment. \\
An \defined{optimal solution} to an \defined{optimisation problem} is a feasible \& optimal assignment.
\end{definitions}
\end{frame}
\begin{frame}[fragile]
\begin{example}[Sudoku]
\begin{center}
\vspace{2mm}
\PuzzleUnitlength=11pt
\renewcommand*\SudokuLinethickness{1pt}
\renewcommand*\PuzzleFont{\sf\tiny}
\begin{Sudoku}
%% https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
%% hardest Sudoku: AI14473 @ http://aisudoku.com, only 21 hints:
|*8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4|*3|*6| 8| 2| 1| 7| 5|.
| 6|*7| 5| 4|*9| 1|*2| 8| 3|.
| 1|*5| 4| 2| 3|*7| 8| 9| 6|.
| 3| 6| 9| 8|*4|*5|*7| 2| 1|.
| 2| 8| 7|*1| 6| 9| 5|*3| 4|.
| 5| 2|*1| 9| 7| 4| 3|*6|*8|.
| 4| 3|*8|*5| 2| 6| 9|*1| 7|.
| 7|*9| 6| 3| 1| 8|*4| 5| 2|.
\end{Sudoku}
~~~~~
\PuzzleSolution
\begin{Sudoku}
| 8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4| 3| 6| 8| 2| 1| 7| 5|.
| 6| 7| 5| 4| 9| 1| 2| 8| 3|.
| 1| 5| 4| 2| 3| 7| 8| 9| 6|.
| 3| 6| 9| 8| 4| 5| 7| 2| 1|.
| 2| 8| 7| 1| 6| 9| 5| 3| 4|.
| 5| 2| 1| 9| 7| 4| 3| 6| 8|.
| 4| 3| 8| 5| 2| 6| 9| 1| 7|.
| 7| 9| 6| 3| 1| 8| 4| 5| 2|.
\end{Sudoku}
\end{center}
\vspace{-2mm}
%
The goal of Sudoku is to \textit{complete} a 9x9 grid with numbers so that each row, column and 3x3 section contain each digit between 1 and 9 once. % Adapted from sudoku.com
\end{example}
\end{frame}
\begin{frame}[fragile]
\begin{center}
\vspace{2mm}
\PuzzleUnitlength=11pt
\renewcommand*\SudokuLinethickness{1pt}
\renewcommand*\PuzzleFont{\sf\tiny}
\begin{Sudoku}
%% https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
%% hardest Sudoku: AI14473 @ http://aisudoku.com, only 21 hints:
|*8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4|*3|*6| 8| 2| 1| 7| 5|.
| 6|*7| 5| 4|*9| 1|*2| 8| 3|.
| 1|*5| 4| 2| 3|*7| 8| 9| 6|.
| 3| 6| 9| 8|*4|*5|*7| 2| 1|.
| 2| 8| 7|*1| 6| 9| 5|*3| 4|.
| 5| 2|*1| 9| 7| 4| 3|*6|*8|.
| 4| 3|*8|*5| 2| 6| 9|*1| 7|.
| 7|*9| 6| 3| 1| 8|*4| 5| 2|.
\end{Sudoku}
~~~~~
\PuzzleSolution
\begin{Sudoku}
| 8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4| 3| 6| 8| 2| 1| 7| 5|.
| 6| 7| 5| 4| 9| 1| 2| 8| 3|.
| 1| 5| 4| 2| 3| 7| 8| 9| 6|.
| 3| 6| 9| 8| 4| 5| 7| 2| 1|.
| 2| 8| 7| 1| 6| 9| 5| 3| 4|.
| 5| 2| 1| 9| 7| 4| 3| 6| 8|.
| 4| 3| 8| 5| 2| 6| 9| 1| 7|.
| 7| 9| 6| 3| 1| 8| 4| 5| 2|.
\end{Sudoku}
\end{center}
\begin{example}[Sudoku CP model]
\footnotesize
\vspace{-1em}
\begin{align}
&G_{ij} \in \{1, 2, \dots, 9\}, \quad &&\forall i, j \in \{1, 2, \dots, 9\} \\ %\text{where } x_{i,j} \text{ represents the value in the cell at row } i \text{ and column } j \\
&\cons{AllDifferent}{G_{i1}, G_{i2}, \dots, G_{i9}}, \quad &&\forall i \in \{1, 2, \dots, 9\} \\ %&\text{For each row } i, \quad \text{the values are all different:} \\
&\cons{AllDifferent}{G_{1j}, G_{2j}, \dots, G_{9j}}, \quad &&\forall j \in \{1, 2, \dots, 9\} \\
&\cons{AllDifferent}{G_{p, q}, ~~~G_{p, q+1}, ~~~G_{p, q+2}}, && \notag \\
&\qquad \quad \quad \quad ~G_{p+1, q}, G_{p+1, q+1}, G_{p+1, q+2}, && \notag \\
&\qquad \quad \quad \quad ~G_{p+2, q}, G_{p+2, q+1}, G_{p+2, q+2}), \quad &&\forall p, q \in \{1, 4, 7\} \\
&G_{ij} = \mbox{given}_{ij}, \quad &&\text{if a value } \mbox{given}_{ij} \text{ is given}
\end{align}
\end{example}
\end{frame}
% THIS IS NOT A FLASHCARD slide, we always show 1 CPMpy and 1 MiniZinc model here
\begin{frame}[fragile]
\begin{center}
\vspace{2mm}
\PuzzleUnitlength=11pt
\renewcommand*\SudokuLinethickness{1pt}
\renewcommand*\PuzzleFont{\sf\tiny}
\begin{Sudoku}
%% https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
%% hardest Sudoku: AI14473 @ http://aisudoku.com, only 21 hints:
|*8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4|*3|*6| 8| 2| 1| 7| 5|.
| 6|*7| 5| 4|*9| 1|*2| 8| 3|.
| 1|*5| 4| 2| 3|*7| 8| 9| 6|.
| 3| 6| 9| 8|*4|*5|*7| 2| 1|.
| 2| 8| 7|*1| 6| 9| 5|*3| 4|.
| 5| 2|*1| 9| 7| 4| 3|*6|*8|.
| 4| 3|*8|*5| 2| 6| 9|*1| 7|.
| 7|*9| 6| 3| 1| 8|*4| 5| 2|.
\end{Sudoku}
~~~~~
\PuzzleSolution
\begin{Sudoku}
| 8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4| 3| 6| 8| 2| 1| 7| 5|.
| 6| 7| 5| 4| 9| 1| 2| 8| 3|.
| 1| 5| 4| 2| 3| 7| 8| 9| 6|.
| 3| 6| 9| 8| 4| 5| 7| 2| 1|.
| 2| 8| 7| 1| 6| 9| 5| 3| 4|.
| 5| 2| 1| 9| 7| 4| 3| 6| 8|.
| 4| 3| 8| 5| 2| 6| 9| 1| 7|.
| 7| 9| 6| 3| 1| 8| 4| 5| 2|.
\end{Sudoku}
\end{center}
\begin{example}[Sudoku in CPMpy (indexing offset 0)]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=17,lastline=27]{models_cpmpy/T01_sudoku.py}
\vspace{-0.5em}
\end{example}
\end{frame}
% THIS IS NOT A FLASHCARD slide, we always show 1 CPMpy and MiniZinc model here
\begin{frame}[fragile]
\begin{center}
\vspace{2mm}
\PuzzleUnitlength=11pt
\renewcommand*\SudokuLinethickness{1pt}
\renewcommand*\PuzzleFont{\sf\tiny}
\begin{Sudoku}
%% https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
%% hardest Sudoku: AI14473 @ http://aisudoku.com, only 21 hints:
|*8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4|*3|*6| 8| 2| 1| 7| 5|.
| 6|*7| 5| 4|*9| 1|*2| 8| 3|.
| 1|*5| 4| 2| 3|*7| 8| 9| 6|.
| 3| 6| 9| 8|*4|*5|*7| 2| 1|.
| 2| 8| 7|*1| 6| 9| 5|*3| 4|.
| 5| 2|*1| 9| 7| 4| 3|*6|*8|.
| 4| 3|*8|*5| 2| 6| 9|*1| 7|.
| 7|*9| 6| 3| 1| 8|*4| 5| 2|.
\end{Sudoku}
~~~~~
\PuzzleSolution
\begin{Sudoku}
| 8| 1| 2| 7| 5| 3| 6| 4| 9|.
| 9| 4| 3| 6| 8| 2| 1| 7| 5|.
| 6| 7| 5| 4| 9| 1| 2| 8| 3|.
| 1| 5| 4| 2| 3| 7| 8| 9| 6|.
| 3| 6| 9| 8| 4| 5| 7| 2| 1|.
| 2| 8| 7| 1| 6| 9| 5| 3| 4|.
| 5| 2| 1| 9| 7| 4| 3| 6| 8|.
| 4| 3| 8| 5| 2| 6| 9| 1| 7|.
| 7| 9| 6| 3| 1| 8| 4| 5| 2|.
\end{Sudoku}
\end{center}
\vspace{-2mm}
\begin{example}[Sudoku in MiniZinc (indexing offset 1)] \footnotesize
\lstinputlisting[language=Mzn]{models_minizinc/sudoku-GoogleTranslate.mzn}
\end{example}
\end{frame}
\begin{frame}
\begin{example}[Agricultural experiment design, AED]
\begin{table} \small
\begin{tabular}{r|c|c|c|c|c|c|c|}
\multicolumn{1}{r}{} & \multicolumn{1}{c}{\BlkOne}
& \multicolumn{1}{c}{\BlkTwo} & \multicolumn{1}{c}{\BlkThree}
& \multicolumn{1}{c}{\BlkFour} & \multicolumn{1}{c}{\BlkFive}
& \multicolumn{1}{c}{\BlkSix} & \multicolumn{1}{c}{\BlkSeven} \\
\cline{2-8}
\VarOne &\alt<2>{1}{\tick}&\alt<2>{1}{\tick}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{0}{--} \\ \cline{2-8}
\VarTwo &\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--} \\ \cline{2-8}
\VarThree &\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{1}{\tick} \\ \cline{2-8}
\VarFour &\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--} \\ \cline{2-8}
\VarFive &\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{1}{\tick} \\ \cline{2-8}
\VarSix &\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick} \\ \cline{2-8}
\VarSeven &\alt<2>{0}{--}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{0}{--}&\alt<2>{1}{\tick}&\alt<2>{1}{\tick}&\alt<2>{0}{--} \\
\cline{2-8}
\end{tabular}
\end{table}
\textbf{Constraints} to be \textbf{satisfied}:
\begin{enumerate}
\item Equal \BlkSem\ load: Every \Block\ \BlkVar\ 3 \Variety s.
\item Equal \VarSem\ size: Every \Variety\ is \VarBlk\ 3 \Block s.
\item Balance: Every \Variety\ pair is \VarBlk\ 1 common \Block.
\end{enumerate}
\textbf{Instance}: $7$ \Block s, $7$ \Variety s, $3$ \Variety
s/\Block, $3$ \Block s/\Variety, balance~$1$.
\end{example}
\alt<2>{General problem: \defined{balanced incomplete block design} (\defined{BIBD})}{}
\end{frame}
\begin{frame}
In a BIBD, the plots are called \defined{blocks} and the \Variety s are called \defined{varieties}:
\begin{example}[BIBD \emph{integer} CP model]
\footnotesize
\vspace{-1em}
\begin{align}
&v = 7, \quad b = 7, &&\mbox{Varieties, Blocks}\\
&k = 3, \quad r = 3, \quad \lambda = 1, &&\mbox{sample size, block size, balance}\\
\\
&B_{ij} \in \{0, 1\}, \quad &&\forall i \in \{1, 2, \dots, v\}, \, \forall j \in \{1, 2, \dots, b\}, \\
&\sum_{j=1}^b B_{ij} = k, \quad &&\forall i \in \{1, 2, \dots, v\}, \mbox{every row must add up to sample size}\\
&\sum_{i=1}^v B_{ij} = r, \quad &&\forall j \in \{1, 2, \dots, b\}, \mbox{every columns must add up to block size}\\
&\sum_{j=1}^b B_{ij} B_{i'j} = \lambda, \quad &&\forall i, i' \in \{1, 2, \dots, v\}, \, i \neq i'. \mbox{every distinct row, scalar product = balance}
\end{align}
\end{example}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}
In a BIBD, the plots are called \defined{blocks} and the \Variety s are called \defined{varieties}:
\begin{example}[BIBD \emph{integer} model in CPMpy]
\lstinputlisting[language=cpmpy,firstline=4,lastline=19]{models_cpmpy/T01_bibd.py}
\end{example}
\end{frame}
\end{flashcardcpmpy}
\begin{flashcardminizinc}
\begin{frame}[fragile]
In a BIBD, the plots are called \defined{blocks} and the \Variety s are called \defined{varieties}:
\begin{example}[BIBD \emph{integer} model \Link{BIBD-int-sum.mzn}:~
$\text{\tick} \rightsquigarrow 1$ ~and~
$\text{--} \rightsquigarrow 0$]
\footnotesize
\lstinputlisting[language=Mzn,firstnumber=-3,firstline=2]{models_cpmpy/bibd-int-sum.mzn}
\end{example}
\begin{example}[Instance data for our AED \Link{BIBD-AED.dzn}]
\footnotesize
\lstinputlisting[language=Mzn,firstnumber=-3]{models_cpmpy/bibd-aed.dzn}
\end{example}
\end{frame}
\begin{frame}[fragile]
Using the \mzninline{count} abstraction instead of \mzninline{sum}:
\begin{example}[BIBD \emph{integer} model in CPMpy]
\begin{lstlisting}[language=cpmpy]
BIBD = boolvar(shape=(varieties, blocks),name="matrix")
model = Model(
# every row adds up to sampleSize
[Count(row,1) == sampleSize for row in BIBD],
# every column adds up to the blocksize.
[Count(col,1)) == blocksize for col in BIBD.T],
# the scalar product of every pair of distinct rows sums up to balance
[Count(row_i*row_j, 1) == balance for row_i, row_j in all_pairs(BIBD)]
model.solve() \end{lstlisting}
\end{example}
\end{frame}
\end{flashcardminizinc}
\begin{flashcardcpmpy}
\begin{frame}[fragile]
Reconsider the model fragment:
{\footnotesize
\begin{lstlisting}[language=cpmpy,firstnumber=4]
[cp.sum(row) == sampleSize for row in BIBD]\end{lstlisting}
}
This constraint is \alert{declarative}, \\
so read it using only the verb ``must be'' or similar: \vfill
\begin{quote}
for each row \texttt{{row}} of \texttt{{BIBD}}, \\
the sum of values in row \texttt{{row}} \\
must be equal to \texttt{{sampleSize}}
\end{quote}\vfill
The constraint is \alert{NOT procedural}: \vfill
\begin{quote}
for each row \texttt{{row}} of \texttt{{BIBD}}, \\
we first sum the values in row \texttt{{row}}\\
and then check if that count equals \texttt{{sampleSize}}\texttt{~-- !WRONG!}
\end{quote}\vfill
The latter reading is appropriate for solution \alert{checking}, \\
but solution \alert{finding} performs no such procedural counting.
\end{frame}
\begin{frame}[fragile]{Symbolic model creation in Python}
Declarative programming in a procedural language???
{\footnotesize
\begin{lstlisting}[language=cpmpy,firstnumber=4]
[cp.sum(row) == sampleSize for row in BIBD]\end{lstlisting}
}
\alert{Yes}, this is a declarative specification: the sum and comparison
are not \textit{executed}, instead they \textit{create objects}!