Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0%| | 0/1000 [00:00<?, ?it/s]
- 0%|▏ | 1/1000 [00:00<12:03, 1.38it/s]
- 6%|████████████▎ | 56/1000 [00:01<02:02, 7.72it/s]
- class Creature:
- """
- Generates a complete virtual creature
- Tests
- -----------
- """
- def __init__(self, params):
- self.rules = params.get('rules')
- self.l_string = params.get('axiom')
- self.constants = params.get('constants')
- self.variables = params.get('variables')
- self.angle = params.get('angle')
- self.recur(params.get('num_char'))
- self.length = params.get('length')
- self.mapper()
- # self.layout()
- def recur(self, n):
- for _ in range(n):
- self.l_string = ''.join([self.next_char(c) for c in self.l_string])
- self.l_string = self.l_string.replace('X', '')
- def next_char(self, c):
- rule = self.rules.get(c, c)
- if len(rule) > 1:
- return np.random.choice(self.rules.get(c, ["Other"])["options"],
- p=self.rules.get(c, ["Other"])[
- "probabilities"])
- else:
- return rule
- def mapper(self):
- theta = 1.570
- if isinstance(self.angle, int):
- randomAngle = False
- elif self.angle == 'random':
- randomAngle = True
- def getAngle():
- if randomAngle:
- return (np.random.uniform(0, 0.5) * pi)
- else:
- return radians(self.angle)
- num_chars = len(self.l_string)
- coords = np.zeros((num_chars + 1, 3), np.double)
- # branchArr = np.full((1, 3), np.nan, np.double)
- def makeRotMat(theta):
- rotMat = np.array((
- (cos(theta), -sin(theta), 0),
- (sin(theta), cos(theta), 0),
- (0, 0, 1)
- ))
- return rotMat
- rotVec = makeRotMat(theta)
- begin_vec = np.array((1, 0, 0), np.float64)
- i = 1
- for c in self.l_string:
- if c == 'F':
- next_vec = np.dot(rotVec, begin_vec)
- coords[i] = (
- coords[i-1] + (self.length * next_vec)
- )
- i += 1
- begin_vec = next_vec
- if c == '-':
- theta = theta - getAngle()
- rotVec = makeRotMat(theta)
- if c == '+':
- theta = theta + getAngle()
- rotVec = makeRotMat(theta)
- coords = np.delete(coords, np.s_[i:], 0)
- self.coords = coords
- from CreatureTools import Creature
- import numpy as np
- from tqdm import tqdm
- import multiprocessing as mp
- def genGen():
- params = {
- 'num_char': 100,
- 'variables': 'X',
- 'constants': 'F+-',
- 'axiom': 'FX',
- 'rules': {
- 'X': {
- 'options': ['+FX', '-FX'],
- 'probabilities': [0.5, 0.5]
- }
- },
- 'point': np.array([0, 0]),
- 'vector': np.array([0, 1]),
- 'length': 1.0,
- 'angle': 25 # random
- }
- return Creature(params).coords
- if __name__ == "__main__":
- iter = 1000
- pool = mp.Pool(4)
- pbar = tqdm(total=iter)
- population = []
- def update(*a):
- pbar.update()
- for i in range(pbar.total):
- results = [pool.apply_async(genGen, callback=update)]
- population.append([f.get() for f in results])
- pool.close()
- pool.join()
- pbar.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement