hyper-quicksort: energy efficient sorting via the templar ...€¦ · a framework for generative...
TRANSCRIPT
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
Hyper-quicksort: energy efficient sortingvia the Templar framework for
Template Method Hyper-heuristics
[email protected]@york.ac.uk
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
2/28
Motivation
Scalability remains an issue for program synthesis:
We don’t yet know how to generate sizeable algorithms fromscratch.
Generative approaches such as GP still work best at the scaleof expressions (though some recent promising results [6]).
Formal approaches require a strong mathematicalbackground.
. . . but human ingenuity already provides a vast repertoire ofspecialized algorithms, usually with known asymptoticbehaviour.
Given these limitations, how can we best use generativehyper-heuristics to improve upon human-designed algorithms?
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
3/28
The Template Method Pattern
The ‘Template Method’ Design Pattern [1] divides analgorithm into a fixed skeleton with one or more variant parts.
The fixed parts orchestrate the behaviour of the variant parts.
Example: Quicksort performance depends on the quality ofthe pivot, so we can treat the pivot function as a variant part:
DoubleArray q s o r t ( Doub leArray a r r ) {double p i v o t = p i vo tFn ( a r r ) ;// ˆˆˆ p i vo tFn can be v a r i e d g e n e r a t i v e l yr e t u r n q s o r t ( a r r . f i l t e r ( < p i v o t ) )
++ a r r . f i l t e r ( == p i v o t )++ q s o r t ( a r r . f i l t e r ( > p i v o t ) ) ;
}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
4/28
Template Method Hyper-heuristics [10]
So if we can express an algorithmic framework in templatemethod terms, then we can learn good implementations forthe variant parts.
By ‘good’, we mean ‘biased towards the distribution to whichthe algorithm is exposed’.
If our algorithms are metaheuristics, this means that they arenot subject to the ‘No Free Lunch’ theorem [8], since thedistribution over problem instances is biased away fromuniform by the training set.
Successfully demonstrated this approach to learn moreeffective GA selection and mutation operators [11, 9].
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
5/28
A framework for generative hyper-heuristics
Generative hyper-heuristics can be specified by:
A list of variation points describing the parts of thealgorithm to be automatically generated.
An algorithm template expressing the algorithm skeleton.The template produces a customized version of the algorithmfrom automatically-generated implementations of the variationpoints.
A fitness function to evaluate the customized algorithm.
An algorithm factory that searches the space of variationpoints to produce an optimized version of the algorithm.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
6/28
A functional description
For algorithm with function signature I → O:
VP : (I1 → O1)× (I2 → O2)× . . .× (In → On).
Template : VP → (I → O).
Fitness : (I → O)→ V .
Factory : VP × Template × Fitness → (I → O).
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
7/28
Why a Framework?
Generative HH are laborious to implement on a per-case basis, butnon-trivial to generalize:
The Factory is typically implemented via GP and is invokedrepeatedly . . .
. . . but popular GP implementations such as ECJ [3] andPushGP [7] expect to be the ‘top’ of the system . . .
. . . hence are not easy to use for generative hyper-heuristics.
Fitness of one VP depends on the other VPs, so some fiddlysoftware engineering is required to enable ‘dependencyinversion’.
Heterogeneous signatures of VPs needs special handling toretain any notion of type-safety.
To prevent overfitting, cross-validation should be built-in tothe fitness function by default.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
8/28
Interlude - higher-order functions in Java
i n t e r f a c e Fun1<Arg , Resu l t> {Re su l t app l y ( Arg arg ) ;
}i n t e r f a c e Fun2<Arg1 , Arg2 , Resu l t> {
Re su l t app l y ( Arg1 arg1 , Arg2 arg2 ) ;}
// We can then use f u n c t i o n s as pa ramete r s// and r e t u r n v a l u e s :Fun1<I n t , S t r i ng>compose ( Fun1<I n t , Double> f , Fun1<Double , S t r i ng> g ) {
r e t u r n new Fun1<I n t , S t r i ng >() {S t r i n g app l y ( I n t a rg ) {
r e t u r n g . app l y ( f . app l y ( a rg ) ) ;}
} ;}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
9/28
Core Templar classes
p u b l i c i n t e r f a c e AlgTemplate<I ,O> {p u b l i c Fun1<I ,O>makeAlg ( ProgramLi s t programs ) ;
}
p u b l i c c l a s s AlgFactory<I ,O> {AlgFac to r y ( GPConfig [ ] v a r i a t i o nP o i n t C o n f i g s ,
AlgTemplate<I ,O> t emp la t e ) { . . . }
ProgramLi s t run ( F i t n e s sCa s e s<I ,O> case s ,LossFn<O> l o s s Fn ) { . . . }
}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
10/28
Trivial example - ‘Identity’ template
Just executes the generated program for the (sole) variation point:
c l a s s I d e n t i t yTemp l a t eimplements AlgTemplate<Double , Double> {
p u b l i c Fun1<Double , Double>makeAlg ( ProgramL i s t p rogs ) {
// Wrap the VP i n a f u n c t i o n :r e t u r n new Fun1<Double , Double>() {
Double app l y ( Double a rg ) {r e t u r n progs . ge t (0 ) . e x e cu t e ( a rg ) ;
}} ;
}}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
11/28
Using Templar
The end-user only needs to do this1:
// 1 . De f i n e an AlgTemplate s u b c l a s s ( p r e v i o u s s l i d e ) .// 2 . Set up the a l go r i t hm−s p e c i f i c s :AlgTemplate<Double , Double> t emp la t e = new
I d e n t i t yTemp l a t e ( ) ;GPConfig [ ] vpCon f i g s={new Ra t i o n a l Fun c t i o nCon f i g ( ) ;}F i t n e s sC a s e s t r a i n i n g S e t = . . .F i t n e s sC a s e s t e s t S e t = . . .
// 3 . I nvoke Templar :ProgramLi s t bestVPs = Templar . t r a i nAndTes t ( template ,
vpConf ig s ,t r a i n i n g S e t , t e s t S e t ,new RMSLossFn<Double>() ) ;
p r i n t l n ( ” be s t VPs : ” + bestVPs ) ;
1These examples describe all the code you need to write.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
12/28
Next simplest example - Composition Template
c l a s s Compos i t ionTemplateimplements AlgTemplate<I n t , S t r i ng> {
Fun1<I n t , S t r i ng> makeAlg ( ProgramL i s t p rogs ) {f = new Fun1<I n t , Double>() {
Double app l y ( I n t a rg ) {r e t u r n progs . ge t (0 ) . e x e cu t e ( a rg ) ;
}} ;g = new Fun1<Double , S t r i ng >() {
S t r i n g app l y ( Double a rg ) {r e t u r n progs . ge t (1 ) . e x e cu t e ( a rg ) ;
}} ;// t h i s t emp la t e j u s t composes// the two v a r i a n t programs . . .r e t u r n compose ( f , g ) ;
}}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
13/28
HyperQuicksort
Just follow the above steps for any algorithm you wish tooptimize.
We’ll see how easy it is to create ‘Hyper-quicksort’ . . .
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
14/28
HyperQuicksort - Pivot Function
a b s t r a c t c l a s s PivotFnextends Fun2<DoubleArray , I n t g e r , Double>{
Double app l y ( DoubleArray a , I n t r e cu s i onDep th ) ;}c l a s s SedgewickPivotFn extends PivotFn {
// coun t e r s the ca se o f s o r t e d// ( or r e v e r s e−s o r t e d ) i npu tDouble app l y ( DoubleArray a , I n t r e cu s i onDep th ) {
r e t u r n median ( a . f i r s t , a [ a . l e n g t h /2 ] , a . l a s t ) ;}
}
I n t q u i c k s o r t ( Doub leArray a , P ivotFn p i vo tFn ) ;// ˆ i n s t r umen t ed to r e t u r n some measure// o f p i vo tFn f i t n e s s ( e . g . max r e c u s i o n depth )
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
15/28
HyperQuicksort - Alg Template
c l a s s Quickso r tTemp la teimplements AlgTemplate<DoubleArray , I n t> {
Fun1<DoubleArray, Int> makeAlg (ProgramList progs ) −> {PivotFn p i vo tFn = (DoubleArray a , Int r e c u r s i o nDep th )
−> {i n t p r ogRe su l t = progs [ 0 ] . e x e cu t e ( a . s i z e ,
r e c u r s i o nDep th ) ;i n t numSamples = min ( abs ( p r ogRe su l t ) , a . s i z e ) ;r e t u r n median ( randomSample ( a , numSamples ) ) ;
} ;r e t u r n (DoubleArray arg ) −> Qu i c k so r t . s o r t ( arg ,
p i vo tFn ) ;}
}
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
16/28
HyperQuicksort - Top Level
// 1 . De f i n e an AlgTemplate s u b c l a s s ( p r e v i o u s s l i d e ) .// 2 . Con f i g u r e GP to gen e r a t e p i vo tFn VP:L i s t<Var> v a r s = {Var ( ” s i z e ” ) , Var ( ” r e cu r s i o nDep th ” ) ;L i s t<Node> f uncSe t = { I f F n ( ) , LessFn ( ) ,AddFn ( ) , . . .} ;GPParams params = . . . // c r o s s o v e r , s e l e c t i o n e t cGPConfig vpCon f i g s={new GPConfig ( funcSet , va r s , params ) ;}
// 3 . I nvoke TemplarAlgTemplate<Double , Double> t emp la t e = new
Quickso r tTemp la te ( ) ;F i t n e s sC a s e s t r a i n i n g S e t = . . .F i t n e s sC a s e s t e s t S e t = . . .Templar . t r a i nAndTes t ( template , vpConf ig s , t r a i n i n g S e t ,
t e s t S e t , new RMSLossFn<Double>() ) ;
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
17/28
Wait - there’s more . . .
Manual creation of GP nodes for function sets on customsolution representations (e.g. Timetable,RoutePlan,AntTrail etc) istedious.
Following [2], Templar.FunctionSetGenerator uses reflection toautomatically build a function set from any Java object.
By this means, a hyper-heuristic for Iterated Local Search overbitstrings was up and running from scratch in under 20minutes
By following the above steps, it’s quick and easy to create atemplate for your favourite algorithm here.
All you need now is lots of CPU time . . .
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
18/28
Experiment - Setup
EpochX for GP.
Jalen [5] for power measurement.
Monitors execution time and processor utilisation to estimatepower consumption.Non-deterministic (e.g. other processes), and accuracy limitedby platform (up to nanosecond).Multiple arrays need to be sorted for each measurement (100).Oracular pivot function.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
19/28
Experiment - Pipeorgan Distribution [4]
Training set size: 70 (* 100).Testing set size: 100 (at 9 different array lengths, * 1000).
0 20 40 60 80 1000
20
40
60
80
DoubleArray index
Val
ue
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
20/28
Results
8 16 32 64 128 256 512 1024 20480.0625
0.25
1
4
16
64
256
DoubleArray size (log2 scale)
Jou
les
(log
2sc
ale)
MidSedgewickRandomHyper-quicksort
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
21/28
Results - Table
Arraysize
Middle index Sedgewick Hyper-quicksort (J)J p e J p e
8 0.191 7.46e-32 0.981 0.163 8.37e-32 0.980 0.09416 0.296 1.20e-30 0.971 0.345 1.25e-31 0.979 0.17332 0.651 8.13e-32 0.980 0.757 7.25e-32 0.981 0.41064 1.366 4.80e-33 0.990 1.145 1.68e-30 0.970 0.976
128 3.505 4.80e-33 0.990 4.034 4.14e-33 0.991 2.341256 8.175 4.14e-33 0.991 7.646 3.41e-32 0.983 6.387512 19.777 4.33e-34 0.998 21.391 3.62e-34 0.999 15.268
1024 62.961 2.52e-34 1.000 42.508 6.44e-33 0.989 33.0122048 198.438 2.52e-34 1.000 132.663 2.52e-34 1.000 70.234
Arraysize
Random indexJ p e
8 0.446 8.37e-32 0.98016 0.410 8.37e-32 0.98032 0.967 4.80e-33 0.99064 1.708 4.80e-33 0.990
128 5.221 2.52e-34 1.000256 9.269 8.87e-34 0.996512 27.685 2.52e-34 1.000
1024 41.245 3.61e-32 0.9832048 111.894 3.47e-33 0.991
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
22/28
Experiment - Conclusions
P-values (Mann-Whitney U-test) and effect sizes(Vargha-Delaney A12) clearly show Hyper-quicksort providessignificant improvement on pipeorgan distributions.
Intermediate results showed that minimal recursion doesn’talways equate to minimal power consumption, as pivotfunction becomes more demanding.
Imprecision and non-determinism of power measurementimposes time constraints on experimentation.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
23/28
Conclusion and Future Work
Algorithms can be decomposed into templates consisting of afixed skeleton and a collection of variant components.
By judicious choice of function signatures, we can usegenerative methods (GP etc) to create variant componentsthat are tuned to some target distribution.
In implementation terms, Templar makes generative HHfor any algorithm a matter of GP parameter tuning.
New methods of power consumption modelling are indevelopment. . .
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
24/28
References I
Erich Gamma, Richard Helm, Ralph Johnson, and JohnVlissides.Design patterns: elements of reusable object-oriented software.
Addison-Wesley Longman Publishing Co., Inc., Boston, MA,USA, 1995.
Simon M. Lucas.Exploiting reflection in object oriented genetic programming.In Maarten Keijzer, Una-May O’Reilly, Simon Lucas, ErnestoCosta, and Terence Soule, editors, Genetic Programming,volume 3003 of Lecture Notes in Computer Science, pages369–378. Springer Berlin Heidelberg, 2004.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
25/28
References II
Sean Luke, Liviu Panait, Gabriel Balan, and Et.ECJ 16: A Java-based Evolutionary Computation ResearchSystem, 2007.
M. Douglas McIlroy.A killer adversary for quicksort.Softw., Pract. Exper., 29(4):341–344, 1999.
Adel Noureddine, Aurelien Bourdon, Romain Rouvoy, andLionel Seinturier.Runtime monitoring of software energy hotspots.In 27th IEEE/ACM Int. Conf. on Autom. Softw. Eng. 2012,pages 160–169. IEEE, 2012.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
26/28
References III
Lee Spector, Kyle Harrington, and Thomas Helmuth.Tag-based modularity in tree-based genetic programming.In Proceedings of the 14th Annual Conference on Genetic andEvolutionary Computation, GECCO ’12, pages 815–822, NewYork, NY, USA, 2012. ACM.
Lee Spector, Jon Klein, and Maarten Keijzer.The Push3 execution stack and the evolution of control.In Hans-Georg Beyer et al, editor, GECCO 2005: Proceedingsof the 2005 conference on Genetic and evolutionarycomputation, volume 2, pages 1689–1696, Washington DC,USA, 25-29 June 2005. ACM Press.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
27/28
References IV
John Woodward and Jerry Swan.Why classifying search algorithms is essential.In 2010 International Conference on Progress in Informaticsand Computing. (PIC-2010), 2010.
John R. Woodward and Jerry Swan.The automatic generation of mutation operators for geneticalgorithms.In Proceedings of the fourteenth international conference onGenetic and evolutionary computation conference companion,GECCO Companion ’12, pages 67–74, New York, NY, USA,2012. ACM.
Introduction Templar Basic Example HyperQuicksort HyperQuicksort Experiment Conclusion References
28/28
References V
John R. Woodward and Jerry Swan.Template method hyper-heuristics.In Proceedings of the 2014 Conference Companion on Geneticand Evolutionary Computation Companion, GECCO Comp ’14,pages 1437–1438, New York, NY, USA, 2014. ACM.
John Robert Woodward and Jerry Swan.Automatically designing selection heuristics.In Proceedings of the 13th annual conference companion onGenetic and evolutionary computation, GECCO ’11, pages583–590, New York, NY, USA, 2011. ACM.