def render(self): c = Circle(self.a[0], self.a[1], width=10, color=(0., .9, 0., 1.)) c.render() c = Circle(self.ac[0], self.ac[1], width=10, color=(0., .9, 0.5, 1.)) c.render() l = Line(self.a, self.ac, stroke=2, color=(0, 1., 1., 1.)) l.render() c = Circle(self.b[0], self.b[1], width=10, color=(0., .9, 1., 1.)) c.render() c = Circle(self.bc[0], self.bc[1], width=10, color=(0., .9, 1., 0.5)) c.render() l = Line(self.b, self.bc, stroke=2, color=(0, 1., 1., 1.)) l.render() steps = 100.0 bz = self.bezier() last = self.a for i in range(int(steps)): t = (i + 1) / steps val = bz.at(t) val = val[0] + self.a.x, val[1] + self.a.y glBegin(GL_LINES) glVertex2f(last[0], last[1]) glVertex2f(val[0], val[1]) glEnd() last = val
def draw(self): if self.stop: director.scene.end() if self.state == self.SWITCH: text = font.Text(self.font, 'switch to:',color=(0.,0.5,0.5,0.5)) text.x = 20 text.y = 300 text.draw() text = font.Text(self.font, self.number ,color=(0.,0.5,0.5,0.5)) text.x = 20 text.y = 250 text.draw() elif self.state in (self.SHOW, self.DRAG): self.path.render() if self.near: c = Circle( self.near.x,self.near.y, width=15,color=(0.,1.,1.,1.) ) c.render() elif self.state == self.ANIMATE: dt = time.time() - self.anim_start bz = self.path.bezier() if dt >= self.duration: self.state = self.SHOW else: x,y = bz.at( (dt/self.duration)**self.time_warp ) c = Circle( x+self.path.a.x,y+self.path.a.y, width=45,color=(0.,1.,1.,1.) ) c.render() text = font.Text(self.font, '[%i,%i] t**%0.2f d=%0.2f n=%d p=%d'%( self.mouse.x, self.mouse.y, self.time_warp, self.duration, len(self.paths), self.pathp ), color=(0.,0.5,0.5,0.5)) text.x = 100 text.y = 20 text.draw()
def draw(self): if self.stop: director.scene.end() if self.state == self.SWITCH: text = "switch to: %s" % self.number label = pyglet.text.Label(text, font_name=self.font_name, font_size=self.font_size, color=(0, 128, 128, 128), x=20, y=300) label.draw() elif self.state in (self.SHOW, self.DRAG): self.path.render() if self.near: c = Circle(self.near.x, self.near.y, width=15, color=(0., 1., 1., 1.)) c.render() elif self.state == self.ANIMATE: dt = time.time() - self.anim_start bz = self.path.bezier() if dt >= self.duration: self.state = self.SHOW else: x, y = bz.at((dt / self.duration)**self.time_warp) c = Circle(x + self.path.a.x, y + self.path.a.y, width=45, color=(0., 1., 1., 1.)) c.render() text = '[%i,%i] t**%0.2f d=%0.2f n=%d p=%d' % ( self.mouse.x, self.mouse.y, self.time_warp, self.duration, len(self.paths), self.pathp) label = pyglet.text.Label(text, font_name=self.font_name, font_size=self.font_size, color=(0, 128, 128, 128), x=100, y=20) label.draw()
def drawO(self, posXY): [px, py] = self.boardPositionToPixelCenter(posXY) circle = Circle(x=px, y=py, z=1, width=self.cell_size * 0.9, color=self.colorO, stroke=5) circle.render()
def render(self, batch, group=None): self.frame.render(batch) l_x, l_y = self.calc_label_position(self.label) label = pyglet.text.Label(self.text, x=l_x, y=l_y, batch=batch) circ_y = l_y + round(self.label.content_height * 0.33) circ_x = round((self.frame.x + 10)) circle = Circle(5, circ_x, circ_y, fill=self.selected) circle.render(batch)
def process_circle(self, c_info, unnamed=True): if isinstance(c_info, str) and not is_number(c_info): assert (Circle(c_info) in self.circles) return Circle(c_info) if not isinstance(c_info, tuple): raise NotImplementedError( f"[process_circle] c_info must be tuple or string") c_pred = c_info[0].lower() ps = [self.process_point(p) for p in c_info[1:]] c_val = None if c_pred == "circ": assert (len(ps) == 3) c_val = FuncInfo("c3", ps) elif c_pred == "coa": assert (len(ps) == 2) c_val = FuncInfo(c_pred, ps) elif c_pred == "diam": assert (len(ps) == 2) c_val = FuncInfo("diam", ps) elif c_pred == "circumcircle": assert (len(ps) == 3) c_val = FuncInfo("circumcircle", ps) elif c_pred == "incircle": assert (len(ps) == 3) c_val = FuncInfo("incircle", ps) elif c_pred == "excircle": assert (len(ps) == 3) c_val = FuncInfo("excircle", ps) elif c_pred == "mixtilinear-incircle": assert (len(ps) == 3) c_val = FuncInfo(c_pred, ps) if c_val is not None: C = Circle(c_val) if unnamed: self.unnamed_circles.append(C) return C else: raise NotImplementedError( f"[process_circle] Unsupported circle pred: {c_pred}")
def drawCell(self, posXY, pixelCenter, cell_size): x = self.board.xyToIndex(posXY) v = self.assessments[x] if v <= -100: return if not self.dev: return stand = (v - self.average) / self.dev circle = Circle(x=pixelCenter[0], y=pixelCenter[1], z=1, width=cell_size * 0.5, color=(stand, 0, 0, 0.98 * stand), stroke=5) circle.render()
def compute(self, cmd): assert (len(cmd) == 4) obj_name = cmd[1] assert (isinstance(obj_name, str)) obj_type = cmd[2].lower() assert (obj_type in ["point", "line", "circle"]) if obj_type == "point": p = Point(obj_name) self.register_pt(p) computation = self.process_point(cmd[3], unnamed=False) assert (not isinstance(computation.val, str)) c_instr = Compute(p, computation) self.instructions.append(c_instr) elif obj_type == "line": l = Line(obj_name) self.register_line(l) computation = self.process_line(cmd[3], unnamed=False) assert (not isinstance(computation.val, str)) c_instr = Compute(l, computation) self.instructions.append(c_instr) elif obj_type == "circle": c = Circle(obj_name) self.register_circ(c) computation = self.process_circle(cmd[3], unnamed=False) assert (not isinstance(computation.val, str)) c_instr = Compute(c, computation) self.instructions.append(c_instr) else: raise RuntimeError("Invalid define type")
def param(self, cmd): assert (len(cmd) == 3 or len(cmd) == 4) obj_type = cmd[2].lower() assert (obj_type in ["point", "line", "circle"]) if obj_type == "line": l = Line(cmd[1]) self.register_line(l) param_method = "line" if len(cmd) == 4: param_method = cmd[3] pred, args = self.process_param_line(param_method) p_instr = Parameterize(l, (pred, args)) self.instructions.append(p_instr) elif obj_type == "circle": c = Circle(cmd[1]) self.register_circ(c) param_method = "circle" if len(cmd) == 4: param_method = cmd[3] pred, args = self.process_param_circ(param_method) p_instr = Parameterize(c, (pred, args)) self.instructions.append(p_instr) else: p = Point(cmd[1]) self.register_pt(p) param_method = "coords" if len(cmd) == 4: param_method = cmd[3] pred, args = self.process_param_point(param_method) p_instr = Parameterize(p, (pred, args)) self.instructions.append(p_instr)
def process_constraint(self, constraint): assert (isinstance(constraint, tuple)) negate = (isinstance(constraint[0], str) and constraint[0].lower() == "not") if negate: constraint = constraint[1] pred = constraint[0].lower() args = constraint[1:] args = [self.process_term(t) for t in args] ps = [t for t in args if isinstance(t, Point)] ls = [t for t in args if isinstance(t, Line)] cs = [t for t in args if isinstance(t, Circle)] ns = [t for t in args if isinstance(t, Num)] # Validate if pred in ["circumcenter", "orthocenter", "incenter", "centroid"]: assert (len(args) == 4) assert (all([isinstance(t, Point) for t in args])) elif pred in ["con-tri", "sim-tri"]: assert (len(args) == 6) assert (all([isinstance(t, Point) for t in args])) elif pred == "coll": assert (len(args) == 3) assert (all([isinstance(t, Point) for t in args])) elif pred == "concur": assert (len(args) == 3) assert (all([isinstance(t, Line) for t in args])) elif pred == "cong": assert (len(args) == 4) assert (all([isinstance(t, Point) for t in args])) elif pred == "cycl": assert (len(args) >= 4) assert (all([isinstance(t, Point) for t in args])) self.unnamed_circles.append(Circle(FuncInfo("c3", args[:3]))) elif pred == "eq" or pred == "=": assert (len(args) == 2) if all([isinstance(t, Num) for t in args]): pred = "eq-n" elif all([isinstance(t, Point) for t in args]): pred = "eq-p" elif all([isinstance(t, Line) for t in args]): pred = "eq-l" else: raise RuntimeError("Invalid usage of eq") elif pred == "foot": assert (len(args) == 3) assert (isinstance(args[0], Point) and isinstance(args[1], Point)) assert (isinstance(args[2], Line)) elif pred == "gt" or pred == ">": pred = "gt" assert (len(args) == 2) assert (all([isinstance(t, Num) for t in args])) elif pred == "gte" or pred == ">=": pred = "gte" assert (len(args) == 2) assert (all([isinstance(t, Num) for t in args])) elif pred == "lt" or pred == "<": pred = "lt" assert (len(args) == 2) assert (all([isinstance(t, Num) for t in args])) elif pred == "lte" or pred == "<=": pred = "lte" assert (len(args) == 2) assert (all([isinstance(t, Num) for t in args])) # elif pred == "eq-angle": # assert(len(args) == 8) # assert(all([isinstance(t, Point) for t in args])) elif pred == "eq-ratio": assert (len(args) == 8) assert (all([isinstance(t, Point) for t in args])) elif pred == "i-bisector": assert (len(args) == 4) assert (all([isinstance(t, Point) for t in args])) elif pred == "inter-ll": assert (len(args) == 5) assert (all([isinstance(t, Point) for t in args])) elif pred == "in-poly": assert (len(args) >= 4) assert (all([isinstance(t, Point) for t in args])) elif pred == "midp": assert (len(args) == 3) assert (all([isinstance(t, Point) for t in args])) elif pred == "on-line": assert (len(args) == 2) assert (isinstance(args[0], Point) and isinstance(args[1], Line)) elif pred in ["on-seg", "on-ray"]: # no more between assert (len(args) == 3) assert (all([isinstance(t, Point) for t in args])) elif pred == "on-circ": assert (len(args) == 2) assert (isinstance(args[0], Point) and isinstance(args[1], Circle)) elif pred == "opp-sides": assert (len(args) == 3) assert (isinstance(args[0], Point) and isinstance(args[1], Point)) assert (isinstance(args[2], Line)) elif pred == "para" or pred == "perp": assert (len(args) == 2) assert (all([isinstance(t, Line) for t in args])) elif pred in ["right", "right-tri"]: assert (len(args) == 3) assert (all([isinstance(t, Point) for t in args])) elif pred == "reflect-pl": assert (len(args) == 4) assert (all([isinstance(t, Point) for t in args])) elif pred == "same-side": assert (len(args) == 3) assert (isinstance(args[0], Point) and isinstance(args[1], Point)) assert (isinstance(args[2], Line)) elif pred == "tangent-cc": assert (len(args) == 2) assert (all([isinstance(t, Circle) for t in args])) elif pred == "tangent-lc": assert (len(args) == 2) assert (isinstance(args[0], Line) and isinstance(args[1], Circle)) elif pred == "tangent-at-cc": assert (len(args) == 3) assert (isinstance(args[0], Point)) assert (isinstance(args[1], Circle) and isinstance(args[2], Circle)) elif pred == "tangent-at-lc": assert (len(args) == 3) assert (isinstance(args[0], Point)) assert (isinstance(args[1], Line) and isinstance(args[2], Circle)) else: raise NotImplementedError( f"[process_constraint] Unsupported pred {pred}") return negate, pred, args
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