def random_good_gene(n, tree, lengths, angle_ranges): while True: angles = np.random.uniform(low=-np.pi/4, high=np.pi/4, size=n) if good_angles(angles, angle_ranges): lines, end_points = tree.traverse(lengths, angles) if validate(Polyline(lines)) == True: return angles
def random_good_tree(n, angle_ranges, branches=2): t = random_tree(n, branches) while True: lengths = np.random.random(n) * 10 angles = np.random.uniform(low=-np.pi/4, high=np.pi/4, size=n) if good_angles(angles, angle_ranges): lines, end_points = t.traverse(lengths, angles) if validate(Polyline(lines)) == True: return t, end_points, lengths, angles
def rectangle(l): p = Point(np.random.uniform(low=-l/2, high=l/2), np.random.uniform(low=0, high=l/2)) w = np.random.uniform(low=0, high=l/3) h = np.random.uniform(low=0, high=l/6) a = np.random.uniform(low=-np.pi/4, high=np.pi/4) p1 = p.translation(w, h).rotation(a, p) p2 = p.translation(w, -h).rotation(a, p) p3 = p.translation(-w, -h).rotation(a, p) p4 = p.translation(-w, h).rotation(a, p) return Polyline([Line(p1, p2), Line(p2, p3), Line(p3, p4), Line(p4, p1)])
def create_arm(angles, lengths): alpha = np.pi/2 A = Point(0, 0) lines = [] for i in range(len(angles)): B = A.translation(lengths[i], 0) alpha += angles[i] B = B.rotation(alpha, A) lines.append(Line(A, B)) A = B return Polyline(lines)
def __init__(self, n, width=const.learn_track_width, curvature=0.5, path_type='random', circ_dir='right', segments=5, sharpness=10): self.left, self.right = generate_path(n, width, curvature, path_type=path_type, circ_dir=circ_dir, segments=segments, sharpness=sharpness) self.finish = Polyline( [Line(self.left.lines[-1].b, self.right.lines[-1].b)]) self.start = Polyline( [Line(self.left.lines[0].a, self.right.lines[0].a)])
def objective_function(gene, tree, lengths, final_points, angle_ranges, generation, good_ratio): lines, end_points = tree.traverse(lengths, gene) coeff = 1. if validate(Polyline(lines)) == False: coeff += 0.5 * np.sqrt(generation) if good_angles(gene, angle_ranges) == False: coeff += 0.2 * generation * generation res = 0 for i in range(len(end_points)): res += final_points[i].distance(end_points[i]) return -res * coeff
def get_polyline(self, fat=1): dxh = self.height * fat * np.cos(self.alpha) dyh = self.height * fat * np.sin(self.alpha) dxw = self.width * fat * np.cos(self.alpha - np.pi / 2) dyw = self.width * fat * np.sin(self.alpha - np.pi / 2) p1 = self.pos.translation(dxh, dyh).translation(dxw, dyw) p2 = self.pos.translation(dxh, dyh).translation(-dxw, -dyw) p3 = self.pos.translation(-dxh, -dyh).translation(dxw, dyw) p4 = self.pos.translation(-dxh, -dyh).translation(-dxw, -dyw) return Polyline( [Line(p1, p2), Line(p2, p4), Line(p3, p4), Line(p3, p1)])
def serpent(segments, sharpness): lines = [Line(Point(0., 0.), Point(0., 10.))] a = np.pi / 2 xprv = 0. yprv = 10. sgn = -1 for s in range(segments): for i in range(sharpness): a += (np.pi / sharpness) * sgn x = xprv + 10. * np.cos(a) y = yprv + 10. * np.sin(a) lines.append(Line(Point(xprv, yprv), Point(x, y))) xprv = x yprv = y sgn *= -1 return Polyline(lines)
def circular(n, circ_dir): lines = [Line(Point(0., 0.), Point(0., 10.))] a = np.pi / 2 xprv = 0. yprv = 10.0 sgn = -1 ### turn right if circ_dir == 'left': sgn = 1 for i in range(n): a += 0.2 * sgn l = 2 * np.sqrt(i + 10) x = xprv + l * np.cos(a) y = yprv + l * np.sin(a) lines.append(Line(Point(xprv, yprv), Point(x, y))) xprv = x yprv = y return Polyline(lines)
def animate(i): print i population = generations[i] d = population.shape[1] / 2 for j, arm in enumerate(arms): lines, e = tree.traverse(lengths,population[j, :d]) for k in range(D): x, y = lines[k].plot_data() arm[k].set_data(x, y) color = 'green' if good_angles(population[j, :d], angle_ranges) == False or validate(Polyline(lines)) == False: color = 'red' for k in range(D): arm[k].set_color(color) if (j > 0): arm[k].set_alpha(0.2) arm[k].set_linewidth(0.5) return flatten(arms)
def horseshoe(n, circ_dir): lines = [Line(Point(0., 0.), Point(1., 10. * n))] a = np.pi / 2 xprv = 1. yprv = 10. * n sgn = -1 if circ_dir == 'left': sgn = 1 for i in range(9): a += np.pi / 10 * sgn x = xprv + 10 * np.cos(a) y = yprv + 10 * np.sin(a) lines.append(Line(Point(xprv, yprv), Point(x, y))) xprv = x yprv = y x = xprv + 10 * n * np.cos(a) y = yprv + 10 * n * np.sin(a) lines.append(Line(Point(xprv, yprv), Point(x, y))) return Polyline(lines)
def random_path(n, sigma, l): lines = [Line(Point(0., 0.), Point(0.5, l))] a = np.pi / 2 xprv = 0.5 yprv = l for i in range(n): turn = np.random.normal(0, sigma) if turn < 0: turn = max(turn, -sigma) else: turn = min(turn, sigma) a += turn l = np.random.random() * l + 2 x = xprv + l * np.cos(a) y = yprv + l * np.sin(a) lines.append(Line(Point(xprv, yprv), Point(x, y))) xprv = x yprv = y return Polyline(lines)
def straight(n): lines = [Line(Point(0., 0.), Point(0.1, 10. * n))] return Polyline(lines)