Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:dydaktyka:csp:lab2 [2017/03/13 00:28]
msl [Kolorowanie wierzchołków grafu]
en:dydaktyka:csp:lab2 [2020/03/13 13:48] (current)
msl
Line 1: Line 1:
-====== Constraint Programming:​ Basic Modelling ​Tenchniques ​=====+====== Constraint Programming:​ Basic Modelling ​Techniques ​=====
  
 ===== Symmetry Breaking =====  ===== Symmetry Breaking ===== 
  
-The problem is said to contain symmetry if there are exist classes of equivalent solutions --- solutions, which are called symmetrical because there exist a simple mechanical procedure to obtain one from another. Graph Coloring Problem has a very obvious symmetry --- in every solution we can freely swap colors, e.g. every red node repaint as blue, and every blue node repaint as red. Solutions of this kind aren't bad, just redundant, leading to much bigger search space. Symmetry breaking prunes the search space by removing symmetries from the problem.+The problem is said to contain symmetry if there exist classes of equivalent solutions --- solutions, which are called symmetrical because there exists ​a simple mechanical procedure to obtain one from another. Graph Coloring Problem has a very obvious symmetry --- in every solution we can freely swap colors, e.g. every red node repaint as blue, and every blue node repaint as red. Solutions of this kind aren't bad, just redundant, leading to much bigger search space. Symmetry breaking prunes the search space by removing symmetries from the problem
 + 
 +All files required to solve the assignments are available via [[https://​gitlab.com/​agh-krr/​2019-2020/​labs/​|the repository]],​ so clone it first.
  
 ==== Graph Coloring ==== ==== Graph Coloring ====
   * Problem: Same as [[http://​ai.ia.agh.edu.pl/​wiki/​en:​dydaktyka:​csp:​lab1#​graph_coloring|before]]   * Problem: Same as [[http://​ai.ia.agh.edu.pl/​wiki/​en:​dydaktyka:​csp:​lab1#​graph_coloring|before]]
   * Assignment:   * Assignment:
-    ​- Download and extract {{:​pl:​dydaktyka:​csp:​csp_coloring.zip|archive}}. +    - Look at and comprehend ''​lab3/​graph_coloring/​graph_coloring.mzn''​ model. ​    
-    ​- Look at and comprehend ''​csp_coloring.mzn''​ model. ​    +    - Try to solve the ''​basic_data.dzn''​ instance. 
-    - Try to solve the ''​csp_coloring_data.dzn''​ instance. +      * You can use the model created during previous classes 
-      * You can use model created during previous classes +      * There is a chance, that problem would too difficult to be solved in a reasonable time. 
-      * There is a chance, that problem would to difficult to be solved in a reasonable time. +    - File ''​data_with_clique.dzn''​ includes info about the largest clique in the graph
-    - File ''​csp_coloring_data2.dzn''​ includes info about the largest clique in the graph+
       * ''​minColorsNumber''​ - size of the largest clique       * ''​minColorsNumber''​ - size of the largest clique
       * ''​maxClique''​ - indexes of the vertices forming the largest clique       * ''​maxClique''​ - indexes of the vertices forming the largest clique
Line 19: Line 20:
     - Try to solve the problem again.     - Try to solve the problem again.
  
-==== Problem ​wielo-plecakowy ​==== +==== Multi-Knapsack ​Problem ==== 
-  * ProblemProblem plecakowy z kilkoma identycznymi plecakami zamiast jednego+  * DefinitionKnapsack problem with several identical knapsacks instead of one
-  * Należy+  * Assignment
-    - Zastanowić się, gdzie leży symetria w tym problemie+    - Find a symmetry in the problem
-    - Pobrać i rozpakować {{:​pl:​dydaktyka:​csp:​csp_n_knapsack.zip|}}  +    - Look at and comprehend ​''​lab3/​multi_knapsack/​multi_knapsack.mzn''​. 
-    - Przyjrzeć się modelowi ​''​csp_n_knapsack.mzn''​. +      * Note :!:: this model is not an example of good constraint ​model; ​it's just used to show a common technique to break symmetries 
-      * Uwagaten model nie jest zbyt dobryma jedynie pokazać prostą technikę łamania niektórych symetrii. +      * Note use of the ''​at_most'' ​global constraint
-      * Proszę zwrócić uwagę na zastosowane ograniczenie globalne ​''​at_most''​. +      * There is a defined predicate named ''​knapsack''​ -> it's the first example of user-defined predicate 
-      * Model zawiera zdefiniowany predykat ​''​knapsack''​ -> warto wiedzieć, że coś takiego typu można modelować w MiniZinc. Pozwala to na większą modularyzację kodu. +    - Run model with the associated data file
-    - Uruchomić ​model z dołączonym plikiem+      * The problem may be too hard for the solver 
-      * Problem może być za trudny do rozwiązania. +    - Break symmetry using [[https://​www.minizinc.org/​doc-2.4.2/en/​lib-globals.html#​lexicographic-constraints|''​lex_less_eq'' ​global constraint]]. 
-      * Gdyby solver ​zwracał wynik "​UNSATISFIABLE",​ proszę spróbować użyć solvera "G12 fd" w zakładce "​Configuration"​ +    - Run the new model with the same data file
-    - Dodać do kodu łamanie symetrii korzystająć z ograniczenia globalnego ​[[http://​www.minizinc.org/​2.0/doc-lib/doc-globals-lexicographic.html|''​lex_less''​]]. Pozwala ona na wymuszenie kolejności plecaków. Plecaki muszą być zawsze uporządkowe leksykograficznie ze względu na swoją zawartość+
-    - Uruchomić nowy model z tymi samymi danymi. +
-    - Czerpać zyski.  ​+
  
-===== Ograniczenia nadmiarowe ​=====+===== Redundant Constraints ​=====
  
-Jedną z podstawowych zasad programowania z ograniczeniami mówi, że ograniczenia ​są jak pieniądze --- nawet jeżeli jest ich wystarczającoprzydałoby się więcej. Załóżmy, że udało nam się opisać wszystkie ograniczenia konieczne ​do opisu zadanego problemu (tjspełnione tylko i wyłącznie przez rozwiązania prawidłowe oraz nie eliminujące żadnych rozwiązań prawidłowych);​ nie oznacza ​to wcależe powinniśmy na nich zaprzestać. Każde kolejne ograniczeniektóre dodaje jakąś informację,​ pozwala ​ +There is a good chance the problem can be defined in more than one way. Also you may find a set of constraints that is sufficient to define the problem. That'coolhowever there can exist so called "​redundant constraints";​ redundant because they do not have an impact on the number or quality of the solutionsThe only reason ​to include them in the model is that they may contain additional info about the structure of the problemtherefore giving solver an opportunity to prune the search space (most of the solver prune the search space by propagating constraintsa redundant constraint may boost this process). 
-na zmniejszenie przestrzeni przeszukiwania ​--- solver jest dzięki nim w stanie wcześniej "​odciąć"​ gałęzie przeszukiwania niedające nadziei na przyszłość.+==== Magic Sequence ===== 
 +  * Definition: Same as [[http://​ai.ia.agh.edu.pl/​wiki/​en:​dydaktyka:​csp:​lab1#​magic_sequence|before]] 
 +  * Assignment: 
 +    Look at and comprehend ''​lab3/​magic_series/​magic_series.mzn''​ 
 +    ​Add redundant constraints,​ hints: 
 +      * what should be equal the sum of the magic sequence? 
 +      * what should be equal the sum of sequence elements multiplied by their indexes (if indexing starts from 0)? 
 +    ​Compare solving time with and without the redundant constraints. 
 +    - Smile with satisfaction
  
-==== Magiczne ciągi =====+==== Channeling ​====
  
-Jeżeli ktoś nie zrobił tego wcześniejproszę przed przystąpieniem ​do ćwiczenia przynajmniej przeczytać opis problemu na stronie poprzedniego laboratorium.+If you have more than one model of the same problemyou can combine them into a single model. Why would one do that? Mostly because some constraints are easier to express with different variables. Other reason could be that the second model often makes a great example of the redundant constraints.
  
-  * Należy: +  * Problem[[http://​ai.ia.agh.edu.pl/wiki/en:​dydaktyka:​csp:​lab1#​n-queens|N-Queens]] again 
-    - Ściągnąć i zrozumieć model: {{:​pl:​dydaktyka:​csp:​csp_magic_series.zip|}} +  * Assignment: 
-    - Dodać do niego nadmiarowe ograniczeniaPodpowiedzi:​ +    - Look at and comprehend ''​lab3/​n_queens/​n_queens.mzn''​ 
-      * ile powinna wynosić suma magicznego ciągu? +    - Add another model of the problem 
-      * ile powinna wynosić suma elementów magicznego ciągupomnożonych przez numery swoich indeksów ​(indeksując od 0)? +      * try to use the boolean array of variables ''​array[1..n1..n] of var bool: qb;'' ​(queen boolean) 
-    - Porównać działanie modelu ​''​z'' ​''​bez'' ​dodatkowych ograniczeń na dłuższych ciągach. +      * add missing constraints so the second model was also independent ​ 
-    - Czerpać zyski. +    - Channel constraints from the both models: 
- +      * create constraint that connects variables from the model  
-==== Modele łączone ​n-hetmans return! ====+    - Compare running time of the normal and channeled model 
 +    - Add symmetry breaking to the problem by using ''​lex_lesseq'' ​constraint on the different permutations of the ''​qb'' ​array   
 +      * below the assignments there is a code listing with all permutations calculated in MiniZinc, can you tell what symmetries they represent? 
 +    - Compare running time again 
 +    Give yourself a [[https://​youtu.be/​kMUkzWO8viY|self-five]], <wrap lo>in this case, it may not improve the running time, but the technique itself is very useful in more complex problems</​wrap>​
  
-Jedną z technik dodawania dodatkowych ograniczeń jest wykorzystanie kilku modeli tego samego problemu równocześnie. Pomimo tego, że wszystkie te modele mogą wskazywać na te same rozwiązania,​ zawarte w nich ograniczenia w innej kolejności zawężają dziedziny zmiennych. Kombinacja kilku modeli pozwala na ograczenie przestrzeni przeszukiwania z kilku "​perspektyw"​ równocześnie. +<​code>​ 
- +array[int] of var boolqb0 = array1d(qb);​ 
-  * Problemponownie problem n-hetmanów. +array[int] of var boolqb1 = [ qb[j,​i] ​| i,j in 1..n ]; 
-  * Należy: +array[int] of var boolqb2 = [ qb[i,j] | i in reverse(1..n), j in 1..]; 
-    - Odkopać model n-hetmanów z poprzednich zajęć. Ewentualnie pobrać ten: {{:​pl:​dydaktyka:​csp:​csp_n_hetmans.zip|}} +array[int] of var bool: qb3 = [ qb[j,i] | in 1..n, j in reverse(1..n) ]; 
-    - Dopisać do niego drugą możliwość wyboru zmiennych decyzyjnych i ograniczeń +array[int] of var bool: qb4 [ qb[i,j] | i in 1..nj in reverse(1..n) ]; 
-      * Jeżeli ostatnio zmienne były indeksowane kolumnami ​wskazywały na numer wierszanależy dopisać model odwrotny. +array[int] of var bool: qb5 = [ qb[j,i] | i in reverse(1..n)j in 1..n ]; 
-    - Powiązać ze sobą ograniczeniami oba zestawy zmiennych+array[int] of var boolqb6 = [ qb[i,j] | i,j in reverse(1..n) ]; 
-      * Pomocne może być globalne ograniczenie ​[[http://www.minizinc.org/2.0/​doc-lib/​doc-globals-channeling.html|''​inverse''​]+array[int] of var bool: qb7 = [ qb[j,i] | i,j in reverse(1..n];
-    - Porównać działanie modelu pojedynczego ​dualnego na problemach wielu hetmanów. +
-    - Czerpać zyski <wrap lo>z nabytej wiedzy, bowiem nowe solvery radzą sobie z n-hetmanami lepiej bez nadmiarowych ograniczeńNiemniej w innych modelach technika ta może okazać się istotną optymalizacją.</​wrap>​ +
- +
-===== Sterowanie przeszukiwaniem ===== +
- +
-O ile budowanie modelu w programowaniu z ograniczeniami jest zadaniem czysto deklaratywnymczęsto wiele zależy od sposobu, w jaki przeszukiwana jest przestrzeń rozwiązańIstnieje wiele możliwych strategii, jednak w podstawowym przypadku można je zdefiniować poprzez dwa parametry:​ +
- +
-  - kolejność wyboru zmiennych - npw kolejnym kroku solver ustali wartość zmiennejktóra ma najmniejszą dziedzinę; ma największą dziedzinę; w kolejności zdefiniowania przez użytkownika,​ etc. +
-  - kolejność wyboru wartości - po wyborze zmiennej, solver musi przypisać jej wartość z dziedziny, np. wartość najmniejszą,​ medianę dziedziny, wartość losową, etc+
- +
-Aby w MiniZincu wymusić na solverze strategię przeszukiwanianależy użyć tzw. adnotacji ​(ang//​annotations//​)W ogólnym przypadku można je przypisywać do zmiennych podobnie jak inne parametry, ale najczęściej używa się ich przy specyfikacji celunp<​code>​ +
-solve :: int_search(array, ​first_fail, indomain_mincompletesatisfy;+
 </​code>​ </​code>​
-daje solverovi znać, że tablica zmiennych array zawiera liczby i należy ją przeszukiwać,​ najpierw wybierając zmienne o najmniejszej dziedzinie (''​first_fail''​),​ następnie przypisywać im jak najmniejsze wartości (''​indomain_min''​) i przeszukiwać całą przestrzeń rozwiązań (''​complete''​). ​ 
  
-Istnieją bardziej zaawansowane metody określania strategii w poszczególnych solverach, ale MiniZinc chcąc być językiem niezależnym od solvera nie może sobie na to pozwolić. Aktualnie konstrukcje pozwalające na pełną kontrolę nad procesem przeszukiwania działają jedynie dla solvera Gecode i wymagają dodania pewnych patchy do standardowej dystrybucji MiniZinc.+===== Reified Constraints =====
  
-==== n-hetmans again ==== +[[https://​en.wikipedia.org/​wiki/​Reification|Reification]] in Constraint Programming means treating the constraint as a first-order citizen, i.e. you can use the constraint as a boolean value in your model. If you've used the ''​bool2int''​ function in the Magic Sequence problem, you could do that only because the constraint ''​=''​ has been reified. Reification allows us to create models with "soft constraints"​ or "​conditional constraints",​ i.e. one constraint has to be satisfed only if the second one is satisfied too, otherwise they both can be ignored. To do that, one has only to reify the constraints and connect them with the implication:​ ''​constraint1 -> constraint2''​. Let's practice this quite useful technique :) 
-  + 
-  * Problem: ​już chyba każdy go zna. +==== Stable Marriage Problem ===== 
-  * Należy: +  * Problem: ​There are two classes of objects ​(men and womenfor examplethat have to be matched according to their preferences. We say that a matching ​(marriageis unstable if both spouses would prefer to be with somebody else. You can read more about this problem on [[https://en.wikipedia.org/wiki/Stable_marriage_problem|wikipedia]]. 
-    - Spróbować różnych adnotacji do przeszukiwania dla tego problemu, np. <​code>​ +  * Assignment: 
-int_search(rowsinput_order,​ indomain_min,​ complete)+    - Look at and comprehend ''​lab3/​stable-marriage/​stable-marriage.mzn''​ 
-int_search(rows, input_order,​ indomain_median,​ complete);​ +    - Add missing variables, constraints 
-int_search(rows,​ first_fail, indomain_min,​ complete)+    - Give a high-five to your teacher :)
-int_search(rows,​ first_fail, indomain_median,​ complete);</​code> ​  +
-    - Zerknąć do [[http://www.minizinc.org/downloads/doc-latest/​flatzinc-spec.pdf|dokumentacji flatzinc, rozdział 5.6.1]]. Przeczytać opisy różnych strategii i wypróbować jakąś szaloną+
-    - Porównać prędkość działania solvera n-hetmanów dla różnych strategii+
-    - Wyczerpać zyski z dzisiejszego laboratorium. ​+
  
  
en/dydaktyka/csp/lab2.1489361330.txt.gz · Last modified: 2019/06/27 16:00 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0