Constraint Modeling. with MiniZinc. Jakub Bulín. Department of CU Prague

Similar documents
Course Summary! What have we learned and what are we expected to know?

MiniZinc Tutorial. Ralph Becket 18 June The G12 project defines three closely related modelling languages:

Constraint Satisfaction Problems. slides from: Padhraic Smyth, Bryan Low, S. Russell and P. Norvig, Jean-Claude Latombe

Modelling with Constraints

Specification of FlatZinc

Round 4: Constraint Satisfaction Problems (CSP)

2. Modeling AEA 2018/2019. Based on Algorithm Engineering: Bridging the Gap Between Algorithm Theory and Practice - ch. 2

Lecture 7. Search. Search. Foundations of Constraint Programming

Gecode an open constraint solving library

Topic 3: MiniZinc (Version of 10th November 2015)

Specification of FlatZinc. Nicholas Nethercote Version 0.8.1

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Announcements. Homework 4. Project 3. Due tonight at 11:59pm. Due 3/8 at 4:00pm

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Constraint Satisfaction Problems

10/11/2017. Constraint Satisfaction Problems II. Review: CSP Representations. Heuristic 1: Most constrained variable

1 Inference for Boolean theories

Constraint Satisfaction Problems

A Brief Introduction to Constraint Programming

General Methods and Search Algorithms

Today. Introduction to Artificial Intelligence COMP 3501 / COMP Lecture 5. Constraint Satisfaction Problems (CSP) CSP Definition

MODELLING AND SOLVING SCHEDULING PROBLEMS USING CONSTRAINT PROGRAMMING

Minion: Fast, Scalable Constraint Solving. Ian Gent, Chris Jefferson, Ian Miguel

Introduction to Combinatorial Algorithms

Specification of Zinc and MiniZinc. Nicholas Nethercote Kim Marriott Reza Rafeh Mark Wallace María García de la Banda Version 0.8

Topic 2: Basic Modelling (Version of 30th January 2018)

Why Should You Care? Writing a CSP Solver in 3 (or 4) Easy Lessons. Talk Aims. Why Should You Care? Not Talk Aims. Talk Aims

Example: Map coloring

CSC Discrete Math I, Spring Sets

Programming Embedded Systems

Constraint Programming

CS 473: Algorithms. Ruta Mehta. Spring University of Illinois, Urbana-Champaign. Ruta (UIUC) CS473 1 Spring / 36

Polygon Triangulation

Constraint (Logic) Programming

What is Search For? CS 188: Artificial Intelligence. Constraint Satisfaction Problems

Announcements. Homework 1: Search. Project 1: Search. Midterm date and time has been set:

Java Classes: Math, Integer A C S L E C T U R E 8

Constraint solving on modular integers

AMS /672: Graph Theory Homework Problems - Week V. Problems to be handed in on Wednesday, March 2: 6, 8, 9, 11, 12.

Constraint Programming. Marco Kuhlmann & Guido Tack Lecture 1

Constraint Satisfaction Problems

CS 343: Artificial Intelligence

3 No-Wait Job Shops with Variable Processing Times

Fundamentals of Integer Programming

Integer Programming! Using linear programming to solve discrete problems

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CS 188: Artificial Intelligence Fall 2011

Lecture Notes on Real-world SMT

3rd CHR Summer School Topics: Introduction to Constraint Programming

JaCoP v Java Constraint Programming Libraray. Krzysztof Kuchcinski

There are no CNF problems. Peter J. Stuckey and countless others!

Introduction to Programming

Modelling. Christina Burt, Stephen J. Maher, Jakob Witzig. 29th September Zuse Institute Berlin Berlin, Germany

Functional Logic Programming. Kristjan Vedel

CS 188: Artificial Intelligence. What is Search For? Constraint Satisfaction Problems. Constraint Satisfaction Problems

System Description of a SAT-based CSP Solver Sugar

Minimum Satisfying Assignments for SMT. Işıl Dillig, Tom Dillig Ken McMillan Alex Aiken College of William & Mary Microsoft Research Stanford U.

Mathematical Programming Formulations, Constraint Programming

2017 SOLUTIONS (PRELIMINARY VERSION)

Congruence Arithmetic

K Reference Card. Complete example

CS 125 Section #10 Midterm 2 Review 11/5/14

Announcements. CS 188: Artificial Intelligence Fall 2010

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Built-in Types of Data

Hybrid Constraint Solvers

Constraint satisfaction problems. CS171, Winter 2018 Introduction to Artificial Intelligence Prof. Richard Lathrop

Abstract Interpretation

11 Linear Programming

Discrete Mathematics Lecture 4. Harper Langston New York University

CSE 20 DISCRETE MATH. Winter

Credit Cards. Validating Credit Cards. Answers

Constraint Satisfaction Problems

DISCRETE MATHEMATICS

ISA 563 : Fundamentals of Systems Programming

MATH 54 - LECTURE 4 DAN CRYTSER

CSE 20 DISCRETE MATH. Fall

CSE 473: Artificial Intelligence

Reading: Chapter 6 (3 rd ed.); Chapter 5 (2 nd ed.) For next week: Thursday: Chapter 8

Set 5: Constraint Satisfaction Problems Chapter 6 R&N

Data types for mcrl2

Bits, Words, and Integers

Relational Database: The Relational Data Model; Operations on Database Relations

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Information Science 1

CS2141 Software Development using C/C++ C++ Basics

Constraint Programming in Practice

Exercises Computational Complexity

Type Processing by Constraint Reasoning

Foundations of Artificial Intelligence

Neighborhood Search: Mixing Gecode and EasyLocal++

Constraint Satisfaction Problems (CSPs) Introduction and Backtracking Search

Constraint Programming 101

Lecture 10: Floating Point, Digital Design

Topics in Constraint Programming: Exercises

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Bitwise Data Manipulation. Bitwise operations More on integers

Introduction to Gecode

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Transcription:

Constraint Modeling with MiniZinc Jakub Bulín Department of Algebra @ CU Prague

Table of contents 1. Intro & the theory part 2. An overview of MinZinc 3. Examples of constraint models 4. Learn more 1

In this class What is constraint modeling Basic ideas for solving MiniZinc: a high-level solver-independent modeling language Gecode: an open source yet competitive constraint solver Examples: how to model problems in MiniZinc Homework: try it yourself 2

Intro & the theory part

What is it Constraint modeling (aka constraint programming, constraint optimization) is a generalization of Linear programming, Convex optimization, Integer programming, SAT solving. A constraint model consists of decision variables (each with a domain) and a list of constraints The goal: satisfy constraints / find all solutions / maximize a given objective function Advantages: Close to real-life problems, easy to model, can exploit structure (lost when translated to SAT) 3

An example: Sudoku What are the decision variables, their domains, constraints? 4

How to solve constraint models Propagation use constraints actively to infer other, implicit constraints forbid local assignments which lead to a contradiction Arc Consistency, Path Consistency, Search try constructing a solution, modify variables which fail some constraints backtracking (DFS), backjumping, for optimization: hill climbing, branch & bound, Global constraints make use of the model s combinatorial structure (which is lost when translated to SAT) run specialized algorithms for parts of the model different solving strategies for different families of constraints (arithmetics, LP, Boolean, ordering, scheduling, packing, circuit, DFA...) 5

Gecode generic constraint development environment 1886 classes, 291202 loc, 87574 lod 6

An overview of MinZinc

MiniZinc MiniZinc: (Towards a) Standard CP Modelling Language (CP 2007) A standard language for modelling CP problems will encourage ex- perimentation with and comparisons between different solvers. Although MiniZinc is not perfect no standard modelling language will be we believe its simplicity, expressiveness, and ease of implementation make it a practical choice for a standard language. Continuous development and improvement. Many mainstream Constraint solvers now understand MiniZinc. 7

Data types and operators Primitive types boolean variables: bool: x; bool y = true; integers: int: n; int: m = 3; floating point: float: z = 2.5; strings: only for formatting the output Operators arithmetic: +, -, *, div, mod, abs(), pow(), relational: =,!=, <, <=, logical: /\, \/, ->, <->, not, 8

Data structures Arrays, e.g. array[0..100,0..100] of var float: temp; array comprehension (similar to Python): [i + j i, j in 1..3 where j < i] evaluates to [1 + 2, 1 + 3, 2 + 3] which is [3, 4, 5] aggregation functions: min, max, sum, product, forall, exists Sets, e.g. set of int: values = {1, 4, 9, 16}; set comprehension (similar) set functions: in, subset, superset, union, inter, diff, symdiff 9

A constraint model Each MiniZinc model has these parts (but the order of statements does not matter) declare parameters, e.g. int n; before execution each parameter will have a fixed value separate model from data (command-line arguments or a.dzn file) declare decision variables, e.g. array[1..10] of var bool: x; the solver will choose values for these define constraints, e.g. constraint x+y > z; solve statement: solve satisfy; or solve maximize/minimize <obj-function>; (optional) output <array-of-strings>; 10

Examples of constraint models

A few examples Sudoku Math Software homework: The Unruly riddle Math Software homework: Schedule of classes Groups on n elements Finite projective planes RC4 Graph coloring SAT 11

Finite projective planes N 2 + N + 1 points, N 2 + N + 1 lines, N + 1 points on each line, N + 1 lines through each point, any two points lie on exactly one line, any two lines intersect at exactly 1 point, there are four points which do not lie on one line A finite projective plane exists if N = p k. Is this if and only if? N = 6 ruled out by theory, N = 10 by massive computation, N = 12 open. 12

Stream cipher RC4 seed generation input a key k 1,..., k 2 n 2 n output a seed π Sym 2 n a 1 = 0 π 1 = id for i = 0 to 2 n 1 do a i = a i 1 + π i 1 (i) + k i (mod 2 n ) π i = (i a i ) π i 1 return π 2 n 1 Goal: compute the key from a known seed. Idea: Model the whole computation process. 13

Learn more

Advanced topics PyMzn MiniZinc Python wrapper convert between MiniZinc and Python data objects useful tools for input and output processing solver specific commands pymzn.minizinc('test.mzn', 'data1.dzn', parallel=4, output_mode='dict') Assertions data consistency testing constraint assert(ammount >= 0, "Invalid input"); Search annotations prescribe solving strategy branch on my_array, variables with smallest domain first, largest values first solve :: int_search(my_arrray, first_fail, indomain_max, complete) satisfy; Predicates define your own (analogous to functions) predicate even(var int:x) = let { var int: y } in x = 2 * y; 14

Want to know more? MiniZinc website, a PDF tutorial! Two Coursera courses Basic modeling for discrete optimization Advanced modeling for discrete optimization http://www.minizinc.org/ http://www.coursera.org/ Hakan Kjellerstrand s MiniZinc webpage with lots of example models http://www.hakank.org/minizinc/ PyMzn homepage Gecode homepage Three courses at MatFyz http://paolodragone.com/pymzn/ NOPT042 Constraint programming NMAG563 Introduction to complexity of CSP NMMB536 Optimization and Approximation CSP http://www.gecode.org/ 15