def crossover(self, individual_1, individual_2): # since the chromosomes are permutation # we keep multiplying two permutation in order to get # 2 children somehow related to the parents if self.__probability_of_crossover < random(): return None, None assert individual_1.length() == individual_2.length() length = individual_1.length() ind_1_chromosome_list = individual_1.get_chromosome_list() ind_2_chromosome_list = individual_2.get_chromosome_list() child_1, child_2 = [], [] for i in range(length): index_1, index_2 = randint(0, length - i - 1), randint( 0, length - i - 1) chrom_1, chrom_2 = ind_1_chromosome_list[ index_1], ind_2_chromosome_list[index_2] ind_1_chromosome_list.pop(index_1) ind_2_chromosome_list.pop(index_2) new_chrom_1 = sample(self.multiply_permutations(chrom_1, chrom_2), len(chrom_1)) new_chrom_2 = sample(self.multiply_permutations(chrom_2, chrom_1), len(chrom_2)) if randint(0, 1): child_1.append(new_chrom_1) child_2.append(new_chrom_2) else: child_1.append(new_chrom_2) child_2.append(new_chrom_1) return Individual(child_1), Individual(child_2)
def one_point_crossover(parent1, parent2): child = Individual(None) point = randint(0, len(parent2.knapsack) - 1) startSet = parent1.knapsack[:point] endSet = parent2.knapsack[point:] child.knapsack = np.concatenate([startSet, endSet]) return child
def hillClimb(self, individualSize, maximumIterations): urAverageGuy = Individual(individualSize) theBestYouCanGet = urAverageGuy while maximumIterations > 0: neighborhood = urAverageGuy.neighbors() for maybeBetterGuy in neighborhood: if maybeBetterGuy.fitness() < urAverageGuy.fitness(): urAverageGuy = maybeBetterGuy if theBestYouCanGet == urAverageGuy or theBestYouCanGet.fitness( ) == 0: return theBestYouCanGet theBestYouCanGet = urAverageGuy maximumIterations -= 1 return theBestYouCanGet
def __generate_an_individual(self): # generate a matrix of return Individual([ sample(range(1, self.__individual_size + 1), self.__individual_size) for _ in range(self.__no_chromosomes) ])
def fixture(): adresse = AddressBuilder().build() fn = "Douakha" ln = "salah" tel = "0606060606" mail = "*****@*****.**" indiv1 = IndividualClient(fn, ln, adresse, mail, tel) addr = AddressBuilder().build() name = "McDonalds" s = "12345678945612" c = Individual('Douakha', 'Sami', addr) pro = ProfessionalClient(s, name, addr, c) adresse = AddressBuilder().build() fn = "Chovin" ln = "antoine" tel = "0606060606" mail = "*****@*****.**" indiv2 = IndividualClient(fn, ln, adresse, mail, tel) return [indiv1, pro, indiv2]
def run(n): current_node = Individual(n) while current_node.fitness() > 0: neighbours = current_node.get_neighbours() neighbours.sort(key=lambda b: b.fitness()) best_neighbour = neighbours[0] if best_neighbour.fitness() < current_node.fitness(): current_node = copy.deepcopy(best_neighbour) else: return current_node, current_node.fitness() return current_node, current_node.fitness()
def AdamAndEvas(self, fitnessTarget, individualSize): Adam = Individual(individualSize) bestSoFar = math.inf while bestSoFar > fitnessTarget: Evas = [Individual(individualSize) for i in range(20)] for Eva in Evas: children = Adam + Eva if children[0].fitness() < Adam.fitness(): Adam = deepcopy(children[0]) elif children[1].fitness() < Adam.fitness(): Adam = deepcopy(children[1]) if Adam.fitness() <= fitnessTarget: return Adam
def run(self): current_node = Individual(self.__n) while current_node.fitness() > 0: neighbours = current_node.get_neighbours() neighbours.sort(key=lambda b: b.fitness()) best_neighbour = neighbours[0] if best_neighbour.fitness() < current_node.fitness(): current_node = copy.deepcopy(best_neighbour) else: self.signal.emit( [current_node, current_node.fitness(), 'done']) return self.signal.emit([current_node, current_node.fitness(), 'done'])
def __init__(self, fname, lname, address, email=None, phone=None): Client.__init__(self) Individual.__init__(self, fname, lname, address, email, phone)
from models.client import Client from models.individual import Individual from models.address_builder import AddressBuilder class IndividualClient(Client, Individual): def __init__(self, fname, lname, address, email=None, phone=None): Client.__init__(self) Individual.__init__(self, fname, lname, address, email, phone) if __name__ == "__main__": adresse = AddressBuilder().build() fn = "Douakha" ln = "salah" tel = "0606060606" mail = "*****@*****.**" contact = Individual(fn, ln, adresse) mcdonald = IndividualClient(fn, ln, adresse, mail, tel) print(mcdonald)
def __init__(self, population_size, number_of_elements=None): if (population_size is not None and number_of_elements is not None): self.individuals = np.empty(population_size, dtype=object) for i in range(population_size): self.individuals[i] = Individual(number_of_elements)
from models.address import Address from core.model import Model from models.individual import Individual class Company(Model): def __init__(self, siret, name, address, contact=None): super().__init__() self.address = address self.name = name self.siret = siret self.contact = contact if __name__ == "__main__": a = Address("34", "avenue de la bajatiere", "38100", "Grenoble") c = Individual('Douakha', 'Sami', a) company = Company("145 678 901 21235", "Noobz", a, c) print(company.id_company)
def __init__(self, size, individualSize): self._size = size self._indiviualSize = individualSize self._population = [Individual(individualSize) for i in range(size)] self._bestIndividual = self.bestIndividual()
def generate_population(self): initial_pop = [] for i in range(self.__population_size): individual = Individual(self.__n) initial_pop.append(individual) return initial_pop[:]
for i in xrange(len(self.trafficLightIdList)): #traverse all the traffic lights tlsLogicList = traci.trafficlights.getCompleteRedYellowGreenDefinition(self.trafficLightIdList[i]) #One traffic light has only one phase list now tlsLogicList = tlsLogicList[0] #each traffic light has several phases phaseList = [] #traverse all the phase for j in xrange(len(tlsLogicList._phases)): # print self.individual.genes[i].times[j] phaseList.append(traci.trafficlights.Phase(self.individual.genes[i].times[j], self.individual.genes[i].times[j], self.individual.genes[i].times[j], tlsLogicList._phases[j]._phaseDef)) tlsLogicList._phases = phaseList traci.trafficlights.setCompleteRedYellowGreenDefinition(self.trafficLightIdList[i], tlsLogicList) #close connection # oriLogic = traci.trafficlights.getCompleteRedYellowGreenDefinition(self.trafficLightIdList[0]) # print(oriLogic[0]._phases[1]._duration) #inductionLoopIdList = traci.inductionloop.getIDList() #print(inductionLoopIdList) for _ in xrange(300): #print traci.simulation.getCurrentTime() traci.simulationStep() #print traci.inductionloop.getLastStepMeanSpeed('e1det_left0to0/0_1') #print traci.inductionloop.getLastStepVehicleNumber('e1det_left0to0/0_1') print traci.inductionloop.getLastStepMeanSpeed('e1det_left1to0/1_0') traci.close() if __name__ == "__main__": instance = SUMOOnline(8813, Individual.random(16)) instance.beginEvaluate()