#hide
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

"""
Explanation: "Visualising Vehicle Routing Problem Solutions"

"Visualise paths of the multi-depot split-delivery vehicle routing problem with inventory constraints and heterogeneous fleet."
- toc: false
- badges: true
- comments: true
- categories: [Operations Research, Python]
End of explanation
"""


def import_instance_input(fn):
    depots = []
    customers = []

    with open(fn) as f:
        data = f.readlines()
        curr_line = 0

        # first line contains number of depots, types of vehicles and number of customers
        problem_info = data[curr_line].split()
        n_type_vehicles = int(problem_info[0])
        n_depots = int(problem_info[1])
        n_customers = int(problem_info[2])

        # process vehicle information
        curr_line = 2
        for idx in range(0, n_type_vehicles):
            curr_line += 1

        # skip empty line
        curr_line += 1

        # process depots
        for idx in range(0, n_depots):
            depot_info = data[curr_line].split()
            depots.append({
                'id': int(depot_info[0]),
                'x_coord': float(depot_info[1]),
                'y_coord': float(depot_info[2]),
                'capacity': int(depot_info[3])
            })

            curr_line += 1

        # skip empty line
        curr_line += 1

        # process customers
        for idx in range(0, n_customers):
            customer_info = data[curr_line].split()
            customers.append({
                'id': int(customer_info[0]),
                'x_coord': float(customer_info[1]),
                'y_coord': float(customer_info[2]),
                'demand': int(customer_info[3]),
                'processing_time': float(customer_info[4])
            })

            curr_line += 1
    
    return (depots, customers)

depots, customers = import_instance_input("../src/data/instanceA_8_input.txt")

"""
Explanation: Import routing problem
End of explanation
"""


def import_instance_output(fn):
    trips = []
    with open(fn) as f:
        data = f.readlines()

        # first line contains objective value
        obj_val = float(data[0])

        # first empty line and then all trips
        for line_idx in range(2, len(data)):
            line = data[line_idx].split()

            # if vehicle goes on trip, add customers
            customers = []
            customer_idx = 8
            while customer_idx < len(line):
                customers.append({
                    'id': line[customer_idx],
                    'load': line[customer_idx+1]
                })
                customer_idx += 2

            trips.append({
                'depot_id': int(line[0]),
                'vehicle_type': int(line[1]),
                'travel_cost': float(line[2]),
                'total_cost': float(line[3]),
                'load': int(line[4]),
                'processing_time': float(line[5]),
                'travel_time': float(line[6]),
                'total_time': float(line[7]),
                'customers': customers
            })
            
    return trips

trips = import_instance_output("../src/data/instanceA_8_output.txt")

"""
Explanation: Import solution
End of explanation
"""


df_depots = pd.DataFrame(depots)
df_customers = pd.DataFrame(customers)

# plot depots
plt.plot(df_depots.x_coord.values, df_depots.y_coord.values, ls="", marker="o", label="Depot")

# plot customers
plt.plot(df_customers.x_coord.values, df_customers.y_coord.values, ls="", marker=".", label="Customers")

# plot trips



# create legend and plot
plt.legend()
plt.show()

trips

"""
Explanation: Plot solution
End of explanation
"""


