Exemplo n.º 1
0
class FrontEnd(object):
    def __init__(self):
        self.sim = Sim()

        pygame.init()
        pygame.font.init()
        self.screen = pygame.display.set_mode((G.screenWidth, G.screenHeight))
        pygame.display.set_caption("AntBridgeSim")
        self.clock = pygame.time.Clock()
        self.setupButtons()
        self.drawGrid()
        for button in self.buttons:
            button.draw(self.screen)

        oldpos = self.sim.ant.pos
        oldId = self.sim.antId

        dead = None
        while G.running:
            time.sleep(G.sleep)
            self.eventHandler()
            self.drawBlock(self.sim.ant.pos)
            self.drawJoint(self.sim.ant.pos)
            self.drawBlock(oldpos)
            self.drawJoint(oldpos)
            oldpos = self.sim.ant.pos
            if self.sim.antId != oldId:
                oldId = self.sim.antId
                self.drawGrid()
            self.drawJoints()  #TODO incrementally draw
            pygame.display.flip()

            if not dead:
                try:
                    if not self.sim.step():
                        break
                except Error as e:
                    print e
                    dead = True
                except Success as e:
                    print e
                    dead = True
            else:
                paused = True
                while paused:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            paused = False
                            G.running = False

    def eventHandler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                G.running = False
            for button in self.buttons:
                button.checkPressed(event)

    def drawGrid(self):
        for x in range(G.numBlocksX):
            for y in range(G.numBlocksY):
                self.drawBlock((x, y))

        for x in range(G.numBlocksX + 1):
            pygame.draw.line(
                self.screen, G.lineColor, (x * G.blockWidth, 0),
                (x * G.blockWidth, G.screenHeight - G.buttonPanelHeight),
                G.lineWidth)

        for y in range(G.numBlocksY + 1):
            pygame.draw.line(self.screen, G.lineColor, (0, y * G.blockHeight),
                             (G.screenWidth, y * G.blockHeight), G.lineWidth)

    def setupButtons(self):
        self.buttons = []

        buttonTexts = []
        actions = []

        # Button for speeding up animation
        buttonTexts.append("Speed up!")

        def action(button):
            G.sleep = G.sleep / 2.0
            print "Speed up! New speed %f" % (G.sleep)

        actions.append(action)

        # Button for slowing down animation
        buttonTexts.append("Slow down.")

        def action(button):
            G.sleep = G.sleep * 2.0
            print "Slow down. New speed %f" % (G.sleep)

        actions.append(action)

        # Button for pausing animation
        buttonTexts.append("Pause")

        def action(button):
            button.text = "Resume"
            button.draw(self.screen)
            pygame.display.flip()
            paused = True
            while paused:
                for event in pygame.event.get():
                    if event.type == MOUSEBUTTONDOWN:
                        paused = False
                    elif event.type == pygame.QUIT:
                        paused = False
                        G.running = False
            button.text = "Pause"
            button.draw(self.screen)
            pygame.display.flip()

        actions.append(action)

        # This sets up all the buttons based on the above definitions
        # To add a new button, just append the appropriate text and action above.
        # When adding buttons, you don't need to change the code below.
        numButtons = float(len(actions))
        for i, (text, action) in enumerate(zip(buttonTexts, actions)):
            button = Button(text, i * G.screenWidth / numButtons,
                            G.screenHeight - G.buttonPanelHeight,
                            G.screenWidth / numButtons, G.buttonPanelHeight,
                            action)
            self.buttons.append(button)

    def drawBlock(self, (x, y)):
        if self.sim.ant.pos == (x, y):
            color = G.searchColor
        elif G.state[(x, y)] == G.SHAKING:
            color = G.shakeColor
        elif G.state[(x, y)] == G.NORMAL:
            color = G.fillColor
        elif G.state[(x, y)] == G.DEAD:
            color = G.deadColor
        else:
            color = G.emptyColor
        rect = pygame.draw.rect(
            self.screen, color,
            (x * G.blockWidth + G.lineWidth, y * G.blockHeight + G.lineWidth,
             G.blockWidth - G.lineWidth, G.blockHeight - G.lineWidth), 0)
Exemplo n.º 2
0
    def transactions(self):
        return self._txs


if __name__ == '__main__':
    fname = './data/eth_usdt_med.csv'
    sim = Sim(fname)
    sim_iters = 50000
    buy_len = 3
    sell_len = 2
    limit = 0.01  # amount to buy
    trader = SimpleTrader(buy_len, sell_len, limit)
    done = False

    for i in range(sim_iters):
        exchange_data, done = sim.step()

        if done:
            break

        trader.act(exchange_data)

    print("Done simulating {} steps".format(i))
    print("SimpleTrader params: buy len {}, sell len {}, buy limit {}".format(
        buy_len, sell_len, limit))
    print("PnL: {:.3f}".format(trader.pnl()))
    print("First 3 transactions:")
    for t in trader.transactions()[:3]:
        print(t)
Exemplo n.º 3
0
class BatchRun(object):
    def __init__(self, numRuns, output):
        # desired statistics
        antsPerRun = 0
        successfulRuns = 0
        failedRuns = 0
        maxHeightAchieved = 0
        heightPerRun = 0

        success = True

        if output is not None:
            outfile = open(output, "w")
            G.outfile = outfile

        for i in range(numRuns):
            if G.verbose:
                print >> G.outfile, "RUNNING BATCH " + str(i + 1)
            print "RUNNING BATCH " + str(i + 1)
            try:
                self.sim = Sim()
                while G.running:
                    if not self.sim.step():
                        break
                G.running = True

            except Error as e:
                if G.verbose:
                    print e
                success = None
            except Success as s:
                if G.verbose:
                    print s

            # accumulate statistics
            heightPerRun += self.sim.maxHeight + 1
            if self.sim.maxHeight > maxHeightAchieved:
                maxHeightAchieved = self.sim.maxHeight
            if success:
                antsPerRun += self.sim.numAnts
                successfulRuns += 1
            else:
                failedRuns += 1
                success = True

        #sanity check
        if numRuns != successfulRuns + failedRuns:
            raise WeirdError("Runs weren't counted right... weird!")

        # summarize statistics
        statsString = "Ran a batch of " + str(numRuns) \
         + " simulations. "
        if successfulRuns != 0:
            statsString += "\n Average Ants To Complete a Bridge: " \
            + str(float(antsPerRun) / float(successfulRuns))
        statsString += "\n Percentage of Successful Runs: " \
         + str(float(successfulRuns) * 100.0 / float(numRuns)) \
         + "%" \
         + "\n Average Height of Bridges Built: " \
         + str(float(heightPerRun) / float(numRuns)) \
         + "\n Maximum Height of Bridges Built: " \
         + str(maxHeightAchieved + 1)
        print >> G.outfile, statsString
Exemplo n.º 4
0
goal_pxl = np.array([CAM_WIDTH / 2., CAM_HEIGHT / 2.])

# Holds features with desired selection criteria
pnts = None
prev_pnts = None
feats = None
draw = []

# Perform VS for t seconds
t = 50
vel = np.array([0., 0., 0., 0., 0., 0.])
prev_time = start
while (time.time() - start) < t:
    # Get image from sim and find ORB keypoints
    print("Starting new frame")
    rgb, depth = sim.step()
    featimg, kps, des = finder.get_orb(rgb)

    # Convert target and goal pixels to cartesian space
    target_pnt = ibvs.feature_to_pnt(target_pxl, depth)
    goal_pnt = ibvs.feature_to_pnt(goal_pxl, depth)

    if constellation.is_empty():
        #                         #
        #   Feature selection     #
        #                         #
        # Find translational distance from goal
        diff = goal_pnt - target_pnt

        # TODO: Select features by score and distance
        feats = []
        ep_return = 0
        obs = s.reset()  # Do this BEFORE adding a manual or random car

        if args.control_car_type == "human":
            s.add_manual_car(fig)
        elif args.control_car_type == "random":
            s.add_random_car()

        obs = s.get_obs()

        if not args.nogui:
            s.render(ax=ax)
            plt.pause(0.01)

        while not done:
            obs, reward, done, info = s.step(get_car_actions(obs))
            ep_return += reward
            if not args.nogui:
                s.render(ax=ax)
                plt.pause(0.01)
        if i % 10 == 0:
            print(i)

        collisions.append(s.check_collisions())
        goals.append(s.goals_reached)
        returns.append(ep_return)

    returns = np.array(returns).sum(axis=1) / NUM_RL_CARS
    print(
        f"avg returns ${np.sum(returns)/len(returns):.2f}\\pm{scipy.stats.sem(returns):.2f}$"
    )
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description='Run autonomous vehicle swarm simulation.')
    parser.add_argument('num_cars', type=int)
    parser.add_argument('map_path')
    parser.add_argument('--path-reversal-prob', type=float, required=False)
    parser.add_argument('--angle-min', type=float, required=False)
    parser.add_argument('--angle-max', type=float, required=False)
    parser.add_argument('--timestep', type=float, required=False, default=0.1)
    parser.add_argument('--angle-mode', choices=['auto', 'auto_noise', 'random'], default='auto', required=False)
    parser.add_argument('--angle-noise', type=float, default=0.0, required=False)
    parser.add_argument('--save-video', action='store_true', default=False, required=False)
    parser.add_argument('--nogui', action='store_true', default=False, required=False)
    parser.add_argument('--collision-penalty', choices=['none', 'low'], default='none', required=False)
    parser.add_argument('--num_episodes', type=int, default=1, required=False)

    args = parser.parse_args()

    POLICY_FILENAME = POLICY_FILES[args.map_path]

    if args.save_video:
        assert not args.nogui

    # WARNING: This must match the class of the saved policy. See the main() method in train.py
    def policy_fn(name, ob_space, ac_space):
        return mlp_mean_embedding_policy.MlpPolicy(name=name, ob_space=ob_space, ac_space=ac_space,
                                                    hid_size=[64], feat_size=[64])

    # Load the policy
    po = ActWrapper.load(POLICY_FILENAME, policy_fn)

    # WARNING: This must match the environment for the saved policy. See the main() method of train.py
    env = Sim(
        args.num_cars, args.map_path, args.path_reversal_prob or 0,
        args.angle_min or 0, args.angle_max or 2*np.pi,
        angle_mode=args.angle_mode, angle_noise=args.angle_noise,
        timestep=args.timestep, save_video=args.save_video, 
        collision_penalty=args.collision_penalty
    )

    if not args.nogui:
        # Create window for rendering
        plt.ion()
        fig, ax = plt.subplots(figsize=(8,8))
        fig.canvas.set_window_title('AV Swarm Simulator')
        plt.show()
        env.render(ax=ax)
        plt.pause(0.01)

    stochastic = True # idk why not
    returns = []
    collisions = []
    goals = []
    for i in range(args.num_episodes):
        obs = env.reset()
        done = False
        ep_return = 0
        while not done:
            ac, vpred = po._act.act(stochastic, obs)
            obs, reward, done, info = env.step(ac)
            ep_return += reward
            if not args.nogui:
                env.render(ax=ax)
                plt.pause(0.01)

        collisions.append(env.check_collisions())
        goals.append(env.goals_reached)
        returns.append(ep_return)
        if i % 10 == 0:
            print(i)

    returns = np.array(returns).sum(axis=1) / args.num_cars
    print(f"avg returns ${np.sum(returns)/len(returns):.2f}\\pm{scipy.stats.sem(returns):.2f}$")
    print(f"avg goals ${np.sum(goals)/len(goals):.2f}\\pm{scipy.stats.sem(goals):.2f}$")
    print(f"avg collisions ${np.sum(collisions)/len(collisions):.2f}\\pm{scipy.stats.sem(collisions):.2f}$")

    env.close()
	)
	
	if not args.nogui:
		s.render(ax=ax)
		plt.pause(0.01)

	# ACTIONS = [(200, 0)] #, (0, np.pi/6), (50, np.pi/12)]
	# Takes 20 steps to turn around
	# ACTIONS = [(100, 0.1)]
	ACTIONS = [(100, 0)] # TEMP: For LIDAR debugging
	rng = np.random.default_rng(42)
	car_actions = [rng.choice(ACTIONS) for _ in range(args.num_cars)]

	import time
	starttime = time.time()
	for i in range(80):
		obs, reward, done, info = s.step(car_actions)
		print('reward', reward)
		if not args.nogui:
			s.render(ax=ax)
			plt.pause(0.01)
	print('total time', time.time()-starttime)

	if not args.nogui:
		plt.pause(10)
	
	s.close()

	# FIXME: Some problems right off the bat:
	# - I can't keep the plt window open without pausing it for a long period of time.