def modify_travel_time(self): """ Modifies the travel times to the cbd. The preferered zone (20) gets minimum travel times, all other zones get high travel times. @old version Modifies the travel times from zone to zone. For zone 20 the travel times to all other zones is set to min_travel_time. For all other zones the trvel time will be set on 31min if the origin travel time is less than 30min, otherwise it's not modified. """ # using default cbd cbd = 129 # set the preferred zone preferential_zone = 20 # set travel times for the preferered zone and other zones min_travel_time = '0.40' # time in minutes # max_travel_time = '121.02' # time in minutes travel_time_border = 30.0 # time in minutes offset = 2.0 # time in minutes logger.log_status("Preferential zone with travel time = 40msec is set to: %s" % preferential_zone) logger.log_status("The travel time border of other zones is set to %f" % travel_time_border) logger.log_status("Offset to travel time border is %f" % offset) travel_data = os.path.join( os.environ['OPUS_HOME'], "opus_matsim", "tmp", "travel_data.csv" ) if not self.travel_data_exsists(travel_data): raise StandardError('Travel data not found! %s' % travel_data) travel_cost_list = self.get_travel_cost_matrix() in_file = open(travel_data, 'r') str_list = [] # read header of travel data to get the indices of the colums (from_zone, to_zone, single_vehicle_travel_time) line = in_file.readline() # init indices get_indices = GetIndices(line) index_from_zone = get_indices.get_from_zone_index() index_to_zone = get_indices.get_to_zone_index() index_travel_times = get_indices.get_am_single_vehicle_to_work_travel_time_index() index_travel_costs = get_indices.get_single_vehicle_to_work_travel_cost_index() number_of_colums = get_indices.get_number_of_colums() # prepare header line for the output file row = line.split(',') str_list.append( row[index_from_zone].strip('\r\n') +','+ row[index_to_zone].strip('\r\n') +','+ row[index_travel_times].strip('\r\n') + ',' + row[index_travel_costs].strip('\r\n') +'\r\n') # get first line of the table content line = in_file.readline() # replaces the travel times as decribed above... while line: row = line.split(',') # consistency check if len(row) != number_of_colums: raise StandardError('Error in number of colums: %s' %row) from_zone_id = int(row[index_from_zone].strip('\r\n')) to_zone_id = int(row[index_to_zone].strip('\r\n')) travel_cost_value = travel_cost_list[from_zone_id][to_zone_id] travel_times_value = float(row[index_travel_times].strip('\r\n')) # new version (compute travel times from/to cbd. If preferential zone occurs set min travel times. # otherwise check if the travel times of other zones from/to cbd is lower than 30min than set it to travel times higher than 30min) # get all zones from and to cbd if( from_zone_id == cbd ) or (to_zone_id == cbd ): # if its the the preferaential zone and the cbd set minimum travel times if ((from_zone_id == preferential_zone) and (to_zone_id == cbd)) or\ ((from_zone_id == cbd) and (to_zone_id == preferential_zone)): row[index_travel_times] = min_travel_time # if its the cbd itself don't change anything elif (from_zone_id == cbd) and (to_zone_id == cbd): pass # if zone form/to cbd is other then the preferential zone set the travel times # higher than 30min (when the travel times are lower than 30min) elif ((from_zone_id != preferential_zone) and (to_zone_id == cbd)) or\ ((from_zone_id == cbd) and (to_zone_id != preferential_zone)): if travel_times_value <= travel_time_border: row[index_travel_times] = str(travel_time_border + offset) #old version (Set travel times from preferential zone to all other zone to min travel times. For all other zones set travel times # higher than 30min if they have less than 30min) ## travel time from zone 20 to other zone or from other zone to zone 20 is set to 40sec. #if (from_zone_id == preferential_zone) or (to_zone_id == preferential_zone): # row[index_travel_times] = min_travel_time # # travel time from/to zone other than 20 is set to 30min+offset if the travel time was originally lower than 30min #elif travel_times_value <= travel_time_border: # row[index_travel_times] = str(travel_time_border + offset) # append modified row to the new travel data content str_list.append( row[index_from_zone].strip('\r\n') +','+ row[index_to_zone].strip('\r\n') +','+ row[index_travel_times].strip('\r\n') + ',' + str(travel_cost_value) +'\r\n') line = in_file.readline() # finished modifying traval data in_file.close() # now write new travel data onto disc out_file = open(travel_data, 'w') logger.log_status("Copying modified travel data onto disc.") for row in str_list: out_file.write(row) out_file.close(); logger.log_status("Finished copy process.")
def modify_travel_costs(self): ''' Modifies the travel costs to the cbd. The preferered zone (20) gets low costs, all other zones get high costs. ''' # using default cbd cbd = '129' preferential_zone = '20' low_travel_cost = '3.47' high_travel_cost = '1689.19' logger.log_status("Set cbd to %s" % cbd) logger.log_status( "Preferential zone with travel cost = 3.47 is set to: %s" % preferential_zone) logger.log_status("The travel costs of other zones is set to %s" % high_travel_cost) travel_data = os.path.join(os.environ['OPUS_HOME'], "opus_matsim", "tmp", "travel_data.csv") if not self.travel_data_exsists(travel_data): raise StandardError('Travel data not found! %s' % travel_data) in_file = open(travel_data, 'r') str_list = [] # read header of travel data to get the indices of the colums (from_zone, to_zone, single_vehicle_travel_cost) line = in_file.readline() # init indices get_indices = GetIndices(line) index_from_zone = get_indices.get_from_zone_index() index_to_zone = get_indices.get_to_zone_index() index_travel_costs = get_indices.get_single_vehicle_to_work_travel_cost_index( ) number_of_colums = get_indices.get_number_of_colums() # prepare header line for the output file row = line.split(',') str_list.append(row[index_from_zone].strip('\r\n') + ',' + row[index_to_zone].strip('\r\n') + ',' + row[index_travel_costs].strip('\r\n') + '\r\n') # get first line of the table content line = in_file.readline() # replaces the travel costs for every occurence of cbd according to origin zone while line: row = line.split(",") # consistency check if len(row) != number_of_colums: print 'Error in number of colums: %s' % row sys.exit() # get all zones from and to cbd if (row[index_from_zone] == cbd) or (row[index_to_zone] == cbd): # if its the travel costs beween the preferaential zone and the cbd set low costs if ((row[index_from_zone] == preferential_zone) and (row[index_to_zone] == cbd)) or\ ((row[index_from_zone] == cbd) and (row[index_to_zone] == preferential_zone)): row[index_travel_costs] = low_travel_cost # append modified row to the new travel data content str_list.append(row[index_from_zone] + ',' + row[index_to_zone] + ',' + row[index_travel_costs] + '\n') # if its the cbd itself don't change anything elif (row[index_from_zone] == cbd) and (row[index_to_zone] == cbd): # append unmodified row to the new travel data content str_list.append(line) # if its another zone than the prefrerred zone or the cbd set high costs elif ((row[index_from_zone] != preferential_zone) and (row[index_to_zone] == cbd)) or\ ((row[index_from_zone] == cbd) and (row[index_to_zone] != preferential_zone)): row[index_travel_costs] = high_travel_cost # append modified row to the new travel data content str_list.append(row[index_from_zone] + ',' + row[index_to_zone] + ',' + row[index_travel_costs] + '\n') # otherwise don't change anything else: # append unmodified row to the new travel data content str_list.append(line) # get next line line = in_file.readline() # finished modifying traval data in_file.close() # now write new travel data ono disc out_file = open(travel_data, 'w') logger.log_status("Copying modified travel data onto disc.") for row in str_list: out_file.write(row) out_file.close() logger.log_status("Finished copy process.")
def modify_travel_costs(self): ''' Modifies the travel costs to the cbd. The preferered zone (20) gets low costs, all other zones get high costs. ''' # using default cbd cbd = '129' preferential_zone = '20' low_travel_cost = '3.47' high_travel_cost = '1689.19' logger.log_status("Set cbd to %s" %cbd) logger.log_status("Preferential zone with travel cost = 3.47 is set to: %s" % preferential_zone) logger.log_status("The travel costs of other zones is set to %s" % high_travel_cost) travel_data = os.path.join( os.environ['OPUS_HOME'], "opus_matsim", "tmp", "travel_data.csv" ) if not self.travel_data_exsists(travel_data): raise StandardError('Travel data not found! %s' % travel_data) in_file = open(travel_data, 'r') str_list = [] # read header of travel data to get the indices of the colums (from_zone, to_zone, single_vehicle_travel_cost) line = in_file.readline() # init indices get_indices = GetIndices(line) index_from_zone = get_indices.get_from_zone_index() index_to_zone = get_indices.get_to_zone_index() index_travel_costs = get_indices.get_single_vehicle_to_work_travel_cost_index() number_of_colums = get_indices.get_number_of_colums() # prepare header line for the output file row = line.split(',') str_list.append( row[index_from_zone].strip('\r\n') +','+ row[index_to_zone].strip('\r\n') +','+ row[index_travel_costs].strip('\r\n') +'\r\n') # get first line of the table content line = in_file.readline() # replaces the travel costs for every occurence of cbd according to origin zone while line: row = line.split(",") # consistency check if len(row) != number_of_colums: print 'Error in number of colums: %s' %row sys.exit() # get all zones from and to cbd if (row[index_from_zone] == cbd) or (row[index_to_zone] == cbd): # if its the travel costs beween the preferaential zone and the cbd set low costs if ((row[index_from_zone] == preferential_zone) and (row[index_to_zone] == cbd)) or\ ((row[index_from_zone] == cbd) and (row[index_to_zone] == preferential_zone)): row[index_travel_costs] = low_travel_cost # append modified row to the new travel data content str_list.append( row[index_from_zone] +','+ row[index_to_zone] +','+ row[index_travel_costs] +'\n') # if its the cbd itself don't change anything elif (row[index_from_zone] == cbd) and (row[index_to_zone] == cbd): # append unmodified row to the new travel data content str_list.append( line ) # if its another zone than the prefrerred zone or the cbd set high costs elif ((row[index_from_zone] != preferential_zone) and (row[index_to_zone] == cbd)) or\ ((row[index_from_zone] == cbd) and (row[index_to_zone] != preferential_zone)): row[index_travel_costs] = high_travel_cost # append modified row to the new travel data content str_list.append( row[index_from_zone] +','+ row[index_to_zone] +','+ row[index_travel_costs] +'\n') # otherwise don't change anything else: # append unmodified row to the new travel data content str_list.append( line ) # get next line line = in_file.readline() # finished modifying traval data in_file.close() # now write new travel data ono disc out_file = open(travel_data, 'w') logger.log_status("Copying modified travel data onto disc.") for row in str_list: out_file.write(row) out_file.close(); logger.log_status("Finished copy process.")