Amazing AlgorithmsFor Solving Problems in SoftwareBarry S. StahlPrincipal Engineer - AZNerds.net@bsstahl@cognitiveinheritance.comhttps://CognitiveInheritance.com |
|
Favorite Physicists
Other notables: Stephen Hawking, Edwin Hubble, Leonard Susskind, Christiaan Huygens |
Favorite Mathematicians
Other notables: Blaise Pascal, Daphne Koller, Grady Booch, Evelyn Berezin, Pascal Van Hentenryck |
|
|
08:45 - 09:00 : Intro and Modeling
09:00 - 10:30 : Dynamic Programming
10:30 - 11:00 : Break
11:00 - 11:15 : Genetic Algorithms
11:15 - 12:15 : Classification & Clustering
12:15 - 1:15 : Lunch
1:15 - 2:45 : Simplex Algorithm (CP, LP & MIP)
2:45 - 3:15 : Break
3:15 - 3:45 : Best-Path Algorithms
3:45 - 4:15 : Cost-Minimization Algorithms
4:15 - 4:30 : Training Neural Networks
4:30 - 4:45 : Q&A and Wrap-up
|
|
|
The success of a problem-solving algorithm is often determined before the algorithm even runs -- by how the problem is modeled |
|
|
|
|
|
|
|
|
|
|
|
|
| Items ($n$) | DP calculations | Brute-force calculations |
|---|---|---|
| 3 | 27 | 8 |
| 30 | 270 | 1,073,741,824 |
| 300 | 2,700 | $\approx 2.037 \times 10^{90}$ |
| 3000 | 27,000 | $\approx 1.230 \times 10^{903}$ |
|
|
DetermineDistance(s,d)
|
|
|
|
|
|
|
|
\[
score(i,j) = avg\_similarity(i..j) - 0.1 \times (chunk\_size - 1)
\]
|
|
\[
DP[i] = \max_{j \ge i} \left( score(i,j) + DP[j+1] \right)
\]
|
|
The clarity-first, object-oriented implementation of a Tokenizer is written in C#, my language of choice. I suspect it will be easy to have it translated into nearly any other programming language if that makes it easier to understand. The goal of this implementation is not speed, it is transparency. You can step through Encode and Decode to see exactly what is happening. The code is available on GitHub.
|
|
Find Solutions by Simulating Darwinian Evolution
|
|
|
|
|
|
|
|
|
|
Labeled examples show the model what the right answer looks like
Features are the details the model is allowed to use
Target classes are the labels the model is allowed to predict
Training searches for a pattern from features to target class
Prediction applies that pattern to an unlabeled input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: For normalized vectors, cosine similarity is the same as the dot-product |
|
| Feature | Example |
|---|---|
| Synonym | "Happy" is closer to "Joyful" than to "Sad" |
| Language | "The Queen" is very close to "La Reina" |
| Idiom | "He kicked the bucket" is closer to "He died" than to "He kicked the ball" |
| Sarcasm | "Well, look who's on time" is closer to "Actually Late" than "Actually Early" |
| Homonym | "Bark" (dog sound) is closer to "Howl" than to "Bark" (tree layer) |
| Collocation | "Fast food" is closer to "Junk food" than to "Fast car" |
| Proverb | "The early bird catches the worm" is closer to "Success comes to those who prepare well and put in effort" than to "A bird in the hand is worth two in the bush" |
| Metaphor | "Time is money" is closer to "Don't waste your time" than to "Time flies" |
| Simile | "He is as brave as a lion" is closer to "He is very courageous" than to "He is a lion" |
|
|
|
|
|
|
|
|
|
|
|
|
|
As an entity, constraints do 2 things
Infeasibility
Objective Function
Specified as a goal relative to an equation
One objective per Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Constraint Programming is about narrowing choices
LP and MIP build on the same idea
|
|
|
|
|
Clay Constraint
Glaze Constraint
|
|
|
|
|
|
|
|
| Room 1 | Room 2 | Room 3 | |
|---|---|---|---|
| Slot 1 | Session 1 | Session 2 | Session 3 |
| Slot 2 | Session 4 | Session 5 | Session 6 |
| Slot 3 | Session 7 | Session 8 | Session 9 |
| Slot 4 | Session 10 | Open | Open |
| Room 1 | Room 2 | Room 3 | |
|---|---|---|---|
| Slot 1 | Session 1 | Session 2 | Session 3 |
| Slot 2 | Session 4 | Session 5 | Session 6 |
| Slot 3 | Session 7 | Session 8 | Session 9 |
| Slot 4 | Session 10 | Open | Open |
| Room 1 | Room 2 | Room 3 | |
|---|---|---|---|
| Slot 1 | Session 1 | Session 2 | Session 3 |
| Slot 2 | Session 4 | Session 5 | Session 6 |
| Slot 3 | Session 7 | Session 8 | Session 9 |
| Slot 4 | Session 10 | Open | Open |
| Session 1 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 1 | 0 |
| Slot 4 | 0 | 0 | 0 |
| Session 2 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 1 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 0 | 0 |
| Slot 4 | 0 | 0 | 0 |
| Session 3 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Session 1 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 1 | 0 |
| Slot 4 | 0 | 0 | 0 |
| Session 1 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 1 | 0 |
| Slot 4 | 0 | 0 | 0 |
| Session 1 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 1 | 0 |
| Slot 4 | 0 | 0 | 0 |
Create a score that improves with the desirability of the solution
| Session 1 | Room 1 | Room 2 | Room 3 |
|---|---|---|---|
| Slot 1 | 0 | 0 | 0 |
| Slot 2 | 0 | 0 | 0 |
| Slot 3 | 0 | 1 | 0 |
| Slot 4 | 0 | 0 | 0 |
Maximize # of sessions in the same room with Sessions 2 & 4 (a track)
|
|
We don't need to implement Simplex ourselves
|
|
Variable Definitions
Implementation
var xS = solver.MakeIntVar(0.0, maxSmall, "xS");var xL = solver.MakeIntVar(0.0, maxLarge, "xL");Clay Constraint Definition
Similar for Glaze Constraint
Implementation
var cClay = solver.MakeConstraint(0.0, claySupply);cClay.SetCoefficient($x_S$, 1);cClay.SetCoefficient($x_L$, 4);\(\max \; 3 x_S + 9 x_L\)
var obj = solver.Objective();obj.SetCoefficient($x_S$, 3);obj.SetCoefficient($x_L$, 9);obj.SetMaximization();int resultStatus = solver.Solve();var xSmall = xS.SolutionValue();var xLarge = xL.SolutionValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize n fireflies to random positions
loop maxEpochs times
for each firefly i
for each firefly j
if intensity(i) < intensity(j)
compute attractiveness
move firefly(i) toward firefly(j)
update firefly(i) intensity
end for
end for
sort fireflies
end loop
return best position found
initialize the amoeba with n (size) locations
loop maxEpochs times
calculate new possible solutions
contracted - midway between centroid and worst
reflected - contracted point reflected across centroid
expanded - beyond reflected point by a constant factor
if any solution is better than the current
replace worst value with best value from new solution
else
shrink (multiple contract) all lesser nodes toward the best
increment epoch count
end loop
return best position found
|
|
|
|
|
|
|
|
The weight (m) often has a greater effect on the error than the bias (b) |
|
|
|
|
...we've invented a fantastic array of tricks and gimmicks for putting together the numbers, without actually doing it. We don't actually [apply \(Y = mX + b\) for every neuron] We do it by the tricks of mathematics, and that's all. So, we're not going to worry about that. You don't have to know about [Linear Algebra]. All you have to know is what it is, tricky ways of doing something which would be laborious otherwise.
With apologies to Professor Feynman, who was talking about the tricks of Calculus as applied to Physics, not the tricks of Linear Algebra as applied to Machine Learning.
Predict the unknown values in a linear equation
|
|
|
|
Understand the Problem Deeply
Clarify inputs, outputs, constraints, and goals
Is optimality required? Can constraints be relaxed?
Identify if it can be broken into reusable parts?
Classify the Problem Type
Search? Optimization? Graph traversal? Dynamic programming candidate?
Are brute-force or exponential-time solutions feasible?
What tools do we have that can help? Can we buy vs build?
Implement and Test
Try a naive solution 1st
Use test cases to validate correctness and performance
Optimize as needed / Start over if necessary
|
|
|
|
Would you like to try to model a problem from one of your domains? |