Added more algos

This commit is contained in:
Vitalik Buterin 2015-01-12 21:13:56 -05:00
parent e41f3f15dd
commit 5549c48500
1 changed files with 88 additions and 29 deletions

View File

@ -1,47 +1,106 @@
import math import math, random
import random
hashpower = [float(x) for x in open('hashpower.csv').readlines()] hashpower = [float(x) for x in open('hashpower.csv').readlines()]
target = 12 target = 12
seconds_in_day = 8640 seconds_in_day = 86400
ema_factor = 0.005 ema_factor = 0.01
f = 80 f = 20
sqrf = 3
threshold = 1.3 threshold = 1.3
maxadjust = 0.1 adj_factor = 0.01
maxadjust = 0.5
blks_back = 10
def expdiff(t): def expdiff(t):
return -math.log(random.random()) * t return -math.log(random.random()) * t
def adjust(timestamps, diffs): def calc_threshold_time(p, t):
if len(timestamps) < 7: return t * -math.log(1 - p)
def abs_sqr(x):
return -(x**2) if x < 0 else x**2
def simple_adjust(timestamps, diffs):
if len(timestamps) < blks_back + 2:
return diffs[-1] return diffs[-1]
blks_back = 5 # Total interval between previous block and block a bit further back
delta = timestamps[-2] - timestamps[-2-blks_back] delta = timestamps[-2] - timestamps[-2-blks_back] + 0.0
expected = target * 0.693 * blks_back # Expected interval
if delta < expected / threshold: expected = target * blks_back
fac = max(1 + 1. * delta / expected / f, 1 + maxadjust) ** 2 fac = max(min(1 - (delta / expected - 1) / f, 1+maxadjust), 1-maxadjust)
elif delta > expected * threshold: return diffs[-1] * fac
fac = max(1 - 1. * delta / expected / f, 1 - maxadjust) ** 2
def quadratic_adjust(timestamps, diffs):
if len(timestamps) < blks_back + 2:
return diffs[-1]
# Total interval between previous block and block a bit further back
delta = timestamps[-2] - timestamps[-2-blks_back] + 0.0
# Expected interval
expected = target * blks_back
fac = max(min(1 - abs_sqr(delta / expected - 1) / sqrf,
1+maxadjust), 1-maxadjust)
return diffs[-1] * fac
def bounded_adjust(timestamps, diffs):
if len(timestamps) < blks_back + 2:
return diffs[-1]
# Total interval between previous block and block a bit further back
delta = timestamps[-2] - timestamps[-2-blks_back] + 0.0
# Expected interval
expected = target * blks_back
if delta / expected > threshold:
fac = (1 - adj_factor)
elif delta / expected < 1 / threshold:
fac = (1 + adj_factor) ** (delta / expected)
else: else:
fac = 1 fac = 1
return diffs[-1] * fac return diffs[-1] * fac
def test(source, adjust): def test(source, adjust):
ema = target ema = maxema = minema = target
chain = [0] lthalf, gtdouble, lttq, gtft = 0, 0, 0, 0
d = [source[0]] times = [0]
emachain = [target] diffs = [source[0]]
sourcechain = [0] nextprint = 10**6
while chain[-1] < len(source) * seconds_in_day: count = 0
hashpower = source[int(chain[-1] // seconds_in_day)] while times[-1] < len(source) * seconds_in_day:
d.append(adjust(chain, d)) if times[-1] > nextprint:
chain.append(chain[-1] + expdiff(d[-1] / hashpower)) print '%d out of %d processed' % \
ema = ema * (1 - ema_factor) + (chain[-1] - chain[-2]) * ema_factor (times[-1], len(source) * seconds_in_day)
emachain.append(ema) nextprint += 10**6
sourcechain.append(hashpower) # Grab hashpower from data source
print 'min', min(emachain[500:]), 'max', max(emachain[500:]), 'avg', sum(emachain) / len(emachain) hashpower = source[int(times[-1] // seconds_in_day)]
return zip(emachain[500:], sourcechain[500:], chain[500:]) # Calculate new difficulty
diffs.append(adjust(times, diffs))
# Calculate next block time
times.append(times[-1] + expdiff(diffs[-1] / hashpower))
# Calculate min and max ema
ema = ema * (1 - ema_factor) + (times[-1] - times[-2]) * ema_factor
minema = min(minema, ema)
maxema = max(maxema, ema)
count += 1
if ema < target * 0.75:
lttq += 1
if ema < target * 0.5:
lthalf += 1
elif ema > target * 1.33333:
gtft += 1
if ema > target * 2:
gtdouble += 1
# Pop items to save memory
if len(times) > 2000:
times.pop(0)
diffs.pop(0)
print 'min', minema, 'max', maxema, 'avg', times[-1] / count, \
'ema < half', lthalf * 1.0 / count, \
'ema > double', gtdouble * 1.0 / count, \
'ema < 3/4', lttq * 1.0 / count, \
'ema > 4/3', gtft * 1.0 / count