1 / 63

Sistemi con Vincoli ( Laboratorio )

Sistemi con Vincoli ( Laboratorio ). Programma laboratorio (12 ore). Introduzione Usare la libreria JaCoP Store Variables and domains Vincoli Primitivi L ogici , condizionali e reificati Globali D ecomposti S u insiemi Metodi di ricerca Debugging in JaCoP Esercitazioni

fernandob
Download Presentation

Sistemi con Vincoli ( Laboratorio )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sistemi con Vincoli (Laboratorio) Sistemi con Vincoli (Laboratorio) - Francesco Santini

  2. Programmalaboratorio (12 ore) Sistemi con Vincoli (Laboratorio) - Francesco Santini • Introduzione • Usare la libreria JaCoP • Store • Variables and domains • Vincoli • Primitivi • Logici, condizionali e reificati • Globali • Decomposti • Su insiemi • Metodi di ricerca • Debugging in JaCoP • Esercitazioni • Eventuale inizio progetto per esame

  3. Esame Sistemi con Vincoli (Laboratorio) - Francesco Santini • Progetto + orale • Progetto: • Informatici: implementazione in JaCoP • Matematici: modellazione di un esempio, tenendocontodeivincoli in JaCoPspiegati a lezione • A gruppi di 2-3… • Da consegnareunasettimana prima dell’esameorale • Dottorandi…

  4. Materiale Sistemi con Vincoli (Laboratorio) - Francesco Santini Slide presentate a lezione (distribuite) e altro (?) Survey http://ktiml.mff.cuni.cz/~bartak/downloads/WDS99.pdf Principles of Constraint Programming, Krzysztof Apt, Cambridge University Press, 2003 ~ 66$ Constraint processing, R. Dechter, Morgan Kaufmann, 2003. Handbook of Constraint Programming, F. Rossi, P. Van Beek, T. Walsh, editors, Elsevier, 2006. Constraint networks: Techniques and Algorithms, C, Lecoutre, Wiley, 2009.

  5. Altromateriale Sistemi con Vincoli (Laboratorio) - Francesco Santini sulla implementazione di risolutori: articolo "Finite domain constraint programming systems" di Christian Schulte e MatsCarlsson sul vincolo globale "all-different": articolo "The alldifferent constraint: a survey" di W.-J. van Hoeve sulla propagazione con vincoli: articolo di Christian Bessiere che descrive in modo preciso vari algoritmi di propagazione di vincoli sui vincoli soft: articolo sulle basi algebriche dei vincoli soft

  6. Altri solver Sistemi con Vincoli (Laboratorio) - Francesco Santini MiniZinc Minion Choco (Java) Comet Gecode (C++) Gecode/R (Ruby) ECLiPSE (Prolog) SICStus Prolog Tailor/Essence

  7. Installing JaCoP 3.1 Sistemi con Vincoli (Laboratorio) - Francesco Santini • JaCoP - Java Constraint Programming solver http://jacop.osolpro.com/ • GNU Affero GPL • API online • Manual • Dowload, http://sourceforge.net/projects/jacop-solver/files/JaCoP-3.1/JaCoP-3.1.jar/download • How to compile and execute •    javac -classpath pathToJacop/JaCoP.jar Main.java   •    java -classpath  pathToJacop/JaCoP.jar Main

  8. Introduction Sistemi con Vincoli (Laboratorio) - Francesco Santini • JaCoP library provides constraint programming paradigm implemented in Java. • It provides primitives to define finite domain (FD) variables, constraints and search methods. • Different domains: • boolean domains, where only true/false constraints apply (SAT problem) • integer domains, rational domains • linear domains, where only linear functions are described and analyzed (although approaches to non-linear problems do exist) • finite domains, where constraints are defined over finite sets • mixed domains, involving two or more of the above

  9. Introduction (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini • An example of the import statements that import the whole subpackages of JaCoP at once is shown below. • importJaCoP.core.*; • importJaCoP.constraints.*;   • importJaCoP.search.*;

  10. Example  // search for a solution and print results         Search<IntVar> search = new DepthFirstSearch<IntVar>(); SelectChoicePoint<IntVar> select =             new InputOrderSelect<IntVar>(store, v,                                          new IndomainMin<IntVar>()); boolean result = search.labeling(store, select);         if ( result ) System.out.println("Solution: " + v[0]+", "+v[1] +", "+                                               v[2] +", "+v[3]);         else System.out.println("*** No");     } } Sistemi con Vincoli (Laboratorio) - Francesco Santini public class Main {     static Main m = new Main ();     public static void main (String[] args) {         Store store = new Store();  // define FD store int size = 4;         // define finite domain variables IntVar[] v = new IntVar[size];         for (inti=0; i<size; i++)             v[i] = new IntVar(store, "v"+i, 1, size);         // define constraints store.impose( new XneqY(v[0], v[1]) ); store.impose( new XneqY(v[0], v[2]) ); store.impose( new XneqY(v[1], v[2]) ); store.impose( new XneqY(v[1], v[3]) ); store.impose( new XneqY(v[2], v[3]) ); 

  11. The store Sistemi con Vincoli (Laboratorio) - Francesco Santini • It knits together all components required to model and solve the problem using JaCoP • Both variables and constraints are stored in the store • The store must be created before constraints and variables • Store store = new Store(); • System.out.println(store.toString());

  12. Variables and Domains Sistemi con Vincoli (Laboratorio) - Francesco Santini • How to declare • IntVar x = new IntVar(store, "X", 1,100); • A variable X with the domain 1..100 ∨ 120..160 • x.addDom(120, 160); • Methods min() and max() to know the current min and max values in the domain • BooleanVar: added to JaCoP as they can be handled more efficiently than FDVs with multiple elements in their domain. • BooleanVarbv = new BooleanVar(s, "bv"); • Some constraints may require BooleanVaras parameters • IfThen(c1, c2);

  13. Variables and Domains (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini • public intsizeConstraints() • It returns current number of constraints which are associated with variable and are not yet satisfied. • public intgetSize() • It returns the size of the current domain. • public intvalue() • This function returns current value in the domain of the variable. If current domain of variable is not singleton then warning is printed and minimal value is returned.

  14. Variables an Domains (3) Sistemi con Vincoli (Laboratorio) - Francesco Santini • JaCoP default domain (cllaedIntervalDomain) is represented as an ordered list of intervals. • It is not always computationally efficient. • A BoundDomainand can be used by a finite domain variable in a same way as interval domain. The only difference is that any attempt to remove values from inside the interval of this domain will have no effect. • IntVar v = new IntVar(s, "v", new BoundDomain(1, 10) );

  15. Primitive Constraints JaCoP offers a set of primitive constraints that include basic arithmetic operations (+,-,*,∕) as well as basic relations Sistemi con Vincoli (Laboratorio) - Francesco Santini

  16. Primitive Constraints (1) Sistemi con Vincoli (Laboratorio) - Francesco Santini • Primitive constraints and global constraints can be imposed using imposemethod • store.impose( newXeqY(x1, x2)); • Alternatively, one can define first a constraint and then impose it, as shown below. • PrimitiveConstraint c = newXeqY(x1, x2); c.impose(store); • These methods create additional data structures within the constraint store as well as constraint itself.

  17. Primitive Constraints (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini Note that constraint imposition does not involve checking if the constraint is consistent. Both methods of constraint imposition does not check whether the store remains consistent. If checking consistency is needed, the method imposeWithConsistency(constraint) should be used instead. This method throws FailException if the store is inconsistent. Primitive constraints can be used as arguments in logical and conditional constraints.

  18. Logical, conditional and reified constraints Sistemi con Vincoli (Laboratorio) - Francesco Santini

  19. Logical, conditional and reified constraints (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini

  20. Logical, conditional and reified constraints (3) Sistemi con Vincoli (Laboratorio) - Francesco Santini • Reified constraints of the form X = Y ⇔ B can be defined in JaCoP as follows. • IntVar x = new IntVar(store, "X", 1, 100); IntVar y = new IntVar(store, "Y", 1, 100); IntVar b = new IntVar(store, "B", 0, 1); store.impose( new Refified( new XeqY(x, y), b); • disjunctive constraints can be imposed. • ArrayList<PrimitiveConstraint> c = newArrayList<PrimitiveConstraint>(); c.add(c1); c.add(c2); c.add(c3); store.impose( new Or(c) );

  21. Global Constraints Sistemi con Vincoli (Laboratorio) - Francesco Santini • A global constraint is a constraint that captures a relation between a non-fixed number of variables. • Alldifferent: diff values for diff vars • A global constraint is semantically redundant: the same relation as the conjunction of several simpler constraints. • To facilitate the work of the constraint solver by providing it with a better view of the structure of the problem. • A global constraint in JaCoP comes with its implemented consistency technique • 30 predefined global constraints in JaCoP

  22. AllDifferent Sistemi con Vincoli (Laboratorio) - Francesco Santini • Alldifferent constraint: paiwise different • IntVar a = new IntVar(store, "a", 1, 3); IntVar b = new IntVar(store, "b", 1, 3); IntVar c = new IntVar(store, "c", 1, 3); IntVar[] v = {a, b, c}; Constraintctr = new Alldifferent(v); store.impose(ctr); • 3 implementation in JaCoP • Aldifferent • Alldiff • Alldistinct

  23. Sum and SumWeight Sistemi con Vincoli (Laboratorio) - Francesco Santini x1 + x2+… + xn = sum w1⋅ x1+ w2⋅ x2 +…+ wn⋅ xn= sum IntVar a = new IntVar(Store, "a", 1, 3); IntVar b = new IntVar(Store, "b", 1, 3); IntVar c = new IntVar(Store, "c", 1, 3); IntVar sum = new IntVar (Store, "sum", 1, 10); IntVar[] v = {a, b, c}; Constraintctr = new Sum(v, sum); Store.impose(ctr);

  24. Knapsack Sistemi con Vincoli (Laboratorio) - Francesco Santini • The classical Thief’s dilemma… • profits - the list of profits, each for the corresponding item no. • weights - the list of weights, each for the corresponding item no. • quantity - finite domain variable specifying allowed values for the vars. • knapsackCapacity- finite domain variable specifying the capacity limit of the knapsack. • knapsackProfit – specifyin the total profit • Knapsack(int[] profits, int[] weights, IntVar[] quantity, IntVarknapsackCapacity, IntVarknapsackProfit)

  25. DecomposedConstraints Sistemi con Vincoli (Laboratorio) - Francesco Santini • Decomposed constraints do not define any new constraints and related pruning algorithms. • They are translated into existing JaCoP constraints. • Imposed with method imposeDecomposition

  26. Sequence Constraint Sistemi con Vincoli (Laboratorio) - Francesco Santini • Sequence constraint restricts values assigned to variables from a list of variables in such a way that any sub-sequence of length q contains N values from a specified set of values. Value N is further restricted by specifying min and max allowed values. Value q, min and max must be integer. • IntVar[] var = new IntVar[5]; for (inti=0; i<var.length; i++) var[i] = new IntVar(store, "v"+i, 0, 2); store.imposeDecomposition(new Sequence(var, //variable list                         new IntervalDomain(1,1), //set of values                         3, // q, sequence length                         2, // min                         2  // max                       )); • Solutions: [01101, 01121, 10110, 10112, 11011, 11211, 12110, 12112, 21101, 21121]

  27. Stretch Constraint Sistemi con Vincoli (Laboratorio) - Francesco Santini • Stretch constraint defines what values can be taken by variables from a list and how sub-sequences of these values are formed. For each possible value it specifies a minimum (min) and maximum (max) length of the sub-sequence of these values. • IntVar[] var = new IntVar[5]; for (inti=0; i<var.length; i++) var[i] = new IntVar(store, "v"+i, 1, 2); store.imposeDecomposition(                      new Stretch(new int[] {1, 2},  // values                                  new int[] {1, 2},  // min for 1 & 2                                  new int[] {2, 3},  // max for 1 & 2 var // variables                             )); • solutions: [11221, 11222, 12211, 12221, 22122, 22211]

  28. Lex Constraint [[0, 0], [1], [1, 0], [2]], [[0, 0], [1], [1, 1], [2]], [[0, 0], [1], [1, 2], [2]], [[0, 1], [1], [1, 0], [2]], [[0, 1], [1], [1, 1], [2]], [[0, 1], [1], [1, 2], [2]], [[0, 2], [1], [1, 0], [2]], [[0, 2], [1], [1, 1], [2]], [[0, 2], [1], [1, 2], [2]] Sistemi con Vincoli (Laboratorio) - Francesco Santini • The Lex constraint enforces ascending lexicographic order between n vectors that can be of different size. The constraints makes it possible to enforce strict ascending lexicographic order, that is vector i must be always before vector i + 1 in the lexicographical order, or it can allow equality between consecutive vectors • IntVar[] d = new IntVar[6]; for (inti = 0; i < d.length; i++) { d[i] = new IntVar(store, "d["+i+"]", 0, 2); }IntVar[][] x = { {d[0],d[1]}, {d[2]}, {d[3],d[4]}, {d[5]} }; store.imposeDecomposition(new Lex(x, true));

  29. Soft-Alldifferent Sistemi con Vincoli (Laboratorio) - Francesco Santini • It makes it possible to violate to some degree the alldifferent relation. The violations will come at a cost which is represented by cost variable. • decomposition based, where violation of any inequality relation between any pair contributes one unit of cost • variable based, which simply states how many times a variable takes value that is already taken by another variable. • IntVar[] x = new IntVar[5]; for (inti=0; i< x.length; i++)       x[i] = new IntVar(store, "x"+i, 1, 4); IntVar cost = new IntVar(store, "cost", 0, 20); store.imposeDecomposition(new SoftAlldifferent(x, cost, ViolationMeasure.DECOMPOSITION_BASED);

  30. Set Constraints Sistemi con Vincoli (Laboratorio) - Francesco Santini • A Set is defined as an ordered collection of integers • greatest lower bound glb(d) and • least upper bound lub(d) • glb(d) ⊆ lub(d) • SetVar s = new SetVar(store, "s", newBoundSetDomain (new IntervalDomain(1,1),  newIntervalDomain(1,3))); • glb(d) = {1} and lub(d) = {1,2,3} = {{1}, {1,2},{1,3}, {1,2,3}} • Only lub{{}, {1,2,3}}, {} is a “real” set • SetVar s = new SetVar(store, "s", 1, 3);

  31. Quiz Sistemi con Vincoli (Laboratorio) - Francesco Santini • glb = {1}, lub= {1..4} • {1..4}, {1..3}, {1..2, 4}, {1..2}, {1, 3..4}, {1, 3}, {1, 4}, {1} • glb= {2}, lub= {2..5} • {2..5}, {2..4}, {2..3, 5}, {2..3}, {2, 4..5}, {2, 4}, {2, 5}, {2}

  32. Set Constraints (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini

  33. Set Constraints (3) Sistemi con Vincoli (Laboratorio) - Francesco Santini • SetVar s1 = new SetVar(store, "s1",                 new BoundSetDomain(new IntervalDomain(1,1),   new IntervalDomain(1,4))); SetVar s2 = new SetVar(store, "s2",                 new BoundSetDomain(new IntervalDomain(2,2),  newIntervalDomain(2,5))); SetVar s = new SetVar(store, "s", 1,10); Constraint c = new AunionBeqC(s1, s2, s); • It performs operation {{1}..{1..4}} ⋃ {{2}..{2..5}} = {{}..{1..10}} • Quiz: some examples of solutions? • s1={1} s2={2} s={1..2} or s1={1..4} s2={2..5} s={1..5}

  34. Search Sistemi con Vincoli (Laboratorio) - Francesco Santini • After specification of the model consisting of variables and constraints, a search for a solution can be started. JaCoP offers a number of methods for doing this. • Consistency check • boolean result = store.consistency(); • When the procedure returns false then the store is in inconsistent state and no solution exists. The result true only indicates that inconsistency cannot be found. In other words, since the finite domain solver is not complete it does not automatically mean that the store is consistent.

  35. Depth First Search Sistemi con Vincoli (Laboratorio) - Francesco Santini • To find a single solution the DepthFirstSearch method can be used. • Search<T> label = new DepthFirstSearch<T>(); SelectChoicePoint<T> select = new SimpleSelect<T>(var, varSelect, tieBreakerVarSelectindomain);  • where T is type of variables we are using for this search (usually IntVar or SetVar) • var is a list of variables • varSelect is a comparator method for selecting variable • tieBreakerVarSelect is a tie breaking comparator method • indomain method is used to select a value that will be assigned to a selected variable

  36. Depth First Search (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini • Different classes can be used to implement SelectChoicePoint interface. • Store store= new Store(); IntVar[] vars;  Constraint c = new AinB();   ...    Search<IntVar> label = new DepthFirstSearch<IntVar>(); SelectChoicePoint<IntVar> select = newSimpleSelect<IntVar>( vars, newSmallestDomain<IntVar>(), newIndomainMin<IntVar>()); boolean result = label.labeling(store, select);

  37. Depth First Search (3) Sistemi con Vincoli (Laboratorio) - Francesco Santini • Additional switches for search for all solutions. • label.getSolutionListener().searchAll(true); // record solutions; if not set false label.getSolutionListener().recordSolutions(true);  label.setTimeOut(10); boolean result = label.labeling(store, select); • To be able to print found solutions during search • label.setSolutionListener(new PrintOutListener<IntVar>()); • The found solutions can also be printed after search is completed using the following statement • label.printAllSolutions();

  38. Depth First Search (4) Sistemi con Vincoli (Laboratorio) - Francesco Santini Solution listener plug-in is called by search when a solution is found Exit child listener plug-in is called every time the search exits the search subtree (it has four different methods; two methods, which are called when the search has exited the left subtree and two methods for the right subtree). Consistency listener plug-in is called after consistency method at the current search node. Exit listener plug-in is called each time the search is about to exit (it can used to collect relevant search information). Time-out listener plug-in is called when the time-out occurs (if specified). Initialize listener plug-in is called at the beginning of the search.

  39. Value Selection for FDV Sistemi con Vincoli (Laboratorio) - Francesco Santini

  40. Variable Selection for FDV Sistemi con Vincoli (Laboratorio) - Francesco Santini

  41. Val-Var Selection for SetVar Sistemi con Vincoli (Laboratorio) - Francesco Santini

  42. Example Sistemi con Vincoli (Laboratorio) - Francesco Santini import JaCoP.core.*; import JaCoP.search.*; import JaCoP.constraints.*; import JaCoP.set.constraints.AunionBeqC; import JaCoP.set.core.BoundSetDomain; import JaCoP.set.core.SetVar;importJaCoP.set.search.IndomainSetRandom; public class Main { public static void main(String[] args) { Store store = new Store(); SetVars1 = new SetVar(store, "s1", new BoundSetDomain(new IntervalDomain(1,1), new IntervalDomain(1,4))); SetVars2 = new SetVar(store, "s2", new BoundSetDomain(new IntervalDomain(2,2), new IntervalDomain(2,5))); SetVars = new SetVar(store, "s", 1,10); Constraint c = new AunionBeqC(s1, s2, s);

  43. Example (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini SetVar[] v= new SetVar[3]; v[0] = s1; v[1] = s1; v[2] = s; Search<SetVar> search = new DepthFirstSearch<SetVar>(); SelectChoicePoint<SetVar> select = new SimpleSelect<SetVar>( v, new MinLubCard<SetVar>(), new MaxGlbCard<SetVar>(), new IndomainSetMin<SetVar>()); search.setSolutionListener(new SimpleSolutionListener<SetVar>()); search.getSolutionListener().searchAll(true); search.getSolutionListener().recordSolutions(true); boolean result = search.labeling(store, select); search.printAllSolutions(); } }

  44. Output Sistemi con Vincoli (Laboratorio) - Francesco Santini Depth First Search DFS0 [s1 = {1}, s2 = {2}, s = {1..2}]No of solutions : 64 Last Solution : [s1={1}, s2={2}, s={1..2}] Nodes : 63 Decisions : 63 Wrong Decisions : 0 Backtracks : 63 Max Depth : 6 Number of Solutions: 64 {1..4} {2..5} {1..5} {1..4} {2..4} {1..4} {1..4} {2..3, 5} {1..5} … {1} {2, 5} {1..2, 5} {1} {2} {1..2}

  45. LDS Search Sistemi con Vincoli (Laboratorio) - Francesco Santini • Limited discrepancy search (LDS) uses the partial search method proposed in [6]. It basically allows only a number of different decisions along a search path, called discrepancies. If the number of discrepancies is exhausted backtracking is initiated. • The number of discrepancies is specified as a parameter for LDS. • Search<IntVar> label = new DepthFirstSearch<IntVar>(); SelectChoicePoint<IntVar> select=newSimpleSelect<IntVar (var,                                      new SmallestDomain<IntVar>(),                                       new IndomainMiddle<IntVar>()); LDS<IntVar> lds = new LDS<IntVar>(2); label.getExitChildListener().setChildrenListeners(lds); boolean result = label.labeling(store, select);

  46. Credit Search Sistemi con Vincoli (Laboratorio) - Francesco Santini • public CreditCalculator(int credit, int backtracks, intmaxDepth) • Credits are divided equally among children • As soon a node has only one credit, there is a restriction how many backtracks can be performed in search • If nodes are at maxDepth then the credits are not splitted among children but sent back to the parent for use in other parts of the tree. • Can or cannot be declared • Since we control the search it is possible to partially explore the whole tree and avoid situations when the search is stuck at one part of the tree which is a common problem of B&B algorithm when a depth first search strategy is used.

  47. Credit Search (2) 4 credits 1 backtrack 3 depth 2 2 2 1 1 2 1 backtrack! Sistemi con Vincoli (Laboratorio) - Francesco Santini

  48. Credit Search (3) Sistemi con Vincoli (Laboratorio) - Francesco Santini SelectChoicePoint<IntVar> select = new SimpleSelect<IntVar>(vars,                                        new SmallestDomain<IntVar>(),                                        new IndomainMin<IntVar>()); int credits=8, backtracks=3, maxDepth=1000; CreditCalculator<IntVar> credit = new CreditCalculator<IntVar>(                                                   credits,                                                   backtracks, maxDepth); Search<IntVar> search = new DepthFirstSearch<IntVar>(); search.setConsistencyListener(credit); search.setExitChildListener(credit); search.setTimeOutListener(credit); boolean result = search.labeling(store, select);

  49. Debugging in JaCoP Sistemi con Vincoli (Laboratorio) - Francesco Santini • JaCoP can generate trace in a format accepted by CPviz • open-source visualization toolkit for finite domain constraint programming (http://sourceforge.net/projects/cpviz/). • mkdirviz.outjava -cp <path to CPviz>/viz/ bin/ ie.ucc.cccc.viz.Vizconfig.xmltree.xmlvis.xml • xmlversion="1.0" encoding="UTF-8"?> <configurationversion="1.0" directory="viz.out">     <tool show="tree" fileroot="tree" repeat="all"/>     <tool show="viz" fileroot="viz"/> configuration>

  50. Debugging in JaCoP (2) Sistemi con Vincoli (Laboratorio) - Francesco Santini In the case of this configuration, the files will be generated in directory viz.out java -cp batik.jar:jhall.jar:<path to cpviz>/viztool/src             components.InternalFrame batik.jar and jhall.jar are separate software packages that must be installed separately Once the visualization tool is started one has to open file viz.out/aaa.idx.

More Related