-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGapPath.py
57 lines (44 loc) · 1.36 KB
/
GapPath.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
import logging
from LeadingOne import *
class GapPath(LeadingOne):
def __init__(self, n):
n=int(n)
self._n=n
self.current_solution=[0]*n
self.current_solution[0]=1
print("initial bitstring is:", self.current_solution)
@staticmethod
def ridge(solution):
last_1_pos=-1
count_1=solution.count(1)
for i in range(len(solution)):
if solution[i]==1:
last_1_pos=i
else:
break
logging.debug("solution:{} last_1_pos:{}, count_1:{}".
format(solution, last_1_pos, count_1))
if last_1_pos+1==count_1:
return last_1_pos+1
else:
return 0
def goal(self, solution):
count_0=solution.count(0)
rid=self.ridge(solution)
logging.info("solution:{} count_0:{}, rid:{}".
format(solution, count_0, rid))
if rid % 3==1:
return count_0
else:
return count_0+2*rid
def mutates(self):
return [("flip_n", [1]), ("flip_n", [2])]
@property
def current_solution(self):
return self._current_solution
@current_solution.setter
def current_solution(self, current_solution):
self._current_solution=current_solution
class GGP(GapPath):
def goal(self, solution):
pass