-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_generation.py
More file actions
138 lines (114 loc) · 4 KB
/
Copy pathdata_generation.py
File metadata and controls
138 lines (114 loc) · 4 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
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
import random
import struct
RECORD_COUNT = 180
TAPE_COUNT = 2
PAGE_SIZE = 10
RECORD_SIZE = struct.calcsize('<dd')
def calculate_distance(record):
if record is None:
return 0
return (record['x'] ** 2 + record['y'] ** 2) ** 0.5
def fibonacci_pair(n):
a, b, c = 1, 1, 1
while n > (a + b):
a, b = b, a + b
c += 1
return a, b, c
def generate_record():
record = {
"x": random.randint(1, 1000) / 100.0,
"y": random.randint(1, 1000) / 100.0
# "x": random.randint(0, 4),
# "y": 0
}
return record
def generate_data():
records = []
for _ in range(RECORD_COUNT):
records.append(generate_record())
phase_count, dummy_run_count = split_records_into_tapes(records)
return dummy_run_count, records, phase_count
def handle_data_from_text_file(filename):
records = []
with open(f"{filename}", "r") as f:
for line in f:
line = line.strip()
if line == "":
continue
x, y = map(float, line.split(','))
records.append({"x": x, "y": y})
phase_count, dummy_run_count = split_records_into_tapes(records)
return dummy_run_count, records, phase_count
def handle_data_from_bin_file(filename):
records = []
with open(f"{filename}", "rb") as f:
while True:
chunk = f.read(RECORD_SIZE)
if not chunk or len(chunk) < RECORD_SIZE:
break
x, y = struct.unpack('<dd', chunk)
records.append({"x": x, "y": y})
phase_count, dummy_run_count = split_records_into_tapes(records)
return dummy_run_count, records, phase_count
def get_run_count(runs):
if not runs:
return 0
count = 1
prev_record = runs[0]
for record in runs[1:]:
if calculate_distance(record) < calculate_distance(prev_record):
count += 1
prev_record = record
return count
def split_records_into_tapes(records):
def write_records_to_tape(tape_number, records_arr, records_count):
for _ in range(records_count):
if not records_arr:
break
record = records_arr.pop(0)
tape_number.write(struct.pack('<dd', record['x'], record['y']))
prev_record = record
while records_arr:
record = records_arr[0]
if calculate_distance(record) >= calculate_distance(prev_record):
tape_number.write(struct.pack('<dd', record['x'], record['y']))
prev_record = record
records_arr.pop(0)
else:
break
run_count = get_run_count(records)
a, b, c = fibonacci_pair(run_count)
dummy_records_count = (a + b) - run_count
print("t1: ", b, " t2: ", a, " dummy: ", dummy_records_count)
with open("tape_1.bin", "wb") as tape1, open("tape_2.bin", "wb") as tape2:
write_records_to_tape(tape2, records, a)
write_records_to_tape(tape1, records, b)
return c, dummy_records_count
def read_manual_input():
print(f"Input the data (x y)")
records = []
while True:
line = input()
if line.strip() == "":
break
x, y = line.strip().split(',')
records.append({"x": float(x), "y": float(y)})
with open(f"input.txt", "w") as f:
for record in records:
f.write(f"{record['x']},{record['y']}\n")
def generate_archive(parameter, tapes):
with open(f"./archive/archive_{parameter}.txt", "w+") as archive:
for tape in tapes:
tape.file.flush()
pos = tape.file.tell()
tape.file.seek(0)
archive.write(f"--- {tape.filename} ---\n")
while True:
chunk = tape.file.read(RECORD_SIZE)
if not chunk or len(chunk) < RECORD_SIZE:
break
x, y = struct.unpack('<dd', chunk)
archive.write(f"{x},{y}\n")
archive.write("\n")
tape.file.seek(pos)
archive.close()