Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. 0%| | 0/1000 [00:00<?, ?it/s]
  2.  
  3.  
  4. 0%|▏ | 1/1000 [00:00<12:03, 1.38it/s]
  5. 6%|████████████▎ | 56/1000 [00:01<02:02, 7.72it/s]
  6.  
  7. class Creature:
  8. """
  9. Generates a complete virtual creature
  10. Tests
  11. -----------
  12.  
  13. """
  14.  
  15. def __init__(self, params):
  16. self.rules = params.get('rules')
  17. self.l_string = params.get('axiom')
  18. self.constants = params.get('constants')
  19. self.variables = params.get('variables')
  20. self.angle = params.get('angle')
  21. self.recur(params.get('num_char'))
  22. self.length = params.get('length')
  23. self.mapper()
  24. # self.layout()
  25.  
  26. def recur(self, n):
  27. for _ in range(n):
  28. self.l_string = ''.join([self.next_char(c) for c in self.l_string])
  29. self.l_string = self.l_string.replace('X', '')
  30.  
  31. def next_char(self, c):
  32. rule = self.rules.get(c, c)
  33. if len(rule) > 1:
  34. return np.random.choice(self.rules.get(c, ["Other"])["options"],
  35. p=self.rules.get(c, ["Other"])[
  36. "probabilities"])
  37. else:
  38. return rule
  39.  
  40. def mapper(self):
  41. theta = 1.570
  42.  
  43. if isinstance(self.angle, int):
  44. randomAngle = False
  45. elif self.angle == 'random':
  46. randomAngle = True
  47.  
  48. def getAngle():
  49. if randomAngle:
  50. return (np.random.uniform(0, 0.5) * pi)
  51. else:
  52. return radians(self.angle)
  53.  
  54. num_chars = len(self.l_string)
  55.  
  56. coords = np.zeros((num_chars + 1, 3), np.double)
  57. # branchArr = np.full((1, 3), np.nan, np.double)
  58.  
  59. def makeRotMat(theta):
  60. rotMat = np.array((
  61. (cos(theta), -sin(theta), 0),
  62. (sin(theta), cos(theta), 0),
  63. (0, 0, 1)
  64. ))
  65. return rotMat
  66.  
  67. rotVec = makeRotMat(theta)
  68.  
  69. begin_vec = np.array((1, 0, 0), np.float64)
  70. i = 1
  71.  
  72. for c in self.l_string:
  73. if c == 'F':
  74. next_vec = np.dot(rotVec, begin_vec)
  75. coords[i] = (
  76. coords[i-1] + (self.length * next_vec)
  77. )
  78. i += 1
  79. begin_vec = next_vec
  80.  
  81. if c == '-':
  82. theta = theta - getAngle()
  83. rotVec = makeRotMat(theta)
  84.  
  85. if c == '+':
  86. theta = theta + getAngle()
  87. rotVec = makeRotMat(theta)
  88.  
  89. coords = np.delete(coords, np.s_[i:], 0)
  90. self.coords = coords
  91.  
  92. from CreatureTools import Creature
  93. import numpy as np
  94. from tqdm import tqdm
  95. import multiprocessing as mp
  96.  
  97.  
  98. def genGen():
  99. params = {
  100. 'num_char': 100,
  101. 'variables': 'X',
  102. 'constants': 'F+-',
  103. 'axiom': 'FX',
  104. 'rules': {
  105. 'X': {
  106. 'options': ['+FX', '-FX'],
  107. 'probabilities': [0.5, 0.5]
  108. }
  109. },
  110. 'point': np.array([0, 0]),
  111. 'vector': np.array([0, 1]),
  112. 'length': 1.0,
  113. 'angle': 25 # random
  114. }
  115.  
  116. return Creature(params).coords
  117.  
  118.  
  119. if __name__ == "__main__":
  120. iter = 1000
  121. pool = mp.Pool(4)
  122.  
  123. pbar = tqdm(total=iter)
  124. population = []
  125.  
  126. def update(*a):
  127. pbar.update()
  128.  
  129. for i in range(pbar.total):
  130. results = [pool.apply_async(genGen, callback=update)]
  131. population.append([f.get() for f in results])
  132.  
  133. pool.close()
  134. pool.join()
  135. pbar.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement