-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL02-BasicModeling.tex
More file actions
2277 lines (1995 loc) · 84.2 KB
/
L02-BasicModeling.tex
File metadata and controls
2277 lines (1995 loc) · 84.2 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}{L02: Basic Modeling} %: variables, constraints (&functions) and objectives}
\begin{center}
~ \\
\includegraphics[height=42mm]{images/beer_taste_prob.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{Recap: combinatorial problem to solution}
\begin{frame}{Combinatorial Optimisation}
\begin{center}
\begin{tabular}{c c} % 2-column grid for images
\includegraphics[height=40mm]{images/CO_examples} &
\includegraphics[width=20mm]{images/physicians} \includegraphics[height=20mm]{images/sectorLondon} \\
\includegraphics[height=20mm]{images/genotype-haplotype} &
\includegraphics[height=20mm]{images/burningLaptop}
\end{tabular}
\end{center}
\end{frame}
\begin{frame}{Model-and-Solve}
\centering
\includegraphics[height=68mm]{images/prob2sol}
\end{frame}
\begin{frame}{Modelling (declarative) vs Programming (imperative)}
\begin{center}
\begin{tikzpicture}
[->,>=stealth',level/.style={level distance=20mm,sibling distance=50mm}]
\tikzset{nod/.style = {font=\bfseries}}
\node[nod, align=center](problem){Problem \\ \normalfont\textit{(multi-eyed beast)}}
child{
node[nod, align=center](def){Problem Definition \\ \normalfont\textit{(free form, unambiguous)}}
child{
node[nod, align=center]{Model \\ \normalfont\textit{(formal specification)}}
child{
node[nod]{program}
edge from parent node[left] {automatic!}
}
edge from parent node[above left, yshift=-8pt] {\underline{what?} }
}
child{
node[nod, align=center]{Algorithm \\ \normalfont\textit{(pseudo-code)}}
child{
node[nod]{program}
edge from parent node[right] {manual!}
}
edge from parent node[above right, yshift=-8pt] {\underline{how?} }
}
};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}%{An example problem definition \& model}
\begin{example}[Problem Definition: Sudoku]
The goal of Sudoku is to \textit{complete} a partially filled 9x9 grid with numbers so that each row, column and 3x3 section contains each digit between 1 and 9 once.
\end{example}
\begin{example}[Model: Sudoku]
\vspace{-1em}
\begin{align*}
&G_{ij} \in \{1, 2, \ldots, 9\} & \quad\quad &\forall i, j \in \{1, \ldots, 9\} \\
&\cons{AllDifferent}{\{G_{ij} | j \in \{1, \ldots, 9\}\}} & \quad\quad & \forall i \in \{1, \ldots, 9\} \\
&\cons{AllDifferent}{\{G_{ij} | i \in \{1, \ldots, 9\}\}} & \quad\quad & \forall j \in \{1, \ldots, 9\} \\
&\cons{AllDifferent}{\{G_{kl} | k \in \{i, \ldots, i+2\}, l \in \{j, \ldots, j+2\}\}} & \quad\quad & \forall i,j \in \{1, 4, 7\} \\
&G_{ij} = v & \quad\quad & \forall (i,j,v) \in {\cal D}
\end{align*}
\end{example}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}%{An example problem definition \& model}
\begin{example}[Problem Definition: Sudoku]
The goal of Sudoku is to \textit{complete} a partially filled 9x9 grid with numbers so that each row, column and 3x3 section contains each digit between 1 and 9 once.
\end{example}
\begin{example}[Model: Sudoku in CPMpy]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=17,lastline=27]{models_cpmpy/T01_sudoku.py}
\vspace{-0.5em}
\end{example}
\end{frame}
\end{flashcardcpmpy}
\begin{frame}
\frametitle{From Model to Model to Solver}
\begin{tikzpicture}[scale=0.8, 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{frame}
\begin{frame}[fragile]{High-level vs low-level modelling languages}
High-level modeling languages (CPMpy, MiniZinc, Essence)
\begin{itemize}
\item Model = list of \textit{complex} expressions over decision variables
\item Boolean logic example: (symbols are explained later) \\
$(a \leftrightarrow (b \vee (c \wedge d))) \wedge (e \vee \neg f)$
\end{itemize} \vfill
Low-level modeling language (SAT, ILP, CP, ...)
\begin{itemize}
\item Model = list of \textit{atomic} constraints over decision variables
\item SAT: CNF Example: $(a \vee \neg b) \wedge (a \vee \neg n) \wedge (\neg a \vee b \vee n) \wedge (n \vee \neg c \vee \neg d) \wedge (\neg n \vee c) \wedge (\neg n \vee d) \wedge (e \vee \neg f)$
\end{itemize} \vfill
Typically, high-level languages can translate to multiple low-level languages \textit{(solver-agnostic)}.
\vfill
This course uses 1 high-level language, all ideas translate to other languages and to low-level languages.
\end{frame}
\begin{frame}{So what does solving do?}
For a CP solver, a model = list of \textit{atomic} constraints over decision variables.
\begin{center}
\includegraphics[height=48mm, trim=0 0 0 50mm, clip]{images/constraint_store} % cut top title
\end{center}
It will iterate over the constraints and try to reduce domains (=propagation) \\
and branch over variables if there is nothing left to reduce (=search)
\end{frame}
\begin{frame}{Branching induces a search tree}
\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) {$X$};
\coordinate (B) at (-7.4,-1.4);
\node[var] at (B) {$Y$};
\coordinate (C) at (-7.4,-2.1);
\node[var] at (C) {$A$};
\coordinate (D) at (-7.4,-2.8);
\node[var] at (D) {$B$};
\coordinate (E) at (-7.4,-3.8);
\node[var] at (E) {$C$};
\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]{0} % X=0
child{node[val]{0} % Y=0
child{node[val]{0} % A=0
child{node[val]{0} % B=0
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
child{node[val]{1} % B=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
}
child[pruned]{node[pruned]{1} % A=1
child[pruned]{node[pruned]{0} % B=0
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]{1} % B=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{node[val]{1} % Y=1
child{node[val]{0} % A=0
child{node[val]{0} % B=0
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
child{node[val]{1} % B=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
}
child[pruned]{node[pruned]{1} % A=1
child[pruned]{node[pruned]{0} % B=0
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=2
}
child[pruned]{node[pruned]{1} % B=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=2
}
}
}
}
child{node[val]{1} % X=1
child{node[val]{0} % Y=0
child{node[val]{0} % A=0
child{node[val]{0} % B=0
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
child{node[val]{1} % B=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
}
child{node[val]{1} % A=1
child{node[val]{0} % B=0
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
child{node[val]{1} % B=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
}
}
child{node[val]{1} % Y=1
child{node[val]{0} % A=0
child{node[val]{0} % B=0
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
child{node[val]{1} % B=1
child{node[val]{1}
child{node[val]{...}}
child{node[val]{...}}
} % C=1
child{node[val]{2}
child{node[val]{...}}
child{node[val]{...}}
} % C=2
}
}
child[pruned]{node[pruned]{1} % A=1
child[pruned]{node[pruned]{0} % B=0
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=2
}
child[pruned]{node[pruned]{1} % B=1
child[pruned]{node[pruned]{1}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=1
child[pruned]{node[pruned]{2}
child[pruned]{node[pruned]{...}}
child[pruned]{node[pruned]{...}}
} % C=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}
\section{Modeling basics}
\subsection{Basic example}
% Its just a knapsack problem, but grounded in Belgium's beer culture,
% which should be drank responsibly (as the example demonstrates)
\begin{frame}{Belgian Beer Tasting problem}
$ $\\
Responsible drinking:\\
What beers to try, so that you can still pay attention in class tomorrow?
\begin{center}
\includegraphics[height=62mm, trim=0 100mm 0 0, clip]{images/beer_taste_prob.png}
\end{center}
\end{frame}
\begin{frame}
\centering
\includegraphics[height=82mm]{images/beer_guide.png}
\end{frame}
\begin{frame}{Belgian Beer Tasting problem}
\begin{columns}
\begin{column}{0.35\textwidth}
What beers to try, so that you can still pay attention in class tomorrow? \\[2em]
\textbf{Model =} \\[1em]
- Variables, with a domain \\[1em]
- Constraints over variables \\[1em]
- Optionally: an objective \\[2em]
\textbf{Model.solve()} \\[2em]
\end{column}
\begin{column}{0.65\textwidth}
{\vspace{-2.5em}
\includegraphics[height=25mm, trim=0 100mm 0 30mm, clip]{images/beer_taste_prob.png}}
\textcolor{green}{st}, \textcolor{orange}{du}, \textcolor{purple}{vi}, \textcolor{brown}{tk}, \textcolor{gray}{gc}, \textcolor{red}{kl} $\in$ \{0,1\} \\[1em]
{\small 52*\textcolor{green}{st} + 85*\textcolor{orange}{du} + 60*\textcolor{purple}{vi} + 84*\textcolor{brown}{tk} + 117*\textcolor{gray}{gc} + 35*\textcolor{red}{kl} $\leq$ 4*52} \\[1em]
{\small maximize(50*\textcolor{green}{st} + 80*\textcolor{orange}{du} + 75*\textcolor{purple}{vi} + 82*\textcolor{brown}{tk} + 95*\textcolor{gray}{gc} + 70*\textcolor{red}{kl})}
\end{column}
\end{columns}
\end{frame}
\begin{flashcardcpmpy}
\begin{frame}[fragile]{Belgian Beer Tasting problem, CPMpy}
\begin{columns}
\begin{column}{0.35\textwidth}
What beers to try, so that you can still pay attention in class tomorrow? \\[2em]
\textbf{Model =} \\[1em]
- Variables, with a domain \\[1em]
- Constraints over variables \\[1em]
- Optionally: an objective \\[2em]
\textbf{Model.solve()} \\[2em]
\end{column}
\begin{column}{0.65\textwidth}
$ $\\[5.2em]
\lstinputlisting[language=cpmpy, firstline=1, lastline=11]{models_cpmpy/T02_beer_tasting.py}
\end{column}
\end{columns}
\end{frame}
\end{flashcardcpmpy}
\subsection{Decision variables}
\begin{frame}{Decision variables}
In this course, we only consider \defined{discrete decision variables}, namely Boolean and integer decision variables:
\begin{align}
b &\in \{0,1\} \\
x &\in \{1, \ldots, 10\}
\end{align}
\vspace{1em}
Variables have a \defined{domain}, a finite set of allowed values:
\begin{itemize}
\item for {Boolean} variables, this is always $\{0,1\}$
\item for {integer} variables, this is specified as two parameters: \\
\textit{lb}=lower bound, \textit{ub}=upper bound, domain: $\{lb..ub\}$
\end{itemize}
\vspace{1em}
Sometimes, you want to create a variable with a \defined{sparse domain}: $x \in \{1,2,5,8,9\}$
\vspace{1em}
Some modeling languages support other variables (floats, sets, strings, bitvectors)
\end{frame}
\subsection{Logical constraints}
\begin{frame}{Logical constraints}
$ $\\
Logical constraints involve Boolean operators over Boolean expressions
$ $\\
Boolean operators:
$ $\\
\begin{itemize}
\item negation: $\neg a$ \quad(in text sometimes $-a$)
\item or: $a \vee b$ \quad(in text sometimes written as $a\mid b$)
\item and: $a \wedge b$ \quad(in text sometimes written as $a~\&~b$)
\item equivalence: $a \leftrightarrow b$ \quad(also called \defined{reification} or double-implication)
\item implication: $a \rightarrow b$ \quad(also called \defined{half-reification})
\end{itemize}
$ $\\
Each operator has its own \textit{truth table}: a/b values that make the expression true or false.
\end{frame}
\begin{frame}{Logical constraints}
Each operator has its own \textit{truth table}: a/b values that make the expression true or false.
$ $\\
The one many people find tricky is the one of \textbf{implication}, called \defined{material implication} in logic:
{%\footnotesize
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
$a$ & $b$ & $a \rightarrow b$ \\
\hline
T & T & T \\
T & F & F \\
F & T & T \\
F & F & T \\
\hline
\end{tabular}
\end{center}
}
$ $\\
Verify: $a \rightarrow b$ is equivalent to $\neg a \vee b$\\
Also: $a = b$ is equivalent to $(a \rightarrow b) \wedge (b \rightarrow a)$
\end{frame}
\begin{frame}{Logical constraints}
$ $\\
Boolean quantifiers:
$ $\\
\begin{itemize}
\item universal quantification: $\forall x \in X, (x \rightarrow y)$
\item existential quantification: $\exists x \in X, (x \wedge y)$
\end{itemize}
$ $\\
Other Boolean operator:
\begin{itemize}
\item exclusive-or: $a \otimes b$ \quad(in text sometimes $a ~\mbox{xor}~ b$)
\end{itemize}
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
\( a \) & \( b \) & \( a \otimes b \) \\
\hline
T & T & F \\
T & F & T \\
F & T & T \\
F & F & F \\
\hline
\end{tabular}
\end{center}
$ $\\
To think about: $a \otimes b$ is equivalent to $(a \vee b) \wedge (\neg a \vee \neg b)$, what about $\mbox{XOR}(a,b,c)$?
\end{frame}
\subsection{Simple comparison constraints}
\begin{frame}{Simple comparison constraints}
$ $\\
Simple comparisons ($=, \neq, <, \leq, >, \geq)$ of an integer variable:
\begin{align}
x &= 3 \\
y &\neq 6 \\
z & < 2
\end{align}
$ $\\
A simple comparison is a Boolean-valued expression, it can be used inside Boolean operators:
\begin{align}
&(x = 3) \wedge (y > 5) \\
&(y \neq 6) \rightarrow (a \vee (z < 2))
\end{align}
Good use of brackets () avoids ambiguity.
\end{frame}
\subsection{Arithmetic constraints}
\begin{frame}{Arithmetic constraints}
Arithmetic constraints combine arithmetic operations $(+,-,*,/)$ over integers with a comparisons ($=, \neq, <, \leq, >, \geq)$:
\begin{align}
x + y &= 3 \\
y - z &\neq 6 \\
2*z - (x*y + y) & < 2
\end{align}
$ $\\
Integer division $x/y == 5$ is tricky because it is undefined for $y=0$. It is a \defined{partial function}. Some languages/systems simply forbid a division where the nominator has $0$ in its domain.
(there are more peculiarities with integer division, such as using \textit{floor division} or \textit{rounding division} for negative numbers...)
\end{frame}
\begin{frame}{Arithmetic constraints}
Linear constraints only involve arithmetic operations $(+, -)$ and a multiplication of a variable with a constant. \defined{Linear inequalities} further only use the comparisons $(<, \leq, >, \geq)$:
\begin{align}
x + y &\geq 3 \\
y - z &< 6 + x
\end{align}
$ $\\
All integer linear inequalities can be rewritten to a normal form $a_1x_1 + \ldots + a_n x_n \leq b$
$ $\\
A linear equality $x + y = 5$ can be rewritten as $(x + y \leq 5) \wedge (x + y \geq 5)$
$ $\\
A linear dis-equality $x + y \neq 5$ leads to a disjunction: $(x + y < 5) \vee (x + y > 5)$ and cannot be rewritten to a conjunction of inequalities without adding a new variable...
\end{frame}
\begin{frame}{Arithmetic constraints}
Many other arithmetic operations exist:
\begin{itemize}
\item absolute value $|a|$
\item modulo $x \% y$
\item minimum $min(x,y,z)$
\item maximum $max(x,y,z)$
\end{itemize}
$ $\\
These can be used in arithmetic constraints too (e.g. arithmetic operators + comparison).
$ $\\
Just like simple constraints, arithmetic constraints are Boolean-valued expressions too; and can be used in Boolean expressions.
$ $\\
A \defined{nested expression} nests all sorts of Boolean operators and/or arithmetic constraints and operators. A contrived example: $(a \vee (|x - y| > z/2)) \rightarrow (r * s \neq max(x, y-t, |z-3|)) \wedge (b \otimes (c \leftrightarrow d))$
\end{frame}
\subsection{Global constraints}
\begin{frame}{Global constraints}
$ $\\
Many other constraints and operations that do not exist in standard mathematics can be defined.
In the constraint programming community, such constraints are called \defined{global constraints}:
\begin{itemize}
\item AllDifferent$(x, y, z)$
\item Table$([x,y], [[1,2],[1,4],[3,4]])$
\item Count$(X, 3) == z$
\item ...
\end{itemize}
In some systems, the concept 'Count$(X, 3) == z$' is modelled with a predicate 'Count$(X, 3, z)$'.
In this course, we call the 'Count$(X, 3)$' part a \defined{global function}, the integer-valued counter-part of a global constraint; such that it can be nested with arithmetic operators, e.g. $10*(Count(X, 3) - Count(Y, 3))$
\end{frame}
\subsection{Objective functions}
\begin{frame}{Objective functions}
$ $\\
The \defined{objective function} is an integer-valued expression that must be minimized or maximized.
$ $\\
\textit{Global functions} are valid integer-valued expressions too, as are nested arithmetic expressions (in high-level languages at least).
$ $\\
Sometimes, we want to relax a hard constraint by allowing it to be violated, but penalizing that \textbf{violation in the objective}.
When we add a constraint (Boolean-valued expression) to the objective function, we call that constraint a \defined{soft constraint}, e.g. $\Iverson{z == 0}$ below:
\vspace{-1em}
{\footnotesize
\begin{align}
\mbox{maximize} \quad&10*x + 3*y + \Iverson{z == 0} \\
\mbox{s.t.} \quad &x + y < 10, \\
&x + y + z > 5, \\
&x, y, z \in \{0..10\}
\end{align}
}
\end{frame}
% THIS ENTIRE PART IS CPMPY SPECIFIC, and hence in flashcard environment
% We will repeat each of the previous subsections, but with language syntax details
\begin{flashcardcpmpy}
\section{Modelling with the CPMpy library}
\begin{frame}
\begin{columns}[T] % align columns at the top
\begin{column}{0.5\textwidth} % left column
\vspace*{5em}
Using CPMpy includes the following:
\begin{itemize}
\item Import and model creation
\item Decision variables
\item Constraints
\item Objective function (optional)
\item Solving
\item Printing output
\end{itemize}
\end{column}
\begin{column}{0.5\textwidth} % right column
\begin{example}[Showcase]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy]{models_cpmpy/T02_showcase.py}
\end{example}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Install, import, create model}
Single page, all you need documentation:
\url{https://cpmpy.readthedocs.io/en/latest/modeling.html}
\vspace{2em}
Installing:
\lstinline[basicstyle=\scriptsize\ttfamily]$pip install cpmpy$
\vspace{2em}
Importing and model creation:
\lstinputlisting[language=cpmpy,firstline=1,lastline=2]{models_cpmpy/T02_showcase.py}
\vspace{2em}
\footnotesize
You can also \lstinline[language=cpmpy]{from cpmpy import *} which will override any/all/min/max/sum for convenience but this can be confusing for novices (e.g. when does \cpminline{sum} compute a value or create an expression).
\end{frame}
\begin{frame}{Decision variables}
CPMpy supports \defined{discrete decision variables}, namely Boolean and integer decision variables:
\lstinputlisting[language=cpmpy,firstline=3,lastline=4]{models_cpmpy/T02_decisvars.py}
\vspace{1em}
Variables have a \defined{domain}, a finite set of allowed values:
\begin{itemize}
\item for {Boolean} variables, this is implicitely $\{0,1\}$
\item for {integer} variables, this is specified as the first two parameters: \\
\textit{lb}=lower bound, \textit{ub}=upper bound, domain: $\{lb..ub\}$
\end{itemize}
\vspace{1em}
If you want a \defined{sparse domain}, containing only a few values, you can:
\begin{itemize}
\item Add constraints to forbid specific values, e.g. \lstinline[language=cpmpy]{x != 3, x != 5, x != 7}
\item Or use the shorthand \textit{InDomain} global constraint: \lstinline[language=cpmpy]{cp.InDomain(x, [1,2,4,6,8,9])}
\end{itemize}
\end{frame}
\begin{frame}{Decision variables 2/2}
Decision variables have a unique \defined{name}. You can set it yourself, otherwise a unique name will automatically be assigned to it. If you print decision variables, \lstinline[language=cpmpy]{print(b, x)}, it will print the name.
\vspace{1em}
CPMpy creates \emph{n-dimensional} \defined{NumPy arrays} when creating variables!
This is very convenient for Numpy-style \emph{vectorized} operations and for integration with machine learning libraries.
\vspace{1em}
The \emph{shape} argument allows you to specify the dimensions of the array. All variables will have the same initial domain:
\lstinputlisting[language=cpmpy,firstline=6,lastline=11]{models_cpmpy/T02_decisvars.py}
\end{frame}
\begin{frame}{Note: Numpy indexing}
Python creates lists of lists, each requiring an index:
\lstinputlisting[language=cpmpy,firstline=4,lastline=6]{models_cpmpy/T02_indexing.py}
Numpy creates an array, which allows indexing with a tuple:
\lstinputlisting[language=cpmpy,firstline=8,lastline=9]{models_cpmpy/T02_indexing.py}
Also accepts `:` which means this entire dimension:
\lstinputlisting[language=cpmpy,firstline=11,lastline=11]{models_cpmpy/T02_indexing.py}
Or using an equal sized array of Booleans (also called a 'selector'):
\lstinputlisting[language=cpmpy,firstline=13,lastline=15]{models_cpmpy/T02_indexing.py}
\end{frame}
\begin{frame}{Advanced: Vectorized operations}
Because decision variables are NumPy arrays in CPMpy, you can also do \defined{vectorized operations} on them: an operation on two equal sized arrays will create an (equal sized) array of element-wise operations:
\begin{example}[vectorized operations]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=3,lastline=6]{models_cpmpy/T02_vectorized.py}
\vspace{-0.5em}
\end{example}
\defined{Broadcasting} in NumPy allows operations between arrays of different shapes, by automatically expanding the smaller array along its dimensions to match the larger array's shape: %, enabling element-wise operations without the need for explicit replication of data.
\begin{example}[broadcasting]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=8,lastline=8]{models_cpmpy/T02_vectorized.py}
\vspace{-0.5em}
\end{example}
\end{frame}
\begin{frame}{Expressing constraints}
A \defined{constraint} is an expression that is added to a model, the solver will enforce it to always be true, e.g.:
\lstinputlisting[language=cpmpy,firstline=9,lastline=11]{models_cpmpy/T02_showcase.py}
The \cpminline{m += O} is Python syntactic sugar for \cpminline{m.__add__(O)}.
$ $\\
We will differentiate \emph{Boolean-valued} \defined{expressions} like \lstinline[language=cpmpy]{X[0] == 1} and \textit{integer-valued} expressions like \lstinline[language=cpmpy]{X[1] + X[2]}. Only Boolean-valued expressions can be added as a constraint.
\end{frame}
\begin{frame}{Common constraints}
\begin{example}[Typical logical constraints]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=5,lastline=10]{models_cpmpy/T02_constraints.py}
\vspace{-0.5em}
\end{example}
CPMpy overloads the Python bitwise operators \cpminline{&, |, ~}. They have precedence over all other operators, so \cpminline{a == 0 | b == 1} is \textbf{wrongly} interpreted as \cpminline{a == (0 | b) == 1} \texttt{-- WRONG!}. So make sure to \textbf{always write explicit brackets} to express \cpminline{(a == 0) | (b == 1)}.
\begin{example}[n-ary logical constraints]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=12,lastline=15]{models_cpmpy/T02_constraints.py}
\vspace{-0.5em}
\end{example}
\end{frame}
\begin{frame}{Common constraints 2/4}
%TODO: have it column-by-column?
\begin{example}[Typical comparison constraints]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=17,lastline=25]{models_cpmpy/T02_constraints.py}
\vspace{-0.5em}
\end{example}
You can not use a numeric expression as a Boolean expression, e.g. invalid: \cpminline{b | x} \texttt{-- WRONG!}, you need to add your intended meaning of truth: \cpminline{b | (x != 0)}
\end{frame}
\begin{frame}{Common constraints 3/4}
%TODO: first show math notation...
\begin{example}[Some arithmetic constraints]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=28,lastline=37]{models_cpmpy/T02_constraints.py}
\vspace{-0.5em}
\end{example}
You \textbf{can} use any Boolean expression as a numeric expression, e.g. valid: \cpminline{b + x > 2}
\end{frame}
\begin{frame}{Common constraints 4/4}
\begin{example}[Typical global constraints]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=40,lastline=48]{models_cpmpy/T02_constraints.py}
\vspace{-0.5em}
\end{example}
Many more global constraints and global functions exists! We will see them throughout the course.
\vspace{1em}
\begin{footnotesize}
Handy summary sheet: \url{https://cpmpy.readthedocs.io/en/latest/summary.html}
\end{footnotesize}
\end{frame}
\begin{frame}{Objective functions (optional)}
If a model has \textit{no objective function} specified, then it is a \defined{satisfaction problem}: the goal is to find out whether a solution, any solution, exists.
When an objective function is added it is an \defined{optimisation problem} and this function needs to be minimized or maximized.
\begin{example}[Objective function, from showcase example]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=13,lastline=14]{models_cpmpy/T02_showcase.py}
\end{example}
Any expression can be added as an objective function (\cpminline{maximize()} or \cpminline{minimize()})
\vspace{1em}
CPMpy does not support multi-objective optimisation yet: \\ multiple
objective functions must either be aggregated into a weighted sum, \\
or handled outside the model.
\end{frame}
\begin{frame}{Solving and printing output}
\begin{example}[Solving]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=3,lastline=7]{models_cpmpy/T02_solving.py}
\vspace{-0.5em}
\end{example}
solve() accepts arguments such as \cpminline{time_limit=}, \cpminline{solver=} and solver-specific ones
\begin{example}[Printing output]
\vspace{-0.5em}
\lstinputlisting[language=cpmpy,firstline=8]{models_cpmpy/T02_solving.py}
\vspace{-0.5em}
\end{example}
\end{frame}
\begin{frame}{Focus point: reification}
Reification enables the reasoning about the truth of a constraint \\
or a Boolean expression. \vfill
\begin{example}
constraint \cpminline{x < y}\\
requires that \cpminline{x} be smaller than \cpminline{y}. \vfill
constraint \cpminline{b == (x < y)}
requires that the Boolean variable \cpminline{b} takes the value
\cpminline{True} iff \cpminline{x} is smaller than \cpminline{y}:\\
the constraint \cpminline{x < y} is said to be \defined{reified},
and \cpminline{b} is called its \defined{reified variable}.
\end{example}\vfill
Reification is a powerful mechanism that enables: \vfill
\begin{itemize}
\item efficient reuse of logical components through their reified variable; \vfill
\item higher-level modelling (e.g. nested expressions, soft constraints)
\end{itemize}
\end{frame}
\begin{frame}
\begin{example}[Soft Constraints: \invisible{\alert{Weighted}} Alignment Photo Problem]
A set of students want to line up for a class photo. \\[+5pt]
Consider: \\
\cpminline{Wishes = [("Dimos", "Stella"), ("Marco", "Dimos"), ...]} \\
where each pair \textit{(who,whom)} denotes that student \textit{who} wants \\
to be next to student \textit{whom} on the photo.\\
Maximise the \invisible{\alert{weighted}} number of
granted wishes. \\[+5pt]
Let decision variable \cpminline{Pos[s]} denote the position in
\cpminline{0..len(Students)} \\ of student \cpminline{s} on the
photo. \\[+5pt]
The array \cpminline{Pos} must form a permutation of the
positions:\\
\cpminline{m = cp.Model( cp.AllDifferent(Pos) )}\vfill
The objective, formulated using nested expressions, is:
\\[+3pt]
\cpminline{m.maximize(cp.sum([ cp.abs(Pos[who] - Pos[whom]) == 1} \\
\quad\quad\quad\quad\quad\quad\quad\quad\quad \cpminline{for (who,whom) in Wishes ]))}
\end{example}
Constraint \cpminline{cp.abs(Pos[who] - Pos[whom]) == 1} will automatically be reified.
\end{frame}
\begin{frame}
\begin{example}[Soft Constraints: \alert{Weighted} Alignment Photo Problem]
A set of students want to line up for a class photo. \\[+5pt]
Consider: \\
\cpminline{Wishes = [("Dimos", "Stella", 2), ("Marco", "Dimos", 1), ...]} \\
where each pair \textit{(who,whom}\alert{\textit{,bid}}\textit{)} denotes that student \textit{who} wants \\