def crossoverCenter(self, selectedCell1, selectedCell2): newCell = Gene() if random.randint(0, 1) == 1: newCell.x = selectedCell1.x else: newCell.x = selectedCell2.x if random.randint(0, 1) == 1: newCell.y = selectedCell1.y else: newCell.y = selectedCell2.y if random.randint(0, 1) == 1: newCell.z = selectedCell1.z else: newCell.z = selectedCell2.z if random.randint(0, 1) == 1: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta newCell.rotateBonds = selectedCell1.rotateBonds[:] else: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta newCell.rotateBonds = selectedCell2.rotateBonds[:] return newCell
def crossoverUniform(self, selectedCell1, selectedCell2): newCell = Gene() if random.randint(0, 1) == 1: newCell.x = selectedCell1.x else: newCell.x = selectedCell2.x if random.randint(0, 1) == 1: newCell.y = selectedCell1.y else: newCell.y = selectedCell2.y if random.randint(0, 1) == 1: newCell.z = selectedCell1.z else: newCell.z = selectedCell2.z if random.randint(0, 1) == 1: newCell.sph_theta = selectedCell1.sph_theta else: newCell.sph_theta = selectedCell2.sph_theta if random.randint(0, 1) == 1: newCell.sph_phi = selectedCell1.sph_phi else: newCell.sph_phi = selectedCell2.sph_phi if random.randint(0, 1) == 1: newCell.theta = selectedCell1.theta else: newCell.theta = selectedCell2.theta for i in range(len(selectedCell1.rotateBonds)): if random.randint(0, 1) == 1: newCell.rotateBonds.append(selectedCell1.rotateBonds[i]) else: newCell.rotateBonds.append(selectedCell2.rotateBonds[i]) return newCell
def crossoverUniform(self, selectedCell1, selectedCell2): newCell = Gene() if random.randint(0,1) == 1: newCell.x = selectedCell1.x else: newCell.x = selectedCell2.x if random.randint(0,1) == 1: newCell.y = selectedCell1.y else: newCell.y = selectedCell2.y if random.randint(0,1) == 1: newCell.z = selectedCell1.z else: newCell.z = selectedCell2.z if random.randint(0,1) == 1: newCell.sph_theta = selectedCell1.sph_theta else: newCell.sph_theta = selectedCell2.sph_theta if random.randint(0,1) == 1: newCell.sph_phi = selectedCell1.sph_phi else: newCell.sph_phi = selectedCell2.sph_phi if random.randint(0,1) == 1: newCell.theta = selectedCell1.theta else: newCell.theta = selectedCell2.theta return newCell
def crossoverBlock(self, selectedCell1, selectedCell2): newCell = Gene() if random.randint(0,1) == 1: newCell.x = selectedCell1.x newCell.y = selectedCell1.y newCell.z = selectedCell1.z else: newCell.x = selectedCell2.x newCell.y = selectedCell2.y newCell.z = selectedCell2.z if random.randint(0,1) == 1: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta else: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta return newCell
def crossoverSPC(self, selectedCell1, selectedCell2): pop1 = [] pop2 = [] pop1.append(selectedCell1.x) pop1.append(selectedCell1.y) pop1.append(selectedCell1.z) pop1.append(selectedCell1.sph_theta) pop1.append(selectedCell1.sph_phi) pop1.append(selectedCell1.theta) for angle in selectedCell1.rotateBonds: pop1.append(angle) pop2.append(selectedCell2.x) pop2.append(selectedCell2.y) pop2.append(selectedCell2.z) pop2.append(selectedCell2.sph_theta) pop2.append(selectedCell2.sph_phi) pop2.append(selectedCell2.theta) for angle in selectedCell2.rotateBonds: pop2.append(angle) genSize = len(pop1) cutPoint = random.randint(0, genSize - 1) length = random.randint(0, genSize - 1) if length == 0: return selectedCell1 elif length > (genSize - cutPoint): newPop = pop1[cutPoint:] turnTop = length - (genSize - cutPoint) for i in range(0, turnTop): newPop.insert(i, pop1[i]) for i in range(turnTop, cutPoint): newPop.insert(i, pop2[i]) else: newPop = pop1[cutPoint:cutPoint + length] for i in range(0, cutPoint): newPop.insert(i, pop2[i]) for i in range(cutPoint + length, genSize): newPop.insert(i, pop2[i]) newCell = Gene() newCell.x = newPop[0] newCell.y = newPop[1] newCell.z = newPop[2] newCell.sph_theta = newPop[3] newCell.sph_phi = newPop[4] newCell.theta = newPop[5] newCell.rotateBonds = newPop[6:] return newCell
def randomCell(self): gen = Gene() gen.x = random.uniform(-self.__searchSpaceSize, self.__searchSpaceSize) gen.y = random.uniform(-self.__searchSpaceSize, self.__searchSpaceSize) gen.z = random.uniform(-self.__searchSpaceSize, self.__searchSpaceSize) gen.sph_theta = math.pi * random.randint(0, 200) / 100.0 gen.sph_phi = math.pi * random.randint(0, 100) / 100.0 gen.theta = math.pi * random.randint(0, 200) / 100.0 for r in xrange(len(self.__rotateAtoms)): gen.rotateAtoms.append(math.pi * random.randint(0, 200) / 100.0) return copy.deepcopy(gen)
def crossover50(self, selectedCell1, selectedCell2): newCell = Gene() #switch center of ligand centrand = random.randint(0,5) if centrand == 0: newCell.x = selectedCell1.x newCell.y = selectedCell2.y newCell.z = selectedCell2.z elif centrand == 1: newCell.x = selectedCell1.x newCell.y = selectedCell1.y newCell.z = selectedCell2.z elif centrand == 2: newCell.x = selectedCell1.x newCell.y = selectedCell1.y newCell.z = selectedCell1.z elif centrand == 3: newCell.x = selectedCell2.x newCell.y = selectedCell2.y newCell.z = selectedCell2.z elif centrand == 4: newCell.x = selectedCell2.x newCell.y = selectedCell1.y newCell.z = selectedCell1.z else: newCell.x = selectedCell2.x newCell.y = selectedCell2.y newCell.z = selectedCell1.z #switch rotation of ligand rotrand = random.randint(0,5) if rotrand == 0: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 1: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 2: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta elif rotrand == 3: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 4: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta else: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell1.theta return newCell
def randomPopulation(self, sizePop): population = [] for i in xrange(sizePop): gen = Gene() gen.id = i gen.x = random.uniform(-self.__spaceLen, self.__spaceLen) gen.y = random.uniform(-self.__spaceLen, self.__spaceLen) gen.z = random.uniform(-self.__spaceLen, self.__spaceLen) gen.sph_theta = math.pi * random.randint(0, 200) / 100.0 gen.sph_phi = math.pi * random.randint(0, 100) / 100.0 gen.theta = math.pi * random.randint(0, 200) / 100.0 for r in xrange(len(self.__rotateAtoms)): gen.rotateAtoms.append(math.pi * random.randint(0, 200) / 100.0) population.append(gen) return population[:]
def crossover50(self, selectedCell1, selectedCell2): newCell = Gene() #switch center of ligand centrand = random.randint(0, 5) if centrand == 0: newCell.x = selectedCell1.x newCell.y = selectedCell2.y newCell.z = selectedCell2.z elif centrand == 1: newCell.x = selectedCell1.x newCell.y = selectedCell1.y newCell.z = selectedCell2.z elif centrand == 2: newCell.x = selectedCell1.x newCell.y = selectedCell1.y newCell.z = selectedCell1.z elif centrand == 3: newCell.x = selectedCell2.x newCell.y = selectedCell2.y newCell.z = selectedCell2.z elif centrand == 4: newCell.x = selectedCell2.x newCell.y = selectedCell1.y newCell.z = selectedCell1.z else: newCell.x = selectedCell2.x newCell.y = selectedCell2.y newCell.z = selectedCell1.z #switch rotation of ligand rotrand = random.randint(0, 5) if rotrand == 0: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 1: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 2: newCell.sph_theta = selectedCell1.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta elif rotrand == 3: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell2.theta elif rotrand == 4: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell1.sph_phi newCell.theta = selectedCell1.theta else: newCell.sph_theta = selectedCell2.sph_theta newCell.sph_phi = selectedCell2.sph_phi newCell.theta = selectedCell1.theta #switch rotational bonds bondrand = random.randint(0, (len(selectedCell1.rotateBonds) - 1)) if random.randint(0, 1) == 1: newCell.rotateBonds = selectedCell1.rotateBonds[: bondrand] + selectedCell2.rotateBonds[ bondrand:] else: newCell.rotateBonds = selectedCell2.rotateBonds[: bondrand] + selectedCell1.rotateBonds[ bondrand:] return newCell