<s> import abc <EOL> import logging <EOL> from fn import _ <EOL> import opentuner <EOL> from opentuner . resultsdb . models import * <EOL> log = logging . getLogger ( __name__ ) <EOL> class SearchObjective ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> @ abc . abstractmethod <EOL> def result_order_by_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> @ abc . abstractmethod <EOL> def result_compare ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def config_compare ( self , config1 , config2 ) : <EOL> """<STR_LIT>""" <EOL> return self . result_compare ( self . driver . results_query ( config = config1 ) . one ( ) , <EOL> self . driver . results_query ( config = config2 ) . one ( ) ) <EOL> @ abc . abstractmethod <EOL> def result_relative ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def config_relative ( self , config1 , config2 ) : <EOL> """<STR_LIT>""" <EOL> return self . result_relative ( self . driver . results_query ( config = config1 ) . one ( ) , <EOL> self . driver . results_query ( config = config2 ) . one ( ) ) <EOL> def __init__ ( self ) : <EOL> self . driver = None <EOL> def set_driver ( self , driver ) : <EOL> self . driver = driver <EOL> def result_order_by ( self , q ) : <EOL> return q . order_by ( * self . result_order_by_terms ( ) ) <EOL> def compare ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( a , Configuration ) : <EOL> return self . config_compare ( a , b ) <EOL> if isinstance ( a , Result ) : <EOL> return self . result_compare ( a , b ) <EOL> assert False <EOL> def relative ( self , a , b ) : <EOL> if isinstance ( a , Configuration ) : <EOL> return self . config_relative ( a , b ) <EOL> if isinstance ( a , Result ) : <EOL> return self . result_relative ( a , b ) <EOL> assert None <EOL> def lt ( self , a , b ) : <EOL> return self . compare ( a , b ) < <NUM_LIT:0> <EOL> def lte ( self , a , b ) : <EOL> return self . compare ( a , b ) <= <NUM_LIT:0> <EOL> def gt ( self , a , b ) : <EOL> return self . compare ( a , b ) > <NUM_LIT:0> <EOL> def gte ( self , a , b ) : <EOL> return self . compare ( a , b ) >= <NUM_LIT:0> <EOL> def min ( self , * l ) : <EOL> if len ( l ) == <NUM_LIT:1> : <EOL> l = l [ <NUM_LIT:0> ] <EOL> rv = l [ <NUM_LIT:0> ] <EOL> for i in l [ <NUM_LIT:1> : ] : <EOL> if self . lt ( i , rv ) : <EOL> rv = i <EOL> return rv <EOL> def max ( self , * l ) : <EOL> if len ( l ) == <NUM_LIT:1> : <EOL> l = l [ <NUM_LIT:0> ] <EOL> rv = l [ <NUM_LIT:0> ] <EOL> for i in l [ <NUM_LIT:1> : ] : <EOL> if self . gt ( i , rv ) : <EOL> rv = i <EOL> return rv <EOL> def limit_from_config ( self , config ) : <EOL> """<STR_LIT>""" <EOL> results = self . driver . results_query ( config = config ) <EOL> if results . count ( ) == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return max ( map ( _ . time , self . driver . results_query ( config = config ) ) ) <EOL> def project_compare ( self , a1 , a2 , b1 , b2 , factor = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> a3 = Result ( ) <EOL> b3 = Result ( ) <EOL> a3 . time = _project ( a1 . time , a2 . time , factor ) <EOL> a3 . accuracy = _project ( a1 . accuracy , a2 . accuracy , factor ) <EOL> a3 . energy = _project ( a1 . energy , a2 . energy , factor ) <EOL> a3 . confidence = _project ( a1 . confidence , a2 . confidence , factor ) <EOL> return self . result_compare ( a3 , b3 ) <EOL> def display ( self , result ) : <EOL> """<STR_LIT>""" <EOL> rv = [ ] <EOL> for k in ( '<STR_LIT:time>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:size>' , '<STR_LIT>' ) : <EOL> v = getattr ( result , k ) <EOL> if v is not None : <EOL> rv . append ( '<STR_LIT>' % ( k , float ( v ) ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( rv ) <EOL> def filter_acceptable ( self , query ) : <EOL> """<STR_LIT>""" <EOL> return query <EOL> def is_acceptable ( self , result ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def stats_quality_score ( self , result , worst_result , best_result ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_acceptable ( result ) : <EOL> return worst_result . time <EOL> else : <EOL> return result . time <EOL> def _project ( a1 , a2 , factor ) : <EOL> if a1 is None or a2 is None : <EOL> return None <EOL> return a2 + factor * ( a2 - a1 ) <EOL> class MinimizeTime ( SearchObjective ) : <EOL> """<STR_LIT>""" <EOL> def result_order_by_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ Result . time ] <EOL> def result_compare ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( result1 . time , result2 . time ) <EOL> def config_compare ( self , config1 , config2 ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( min ( map ( _ . time , self . driver . results_query ( config = config1 ) ) ) , <EOL> min ( map ( _ . time , self . driver . results_query ( config = config2 ) ) ) ) <EOL> def result_relative ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> if result2 . time == <NUM_LIT:0> : <EOL> return float ( '<STR_LIT>' ) * result1 . time <EOL> return result1 . time / result2 . time <EOL> class MaximizeAccuracy ( SearchObjective ) : <EOL> """<STR_LIT>""" <EOL> def result_order_by_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ - Result . accuracy ] <EOL> def result_compare ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( result2 . accuracy , result1 . accuracy ) <EOL> def result_relative ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> if result1 . accuracy == <NUM_LIT:0> : <EOL> return float ( '<STR_LIT>' ) * result2 . accuracy <EOL> return result2 . accuracy / result1 . accuracy <EOL> def stats_quality_score ( self , result , worst_result , best_result ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_acceptable ( result ) : <EOL> return worst_result . time <EOL> else : <EOL> return result . time <EOL> def stats_raw_score ( self , result ) : <EOL> return result . accuracy <EOL> class MaximizeAccuracyMinimizeSize ( MaximizeAccuracy ) : <EOL> """<STR_LIT>""" <EOL> def result_order_by_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ - Result . accuracy , Result . size ] <EOL> def result_compare ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( ( - result1 . accuracy , result1 . size ) , <EOL> ( - result2 . accuracy , result2 . size ) ) <EOL> def display ( self , result ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( result . accuracy , result . size ) <EOL> def result_relative ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> log . warning ( '<STR_LIT>' , <EOL> self . __class__ . __name__ ) <EOL> return None <EOL> class ThresholdAccuracyMinimizeTime ( SearchObjective ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , accuracy_target , low_accuracy_limit_multiplier = <NUM_LIT> ) : <EOL> self . accuracy_target = accuracy_target <EOL> self . low_accuracy_limit_multiplier = low_accuracy_limit_multiplier <EOL> super ( ThresholdAccuracyMinimizeTime , self ) . __init__ ( ) <EOL> def result_order_by_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" % self . accuracy_target , <EOL> opentuner . resultsdb . models . Result . time ] <EOL> def result_compare ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> return cmp ( ( - min ( self . accuracy_target , result1 . accuracy ) , <EOL> result1 . time ) , <EOL> ( - min ( self . accuracy_target , result2 . accuracy ) , result2 . time ) ) <EOL> def config_compare ( self , config1 , config2 ) : <EOL> """<STR_LIT>""" <EOL> return self . result_compare ( <EOL> self . driver . results_query ( config = config1 , objective_ordered = True ) [ <NUM_LIT:0> ] , <EOL> self . driver . results_query ( config = config2 , objective_ordered = True ) [ <NUM_LIT:0> ] ) <EOL> def limit_from_config ( self , config ) : <EOL> """<STR_LIT>""" <EOL> results = self . driver . results_query ( config = config ) <EOL> if results . count ( ) == <NUM_LIT:0> : <EOL> return None <EOL> if self . accuracy_target > min ( map ( _ . accuracy , results ) ) : <EOL> m = self . low_accuracy_limit_multiplier <EOL> else : <EOL> m = <NUM_LIT:1.0> <EOL> return m * max ( map ( _ . time , results ) ) <EOL> def filter_acceptable ( self , query ) : <EOL> """<STR_LIT>""" <EOL> return query . filter ( opentuner . resultsdb . models . Result . accuracy <EOL> >= self . accuracy_target ) <EOL> def is_acceptable ( self , result ) : <EOL> """<STR_LIT>""" <EOL> return result . accuracy >= self . accuracy_target <EOL> def result_relative ( self , result1 , result2 ) : <EOL> """<STR_LIT>""" <EOL> log . warning ( '<STR_LIT>' , <EOL> self . __class__ . __name__ ) <EOL> return None </s>
