在google ortools示例和指南中,我看到的是正常TSP的实现,以最大程度地减少旅行时间。如果我还有其他功能(例如票价)想要最大化,该怎么办?有什么办法可以实现这一目标。
我以矩阵的形式列出了所有城市组合的票价。
我希望我有一种方法可以使用此功能来计算如果我必须一次访问所有城市的最高票价。
def create_data_model():
final_data= pd.read_csv('C:\\Users\\dojha\\final_data.csv')
rate_city_matrix_df = pd.DataFrame()
rate_city_matrix_df['origin']= final_data['origin_city']
rate_city_matrix_df['destination']= final_data['destination_city']
rate_city_matrix_df['rate']= final_data['line_haul_rate']
rate_city_matrix = rate_city_matrix_df.groupby(['origin','destination']).sum().unstack('destination')
rate_city_matrix_array= rate_city_matrix.values[0:4,:]
all_nans= np.isnan(rate_city_matrix_array)
rate_city_matrix_array[all_nans]=0
data={}
data['rate_matrix']=rate_city_matrix_array
data['num_vehicles']=1
data['depot']=0
return data
def print_solution(manager, routing, assignment):
"""Prints assignment on console."""
print('Objective: {} dollars'.format(assignment.ObjectiveValue()))
index = routing.Start(0)
plan_output = 'Route for vehicle 0:\n'
route_distance = 0
while not routing.IsEnd(index):
plan_output += ' {} ->'.format(manager.IndexToNode(index))
previous_index = index
index = assignment.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
plan_output += ' {}\n'.format(manager.IndexToNode(index))
print(plan_output)
plan_output += 'Route distance: {}miles\n'.format(route_distance)
def main():
#entry point for the program
data= create_data_model()
#create the routing index manager
manager= pywrapcp.RoutingIndexManager(
len(data['rate_matrix']), data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['rate_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# Solve the problem.
assignment = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if assignment:
print_solution(manager, routing, assignment)
if __name__ == '__main__':
main()
线路速率为负值