def list_problems(patterns): """ List problems. :param patterns: Patterns for problems. :return: """ for p in problems.find_problems(): if not patterns: print "[%s] %s" % (p.index, p.name) else: base_info = [ str(p.index), str(p.name.lower()), str(p.get_description().lower()), ] for x in patterns: is_matched = False for b in base_info: if x in b: is_matched = True if is_matched: print "[%s] %s" % (p.index, p.name)
def get_problems_for_smartEstimate(): NUM_PROBLEMS = 50 param = ProblemParameters(BOARD_X=5, BOARD_Y=5, NUM_ROCKS=1, NUM_SOILS=1, RAND_RANGE=10, A_PROB=0.3) to_return = {} to_return[param] = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, limit=NUM_PROBLEMS) return to_return
def get_problems_for_rand_board_sizes(): NUM_PROBLEMS = 20 BOARD_SIDES = range(5, 12) problems = {} for SIDE in BOARD_SIDES: param = ProblemParameters(BOARD_X=int(math.floor(SIDE)), BOARD_Y=int(math.ceil(SIDE)),\ NUM_ROCKS=3, NUM_SOILS=3, RAND_RANGE=10, A_PROB=0.3) problems[param] = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, limit=NUM_PROBLEMS) return problems
def get_problems_for_BPR_over_costs(): NUM_PROBLEMS = 50 param = ProblemParameters(BOARD_X=5, BOARD_Y=5, NUM_ROCKS=1, NUM_SOILS=1, RAND_RANGE=10, A_PROB=0.3) to_return = {} problems = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, limit=NUM_PROBLEMS) to_return[param] = problems return to_return
def get_problems_for_smartEstimateII(): NUM_PROBLEMS = 30 BOARD_SIDES = [0.5 * x for x in range(10,22)] problems = {} for SIDE in BOARD_SIDES: param = ProblemParameters(BOARD_X=int(math.floor(SIDE)), BOARD_Y=int(math.ceil(SIDE)),\ NUM_ROCKS=1, NUM_SOILS=1, RAND_RANGE=10, A_PROB=0.5) problems[param] = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, limit=NUM_PROBLEMS) return problems
def show_problem(patterns): """ Show description of problems. :param patterns: :return: """ for p in problems.find_problems(): if str(p.index) in patterns: print "[%s] %s" % (p.index, p.name) print p.get_description()
def get_problems_for_randPlanner(): NUM_PROBLEMS = 50 n_r = 2 n_s = 2 a_prob = 0.5 param = ProblemParameters(BOARD_X=5, BOARD_Y=5, NUM_ROCKS=n_r, NUM_SOILS=n_s, RAND_RANGE=10,\ A_PROB=a_prob) to_return = {} to_return[param] = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, limit=NUM_PROBLEMS) return to_return
def get_problems_for_costs(num_repeat=1): NUM_PROBLEMS = 2 n_r = 2 n_s = 2 a_prob = 0.5 num_agents = 3 param = ProblemParameters(BOARD_X=5, BOARD_Y=5, NUM_ROCKS=n_r, NUM_SOILS=n_s, RAND_RANGE=10, A_PROB=a_prob) to_return = {} to_return[param] = ProblemLib.find_problems(param.BOARD_X, param.BOARD_Y, NUM_ROCKS=param.NUM_ROCKS, NUM_SOILS=param.NUM_SOILS,\ RAND_RANGE=param.RAND_RANGE, RAND_PROB=param.A_PROB, num_agent=num_agents, limit=NUM_PROBLEMS) return to_return
def run_problem(patterns): """ Run problems. :param patterns: :return: """ for p in problems.find_problems(): if str(p.index) in patterns: try: pi = p() assert isinstance(pi, base.ProblemBase) pi.solve() except NotImplementedError as ex: logging.error(ex)
def TestOneRandomProb(): # PROBLEMS = [rrw.make_random_problem(BOARD_X, BOARD_Y, \ # name=str(time.time()) + '.' + str(i)) for i in range(10)] # PROBLEMS = ProblemLib.find_problems(BOARD_X, BOARD_Y, RAND_PROB=0.3, limit=20)[14:] PROBLEMS = ProblemLib.find_problems(BOARD_X, BOARD_Y, problem_name="5_5_21_10_0.5_603") for PROBLEM in PROBLEMS: for AGENT_TYPE in MODELS: for PLANNER in PLANNERS: # Each point is the average over all problems PROBLEM.COST_OF_COMM = 0.1 PROBLEM.COST_REPLAN = 0 num_iter = 1 costs = 0 for i in range(num_iter): # Run simulation = Simulation(PROBLEM, AGENT_TYPE, PLANNER, gui=True) simulation.run() costs += simulation.get_total_cost() # logger.info(simulation.get_summary(cost=True, cost_bd=True,obs=True, comm=True, void=True, planner=False)) print("problem: {} avg: {}".format(PROBLEM, costs*1.0/num_iter))
def SimulateVaryingBoard(COC): lines = [] base_line = None base_line_name = None # 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] = ProblemLib.find_problems(int(math.floor(SIDE)), int(math.ceil(SIDE)),\ RAND_RANGE=RAND_RANGE, RAND_PROB=RAND_PROB, limit=NUM_PROBLEMS) simulations = {} plot_lines = {} raw_lines = {} percent_improv = {} for PLANNER in PLANNERS: # Each planner result in a different plot for AGENT_TYPE in MODELS: # Each agent is a line in the plot line_name = PLANNER.name + '_' + AGENT_TYPE.__name__ logger.info("*** Running simulations for [MODEL: {}]".format(line_name)) simulations[line_name] = {} cur_line = (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][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 # 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('>') sys.stdout.flush() avg_cost = costs * 1.0 / len(simulations[line_name][SIDE]) cur_line[1].append(avg_cost) sys.stdout.write('\n') sys.stdout.flush() if base_line == None: plot_lines[line_name] = cur_line base_line_name = line_name base_line = plot_lines[line_name] else: plot_lines[line_name] = (cur_line[0], [x-y for x, y in zip(cur_line[1], base_line[1])]) percent_improv[line_name] = (cur_line[0], [(x-y) * 1.0 / y for x, y in zip(cur_line[1], base_line[1])]) raw_lines[line_name] = cur_line # Renormalizing based on the no-comm baseline. plot_lines[base_line_name] = (BOARD_SIDES, [0]*len(BOARD_SIDES)) percent_improv[base_line_name] = (BOARD_SIDES, [0]*len(BOARD_SIDES)) # Plot lines logger.info("Plot Lines: {}".format(plot_lines)) # Adjust Plotting exp_id = int(time.time()) """ First we plot the lines relative to no-comm """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in plot_lines.items(): lines.append(ax.plot([x * x for x in 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}) for legobj in legend.legendHandles: legobj.set_linewidth(2.0) # Labels plt.xlabel("Board Size") plt.ylabel("Average Cost-differences relative to no-comm") plt.title('Planner:{} COC:{} -- Averaged over {} Random Problem'.format(PLANNER.name, COC, NUM_PROBLEMS)) plt.setp(lines, linewidth=2.0) plt.savefig("images/VaryingBoardSize_{}.png".format(exp_id)) """ Same plot but x-axis is board-length """ 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}) for legobj in legend.legendHandles: legobj.set_linewidth(2.0) # Labels plt.xlabel("Board Size") plt.ylabel("Average Cost-differences relative to no-comm") plt.title('Planner:{} COC:{} -- Averaged over {} Random Problem'.format(PLANNER.name, COC, NUM_PROBLEMS)) plt.setp(lines, linewidth=2.0) plt.savefig("images/VaryingBoardLength_{}.png".format(exp_id)) # """ # Second plot, we plot the raw lines # """ # fig = plt.figure() # ax = plt.subplot(111) # for (name, line) in raw_lines.items(): # lines.append(ax.plot([x * x for x in 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}) # for legobj in legend.legendHandles: # legobj.set_linewidth(2.0) # # Labels # plt.xlabel("Board Size") # plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) # plt.title('Planner:{} COC:{} -- Raw Costs'.format(PLANNER.name, COC)) # plt.setp(lines, linewidth=2.0) # plt.savefig("images/VaryingBoardSize_raw{}.png".format(exp_id)) # """ # Same plot but x-axis is board-length # """ # fig = plt.figure() # ax = plt.subplot(111) # for (name, line) in raw_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}) # for legobj in legend.legendHandles: # legobj.set_linewidth(2.0) # # Labels # plt.xlabel("Board Size") # plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) # plt.title('Planner:{} COC:{} -- Raw Costs'.format(PLANNER.name, COC)) # plt.setp(lines, linewidth=2.0) # plt.savefig("images/VaryingBoardLength_raw{}.png".format(exp_id)) """ third plot, we plot the percent-increase """ fig = plt.figure() ax = plt.subplot(111) for (name, line) in percent_improv.items(): lines.append(ax.plot([x * x for x in 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}) for legobj in legend.legendHandles: legobj.set_linewidth(2.0) # Labels plt.xlabel("Board Size") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} COC:{} -- Improvement over Baseline'.format(PLANNER.name, COC)) plt.setp(lines, linewidth=2.0) plt.savefig("images/VaryingBoardLength_percImprov_{}.png".format(exp_id)) """ Same plot but x-axis is board-length """ 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}) for legobj in legend.legendHandles: legobj.set_linewidth(2.0) # Labels plt.xlabel("Board Size") plt.ylabel("Average Costs over {} Random Problems".format(NUM_PROBLEMS)) plt.title('Planner:{} COC:{} -- Improvement over Baseline'.format(PLANNER.name, COC)) plt.setp(lines, linewidth=2.0) plt.savefig("images/VaryingBoardLength_percImprov_{}.png".format(exp_id)) plt.show()