-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi1.py
40 lines (32 loc) · 1.43 KB
/
i1.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
import random
from hfunctions import HelpfulFunctions
class I1:
def generate_solution(cities, solution_heur):
cities_copy = cities[:]
solution = []
random_index = random.randint(0, len(cities_copy)-1)
solution.append(cities_copy[random_index])
cities_copy.pop(random_index)
random_index = random.randint(0, len(cities_copy)-1)
solution.append(cities_copy[random_index])
cities_copy.pop(random_index)
counter = 2
while counter < solution_heur:
min_p_d = 100000
min_p_i = -1
min_p_g = -1
for i in range(len(solution)):
for j in range(len(cities_copy)):
d = HelpfulFunctions.distance(solution[i][1], cities_copy[j][1], solution[i][2], cities_copy[j][2]) + HelpfulFunctions.distance(solution[i-1][1], cities_copy[j][1], solution[i-1][2], cities_copy[j][2])
if d < min_p_d:
min_p_d = d
min_p_i = i
min_p_g = j
solution.insert(min_p_i, cities_copy[min_p_g])
cities_copy.pop(min_p_g)
counter += 1
while len(cities_copy) > 0:
random_index = random.randint(0, len(cities_copy) - 1)
solution.append(cities_copy[random_index])
cities_copy.pop(random_index)
return solution