def _write_move(self, all_buses, prefix): all_movements = self._assign.compute_all_movements( self._assign.get_buses()) if len(all_movements) > 0: specific_dir = self._output_directory + prefix create_dir(specific_dir) result_move = open(specific_dir + "/results_move.csv", "w+") move_heading = trip_heading_prefix for _bus in all_buses: move_heading += _bus.__str__() + ", " move_heading += "\n" result_move.write(move_heading) for movement_dict in all_movements: for movement in movement_dict.keys(): move_content = movement.__content__() for _bus in all_buses: if _bus.__key__() == movement_dict[movement].__key__(): move_content += "1," else: move_content += "0," move_content += "\n" result_move.write(move_content) result_move.flush() os.fsync(result_move.fileno()) result_move.close()
def split_files(file_name, lines_limit=500000): """ Args: file_name: name of the file, probably the file_name for energy estimates lines_limit: number of lines for each smaller files generated from the large file """ import pandas as pd dir_name = file_name.replace(".csv", "") df = pd.read_csv(file_name, usecols=[ "Start_Lat", "Start_Lon", "End_Lat", "End_Lon", "Path_Timestamp", "Predicted_Values" ], index_col=False) df.to_csv(file_name, columns=[ "Start_Lat", "Start_Lon", "End_Lat", "End_Lon", "Path_Timestamp", "Predicted_Values" ], index=False) input_file = open(file_name, "r+") contents = input_file.readlines() number_of_sub_portion = int(len(contents) / lines_limit) create_dir(dir_name) for i in range(number_of_sub_portion + 2): out_file = open(dir_name + "/{0}.csv".format(str(i)), "w+") out_file.writelines(contents[i * lines_limit:(i + 1) * lines_limit]) out_file.close() input_file.close()
def write(self, prefix): create_dir(self._output_directory) all_buses = self._assign.get_buses() filtered_trips = [] for trip in self._assign.get_trips(): if isinstance(trip, OperatingTrip): filtered_trips.append(trip) self._write_assign(filtered_trips, all_buses, prefix) self._write_move(all_buses, prefix)
def generate_random_dump_data_internal(info_enabled=True, dump_config=None, operating_trips=None, suffix=""): """ Args: info_enabled: this is a boolean value to determine updating info dump_config: dump configuration operating_trips: available operating trips suffix: additional suffix to contain multiple dump_structure with same dump_config Returns: selected operating trips Note : this function also update the dump.info file """ _selected_trips, _route_values = extract_specific_data_random(dump_config, operating_trips) _filtered_trips = [] if info_enabled: dump_info_file_name = dump_info_directory + dump_config.__key__() + suffix + "_dump.info" create_dir(dump_info_directory) route_details = FileWriter(dump_info_file_name) route_details.write("Total number of bus-lines : " + str(dump_config.route_limit)) route_details.write("Maximum number of trips per bus-line : " + str(dump_config.trip_limit)) route_details.write("\n[BUS LINE NUMBERS]\n" + "\n".join([str(route_no) for route_no in _route_values])) for route_no in _route_values: random.seed(random_seed) sample_trips = random.sample(_selected_trips[route_no], min(len(_selected_trips[route_no]), dump_config.trip_limit)) route_details.write("\n[TRIPS FOR BUS LINE NUMBER : " + str(route_no) + "]") route_details.write("\nNo, Trip ID, Start Time, End Time, Duration") route_details_dict = {} for trip in sample_trips: _filtered_trips.append(trip) start_time_in_sec = trip.start_s() if start_time_in_sec in route_details_dict: start_time_in_sec += random.randint(1, 100) route_details_dict[start_time_in_sec] = trip counter = 1 for time_in_sec in sorted(route_details_dict.keys()): trip = route_details_dict[time_in_sec] route_details.write(", ".join([str(counter), str(trip.trip_id), str(trip.start_time.time), str(trip.end_time.time), str(trip.duration.time)])) counter += 1 route_details.close() else: for route_no in _route_values: random.seed(random_seed) sample_trips = random.sample(_selected_trips[route_no], min(len(_selected_trips[route_no]), dump_config.trip_limit)) for trip in sample_trips: _filtered_trips.append(trip) cleaned_trips = [] for _trip in _filtered_trips: _duration = diff(_trip.end_time, _trip.start_time) _trip.end_time = diff(_trip.end_time, time(_duration.time_in_seconds * default_time_tol)) _trip.duration = diff(_trip.end_time, _trip.start_time) cleaned_trips.append(_trip) return cleaned_trips
def plot(obj): import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv(obj.summary_file_name) plt.figure() df.plot(x="iteration", y="energy_cost") plt.xlabel("Iterations") plt.ylabel("Energy Cost") image_file_name = obj.summary_file_name.replace(".csv", ".png") create_dir(image_directory) image_file_name = image_file_name.replace(summary_directory, image_directory) plt.savefig(image_file_name)
def update_dump_log(self, dump_config, start, end): if self.log_enabled: time_taken = end - start create_dir(self.log_directory) dump_log_file_name = self.log_directory + dump_config.__key__() + \ self.get_suffix() + self.dump_prefix + ".log" file_writer = FileAppender(dump_log_file_name) file_writer.append("Dump generation started at " + str(start)) file_writer.append("Dump generation finished at " + str(end)) file_writer.append("Time taken for dump generation is " + str(time_taken)) file_writer.close()
def run(self, arg): start_time = datetime.now() self._init_population(arg) convergence_limit = 0 minimum_cost = math.inf new_population = self.sys_population generation = 0 self.summary_file_name = summary_directory + "genetic_algorithm.csv" create_dir(summary_directory) summary_file = FileWriter(self.summary_file_name) summary_file.write("iteration,energy_cost") while convergence_limit < gen_alg_convergence_limit and generation < self.generation_limit: s_print("Current Generation {}".format(str(generation))) scores = score_population(new_population) best = new_population[scores.index(min(scores))] fitness_cost = fitness(best) self.best_costs.append(fitness_cost) if fitness_cost < minimum_cost: convergence_limit = 0 minimum_cost = fitness_cost self.over_all_best = best else: convergence_limit += 1 summary_file.write([generation, minimum_cost]) new_population = select( new_population, max(int(len(new_population) * selection_ratio), min_pop)) s_print("Creating next generation ") new_population.extend(self._create_crossover(new_population)) new_population.extend(self._create_mutation(new_population)) self.population_limit = len(new_population) self.generation_count = generation generation += 1 summary_file.close() end_time = datetime.now() self.time_consumed = (end_time - start_time).total_seconds() s_print("Total time taken is " + time(int(self.time_consumed)).time) if self.over_all_best is not None: if isinstance(self.over_all_best, Assignment): self.over_all_best.write(self.dump_structure.__key__()) self.over_all_best.write_bus_stat( self.dump_structure.__key__(), do_print=True)
def write_bus_stat(prefix, output_dir=output_directory, additional_prefix="", bus_stats=None): specific_dir = output_dir + prefix if additional_prefix != "": specific_dir = specific_dir + "_" + additional_prefix create_dir(specific_dir) result_bus_stat = open(specific_dir + "/bus_stats.csv", "w+") result_bus_stat.write(bus_stat_heading + "\n") for _bus in bus_stats: if _bus is not None: if not is_dummy(_bus): bus_stat = bus_stats[_bus] bus_stat_content = _bus.__str__( ) + "," + bus_stat.get_min_csv_line() bus_stat_content += "\n" result_bus_stat.write(bus_stat_content) result_bus_stat.flush() os.fsync(result_bus_stat.fileno()) result_bus_stat.close()
def _write_assign(self, filtered_trips, all_buses, prefix): if len(filtered_trips) > 0: specific_dir = self._output_directory + prefix create_dir(specific_dir) result_assign = open(specific_dir + "/results_assign.csv", "w+") assign_heading = trip_heading_prefix for _bus in all_buses: assign_heading += _bus.__str__() + ", " assign_heading += "\n" result_assign.write(assign_heading) for trip in filtered_trips: assign_content = trip.__content__() for _bus in all_buses: if _bus.__key__() == self._assign.get(trip).__key__(): assign_content += "1," else: assign_content += "0," assign_content += "\n" result_assign.write(assign_content) result_assign.flush() os.fsync(result_assign.fileno()) result_assign.close()
def _assist_inner(self, args=None): dump_config = DumpConfigWTC(3, 50, 17, 230, 1) day = self._selected_date.day month = self._selected_date.month month_val, day_val = convert_month_day_num_to_str( str(month) + "/" + str(day)) file_name = data_main_directory + "trips/" + month_val + "/" + day_val + ".csv" date_str = self._selected_date.strftime("%Y/%m/%d") print( "Assigning trips to buses as of data for date {}".format(date_str)) dump_structure = self._dump_util.load_filtered_data(dump_config) df = pd.read_csv(file_name) buses_dicts = {} for x in range(101, 152): buses_dicts[x] = bus(str(x), gas_bus_type) for x in range(501, 508): buses_dicts[x] = bus(str(x), gas_bus_type) for x in range(751, 755): buses_dicts[x] = bus(str(x), electric_bus_type) trips_dicts = {} for trip in dump_structure.filtered_trips: trips_dicts[trip.get_trip_id()] = trip trip_vehicle_pairs = [] real_times = {} real_times_stamps = {} trips = [] for i in range(len(df)): entry = df.iloc[i] try: trip_id = str(int(entry["tatripid"])) except ValueError: trip_id = "null" vehicle_id = str(entry["vid"]) start_time_stamp = entry["start_timestamp"] end_time_stamp = entry["end_timestamp"] start_time = entry["start_tmstmp"].split(" ")[1] + ":00" end_time = entry["end_tmstmp"].split(" ")[1] + ":00" route = str(entry["route_id"]) current_trip = None current_vehicle = None start_time_stamp = int(float(start_time_stamp)) end_time_stamp = int(float(end_time_stamp)) if route not in ["33", "34", "U", "PI", "PO"]: if trip_id in trips_dicts.keys(): current_trip = trips_dicts[trip_id] if int(vehicle_id) in buses_dicts.keys(): current_vehicle = buses_dicts[int(vehicle_id)] if start_time_stamp != end_time_stamp: if current_trip in trips: if current_trip is not None and current_vehicle is not None: (p_start_time, p_end_time ) = real_times[current_trip.get_trip_id()] (p_start_time_stamp, p_end_time_stamp, p_bus_name ) = real_times_stamps[current_trip.get_trip_id()] if p_bus_name == current_vehicle.bus_name: if p_start_time_stamp < start_time_stamp: start_time_stamp = p_start_time_stamp start_time = p_start_time if p_end_time_stamp > end_time_stamp: end_time_stamp = p_end_time_stamp end_time = p_end_time real_times[current_trip.get_trip_id()] = ( start_time, end_time) real_times_stamps[current_trip.get_trip_id( )] = (start_time_stamp, end_time_stamp, current_vehicle.bus_name) else: if int(float(end_time_stamp)) - int(float(start_time_stamp)) >= 300 >= \ int(float(p_end_time_stamp)) - int(float(p_start_time_stamp)): previous_vehicle = buses_dicts[int( p_bus_name)] trip_vehicle_pairs.remove( [current_trip, previous_vehicle]) trip_vehicle_pairs.append( [current_trip, current_vehicle]) real_times[current_trip.get_trip_id()] = ( start_time, end_time) real_times_stamps[current_trip.get_trip_id( )] = (start_time_stamp, end_time_stamp, current_vehicle.bus_name) else: trips.append(current_trip) if current_trip is not None and current_vehicle is not None: trip_vehicle_pairs.append( [current_trip, current_vehicle]) real_times[current_trip.get_trip_id()] = ( start_time, end_time) real_times_stamps[current_trip.get_trip_id()] = ( start_time_stamp, end_time_stamp, current_vehicle.bus_name) print("Number of filtered trips for forced assign {}".format( str(len(trip_vehicle_pairs)))) trip_count = 0 assign_pairs = {} for [selected_trip, selected_bus] in trip_vehicle_pairs: if selected_trip in trips_dicts.values( ) and selected_bus in buses_dicts.values(): trip_count += 1 time_in_sec = selected_trip.start_s() assigns = [] if time_in_sec in assign_pairs.keys(): assigns = assign_pairs[time_in_sec] assigns.append((selected_trip, selected_bus)) assign_pairs[time_in_sec] = assigns.copy() specific_trip_dir = trips_directory + month_val + "/" + day_val + "/" create_dir(specific_trip_dir) fu_assign = ForcedUpdate(dump_structure=dump_structure, result_dir=specific_trip_dir, date=date_str) _ = fu_assign.force_update(assign_pairs, real_times=real_times) self._assignment = fu_assign.assignment
def _open_writer(self): create_dir(self._output_dir) self.__summary = FileWriter(self._path)
def _save_model(self): create_dir(self._ip_file_name) self.write(self._ip_file_name)
def dump_obj(obj, file_name): create_dir(file_name) dump_file = open(file_name, "wb") pickle.dump(obj, dump_file) dump_file.close()
def _make_dump_directory(self): create_dir(self.dump_directory)
import os import sys sys.path.append(os.getcwd()) from base.dump.with_charge.DumpConfig import DumpConfigWTC from base.dump.with_charge.DumpUtil import DumpUtilIPWTC, DumpUtilWTC from common.configs.global_constants import run_mode, max_ev_count, max_gv_count, dump_directory from common.mode.RunMode import get_r_t_h_values, RunMode from common.util.common_util import create_dir if run_mode == RunMode.FULL: dump_util = DumpUtilWTC() else: dump_util = DumpUtilIPWTC() create_dir(dump_directory) for (r, t, h) in get_r_t_h_values(run_mode): e = max_ev_count g = min(max_gv_count, 5 * r - e) dump_config = DumpConfigWTC(e, g, r, t, h) dump_util.load_filtered_data(dump_config)
def run(self, dump_structure, args): cycle_count = int(args.cycle_count) start_prob = float(args.start_prob) end_prob = float(args.end_prob) swap_prob = float(args.swap_prob) swap_condition = 0 < swap_prob < 1 start_end_condition = 0 < end_prob < start_prob < 1 s_print("Simulated annealing configs: \ncycle Count: {}\nstart prob: {}\nend prob: {}\nswap prob: {}". format(args.cycle_count, args.start_prob, args.end_prob, args.swap_prob)) if not swap_condition or not start_end_condition: raise ValueError("inconsistent parameters") assignment = greedy_assign(dump_structure, AssignUtilConfig(do_print=True), args=args) energy_cost = assignment.total_energy_cost() self.min_assign = assignment self.min_cost = energy_cost temp_start = -1.0 / math.log(start_prob) temp_end = -1.0 / math.log(end_prob) rate_of_temp = (temp_end / temp_start) ** (1.0 / (cycle_count - 1.0)) selected_temp = temp_start delta_e_avg = 0.0 number_of_accepted = 1 prefix = "{}_{}_{}_".format(args.start_prob, args.end_prob, args.swap_prob) prefix = prefix.replace(".", "_") self.summary_file_name = summary_directory + prefix + "simulated_annealing.csv" create_dir(summary_directory) summary_file = FileWriter(self.summary_file_name) summary_file.write("iteration,energy_cost") summary_file.write([0, energy_cost]) for i in range(cycle_count): s_print('Cycle: {} with Temperature: {}'.format(str(i), str(selected_temp))) nn_assignment = nearest_neighbour(assignment, swap_prob) nn_energy_cost = nn_assignment.total_energy_cost() delta_e = abs(nn_energy_cost - energy_cost) if nn_energy_cost > energy_cost: if i == 0: delta_e_avg = delta_e denominator = (delta_e_avg * selected_temp) p = math.exp(-1 * math.inf) if denominator == 0 else math.exp(-delta_e / denominator) accept = True if random.random() < p else False else: accept = True # save current minimum to avoid losing details due to crash if self.min_cost > nn_energy_cost: self.min_assign = nn_assignment.copy() self.min_cost = nn_energy_cost nn_assignment.write("current_min") nn_assignment.write_bus_stat("current_min") if accept: assignment = nn_assignment energy_cost = nn_energy_cost summary_file.write([i, energy_cost]) delta_e_avg = delta_e_avg + (delta_e - delta_e_avg) / number_of_accepted number_of_accepted += 1 selected_temp = rate_of_temp * selected_temp summary_file.close() improve_perc = round(100.0 * (energy_cost - self.min_cost) / energy_cost, 3) s_print("Improvement in energy cost {}%".format(str(improve_perc)))