def draw_lunar_lander_dqn(settings, save_path, max_steps=2000000): project_root = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) log_name = "log" parser_func = parse_steps_log path_formatters = [] config_files, runs, num_datapoints, labels = [], [], [], [] for cfg, param_setting, num_runs, num_steps, label in settings: config_files.append((cfg, param_setting)) runs.append(num_runs) num_datapoints.append(num_steps) labels.append(label) for cf, best_setting in config_files: swp = Sweeper(os.path.join(project_root, cf)) cfg = swp.parse( best_setting) # Creating a cfg with an arbitrary run id cfg.data_root = os.path.join(project_root, 'data', 'output') logdir_format = cfg.get_logdir_format() path_format = os.path.join(logdir_format, log_name) path_formatters.append(path_format) v = RunLines(path_formatters, runs, num_datapoints, labels, parser_func=parser_func, save_path=save_path, xlabel="Number of steps", ylabel="Reward", interval=10000) v.draw()
def draw_reject_ratio(settings, save_path): project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) log_name = "log" parser_func = parse_reject_ratio path_formatters = [] config_files, runs, num_datapoints, labels = [], [], [], [] for cfg, param_setting, num_runs, num_steps, label in settings: config_files.append((cfg, param_setting)) runs.append(num_runs) num_datapoints.append(num_steps) labels.append(label) for cf, best_setting in config_files: swp = Sweeper(os.path.join(project_root, '/'.join(cf.split("/")[:-1]))) cfg = swp.parse(best_setting) # Creating a cfg with an arbitrary run id cfg.data_root = os.path.join(project_root, 'data', 'output') logdir_format = cfg.get_logdir_format() path_format = os.path.join(logdir_format, log_name ) path_formatters.append(path_format + '/' + cf.split("/")[-1]) v = RunLines(path_formatters, runs, num_datapoints, labels, parser_func=parser_func, save_path=save_path, xlabel="Number of steps in 10000s", ylabel="Reject Ratio", interval=10000, ylim=(0, 1.2)) v.draw()
def test_file_dups_safe_mode(self): swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_dups')], safe_mode=True) dups = swp.file_dups() for h, flist in dups.items(): if len(flist) > 1: dups_exist = True self.assertTrue(dups_exist)
def init_start(self): global step_size, nrow, ncol global sand, lineList nrow = 10 ncol = 10 if self.width < self.height: step_size = self.width else: step_size = self.height step_size = int(step_size / 1.2) step_size = int(step_size / ncol) init() no_obs = 20 matrix, start_position = random_matrix(nrow, ncol, no_obs) for ln in lineList: self.canvas.remove(ln) lineList = [] #print(start_position) with self.canvas: for i in range(nrow): for j in range(ncol): wdt = 1 if matrix[i][j] != 0: Color(1.0, 0.0, 0.1) wdt = 3 else: Color(0.0, 1.0, 0.1, 0.7) scrX, scrY = self.car.grid_to_screen(i, j, nrow, ncol) lineList.append( Line(rectangle=(scrX - step_size / 2, scrY - step_size / 2, step_size, step_size), width=wdt)) sand = matrix.copy() matrix = [[]] srX, srY = self.car.grid_to_screen(start_position['x'], start_position['y'], nrow, ncol) self.move_car(Vector(srX, srY)) self.car.move_init() print(sand) #start_position={'x': self.car.x, 'y': self.car.y} start_direction = 0 # run with dfs self.robot = MyRobot(matrix, start_position, start_direction, self.car) # robot.log() self.sweeper = Sweeper(self.robot) self.sweeper.loggable = False self.robot.loggable = False
def main(): maps, observed_map = generate_map_random(100, 100, 1000) start_position = Position(0, 0) robot = Robot(maps, start_position, 0) sweeper = Sweeper(robot, observed_map) #sweeper = SweeperDFS(robot, maps) sweeper.work() print('Robot result. Step number:', robot.step_number, 'Turn number:', robot.turn_number)
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 last_tm global time_elapsed global time_max global rects if first_update: longueur = int(self.width) largeur = int(self.height) longueur = int(longueur / size) largeur = int(largeur / size) init() no_obs = int(longueur * largeur / 5) matrix, start_position = random_matrix(longueur, largeur, no_obs) sand = matrix print(sand) start_position = {'x': self.car.x, 'y': self.car.y} start_direction = 0 #random.randint(0, 3) # run with dfs self.robot = MyRobot(sand, start_position, start_direction) # robot.log() self.sweeper = Sweeper(self.robot) self.sweeper.loggable = False self.robot.loggable = False # print("rotation") rotation = self.sweeper.get_move() if rotation == -1 or rotation == None: #self.car.move(0) pass #pass #self.car.velocity= Vector(1, 0).rotate(self.car.angle)*0 # Stop the car else: self.car.move(rotation) print(rotation) #print(rotation) self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3
def clear_canvas(self): global longueur global largeur self.canvas.clear() self.serve_car() sand = np.zeros((longueur, largeur)) start_position = {'x': self.car.x, 'y': self.car.y} start_direction = 0 #random.randint(0, 3) # run with dfs self.robot = MyRobot(sand, start_position, start_direction) # robot.log() self.sweeper = Sweeper(self.robot) self.sweeper.loggable = False self.robot.loggable = False
def _eval_lines(config_file, start_idx, end_idx, max_steps, interval=10000): print('config_file: {}'.format(config_file)) project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) sweeper = Sweeper(os.path.join(project_root, config_file)) eval = [] eval_lines = [] for k in range(sweeper.total_combinations): eval.append([]) eval_lines.append([]) for idx in range(start_idx, end_idx): cfg = sweeper.parse(idx) cfg.data_root = os.path.join(project_root, 'data', 'output') log_dir = cfg.get_logdir() log_path = os.path.join(log_dir, 'log') try: with open(log_path, "r") as f: lines = f.readlines() except FileNotFoundError: continue if len(lines) == 0: continue # ugly parse based on the log_file format try: num_steps = get_max_steps(lines) if num_steps >= max_steps: assert idx % sweeper.total_combinations == cfg.param_setting avg_eval_steps = extract_line(lines, max_steps, interval=interval) eval[idx % sweeper.total_combinations].append(np.mean(avg_eval_steps[-int(len(avg_eval_steps)/2):])) except IndexError: print(idx) raise summary = list(map(lambda x: (x[0], np.mean(x[1]), np.std(x[1]), len(x[1])), enumerate(eval))) summary = [x for x in summary if np.isnan(x[1]) == False] # new_summary = [] # for s in summary: # if np.isnan(s[1]) == False: # new_summary.append(s) # print(summary[0]) # print(new_summary[0]) # quit() summary = sorted(summary, key=lambda s: s[1], reverse=True) for idx, mean, std, num_runs in summary: print("Param Setting # {:>3d} | Rewards: {:>10.10f} +/- {:>5.2f} ({:>2d} runs) {} | ".format( idx, mean, std, num_runs, sweeper.param_setting_from_id(idx)))
def test_iter_file_dups_nodups(self): swp = Sweeper([os.path.join(mydir, 'testfiles_nodups')]) dups_exist = False for x in swp: dups_exist = True break self.assertFalse(dups_exist)
def draw_lunar_lander_traces_h512(save_path, max_steps=2000000): project_root = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) log_name = "log" parser_func = parse_steps_log path_formatters = [] settings = [ ("experiment/config_files/lunar_lander/sarsa_lmbda/sweep_h512.json", 1, 5, max_steps, "Sarsa(0)"), ("experiment/config_files/lunar_lander/sarsa_lmbda/sweep_h512.json", 4, 5, max_steps, "Sarsa(0.5)"), ("experiment/config_files/lunar_lander/sarsa_lmbda/sweep_h512.json", 7, 5, max_steps, "Sarsa(0.7)"), ("experiment/config_files/lunar_lander/sarsa_lmbda/sweep_h512.json", 11, 5, max_steps, "Sarsa(0.85)"), ("experiment/config_files/lunar_lander/sarsa_lmbda/sweep_h512.json", 14, 5, max_steps, "Sarsa(0.9)") ] config_files, runs, num_datapoints, labels = [], [], [], [] for cfg, param_setting, num_runs, num_steps, label in settings: config_files.append((cfg, param_setting)) runs.append(num_runs) num_datapoints.append(num_steps) labels.append(label) for cf, best_setting in config_files: swp = Sweeper(os.path.join(project_root, cf)) cfg = swp.parse( best_setting) # Creating a cfg with an arbitrary run id cfg.data_root = os.path.join(project_root, 'data', 'output') logdir_format = cfg.get_logdir_format() path_format = os.path.join(logdir_format, log_name) path_formatters.append(path_format) v = RunLines(path_formatters, runs, num_datapoints, labels, parser_func=parser_func, save_path=save_path, xlabel="Number of steps", ylabel="Episodes Completed", interval=10000) v.draw()
def test_iter_file_dups_dups(self): swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_dups')]) dups_exist = False for x in swp: dups_exist = True filepath, h, dups = x self.assertNotIn(filepath, dups) self.assertTrue(len(dups) > 0) self.assertTrue(dups_exist)
def run(): start = time.time() parser = argparse.ArgumentParser(description="run_file") parser.add_argument('--idx', default=0, type=int, help='identifies run number and configuration') parser.add_argument('--config-file', default='config_files/actor_critic.json') args = parser.parse_args() project_root = os.path.abspath(os.path.join(os.path.dirname(__file__))) sweeper = Sweeper(os.path.join(project_root, args.config_file)) cfg = sweeper.parse(args.idx) cfg.env_instance = MountainCar(cfg) agent_class = getattr(agents.agent, cfg.agent_class) agent = agent_class(cfg) log_dir = cfg.get_logdir() ensure_dirs([log_dir]) steps_log = os.path.join(log_dir, 'steps_log') steps_logger = setup_logger(steps_log, stdout=True) cfg.log_config(steps_logger) ep_log = os.path.join(log_dir, 'ep_log') ep_logger = setup_logger(ep_log, stdout=False) cfg.log_config(ep_logger) exp = Experiment(agent, cfg.env_instance, max_steps=cfg.max_steps, seed=args.idx, steps_log=steps_log, ep_log=ep_log) exp.run_step_mode() print("Memory used: {:5} MB".format(memory_usage_psutil())) print("Time elapsed: {:5.2} minutes".format((time.time() - start) / 60))
def draw_mountain_car(save_path): project_root = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) log_name = "log" parser_func = parse_steps_log path_formatters = [] settings = [ ("experiment/config_files/mountain_car/sarsa/sweep.json", 12, 3, 300000, "Sarsa(0)"), ] config_files, runs, num_datapoints, labels = [], [], [], [] for cfg, param_setting, num_runs, num_steps, label in settings: config_files.append((cfg, param_setting)) runs.append(num_runs) num_datapoints.append(num_steps) labels.append(label) for cf, best_setting in config_files: swp = Sweeper(os.path.join(project_root, cf)) cfg = swp.parse( best_setting) # Creating a cfg with an arbitrary run id cfg.data_root = os.path.join(project_root, 'data', 'output') logdir_format = cfg.get_logdir_format() path_format = os.path.join(logdir_format, log_name) path_formatters.append(path_format) v = RunLines(path_formatters, runs, num_datapoints, labels, parser_func=parser_func, save_path=save_path, xlabel="Number of steps", ylabel="Reward", interval=10000) v.draw()
runner_args = RunnerCfg() args = parser.parse_args() hparams = ["sigma"] sweep_root_path = generate_sweep_path(Experiment) # this is specific to the jean-zay cluster if args.mp_method == "SLURM": sweep_root_path = os.path.join(os.path.expandvars("$SCRATCH"), sweep_root_path) processes = [] sweeper = Sweeper(grid_vars=hparams, gridsteps=args.gridsteps, warmup=False, grid_range=2) print("[SWEEPER]: Starting experiment at path:", sweep_root_path) for vars in sweeper.sweep_grid(): for var, value in vars: args.__setattr__(var, value) path: str = generate_run_path(sweep_root_path, args, hparams) print("Starting experiment on path", path) if args.mp_method == "MP": procs = run_single_from_sweep_mp(Experiment, args, path) processes += procs elif args.mp_method == "SLURM": for rank in range(args.nprocs): jobname = generate_tracking_tag(hparams) + str(rank)
eps_over_time[prev_num_steps:] = eps return eps_over_time if __name__ == '__main__': project_root = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) log_name = "ep_log" parser_func = parse_ep_log if log_name == "ep_log" else parse_steps_log path_formatters = [] config_files = [("config_files/sw_actor_critic.json", 13)] for cf, best_setting in config_files: swp = Sweeper(os.path.join(project_root, cf)) cfg = swp.parse( best_setting) # Creating a cfg with an arbitrary run id logdir_format = cfg.get_logdir_format() path_format = os.path.join(logdir_format, log_name) path_formatters.append(path_format) runs = [10] num_datapoints = [40000] labels = ['Actor-Critic'] v = RunLines(path_formatters, runs, num_datapoints, labels, parser_func=parser_func, save_path="mountain_car.png",
class Game(Widget): car = ObjectProperty(None) ball1 = ObjectProperty(None) ball2 = ObjectProperty(None) ball3 = ObjectProperty(None) def move_car(self, position): self.car.center = position self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3 def rotate_car(self, angle): self.car.move(angle) def clear_canvas(self): global step_size, nrow, ncol global sand self.serve_car() sand = np.zeros((nrow, ncol)) self.init_start() def serve_car(self): global step_size, nrow, ncol self.car.center = self.center self.car.velocity = Vector(1, 0) self.car.angle = 0 def init_start(self): global step_size, nrow, ncol global sand, lineList nrow = 10 ncol = 10 if self.width < self.height: step_size = self.width else: step_size = self.height step_size = int(step_size / 1.2) step_size = int(step_size / ncol) init() no_obs = 20 matrix, start_position = random_matrix(nrow, ncol, no_obs) for ln in lineList: self.canvas.remove(ln) lineList = [] #print(start_position) with self.canvas: for i in range(nrow): for j in range(ncol): wdt = 1 if matrix[i][j] != 0: Color(1.0, 0.0, 0.1) wdt = 3 else: Color(0.0, 1.0, 0.1, 0.7) scrX, scrY = self.car.grid_to_screen(i, j, nrow, ncol) lineList.append( Line(rectangle=(scrX - step_size / 2, scrY - step_size / 2, step_size, step_size), width=wdt)) sand = matrix.copy() matrix = [[]] srX, srY = self.car.grid_to_screen(start_position['x'], start_position['y'], nrow, ncol) self.move_car(Vector(srX, srY)) self.car.move_init() print(sand) #start_position={'x': self.car.x, 'y': self.car.y} start_direction = 0 # run with dfs self.robot = MyRobot(matrix, start_position, start_direction, self.car) # robot.log() self.sweeper = Sweeper(self.robot) self.sweeper.loggable = False self.robot.loggable = False def update(self, dt): global step_size, nrow, ncol global move_car_now global lineList if first_update: lineList = [] self.init_start() #if move_car_now == True: #self.sweeper.get_move() #move_car_now=False #print(step_size) self.sweeper.get_move() self.ball1.pos = self.car.sensor1 self.ball2.pos = self.car.sensor2 self.ball3.pos = self.car.sensor3
def main(): no_rows = 3 no_cols = 5 no_obs = 2 no_matrix = 1 total_elapsed_bfs = 0 total_steps_bfs = 0 total_turns_bfs = 0 total_elapsed_dfs = 0 total_steps_dfs = 0 total_turns_dfs = 0 for i in range(no_matrix): ''' Initialize Matrix, robot and start position ''' ############ HERE Add initialization of EV3 print("[INFO] Initialising Matrix...") debug_print("[INFO] Initialising Matrix...") #matrix, start_position = random_matrix(no_rows, no_cols, no_obs) #matrix, start_position = fixed_matrix(no_rows, no_cols, no_obs) matrix, start_position = custom_map(no_rows, no_cols, no_obs) start_direction = random.Random(4).randint(0, 3) print("[INFO] Initialising Robot and Sweeper...") debug_print("[INFO] Initialising Robot and Sweeper...") # initialise robot shovel = moveShovel.Shovel() music = playMusic.Music() music.playMusic() diffRobot = drive_gyro.Drive_gyro(shovel) print("[INFO] Initialising the respective Algorithm...") debug_print("[INFO] Initialising the respective Algorithm...") #algo = "dfs" algo = "bfs" robot = Robot(matrix, start_position, start_direction, diffRobot, shovel, algo) robot.log() #sweeper = DFSSweeper(robot) sweeper = Sweeper(robot) sweeper.loggable = False robot.loggable = True print("[INFO] Starting DFS sweep...") debug_print("[INFO] Starting DFS sweep...") start = time.time() sweeper.sweep() elapsed = time.time() - start print("[INFO] Finished DFS sweep...") debug_print("[INFO] Finished DFS sweep...") #print("[INFO] Move to home position...") #debug_print("[INFO] Move to home position...") #x_move = start_position['x']-sweeper.current_position['x'] #y_move = start_position['y'] - sweeper.current_position['y'] 'move robot to correct x position' #debug_print('move robot to x position') #if x_move > 0: # left_turns = 0 - sweeper.current_direction # current = 0 #elif x_move < 0: # left_turns = 2 - sweeper.current_direction # current = 2 #if left_turns < 0: # left_turns += 4 # debug_print('keine drehung notwendig') #if left_turns == 3: # debug_print('roboter dreht sich einmal nach rechts') #bewege roboter 1x nach rechts # diffRobot.turn_right() #else: # for i in range(left_turns): # debug_print('roboter nach links') #bewege roboter entsprechend oft nach links # diffRobot.turn_left() #debug_print('so viele schritte macht der roboter') #for steps in range(abs(x_move)): # debug_print(steps) #bewege den roboter jeweils eins nach vorne # diffRobot.driveGyro(10) #'move robot to correct y position' #debug_print('move robot zu y position') #if y_move > 0: # left_turns = 3 - current #elif y_move < 0: # left_turns = 1 - current #if left_turns < 0: # left_turns += 4 # debug_print('keine drehung notwendig') #if left_turns == 3: # debug_print('bewege einmal nach links') # bewege roboter 1x nach rechts # diffRobot.turn_right() #else: # for i in range(left_turns): # debug_print('bewege roboter nach links') # bewege roboter entsprechend oft nach links # diffRobot.turn_left() #print('so viele schritte macht der roboter') #for steps in range(abs(y_move)): # debug_print(steps) #bewege den roboter jeweils eins nach vorne # diffRobot.driveGyro(10) total_elapsed_dfs += elapsed total_steps_dfs += robot.move_count total_turns_dfs += robot.turn_count print('steps taken by dfs: %d, turns taken: %d, time taken: %.2fms' % (robot.move_count, robot.turn_count, elapsed * 1000)) # run with bfs #print("[INFO] Starting BFS sweep...") #debug_print("[INFO] Starting BFS sweep...") #robot = Robot(matrix, start_position, start_direction, diffRobot) #sweeper = Sweeper(robot) #sweeper.loggable = False #robot.loggable = True #start = time.time() #sweeper.sweep() #elapsed = time.time() - start #total_elapsed_bfs += elapsed #total_steps_bfs += robot.move_count #total_turns_bfs += robot.turn_count #print('steps taken by planned bfs: %d, turns taken: %d, time taken: %.2fms' # % (robot.move_count, robot.turn_count, elapsed * 1000)) # sweeper.print_map() # robot.log() print('DFS: average steps taken: %d, turns taken: %d, time taken: %.2fms' % (int(total_steps_dfs / no_matrix), int(total_turns_dfs / no_matrix), total_elapsed_dfs * 1000 / no_matrix)) print( 'Planned BFS: average steps taken: %d, turns taken: %d, time taken: %.2fms' % (int(total_steps_bfs / no_matrix), int(total_turns_bfs / no_matrix), total_elapsed_bfs * 1000 / no_matrix)) music.playMusic()
def init_sweeper(self): """init a sweeper""" sweeper = Sweeper()
def final_field(self): field = [[9 for col in range(self.length)] for row in range(self.width)] return field def init_sweeper(self): """init a sweeper""" sweeper = Sweeper() # begin to sweep mine user = User() sweeper = Sweeper( user.length, user.width) # !!!!!!!!!!don't input the mine number to the sweeper for i in range(user.length): for j in range(user.width): print("%2d" % (user.field.field[i][j]), end=' ') print() print() while True: # return the cell sweeper want to reveal (row, col) = sweeper.running() if -1 == row: print('Success') break
def main(): no_rows = 10 no_cols = 9 no_obs = 10 no_matrix = 1 total_elapsed_bfs = 0 total_steps_bfs = 0 total_turns_bfs = 0 total_elapsed_dfs = 0 total_steps_dfs = 0 total_turns_dfs = 0 import time for i in range(no_matrix): matrix, start_position = random_matrix(no_rows, no_cols, no_obs) start_direction = random.randint(0, 3) # run with dfs robot = Robot(matrix, start_position, start_direction) # robot.log() sweeper = DFSSweeper(robot) sweeper.loggable = False robot.loggable = True start = time.time() sweeper.sweep(callback_a) elapsed = time.time() - start total_elapsed_dfs += elapsed total_steps_dfs += robot.move_count total_turns_dfs += robot.turn_count print('steps taken by dfs: %d, turns taken: %d, time taken: %.2fms' % (robot.move_count, robot.turn_count, elapsed * 1000)) # run with bfs robot = Robot(matrix, start_position, start_direction) sweeper = Sweeper(robot) # sweeper.loggable = False # robot.loggable = True # start = time.time() # sweeper.sweep() # elapsed = time.time() - start total_elapsed_bfs += elapsed total_steps_bfs += robot.move_count total_turns_bfs += robot.turn_count print( 'steps taken by planned bfs: %d, turns taken: %d, time taken: %.2fms' % (robot.move_count, robot.turn_count, elapsed * 1000)) # sweeper.print_map() # robot.log() print('DFS: average steps taken: %d, turns taken: %d, time taken: %.2fms' % (int(total_steps_dfs / no_matrix), int(total_turns_dfs / no_matrix), total_elapsed_dfs * 1000 / no_matrix)) print( 'Planned BFS: average steps taken: %d, turns taken: %d, time taken: %.2fms' % (int(total_steps_bfs / no_matrix), int(total_turns_bfs / no_matrix), total_elapsed_bfs * 1000 / no_matrix)) print(lst)
import os import numpy as np from sweeper import Sweeper if __name__ == '__main__': start_idx = 0 end_idx = 640 config_file = 'config_files/sw_actor_critic.json' max_steps = 40000 project_root = os.path.abspath(os.path.join(os.path.dirname(__file__))) sweeper = Sweeper(os.path.join(project_root, config_file)) eval = [] for k in range(sweeper.total_combinations): eval.append([]) for idx in range(start_idx, end_idx): cfg = sweeper.parse(idx) log_dir = cfg.get_logdir() log_path = os.path.join(log_dir, 'steps_log') with open(log_path, "r") as f: lines = f.readlines() if len(lines) == 0: continue # ugly parse based on the log_file format num_steps = int(lines[-1].split("|")[1].split(":")[1]) episodes = int(lines[-1].split("|")[2].split(":")[1]) if cfg.max_steps == num_steps:
def test_file_dups_nodups(self): swp = Sweeper(topdirs=[os.path.join(mydir, 'testfiles_nodups')]) dups = swp.file_dups() for h, flist in dups.items(): self.assertTrue(len(flist) == 1)