2. Frank A. Krueger
* @praeclarum on Twitter
* Independent mobile app developer (8 years!)
specializing in .NET and Xamarin
- iCircuit, Calca, Continuous
* Open Source Contributor: SQLite-net,
NGraphics, Netjs, …
* Co-host of Merge Conflict podcast with
James Montemagno (http://mergeconflict.fm)
6. AGENDA
* Linked List Traversal----------------------- NO
* Fuzzy Logic
* Eval
* General Problem Solvers
* Simulated Annealing
(in no particular order)
7.
8.
9. if temp < desiredTemp
then
setFan(100%)
else
setFan(0%)
“Chatter”
These
can’t be
right
10. “Chatter” occurs when the fan jumps
from 100% to 0%
when it’s near desiredTemp
0% 100% 0% 100% 0% 100% 0%
temp
desiredTemp
fan
11. let error = desiredTemp -
temp
if error < 0° then
setFan(0%)
elif error < 10° then
setFan(10%)
elif error < 25° then
setFan(50%)
else
setFan(100%)
More Granularity Fixes All Problems
FUZZY
LOGIC
12. let error = desiredTemp -
temp
if error < 0° then
setFan(0%)
elif error < 10° then
setFan(10%)
elif error < 25° then
setFan(50%)
else
setFan(100%)
Input Classes
Outputs
13. Build a Table of Inputs and Outputs
Input | Output
(Error) | (Fan)
============+=========
TooHot | 0%
AlmostThere | 10%
AWaysToGo | 50%
JustStarted | 100%
Input Classes
Output per Class
20. FUZZY LOGIC
* Formalizes a common pattern
* Simplifies problem with classes
- Doesn’t require mathematical models
- Your intuition is usually enough
* Smooth output thanks to blending
* Scales to many inputs and many outputs
21. A COURSE IN FUZZY SYSTEMS
AND CONTROL by Li Xin-Wang
26. type Syntax =
| Number of float
| Assign of string * Syntax
| Variable of string
| Invoke of Syntax * Syntax[]
| Program of Syntax[]
distance = 101390
velocity = 1000
G = 9.81
angle = 0.5 * asin(G*distance/velocity^2)
Source Code
Syntax Tree
33. Why Learn Eval?
* Let users type math when entering numbers
* Add scripting to your app with full sandboxing
* Write Domain Specific Languages
* Write compilers in your spare time
37. I have a problem…
* I know roughly what the solution looks
like
* I know how to determine the quality of
a potential solution
* I don’t know how to find the solution
(or it takes too long)
38. * Layout Circuit Boards
- Know: Components & connections
- Want: Optimal layout
* Plan an Epic Trip
- Know: Cities you want to visit
- Want: Flight & hotel bookings
39. An algorithm to solve problems:
1. Guess at the solution
2. Randomly change the solution a bit
3. Measure the change in quality
4. If it is better, then accept it
Else, reject it
5. Good enough? DONE
6. GOTO 2
40. * Greedily looks for a “good” solution,
not the best
- Local minima, not global
“Goodish”
* General purpose
* Intuitive and simple to implement
* A bit slow because it has no knowledge
of the domain
41. An algorithm to solve problems:
1. Guess at the solution
2. Randomly change the solution a bit
3. Measure the change in quality
4. If it is better, then accept it
Else, reject it
5. Good enough? DONE
6. GOTO 2
Insight: let’s accept
bad changes!
44. An algorithm to solve problems:
1. Guess at the solution
2. Randomly change the solution a bit
3. Measure the change in quality
4. If it is better, then accept it
Else, reject it
5. GOTO 2
45. Simulated Annealing:
1. Guess at the solution
2. Start with a high temperature
3. Randomly change the solution a bit
4. Measure the change in quality (“energy”)
5. If it is better, then accept it
Else, calculate the change in energy
and the probability of that change
given the temperature
and accept if randomly chosen
6. If it’s been “awhile”, decrease temperature
7. GOTO 3
46. I want to visit London, Barcelona, Russia, and Japan:
Guess: Tokyo – Moscow – London – Barcelona
Random Change: Barcelona – Moscow – London – Tokyo
Energy = 900 ΔE = -100
Energy = 1000
Random Change: Barcelona – Moscow – London – Tokyo
Energy = 900 ΔE = -100
Random Change: Moscow – Barcelona – London – Tokyo
Energy = 1500 ΔE = 600
Random Change: Moscow – Barcelona – London – Tokyo
Energy = 1500 ΔE = 600
Boltzmann Probability (ΔE, T) = 0.1
*Roll the dice*
50. How to Solve Problems, Generally
State of the World is a collection of “truths”
Sunny
Outside
Watching a
Presentation
Bags
Unpacked
Happy
Curious
about
Twitter
Hungry
51. How to Solve Problems, Generally
Action: Pack My Bags
Remove: Bags Unpacked
Add: Bags Packed
Require: In Hotel Room
&& Bags Unpacked
Actions Remove Truths and Add New Ones
52. How to Solve Problems, Generally
Want: Bags Packed && Checked
into Flight
Problems are described by stating which truths
you want to be true
GPS
Current World State
All Possible Actions
Action 1
Action 2
Action 3
Action 4
Action …
New World State
53. General Problem Solvers Algorithm
1. Look for an Action that adds the needed state
to the world with all its requirements met
2. If one found, add it to the solution;
DONE!
3. Else, look for an Action that adds the needed
state to the world (requirements not met).
4. If one found, Solve the Sub-problem of meeting
that action’s Requirements. GOTO 1.
5. FAIL! :-(
Recursive
57. Where does the data come from?
Make Tile
Street Data
Building Data
Terrain Data
Vector
Data
Terrain
Server
Vector
Server
Route Car
Show Address
58. async Task<Tile> MakeTile ()
{
var terrain = await GetTerrain();
var vectors = await GetVectors();
var roads = GetRoads(vectors);
var buildings = GetBuildings(vectors);
return ReallyMakeTile(terrain, roads, buildings);
}
How do you handle network errors?
59. async Task<Tile> TryHarderToMakeTile ()
{
for (var i = 0; i < NumRetries; i++) {
try {
return await MakeTile();
}
catch (NetworkError e) {
// Keep trying after a little wait
await Task.Delay(1000);
}
catch {
throw;
}
}
}
I’m not impressed…
60. GPS as an Architecture
Possible Truths of the World
Vector
Terrain Network
Streets
Buildings
61. Define Single Problem Solver Actions
Action: Load Terrain
Add: Terrain
Require: Network
62. Define Single Problem Solver Actions
Action: Load Streets
Add: Streets
Require: Vector
63. Define Single Problem Solver Actions
Action: Load Vector
Add: Vector
Require: Network
64. GPS as an Architecture (Example 1)
Want: Streets && Buildings &&
Terrain
GPS
Network
All Actions
Load Vector
Load Streets
Load Buildings
Load Terrain
65. GPS as an Architecture (Example 2)
Want: Streets && Buildings &&
Terrain
GPS
Terrain && Vector
All Actions
Load Streets
Load Buildings
66. GPS as an Architecture (Example 3)
Want: Streets && Buildings &&
Terrain
GPS
Terrain
All Actions
FAIL
(No Network)
67. GPS Benefits
1. Standardized way of looking at problems
– helps you think!
2. Actions become very specialized doing
one thing very well.
3. Restartable – unlike async methods,
partially solved problems can be
continued.
4. Even the dumbest version of GPS can
help us write better software.