-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainControl.py
76 lines (65 loc) · 2.32 KB
/
mainControl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# ===========================================================================
# Author: Max ZIMMER
# Project: multi-commodity-flows-over-time 2020
# File: mainControl.py
# Description: Script to be called from Terminal
# ===========================================================================
from multiFlowClass import MultiFlow
from argparse import ArgumentParser
import pickle
import sys
import os
import timeit
def load_graph(path):
"""
Load graph instance from '.cg' file
:param path: Path to load graph from
:return: returns networkx graph instance
"""
with open(path, 'rb') as f:
network = pickle.load(f)
return network
def read_files(networkFile, inflowFile):
"""
Reads the files and initiates MultiFlow instance
:param networkFile: networkx graph
:param inflowFile: File containing commodities
:return: MultiFlow object
"""
network = load_graph(networkFile)
mf = MultiFlow(network)
with open(inflowFile, 'r') as fRead:
firstLine = True
for line in fRead:
if firstLine:
firstLine = False
else:
line = line.strip()
rate, interval, path = line.split()
startTime, endTime = interval.split(",")
path = tuple(path.split(","))
mf.add_commodity(path, float(startTime), float(endTime), float(rate))
rc = mf.validate_input()
if rc != 0:
# Return code is error message
sys.exit(rc)
return mf
# Parser configuration
parser = ArgumentParser(description='Compute multi-commodity Flows over Time.')
parser.add_argument("networkFile", help="Network in .cg-Format.")
parser.add_argument("inflowFile", help="Inflow .txt File containing the commodities.")
args = parser.parse_args()
# Positional arguments
networkFile = args.networkFile
inflowFile = args.inflowFile
# Main
mf = read_files(networkFile, inflowFile)
print("Starting computation.\n")
mf.compute()
baseName = os.path.basename(networkFile)
baseName = baseName[:baseName.rfind(".")]
print("Generating output.\n")
startTime = timeit.default_timer()
mf.generate_output(os.path.dirname(os.path.abspath(networkFile)), baseName)
endTime = timeit.default_timer()
print("Finished generating output. Total elapsed: {0:.2f} seconds".format(endTime - startTime))