def write_problems(x, y, rand_range, max_cost, a_prob, num_repeat, file_name=None, file_obj=None): if file_obj == None: file_obj = open("problems/problem_X{}_Y{}.txt".format(x, y), 'a') problems = [] for i in range(num_repeat): p_name = "{}_{}_{}_{}_{}".format(x, y, i, rand_range, a_prob) + "_%.0f" % (time.time()*100000%1000) problems.append(rrw.make_random_problem(x, y, rand_range=rand_range, max_cost=max_cost, a_prob=a_prob, name=p_name)) write_problems_to_file(problems, file_obj=file_obj)
def CompareDetVsRand_BoardSize(COC): global MODELS # First make the same set of NUM_PROBLEMS for a given Board-size for all models PROBLEMS_dict = {} for SIDE in BOARD_SIDES: PROBLEMS_dict[SIDE] = [rrw.make_random_problem(int(math.floor(SIDE)), int(math.ceil(SIDE)), rand_range=RAND_RANGE, \ name=str(time.time()) + '.' + str(i)) \ for i in range(NUM_PROBLEMS)] lines = [] simulations = {} plot_lines = {} relative_to_noComm = {} baseline_avg = {} percent_improv = {} for PLANNER in PLANNERS: # Each planner result in a different plot if PLANNER.name == Planner.get_HPlanner_v15().name: MODELS = [models.AgentNoComm, models.AgentSmartEstimate] for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = AGENT_TYPE.__name__ + '_' + PLANNER.name logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} plot_lines[line_name] = [BOARD_SIDES, []] relative_to_noComm[PLANNER.name] = [BOARD_SIDES, []] percent_improv[PLANNER.name] = [BOARD_SIDES, []] for SIDE in BOARD_SIDES: # Each cost is a data point sys.stdout.write('SIDE : {}\t'.format(SIDE)) sys.stdout.flush() simulations[line_name][SIDE] = [] costs = 0 for PROBLEM in PROBLEMS_dict[SIDE]: # Each point is the average over all problems PROBLEM.COST_OF_COMM = COC PROBLEM.COST_REPLAN = 0 if 'Det' in PLANNER.name and 'Rand' not in PLANNER.name: num_iter = 1 elif 'Rand' in PLANNER.name and 'Det' not in PLANNER.name: num_iter = 5 else: assert(False), "PlannerName: {}".format(PLANNER.name) for j in range(num_iter): # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=GUI) simulation.run() # Do not include simulation if there is no solution if simulation.get_total_cost() > sys.maxint/2: continue # Add simulations[line_name][SIDE].append(simulation) costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=False, obs=False, comm=True, void=True)) sys.stdout.write('{},'.format(int(simulation.get_total_cost()))) sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][SIDE]) plot_lines[line_name][1].append(avg_cost) if AGENT_TYPE.__name__ == 'AgentNoComm': baseline_avg[(PLANNER.name, SIDE)] = avg_cost else: b = baseline_avg[(PLANNER.name, SIDE)] relative_to_noComm[PLANNER.name][1].append(b-avg_cost) percent_improv[PLANNER.name][1].append((b-avg_cost)/b) sys.stdout.write('Avg: {}\n'.format(avg_cost)) sys.stdout.flush() logger.info("Plot Lines: {}".format(plot_lines)) lines = [] # Adjust Plotting fig = plt.figure() ax = plt.subplot(111) for (name, line) in plot_lines.items(): lines.append(ax.plot(line[0], line[1], label=name)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Board Size") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandBoard_raw_{}".format(time.time()%1000) plt.savefig(filename+".png") """ Plot Improvement of Cost """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in relative_to_noComm.items(): lines.append(ax.plot(line[0], line[1], label=name)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Board Size") plt.ylabel("SmartComm Improvement") # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandBoard_diff_{}".format(time.time()%1000) plt.savefig(filename+".png") """ Plot percent improved """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in percent_improv.items(): lines.append(ax.plot(line[0], line[1], label=name)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Board Size") plt.ylabel("SmartComm Improvement") # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandBoard_perImprov_{}".format(time.time()%1000) plt.savefig(filename+".png") plt.show()
def SimulateVaryingCosts(BOARD_X, BOARD_Y): global COSTS PROBLEMS = [rrw.make_random_problem(BOARD_X, BOARD_Y, \ name=str(time.time()) + '.' + str(i)) for i in range(NUM_PROBLEMS)] # PROBLEMS = [rrw.make_rand_nav_problem(BOARD_X, BOARD_Y, \ # name=str(time.time()) + '.' + str(i)) for i in range(NUM_PROBLEMS)] # PROBLEMS=[problem_bank.test_exp_cost()] simulations = {} plot_lines = {} for PLANNER in PLANNERS: # Each planner result in a different plot #Temp if PLANNER.name == Planner.get_HPlanner_v15().name: MODELS = [models.AgentSmartComm] else: MODELS = [models.AgentSmartComm, models.AgentNoComm, models.AgentFullComm] for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = AGENT_TYPE.__name__ + '_' + PLANNER.name logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} plot_lines[line_name] = (COSTS, []) # X, Y for COC in COSTS: # Each cost is a data point sys.stdout.write('COST : {}\t'.format(COC)) sys.stdout.flush() simulations[line_name][COC] = [] costs = 0 for PROBLEM in PROBLEMS: # Each point is the average over all problems PROBLEM.COST_OF_COMM = COC PROBLEM.COST_REPLAN = 0 # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=GUI) simulation.run() sys.stdout.write('>') # Do not include simulation if there is no solution if simulation.get_total_cost() > sys.maxint/2: continue # Add simulations[line_name][COC].append(simulation) costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=False, obs=False, comm=True, void=True)) sys.stdout.write('{}\t'.format(int(simulation.get_total_cost()))) sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][COC]) plot_lines[line_name][1].append(avg_cost) sys.stdout.write('\n') sys.stdout.flush() logger.info("Plot Lines: {}".format(plot_lines)) lines = [] # Adjust Plotting fig = plt.figure() ax = plt.subplot(111) for (name, line) in plot_lines.items(): lines.append(ax.plot(line[0], line[1], label=name)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Cost of Communication") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} Board-Size:{}'.format(PLANNER.planner.__name__, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/SimulateVaryingCosts_{}".format(time.time()%1000) plt.savefig(filename+".png") plt.show()
def CompareDetVsRand_Cost(BOARD_X, BOARD_Y): global MODELS PROBLEMS = [rrw.make_random_problem(BOARD_X, BOARD_Y, rand_range=RAND_RANGE, \ name=str(time.time()) + '.' + str(i)) \ for i in range(NUM_PROBLEMS)] lines = [] simulations = {} plot_lines = {} relative_to_noComm = {} baseline_avg = {} percent_improv = {} for PLANNER in PLANNERS: # Each planner result in a different plot num_iter = 1 if PLANNER.name == Planner.get_HPlanner_bb().name: MODELS = [models.AgentNoComm, models.AgentSmartPlanRec] elif PLANNER.name == Planner.get_HPlanner_v15().name: MODELS = [models.AgentNoComm, models.AgentSmartEstimate] num_iter = 5 elif PLANNER.name == Planner.get_HPlanner_v17().name: MODELS = [models.AgentNoComm, models.AgentSmartEstimate] num_iter = 5 else: MODELS = [models.AgentNoComm, models.AgentSmartComm] for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = AGENT_TYPE.__name__ + '_' + PLANNER.name logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} plot_lines[line_name] = [COSTS, [0 for i in range(len(COSTS))]] if AGENT_TYPE.__name__ != 'AgentNoComm': relative_to_noComm[line_name] = [COSTS, [0 for i in range(len(COSTS))]] percent_improv[line_name] = [COSTS, [0 for i in range(len(COSTS))]] if AGENT_TYPE.__name__ == 'AgentNoComm': USE_COSTS = [0] baseline_costs = {} elif AGENT_TYPE.__name__ == 'AgentFullComm': USE_COSTS = [COSTS[0], COSTS[-1]] plot_lines[line_name] = [USE_COSTS, [0 for i in range(len(USE_COSTS))]] else: USE_COSTS = COSTS for i in range(len(USE_COSTS)): COC = USE_COSTS[i] # Each cost is a data point sys.stdout.write('COST : {}\t'.format(COC)) sys.stdout.flush() simulations[line_name][COC] = [] costs = 0 for P in PROBLEMS: PROBLEM = copy.deepcopy(P) """ Pretending to have elimited the other possible decompositions. Lowering the amount of uncertainty """ # if PLANNER.name == Planner.get_HPlanner_v17().name: # PROBLEM = copy.deepcopy(P) # PROBLEM.soils.remove('S1') # PROBLEM.rocks.remove('R1') """ Similarly, removing uncertainties """ if PLANNER.name == Planner.get_HPlanner_v17().name: PROBLEM = copy.deepcopy(P) PROBLEM.soils = [] # Each point is the average over all problems PROBLEM.COST_OF_COMM = COC PROBLEM.COST_REPLAN = 0 # if 'Det' in PLANNER.name and 'Rand' not in PLANNER.name: # num_iter = 1 # elif 'Rand' in PLANNER.name and 'Det' not in PLANNER.name: # num_iter = 10 # else: # assert(False), "PlannerName: {}".format(PLANNER.name) for j in range(num_iter): # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=GUI) simulation.run() # Do not include simulation if there is no solution if simulation.get_total_cost() > sys.maxint/2: continue # Add simulations[line_name][COC].append(simulation) costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=False, obs=False, comm=True, void=True)) if AGENT_TYPE.__name__ == 'AgentNoComm': baseline_costs[PROBLEM.name] = simulation.get_total_cost() sys.stdout.write('{}, '.format(int(simulation.get_total_cost()))) else: diff = int(simulation.get_total_cost() - baseline_costs[PROBLEM.name]) sys.stdout.write('{}, '.format(diff)) # if PLANNER.name == Planner.get_HPlanner_v14().name \ # and AGENT_TYPE.__name__ == 'AgentSmartComm' \ # and diff > 2 and COC < 1: # PROBLEMS.remove(P) sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][COC]) plot_lines[line_name][1][i] = avg_cost if AGENT_TYPE.__name__ == 'AgentNoComm': baseline_avg[PLANNER.name] = avg_cost plot_lines[line_name][1] = [avg_cost for i in range(len(COSTS))] else: relative_to_noComm[line_name][1][i] = (baseline_avg[PLANNER.name]-avg_cost) percent_improv[line_name][1][i] = (baseline_avg[PLANNER.name]-avg_cost)/baseline_avg[PLANNER.name] sys.stdout.write('Avg: {}\n'.format(avg_cost)) sys.stdout.flush() logger.info("Plot Lines: {}".format(plot_lines)) lines = [] # Adjust Plotting fig = plt.figure() ax = plt.subplot(111) for (name, line) in plot_lines.items(): lines.append(ax.plot(line[0], line[1], label=name, marker='o')) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Cost of Communication") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandCost_raw_{}".format(time.time()%1000) plt.savefig(filename+".png") """ Plot Improvement of Cost """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in relative_to_noComm.items(): if 'Estimate' in name: c = 'green' elif 'SmartCommII' in name: c = 'orange' elif 'SmartComm' in name: c = 'blue' lines.append(ax.plot(line[0], line[1], label=name, marker='o', color=c)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Cost of Communication") plt.ylabel("SmartComm Improvement") # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandCost_diff_{}".format(time.time()%1000) plt.savefig(filename+".png") """ Plot percent improved """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in percent_improv.items(): if 'Estimate' in name: c = 'green' elif 'SmartCommII' in name: c = 'orange' elif 'SmartComm' in name: c = 'blue' lines.append(ax.plot(line[0], line[1], label=name, marker='o', color=c)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Cost of Communication") plt.ylabel("SmartComm Improvement") # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/CompareDetVsRandCost_perImprov_{}".format(time.time()%1000) plt.savefig(filename+".png") plt.show()
def SimulateVaryingBoard(COC): for PLANNER in PLANNERS: # Each planner result in a different plot simulations = {} plot_lines = {} # First make the same set of NUM_PROBLEMS for a given Board-size for all models PROBLEMS_dict = {} for SIDE in BOARD_SIDES: PROBLEMS_dict[SIDE] = [rrw.make_random_problem(SIDE, SIDE, \ name=str(time.time()) + '.' + str(i)) for i in range(NUM_PROBLEMS)] for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = AGENT_TYPE.__name__ + PLANNER.name logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} plot_lines[line_name] = ([x * x for x in BOARD_SIDES], []) # X, Y for SIDE in BOARD_SIDES: # Each cost is a data point sys.stdout.write('SIDE : {}\t'.format(SIDE)) sys.stdout.flush() simulations[line_name][COC] = [] costs = 0 for PROBLEM in PROBLEMS_dict[SIDE]: # Each point is the average over all problems PROBLEM.COST_OF_COMM = COC PROBLEM.COST_REPLAN = 0 # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=GUI) simulation.run() # Do not include simulation if there is no solution if simulation.get_total_cost() > sys.maxint/2: continue # Add simulations[line_name][COC].append(simulation) costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=False, obs=False, comm=True, void=True)) sys.stdout.write('>\t') sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][COC]) plot_lines[line_name][1].append(avg_cost) sys.stdout.write('\n') sys.stdout.flush() logger.info("Plot Lines: {}".format(plot_lines)) lines = [] for (name, line) in plot_lines.items(): lines.append(plt.plot(line[0], line[1], label=name)) plt.legend(loc='lower right') plt.xlabel("Board Size") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} COC:{}'.format(PLANNER.planner.__name__, COC)) plt.setp(lines, linewidth=2.0) plt.savefig("images/SimulateVaryingBoardSize_{}.png".format(time.time()%1000)) plt.show()
def SimulateVaryingCosts_Det_Planner(BOARD_X, BOARD_Y): global COSTS, MODELS PROBLEMS = [rrw.make_random_problem(BOARD_X, BOARD_Y, rand_range=RAND_RANGE, max_cost=MAX_COST,\ name=str(time.time()) + '.' + str(i)) \ for i in range(NUM_PROBLEMS)] lines = [] simulations = {} plot_lines = {} for PLANNER in PLANNERS: # Each planner result in a different plot if PLANNER.name == Planner.get_HPlanner_bb_prob().name: MODELS = [models.AgentSmartBPRII, models.AgentSmartEstimate] for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = AGENT_TYPE.__name__ + '_' + PLANNER.name logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} plot_lines[line_name] = [COSTS, [0 for i in range(len(COSTS))]] if AGENT_TYPE.__name__ == 'AgentNoComm': USE_COSTS = [0] baseline_costs = {} elif AGENT_TYPE.__name__ == 'AgentFullComm': USE_COSTS = [COSTS[0], COSTS[-1]] plot_lines[line_name] = [USE_COSTS, [0 for i in range(len(USE_COSTS))]] else: USE_COSTS = COSTS for i in range(len(USE_COSTS)): COC = USE_COSTS[i] # Each cost is a data point sys.stdout.write('COST : {}\t'.format(COC)) sys.stdout.flush() simulations[line_name][COC] = [] costs = 0 for PROBLEM in PROBLEMS: # Each point is the average over all problems PROBLEM.COST_OF_COMM = COC PROBLEM.COST_REPLAN = 0 num_iter = 3 for j in range(num_iter): # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=GUI) simulation.run() # Do not include simulation if there is no solution if simulation.get_total_cost() > sys.maxint/2: continue # Add simulations[line_name][COC].append(simulation) costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=False, obs=False, comm=True, void=True)) if AGENT_TYPE.__name__ == 'AgentNoComm': baseline_costs[PROBLEM.name] = simulation.get_total_cost() sys.stdout.write('{}, '.format(int(simulation.get_total_cost()))) else: diff = int(simulation.get_total_cost() - baseline_costs[PROBLEM.name]) sys.stdout.write('{}, '.format(diff)) # if diff > 1 and ('Smart' in AGENT_TYPE.__name__): # Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=True) sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][COC]) plot_lines[line_name][1][i] = avg_cost # FOr No-COMM, the cost is the same for all COC values. if AGENT_TYPE.__name__ == 'AgentNoComm': plot_lines[line_name][1] = [avg_cost for i in range(len(COSTS))] sys.stdout.write('Avg: {}\n'.format(avg_cost)) sys.stdout.flush() logger.info("Plot Lines: {}".format(plot_lines)) lines = [] # Adjust Plotting fig = plt.figure() ax = plt.subplot(111) for (name, line) in plot_lines.items(): # if 'NoComm' in name: # c = 'green' # elif 'SmartCommII' in name: # c = 'cyan' # elif 'SmartComm' in name: # c = 'blue' # elif 'FullComm' in name: # c = 'red' # elif 'RandComm' in name: # c = 'purple' # else: # c = None # if c == None: # lines.append(ax.plot(line[0], line[1], label=name)) # else: # lines.append(ax.plot(line[0], line[1], label=name, color=c)) lines.append(ax.plot(line[0], line[1], label=name)) # Locate Legend box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.2, box.width, box.height * 0.8]) legend = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.13), fancybox=True, shadow=True, ncol=2, prop={'size':9}) # Set Line Width for legobj in legend.legendHandles: legobj.set_linewidth(2.0) plt.setp(lines, linewidth=2.0) # Labels plt.xlabel("Cost of Communication") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # plt.title('Planner:{} Board-Size:{}'.format(PLANNER.name, BOARD_X*BOARD_Y)) # write simulation parameters to file. filename = "images/VaryingCosts_{}_{}".format(PLANNER.name, time.time()%1000) plt.savefig(filename+".png") plt.show()