secondary_discrete_auto_crawler.py 5.2 KB

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