def plan_path_no_temp_info(g, base_availability_models, base_model_variances, availability_observations, requests_left_to_deliver, curr_time, curr_node, mu, params): constant_availability_models = {} constant_model_variances = {} for request in requests_left_to_deliver: avails = [] variances = [] num_intervals = int(params['budget'] / params['time_interval']) # for i in range(len(base_availability_models[request])): for i in range(num_intervals): avails.append(1.0) variances.append(0.0) constant_availability_models[request] = avails constant_availability_models[request] = variances st_g = SpatioTemporalGraph( constant_availability_models, constant_model_variances, availability_observations, False, mu, int((params['budget'] - curr_time) / params['time_interval']), params['budget'] - curr_time, params['time_interval'], params['maintenance_node'], params['maintenance_reward'], params['deliver_reward'], params['uncertainty_penalty'], params['observation_reward'], params['deliver_threshold'], False) st_g.build_graph_single_delivery(g, curr_node, curr_time, requests_left_to_deliver, availability_observations, False, False, False, params['ensemble_method']) L = st_g.topological_sort() path = st_g.calc_max_profit_path_single_delivery(L, requests_left_to_deliver, False, params['ensemble_method']) return path
def plan_path_observe_emergent(g, base_availability_models, base_model_variances, availability_observations, requests_left_to_deliver, curr_time, curr_node, mu, params): params['uncertainty_penalty'] = 0.0 params['observation_reward'] = 0.0 params['deliver_threshold'] = 0.0 st_g = SpatioTemporalGraph( base_availability_models, base_model_variances, availability_observations, True, mu, int((params['budget'] - curr_time) / params['time_interval']), params['budget'] - curr_time, params['time_interval'], params['maintenance_node'], params['maintenance_reward'], params['deliver_reward'], params['uncertainty_penalty'], params['observation_reward'], params['deliver_threshold'], params['use_gp']) st_g.build_graph_single_delivery(g, curr_node, curr_time, requests_left_to_deliver, availability_observations, True, False, False, params['ensemble_method']) L = st_g.topological_sort() path = st_g.calc_max_profit_path_single_delivery(L, requests_left_to_deliver, False, params['ensemble_method']) return path
def sample_best_path(g, base_availability_models, base_model_variances, availability_observations, requests_left_to_deliver, budget, time_interval, num_intervals, curr_time, curr_node, maintenance_node, mu, maintenance_reward, deliver_reward, uncertainty_penalty, observation_reward, deliver_threshold, num_paths, num_worlds, incorporate_observation, incorporate_observation_hack, variance_bias, multiple_visits, replan, schedule_generation_method, use_gp): # generate potential solutions solution_set = [] for path_index in range(num_paths): sample_availability_models = {} for request in requests_left_to_deliver: sample_availability_models[ request] = sample_bernoulli_avialability_model( base_availability_models[request]) st_g = SpatioTemporalGraph( sample_availability_models, base_model_variances, availability_observations, True, mu, int((budget - curr_time) / time_interval), budget - curr_time, time_interval, maintenance_node, maintenance_reward, deliver_reward, uncertainty_penalty, observation_reward, deliver_threshold, use_gp) st_g.build_graph(g, curr_node, curr_time, requests_left_to_deliver, availability_observations, incorporate_observation, incorporate_observation_hack, variance_bias, params['ensemble_method']) ## topological sort # print ("Sorting of the topological variety...") L = st_g.topological_sort() ## maximal profit path # print ("Calculating path...") # print () path = st_g.calc_max_profit_path(L, requests_left_to_deliver, multiple_visits) solution_set.append(path) # generate evaluation worlds sim_worlds = {} for world_index in range(num_worlds): true_availability_models = {} for request in requests_left_to_deliver: true_availability_models[ request] = sample_bernoulli_avialability_model( base_availability_models[request]) sim_worlds[world_index] = generate_schedule( requests_left_to_deliver, true_availability_models, mu, num_intervals, schedule_generation_method, incorporate_observation) # evaluate potential solutions best_path = None best_score = -float("inf") for orig_path in solution_set: total_score = 0.0 ave_score = 0.0 for world_index in range(num_worlds): ### plan, execution loop num_requests = len(requests_left_to_deliver) sim_requests_left_to_deliver = copy.deepcopy( requests_left_to_deliver) sim_availability_observations = copy.deepcopy( availability_observations) total_profit = 0.0 total_maintenance_profit = 0.0 delivery_history = [] sim_time = curr_time sim_curr_node = curr_node path_length = 1 path_visits = 0 orig_path_used = False while (path_visits < path_length): if orig_path_used: st_g = SpatioTemporalGraph( base_availability_models, base_model_variances, availability_observations, True, mu, int((budget - sim_time) / time_interval), budget - sim_time, time_interval, maintenance_node, maintenance_reward, deliver_reward, uncertainty_penalty, observation_reward, deliver_threshold, use_gp) st_g.build_graph(g, sim_curr_node, sim_time, sim_requests_left_to_deliver, sim_availability_observations, incorporate_observation, incorporate_observation_hack, variance_bias, params['ensemble_method']) ## topological sort # print ("Sorting of the topological variety...") L = st_g.topological_sort() ## maximal profit path # print ("Calculating path...") # print () path = st_g.calc_max_profit_path( L, sim_requests_left_to_deliver, multiple_visits) else: path = orig_path orig_path_used = True ### Execute path_visits = 1 for visit in path[1:]: if sim_curr_node == visit: dist = 1 else: # dist = g.get_distance(sim_curr_node, visit) dist = g[sim_curr_node][visit]['weight'] sim_time += dist sim_curr_node = visit path_visits += 1 if visit == maintenance_node: total_maintenance_profit += maintenance_reward if visit in sim_requests_left_to_deliver: # profit = availability_models[trial][visit](sim_time) # total_profit += profit # delivery_history.append([visit, sim_time]) # sim_requests_left_to_deliver.remove(visit) curr_time_index = int(sim_time / time_interval) # if curr_time_index > (num_intervals - 1): # print("Curr time index exceeds num intervals: " + str(curr_time_index) + ", " + str(num_intervals)) # curr_time_index = num_intervals-1 assert (curr_time_index <= (num_intervals - 1)) available = sim_worlds[world_index][request][ curr_time_index] if bool(available): sim_requests_left_to_deliver.remove(visit) total_profit += deliver_reward delivery_history.append([visit, sim_time]) if multiple_visits: if replan: if incorporate_observation: sim_availability_observations[ visit] = [1, sim_time] path_visits = 0 path_length = 1 break else: if replan: # print ("Replanning, curr time: " + str(sim_time)) if incorporate_observation: sim_availability_observations[visit] = [ 0, sim_time ] path_visits = 0 path_length = 1 break ratio_divisor = num_requests + (budget - num_requests) * maintenance_reward competitive_ratio = (float(total_profit) + total_maintenance_profit) / ratio_divisor total_score += competitive_ratio ave_score = total_score / num_worlds if ave_score > best_score: best_path = orig_path best_score = ave_score return best_path