640 likes | 756 Views
Modelling with Constraints. COMP4418 Lecture 1. Models. A model is a simplified representation of a problem or situation faithful in some respects used to predict/explain/describe the modelled situation. Models. Model planes, railways Flight simulators, video games Economic models
E N D
Modelling with Constraints COMP4418 Lecture 1
Models • A model • is a simplified representation of a problem or situation • faithful in some respects • used to predict/explain/describe the modelled situation
Models • Model planes, railways • Flight simulators, video games • Economic models • Weather forecasting models • Road map
Problem Solving • Informal problem • Semi-formal problem • Model (constraint representation) • Solution of the model • Solution of the problem
Constraint Programming • Declarative expression of model • Based on pre-defined relations • arithmetic, Boolean, set-oriented • Built-in techniques for reasoning about the relations • Suitable only for some forms of modelling
Constraint Programming • High level language • allows model to be closer to the semi-formal problem • supports rapid prototyping • Algorithmic techniques • efficient computation of solutions
CP Languages • MiniZinc • Medium level constraint language • Subset of Zinc • Restricted expressiveness • Currently only one constraint solver • ECLiPSe • Constraint logic programming language • Several constraint solvers
Related Languages • Relational Database Languages (SQL) • ad hoc, dynamic relations • Logic Programming Languages (Prolog) • user-defined relations • Mathematical Programming Languages (AMPL) • limited to arithmetic relations
Problems Satisfaction problem • Is this satisfiable? • What is a solution? Optimization problem • What is the “best” solution?
Triangle Draw a right-angled triangle with sides of integer length, so that the perimeter is between 10 and 20
Triangle Introduce variables to represent unknowns z y x
Triangle Formulate conditions in terms of variables z y x
Triangle Restrict variables to a finite range. z y x
z y x Triangle Avoid symmetric solutions. z x y
z y x Triangle Avoid symmetric solutions. or z x y
Triangle Solve z y x
Triangle Solve z y x Now, draw the triangle.
Problem Solving • Informal problem • Semi-formal problem • Model (constraint representation) • Solution of the model • Solution of the problem Multiple iterations, in general
MiniZinc Program var 1..20: x; var 1..20: y; var 1..20: z; constraint x*x + y*y = z*z; constraint 10 <= x + y + z; constraint x + y + z <= 20; constraint x >= y; solve satisfy;
MiniZinc Program var 1..20: x; var 1..20: y; var 1..20: z; constraint x*x + y*y = z*z; constraint 10 <= x + y + z; constraint x + y + z <= 20; solve maximise x;
Eclipse Program :- lib(ic). solve(X, Y, Z) :- [X, Y, Z] :: 1..20, X*X + Y*Y #= Z*Z, 10 #<= X + Y + Z, X + Y + Z #<= 20, X #>= Y. :- solve(X, Y, Z), labeling([X,Y,Z]).
Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne
Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne
Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Analog model Gary Louisville Evansville Fort Wayne Indianapolis
Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne
Impose a coordinate system Gary (1, -3) Fort Wayne (12, -5) Evansville (0, -61) Louisville (11, -63) Gary Louisville Evansville Fort Wayne
Introduce variables to represent unknowns Indianapolis (x, y) Gary Louisville Evansville Fort Wayne
Interpret “between” as “on the same line” Gary Louisville Evansville Fort Wayne
Interpret “between” as “on the same line” Express algebraically Gary Louisville Evansville Fort Wayne
Add data Gary Louisville Evansville Fort Wayne
Solve Gary Louisville Evansville Fort Wayne
Interpret algebraic solution Gary Louisville Evansville Fort Wayne Indianapolis
Interpret algebraic solution Verify solution Gary Louisville Evansville Fort Wayne Indianapolis
MiniZinc program var 0..20: x; var -70..0: y; constraint y + 6 * x - 3 = 0; constraint 3 * y - 14 * x + 183 = 0; solve satisfy; output [ "Position of Indianapolis is (", show(x), ", ", show(y), ")\n” ];
Eclipse program :- lib(ic). solve(X, Y) :- Y + 6 * X - 3 #= 0, 3 * Y - 14 * X + 183 #= 0. :- X :: 0..20, Y :: -70..0, solve(X, Y), printf("Position of Indianapolis is (%d, %d)\n”, [X, Y]).
Problems These programs • very specific to the problem • not reusable • We would like to be able to solve similar problems easily
Problems Problem class • a collection of problems that share the same structure Problem instance • a single problem Instance = Class + Data
Problem class Sorting Graph colouring Timetabling Problem instance Sort the list 9,4,7,2,1 Colour this graph Timetable UNSW 10s2 Problems
Data independent model (1) constraint between(1, -3, x, y, 11, -63) /\ between(0, -61, x, y, 12, -5); predicate between(var int: x1, var int: y1, var int: x, var int: y, var int: x2, var int: y2) = ((y2 - y1) * (x - x1)) = ((y - y1) * (x2 - x1));
Data independent model (2) int: Gx = 1; int: Gy = -3; % Gary int: Lx = 11; int: Ly = -63; % Louisville int: Ex = 0; int: Ey = -61; % Evansville int: FWx = 12; int: FWy = -5; % Fort Wayne constraint between(Gx, Gy, x, y, Lx, Ly) /\ between(Ex, Ey, x, y, FWx, FWy);
Typical CP Execution Model Interpreter Data Constraints Constraint Solver Solutions
Polynomial Solve for integer value
Polynomial Solve for integer value Reformulate
Polynomial Solve for integer value Reformulate Simplify
Polynomial Solve for integer value Reformulate Simplify Solve
A model is a formal object It can be analysed mathematically, possibly automatically
Dirichlet’s Problem • Heating a square metal sheet by controlling its temperature at the edges • Temperature of a point varies over time, depending on • Heat conductivity • Surrounding temperatures • Find steady-state temperature at centre
Dirichlet’s Problem • In Physics, modelled by a differential equation • We use a discrete approximation …
Dirichlet’s Problem (MiniZinc) include "dirichlet_data.mzn"; int: n; array[1..n, 1..n] of var float: temp; constraint forall(i,j in 2..n-1)( temp[i, j] = 1.0/4.0 * (temp[i-1, j] + temp[i+1, j] + temp[i, j-1] + temp[i, j+1]) ); solve satisfy;
Dirichlet’s Problem (MiniZinc) int: half_n = n div 2; output [ "Temperature at (", show(half_n), ", ", show(half_n), ") is ", show(temp[half_n, half_n]) ];