<s> """<STR_LIT>""" <EOL> from math import exp <EOL> import random <EOL> from pebl import network , result , evaluator , config <EOL> from pebl . learner . base import * <EOL> class SALearnerStatistics : <EOL> def __init__ ( self , starting_temp , delta_temp , max_iterations_at_temp ) : <EOL> self . temp = starting_temp <EOL> self . iterations_at_temp = <NUM_LIT:0> <EOL> self . max_iterations_at_temp = max_iterations_at_temp <EOL> self . delta_temp = delta_temp <EOL> self . iterations = <NUM_LIT:0> <EOL> self . best_score = <NUM_LIT:0> <EOL> self . current_score = <NUM_LIT:0> <EOL> def update ( self ) : <EOL> self . iterations += <NUM_LIT:1> <EOL> self . iterations_at_temp += <NUM_LIT:1> <EOL> if self . iterations_at_temp >= self . max_iterations_at_temp : <EOL> self . temp *= self . delta_temp <EOL> self . iterations_at_temp = <NUM_LIT:0> <EOL> class SimulatedAnnealingLearner ( Learner ) : <EOL> _params = ( <EOL> config . FloatParameter ( <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> config . atleast ( <NUM_LIT:0.0> ) , <EOL> default = <NUM_LIT> <EOL> ) , <EOL> config . FloatParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> config . atleast ( <NUM_LIT:0.0> ) , <EOL> default = <NUM_LIT:0.5> <EOL> ) , <EOL> config . IntParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> config . atleast ( <NUM_LIT:0> ) , <EOL> default = <NUM_LIT:100> <EOL> ) , <EOL> config . StringParameter ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> def __init__ ( self , data_ = None , prior_ = None , ** options ) : <EOL> """<STR_LIT>""" <EOL> super ( SimulatedAnnealingLearner , self ) . __init__ ( data_ , prior_ ) <EOL> config . setparams ( self , options ) <EOL> if not isinstance ( self . seed , network . Network ) : <EOL> self . seed = network . Network ( self . data . variables , self . seed ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stats = SALearnerStatistics ( self . start_temp , self . delta_temp , <EOL> self . max_iters_at_temp ) <EOL> self . result = result . LearnerResult ( self ) <EOL> self . evaluator = evaluator . fromconfig ( self . data , self . seed , self . prior ) <EOL> self . evaluator . score_network ( self . seed . copy ( ) ) <EOL> self . result . start_run ( ) <EOL> curscore = self . evaluator . score_network ( ) <EOL> while self . stats . temp >= <NUM_LIT:1> : <EOL> try : <EOL> newscore = self . _alter_network_randomly_and_score ( ) <EOL> except CannotAlterNetworkException : <EOL> return <EOL> self . result . add_network ( self . evaluator . network , newscore ) <EOL> if self . _accept ( newscore ) : <EOL> self . stats . current_score = newscore <EOL> if self . stats . current_score > self . stats . best_score : <EOL> self . stats . best_score = self . stats . current_score <EOL> else : <EOL> self . evaluator . restore_network ( ) <EOL> self . stats . update ( ) <EOL> self . result . stop_run ( ) <EOL> return self . result <EOL> def _accept ( self , newscore ) : <EOL> oldscore = self . stats . current_score <EOL> if newscore >= oldscore : <EOL> return True <EOL> elif random . random ( ) < exp ( ( newscore - oldscore ) / self . stats . temp ) : <EOL> return True <EOL> else : <EOL> return False </s>
