secondary_discrete_auto_crawler_pi.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from argparse import ArgumentParser
  2. from core.lottery import DarkfiTable
  3. from core.utils import *
  4. from core.darkie import Darkie
  5. from tqdm import tqdm
  6. from core.strategy import *
  7. import os
  8. AVG_LEN = 5
  9. KP_STEP=0.01
  10. KP_SEARCH= -0.01
  11. KI_STEP=0.01
  12. KI_SEARCH=-0.036
  13. EPSILON=0.0001
  14. RUNNING_TIME=1000
  15. NODES = 1000
  16. highest_acc = 0.2
  17. KP='kp'
  18. KI='ki'
  19. KP_RANGE_MULTIPLIER = 2
  20. KI_RANGE_MULTIPLIER = 2
  21. highest_gain = (KP_SEARCH, KI_SEARCH)
  22. parser = ArgumentParser()
  23. parser.add_argument('-p', '--high-precision', action='store_true')
  24. parser.add_argument('-r', '--randomize-nodes', action='store_false')
  25. parser.add_argument('-t', '--rand-running-time', action='store_false')
  26. parser.add_argument('-d', '--debug', action='store_false')
  27. args = parser.parse_args()
  28. high_precision = args.high_precision
  29. randomize_nodes = args.randomize_nodes
  30. rand_running_time = args.rand_running_time
  31. debug = args.debug
  32. def experiment(controller_type, kp, ki, distribution=[], hp=True):
  33. RND_NODES = random.randint(5, NODES) if randomize_nodes else NODES
  34. dt = DarkfiTable(sum([distribution[i] for i in range(RND_NODES)]), RUNNING_TIME, controller_type, kp=kp, ki=ki)
  35. for idx in range(0,RND_NODES):
  36. darkie = Darkie(distribution[idx], strategy=random_strategy(EPOCH_LENGTH))
  37. dt.add_darkie(darkie)
  38. acc, cc_acc, apy, reward, stake_ratio, apr = dt.background(rand_running_time, hp)
  39. return acc
  40. def multi_trial_exp(kp, ki, distribution = [], hp=True):
  41. global highest_acc
  42. global highest_gain
  43. new_record=False
  44. exp_threads = []
  45. accs = []
  46. for i in range(0, AVG_LEN):
  47. acc = experiment(CONTROLLER_TYPE_DISCRETE, kp=kp, ki=ki, distribution=distribution, hp=hp)
  48. accs += [acc]
  49. avg_acc = sum(accs)/float(AVG_LEN)
  50. buff = 'accuracy:{}, kp: {}, ki:{}'.format(avg_acc, kp, ki)
  51. if avg_acc > 0:
  52. gain = (kp, ki)
  53. acc_gain = (avg_acc, gain)
  54. if avg_acc > highest_acc:
  55. new_record = True
  56. highest_acc = avg_acc
  57. highest_gain = (kp, ki)
  58. with open('log'+os.sep+"highest_gain.txt", 'w') as f:
  59. f.write(buff)
  60. return buff, new_record
  61. SHIFTING = 0.05
  62. def crawler(crawl, range_multiplier, step=0.1):
  63. start = None
  64. if crawl==KP:
  65. start = highest_gain[0]
  66. elif crawl==KI:
  67. start = highest_gain[1]
  68. range_start = (start*range_multiplier if start <=0 else -1*start)
  69. range_end = (-1*start if start<=0 else range_multiplier*start)
  70. # if number of steps under 10 step resize the step to 50
  71. while (range_end-range_start)/step < 10:
  72. range_start -= SHIFTING
  73. range_end += SHIFTING
  74. step /= 10
  75. crawl_range = np.arange(range_start, range_end, step)
  76. np.random.shuffle(crawl_range)
  77. crawl_range = tqdm(crawl_range)
  78. distribution = [random.gauss(ERC20DRK/NODES, ERC20DRK/NODES*0.1) for i in range(NODES)]
  79. for i in crawl_range:
  80. kp = i if crawl==KP else highest_gain[0]
  81. ki = i if crawl==KI else highest_gain[1]
  82. buff, new_record = multi_trial_exp(kp, ki, distribution, hp=high_precision)
  83. crawl_range.set_description('highest:{} / {}'.format(highest_acc, buff))
  84. if new_record:
  85. break
  86. while True:
  87. prev_highest_gain = highest_gain
  88. # kp crawl
  89. crawler(KP, KP_RANGE_MULTIPLIER, KP_STEP)
  90. if highest_gain[0] == prev_highest_gain[0]:
  91. KP_RANGE_MULTIPLIER+=1
  92. KP_STEP/=10
  93. else:
  94. start = highest_gain[0]
  95. range_start = (start*KP_RANGE_MULTIPLIER if start <=0 else -1*start) - SHIFTING
  96. range_end = (-1*start if start<=0 else KP_RANGE_MULTIPLIER*start) + SHIFTING
  97. while (range_end - range_start)/KP_STEP >500:
  98. if KP_STEP < 0.1:
  99. KP_STEP*=10
  100. KP_RANGE_MULTIPLIER-=1
  101. #TODO (res) shouldn't the range also shrink?
  102. # not always true.
  103. # how to distinguish between thrinking range, and large step?
  104. # good strategy is step shoudn't > 0.1
  105. # range also should be > 0.8
  106. # what about range multiplier?
  107. # ki crawl
  108. crawler(KI, KI_RANGE_MULTIPLIER, KI_STEP)
  109. if highest_gain[1] == prev_highest_gain[1]:
  110. KI_RANGE_MULTIPLIER+=1
  111. KI_STEP/=10
  112. else:
  113. start = highest_gain[1]
  114. range_start = (start*KI_RANGE_MULTIPLIER if start <=0 else -1*start) - SHIFTING
  115. range_end = (-1*start if start<=0 else KI_RANGE_MULTIPLIER*start) + SHIFTING
  116. while (range_end - range_start)/KI_STEP >500:
  117. if KP_STEP < 0.1:
  118. KI_STEP*=10
  119. KI_RANGE_MULTIPLIER-=1