auto_crawler.py 5.1 KB

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