From 9ac1b21b42571c0e8e3dee9619931a670f1a3c10 Mon Sep 17 00:00:00 2001 From: Mitsuo Tokumori Date: Fri, 6 May 2022 03:11:18 -0500 Subject: Refactor some functions out of example1.py --- readerOPP.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 readerOPP.py (limited to 'readerOPP.py') diff --git a/readerOPP.py b/readerOPP.py new file mode 100644 index 0000000..ccd213e --- /dev/null +++ b/readerOPP.py @@ -0,0 +1,75 @@ +""" +Functions to read input data from files +""" +import csv +from nodeOPP import Node + + +def read_nodes(depot_num): + """ + Read nodes from input file. + + Attributes + ---------- + depot_num : int + Number of depots + + Returns + ------- + list + List of Nodes. The first `depot_num` nodes are the depots. + """ + depots = [] + no_depots = [] + with open('odiparpack/inf226.oficinas_mod.csv', newline='') as csvfile: + orders = csv.reader(csvfile) + count = 1 + index_customer = depot_num + index_depot = 0 + for row in orders: + if count == 1: + count += 1 + continue + + ubigeo, dept, prov, lat, lon, region_natural, is_depot = row[:7] + demand, ready_time, due_time, service_time = row[7:] + + n = Node( + -1, ubigeo, float(lat), float(lon), + int(is_depot), + demand=float(demand), ready_time=0, + due_time=float(due_time), service_time=60 + ) + + if n.is_depot: + n.index = index_depot + depots.append(n) + index_depot += 1 + else: + n.index = index_customer + no_depots.append(n) + index_customer += 1 + + count += 1 + + return depots + no_depots + + +def read_tramos(): + """ + Lee archivo de tramos + + Returns + ------- + list + Lista de tuplas con los tramos (ubigeo1, ubigeo2) + """ + tramos = [] + with open('odiparpack/inf226.tramos.v.2.0.csv', newline='') as f: + rows = csv.reader(f) + count = 1 + for row in rows: + if count >= 2: + tramos.append([row[0], row[1]]) + count += 1 + return tramos -- cgit v1.2.3