def crossover(self, x, y): """Do Gene Crossover and use real type""" ratio = (random.random() - 0.5) * 2 * self.rat_CS gx = Gene.Gene() gy = Gene.Gene() for i in range(len(x.DNA)): gx.DNA[i] = x.DNA[i] + ratio * (x.DNA[i] - y.DNA[i]) gy.DNA[i] = y.DNA[i] - ratio * (x.DNA[i] - y.DNA[i]) return gx, gy
def addNode(self, innovationHistory): #pick a random connection to create a node between if len(self.genes) == 0: self.addConnection(innovationHistory) return #Bias is node 0, so we don't want to lose it if len(self.genes) > 1: randomConnection = math.floor( random.randint(1, len(self.genes) - 1)) else: randomConnection = 0 self.genes[randomConnection].enabled = False #disable it newNodeNo = self.nextNode self.nodes.append(Node(newNodeNo)) self.nextNode += 1 #add a new connection to the new node with a weight of 1 connectionInnovationNumber = self.getInnovationNumber( innovationHistory, self.genes[randomConnection].fromNode, self.getNode(newNodeNo)) self.genes.append( Gene(self.genes[randomConnection].fromNode, self.getNode(newNodeNo), 1, connectionInnovationNumber)) connectionInnovationNumber = self.getInnovationNumber( innovationHistory, self.getNode(newNodeNo), self.genes[randomConnection].toNode) #fix this #add a new connection from the new node with a weight the same as the disabled connection self.genes.append( Gene(self.getNode(newNodeNo), self.genes[randomConnection].toNode, self.genes[randomConnection].weight, connectionInnovationNumber)) self.getNode( newNodeNo).layer = self.genes[randomConnection].fromNode.layer + 1 connectionInnovationNumber = self.getInnovationNumber( innovationHistory, self.nodes[self.biasNode], self.getNode(newNodeNo)) #connect the bias to the new node with a weight of 0 self.genes.append( Gene(self.nodes[self.biasNode], self.getNode(newNodeNo), 0, connectionInnovationNumber)) #if the layer of the new node is equal to the layer of the output node of the old connection then a new layer needs to be created #more accurately the layer numbers of all layers equal to or greater than this new node need to be incrimented if self.getNode( newNodeNo).layer == self.genes[randomConnection].toNode.layer: for i in self.nodes: #dont include this newest node if i.layer >= self.getNode(newNodeNo).layer: i.layer += 1 self.layers += 1 self.connectNodes()
def crossover(self, permutation, param=Parameters): #obtain the index list of individuals who are selected for crossover index_pool = self.selection(permutation, param) #offsprings list offspring = [] #loop over the pool in order to perform the crossovers for i in range(0, int(len(index_pool)), 2): #sample two indices of the list to be the cut points (1, len-2) cut_point = random.sample(range(1, param.jobs - 1), 2) #sort the cut point cut_point = sorted(cut_point) #crossover then reparation, it's faster to repair than to treat before repairing h1 = permutation[index_pool[i]].genotype[:cut_point[0]] h2 = permutation[index_pool[i + 1]].genotype[:cut_point[0]] t1 = permutation[index_pool[i]].genotype[cut_point[1] + 1:] t2 = permutation[index_pool[i + 1]].genotype[cut_point[1] + 1:] #look up the elements that are missing in both junctions of subsets diff1 = set(list(range(0, param.jobs))) - set(h1 + t2) diff2 = set(list(range(0, param.jobs))) - set(h2 + t1) #initialize offsprings as new clean individuals offspring.append(Gene.Gene(param, type=1)) offspring.append(Gene.Gene(param, type=1)) #append h1+ shuffle(diff1) + t2 offspring[i].genotype = h1 + random.sample(diff1, len(diff1)) + t2 #append h2+ shuffle(diff2) + t1 offspring[i + 1].genotype = h2 + random.sample(diff2, len(diff2)) + t1 #print(offspring[i].genotype) #print(offspring[i+1].genotype) #do any reparation necessary offspring[i].genotype = list(unique_everseen( offspring[i].genotype)) offspring[i + 1].genotype = list( unique_everseen(offspring[i + 1].genotype)) #print(offspring[i].genotype) #print(offspring[i+1].genotype) #print("\n") #print("POPULATION") #for i in range(0, int(len(permutation))): #print(permutation[i].genotype) #loop over the pool in order to evaluate everything #print("OFFSPRING") for i in range(0, int(len(offspring))): offspring[i].fitness = self.makespam(offspring[i].genotype, param) #print(offspring[i].genotype, offspring[i].fitness ) return offspring
def _mutate_node(self): gene = random.choice(list(filter(lambda g: g.enabled, self.genes))) node = Node(self.functions, self.nodes[gene.in_node].after + [gene.in_node]) i = len(self.nodes) self.nodes[gene.out_node].after.append(i) self.nodes.append(node) self.layers[-2].append(node) gene1 = Gene(gene.in_node, i, gene.weight, self.new_innovation(gene.in_node, i)) gene2 = Gene(i, gene.out_node, 1, self.new_innovation(i, gene.out_node)) self._add_gene(gene2) self._add_gene(gene1) gene.disable()
def __init__(self, param = Parameters): self.population = [] self.tnPool = [] self.param = param #list with every best element self.bests = [] #list with all the children self.children = [] #number of restriction self.restrictions = param.restrictions #define the pool size self.pool_size = (math.floor(self.param.popNum * self.param.cRate)) #ensuring an even value if self.pool_size % 2 != 0: self.pool_size = self.pool_size - 1 for _ in range(0, self.param.popNum): self.population.append(Gene.Gene(self.param.lowBound, self.param.uppBound, self.param.dim)) #as a placeholder, set the superindividual as the first element of the population self.super = self.population[0]
def parse_sampler_params(header): """ Parse parameters that were used to produce a set of samples. Build a Gene instance out of the Gene it was used on and return that as well. """ if header[0] == '#': # strip header start header = header[1:] fields = header.split('\t') isoforms = [] params = {} exon_lens = {} for field in fields: key, value = field.split('=') if key == 'isoforms': isoform_desc = eval(value) elif key == 'exon_lens': exon_lens = dict(eval(value)) else: params[key] = value exons = [] for e, exon_len in exon_lens.iteritems(): exons.append(Gene.Exon(0, 0 + exon_len - 1, label=e)) gene = Gene.Gene(isoform_desc, exons) return (params, gene)
def makeCopy(self, id): genome = Genome(id) genome.neurons = [] neuronMap = {} if self.neurons is not None: for neuron in self.neurons: copy = Neuron(neuron.id, neuron.type, active=neuron.active, trait=neuron.trait) genome.neurons.append(copy) neuronMap[copy.id] = copy # XXX: not using traits genome.genes = [] if self.genes is not None: for gene in self.genes: if gene.input.id in neuronMap and gene.output.id in neuronMap: input = neuronMap[gene.input.id] output = neuronMap[gene.output.id] genome.genes.append( Gene(input, output, gene.synapse.weight, gene.synapse.recurrent, gene.synapse.trait, gene.enabled, gene.mutation, gene.innovation)) # XXX: copying reference genome.traits = self.traits return genome
def addConnection(self, innovationHistory): #cannot add a connection to a fully connected network if self.fullyConnected(): print("connection failed") return #get random nodes randomNode1 = math.floor(random.randint(1, len(self.nodes) - 1)) randomNode2 = math.floor(random.randint(1, len(self.nodes) - 1)) while self.randomConnectionNodesAreBad(randomNode1, randomNode2): #while the random nodes are no good, get new ones randomNode1 = math.floor(random.randint(1, len(self.nodes) - 1)) randomNode2 = math.floor(random.randint(1, len(self.nodes) - 1)) temp = 0 if self.nodes[randomNode1].layer > self.nodes[randomNode2].layer: #if the first random node is after the second then switch temp = randomNode2 randomNode2 = randomNode1 randomNode1 = temp #get the innovation number of the connection #this will be a new number if no identical genome has mutated in the same way connectionInnovationNumber = self.getInnovationNumber( innovationHistory, self.nodes[randomNode1], self.nodes[randomNode2]) #add the connection with a random array self.genes.append( Gene(self.nodes[randomNode1], self.nodes[randomNode2], random.uniform(-1, 1), connectionInnovationNumber)) self.connectNodes()
def reproduct(self, genelist, pickedlist): """Do Gene Reproduction and use real type""" gene = Gene.Gene() gene.f = 1e9 for i in pickedlist: if genelist[i].f < gene.f: gene = genelist[i].clone() return gene
def complete_connect( self): # call after creation to make completely connected for node_in in self.layers[0]: for node_out in self.layers[1]: gene = Gene( self.nodes.index(node_in), self.nodes.index(node_out), random.uniform(0, 1), self.new_innovation(self.nodes.index(node_in), self.nodes.index(node_out))) self._add_gene(gene)
def init(self, bestGeneList): """initialize""" if not bestGeneList: for _ in range(self.bestgenesize): gene = Gene.Gene() gene.f = 1e9 self.bestgenelist.append(gene) for _ in range(self.poolsize): gene = Gene.Gene() gene.generate() self.genelist.append(gene) else: self.bestgenelist = copy.deepcopy(bestGeneList) percent25 = int(self.poolsize / 4) for _ in range(percent25): x = random.sample(range(len(self.bestgenelist)), 1)[0] gene = self.bestgenelist[x].clone() self.genelist.append(gene) for _ in range(percent25, self.poolsize): gene = Gene.Gene() gene.generate() self.genelist.append(gene)
def __init__(self, outname, param=Parameters): self.population = [] self.param = param #output file name self.outname = outname #list with every best element self.bests = [] for _ in range(0, self.param.popNum): self.population.append(Gene.Gene(self.param))
def initialize(numStrains: int = 1): seq = rRNA.rRNA(length=5) print(seq.seq()) gene = Gene.Gene("A", 1) strain = Strain.Strain(name="asdasd", genome={gene.name(): gene}, ssu=seq) community = Community.Community({strain.name(): strain}) print(community.richness()) community.speciate(strainName=strain.name(), newStrainName="B", changeSSU=True) print(community.richness()) print(community.getStrain(strain.name()).ssu().seq()) return None
def __init__(self, outname, param=Parameters): self.population = [] self.param = param #output file name self.outname = outname #list with every best element self.bests = [] #list holding information on the amount of each behavior in the population self.behavior_ind = [] for _ in range(0, self.param.popNum): self.population.append(Gene.Gene(self.param))
def _mutate_gene(self): in_node = random.choice(list(sum(self.layers[:-1], []))) connections = [ self.nodes[gene.out_node] if self.nodes[gene.in_node] == in_node else None for gene in self.genes ] # TODO make better ls = filter( lambda x: True if (self.nodes.index(x) not in in_node.after) and (x not in connections) and (x != in_node) and (x not in self._get_input() + [self._get_bias()]) else False, self.nodes) ls = list(ls) if len(ls) == 0: # print("no possible connections") return None out_node = self.nodes.index(random.choice(ls)) in_node = self.nodes.index(in_node) weight = random.uniform(-1, 1) innovation = self.new_innovation(in_node, out_node) gene = Gene(in_node, out_node, weight, innovation) self._add_gene(gene)
def main(): """Start Gene Machine to find best parameters of RBF Network""" ret = setinfo() path = "./data/no_pos/" inputt = [] outputt = [] for dirPath, fileNames in os.walk(path): for filee in fileNames: filee = os.path.join(dirPath, filee) f = open(filee, "r") for line in f.readlines(): tp = line.split() listt = [] if isinstance(eval(tp[0]), float): listt.append(eval(tp[0])) listt.append(eval(tp[1])) listt.append(eval(tp[2])) inputt.append(listt) outputt.append(eval(tp[3])) else: print tp print "N = " + str(len(inputt)) print "input list = " + str(inputt) print "output list = " + str(outputt) # Normalize for i in range(len(outputt)): outputt[i] = (outputt[i] + 40.0) / 80.0 fError_ori = 1e9 fError_now = 1e9 bestGeneSize = int(ret.get('poolsize') / 10) while fError_now > 5: if os.path.isfile("./bestGA.txt"): readfile = open("./bestGA.txt", "r") strlist = [] for i in range(bestGeneSize): strlist.append(readfile.readline()) readfile.close() genelist = [] for i in range(bestGeneSize): gene = Gene.Gene() gene.setGene(strlist[i]) gene.calculateFitness(inputt, outputt) genelist.append(gene) fError_ori = genelist[0].f print("before min function error = ", genelist[0].f) genepool = GenePool.GenePool(ret.get("poolsize"), ret.get('itertimes'), ret.get("pro_CS"), ret.get("rat_CS"), ret.get("pro_MU"), ret.get("rat_MU"), genelist) else: genepool = GenePool.GenePool(ret.get("poolsize"), ret.get('itertimes'), ret.get("pro_CS"), ret.get("rat_CS"), ret.get("pro_MU"), ret.get("rat_MU"), []) bestGeneList = genepool.geneIteration(inputt, outputt) print("after min function error", bestGeneList[0].f) fError_now = bestGeneList[0].f if fError_now < fError_ori: fw = open("./bestGA.txt", 'w') for i in range(genepool.bestgenesize): DNAList = bestGeneList[i].getDNAList() s = " ".join(str(ele) for ele in DNAList) + "\n" fw.write(s) fw.close() print "Training Complete!"
def get_gene(self, g): # Get the definition file gene_definition = get_definition_file(g) gene = Gene.Gene(gene_definition, build=self.build, debug=self.debug) return gene
def generate(self, size): for i in range(0, size): self.genes.append(gn.Gene(scdule.generate_schedule(self.events))) return self
def mutateAddNeuron(self, population): splitGene = None if len(self.genes) < 15: checking = False for gene in self.genes: if checking: if randfloat( ) > 0.3 and gene.synapse.input.type != Neuron.BIAS: splitGene = gene break elif gene.enabled and gene.synapse.input.type != Neuron.BIAS: checking = True else: tryCount = 0 while tryCount < 20 and splitGene is None: index = random.randint(0, len(self.genes) - 1) if self.genes[index].enabled and self.genes[ index].synapse.input.type != Neuron.BIAS: splitGene = self.genes[index] break else: tryCount += 1 if splitGene is not None: splitGene.enabled = False synapse = splitGene.synapse oldWeight = synapse.weight input = synapse.input output = synapse.output newGene1 = None newGene2 = None newNeuron = None found = False for innovation in population.innovations: if innovation.type == Innovation.NEURON and \ innovation.inId == input.id and \ innovation.outId == output.id and \ innovation.oldId == splitGene.innovation: newNeuron = Neuron(innovation.newNeuronId) if len(self.traits) > 0: newNeuron.trait = self.traits[0] newGene1 = Gene(input, newNeuron, 1.0, synapse.recurrent, synapse.trait, innovation=innovation.idA) newGene2 = Gene(newNeuron, output, oldWeight, False, synapse.trait, innovation=innovation.idB) found = True break if not found: id = population.getNextNeuronId() newNeuron = Neuron(id) if len(self.traits) > 0: newNeuron.trait = self.traits[0] innovationId = population.getNextInnovationId(2) newGene1 = Gene(input, newNeuron, 1.0, synapse.recurrent, synapse.trait, innovation=innovationId) newGene2 = Gene(newNeuron, output, oldWeight, False, synapse.trait, innovation=innovationId + 1) population.innovations.append( Innovation(input.id, output.id, idA=innovationId, idB=innovationId + 1, oldId=splitGene.innovation, type=Innovation.NEURON, newNeuronId=newNeuron.id)) if newNeuron is not None: self.genes.append(newGene1) self.genes.append(newGene2) self.neurons.append(newNeuron)
def mutateAddSynapse(self, population, tries): recurrent = False if randfloat() < Configuration.recurrentProbability: recurrent = True startIndex = 0 for neuron in self.neurons: if neuron.type != Neuron.INPUT: break else: startIndex += 1 numNeurons = len(self.neurons) threshold = numNeurons * numNeurons found = False count = 0 input = None output = None if recurrent: while count < tries: inputIndex = 0 outputIndex = 0 # random recurrency if randfloat() > 0.5: inputIndex = random.randint(startIndex, numNeurons - 1) outputIndex = inputIndex else: inputIndex = random.randint(0, numNeurons - 1) outputIndex = random.randint(startIndex, numNeurons - 1) input = self.neurons[inputIndex] output = self.neurons[outputIndex] if output.type != Neuron.INPUT and self.phenotype.isRecurrent( input.analogue, output.analogue, 0, threshold): for gene in self.genes: if gene.synapse.input == input and gene.synapse.output == output and gene.synapse.recurrent: found = True break if found: break count += 1 else: while count < tries: inputIndex = random.randint(0, numNeurons - 1) outputIndex = random.randint(startIndex, numNeurons - 1) input = self.neurons[inputIndex] output = self.neurons[outputIndex] if output.type != Neuron.INPUT and not self.phenotype.isRecurrent( input.analogue, output.analogue, 0, threshold): for gene in self.genes: if gene.synapse.input == input and gene.synapse.output == output and not gene.synapse.recurrent: found = True break if found: break count += 1 if found: found = False newGene = None for innovation in population.innovations: if innovation.type == Innovation.SYNAPSE and \ innovation.inId == input.id and \ innovation.outId == output.id and \ innovation.recurrent == recurrent: newGene = Gene(input, output, innovation.weight, recurrent, self.traits[innovation.traitId], innovation=innovation.idA) found = True break if not found: innovationId = population.getNextInnovationId() traitIndex = random.randint(0, len(self.traits) - 1) newWeight = randposneg() * randfloat() * 10.0 newGene = Gene(input, output, newWeight, recurrent, innovation=innovationId, mutation=newWeight) population.innovations.append( Innovation(inId=input.id, outId=output.id, weight=newWeight, idA=innovationId, traitId=traitIndex, type=Innovation.SYNAPSE, recurrent=recurrent)) self.genes.append(newGene)
def crossover(self, type, dad, id, momFitness=0, dadFitness=0): babyTraits = [] for i in range(len(self.traits)): babyTraits.append(Trait(t1=self.traits[i], t2=dad.traits[i])) babyGenes = [] babyNeurons = [] blxPos = randfloat() momBetter = True if momFitness < dadFitness or (momFitness == dadFitness and len(dad.genes) < len(self.genes)): momBetter = False momIndex, dadIndex = 0, 0 momStopIndex, dadStopIndex = len(self.genes), len(dad.genes) geneCounter = 0 crossoverPoint = 0 momGenes = self.genes dadGenes = dad.genes if type == Crossover.SINGLEPOINT: if momStopIndex < dadStopIndex: crossoverPoint = random.randint(0, momStopIndex) else: crossoverPoint = random.randint(0, dadStopIndex) momGenes = dad.genes momStopIndex = dadStopIndex dadGenes = self.genes dadStopIndex = len(dadGenes) while momIndex < momStopIndex or dadIndex < dadStopIndex: skip = False chosenGene = None disabled = False if momIndex == momStopIndex: chosenGene = dadGenes[dadIndex] dadIndex += 1 if type != Crossover.SINGLEPOINT and momBetter: skip = True elif dadIndex == dadStopIndex: chosenGene = momGenes[momIndex] momIndex += 1 if type != Crossover.SINGLEPOINT and not momBetter: skip = True else: momGene = momGenes[momIndex] dadGene = dadGenes[dadIndex] momInnovation = momGene.innovation dadInnovation = dadGene.innovation if momInnovation == dadInnovation: if type == Crossover.MULTIPOINT: if randfloat() < 0.5: chosenGene = momGene else: chosenGene = dadGene if not momGene.enabled or not dadGene.enabled: if randfloat() < 0.75: disabled = True else: useAverage = True if type == Crossover.SINGLEPOINT: useAverage = False if geneCounter < crossoverPoint: chosenGene = momGene elif geneCounter > crossoverPoint: chosenGene = dadGene else: useAverage = True if useAverage: enabled = True trait = None weight = None input = None output = None recurrent = None if randfloat() > 0.5: trait = momGene.synapse.trait else: trait = dadGene.synapse.trait if type == Crossover.BLX: blxAlpha = -0.4 blxMax = 0.0 blxMin = 0.0 w1 = momGene.synapse.weight w2 = dadGene.synapse.weight if w1 > w2: blxMax = w1 blxMin = w2 else: blxMax = w2 blxMin = w1 blxRange = blxMax - blxMin blxExplore = blxAlpha * blxRange blxMin -= blxExplore blxMax += blxExplore blxRange = blxMax - blxMin weight = blxMin + blxPos * blxRange else: weight = (momGene.synapse.weight + dadGene.synapse.weight) / 2.0 if randfloat() > 0.5: input = momGene.synapse.input else: input = dadGene.synapse.input if randfloat() > 0.5: output = momGene.synapse.output else: output = dadGene.synapse.output if randfloat() > 0.5: recurrent = momGene.synapse.recurrent else: recurrent = dadGene.synapse.recurrent innovation = momGene.innovation mutation = (momGene.mutation + dadGene.mutation) / 2.0 if not momGene.enabled or not dadGene.enabled: if randfloat() < 0.75: enabled = False chosenGene = Gene(input, output, weight, recurrent, trait, enabled, mutation, innovation) momIndex += 1 dadIndex += 1 geneCounter += 1 elif momInnovation < dadInnovation: if type == Crossover.SINGLEPOINT: if geneCounter < crossoverPoint: chosenGene = momGene momIndex += 1 geneCounter += 1 else: chosenGene = dadGene dadIndex += 1 else: chosenGene = momGene momIndex += 1 if not momBetter: skip = True elif dadInnovation < momInnovation: if type == Crossover.SINGLEPOINT: dadIndex += 1 skip = True else: chosenGene = dadGene dadIndex += 1 if momBetter: skip = True for gene in babyGenes: if gene.synapse.input.id == chosenGene.synapse.input.id and \ gene.synapse.output.id == chosenGene.synapse.output.id and \ (gene.synapse.recurrent == chosenGene.synapse.recurrent or \ gene.synapse.recurrent or chosenGene.synapse.recurrent): skip = True break if not skip: traitId = 0 newInput = None newOutput = None if chosenGene.synapse.trait is None: traitId = self.traits[0].id else: traitId = chosenGene.synapse.trait.id - self.traits[0].id input = chosenGene.synapse.input output = chosenGene.synapse.output if input.id < output.id: newInput = self.addNeuron(input, babyNeurons, babyTraits) newOutput = self.addNeuron(output, babyNeurons, babyTraits) else: newOutput = self.addNeuron(output, babyNeurons, babyTraits) newInput = self.addNeuron(input, babyNeurons, babyTraits) newGene = Gene(newInput, newOutput, chosenGene.synapse.weight, \ chosenGene.synapse.recurrent, babyTraits[traitId], \ enabled=chosenGene.enabled, \ mutation=chosenGene.mutation, innovation=chosenGene.innovation) if disabled: newGene.enabled = False disabled = False babyGenes.append(newGene) # TODO: # make sure all possible input, output, and bias neurons are replicated to children # this allows disconnected nodes to survive crossover # see the NEAT FAQ for more info # TODO: should also add mutateAddInput above if this is used #for neuron in self.neurons: # if neuron.type == Neuron.INPUT or neuron.type == Neuron.BIAS or neuron.type == Neuron.OUTPUT: # self.addNeuron(neuron, babyNeurons, babyTraits) # TODO: should validate the network here to make sure there is at least # one path from at least one input to at least one output # otherwise mutate the network until this occurs or throw the network away return Genome(id, babyNeurons, babyGenes, babyTraits)
def load(self, file): traitMap = {} neuronMap = {} for line in file.readlines(): if not line.startswith('/*'): pieces = line.split() type = pieces.pop(0) if type == 'genomestart': self.id = int(pieces[0]) elif type == 'genomeend': if self.id != int(pieces[0]): print "ERROR: id mismatch in genome specification" break elif type == 'trait': trait = Trait(config=pieces) self.traits.append(trait) traitMap[trait.id] = trait elif type == 'node': id = int(pieces[0]) traitId = int(pieces[1]) type = int(pieces[2]) # wtf? type = int(pieces[3]) if traitId in traitMap: neuron = Neuron(id, type, trait=traitMap[traitId]) else: neuron = Neuron(id, type) self.neurons.append(neuron) neuronMap[id] = neuron elif type == 'gene': traitId = int(pieces[0]) inputId = int(pieces[1]) outputId = int(pieces[2]) weight = float(pieces[3]) recurrent = int(pieces[4]) innovation = int(pieces[5]) mutation = int(pieces[6]) enabled = int(pieces[7]) if recurrent == 1: recurrent = True else: recurrent = False if enabled == 1: enabled = True else: enabled = False trait = None input = None output = None if traitId in traitMap: trait = traitMap[traitId] if inputId in neuronMap: input = neuronMap[inputId] if outputId in neuronMap: output = neuronMap[outputId] if trait is not None and input is not None and output is not None: self.genes.append( Gene(input, output, weight, recurrent, trait, enabled, mutation, innovation))
def mutation(permutation, p=Parameters): #getting a random index shift = random.sample(range(0, p.jobs), 1) #saving the value of the random index tmp = permutation[shift[0]] #getting how many units the index will shift (it will either shift the length of permutation #vector - 1 to the left or to the right) move = random.randint(-(p.jobs - 2), (p.jobs - 2)) #remove the element from the list permutation.remove(tmp) #add the removed element in the list mod(len(list)) units to right or left permutation.insert((shift[0] + move) % (len(permutation) + 1), tmp) #print("VALUE:",tmp, " MOVE:", move) return permutation p = Parameters.Params("test") #print(p.jobs,p.machines, p.popNum, p.tn_size, p.generations) #print(p.joblist) perm = [] for i in range(0, p.popNum): perm.append(Gene.Gene(p)) #print(perm[0].genotype) #print(mutation(perm[0].genotype,p)) for i in range(0, len(perm)): perm[i].fitness = makespam(perm[i].genotype, p) #selection(perm,p) update(perm, crossover(perm, p), p)
def Kcross(lowerBound, upperBound, dir, parent1=Gene, parent2=Gene, param=Parameters): offspring1 = parent1.genotype offspring2 = parent2.genotype resultoffspring1 = list(range(param.dim)) resultoffspring2 = list(range(param.dim)) for i in range(0, param.dim): gene1 = offspring1[i] gene2 = offspring2[i] if (random.uniform(0, 1) < param.getcRate()): result = getGeneResult(gene1, gene2, param.getZeta()) if dir[i] > 0: resultoffspring1[i] = gene1 + result resultoffspring2[i] = gene2 + result #bounding the problem if resultoffspring1[i] > upperBound[i]: resultoffspring1[1] = upperBound[i] if resultoffspring2[i] > upperBound[i]: resultoffspring2[1] = upperBound[i] else: resultoffspring1[i] = gene1 - result resultoffspring2[i] = gene2 - result #bounding the problem if resultoffspring1[i] < lowerBound[i]: resultoffspring1[1] = lowerBound[i] if resultoffspring2[i] < lowerBound[i]: resultoffspring2[1] = lowerBound[i] if resultoffspring1[i] < 0: resultoffspring1[i] = 0 if resultoffspring2[i] < 0: resultoffspring2[i] = 0 if random.uniform(0, 1) < param.getdirCoeff(): if dir[i] > 0: resultoffspring1[i] = gene2 - result resultoffspring2[i] = gene1 - result #bounding the problem if resultoffspring1[i] < lowerBound[i]: resultoffspring1[1] = lowerBound[i] if resultoffspring2[i] < lowerBound[i]: resultoffspring2[1] = lowerBound[i] else: resultoffspring1[i] = gene2 + result resultoffspring2[i] = gene1 + result #bounding the problem if resultoffspring1[i] > upperBound[i]: resultoffspring1[1] = upperBound[i] if resultoffspring2[i] > upperBound[i]: resultoffspring2[1] = upperBound[i] else: if dir[i] > 0: resultoffspring1[i] = gene2 + result resultoffspring2[i] = gene1 + result #bounding the problem if resultoffspring1[i] > upperBound[i]: resultoffspring1[1] = upperBound[i] if resultoffspring2[i] > upperBound[i]: resultoffspring2[1] = upperBound[i] else: resultoffspring1[i] = gene2 - result resultoffspring2[i] = gene1 - result #bounding the problem if resultoffspring1[i] < lowerBound[i]: resultoffspring1[1] = lowerBound[i] if resultoffspring2[i] < lowerBound[i]: resultoffspring2[1] = lowerBound[i] else: resultoffspring1[i] = gene1 resultoffspring2[i] = gene2 child1 = Gene.Gene(param.lowBound, param.uppBound, param.dim) child2 = Gene.Gene(param.lowBound, param.uppBound, param.dim) child1.genotype = list(resultoffspring1) child2.genotype = list(resultoffspring2) return child1, child2