-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_parser.jl
More file actions
96 lines (82 loc) · 3.22 KB
/
Copy pathdata_parser.jl
File metadata and controls
96 lines (82 loc) · 3.22 KB
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
### Data Parser ###
# Data Read: Reads the structured data from the given instance file
# Input: - filename: A string containing the file name with its relative path.
# Output: - n: An Integer containing the number of tasks;
# - tasks_times: An (n) Array of Integers with each task's time;
# - prec_relations: A (n,n) 2-dimensional Binary Array representing
# each task's Precedence Relation.
function parser_data_read(filename::String)
# Gets every line in the instance file for processing
lines = []
open(filename,"r") do f
lines = readlines(filename)
end
# The number n of tasks is given in the first line
n = parse(Int64, lines[1])
# Gets each task's time in the next n lines
task_times = Int64[]
for i = 2:(n+1)
append!(task_times, parse(Int64, lines[i]))
end
# Gets the i,j Direct Precedence Relations until the "-1,-1" end mark
# and transforms it into a Binary Matrix of the Relations
prec_relations = zeros(Int64, (n,n))
curr_line = n+2
# Julia has no "Do While" so a first verification is necessary to avoid a break
rs = split(lines[curr_line], ",")
r = [parse(Int64, rs[1]), parse(Int64, rs[2])]
while (r[1] > 0)
prec_relations[r[1], r[2]] = 1
curr_line += 1
rs = split(lines[curr_line], ",")
r = [parse(Int64, rs[1]), parse(Int64, rs[2])]
end
println("Data successfully read from file \"", filename, "\" containing ", n, " tasks.")
println("")
# Returns the structured data
return n, task_times, prec_relations
end
# Parser Write: Parses the Data from the given Instance to fit GLPK pattern.
# Input: - filename: A string with the name for the parser file that will be written;
# - n, s, task_times, times_sum, prec_relations: Needed to write the Data.
# Output: None.
function parser_write(filename::String, n, s, task_times, prec_relations)
open(filename, "w") do f
print(f, "data;\r\n")
print(f, "\r\n")
print(f, "param n := ", n, ";\r\n")
print(f, "param m := ", s, ";\r\n")
print(f, "param times := ")
for i = 1:n
print(f, i, " ", task_times[i], "\r\n")
end
print(f, ";\r\n")
print(f, "precedences : ")
for i = 1:n
print(f, i, " ")
end
print(f, ":=\r\n")
for i = 1:n
prec_line = string(prec_relations[i, :])
prec_line = replace(prec_line, ",", "")
prec_line = replace(prec_line, "[", "")
prec_line = replace(prec_line, "]", "")
print(f, i, " ", prec_line, "\r\n")
end
print(f, ";\r\n")
end
println("Solution saved in file: ", filename)
println("")
end
### Main Function ###
function main()
s = 3
filename = ["instances/HAHN.IN2", "instances/LUTZ3.IN2", "instances/WEE-MAG.IN2"]
out_file = ["instances/HAHN.DAT", "instances/LUTZ3.DAT", "instances/WEE-MAG.DAT"]
for i = 1:(length(filename))
n, task_times, prec_relations = parser_data_read(filename[i])
parser_write(out_file[i], n, s, task_times, prec_relations)
end
println("Parsing finished.")
end
main()