-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·157 lines (120 loc) · 4.68 KB
/
main.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
import numpy as np
import pandas as pd
import sys
import math
import random
## Ideas
# Priorizar coches que tengan minima suma de costes de calle
# Propuesta de Angelito
# { "calle-a": [duracion_calle = 3, duracion_semaforo_fin_calle = 1] }
# Propuesta de Dautito
# { "intersection_id": [(in_street_name, green_seconds)] }
# Propuesta del impostor
# [ time_0: [intersection_0_semaphore_green_id, intersection_1_semaphore_green_id, ... ],
# time_1: [intersection_0_semaphore_green_id, intersection_1_semaphore_green_id, ... ],
# ... ]
def write_solution(solution, output_file_path):
"""
3
1
2
rue-d-athenes 2
rue-d-amsterdam 1
0
1
rue-de-londres 2
2
1
rue-de-moscou 1
"""
with open(output_file_path, 'w') as writer:
count = 0
temp = ""
for intersection_id, in_streets in solution.items():
if len(in_streets) > 0:
count += 1
temp += str(intersection_id) + "\n"
temp += str(len(in_streets)) + "\n"
for street_name, green_seconds in in_streets:
temp += str(street_name) + ' ' + str(green_seconds) + '\n'
writer.write(str(count) + "\n")
writer.write(temp)
#
# solution = {
# time = D
# }
def get_problem_statement_from_file(file_path):
"""
6 4 5 2 1000
2 0 rue-de-londres 1
0 1 rue-d-amsterdam 1
3 1 rue-d-athenes 1
2 3 rue-de-rome 2
1 2 rue-de-moscou 3
4 rue-de-londres rue-d-amsterdam rue-de-moscou rue-de-rome
3 rue-d-athenes rue-de-moscou rue-de-londres
"""
statement = {}
with open(file_path, 'r') as reader:
[D, I, S, V, F] = [int(x) for x in reader.readline().split()]
statement["info"] = [D, I, S, V, F]
statement["streets"] = {}
statement["intersections"] = {}
for street in range(S):
[inter_in, inter_out, street_name, street_duration] = reader.readline().split()
statement["streets"][street_name] = [int(street_duration), int(inter_in), int(inter_out), 0]
if inter_out not in statement["intersections"]:
statement["intersections"][inter_out] = []
# Añadimos la interseccion con 1, luego en generacion de solucion cambios ese numero
statement["intersections"][inter_out].append((street_name, 1))
# Aqui empieza demanda de la calle
statement["cars"] = {}
for raw_car in range(V):
raw_car_array = reader.readline().split()
car_id = int(raw_car_array[0])
car_streets = raw_car_array[1:]
for street in car_streets: #[:len(car_streets) // 2]:
statement["streets"][street][3] += 1 # Añadimos numero de veces que aparece esa calle
statement["cars"][car_id] = car_streets
# print(statement)
return statement
def get_naive_solution_from_statement(statement):
solution = {}
# dtype = [('street_duration', int), ('inter_in', int), ('inter_out', int), ('car_count', int)]
for inter_out, streets_raw in statement["intersections"].items():
# [0, [(street_name, 1), (street_name, 2)]]
number_of_streets = len(streets_raw)
streets = []
sum_street_values = 1
sum_street_duration = 1
for street_name, x in streets_raw:
street_info = statement["streets"][street_name] # ('street_duration', 'inter_in', 'inter_out', 'car_count')
street_value = street_info[3]
street_duration = street_info[0]
sum_street_values += street_value
sum_street_duration += street_duration
streets.append((street_name, street_value, sum_street_duration))
# Normalizar street_value
normalized_streets = []
for street_name, street_value, street_duration in streets:
normalized_value = (street_value / sum_street_values) # - (street_duration / sum_street_duration)
normalized_streets.append((street_name, max(normalized_value, 0), street_duration))
# Ordenar probando
normalized_streets = sorted(normalized_streets, key=lambda item: item[-1], reverse=True)
# print(sorted_streets)
solution[inter_out] = []
for street_index in range(len(normalized_streets)):
duration = math.ceil(normalized_streets[street_index][1] * 2)
duration = min(duration, statement["info"][0])
if duration > 0:
solution[inter_out].append((normalized_streets[street_index][0], int(duration * 1.3)))
# for street_index in range(len(normalized_streets)):
# duration =
# if duration > 0:
# solution[inter_out].append((normalized_streets[street_index][0], duration))
return solution
if __name__ == "__main__":
for code in range(ord('a'), ord('f') + 1):
statement = get_problem_statement_from_file('data/' + chr(code) + '.txt')
solution = get_naive_solution_from_statement(statement)
write_solution(solution, 'out/' + chr(code) + '.txt')