secondary_discrete_auto_crawler.py 5.2 KB

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