-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetic.py
84 lines (73 loc) · 2.54 KB
/
Genetic.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
from Organization import *
import pickle
import random
import numpy as np
class Genetic:
def __init__(self, org):
self.org = org
for i in range(len(self.org.skillDist)):
random.shuffle(self.org.skillDist[i])
org.display()
self.randpnts = []
self.initPop = []
self.initPop.append(self._generateInitChrom())
self.initPop.append(self._generateInitChrom())
self._evalFitness()
def _evalFitness(self):
self.efficiency = []
for chrom in self.initPop:
E = 0
i = 0
for proj in self.org.skillReq:
mems = []
M = 0
for j in range(sum(proj)):
mems.append(chrom[i])
i += 1
for x in range(len(mems) - 1):
for y in range(i + 1, len(mems)):
M += self.socioMat[mems[x]][mems[y]]
E += 1 + (M/(sum(proj) ** 2))
self.efficiency.append(E)
def _generateInitChrom(self):
self.randpnts = []
initChrom = []
indices = [0 for _ in range(self.org.numSkill)]
for i in self.org.skillReq:
for j, ele in enumerate(i):
temp = []
for k in range(ele):
temp.append(self.org.skillDist[j][indices[j]])
indices[j] += 1
random.shuffle(temp)
for xx in temp:
initChrom.append(xx)
self.randpnts.append(len(initChrom) - 1)
return initChrom
def crossover(self):
newPop = []
random.shuffle(self.initPop)
for i in range(len(self.initPop) - 1):
newChrom1 = self.initPop[i].copy()
newChrom2 = self.initPop[i + 1].copy()
#print(self.randpnts)
x = np.random.choice(self.randpnts, 2, replace = False)
#print(x)
c1, c2 = x
c1, c2 = (c2, c1) if c2 < c1 else (c1, c2)
for ind in range(c1, c2 + 1):
newChrom1[ind], newChrom2[ind] = newChrom2[ind], newChrom1[ind]
newPop.append(newChrom1.copy())
newPop.append(newChrom2.copy())
self.initPop += newPop
self._evalFitness()
def mutation(self):
#------
if __name__ == "__main__":
with open('filename3.pickle', 'rb') as handle:
org = pickle.load(handle)
gen = Genetic(org)
print(len(gen.initPop))
gen.crossover()
for i in gen.efficiency:
print(i)