def update_widget_graphics(self, *l): if not self.activated: return if self.widget is None: self.grect.size = 0, 0 return gr = self.grect widget = self.widget # determine rotation a = Vector(1, 0) if widget is self.win: b = Vector(widget.to_window(0, 0)) c = Vector(widget.to_window(1, 0)) else: b = Vector(widget.to_window(*widget.to_parent(0, 0))) c = Vector(widget.to_window(*widget.to_parent(1, 0))) - b angle = -a.angle(c) # determine scale scale = c.length() # apply transform gr.size = widget.size if widget is self.win: self.gtranslate.xy = Vector(widget.to_window(0, 0)) else: self.gtranslate.xy = Vector(widget.to_window(*widget.pos)) self.grotate.angle = angle # fix warning about scale property deprecation self.gscale.xyz = (scale,) * 3
def check_slide_bbox(self): if self.app.config.getint('viewer', 'thumb') == 1: img_point = (self.bbox[0][0] + self.bbox[1][0], self.bbox[0][1] + self.bbox[1][1]) parent_point = (self.parent.size[0] + self.parent.pos[0], self.parent.size[1] + self.parent.pos[1]) if not Vector.in_bbox(self.bbox[0], self.parent.pos, parent_point) or not Vector.in_bbox(img_point, self.parent.pos, parent_point): if all(child.id != 'img_zoom' for child in self.parent.children): # TODO: Change thumbnail position and size based on config. thumb = AsyncImage(source=self.image.source, id='img_zoom', size_hint=(None, None), keep_ratio=True, size=(self.parent.size[0] / 5, (self.parent.size[0] / 5) / self.image.image_ratio), pos=[0, 0.05 * self.parent.size[1]]) self.parent.add_widget(thumb) elif min(self.bbox[0]) > 0: img_zoom = [child for child in self.parent.children if child.id == 'img_zoom'] try: self.parent.remove_widget(img_zoom[0]) except IndexError: pass
def test_intersection_roundingerror(self): # ref #2983, #5568 v1 = (25.0, 200.0) v2 = (25.0, 400.0) v3 = (36.75, 300.0) result = [25.0, 300.0] def almost(a, b): # 300.0 sometimes is 299.9.. or 300.1.. however # we just want to know that it's really close self.assertIsNotNone(a) self.assertIsNotNone(b) self.assertAlmostEqual(a[0], b[0], places=0) self.assertAlmostEqual(a[1], b[1], places=0) for i in range(1, 100): st = "6.4" + "9" * i v = (float(st), 300.0) almost(result, Vector.segment_intersection(v1, v2, v3, v)) for i in range(1, 100): st = "6.1" + "1" * i v = (float(st), 300.0) almost(result, Vector.segment_intersection(v1, v2, v3, v)) for i in range(1, 100): st = "6.4" + "4" * i v = (float(st), 300.0) almost(result, Vector.segment_intersection(v1, v2, v3, v)) for i in range(1, 100): st = "300.4" + "9" * i v = (6.5, float(st)) almost(result, Vector.segment_intersection(v1, v2, v3, v))
class Ball(Widget): r = 30 / 2 r2 = r**2 trail_pts = [] g_trail = "trail" def setup(self): self.pos = (cx, cy+cy/2) self.velocity = Vector(0,5) def move(self, dt): self.canvas.remove_group(self.g_trail) with self.canvas: Color(1, 1, 1, 0.5, mode='rgba', group=self.g_trail) Line(points=sum(self.trail_pts, []), group=self.g_trail) to_center = (Vector(cx, cy) - Vector(self.pos)) l = self.velocity.length() cen_d = to_center.length() self.velocity = self.velocity * 0.99 + to_center.normalize() * sqrt(cen_d) * 0.04 if l > 25: self.velocity = self.velocity.normalize() * 20 self.pos = Vector(self.pos) + self.velocity * dt * 30 if len(self.trail_pts) == 0: self.trail_pts = [self.pos] else: self.trail_pts.insert(0, self.pos) while len(self.trail_pts) > 30: self.trail_pts.pop()
def circleToCircle(a, b): v = Vector(a.pos) - Vector(b.pos) if v.length() < a.r+b.r: return (True, None, v.normalize()) return (False, None, None)
def avoid_obstacles_vector(self, entity_id, position): entities = self.gameworld.entities ship_system = entities[entity_id].ship_system obstacles_to_avoid = ship_system.in_view sum_avoidance = Vector(0, 0) ob_count = 0 for obstacle in obstacles_to_avoid: if obstacle != entity_id: obstacle = entities[obstacle] if not hasattr(obstacle, 'cymunk_physics') or hasattr( obstacle, 'boundary_system'): continue ob_location = obstacle.position dist = Vector((ob_location.x, ob_location.y)).distance( (position.x, position.y)) scale_factor = (150.-dist)/150. avoidance_vector = Vector( (position.x, position.y)) - Vector( (ob_location.x, ob_location.y)) avoidance_vector = avoidance_vector.normalize() avoidance_vector *= scale_factor sum_avoidance += avoidance_vector ob_count += 1 if ob_count > 0: sum_avoidance /= float(ob_count) sum_avoidance *= ship_system.max_speed return sum_avoidance
def on_touch_move(self, touch): if touch.grab_current is not self.transform: return touch_point_in_parent = self.transform.to_parent(*touch.pos) if len(self.touches) == 1 and self.do_translation: ptx, pty = self.transform.pos px0, py0 = self.transform.to_parent(touch.px, touch.py) px1, py1 = touch_point_in_parent pdx, pdy = px1 - px0, py1 - py0 self.transform.pos = ptx + pdx, pty + pdy else: touch_point = Vector(touch_point_in_parent) points = [self.prev_pos[t] for t in self.touches] pivot = max(points, key=touch_point.distance) farthest = max(points, key=pivot.distance) if points.index(farthest) == self.touches.index(touch): old_line = Vector(self.transform.to_parent(*touch.ppos)) - pivot new_line = Vector(touch_point_in_parent) - pivot vx, vy = self.transform.viewport_pos local_pivot = self.transform.to_local(*pivot) self.transform.viewport_pos = vx-local_pivot[0], vy-local_pivot[1] if self.do_rotation: self.transform.do_rotate(radians(new_line.angle(old_line))) if self.do_scale: ratio = new_line.length() / old_line.length() self.transform.do_scale(ratio, ratio) self.transform.viewport_pos = vx, vy self.prev_pos[touch] = Vector(touch_point_in_parent)
def test_inbbox(self): bmin = (0, 0) bmax = (100, 100) result = Vector.in_bbox((50, 50), bmin, bmax) self.assertTrue(result) result = Vector.in_bbox((647, -10), bmin, bmax) self.assertFalse(result)
def on_touch_up(self, touch): if not self.collide_point(*touch.pos): return vec=Vector(touch.pos)-Vector(touch.opos) if vec.length()>10: if abs(vec.x)>abs(vec.y): if vec.x>0: self.fadeMenu()
def update(self, dt): if self.expired: return True pos = Vector(self.pos) pos.y = pos.y + self.delta * dt self.pos = pos.x, pos.y self.incState() self.expiryCheck() return self.expired
def update(self, dt): self.ball.move(dt) col = self.paddle.collide_widget(self.ball) if col[0]: #print dt,"bam" self.sounds["hit_2"].play() d = self.ball.r + self.paddle.r - (Vector(self.ball.pos) - Vector(self.paddle.pos)).length() #print d self.ball.pos = Vector(self.ball.pos) + col[2] * d v = self.ball.velocity v_n = v.normalize() normal = col[2] m = max((280-v.length2())/100,1) v = v - normal * 2 * (normal.dot(v)) * m self.ball.velocity = v killspace_col_paddle = self.killspace.collide_widget (self.ball) # WE DIED! if killspace_col_paddle[0]: self.sounds["die"].play() self.start() for w in self.blocks: col = w.collide_widget(self.ball) if col[0]: self.sounds["hit"].play() closest = col[1] circle = self.ball mtd = Vector(circle.pos[0], circle.pos[1]) - closest mtd = mtd.normalize() pos = closest + mtd * 1.05 * self.ball.size[0]/2 self.ball.pos = pos v = self.ball.velocity normal = col[2] v = v - normal * 2 * (normal.dot(v)) self.ball.velocity = v self.remove_widget (w) self.blocks.remove(w) self.score += 5 + 2 * self.level_num self.best_score = max(self.score, self.best_score) if len(self.blocks) == 0: self.n_rings += 1 self.level_num += 1 self.ball.setup() self.generate_level() break
def on_touch_up(self, touch): v = Vector(touch.pos) - Vector(touch.opos) if v.length() < 20: return if abs(v.x) > abs(v.y): v.y = 0 else: v.x = 0 self.move(*v.normalize())
def on_touch_up(self, touch): v = Vector(touch.pos) - Vector(touch.opos) if v.length() < dp(20): return # detect direction dx, dy = v if abs(dx) > abs(dy): self.move_leftright(dx > 0) else: self.move_topdown(dy > 0)
def get_poligon(vertices, density, center_x, center_y, radius, angle=None): """ Returns coordinates of star poligon: (x1, y1), (x1, y2), ... """ v = Vector(0, radius) if angle: v = v.rotate(angle) for i in xrange(vertices+1): v = v.rotate(-float(360)/vertices * density) yield v.x + center_x, v.y + center_y
def transform_with_touch(self, touch): # just do a simple one finger drag changed = False if len(self._touches) == self.translation_touches: # _last_touch_pos has last pos in correct parent space, # just like incoming touch dx = (touch.x - self._last_touch_pos[touch][0]) \ * self.do_translation_x dy = (touch.y - self._last_touch_pos[touch][1]) \ * self.do_translation_y dx = dx / self.translation_touches dy = dy / self.translation_touches self.apply_transform(Matrix().translate(dx, dy, 0)) changed = True if len(self._touches) == 1: return changed # we have more than one touch... points = [Vector(self._last_touch_pos[t]) for t in self._touches] # we only want to transform if the touch is part of the two touches # furthest apart! So first we find anchor, the point to transform # around as the touch farthest away from touch anchor = max(points, key=lambda p: p.distance(touch.pos)) # now we find the touch farthest away from anchor, if its not the # same as touch. Touch is not one of the two touches used to transform farthest = max(points, key=anchor.distance) if points.index(farthest) != self._touches.index(touch): return changed # ok, so we have touch, and anchor, so we can actually compute the # transformation old_line = Vector(*touch.ppos) - anchor new_line = Vector(*touch.pos) - anchor angle = radians(new_line.angle(old_line)) * self.do_rotation self.apply_transform(Matrix().rotate(angle, 0, 0, 1), anchor=anchor) if self.do_scale: scale = new_line.length() / old_line.length() new_scale = scale * self.scale if new_scale < self.scale_min: scale = self.scale_min / self.scale elif new_scale > self.scale_max: scale = self.scale_max / self.scale self.apply_transform(Matrix().scale(scale, scale, scale), anchor=anchor) changed = True return changed
def nearest_square(self, current): cpos = Vector(current.pos) nearest_d = 999 nearest_child = None for child in self.children: if not isinstance(child, PentaminoSquare): continue if child is current: continue d = cpos.distance(child.pos) if d < nearest_d: nearest_d = d nearest_child = child return nearest_child, nearest_d
def load(self, fling_board): num_points = 24 r = 250 center_x = fling_board.width / 2. center_y = fling_board.height / 2. black_hole = BlackHole(pos=(center_x, center_y)) fling_board.add_black_hole(black_hole) v = Vector(r, 0) rotation_angle = 360. / num_points for i in range(num_points): goal_point = GoalPoint(pos=(center_x + v.x, center_y + v.y)) fling_board.add_goal_point(goal_point) v = v.rotate(rotation_angle)
def get_rigid_rotation(self, dstpts): ''' Extract the rotation to apply to a group of points to minimize the distance to a second group of points. The two groups of points are assumed to be centered. This is a simple version that just pick an angle based on the first point of the gesture. ''' if len(self.strokes) < 1 or len(self.strokes[0].points) < 1: return 0 if len(dstpts.strokes) < 1 or len(dstpts.strokes[0].points) < 1: return 0 p = dstpts.strokes[0].points[0] target = Vector([p.x, p.y]) source = Vector([p.x, p.y]) return source.angle(target)
def _get_scale_y(self): p1 = Vector(*self.to_parent(0, 0)) p2 = Vector(*self.to_parent(0, 1)) scale_y = p1.distance(p2) # XXX float calculation are not accurate, and then, scale can be # throwed again even with only the position change. So to # prevent anything wrong with scale, just avoid to dispatch it # if the scale "visually" didn't change. #947 # Remove this ugly hack when we'll be Python 3 only. if hasattr(self, '_scale_py'): if str(scale_y) == str(self._scale_py): return self._scale_py return scale_y
def find_double_tap(self, ref): '''Find a double tap touch within self.touches. The touch must be not a previous double tap, and the distance must be ok, also, the touch profile must be compared so the kind of touch is the same ''' ref_button = None if 'button' in ref.profile: ref_button = ref.button for touchid in self.touches: if ref.uid == touchid: continue etype, touch = self.touches[touchid] if etype != 'end': continue if touch.is_double_tap: continue distance = Vector.distance( Vector(ref.sx, ref.sy), Vector(touch.osx, touch.osy)) if distance > self.double_tap_distance: continue if touch.is_mouse_scrolling or ref.is_mouse_scrolling: continue touch_button = None if 'button' in touch.profile: touch_button = touch.button if touch_button != ref_button: continue touch.double_tap_distance = distance return touch return None
def _calculate_resistance_vector(self, affection_zone): """Calculate vector from self to widget :param affection_zone: bitmap of collission sector :type affection_zone: numpy.ones :returns: vector from self to widget :rtype: kivy.vector.Vector """ resistance_vector = Vector(0, 0) for x in range(affection_zone.shape[0]): for y in range(affection_zone.shape[1]): if not affection_zone[x, y]: resistance_vector += Vector( x - (affection_zone.shape[0] - 1) / 2, y - (affection_zone.shape[1] - 1) / 2).normalize() return resistance_vector.normalize()
def move(self, previousPart, magnitude): #moves an individual part #first calculates the previous part's position and vectorizes it previousPos = (previousPart.absolutePos[0],previousPart.absolutePos[1]) destination = Vector(previousPos[0], previousPos[1]) current = (self.absolutePos[0], self.absolutePos[1]) velocity = Vector(destination.x-current[0],destination.y-current[1]) #determines velocity based on destination and current position if destination.distance(current) < self.width: #realigns the part velocity so that parts don't outrun the head #when the head turns magnitude *= velocity.length() / self.width velocity = velocity.normalize() * magnitude self.absolutePos[0] += velocity.x self.absolutePos[1] += velocity.y self.wrapAround()
def find_triple_tap(self, ref): """Find a triple tap touch within *self.touches*. The touch must be not be a previous triple tap and the distance must be be within the bounds specified. Additionally, the touch profile must be the same kind of touch. """ ref_button = None if "button" in ref.profile: ref_button = ref.button for touchid in self.touches: if ref.uid == touchid: continue etype, touch = self.touches[touchid] if not touch.is_double_tap: continue if etype != "end": continue if touch.is_triple_tap: continue distance = Vector.distance(Vector(ref.sx, ref.sy), Vector(touch.osx, touch.osy)) if distance > self.triple_tap_distance: continue if touch.is_mouse_scrolling or ref.is_mouse_scrolling: continue touch_button = None if "button" in touch.profile: touch_button = touch.button if touch_button != ref_button: continue touch.triple_tap_distance = distance return touch return None
def __init__(self, image = "images/fish.png", box = [0, 0, 100, 100], **kwargs): self.direction = Vector(-1, 0) self.angle = 1 self.size = (48,48) self.box = box self.center = (Window.width / 2, Window.height) self.image = Image(source=image, allow_stretch=True, size=self.size) # Can't be arsed to 'rotate' texture 'properly', this is so frikin more simple self.texture_left = self.image.texture.get_region(0, 0, 194, 192) self.texture_right = self.image.texture.get_region(205, 0, 194, 192) self.image.texture = self.texture_left self.target_pos = self.center super(Fish, self).__init__(**kwargs) self.add_widget(self.image) self.register_event_type('on_death') # Every living creature consumes own self self.bind(active=lambda instance, value: Clock.schedule_interval(instance.consume_calories, 0.5) if value else Clock.unschedule(instance.consume_calories)) # Dynamic entry self.bind(active=lambda instance, value: Animation(y=Window.height - 400, t="out_back", d=1.2).start(instance) if value else True) # Too many calories make you obese self.bind(total_calories=self.lvlup)
def setup_mode_free(self): """Setup the keyboard in free mode. Free mode is designed to let the user control the position and orientation of the keyboard. The only real usage is for a multiuser environment, but you might found other ways to use it. If a :data:`target` is set, it will place the vkeyboard under the target. .. note:: Don't call this method directly, use :meth:`setup_mode` instead. """ self.do_translation = True self.do_rotation = True self.do_scale = True target = self.target if not target: return # NOTE all math will be done in window point of view # determine rotation of the target a = Vector(1, 0) b = Vector(target.to_window(0, 0)) c = Vector(target.to_window(1, 0)) - b self.rotation = -a.angle(c) # determine the position of center/top of the keyboard dpos = Vector(self.to_window(self.width / 2.0, self.height)) # determine the position of center/bottom of the target cpos = Vector(target.to_window(target.center_x, target.y)) # the goal now is to map both point, calculate the diff between them diff = dpos - cpos # we still have an issue, self.pos represent the bounding box, not the # 0,0 coordinate of the scatter. we need to apply also the diff between # them (inside and outside coordinate matrix). It's hard to explain, but # do a scheme on a paper, wrote all the vector i'm calculating, and # you'll understand. :) diff2 = Vector(self.x + self.width / 2.0, self.y + self.height) - Vector( self.to_parent(self.width / 2.0, self.height) ) diff -= diff2 # now we have a good "diff", set it as a pos. self.pos = -diff
def test_(self): a = (98, 28) b = (72, 33) c = (10, -5) d = (20, 88) result = Vector.line_intersection(a, b, c, d) self.assertEqual(result.x, 15.25931928687196) self.assertEqual(result.y, 43.911669367909241)
def __init__(self, *args, gravity=(0, 0), **kwargs): """PlainPisics constructor :param gravity: gravity vector :param gravity: kivy.Vector """ super(PlainPhisics, self). __init__(*args, **kwargs) self.gravity = Vector(gravity)
def _get_acceleration(self, world_object): """Returns object's acceleration change :param world_object: object which acceleration will be changed :type world_object: parabox.base_object.BaseObject :return: acceleration change :rtype: Vector """ acceleration_vector = Vector( self.x - world_object.x, self.y - world_object.y) if self.affect_radius < acceleration_vector.length(): acceleration_vector *= 0 else: acceleration_vector *= ( (1 - acceleration_vector.length() / self.affect_radius) * self.gravity) return acceleration_vector
def highlight_at(self, *largs): '''A function to highlight the current self.widget''' gr = self.grect widget = self.widget # determine rotation a = Vector(1, 0) b = Vector(widget.to_window(*widget.to_parent(0, 0))) c = Vector(widget.to_window(*widget.to_parent(1, 0))) - b angle = -a.angle(c) # determine scale scale = c.length() # apply transform gr.size = widget.size self.gtranslate.xy = Vector(widget.to_window(*widget.pos)) self.grotate.angle = angle self.gscale.scale = scale
def calculate_desired_vector(self, target, location, ship_data, ship_ai_data): g_map = self.gameworld.systems['default_map'] map_size_x = g_map.map_size[0]/1.9 map_size_y = g_map.map_size[1]/1.9 dist_x = math.fabs(target[0] - location[0]) dist_y = math.fabs(target[1] - location[1]) ship_ai_data['distance_to_target'] = Vector(target).distance2(location) max_speed = ship_data['max_speed'] v = Vector(target) - Vector(location) v = v.normalize() v *= max_speed if ship_ai_data['ai_state'] == 'flee': v *= -1 if dist_x > map_size_x: v[0] *=-1 if dist_y > map_size_y: v[1] *=-1 return v
def distance(self, other_touch): '''Return the distance between the current touch and another touch. ''' return Vector(self.pos).distance(other_touch.pos)
def move(self): self.pos = Vector(*self.velocidade) + self.pos
def move(self, rotation): print("moving by",rotation) self.pos = Vector(*self.velocity) + self.pos self.rotation = rotation self.angle = self.angle + self.rotation
def update(self): self.pos = Vector(*self.velocity) + self.pos
def expiryCheck(self): if Vector(self.pos).y > self.parent.height: self.expired = True
def calcPos(self, current): current = Vector(current) dx, dy = current.x * self.velocity_x, current.y * self.velocity_y return Vector(dx, dy) + Vector(self.parent.pos)
def step(self, action, last_distance): global goal_x global goal_y global done global swap global sand_penalty global living_penalty #car = car() self.car.move(action) xx = goal_x - self.car.x yy = goal_y - self.car.y done = False obs, orientation, new_distance = self.get_obs(xx, yy) if sand[int(self.car.x), int(self.car.y)] > 0: self.car.velocity = Vector(1, 0).rotate(self.car.angle) #print(1, goal_x, goal_y, new_distance, max(int(self.car.x),0),max(int(self.car.y),0), im.read_pixel(max(int(self.car.x),0),max(int(self.car.y),0))) #Penalty for going on sand reward = -0.3 sand_penalty += 0.3 else: self.car.velocity = Vector(2, 0).rotate(self.car.angle) reward = 1.5 living_penalty += 1.5 #print(0, goal_x, goal_y, new_distance, max(int(self.car.x),0),max(int(self.car.y),0), im.read_pixel(max(int(self.car.x),0),max(int(self.car.y),0))) if new_distance < last_distance: #Reward for going towards goal reward += 2 living_penalty += 2 #else: # reward = -0.5 #last_reward = last_reward +(-0.2) #Adding done condition and negative reward for going near borders # self.car.velocity = Vector(2, 0).rotate(self.car.angle) # reward = 1 if self.car.x < 5: self.car.x = 5 reward -= 10 sand_penalty += 10 done = True if self.car.x > self.width - 5: self.car.x = self.width - 5 reward -= 10 sand_penalty += 10 done = True if self.car.y < 5: self.car.y = 5 reward -= 10 sand_penalty += 10 done = True if self.car.y > self.height - 5: self.car.y = self.height - 5 reward -= 10 sand_penalty += 10 done = True if new_distance < 25: if swap == 1: goal_x = 360 goal_y = 315 swap = 0 file1 = open("rewards.txt", "a") file1.write( "Goal1Total Timesteps: {} Episode Num: {} Reward: {}". format(total_timesteps, episode_num, episode_reward)) file1.write("\n") file1.close() else: goal_x = 1080 goal_y = 420 swap = 1 file1 = open("rewards.txt", "a") file1.write( "Goal2Total Timesteps: {} Episode Num: {} Reward: {}". format(total_timesteps, episode_num, episode_reward)) file1.write("\n") file1.close() #Reward for reaching the Goal reward += 25 done = True return obs, reward, done, new_distance, orientation
def putDot(self): self.dot.center = self.center self.dot.velocity = Vector(0, 0)
def serve_car(self): self.car.center = self.center self.car.velocity = Vector(6, 0)
def move(self): self.pos = Vector(*self.velocity) + self.pos
def serve_ball(self): self.ball.center = self.center self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))
def update(self, dt): global brain global last_reward global scores global last_distance global goal_x global goal_y global longueur global largeur global swap longueur = self.width largeur = self.height if first_update: init() # print("self.car.x = ", self.car.x) # print("self.car.y = ", self.car.y) xx = goal_x - self.car.x yy = goal_y - self.car.y orientation = Vector(*self.car.velocity).angle((xx, yy)) / 180. last_signal = [ self.car.signal1, self.car.signal2, self.car.signal3, orientation, -orientation ] action = brain.update(last_reward, last_signal) scores.append(brain.score()) rotation = action2rotation[action] self.car.move(rotation) distance = np.sqrt((self.car.x - goal_x)**2 + (self.car.y - goal_y)**2) self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3 # if car walks on sand if sand[int(self.car.x), int(self.car.y)] > 0: self.car.velocity = Vector(0.5, 0).rotate(self.car.angle) # print(1, goal_x, goal_y, distance, int(self.car.x),int(self.car.y), im.read_pixel(int(self.car.x),int(self.car.y))) last_reward = -5.0 # car not on sand else: self.car.velocity = Vector(2, 0).rotate(self.car.angle) last_reward = -0.2 # print(0, goal_x, goal_y, distance, int(self.car.x),int(self.car.y), im.read_pixel(int(self.car.x),int(self.car.y))) if distance < last_distance: last_reward = 5.0 else: last_reward = last_reward + (-1.0) if self.car.x < 10: self.car.x = 10 last_reward = -10.0 if self.car.x > self.width - 10: self.car.x = self.width - 10 last_reward = -10.0 if self.car.y < 10: self.car.y = 10 last_reward = -10.0 if self.car.y > self.height - 10: self.car.y = self.height - 10 last_reward = -10.0 if distance < 25: if swap == 0: print("\n\n\nPatient 1 FOUND, Moving to Patient 2") # patient2 home goal_x = 845 goal_y = 703 swap = 1 elif swap == 1: print("\n\n\nPatient 2 FOUND, Moving to Hospital") #hospital goal_x = 350 goal_y = 349 swap = 2 else: # patient1 home goal_x = 985 goal_y = 428 swap = 0 print("\n\n\nHospital FOUND, Moving to Patient 1") last_distance = distance
def move(self): self.pos = Vector(*self.vel) + self.pos
def touch_down(self, point): if not self.lastPoint: self.lastPoint = Vector(point.x, point.y)
def step(self, dt, levelsGap, bullets): # 160 x 25 (parent in velocity units) # 128 x 16 (invader grid) # If our current Y position starts at 25 - 16 = 9, then we iterate until that # position is zero (adjusted for max_row). # We can decrement Ypos only when we reach the sides (adjusted for max_col/min_col # min_col, max_col = None, None count = 0 self.bombs = [] max_row = None for level in self.children: level.checkState(bullets, dt) count = count + level.count if level.min_col is not None: if min_col is None or min_col > level.min_col: min_col = level.min_col if level.max_col is not None: if max_col is None or max_col < level.max_col: max_col = level.max_col if min_col is not None and max_col is not None: if max_row is None or level.row > max_row: max_row = level.row self.bombs.extend( level.bombs) # building list of known valid bombs self.noMoreInvaders = False if min_col is None or max_col is None: self.noMoreInvaders = True return False # Invaders are all shot up. Restart level self.min_col, self.max_col = min_col, max_col self.counter = self.counter + dt if self.ship: if not self.ship.update(dt): self.ship = None else: for bullet in bullets: if self.ship.beenShot(bullet): self.ship = None # already removed from parent break else: if randint(0, 10000) < 10: # Do we spawn a ship? one in a thousand if randint(0, 20) < 4: # yes, which kind? big is 80% of the time self.ship = lilShip(self.parent) else: self.ship = bigShip(self.parent) self.parent.add_widget(self.ship) if self.counter > self.maxDelta: completion = 1 - (count / (AliensPerRow * AlienRows + 1.0)) self.counter = 0 invaderWidth = self.width / AliensPerRow # invaders per level invaderHeight = self.height / AlienRows # invader row count min_pos = min_col * -invaderWidth # adjust for invaders that have been shot max_pos = levelsGap + ( AliensPerRow - 1 - max_col) * invaderWidth # width (40) + half spacing(20) = 50 current = Vector(self.current) current = Vector(current.x + self.speed(completion), current.y) # Add one to current pos = self.calcPos(current) # Convert to position if pos.x > max_pos: self.direction = self.direction * -1 # toggle direction current = Vector(current.x + self.speed(completion), current.y - 1) # Reduce Y if pos.x < min_pos: self.direction = self.direction * -1 # toggle direction current = Vector(current.x + self.speed(completion), current.y - 1) # Reduce Y pos = self.calcPos(current) # Convert to position self.current = current.x, current.y # Adjust the speed based on a few things sensitivity = 20 # controls speedup for closeness to ground level factor = (sensitivity + current.y) / (sensitivity + 25.0) self.maxDelta = 0.05 + 0.45 * ( 1 - completion) # speed depends on number of remaining aliens self.maxDelta = 0.05 + self.maxDelta * factor # and closeness to completion self.pos = pos.x, pos.y for level in self.children: level.step() height_adj = (AlienRows - 1 - max_row) * (invaderHeight / self.velocity_y) if int(current.y + height_adj) < 0: return True # Game over; invaders progressed past end else: ofs = int(completion * 4) speeds = [ 'fastinvader1', 'fastinvader2', 'fastinvader3', 'fastinvader4' ] soundFile = speeds[ofs] sound = Sounds[soundFile] if sound is not None: sound.stop() sound.play() return False
def update(self, dt): global brain global last_reward global scores global last_distance global goal_x global goal_y global width global height width = self.width # assuming user can change window size runtime height = self.height if first_update: init() # Move the snake xx = self.cheese.pos[0] - self.player.x yy = self.cheese.pos[1] - self.player.y if self.manuel_mode: self.player.move(self.rotation) self.rotation = 0 else: orientation = Vector(*self.player.velocity).angle((xx, yy)) / 180. last_signal = [ self.player.signal1, self.player.signal2, self.player.signal3, orientation ] action = brain.update(last_reward, last_signal) scores.append(brain.score()) self.rotation = action2rotation[action] self.player.move(self.rotation) self.distance = int(np.sqrt(xx**2 + yy**2)) self.ball1.pos = self.player.sensor1 self.ball2.pos = self.player.sensor2 self.ball3.pos = self.player.sensor3 # Punishments and rewards try: if field[int(self.player.x), int(self.player.y)] > 0: self.player.velocity = Vector(9, 0).rotate(self.player.angle) last_reward = -1 else: self.player.velocity = Vector(9, 0).rotate(self.player.angle) last_reward = -0.2 if self.distance < last_distance: last_reward = 0.1 except IndexError: self.player.velocity = Vector(9, 0).rotate(self.player.angle) last_reward = -1 if self.rotation != 0 and last_reward != -1: last_reward -= 0.05 for trap in self.trap.trap_objects: aa = self.player.x - trap.pos[0] bb = self.player.y - trap.pos[1] d = np.sqrt(aa**2 + bb**2) if -20 <= d <= 20: # Trappe last_reward = -0.8 self.trap.remove_trap(trap) self.score_holder = 0 self.life -= 1 if self.life <= 0: # Game over reset everything self.life = 5 self.high_score = 0 self.time = 600 self.score_holder = 0 self.generate_cheese() if self.out_of_bound(): last_reward = -1 # check if the cheese is being eaten if self.distance < self.cheese.width: last_reward = 1 self.generate_cheese() self.time = 600 elif self.time <= 0: self.generate_cheese() self.time = 600 self.time -= 1 last_distance = self.distance
def expiryCheck(self): if Vector(self.pos).y < 5: self.expired = True
def pointCol(cx, cy, cr, px, py): """ Return bool of whether circle (represented with arguments 'cx', 'cy', and 'cr') collides with point (represented with arguments 'px' and 'py'.) """ return Vector(cx, cy).distance(Vector(px, py)) <= cr
def update(self): self.pos = Vector(*self.velocity) + self.pos if self.pos[1] <= 104: Clock.unschedule(self.stop_jumping) self.bird_image.source = "res/images/flappynormal.png" self.pos = (self.pos[0], 104)
def draw_line(self, points_list, width=5.): vertex_format = [('vPosition', 2, 'float'), ('vdxdy', 2, 'float'), ('vWidth', 1, 'float'), ('vColor', 4, 'float')] indices = [] ie = indices.extend vertices = [] e = vertices.extend line_color = self.line_color for numpoint in range(len(points_list) - 1): point1 = points_list[numpoint] point2 = points_list[numpoint + 1] dx = point2[0] - point1[0] dy = point2[1] - point1[1] a = Vector(dx, dy).normalize() if numpoint == 0: ie([0, 5, 3, 3, 1, 0, 5, 0, 2, 2, 4, 5]) e([ point1[0], point1[1], 0.0, 0.0, width, line_color[0], line_color[1], line_color[2], line_color[3], point1[0], point1[1], a[0], -a[1], width, 0.0, 0.0, 0.0, 0.0, point1[0], point1[1], -a[0], a[1], width, 0.0, 0.0, 0.0, 0.0, point2[0], point2[1], a[0], -a[1], width, 0.0, 0.0, 0.0, 0.0, point2[0], point2[1], -a[0], a[1], width, 0.0, 0.0, 0.0, 0.0, point2[0], point2[1], 0.0, 0.0, width, line_color[0], line_color[1], line_color[2], line_color[3], ]) else: offset = 5 + (numpoint - 1) * 3 ie([ offset, offset + 3, offset + 1, offset + 1, offset - 2, offset, offset + 3, offset, offset - 1, offset - 1, offset + 2, offset + 3 ]) e([ point2[0], point2[1], a[0], -a[1], width, 0.0, 0.0, 0.0, 0.0, point2[0], point2[1], -a[0], a[1], width, 0.0, 0.0, 0.0, 0.0, point2[0], point2[1], 0.0, 0.0, width, line_color[0], line_color[1], line_color[2], line_color[3], ]) with self.canvas: self.mesh = Mesh( indices=indices, vertices=vertices, fmt=vertex_format, mode='triangles', )
def _set_center(self, center): if center == self.center: return False t = Vector(*center) - self.center trans = Matrix().translate(t.x, t.y, 0) self.apply_transform(trans)
def serve_ball(self): self.ball.velocity = Vector(game_level, 0).rotate(randint(0, 360))
def _get_scale(self): p1 = Vector(*self.to_parent(0, 0)) p2 = Vector(*self.to_parent(1, 0)) scale = p1.distance(p2) return float(scale)
def collide_point(self, x, y): return Vector(x, y).distance(self.center) <= self.width / 2
def serve_player(self): self.player.center = self.center self.player.velocity = Vector(8, 0)
def move(self, rotation): #print("move") self.pos = Vector(*self.velocity) + self.pos ##print(rotation,type(rotation)) self.rotation = rotation self.angle = self.angle + self.rotation
def update(self, dt): global brain global reward global scores global last_distance global goal_x global goal_y global longueur global largeur global swap global orientation global obs # NEW GLOBALS global replay_buffer global seed global start_timesteps global eval_freq #global max_timesteps global save_models global expl_noise global batch_size global discount global tau global policy_noise global noise_clip global policy_freq global done global total_timesteps global timesteps_since_eval global episode_num global episode_reward global reward_window global episode_timesteps global main_img global image_size global last_time_steps # NEW GLOBALS longueur = self.width largeur = self.height if first_update: init() #if total_timesteps < max_timesteps: if True : # If the episode is done if done: # If we are not at the very beginning, we start the training process of the model if total_timesteps != 0: print("Total Timesteps: {} Episode Num: {} Timesteps diff: {} Reward: {} score: {}".format(total_timesteps, episode_num, total_timesteps - last_time_steps,episode_reward, episode_reward/(total_timesteps - last_time_steps))) brain.train(replay_buffer, episode_timesteps, batch_size, discount, tau, policy_noise, noise_clip, policy_freq) last_time_steps = total_timesteps print("devug") # We evaluate the episode and we save the policy # WILL COME TO THIS LATER # if timesteps_since_eval >= eval_freq: # timesteps_since_eval %= eval_freq # evaluations.append(evaluate_policy(policy)) # policy.save(file_name, directory="./pytorch_models") # np.save("./results/%s" % (file_name), evaluations) # state calculation # self.serve_car() # xx = goal_x - self.car.x # yy = goal_y - self.car.y # orientation = Vector(*self.car.velocity).angle((xx,yy))/180. # obs = [self.car.signal1, self.car.signal2, self.car.signal3, orientation, -orientation] # state calculation #cnn state calculation self.serve_car() _,_,obs = get_target_image(main_img, self.car.angle, [self.car.x, self.car.y], image_size) save_cropped_image(obs, self.car.x, self.car.y, name = "initial") xx = goal_x - self.car.x yy = goal_y - self.car.y orientation = Vector(*self.car.velocity).angle((xx,yy))/180. orientation = [orientation, -orientation] #cnn state calculation # When the training step is done, we reset the state of the environment # obs = env.reset() # Set the Done to False done = False # Set rewards and episode timesteps to zero episode_reward = 0 episode_timesteps = 0 episode_num += 1 # Before 10000 timesteps, we play random actions if total_timesteps < start_timesteps: action = [random.uniform(-max_action_agent * 1.0, max_action_agent * 1.0)] #action = env.action_space.sample() else: # After 10000 timesteps, we switch to the model action = brain.select_action(np.array(obs), np.array(orientation)) # If the explore_noise parameter is not 0, we add noise to the action and we clip it if expl_noise != 0: action = (action + np.random.normal(0, expl_noise, size=action_len)).clip(-1*max_action_agent,max_action_agent) # The agent performs the action in the environment, then reaches the next state and receives the reward # ENV STEP PERFORM START if type(action) != type([]): #print("action : ",type(action.tolist()[0]), type(action[0])) self.car.move(action.tolist()[0]) else: self.car.move(action[0]) distance = np.sqrt((self.car.x - goal_x)**2 + (self.car.y - goal_y)**2) self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3 if sand[int(self.car.x),int(self.car.y)] > 0: self.car.velocity = Vector(0.5, 0).rotate(self.car.angle) #print(1, goal_x, goal_y, distance, int(self.car.x),int(self.car.y), im.read_pixel(int(self.car.x),int(self.car.y))) reward = -1 else: # otherwise self.car.velocity = Vector(2, 0).rotate(self.car.angle) reward = -0.2 #print(0, goal_x, goal_y, distance, int(self.car.x),int(self.car.y), im.read_pixel(int(self.car.x),int(self.car.y))) if distance < last_distance: reward = 0.1 # else: # last_reward = last_reward +(-0.2) if self.car.x < 5: self.car.x = 5 reward = -1 if self.car.x > self.width - 5: self.car.x = self.width - 5 reward = -1 if self.car.y < 5: self.car.y = 5 reward = -1 if self.car.y > self.height - 5: self.car.y = self.height - 5 reward = -1 if distance < 25: if swap == 1: goal_x = 1420 goal_y = 622 swap = 0 else: goal_x = 9 goal_y = 85 swap = 1 last_distance = distance # cnn state calculation _,_,new_obs = get_target_image(main_img, self.car.angle, [self.car.x, self.car.y], image_size) xx = goal_x - self.car.x yy = goal_y - self.car.y new_orientation = Vector(*self.car.velocity).angle((xx,yy))/180. new_orientation = [new_orientation, -new_orientation] save_cropped_image(new_obs, self.car.x, self.car.y, name = "") # cnn state calculation # state calculation # xx = goal_x - self.car.x # yy = goal_y - self.car.y # orientation = Vector(*self.car.velocity).angle((xx,yy))/180. # new_obs = [self.car.signal1, self.car.signal2, self.car.signal3, orientation, -orientation] # state calculation reward_window.append(reward) if sum(reward_window[len(reward_window)-20:]) <= -19 or episode_timesteps % 2500 == 0 and episode_timesteps != 0: done = True reward_window = [] # ENV STEP PERFORM END # new_obs, reward, done, _ = env.step(action) # We check if the episode is done #done_bool = 0 if episode_timesteps + 1 == env._max_episode_steps else float(done) # We increase the total reward episode_reward += reward # We store the new transition into the Experience Replay memory (ReplayBuffer) replay_buffer.add((obs, orientation, new_obs, new_orientation, action, reward, done)) # We update the state, the episode timestep, the total timesteps, and the timesteps since the evaluation of the policy obs = new_obs orientation = new_orientation episode_timesteps += 1 total_timesteps += 1 timesteps_since_eval += 1
def move(self): self._pos = Vector(*self.velocity) + self._pos if self.widget: # self.widget.pos_hint = {'center_x': self.pos[0], 'center_y' : self.pos[1]} self.widget.pos = self._pos
def take_step(self,action,last_distance,episode_step): global car_prev_x global car_prev_y rotation = action self.car.move(rotation) if int(self.car.x) > 15 and int(self.car.x) < 1429 and int(self.car.y) > 15 and int(self.car.y) < 661: new_state = sand[int(self.car.x)-14:int(self.car.x)+14, int(self.car.y)-14 : int(self.car.y)+14] else: value = random_positions[randint(2,15)] car_prev_x = value[0] car_prev_y = value[1] new_state = sand[int(car_prev_x)-14:int(car_prev_x)+14,int(car_prev_y)-14:int(car_prev_y)+14] #self.car.x = int(car_prev_x) #self.car.y = int(car_prev_x) print("stuck near border{}".format(new_state)) distance = np.sqrt((self.car.x - goal_x)**2 + (self.car.y - goal_y)**2) print("Car position in take_step {} and {}".format(self.car.x,self.car.y)) if sand[int(self.car.x),int(self.car.y)] > 0: print('Car in sand') else: print("Car on Road") car_prev_x = int(self.car.x) car_prev_y = int(self.car.y) print('x value {}'.format(car_prev_x)) print('y value {}'.format(car_prev_y)) episode_step += 1 print('episode step',episode_step) if int(sand[int(self.car.x),int(self.car.y)]) > 0: reward = -2.0 self.car.velocity = Vector(2,0).rotate(self.car.angle) done = True #nk else : reward = -0.2 self.car.velocity = Vector(5,0).rotate(self.car.angle) done = False if episode_step == 100: #nk #nk16th again # episode_step = 0 print('greater than 500') done = True if int(last_distance) < int(distance): reward += -1.0 if int(last_distance) > int(distance): reward += 0.5 if self.car.x < 10: self.car.x = 580 self.car.y = 310 reward += -3 done = True if self.car.x > self.width - 10: self.car.x = 580 self.car.y = 310 reward += -3 done = True if self.car.y < 10: self.car.x = 580 self.car.y = 310 reward += -3 done = True if self.car.y > self.height - 10: self.car.x = 580 self.car.y = 310 reward += -3 done = True last_distance = distance return new_state,reward,done,episode_step
def _get_rotation(self): v1 = Vector(0, 10) tp = self.to_parent v2 = Vector(*tp(*self.pos)) - tp(self.x, self.y + 10) return -1.0 * (v1.angle(v2) + 180) % 360