def render(self, batch, group=None): self.calc_item_size() self.calc_geometry() self.frame.set_geometry(self.x, self.y, self.frame.width, self.frame.height) print self, self.frame self.frame.render(batch, group=group) chevron = Triangle([(self.x + self.frame.width - 12, self.y + round(self.frame.height * 0.75)), (self.x + self.frame.width - 8, self.y + round(self.frame.height * 0.25)), (self.x + self.frame.width - 4, self.y + round(self.frame.height * 0.75))]) chevron.render(batch, group=group) if self.selection: l_x, l_y = self.calc_label_position(self.selection.label) label = pyglet.text.Label(self.selection.text, x=l_x, y=l_y, batch=batch, group=group) self.dispatch_event('on_listbox_select', self.selection) if self.expanded: for item in self.options: item.render(batch, group=group)
def __init__(self, *args, **kwargs): super(MyWindow, self).__init__(*args, **kwargs) self.set_minimum_size(400, 300) # pyglet.gl.glClearColor(0.2, 0.3, 0.2, 1.0) self.line = Line() self.triangle = Triangle(100, 100) self.point = Point(200, 200) self.objects = [] self.food_manager = FoodManager() self.creature_manager = CreatureManager() self.life_breath()
def flatten_fastBVH(root: TreeBox): flat_boxes = [FastBox(ZEROS, ZEROS) for _ in range(0)] flat_triangles = [ Triangle(ZEROS, ZEROS, ZEROS, WHITE, False, Material.DIFFUSE.value) for _ in range(0) ] emitters = [ Triangle(ZEROS, ZEROS, ZEROS, WHITE, False, Material.DIFFUSE.value) for _ in range(0) ] box_queue = [root] while box_queue: # pop box = box_queue[0] # transform TreeBox to FastBox fast_box = FastBox(box.min, box.max) if box.right is not None and box.left is not None: # if inner node (non-leaf), left is an index in flat_boxes fast_box.left = len(flat_boxes) + len(box_queue) # right will always be at left + 1, so use right as inner-vs-leaf flag fast_box.right = 0 # push children to queue box_queue.append(box.left) box_queue.append(box.right) else: # if leaf, left is index into flat_triangles fast_box.left = len(flat_triangles) for triangle in box.triangles: if triangle.emitter: emitters.append(triangle) flat_triangles.append(triangle) # so now flat_triangles[left:right] is the triangles in this box. nonzero right signals leaf in traverse. fast_box.right = len(flat_triangles) flat_boxes.append(fast_box) # todo: is this queue efficient? the pointer moves arbitrarily forward in memory... box_queue = box_queue[1:] return flat_boxes
def load_obj(path, material=Material.DIFFUSE.value): obj = objloader.Obj.open(path) logger.info('model %s has %d vertices and %d faces', path, len(obj.vert), len(obj.face)/3) if obj.norm: logger.info('model %s specifies normal vectors', path) if obj.text: logger.info('model %s is texture-mapped', path) packed_model = obj.pack() # build the vertices and triangles vertices = numba.typed.List() for vertex in obj.vert: vertices.append(point(*vertex)) triangles = numba.typed.List() for (v0, _, _), (v1, _, _), (v2, _, _) in zip(*[iter(obj.face)]*3): triangles.append(Triangle(vertices[v0 - 1], vertices[v1 - 1], vertices[v2 - 1], material=material)) return triangles
def load_obj(path, material=Material.DIFFUSE.value): obj = objloader.Obj.open(path) logger.info('model %s has %d vertices and %d faces', path, len(obj.vert), len(obj.face) / 3) if obj.norm: logger.info('model %s specifies normal vectors', path) if obj.text: logger.info('model %s is texture-mapped', path) # build the vertices and triangles verts = [point(*vert) for vert in obj.vert] triangles = [ Triangle(verts[v0 - 1], verts[v1 - 1], verts[v2 - 1], material=material) for (v0, _, _), (v1, _, _), (v2, _, _) in zip(*[iter(obj.face)] * 3) ] return triangles
def big_triangle(): return Triangle(ZEROS, UNIT_X * 5, UNIT_Y * 5)
def wrong_handed_triangle(): return Triangle(ZEROS, UNIT_Y, UNIT_X)
def basic_triangle(): return Triangle(ZEROS, UNIT_X, UNIT_Y)
def triangles_for_box(box: Box, material=Material.DIFFUSE.value): left_bottom_back = box.min right_bottom_back = box.min + box.span * UNIT_X left_top_back = box.min + box.span * UNIT_Y left_bottom_front = box.min + box.span * UNIT_Z right_top_front = box.max left_top_front = box.max - box.span * UNIT_X right_bottom_front = box.max - box.span * UNIT_Y right_top_back = box.max - box.span * UNIT_Z shrink = np.array([.5, .95, .5], dtype=np.float32) tris = [ # back wall Triangle(left_bottom_back, right_bottom_back, right_top_back, color=RED, material=material), Triangle(left_bottom_back, right_top_back, left_top_back, color=RED, material=material), # left wall Triangle(left_bottom_back, left_top_front, left_bottom_front, color=BLUE, material=material), Triangle(left_bottom_back, left_top_back, left_top_front, color=BLUE, material=material), # right wall Triangle(right_bottom_back, right_bottom_front, right_top_front, color=GREEN, material=material), Triangle(right_bottom_back, right_top_front, right_top_back, color=GREEN, material=material), # front wall Triangle(left_bottom_front, right_top_front, right_bottom_front, color=CYAN, material=material), Triangle(left_bottom_front, left_top_front, right_top_front, color=CYAN, material=material), # floor Triangle(left_bottom_back, right_bottom_front, right_bottom_back, color=WHITE, material=material), Triangle(left_bottom_back, left_bottom_front, right_bottom_front, color=WHITE, material=material), # ceiling Triangle(left_top_back, right_top_back, right_top_front, color=WHITE, material=material), Triangle(left_top_back, right_top_front, left_top_front, color=WHITE, material=material), # ceiling light # NB this assumes box is centered on the origin, at least wrt x and z Triangle(left_top_back * shrink, right_top_back * shrink, right_top_front * shrink, color=WHITE, emitter=True), Triangle(left_top_back * shrink, right_top_front * shrink, left_top_front * shrink, color=WHITE, emitter=True), ] return tris
def __init__(self, x=0, y=0, random=False, show_vision=False, dna=None, **kwargs): self.id = uuid.uuid4() self.life = 100 self._alive = True self.genes = 15 self.age = 0 self.memory_s = [] self.memory_a = [] self.all_memory = None if kwargs.get('old_memory', None): self.all_memory = [x[:] for x in kwargs['old_memory']] if not dna: layers = randint(2, 100) neurons = randint(2, 100) iters = randint(2, 100) memorysize = randint(2, 100) # [Layers, neurons, iters , memory] self.dna = [layers, neurons, iters, memorysize] else: self.dna = dna self._hidden_layers = [self.dna[1] for _ in range(self.dna[0])] self._max_memory = self.dna[3] self.brain = MLPRegressor(activation='logistic', alpha=1e-05, batch_size='auto', beta_1=0.9, beta_2=0.999, early_stopping=False, epsilon=1e-08, hidden_layer_sizes=self._hidden_layers, learning_rate='constant', learning_rate_init=0.001, max_iter=self.dna[2] * 100, momentum=0.9, nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1, verbose=False, warm_start=False) if random: x = randint(0, WIDTH) y = randint(0, HEIGHT) self.body = Triangle(x=x, y=y, random=False) self.vision = Circle(x=x, y=y, radius=VISION_RADIUS) self.show_vision = show_vision self.position = Vector(x, y) self.velocity = Vector(0, 0) self.aceleration = Vector(0, 0) self.mass = 10 self.visible_objects = [] self.last_action = [0, 0, 0] if self.all_memory is None: start_sensor = [[randint(-5, 5), randint(-5, 5)] for _ in range(10)] start_out = [[randint(0, 1), randint(-5, 5), randint(-5, 5)] for _ in range(10)] self.all_memory = [start_sensor, start_out] else: if np.random.random() < RANDOM_MEMORY: # creating some random memories self.all_memory[0] += [[randint(-5, 5), randint(-5, 5)]] self.all_memory[1] += [[ randint(0, 1), randint(-5, 5), randint(-5, 5) ]] start_sensor = self.all_memory[0] start_out = self.all_memory[1] try: self.brain.fit(start_sensor, start_out) except: print(start_sensor, start_out) raise ValueError
class Animal: def __init__(self, x=0, y=0, random=False, show_vision=False, dna=None, **kwargs): self.id = uuid.uuid4() self.life = 100 self._alive = True self.genes = 15 self.age = 0 self.memory_s = [] self.memory_a = [] self.all_memory = None if kwargs.get('old_memory', None): self.all_memory = [x[:] for x in kwargs['old_memory']] if not dna: layers = randint(2, 100) neurons = randint(2, 100) iters = randint(2, 100) memorysize = randint(2, 100) # [Layers, neurons, iters , memory] self.dna = [layers, neurons, iters, memorysize] else: self.dna = dna self._hidden_layers = [self.dna[1] for _ in range(self.dna[0])] self._max_memory = self.dna[3] self.brain = MLPRegressor(activation='logistic', alpha=1e-05, batch_size='auto', beta_1=0.9, beta_2=0.999, early_stopping=False, epsilon=1e-08, hidden_layer_sizes=self._hidden_layers, learning_rate='constant', learning_rate_init=0.001, max_iter=self.dna[2] * 100, momentum=0.9, nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1, verbose=False, warm_start=False) if random: x = randint(0, WIDTH) y = randint(0, HEIGHT) self.body = Triangle(x=x, y=y, random=False) self.vision = Circle(x=x, y=y, radius=VISION_RADIUS) self.show_vision = show_vision self.position = Vector(x, y) self.velocity = Vector(0, 0) self.aceleration = Vector(0, 0) self.mass = 10 self.visible_objects = [] self.last_action = [0, 0, 0] if self.all_memory is None: start_sensor = [[randint(-5, 5), randint(-5, 5)] for _ in range(10)] start_out = [[randint(0, 1), randint(-5, 5), randint(-5, 5)] for _ in range(10)] self.all_memory = [start_sensor, start_out] else: if np.random.random() < RANDOM_MEMORY: # creating some random memories self.all_memory[0] += [[randint(-5, 5), randint(-5, 5)]] self.all_memory[1] += [[ randint(0, 1), randint(-5, 5), randint(-5, 5) ]] start_sensor = self.all_memory[0] start_out = self.all_memory[1] try: self.brain.fit(start_sensor, start_out) except: print(start_sensor, start_out) raise ValueError def rotate(self): angle = self.position.angle self.body.rotate(angle=angle) def move(self, vel): self.spend_energy(0.1) self.velocity = vel self.position += vel if self.position.x >= WIDTH: self.position.x = WIDTH if self.position.y >= HEIGHT: self.position.y = HEIGHT if self.position.x <= 0: self.position.x = 0 if self.position.y <= 0: self.position.y = 0 self.body.move(self.position, vel) def check_near_food(self, foods): creature_path = pltpath.Path( [*np.array_split(self.vision.vertex, self.vision.points)]) food_positions = [food.body.vertex for food in foods] collide = creature_path.contains_points(food_positions) if any(collide): food_positions = np.array(list(foods)) near_food = food_positions[collide] self.visible_objects = near_food else: self.visible_objects = [] # print(self.visible_objects) def check_collision(self, food_manager): foods = food_manager.foods.values() body_path = pltpath.Path([*np.array_split(self.body.vertex2D, 3)]) food_positions = [food.body.vertex for food in foods] collide = body_path.contains_points(food_positions) if any(collide): food_positions = np.array(list(foods)) target_food = food_positions[collide] energy = food_manager.consume_food(target_food[0].id) self.eat(energy) else: target_food = [] # print(self.life) def breath(self): self.spend_energy(0.1) def eat(self, energy): self.spend_energy(0.1) self.life += energy max_m = self._max_memory * 2 if len(self.memory_s) < max_m and len(self.memory_a) < max_m: self.memory_s += [ list(self.visible_objects[0].position - self.position) ] self.memory_a += [self.last_action] def spend_energy(self, energy): self.life -= energy def update(self, food_manager): self.breath() foods = food_manager.foods.values() if self.show_vision: self.vision.update(self.position[0], self.position[1]) pyglet.graphics.draw(*self.vision.vertex_list) self.check_near_food(foods) self.check_collision(food_manager) saida = None if any(self.visible_objects): saida = self.brain.predict([self.visible_objects[0].position._v]) self.take_action(saida) def take_action(self, trial): actions = { 0: self.walk, 1: self.think, } if trial is not None: trial = np.round(trial) # print(trial) try: choice = actions.get(int(trial[0][0]), self.walk) choice(trial[0][1:]) except: choice = actions[0] choice() traceback.print_exc() raise ValueError else: # if no trial, walk choice = actions[0] choice() def walk(self, direction=None): self.spend_energy(0.1) if direction is None: acc = Vector(randint(-MAX_SPEED, MAX_SPEED), randint(-MAX_SPEED, MAX_SPEED)) else: acc = Vector(*direction) if self.velocity.x >= MAX_SPEED and acc.x < 0: self.velocity.x += acc.x elif self.velocity.x <= -MAX_SPEED and acc.x > 0: self.velocity.x += acc.x if self.velocity.y >= MAX_SPEED and acc.y < 0: self.velocity.y += acc.y elif self.velocity.y <= -MAX_SPEED and acc.y > 0: self.velocity.y += acc.y if -MAX_SPEED <= self.velocity.x <= MAX_SPEED: self.velocity.x += acc.x if -MAX_SPEED <= self.velocity.y <= MAX_SPEED: self.velocity.y += acc.y self.last_action = [0, acc[0], acc[1]] self.move(self.velocity) # print(f" velocidade: {self.velocity}, acc: {acc}") def think(self, *args): # self.think_count += 1 self.spend_energy(0.1) self.last_action = [1, 0, 0] if any(self.memory_a) or any(self.memory_s): # print("memoria de entrada", self.Memory_s) # print("memoria de saida", self.Memory_a) try: self.brain.partial_fit(self.memory_s, self.memory_a) except: traceback.print_exc() print("Memoria", self.memory_a, self.memory_s) raise ValueError self.remember() self.memory_s = [] self.memory_a = [] def remember(self): # put good lessons on memory self.all_memory[0] += self.memory_s self.all_memory[1] += self.memory_a # check memory capacity, forget the long past if self.all_memory: while len(self.all_memory[0]) > self._max_memory: self.all_memory[0].pop(0) while len(self.all_memory[1]) > self._max_memory: self.all_memory[1].pop(0)