<s> import random <EOL> import btcnet_info <EOL> import gevent <EOL> import logging <EOL> import json <EOL> from collections import defaultdict <EOL> import bitHopper . Logic <EOL> import bitHopper . Network <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> np = None <EOL> blocks_timing = { } <EOL> blocks_calculated = { } <EOL> blocks_actual = { } <EOL> used_pools = [ '<STR_LIT>' ] <EOL> weights = [ - <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> def set_neg ( block ) : <EOL> if block not in blocks_actual : <EOL> blocks_actual [ block ] = <NUM_LIT:0> <EOL> def learn_block ( blocks , current_block ) : <EOL> print '<STR_LIT>' <EOL> for block in blocks : <EOL> if block not in blocks_timing : <EOL> blocks_timing [ block ] = blocks [ block ] <EOL> gevent . spawn_later ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , set_neg , block ) <EOL> if len ( blocks [ current_block ] ) < len ( blocks_timing [ current_block ] ) : <EOL> blocks_timing [ current_block ] = blocks [ current_block ] <EOL> if have_all_data ( blocks [ current_block ] ) : <EOL> calculate_block ( current_block ) <EOL> def have_all_data ( block_info ) : <EOL> for pool in used_pools : <EOL> if pool not in block_info : <EOL> return False <EOL> return True <EOL> def vec_mult ( a , b ) : <EOL> return sum ( [ i * j for i , j in zip ( a , b ) ] ) <EOL> def extract_vector ( current_block ) : <EOL> result = [ <NUM_LIT:1> ] <EOL> for pool in used_pools : <EOL> result . append ( blocks_timing [ current_block ] [ pool ] ) <EOL> return result <EOL> def calculate_block ( current_block ) : <EOL> print '<STR_LIT>' <EOL> if vec_mult ( weights , extract_vector ( current_block ) ) > <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> btcnet_info . get_pool ( '<STR_LIT>' ) . namespace . get_node ( '<STR_LIT>' ) . set_value ( <NUM_LIT:0> ) <EOL> blocks_calculated [ current_block ] = <NUM_LIT:1> <EOL> else : <EOL> blocks_calculated [ current_block ] = <NUM_LIT:0> <EOL> def check_learning ( ) : <EOL> print '<STR_LIT>' <EOL> while True : <EOL> gevent . sleep ( <NUM_LIT> ) <EOL> deepbit_blocks = set ( json . loads ( btcnet_info . get_pool ( '<STR_LIT>' ) . blocks ) ) <EOL> for block in blocks_timing : <EOL> if block in blocks_actual and blocks_actual [ block ] == <NUM_LIT:1> : <EOL> continue <EOL> if block in deepbit_blocks : <EOL> blocks_actual [ block ] = <NUM_LIT:1> <EOL> print '<STR_LIT>' % ( block , blocks_actual [ block ] ) <EOL> def poke_deepbit ( ) : <EOL> choices = list ( bitHopper . Logic . generate_tuples ( '<STR_LIT>' ) ) <EOL> if len ( choices ) == <NUM_LIT:0> : <EOL> logging . info ( '<STR_LIT>' ) <EOL> return <EOL> server , username , password = random . choice ( choices ) <EOL> url = btcnet_info . get_pool ( server ) [ '<STR_LIT>' ] <EOL> bitHopper . Network . send_work_lp ( url , username , password , '<STR_LIT>' ) <EOL> def calc_good_servers ( ) : <EOL> used_pools = set ( ) <EOL> used_pools . add ( '<STR_LIT>' ) <EOL> def yield_timings_block ( blocks ) : <EOL> for block in blocks : <EOL> for server in block : <EOL> yield server <EOL> times_found = list ( yield_timings_block ( block for name , block in blocks_timing . iteritems ( ) ) ) <EOL> def make_count_map ( servers ) : <EOL> count = defaultdict ( int ) <EOL> for server in servers : <EOL> count [ server ] += <NUM_LIT:1> <EOL> return sorted ( list ( count . iteritems ( ) ) , key = lambda x : - <NUM_LIT:1> * x [ <NUM_LIT:1> ] ) <EOL> counts = make_count_map ( times_found ) <EOL> if not counts : <EOL> return used_pools <EOL> cutoff = counts [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> for server , count in counts : <EOL> if count >= cutoff : <EOL> used_pools . add ( server ) <EOL> return used_pools <EOL> def linreg ( data ) : <EOL> ydata = [ p [ - <NUM_LIT:1> ] for p in data ] <EOL> xdata = [ [ <NUM_LIT:1> ] + p [ : - <NUM_LIT:1> ] for p in data ] <EOL> l = <NUM_LIT> <EOL> n = len ( xdata [ <NUM_LIT:0> ] ) <EOL> Z = np . matrix ( xdata ) <EOL> wreg = ( Z . T * Z + np . identity ( n ) * l ) . I * Z . T * np . matrix ( ydata ) . T <EOL> return [ float ( d ) for d in list ( wreg ) ] <EOL> def train_data ( ) : <EOL> if np == None : <EOL> return <EOL> while True : <EOL> servers = calc_good_servers ( ) <EOL> global used_pools <EOL> used_pools = servers <EOL> data = [ ] <EOL> for block in blocks_timing : <EOL> if not blocks_actual . get ( block , None ) : <EOL> continue <EOL> point = [ block . get ( server , <NUM_LIT> ) for server in servers ] <EOL> point . append ( blocks_actual . get ( block ) ) <EOL> data . append ( point ) <EOL> global weights <EOL> if data : <EOL> weights = linreg ( data ) <EOL> gevent . sleep ( <NUM_LIT> * <NUM_LIT:5> ) <EOL> gevent . spawn ( train_data ) <EOL> gevent . spawn ( poke_deepbit ) <EOL> gevent . spawn ( check_learning ) </s>
