-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoOpt.py
40 lines (32 loc) · 1.56 KB
/
TwoOpt.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 numpy as np
from Species import Species
class TwoOpt:
def __init__(self) -> None:
pass
def twoOptTour(self, org: Species) -> None:
current_fitness, _ = org.calc_fitness(org.tour, org.knapsack)
for i in range(52):
for k in range(i, 52):
for j in range(k-i+1):
org.tour[i+j], org.tour[k-j] = org.tour[k-j], org.tour[i+j]
tmp_fitness, _ = org.calc_fitness(org.tour, org.knapsack)
if current_fitness > tmp_fitness:
current_fitness = tmp_fitness
else:
org.tour[k-j], org.tour[i+j] = org.tour[i+j], org.tour[k-j]
org.calc_fitness()
def twoOptKS(self, org: Species) -> None:
_, current_fitness = org.calc_fitness(org.tour, org.knapsack)
for i in range(52):
for k in range(i, 52):
for j in range(k-i+1):
org.knapsack[i+j], org.knapsack[k-j] = org.knapsack[k-j], org.knapsack[i+j]
tmp_fitness, _ = org.calc_fitness(org.tour, org.knapsack)
reverse = True
if org.instKS.is_valid(org.knapsack):
if current_fitness < tmp_fitness:
current_fitness = tmp_fitness
reverse = False
if reverse:
org.knapsack[k-j], org.knapsack[i+j] = org.knapsack[i+j], org.knapsack[k-j]
org.calc_fitness()