-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsvGen.py
164 lines (127 loc) · 6.93 KB
/
csvGen.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import csv
import os
import datetime
import common
import chargePorts
# --------- Exporting to CSV -----------
# every time an alrogithm is run, it will create csv files for vehicles and chargePorts
# files will be save in /csv/<algorithm type>/timeStamp/
# NOTE: folderName must be a String of one of our algorihtm names: "fcfs" , "edf" , or "llfSmart" , "llfSimple"
def generateCSV( folderName ):
if common.csvOn:
global vehiclePath
global vehicleCSV
global timeStamp
# generate a unique fipame with a time stampg
timeStamp = datetime.datetime.now().strftime( "%H-%M-%S%f-%m-%d-%Y" )
# thank stack overflow for making this easy
# setup file to save in a directory
script_dir = os.path.dirname( os.path.abspath(__file__) )
dest_dir = os.path.join( script_dir, 'csv', folderName , timeStamp )
try:
os.makedirs(dest_dir)
except OSError:
pass # already exists
# make a CSV for both Vehicle and ChargePorts
vehiclePath = os.path.join( dest_dir, "vehicles.csv" )
# and now write them up
vehicleCSV = csv.writer( open( vehiclePath , "wb" ) )
# write some basic info info in vehicleCSV
# basic stats
vehicleCSV.writerow( [ "Interval time" , common.interval , "Number of vehicles" , common.numberOfVehiclesInSimulation ] )
# initialize some columns
vehicleCSV.writerow( [ "Vehicle ID" , \
"Status" , \
"Arrival Time" , \
"Departure Time" , \
"Initial Charge" , \
"Current Charge" , \
"Charge Rate" , \
"Charge Level Needed" , \
"Max Capacity" , \
"Charge Time Needed" , \
"Original Free Time" , \
"Original Total Time" , \
"Original Laxity" \
] )
# when a vehicle is leaving a lot, throw it into the CSV so we can study it
def exportVehicleToCSV( vehicle, status ):
if common.csvOn:
global vehiclePath
global vehicleCSV
vehicleCSV.writerow( [ vehicle.id , \
status , \
vehicle.arrivalTime , \
vehicle.depTime , \
vehicle.initialCharge , \
vehicle.currentCharge , \
vehicle.chargeRate , \
vehicle.chargeNeeded , \
vehicle.maxCapacity , \
vehicle.timeToCharge , \
vehicle.freeTime , \
vehicle.totalTime , \
vehicle.originalLaxity \
] )
def exportChargePortsToCSV( folderName ):
if common.csvOn:
global chargePortCSV
global chargePortPath
global timeStamp
# should already be built
# timeStamp = datetime.datetime.now().strftime( "%Y%m%d-%H%M%S" )
# make and write a CSV file for the logs of each chargePort
for index, chargePort in enumerate( chargePorts.chargePortListeners ):
# thank stack overflow for making this easy
# setup file to save in a directory
script_dir = os.path.dirname( os.path.abspath( __file__ ) )
dest_dir = os.path.join( script_dir, 'csv' , folderName , timeStamp , 'chargePorts' )
try:
os.makedirs(dest_dir)
except OSError:
pass # already exists
# make file name and reference path
fileName = 'port' + str( index ) + '.csv'
# print 'fileName: ' , fileName
portPath = os.path.join( dest_dir, fileName )
# write the file
portCSV = csv.writer( open( portPath , "wb" ) )
# basic stats
portCSV.writerow( [ "Interval time" , common.interval , "Number of charge ports" , chargePorts.numChargePorts ] )
# initialize some columns for stuff
# the duplicate Vehicle ID is a parity check. If they don't match, shit broke
portCSV.writerow( [ "ChargePort Instance ID" , \
"Start Time" , \
"End Time" , \
"Time Charging" , \
"Initial Charge" , \
"Charge Requested" , \
"Vehicle ID" , \
"Vehicle ID" , \
"Final Charge" \
] )
# iterate through the chargePort and write it all out
for index, chargeEvent in enumerate( chargePort ):
# csvPrep does all the labor
portCSV.writerow( chargeEvent.csvPrep() )
def exportSimulationDataToCSV( simulationData , title ):
# generate a unique fipame with a time stamp
timeStamp = datetime.datetime.now().strftime( "%H-%M-%S%f-%m-%d-%Y" )
# thank stack overflow for making this easy
# setup file to save in a directory
script_dir = os.path.dirname( os.path.abspath(__file__) )
dest_dir = os.path.join( script_dir, 'csv', 'simulations' )
try:
os.makedirs(dest_dir)
except OSError:
pass # already exists
# make a CSV of it all
csvPath = os.path.join( dest_dir, title + timeStamp + '.csv' )
# and now write them up
writeCSV = csv.writer( open( csvPath , "wb" ) )
# write some basic info info in vehicleCSV
# columns
writeCSV.writerow( [ "Arrival Rate" , "FCFS" , "FCFS-AC" , "EDF" , "EDF-AC-Basic" , "EDF-AC-Pro" , "LLFsimple" , "LLFsimple-AC-Basic" , "LLFsimple-AC-Pro" , "LLFsmart" , "LLFsmart-AC" , "DSAC" ] )
# write each row
for index, simulationRound in enumerate( simulationData ):
writeCSV.writerow( simulationData[ index ] )