def init_rendering(self): self.FloorColors = gen_colors(self.NFloors, (0.1, 0.1, 0.9), (0.1, 0.9, 0.1), (1.0, 1.0, 0.1)) self.Viewer = Viewer(600, 600) self.Frame = self.Viewer.frame(0., 20., 0., self.NFloors) elevator_well = Frame() self.Frame.add(elevator_well, at=(3.0, 0.0)) self.ElevatorFrame = Frame() self.ElevatorFrame.add(Rectangle(0.0, self.Capacity, 0.0, 1.0, filled=False).color(0.8, 0.8, 0.8), at=(0, 0)) elevator_well.add(self.ElevatorFrame, at=(0.0, 0)) self.FloorFrames = [] self.QueueFrames = [] for f in range(self.NFloors): frame = Frame() self.Frame.add(frame, at=(0.0, f)) self.FloorFrames.append(frame) frame.add(Rectangle(0.0, 2.0, 0.0, 1.0).color(*self.FloorColors[f]), at=(0, 0)) qf = Frame() frame.add(qf, at=(3.0 + self.Capacity, 0.0)) self.QueueFrames.append(qf)
class Tank(object): def __init__(self, parent): self.Frame = Frame(hidden=True) self.Body = Polygon([(-0.01, -0.01), (0.01, -0.005), (0.01, 0.005), (-0.01, 0.01)], filled=True) self.Frame.add(self.Body) self.FireLine = Line( (0.005, 0.0), (_TanksEnv.FireRange, 0.0)).color(1.0, 0.8, 0.1) self.FireLine.hidden = True self.Frame.add(self.FireLine) def show(self, pos, angle, fire, hit): self.Frame.hidden = False self.Frame.move_to(*pos) self.Frame.rotate_to(angle) self.FireLine.hidden = fire == 0 #print (self.FireLine.hidden) if hit: self.Body.color(1, 0, 0) else: self.Body.color(0, 1, 1)
from draw2d import Viewer, Rectangle, Frame, Circle, Line import math, time viewer = Viewer(800, 800, clear_color=(1, 1, 1, 1)) frame = viewer.frame(-1.1, 1.1, -1.1, 1.1) for i in range(12): a = math.pi / 2 - i * 2 * math.pi / 12.0 if i % 3 == 0: m = Rectangle(-0.015, 0.015, -0.1, 0.05).color(0, 0, 0) else: m = Rectangle(-0.01, 0.01, -0.05, 0.05).color(0, 0, 0) f = Frame() f.add(m, at=(0.0, 1.0)) f.rotate_by(a) frame.add(f) hour_hand = Rectangle(-0.02, 0.02, -0.02, 0.5).color(0, 0, 0) frame.add(hour_hand) minute_hand = Rectangle(-0.01, 0.01, -0.02, 0.75).color(0, 0, 0) frame.add(minute_hand) second_hand = Rectangle(-0.003, 0.003, -0.1, 0.95).color(0, 0, 0) frame.add(second_hand) while True: t = time.localtime() s = t.tm_sec m = t.tm_min h = t.tm_hour % 12
F.add( Text("North", anchor_x="center", anchor_y="top", color=(0.2, 0.2, 1.0)).move_to(0.5, 0.9)) F.add( Text("South", anchor_x="center", anchor_y="bottom", color=(1.0, 1.0, 0.1)).move_to(0.5, 0.1)) F.add( Text("East", anchor_x="right", anchor_y="center", color=(0.2, 1.0, 1.0)).move_to(0.9, 0.5)) F.add( Text("West", anchor_x="left", anchor_y="center", color=(1.0, 0.2, 0.1)).move_to(0.1, 0.5)) fly = Frame() fly.add(Circle(radius=0.01).color(1, 1, 1)) label = Text("").move_to(0.01, 0.01) vlabel = Text("", rotation=0.0, anchor_x="left", anchor_y="center").move_to(0.02, 0.0) fly.add(label) fly.add(vlabel) F.add(fly, "fly") x, y = random.random(), random.random() vx, vy = 0.0, 0.0 vmax = 0.5 r = random.random() omega = 0.0 max_omega = 0.1 tau = 0.1
from draw2d import Viewer, Rectangle, Frame, Circle, Line, Text import math, time viewer = Viewer(800, 800, clear_color=(1, 1, 1, 1)) frame = viewer.frame(-1.1, 1.1, -1.1, 1.1) for i in range(12): a = math.pi / 2 - i * 2 * math.pi / 12.0 if i % 3 == 0: m = Rectangle(-0.015, 0.015, -0.1, 0.05).color(0, 0, 0) else: m = Rectangle(-0.01, 0.01, -0.05, 0.05).color(0, 0, 0) f = Frame() f.add(m, at=(0.0, 1.0)) f.rotate_by(a) frame.add(f) second_hand = Frame() text = Text("00:00:00", 0.5, 0.0, anchor_x="center", anchor_y="center").color(1, 1, 1) second_hand.add(text) frame.add(second_hand) while True: t = time.localtime() s = t.tm_sec m = t.tm_min h = t.tm_hour % 12
class _Elevator(object): NFloors = 8 Capacity = 10 ArrivalRate = 0.2 def __init__(self): self.ObservationDim = self.NFloors * 4 high = np.array([np.inf] * self.ObservationDim) self.observation_space = spaces.Box(-high, high) self.action_space = spaces.Discrete(3) self.Viewer = None def init_rendering(self): self.FloorColors = gen_colors(self.NFloors, (0.1, 0.1, 0.9), (0.1, 0.9, 0.1), (1.0, 1.0, 0.1)) self.Viewer = Viewer(600, 600) self.Frame = self.Viewer.frame(0., 20., 0., self.NFloors) elevator_well = Frame() self.Frame.add(elevator_well, at=(3.0, 0.0)) self.ElevatorFrame = Frame() self.ElevatorFrame.add(Rectangle(0.0, self.Capacity, 0.0, 1.0, filled=False).color(0.8, 0.8, 0.8), at=(0, 0)) elevator_well.add(self.ElevatorFrame, at=(0.0, 0)) self.FloorFrames = [] self.QueueFrames = [] for f in range(self.NFloors): frame = Frame() self.Frame.add(frame, at=(0.0, f)) self.FloorFrames.append(frame) frame.add(Rectangle(0.0, 2.0, 0.0, 1.0).color(*self.FloorColors[f]), at=(0, 0)) qf = Frame() frame.add(qf, at=(3.0 + self.Capacity, 0.0)) self.QueueFrames.append(qf) def reset(self): self.Floor = 0 self.Load = np.zeros((self.NFloors, )) self.Queues = [[] for _ in range(self.NFloors)] return self.observation() def exchange(self): # # unload # unloaded = self.Load[self.Floor] self.Load[self.Floor] = 0 q = self.Queues[self.Floor] n = max(0, self.Capacity - int(sum(self.Load))) n = min(n, len(q)) #print (type(n), n) for i in q[:n]: self.Load[i] += 1 self.Queues[self.Floor] = q[n:] return unloaded * 10.0 def observation(self): # # one-hot floor number # load # up buttons # down buttons # up_buttons = np.zeros((self.NFloors, )) down_buttons = np.zeros((self.NFloors, )) for f, q in enumerate(self.Queues): up = down = 0 for d in q: if d > f: up = 1 elif d < f: down = 1 up_buttons[f] = up down_buttons[f] = down floor = np.zeros((self.NFloors, ), dtype=np.float) floor[self.Floor] = 1 v = np.concatenate([floor, self.Load, up_buttons, down_buttons]) return np.array(v, dtype=np.float) def step(self, action): reward = -0.1 * (sum(len(q) for q in self.Queues) + sum(self.Load)) if action == 0: reward += self.exchange() elif action == 1: if self.Floor > 0: self.Floor -= 1 else: reward -= 10.0 reward -= 0.01 elif action == 2: if self.Floor < self.NFloors - 1: self.Floor += 1 else: reward -= 10.0 reward -= 0.01 if self.ArrivalRate > random.random(): i = random.randint(0, self.NFloors - 1) if len(self.Queues[i]) < self.Capacity: j = random.randint(0, self.NFloors - 1) while j == i: j = random.randint(0, self.NFloors - 1) self.Queues[i].append(j) return self.observation(), reward, False, {} PersonUp = [(0.0, 0.1), (0.9, 0.1), (0.45, 0.4)] PersonDown = [(0.0, 0.4), (0.9, 0.4), (0.45, 0.1)] PersonSide = [(0.0, 0.1), (0.0, 0.4), (0.9, 0.25)] def render(self): if self.Viewer is None: self.init_rendering() self.ElevatorFrame.remove_all() self.ElevatorFrame.move_to(0.0, self.Floor) self.ElevatorFrame.add(Rectangle(0.0, self.Capacity, 0.0, 1.0, filled=False).color(0.5, 0.5, 0.5), at=(0, 0)) x = 0 for f, n in enumerate(self.Load): n = int(n) for _ in range(n): p = self.PersonUp if f > self.Floor else ( self.PersonDown if f < self.Floor else self.PersonSide) p = Polygon(p).color(*self.FloorColors[f]) self.ElevatorFrame.add(p, at=(x, 0)) x += 1 for f, q in enumerate(self.Queues): qf = self.QueueFrames[f] qf.remove_all() x = 0 for df in q: p = self.PersonUp if df > f else ( self.PersonDown if df < f else self.PersonSide) p = Polygon(p).color(*self.FloorColors[df]) qf.add(p, at=(x, 0)) x += 1 self.Viewer.render() time.sleep(0.1)