1 / 108

Declarative Methods in Generate-and-Test Programming

Explore the principles of declarative programming and its application in generate-and-test problems, highlighting differences between procedural and declarative approaches. Learn about NP problems, SAT, and more.

pingd
Download Presentation

Declarative Methods in Generate-and-Test Programming

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. Satisfiability Generate-and-test / NP / NP-complete SAT, weighted MAX-SAT, CNF-SAT, DNF-SAT, 3-CNF-SAT, Tseitin, TAUT, QSAT Some applications: LSAT , register allocation 600.325/425 Declarative Methods - J. Eisner

  2. From a previous lecture … • So, Prof. Eisner, what are declarative methods?? • A declarative program states only what is to be achieved • A procedural program describes explicitly how to achieve it • Sorting in a declarative language • “Given array X, find an array Y such that • (1) Y is a permutation of X • (2) Y’s elements are in non-decreasing order” • Compiler is free to invent any sorting algorithm! (Hard?!) • You should be aware of when compiler will be efficient/inefficient • Sorting in a procedural language • “Given array X, run through it from start to finish, swapping adjacent elements that are out of order …” • Longer and probably buggier • Never mentions conditions (1) and (2), except in comments 600.325/425 Declarative Methods - J. Eisner

  3. Generate-and-test problems(a common form of declarative programming) • Problem specified by a fast “checking” function f(X,Y) • Input: string x • Output: Some string y such that f(x,y) = true x, y may encode any data you like • Examples: • f(x,y) is true iff y is sorted permutation of x • f(x,y) is true iff y is timetable that satisfies everyone’s preferences x (note that x and y are encodings as strings) • NP = {all generate-and-test problems with “easy” f} • f(x,y) can be computed in polynomial time O(|x|k), for any y we should consider (e.g., legal timetables) • Could do this for all y in parallel (NP = “nondeterministic polynomial time”) 600.325/425 Declarative Methods - J. Eisner

  4. Generate-and-compare problems(a common form of declarative programming) • Problem is specified by a fast “scoring” function f(X,Y) • Input: string x • Output: Some string y such that f(x,y) is maximized x, y may encode any data you like • Examples: • f(x,y) evaluates how well the timetable y satisfies everyone’s preferences x (note that x and y are encodings as strings) • OptP = {all generate-&-compare problems with easy f} • f(x,y) can be computed in polynomial time O(|x|k), for any y we should consider (e.g., legal timetables) 600.325/425 Declarative Methods - J. Eisner

  5. An LSAT Practice Problem(can we encode this in logic?) • When the animated “Creature Buddies” go on tour, they are played by puppets. • Creatures: Dragon, Gorilla, Kangaroo, Tiger • Names: Audrey, Hamish, Melville, Rex • Chief Puppeteers: Ben, Jill, Paul, Sue • Asst. Puppeteers: Chris, Emily, Faye, Zeke 600.325/425 Declarative Methods - J. Eisner from http://www.testprepreview.com/modules/logicalreasoning.htm

  6. An LSAT Practice Problem(can we encode this in logic?) Creatures: Dragon, Gorilla, Kangaroo, Tiger Names: Audrey, Hamish, Melville, Rex Chief Puppeteers: Ben, Jill, Paul, Sue Asst. Puppeteers: Chris, Emily, Faye, Zeke • Melville isn’t the puppet operated by Sue and Faye. • Hamish’s chief puppeteer (not Jill) is assisted by Zeke. • Ben does the dragon, but Jill doesn’t do the kangaroo. • Chris assists with the tiger. • Rex (operated by Paul) isn’t the gorilla (not named Melville). • What is the Dragon’s name? • Who assists with puppet Melville? • Which chief puppeteer does Zeke assist? • What kind of animal does Emily assist with? Is there a technique that is guaranteed to solve these? • Generate & test? • Is the “grid method” faster? Always works? 600.325/425 Declarative Methods - J. Eisner from http://www.testprepreview.com/modules/logicalreasoning.htm

  7. creature assistant chief name chief Melville isn’t the puppet operated by Sue and Faye. assistant 600.325/425 Declarative Methods - J. Eisner

  8. creature assistant chief name chief Melville isn’t the puppet operated by Sue and Faye. assistant 600.325/425 Declarative Methods - J. Eisner

  9. This English gave a few facts: e.g., Jill is not assisted by Zeke. creature assistant chief name chief Hamish’s chief puppeteer (not Jill) is assisted by Zeke. assistant 600.325/425 Declarative Methods - J. Eisner

  10. creature assistant chief name chief Hamish’s chief puppeteer (not Jill) is assisted by Zeke. assistant 600.325/425 Declarative Methods - J. Eisner

  11. creature assistant chief name chief Ben does the dragon, but Jill doesn’t do the kangaroo. assistant 600.325/425 Declarative Methods - J. Eisner

  12. creature assistant chief name chief Chris assists with the tiger. assistant 600.325/425 Declarative Methods - J. Eisner

  13. creature assistant chief name chief Rex (operated by Paul) isn’t the gorilla (not named Melville). assistant 600.325/425 Declarative Methods - J. Eisner

  14. creature assistant chief name chief Paul doesn’t operate the gorilla. Rex (operated by Paul) isn’t the gorilla (not named Melville). assistant 600.325/425 Declarative Methods - J. Eisner

  15. Grid method (for puzzles that match people with roles, etc.) • Each statement simply tells you to fill a particular cell with or • If you fill a cell with , then fill the rest of its row and column with  • If a row or column has one blank and the rest are , then fill the blank with 600.325/425 Declarative Methods - J. Eisner

  16. Is that enough? No! It didn’t even solve the puzzle before. We need more powerful reasoning patterns (“propagators”): • We were told that Ben operates the dragon. • We already deduced that Ben doesn’t work with Faye. • What should we conclude? 600.325/425 Declarative Methods - J. Eisner

  17. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  18. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 18 600.325/425 Declarative Methods - J. Eisner

  19. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 19 600.325/425 Declarative Methods - J. Eisner

  20. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  21. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  22. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  23. creature assistant chief name chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  24. creature assistant chief name No new conclusionsfrom this one chief Combine facts from different 4x4 grids: If XY and YZ then conclude XZ  assistant 600.325/425 Declarative Methods - J. Eisner

  25. creature assistant chief name chief Now we can make someeasy progress! assistant 600.325/425 Declarative Methods - J. Eisner

  26. creature assistant chief name chief Now we can make someeasy progress! assistant 600.325/425 Declarative Methods - J. Eisner

  27. creature assistant chief name chief Now we can make someeasy progress! assistant 600.325/425 Declarative Methods - J. Eisner

  28. creature assistant chief name chief The “transitivity rules” let us continue as before. (In fact, they’ll let us completely merge name and chief: we don’t need to keep track of both anymore.) assistant 600.325/425 Declarative Methods - J. Eisner

  29. But does the grid method always apply?(assuming the statements given are enough to solve the problem) “Each statement simply tells you to fill a particular cell with  or ” – always true? People: Alice, Bob, Cindy Roles: Hero, Villain, Jester • Alice is not the Jester. • The Villain and the Hero got legally married in Nigeria. Let’s try it on the board and see what happens … 600.325/425 Declarative Methods - J. Eisner

  30. But does the grid method always apply?(assuming the statements given are enough to solve the problem) • Alice is not the Jester. • The Villain and the Hero got legally married in Nigeria. The problem is that the second statement didn’t tell us exactly which cells to fill in. It gave us choices. If these English statements aren’t just telling us about cells to fill in, what kinds of things are they telling us? Can we encode them formally using some little language? Then the computer can solve them … 600.325/425 Declarative Methods - J. Eisner

  31. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). 31 600.325/425 Declarative Methods - J. Eisner

  32. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans 32 600.325/425 Declarative Methods - J. Eisner

  33. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans 33 600.325/425 Declarative Methods - J. Eisner

  34. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans 34 600.325/425 Declarative Methods - J. Eisner

  35. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans 35 600.325/425 Declarative Methods - J. Eisner

  36. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans 36 600.325/425 Declarative Methods - J. Eisner

  37. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. “Truth tables” – like the multiplication table, but for booleans “F implies G” F≠G F==G if F then G F F & G F | G 37 600.325/425 Declarative Methods - J. Eisner

  38. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) Base case Build bigger formulas out of littler ones ^ ~  ~ v G F G H 0 0 1 0 1 0 0 38 600.325/425 Declarative Methods - J. Eisner 0 0 0

  39. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) ^ ~  ~ v G F G H 0 0 0 0 0 1 1 0 39 600.325/425 Declarative Methods - J. Eisner 0 0 1

  40. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) ^ ~  ~ v G F G H 0 0 0 40 0 600.325/425 Declarative Methods - J. Eisner 1

  41. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) This particular formula is satisfied (has value 1)for only one assignment: F=1, G=0, H=0 So it’s equivalent to F ^ ~G ^ ~H 0 0 0 41 0 600.325/425 Declarative Methods - J. Eisner 1

  42. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) This particular formula is satisfied (has value 1)for only one assignment: F=1, G=0, H=0 So it’s equivalent to F ^ ~G ^ ~H o t h e r w i s e 42 More concise form of truth table 600.325/425 Declarative Methods - J. Eisner

  43. Logical formulas (inductive definition) If A is a [boolean] variable, then A and ~A are “literal” formulas. If F is a formula, then so is ~F (“not F”). If F and G are formulas, then so are F ^ G, F v G, etc. So this is a formula: (~F  G) ^ ~(G v H) Given an assignment of values to the variables F, G, H, We can compute the value of the whole formula, bottom-up Just like evaluating arithmetic expressions: (-F/G)  -(F  H) This particular formula is satisfied (has value 1)for only one assignment: F=1, G=0, H=0 So it’s equivalent to F ^ ~G ^ ~H 43 600.325/425 Declarative Methods - J. Eisner Compressed version with “don’t care” values *

  44. I hope you find assignment 1 to be satisfying Satisfying assignments • An “assignment” of values to boolean variables is a choice of 0 (false) or 1 (true) for each variable • Given an assignment, can compute whether formula is satisfied (true) • Satisfiability problem: • Input: a formula • Output: a satisfying assignment to its variables, if one exists. Otherwise return “UNSAT”. 600.325/425 Declarative Methods - J. Eisner

  45. Asking for a satisfying assignment Let’s try encoding the hero-villain problem on the board: People: Alice, Bob, Cindy Roles: Hero, Villain, Jester Alice is not the Jester. The Villain and the Hero got legally married in Nigeria. “F implies G” F≠G F==G if F then G F F & G F | G 45 600.325/425 Declarative Methods - J. Eisner

  46. Satisfying assignments An “assignment” of values to boolean variables is a choice of 0 (false) or 1 (true) for each variable Given an assignment, can compute whether formula is satisfied (true) Satisfiability problem: Input: a formula Output: a satisfying assignment to its variables, if one exists. Otherwise return “UNSAT”. Traditional version: Input: a formula Output: “true” if there exists a satisfying assignment, else “false.” Why isn’t this useless in practice?? 46 600.325/425 Declarative Methods - J. Eisner

  47. no no yes yes yes yes yes yes yes yes Is there a satisfying assignment? no no no no Find a satisfying assignment A ReductionUnlocking an A..Z assignment in 26 steps returnUNSAT SAT()? now knowSAT(~A^) SAT(A^)? now knowSAT(A^~B^) now knowSAT(~A^~B^) SAT(A^B^)? SAT(~A^B^)? … … … … … now know SAT(~A^B^…^~Z^) SAT(A^B^…^Z^)? SAT(~A^B^…^Z^)? return (A,B,…,Z)=(1,1,1,…1) return (A,B,…,Z)=(0,1,1,…1) return (A,B,…,Z) =(0,1,1,…0) 47 600.325/425 Declarative Methods - J. Eisner

  48. Another LSAT Practice Problem • When the goalie has been chosen, the Smalltown Bluebirds hockey team has a starting lineup that is selected from two groups: • 1st Group: John, Dexter, Bart, Erwin • 2nd Group: Leanne, Roger, George, Marlene, Patricia • Certain requirements are always observed (for the sake of balance, cooperation, and fairness). 600.325/425 Declarative Methods - J. Eisner from http://www.testprepreview.com/modules/logicalreasoning.htm

  49. “3 of 5” here. Could you efficiently encode “13 of 26”? Another LSAT Practice Problem 1st Group: John, Dexter, Bart, Erwin 2nd Group:  Leanne, Roger, George, Marlene, Patricia • Two players are always chosen from the 1st group.Three players are always chosen from the 2nd group. • George will only start if Bart also starts. • Dexter and Bart will not start together. • If George starts, Marlene won’t start. • The 4 fastest players are: John, Bart, George and Patricia.3 of the 4 fastest players will always be chosen. • Who always starts? • If Marlene starts, what subset of first-group players starts with her? • If George starts, who must also start? (M or J, D or L, D or J, J or P, M or R) • Which of these pairs cannot start together? (ED, GJ, RJ, JB, PM) These questions are different:  not  600.325/425 Declarative Methods - J. Eisner

  50. Encoding “at least 13 of 26”(without listing all 38,754,732 subsets!) 26 original variables A … Z, plus < 262 new variables such as A-L3 • SAT formula should require that A-Z13 is true … and what else? • yadayada ^ A-Z13 ^ (A-Z13  (A-Y13 v (A-Y12 ^ Z))) ^ (A-Y13  (A-X13 v (A-X12 ^ Y))) ^ … 600.325/425 Declarative Methods - J. Eisner

More Related