Skip to content
Snippets Groups Projects
Commit 7a21017a authored by Hans-Jörg's avatar Hans-Jörg
Browse files

Normalize CSV file usage: delimiter, header

- Use ; as a delimiter for parsing CSV results
- Use a header when writing and parsing CSV schedules
parent 5c4985c4
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import pulp
import csv
from schedgen.opt.helper import message
def read_from_file(csvfile):
"""
Read a schedule from a CSV file.
The file should have a header and columns are separated by ";".
The two columns are "strategy" for the strategy to execute and "time"
for the timeout (floating point in seconds).
"""
schedule = []
reader = csv.DictReader(csvfile, delimiter=";")
for row in reader:
time = float(row["time"].strip())
strategy = row["strategy"]
schedule.append((time, strategy))
return schedule
def prune_unsolved(frame, timeout, epsilon=0.0):
"""
Prune unsolved problems: problems solved by no strategy
......
......@@ -18,7 +18,7 @@ class CSVResult:
results = {}
with open(data_path, newline="") as csvfile:
reader = csv.DictReader(csvfile, skipinitialspace=True)
reader = csv.DictReader(csvfile, skipinitialspace=True, delimiter=";")
for row in reader:
strategy = row[self.__strategy_column].strip()
result = row[self.__solved_column].strip().lower()
......
......@@ -2,7 +2,12 @@
import argparse
from schedgen.opt.schedule import OptimizeSchedule, prune_trivial, prune_unsolved
from schedgen.opt.schedule import (
OptimizeSchedule,
prune_trivial,
prune_unsolved,
read_from_file,
)
from schedgen.opt.order import OptimizeOrder
from schedgen.opt.helper import message, file_or_dir_path, new_file_path, file_path
from schedgen.parsers.gridtpt import GridTPT
......@@ -149,7 +154,8 @@ def writeScript(outFile, logics, schedule, seconds=False):
def writeCSV(outFile, logics, schedule):
message(f"Writing CSV schedule to {outFile}.", logics)
with open(outFile, "w") as af:
with open(outFile, "w") as af:,
af.write("time;strategy\n")
for i in range(len(schedule)):
(t, e) = schedule[i]
af.write(f"{t:.3f};{e}\n")
......@@ -162,12 +168,6 @@ def writeSchedule(outFile, logics, full, schedule, seconds=False):
writeCSV(outFile, logics, schedule)
def readPreSchedule(preScheduleFile):
with open(preScheduleFile, "r") as inFile:
splits = [line.split(";") for line in inFile]
return [(float(s[0].strip()), s[1].strip()) for s in splits]
def mergeSchedules(logics, schedule1, schedule2):
for i in range(len(schedule1) - 1, -1, -1):
(t1, e1) = schedule1[i]
......@@ -175,7 +175,10 @@ def mergeSchedules(logics, schedule1, schedule2):
(t2, e2) = schedule2[j]
if e1 == e2:
if t2 < t1:
message("Merging error, later scheduler has strategy for shorter timeout!", logics)
message(
"Merging error, later scheduler has strategy for shorter timeout!",
logics,
)
exit(1)
del schedule1[i]
del schedule2[j]
......@@ -223,7 +226,7 @@ if __name__ == "__main__":
pre_schedule = None
if args.pre_schedule:
pre_schedule = readPreSchedule(args.pre_schedule)
pre_schedule = read_from_file(args.pre_schedule)
pre_schedule_timeout = args.pre_schedule_time
if pre_schedule_timeout == 0.0:
message("Error: no pre-schedule timeout given.", logics)
......@@ -250,7 +253,9 @@ if __name__ == "__main__":
message("No solvable benchmarks!.", logics)
if pre_schedule and args.result:
message("Writing pre-schedule.", logics)
writeSchedule(args.result, logics, args.full, pre_schedule, args.seconds_out)
writeSchedule(
args.result, logics, args.full, pre_schedule, args.seconds_out
)
exit(0)
exit(1)
......
benchmark, logic, strategy, solved, time
UF/a, UF, -test1, 1, 1.0
UF/b, UF, -test1, yes, 1.0
LRA/go, LRA, -test1, true, 2.0
UF/c, UF, -test1, no, --
UF/d, UF, -test1, no, --
UF/e, UF, -test1, yes, 0.9
UF/a, UF, -test2, no, --
UF/b, UF, -test2, yes, 5.0
UF/c, UF, -test2, yes, 5.0
UF/d, UF, -test2, no, --
UF/e, UF, -test2, no, 0.5
UF/d, UF, -test2, no, --
UF/a, UF, -test3, no, 1.0
UF/b, UF, -test3, yes, 1.0
UF/c, UF, -test3, no, NA
UF/d, UF, -test3, no, --
UF/e, UF, -test3, yes, 0.9
UF/d, UF, -test2, no, --
benchmark; logic; strategy; solved; time
UF/a; UF; -test1; 1; 1.0
UF/b; UF; -test1; yes; 1.0
LRA/go; LRA; -test1; true; 2.0
UF/c; UF; -test1; no; --
UF/d; UF; -test1; no; --
UF/e; UF; -test1; yes; 0.9
UF/a; UF; -test2; no; --
UF/b; UF; -test2; yes; 5.0
UF/c; UF; -test2; yes; 5.0
UF/d; UF; -test2; no; --
UF/e; UF; -test2; no; 0.5
UF/d; UF; -test2; no; --
UF/a; UF; -test3; no; 1.0
UF/b; UF; -test3; yes; 1.0
UF/c; UF; -test3; no; NA
UF/d; UF; -test3; no; --
UF/e; UF; -test3; yes; 0.9
UF/d; UF; -test2; no; --
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment