-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplicer.py
executable file
·161 lines (125 loc) · 2.84 KB
/
splicer.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
156
157
158
159
160
#!/usr/bin/python2
from dbclient import *
import random
import pickle
import copy
def splice():
conn = init_db("bottsydb")
EID = get_open_eid(conn)
setScore = 0
for i in get_active_xc(conn, EID):
setScore += i[6]
for i in range(100):
parent = select_xc(conn, xcid)
xc = extract_xc(parent)
child = mutatue(xc)
add_xc(conn, EID, 1, child, "parents", 0, 0)
def get_weighted_id(conn, EID):
rows = get_active_xc(conn,EID)
xc_data = map(lambda r: select_xc(conn, r[0]), rows)
random.shuffle(xc_data)
ranks = map(lambda r: r[6], xc_data)
rnd = random.random() * sum(ranks)
for i, w in enumerate(ranks):
rnd -= w
if rnd < 0:
return xc_data[i]
def extract_xc(parent):
return pickle.loads(str(parent[3]))
def print_xc(xc):
print xc[0]
for i in xc[1]:
print i
# grabs one portion of a chromosome
# and swaps it with another portion
def transpose(xc):
selection1 = rand_selection()
selection2 = rand_selection()
return None
#
def cross(xc1, xc2):
child = copy.deepcopy(xc1)
child['light'] = xc2['dark']
return child
def mutate(xc):
child = copy.deepcopy(xc)
selection = rand_selection()
threshold = selection[0]
field = selection[1]
move = selection[2]
weight = selection[3]
# if it is a weighted value,
if(field == "weights"):
child[threshold][field][move][weight] = random.random()
weight2 = {
"L": "T",
"T": "L",
} [weight]
child[threshold][field][move][weight2] = (1.0 - child[threshold][field][move][weight])
else:
child[threshold][field] = random.randint(randMin,randMax)
# print "%s %s %s %s" % (threshold, field, move, weight)
return child
def get_mutated_xc(conn, eid):
xc_row1 = get_weighted_id(conn,eid)
xc_row2 = get_weighted_id(conn,eid)
#print 'chosen id = %d' % xc_row[0]
return mutate(cross(extract_xc(xc_row1),extract_xc(xc_row2)))
# returns a tuple of the following format:
# (threshold, field, move, weight)
# NOTE: if field is not of type "weight",
# both move and weight will be empty (None)
def rand_selection():
threshold = random.randint(1,2)
field = random.randint(1,8)
move = random.randint(1,2)
weight = random.randint(1,2)
if (field == 8):
move = {
1: "line",
2: "turn",
} [move]
weight = {
1: "L",
2: "T",
} [weight]
else:
move = None
weight = None
# define what the maximum/minimum random value will be
# based on what was chosen
randMin = {
1: 0,
2: 0,
3: -360,
4: -360,
5: 100,
6: 100,
7: 0,
} [field]
randMax = {
1: 500,
2: 500,
3: 360,
4: 360,
5: 20000,
6: 20000,
7: 254,
} [field]
# Map numbers to threshold type
threshold = {
1: "light",
2: "dark",
} [threshold]
# Map number to field type
field = {
1: "dMax",
2: "dMin",
3: "phiMax",
4: "phiMin",
5: "rtMax",
6: "rtMin",
7: "lSense",
8: "weights",
} [field]
return (threshold, field, move, weight)