lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

index.md (13660B)


      1 +++
      2 title = "Logical agents"
      3 template = 'page-math.html'
      4 +++
      5 # Logical agents
      6 
      7 ## What is logic
      8 logic: generic method to deal with partial/imperfect/implicit information
      9 
     10 we need:
     11 * syntax to write statement about rules & knowledge of the game (a language)
     12 * semantics to say what legal expressions mean, the meaning of each sentence with respect to interpretations
     13 * calculus for how to determine meaning for legal expressions
     14 
     15 knowledge-based/logical agents must be able to:
     16 * represent states & actions
     17 * incorporate new percepts, update internal representation of world
     18 * deduce hidden properties of the world & appropriate actions
     19 
     20 online/exploratory search: go to position, evaluate all options, possibly look ahead. have to re-evaluate current position.
     21 
     22 ## Syntax
     23 ### Propositional logic (PL)
     24 assumes world contains facts
     25 
     26 uses proposition symbols to state these facts.
     27 
     28 pros:
     29 * declarative
     30 * allows partial/disjunctive/negated information
     31 * is compositional
     32 * meaning of statements is context-independent
     33 
     34 cons:
     35 * very limited expressive power
     36 
     37 ### First order logic (FOL)
     38 an extension of propositional logic.
     39 
     40 allows variables to range over atomic symbols in the domain.
     41 
     42 assumes world contains:
     43 * objects: people, houses, colors, baseball games, etc.
     44 * relations: red, round, prime, brother of, comes between, bigger than, etc.
     45 * functions: father of, best friend, one more than, plus, etc.
     46 
     47 #### Basic elements:
     48 - Constants: KingJohn, 2, UCB, ...
     49 - Predicates: Brother, >, ...
     50 - Functions: Sqrt, LeftLegOf, ...
     51 - Variables: x, y, ...
     52 - Connectives: ∧, ∨, ¬, ⇒, ⇔
     53 
     54 #### Sentences
     55 ```
     56 Atomic sentence = predicate (term_1,..., term_n)
     57                   or term_1 = term_2
     58 Term = function(term_1,..., term_n)
     59       or constant
     60       or variable
     61 ```
     62 
     63 Complex sentences are made from atomic sentences using connectives.
     64 
     65 #### Quantification
     66 ##### Universal quantification
     67 ∀ <variables> <sentence>
     68 
     69 ∀x P is true in a model _m_ iff P is true with x being each possible object in the model
     70 
     71 (you can roughly translate that to conjunctions)
     72 
     73 typically used with ⇒
     74 
     75 *CARE:* ∀x ∀y ≠ ∀y ∀x
     76 
     77 ##### Existential quantification
     78 ∃ <variables> <sentence>
     79 
     80 ∃x P is true in a model _m_ iff P is true with x being some possible object in the model
     81 
     82 (you can roughly translate that to disjunction of instantiations of P)
     83 
     84 typically used with ∧
     85 
     86 watch out, if you use it with ⇒, it works even if the LHS is false!
     87 
     88 *CARE*: ∃x ∃y ≠ ∃y ∃x
     89 
     90 ##### Quantifier Duality
     91 each quantifier can be expressed in terms of the other
     92 
     93 e.g. these are the same:
     94 * ∀x Likes(x, IceCream) -- "everyone likes ice cream"
     95 * ¬∃x ¬Likes(x, IceCream) -- "there is nobody who doesn't like ice cream"
     96 
     97 #### Decidability vs undecidability
     98 undecidability
     99 * Turing machine can calculate everything that can be calculated
    100 * halting problem: $K := { (i,x) | \text{program i halts when run on input x})$
    101 
    102 decidability
    103 * validity of FOL is not decidable (but semi-decidable)
    104 * if a theorem is logically entailed by an axiom, you can prove that it is.
    105 * if not, you can't necessarily prove that it's not (because you can continue with your proof infinitely).
    106 
    107 #### Knowledge engineering in FOL
    108 1. Identify the task
    109 2. Assemble relevant knowledge
    110 3. Decide on vocabulary of predicates, functions, and constants
    111 4. Encode general knowledge about the domain (terms that we want to use)
    112 5. Encode description of the specific problem instance
    113 6. Pose queries to the inference procedure and get answers
    114 
    115 ### Choice of formalisms
    116 first-order logic: represents knowledge
    117 
    118 propositional logic: used for reasoning ("propositionalisation")
    119 
    120 then use reasoner to check for entailment of propositional logic knowledge base an decision query
    121 * Davis Putnam (DPLL) algorithm
    122 * formulas have to be in clause normal form (CNF)
    123 * calculus is proof by refutation:
    124 * DPLL determines satisfiability of a KB
    125 * entailment of KB |= a by "refutation":
    126     * KB |= a if KB ∩ {~a} is unsatisfiable
    127     * assume the opposite and prove it's impossible
    128 
    129 ### Propositionalising FOL
    130 #### Reduction to propositional inference
    131 every FOL KB can be propositionalised so as to preserve entailment
    132 
    133 if a sentence α is entailed by an FOL KB, it is entailed by a _finite_ subset of the propositionalised KB
    134 
    135 #### Universal instantiation (UI):
    136 every instantiation of a universally quantified sentence is entailed by it.
    137 
    138 ![Universal instantiation](univ-instant.png)
    139 
    140 example:
    141 ```
    142 ∀x King(x) ∧ Greedy(x) ⇒ Evil(x)
    143 King(John) ∧ Greedy(John) ⇒ Evil(John)
    144 etc.
    145 ```
    146 
    147 #### Existential instantiation (EI):
    148 ![Existential instantiation](exist-instant.png)
    149 
    150 example:
    151 ```
    152 ∃x Crown(x) ∧ OnHead(x,John)
    153 Crown(C_1) ∧ OnHead(C_1, John)
    154 ```
    155 
    156 #### Applying in Schnapsen - Strategies (examples)
    157 ##### Play Jack
    158 
    159 check whether card is a jack:
    160 
    161 ```
    162 KB |= PlayJack(x) ?
    163 ```
    164 
    165 represent strategy:
    166 
    167 ```
    168 ∀x PlayJack(x) ⇔ Jack(x)
    169 ```
    170 
    171 represent game information:
    172 
    173 ```
    174 KB = {Jack(4), Jack(0), Jack(14), Jack(19)}
    175 ```
    176 
    177 ##### Play cheap
    178 only play Jacks: check whether card is cheap
    179 
    180 ```
    181 KB |= PlayCheap(x) ?
    182 ```
    183 
    184 represent strategy:
    185 
    186 ```
    187 ∀x PlayCheap(x) ⇔ Jack(x) ∨ Queen(x) ∨ King(x)
    188 ```
    189 
    190 represent game information:
    191 
    192 ```
    193 KB = {Jack(4), Jack(9), Jack(14), Jack(19), Queen(5), ...}
    194 ```
    195 
    196 ##### Play trump marriage
    197 ```
    198 TrumpMarriage(x) ⇔ Q(x) & Trump(x) & ∃y: SameColor(x,y) & K(y) & MyCard(y)
    199 SameColor(x,y) ⇔ (C(x) & C(y)) ∨ (D(x) & D(y)) ∨ (H(x) & H(y)) ∨ (S(x) & S(y))
    200 ```
    201 
    202 ## Semantics
    203 ### Interpretations & Models
    204 interpretation: assignment of meaning to symbols of formal language
    205 
    206 model: interpretation that satisfies defining axioms of knowledge base
    207 
    208 _m_ is a model of a sentence _α_ if _α_ holds in _m_.
    209 
    210 M(a) is the set of all models of a.
    211 
    212 each model specifies true/false for each proposition symbol (∧, ∨, ¬, ⇒, ⇐, ⇔)
    213 
    214 ### Entailment
    215 the knowledge base (KB) entails _α_: _α_ follows from the information in the knowledge base (KB |= _α_)
    216 
    217 KB entails _α_ iff _α_ holds in all worlds where KB is true.
    218 
    219 a knowledge base is the rules + observations.
    220 
    221 a sentence is:
    222 * entailed by KB iff α holds in all models of KB
    223 * valid if it is true in all models
    224 * satisfiable  if it is true in some model
    225 * unsatisfiable if it is true in no models
    226 
    227 two statements are logically equivalent if they are true in same set of models:
    228 
    229 α ≡ β iff α |= β and β |= α
    230 
    231 ### Truth
    232 sentences are true with respect to model and interpretation.
    233 
    234 model contains objects and relations among them
    235 
    236 interpretation specifies referents for:
    237 * constant symbols -- objects
    238 * predicate symbols -- relations
    239 * function symbols -- functional relations
    240 
    241 an atomic sentence $predicate(term_1, ..., term_n)$ is true
    242 iff the objects referred to by $term_1,..., term_n$
    243 are in the relation referred to by $predicate$
    244 
    245 ### Validity
    246 valid if it is true in all models.
    247 
    248 e.g. True, A ∨ ¬A, A ⇒ A, (A ∧ (A e.g. True, A ∨ ⇒ B)) ⇒ B)
    249 
    250 ### Satisfiability
    251 - satisfiable if it is true in _some_ model
    252 - unsatisfiable if it is true in _no_ models
    253 
    254 ## Calculus (algorithms for inference)
    255 ### Properties of inference
    256 sound: if an algorithm $|-$ only derives entailed sentences.
    257   i.e. if KB $|-$ α also KB |= α
    258 
    259 complete: if an algorithm derives any sentence that is entailed.
    260   i.e. KB |= α implies KB |- α
    261 
    262 a calculus terminates if it finds entailed sentences in finite time.
    263 
    264 a logic is _decidable_ if there is _sound and complete_ calculus that _terminates_.
    265 
    266 ### Proof methods
    267 1. Model checking and search
    268   * truth table enumeration (exponential in n)
    269   * improved backtracking (DPLL)
    270   * heuristics for choosing right order
    271 2. application of inference rules
    272   * sound generation of new sentences from old
    273   * proof = sequence of rule applications (actions)
    274 
    275 #### Model checking & search
    276 ##### Truth Tables for inference
    277 enumerate interpretations and check that where KB is true, α is true.
    278 
    279 | $fact_1$ | $fact_2$ | $fact_3$ | $KB$   | $α$    |
    280 |----------|----------|----------|--------|--------|
    281 | false    | false    | false    | false  | true   |
    282 | false    | false    | false    | false  | true   |
    283 | false    | true     | false    | _true_ | _true_ |
    284 
    285 algorithm:
    286 
    287 ```
    288 for (m in truth assignments) {
    289   if (m makes F true) return "satisfiable"
    290 }
    291 return "unsatisfiable"
    292 ```
    293 
    294 ##### Effective proofs by model checking
    295 
    296 Clever search (depth first, redundancy, heuristics)
    297 
    298 Two families of efficient algorithms for propositional inference based on model checking
    299 * complete backtracking search algorithms -- DPLL (Davis, Putnam, Logemann, Loveland)
    300 * incomplete local search algorithm (WalkSAT algorithm)
    301 
    302 ##### Clause Normal Form (CNF)
    303 
    304 memo technique: the C in CNF for _conjunction_ normal form
    305 
    306 A PL formula is in CNF if it is a conjunction of disjunctions of literals.
    307   * e.g.: {{a,b}, {~a, c}, {~b, c}}
    308   * equivalent to (a ∨ b) ∧ (~ a ∨ c) ∧ (~ b ∨ c)
    309 
    310 calculating CNF:
    311 1. Remove implications:
    312   * (p ⇔ q) to ((p ⇒ q) ∧ (q ⇒ p))
    313   * (p → q) to (¬ p ∨ q)
    314 2. Move negations inward:
    315   * ¬ (p ∨ q) to (¬ p ∧ ¬ q)
    316   * ¬ (p ∧ q) to (¬ p ∨ ¬ q)
    317 3. Move conjunctions outward:
    318   * (r ∨ (p ∧ q)) to ((r ∨ p) ∧ (r ∨ q))
    319 4. Split up conjunctive clauses:
    320   * ( (p1 ∨ p2) ∧ (q1 ∨ q2) ) to (p1 ∨ p2), (q1 ∨ q2)
    321 
    322 ##### DPLL algorithm
    323 
    324 when you have CNF, you can run the DPLL algorithm. determines if propositional logic sentence in CNF is satisfiable.
    325 
    326 returns true if F is satisfiable, false otherwise.
    327 
    328 basically assign values until contradiction, then backtrack.
    329 
    330 Improving DPLL:
    331 * if a literal in a disjunction clause is true, the clause is true
    332 * if a literal in a disjunction clause is false, the literal can be removed
    333 * if a clause is empty, it is false
    334 * a unit literal has to be true
    335 * a pure literal (only appears non-negated) has to be true
    336 
    337 the algorithm:
    338 
    339 ```
    340 dpll (F, literal) {
    341   remove clauses containing literal
    342   shorten clauses containing ¬literal
    343   if (F contains no clauses)
    344     return true
    345   if (F contains empty clause)
    346     return false
    347   if (F contains a unit or pure literal)
    348     return dpll(F, literal)
    349 
    350   choose P in F
    351   if (dpll(F, ¬P))
    352     return true
    353 
    354   return dpll(F, P)
    355 }
    356 ```
    357 
    358 ###### Heuristic search in DPLL
    359 
    360 used in DPLL to select proposition for branching
    361 
    362 idea: identify most constrained variable, likely to create many unit clauses
    363 
    364 MOM's heuristic: most occurrences in clauses of minimum length
    365 
    366 why is it better than truth table enumeration?
    367   * early termination: clause is true if any literal is true. sentence is false if any clause is false.
    368   * pure symbol heuristic: always appears with the same sign in all clauses, has to be true
    369   * unit clause heuristic: only one literal in the clause, so it must be true
    370 
    371 proving entailment KB |= a by refutation:
    372   1. translate KB into CNF to get cnf(KB)
    373   2. translate ~a into CNF to get cnf(~a)
    374   3. add cnf(~a) to cnf(KB)
    375   4. apply DPLL until either satisfiable (model is found) or unsatisfiable (search exhausted)
    376   5. if satisfiable, not entailed. otherwise, entailed.
    377 
    378 ##### Satisfiability modulo theory
    379 
    380 Boolean satisfiability (SAT): is there an assignment to the $p_1, p_2, ..., p_n$ variables such that $\phi$ evaluates to 1?
    381 
    382 ![Boolean satisfiability diagram](boolean-satisfiability.png)
    383 
    384 SAT vs SMT:
    385 * SMT (satisfiability modulo theories) extend SAT solving by adding extensions.
    386 * SMT solver can solve SAT problem, but not vice-versa.
    387 * SMT is used in analog circuit verification, RTL, verification, and card games.
    388 
    389 SMT theories:
    390 * real or integer arithmetic
    391 * equality and uninterpreted functions
    392 * bit vectors and arrays
    393 * properties:
    394   * decidable: an effective procedure exists to determine if formula is member of theory T
    395   * often quantifier-free
    396 * core theory:
    397   * type boolean
    398   * constants {TRUE, FALSE}
    399   * functions {AND, OR, XOR, =>}
    400 * integer theory:
    401   * type int
    402   * all numerals are int constants
    403   * functions {+, -, x, mod, div, abs}
    404 * reals theory:
    405   * type real
    406   * functions {+, -, x, /, <, >}
    407 
    408 #### Rule-based reasoning
    409 ##### Inference rules
    410 inference rule: logical form consisting of function taking premises, analyzing their syntax, and returning one or more conclusions
    411 
    412 Modens Ponens: $\frac{\alpha\implies\beta,\;\alpha}{\beta}$
    413 
    414 And-elimination: $\frac{\alpha\land\beta}{\alpha}$
    415 
    416 logical equivalences used as rules: $\frac{\alpha\iff\beta}{(\alpha\implies\beta)\land(\beta\implies\alpha)}$
    417 
    418 all logical equivalence rewriting rules:
    419 
    420 ![Rewriting rules for logic](logical-rewriting-rules.png)
    421 
    422 ##### Searching for proofs
    423 Finding proofs is like finding solutions to search problems.
    424 
    425 monotonicity: set of entailed sentences can only increase as info is added to the knowledge base.
    426 * for any sentence α and β,
    427 * if KB |= α, then KB ∧ β |= α
    428 
    429 ##### Forward and backward chaining
    430 FC is data-driven, automatic, unconscious:
    431 * derive all facts entailed by the KB
    432 * may do lots of work irrelevant to the goal
    433 
    434 BC is goal-driven, appropriate for problem-solving
    435 * specific fact entailed by the KB
    436 * complexity of BC can be much less than linear in size of KB
    437 
    438 ##### Resolution
    439 a rule is sound if its conclusion is evaluated to true whenever the premise is evaluated to true.
    440 
    441 can be shown to be sound using truth table:
    442 
    443 ![Sound rules for inference](sound-rules-inference.png)
    444 
    445 properties resolution:
    446 * resolution rule is sound
    447 * resolution rule is complete (on its own) for formulas in CNF
    448 * resolution can only decide satisfiability
    449 
    450 algorithm (again proof by refutation):
    451 1. Convert KB ∧ ¬ α into CNF
    452 2. Apply resolution rule to resulting clauses
    453 3. Continue until:
    454   a) no new clauses can be added, hence α does not entail β
    455   b) two clauses resolve to entail empty clause, hence α entails β
    456