1 / 63

Modelling with Constraints

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

noelle
Télécharger la présentation

Modelling with Constraints

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. Modelling with Constraints COMP4418 Lecture 1

  2. Models • A model • is a simplified representation of a problem or situation • faithful in some respects • used to predict/explain/describe the modelled situation

  3. Models • Model planes, railways • Flight simulators, video games • Economic models • Weather forecasting models • Road map

  4. Problem Solving • Informal problem • Semi-formal problem • Model (constraint representation) • Solution of the model • Solution of the problem

  5. 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

  6. Constraint Programming • High level language • allows model to be closer to the semi-formal problem • supports rapid prototyping • Algorithmic techniques • efficient computation of solutions

  7. 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

  8. 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

  9. Problems Satisfaction problem • Is this satisfiable? • What is a solution? Optimization problem • What is the “best” solution?

  10. Triangle Draw a right-angled triangle with sides of integer length, so that the perimeter is between 10 and 20

  11. Triangle Introduce variables to represent unknowns z y x

  12. Triangle Formulate conditions in terms of variables z y x

  13. Triangle Restrict variables to a finite range. z y x

  14. z y x Triangle Avoid symmetric solutions. z x y

  15. z y x Triangle Avoid symmetric solutions. or z x y

  16. Triangle Solve z y x

  17. Triangle Solve z y x Now, draw the triangle.

  18. Problem Solving • Informal problem • Semi-formal problem • Model (constraint representation) • Solution of the model • Solution of the problem Multiple iterations, in general

  19. 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;

  20. 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;

  21. 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]).

  22. Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne

  23. Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne

  24. Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Analog model Gary Louisville Evansville Fort Wayne Indianapolis

  25. Indianapolis is between Gary and Louisville, and between Evansville and Fort Wayne. Find it. Gary Louisville Evansville Fort Wayne

  26. Impose a coordinate system Gary (1, -3) Fort Wayne (12, -5) Evansville (0, -61) Louisville (11, -63) Gary Louisville Evansville Fort Wayne

  27. Introduce variables to represent unknowns Indianapolis (x, y) Gary Louisville Evansville Fort Wayne

  28. Interpret “between” as “on the same line” Gary Louisville Evansville Fort Wayne

  29. Interpret “between” as “on the same line” Express algebraically Gary Louisville Evansville Fort Wayne

  30. Add data Gary Louisville Evansville Fort Wayne

  31. Solve Gary Louisville Evansville Fort Wayne

  32. Interpret algebraic solution Gary Louisville Evansville Fort Wayne Indianapolis

  33. Interpret algebraic solution Verify solution Gary Louisville Evansville Fort Wayne Indianapolis

  34. 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” ];

  35. 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]).

  36. Problems These programs • very specific to the problem • not reusable • We would like to be able to solve similar problems easily

  37. Problems Problem class • a collection of problems that share the same structure Problem instance • a single problem Instance = Class + Data

  38. Problem class Sorting Graph colouring Timetabling Problem instance Sort the list 9,4,7,2,1 Colour this graph Timetable UNSW 10s2 Problems

  39. 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));

  40. 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);

  41. Typical CP Execution Model Interpreter Data Constraints Constraint Solver Solutions

  42. Polynomial Solve for integer value

  43. Polynomial Solve for integer value Reformulate

  44. Polynomial Solve for integer value Reformulate Simplify

  45. Polynomial Solve for integer value Reformulate Simplify Solve

  46. A model is a formal object It can be analysed mathematically, possibly automatically

  47. 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

  48. Dirichlet’s Problem • In Physics, modelled by a differential equation • We use a discrete approximation …

  49. 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;

  50. 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]) ];

More Related