Exemplo n.º 1
0
	def crossover(self, parent1, parent2, numCities):
		child = Tour(numCities)
		startPos = int(random.random() * parent1.tourSize())
		endPos = int(random.random() * parent1.tourSize())
		
		# parent 1
		for i in range(0, child.tourSize()):
			# add city to child tour from parent1 that is between startPos and endPos
			if ((startPos < endPos) and (i > startPos) and (i < endPos)):
				child.setCity(i, parent1.getCity(i))
			elif (startPos > endPos):
				if not((i < startPos) and (i > endPos)):
					child.setCity(i, parent1.getCity(i))
					
		# parent 2
		for i in range(0, parent2.tourSize()):
			if not(child.containsCity(parent2.getCity(i))):
				for j in range(0, child.tourSize()):
					try:
						if (child.getCity(j) == None):
							child.setCity(j, parent2.getCity(i))
							break
					except IndexError:
						a = raw_input("IndexError in crossover mutation.")
						child.setCity(j, parent2.getCity(i))
		return child
    def crossover(self, parent1, parent2):
        #New child tour
        child = Tour(self.tourmanager)

        #Get start and end sub-tour positions ofr parent1's tour
        startPos = int(random.random() * parent1.tourSize())
        endPos = int(random.random() * parent1.tourSize())

        #Loop and add the subtour from parent1 to the child
        for i in range(0, child.tourSize()):
            #If the start position is less than the end position
            if startPos < endPos and i > startPos and i < endPos:
                child.setCity(i, parent1.getCity(i))
            #If the start position is larger
            elif startPos > endPos:
                if not (i < startPos and i > endPos):
                    child.setCity(i, parent1.getCity(i))

        #Loop through parent2's city tour
        for i in range(0, parent2.tourSize()):
            #If the child doesn't have the city, add it
            if not child.containsCity(parent2.getCity(i)):
                #Find an open spot in the child's tour
                for ii in range(0, child.tourSize()):
                    #When an open spot is found, add city
                    if child.getCity(ii) == None:
                        child.setCity(ii, parent2.getCity(i))
                        break

        return child
Exemplo n.º 3
0
    def crossover(self, parenti, parentii):
        child = Tour(self.tourManager)
        startPos = int(random.random() * parenti.tourSize())
        endPos = int(random.random() * parentii.tourSize())

        for i in range(child.tourSize()):
            if startPos < endPos and startPos < i < endPos:
                child.setCity(i, parenti.getCity(i))
            elif startPos > endPos:
                if not startPos < i < endPos:
                    child.setCity(i, parenti.getCity(i))

        for i in range(parentii.tourSize()):
            if not child.containsCity(parentii.getCity(i)):
                for j in range(child.tourSize()):
                    if child.getCity(j) is None:
                        child.setCity(j, parentii.getCity(i))
                        break

        return child