Goal Optimizations
The user can explicitly turn any combination of these procedures on or off in achieving a certain goal.
> L1 satisfy sort using quick-sort.
> L1 satisfy sort using quick-sort broken-sort.
More realistically, we like to dynamically decide the mixture of procedures to use to accomplish the task at hand. JOHN allows optimization definitions for goal to achieve this.
goal-optimization AList sort use swap-items if its items almost-sorted = yes. goal-optimization AList sort use pop-min if its items almost-sorted = no.
> make AList L1 [ 3, 1, 4, 5 ] []. ok. > L1 satisfy sort by L1. looking 1 time unit ahead (1 possible worlds) satisfied at time: 1 satisfied by: [ L1 swap-items ]
> make AList L2 [ 4, 3, 5, 1 ] []. > L2 satisfy sort by L2. looking 1 time unit ahead (1 possible worlds) looking 2 time unit ahead (1 possible worlds) looking 3 time unit ahead (1 possible worlds) looking 4 time unit ahead (1 possible worlds) satisfied at time: 4 satisfied by: [ L2 pop-min , L2 pop-min , L2 pop-min , L2 pop-min ]
It is also possible for optimizations to choose a mixture of procedures, as long as those procedures can be used collaboratively to get to a goal state. In the example below, we are trying to get the first 10 numbers in the Fibonacci sequence.
We like to use the nice mathematical formula Fib(n) = Fib(n-1) + Fib(n-2) , for all n > 1 as long as the numbers are small, but this is a rather expensive calculation and after a while (say after n>5) we like to use the iterative method to get the next fib number in the sequence, by simply adding the last two items in the sequence to get the next Fibonacci number. We call the two procedures slow-fib and fast-fib.
goal-optimization Calculator fibonacci-sequence use fast-fib if its answer size > 5. goal-optimization Calculator fibonacci-sequence use slow-fib if its answer size <= 5.
> Calc answer. [ 1, 1 ] > Calc satisfy fibonacci-sequence 10. satisfied at time: 9 satisfied by: [ Calc slow-fib , Calc slow-fib , Calc slow-fib , Calc slow-fib , Calc fast-fib , Calc fast-fib , Calc fast-fib , Calc fast-fib , Calc fast-fib ] > Calc answer. [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]