-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_generator.py
140 lines (123 loc) · 5.57 KB
/
input_generator.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
from PIL import Image, ImageDraw, ImageColor
from os import mkdir, listdir
regexp_float = r'-?[0-9]*[.,]?[0-9]+' # выражение выводит все целые числа и десятичные дроби в строке
rec_count = 0
library_counter = 0
SEP_LINE = '#######################'
# визуализация карты препятствий
picture_dir = 'pictures'
picture_width = 600
picture_height = 600
picture_counter = 1
def draw_picture(name, line_list, circles_list, scale=1000):
global picture_counter
image = Image.new('RGB', (picture_width, picture_height))
draw = ImageDraw.Draw(image)
# отрисовка четырехугольников
for idx, line in enumerate(line_list):
draw.line(tuple(
x * scale + picture_width / 2 if idx % 2 == 0 else picture_height - x * scale for idx, x in
enumerate(line)))
# отрисовка окружностей
for circ in circles_list:
center_x, center_y, radius = circ
x0, x1 = sorted(((center_x - radius) * scale + picture_width / 2,
(center_x + radius) * scale + picture_width / 2))
y0, y1 = sorted((picture_height - (center_y - radius) * scale,
picture_height - (center_y + radius) * scale))
draw.ellipse((x0, y0, x1, y1))
# отрисовка начала координат
draw.rectangle((picture_width / 2 - 3, picture_height - 3, picture_width / 2 + 3, picture_height),
fill=ImageColor.getrgb('yellow'))
if picture_dir not in listdir():
mkdir(picture_dir)
image.save(f'{picture_dir}\\{name}_{picture_counter}.png', 'PNG')
picture_counter += 1
# генератор float от start до stop включительно
def float_range(start, stop, step):
while round(start, 4) < round(stop, 4):
yield float(start)
start += round(step, 4)
def quads_handler():
res_list = list()
for quad in range(n_quads):
for point in range(3):
res_list.append(
(round(eval(quads[8 * quad + point * 2]), 4),
round(eval(quads[8 * quad + point * 2 + 1]), 4),
round(eval(quads[8 * quad + point * 2 + 2]), 4),
round(eval(quads[8 * quad + point * 2 + 3]), 4)))
res_list.append((round(eval(quads[8 * (quad + 1) - 2]), 4),
round(eval(quads[8 * (quad + 1) - 1]), 4),
round(eval(quads[8 * quad + 0]), 4),
round(eval(quads[8 * quad + 1]), 4)))
return res_list
def circles_handler():
res_list = list()
for circle in range(n_circle):
res_list.append((round(eval(circles[3 * circle + 0]), 4),
round(eval(circles[3 * circle + 1]), 4),
round(eval(circles[3 * circle + 2]), 4)))
return res_list
# функция разбивающая фигуры на примитивы
def rec_fun():
global rec_count, n_params, library_counter
rec_count += 1
for j in float_range(t_min[rec_count - 1], t_max[rec_count - 1] + t_step[rec_count - 1], t_step[rec_count - 1]):
t[rec_count] = round(j, 4)
if rec_count == n_params:
quad_list = quads_handler()
circle_list = circles_handler()
draw_picture('pic', quad_list, circle_list)
for line in quad_list:
res_file.write(f'{line[0]} {line[1]} {line[2]} {line[3]}\n')
if line[0] != line[2]: # Not horizontal
min_x, max_x = sorted([line[0], line[2]])
res_file.write(f'x\n{min_x} {max_x}\n')
else:
min_y, max_y = sorted([line[1], line[3]])
res_file.write(f'y\n{min_y} {max_y}\n')
for circ in circle_list:
x, y, r = circ
res_file.write(f'{x} {y} {r}\n')
res_file.write(f'0\n')
res_file.write(SEP_LINE + '\n')
# запись справочной информации в файл-библиотеку
library_counter += 1
library_file.write(f'Решение №{library_counter}\n')
for param in range(n_params):
library_file.write(f'{t_name[param]} = {t[param + 1]}\n')
else:
rec_fun()
rec_count -= 1
input_file = open('gen_input.txt', 'r')
name_of_output = input_file.readline()[:-1]
library_file = open(f'{name_of_output}_library.txt', 'w')
res_file = open('input_generated.txt', 'w')
n_params = int(input_file.readline())
t = [0. for x in range(n_params + 1)] #
t_min, t_max, t_step, t_name = list(), list(), list(), list()
for i in range(n_params):
string = input_file.readline()
subs = string.split()
t_min.append(float(subs[0])), t_step.append(float(subs[1])), t_max.append(float(subs[2])), t_name.append(subs[3])
n_quads, n_circle = [int(x) for x in input_file.readline().split()]
quads, circles = list(), list()
for i in range(1, n_quads + 1, 1):
for j in range(8):
quads.append(input_file.readline()[:-1])
for i in range(1, n_circle + 1, 1):
for j in range(3):
circles.append(input_file.readline()[:-1])
input_file.close()
# подсчет количества сгенерированных случаев
n_iter = 1
for i in range(n_params):
n_iter *= (t_max[i] - t_min[i]) / t_step[i] + 1.
n_iter = int(round(n_iter))
n_lines = 4 * n_quads
library_file.write(f'{name_of_output}\n{n_iter}\n{n_lines} {n_circle}\n')
res_file.write(f'{name_of_output}\n{n_iter}\n{n_lines} {n_circle}\n')
rec_fun()
res_file.close()
library_file.close()