And, Or, nd, Or, Not tant logical operations are and , • The most important logi or , and not . x and y is True only if ue only if both x and y are True. • x or y is True only if ue only if either x or y are True. • • not x is True only if x ue only if x is False. • A lot like their English m nglish meanings, but unambiguous.
And, Or, nd, Or, Not A not A A B A or B A B A and B False True False False False False F False False True False False True False False True True True F False True True False False True True True True True True • Order of operations: Parenthes : Parentheses, not, and, or
Relating And/ ng And/Or/Not • Note: not (not x ) = x • DeMorgan’s Law : not fl : not flips ands and ors • not ( x and y ) = (not (not x ) or (not y ) • not ( x or y ) = (not x ) and ( ) and (not y )
DeMorgan’s Law eMorgan’s Law Example • x : Car is registered. • y : Car displays a valid R alid Rutgers permit. • accepted = x and y • not ( accepted ) if • either Car is not regist registered (not x ) • or Car does not displa display a valid permit (not y) • not (x and y) = (not x) or t x) or (not y)
More Exampl e Examples • x = True, y = False, z = F = False • not x = ? not z = ? • x and y = ? y or z = ? • y or ( x and not z ) = ?
Implementing ementing Logic • Clearly, our brains can handl ns can handle these sorts of expressions. • But, can we automate them automate them?
Implementing ementing Logic • Clearly, our brains can handl ns can handle these sorts of expressions. • But, can we automate them automate them? • Yes, obviously, but let’s , but let’s start with a really simple way to do it befor to do it before we move on to fancier stuff.
Simple C mple Circuit A = True Switch A is either tOn = True lightOn = on or off making the light either on or off: lightOn=A . A = False Symbols: battery False lightOn = switch bulb ground (completes circuit) http://scratch.mit.edu/projects/cs105/35907 105/35907
Multiple Sw iple Switches A = False Switches A and B are wired in parallel : either will B = True light the bulb. http://scratch.m tch.mit.edu/pr Switches C and D ojects/cs105/ s105/35909 are wired in series : C = True D = False both are needed to light the bulb. http://scratch.mit.edu/pr ojects/cs105/35905
Multiple C tiple Circuits A light1 = ? B light2 = ? Special switches allow a singl a single mechanical switch to control two circuits ircuits simultaneously.
Multiple C tiple Circuits A B light1 A F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T B ligh light2 = ? A B light2 F F F F T F T F F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T T B ligh light2 = ? A B light2 F F F F T F T F F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T T B ligh light2 = ? A B light2 F F F F T F T F F T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T T B ligh light2 = ? A B light2 F F F F T F T F F T T T
Multiple C tiple Circuits A B light1 A F F F F T T T F T light1 = ? T T T B ligh light2 = ? A B light2 F F F F T F T F F T T T
Multiple C tiple Circuits A light1 = A or B B light2 = ? ligh A B light2 F F F F T F T F F T T T
Multiple C tiple Circuits A light1 = A or B B light2 = A and B
miniNim miniN • There’s a pile of objects of objects, say 10. • On her turn, a player can tak ayer can take away either one or two objects. • Players alternate. • The player to take the l e the last object wins.
Ideas? Ideas? • The Strategy?
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5-object version. • We’ll design a strategy for the computer “C” to beat the beat the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. • We’ll design a strategy for the computer “C” to beat the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the ?? computer “C” to beat the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the C takeOne computer “C” to beat the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left the user “U”.
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left the user “U”. takeOne takeTwo
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left the user “U”. takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the ?? computer “C” to beat 3 left the user “U”. takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the ?? computer “C” to beat 3 left the user “U”. takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the C takeOne computer “C” to beat 3 left the user “U”. takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left 2 left the user “U”. takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left 2 left the user “U”. takeOne takeTwo takeOne takeTwo C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left 2 left the user “U”. takeOne takeTwo takeOne takeTwo C C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left 2 left the user “U”. takeOne takeTwo takeOne takeTwo C C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering the 5- 5 left object version. takeOne takeTwo • We’ll design a strategy for the computer “C” to beat 3 left 2 left the user “U”. takeOne takeTwo takeOne takeTwo C U C C
Nim5Bot: Game ot: Game Tree • Let’s start by considering 5 left the 5-object version. • We’ll design a strategy for takeOne takeTwo the computer “C” to beat the beat the user “U”. 3 left 2 left takeOne takeTwo takeOne takeTwo C C C U
Backwards I ards Induction • There’s a powerful idea ful idea here: • Many decision making making problems can be solved optimally by reas y by reasoning backwards from the end of the gam the end of the game. • We know how to win to win from 1 or 2. • We use this to win from to win from 4 or 5. • We use this to win from to win from 7 or 8. • Chess: removed pieces pieces don’t return.
Further Consi her Considerations • To win miniNim: if poss if possible, remove pieces to leave opponent with a m ith a multiple of 3. • Why does it work? W ork? We win if opponent has 3; if opponent has a m has a multiple of 3, can leave her with next smaller aller multiple of 3. • What if goal is to not take the last object? • What if we can take 1, 2, or take 1, 2, or 3 objects per round? 2 or 3? 1 or 3? Is 3? 1 or 3? Is there a general rule?
Complete Ni ete Nim5 Logic 5 left • fiveLeft = True • threeLeft = fiveLeft and takeOne One 1 takeOne 1 takeTwo 1 twoLeft = fiveLeft and takeTwo 1 • C-Win = (threeLeft and (takeOne • One 2 or takeTwo 2 )) or (twoLeft and 3 left 2 left takeOne 2 ) U-Win = twoLeft and takeTwo 2 • takeOne 2 takeTwo 2 takeOne 2 takeTwo 2 C C C U
Nim5 Ci m5 Circuit fiveLeft threeLeft http://scratch.mit.edu/pro jects/cs105/35736 takeTwo 2 U-Win takeOne 1 takeTwo 1 takeOne 2 C-Win tw twoLeft
What a Headache! a Headache! • Encoding Nim10 with s 10 with switches is a nightmare. Why? • Since some switches itches are used in multiple places, needs more than a doubl ore than a double-throw switch. • Since values are reus are reused, hard to keep track of different circuits. • Appears to need a separ to need a separate circuit for each output: Gets too com too complex too fast.
Bits, Inside nside and Out Inputs: Switches Outputs: Lights You win! takeOne 1 takeTwo 1 takeOne 2 takeTwo 2 I win! Nim Player
Bits, Inside nside and Out Inputs: Switches Outputs: Lights Inside: Logi de: Logic A = False light1On = False AND OR AND OR B = True AND OR AND OR light2On = True NOT NOT OT
Need a New a New Approach • Let’s consider an alternate w an alternate way of building “and” and “or” logic. • Makes simple things things more complex. • Makes complex things things much simpler! • That’s a tradeoff we can adeoff we can deal with.
The Magi he Magic Box
The Magi he Magic Box
The Magi he Magic Box
The Magi he Magic Box
The Magi he Magic Box
The Magi he Magic Box
The Magi he Magic Box
How does i does it Work? A lightOn = ? ?
Recommend
More recommend