Last active
September 24, 2017 21:15
-
-
Save mathieumandret/ec1bad89931538a8188c4b2a986201fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def crossover(self, parent1, parent2): | |
| # new child Route() | |
| child_rt = Route() | |
| for x in range(0,len(child_rt.route)): | |
| child_rt.route[x] = None | |
| # Two random integer indices of the parent1: | |
| start_pos = random.randint(0,len(parent1.route)) | |
| end_pos = random.randint(0,len(parent1.route)) | |
| #### takes the sub-route from parent one and sticks it in itself: | |
| # if the start position is before the end: | |
| if start_pos < end_pos: | |
| # do it in the start-->end order | |
| for x in range(start_pos,end_pos): | |
| child_rt.route[x] = parent1.route[x] # set the values to eachother | |
| # if the start position is after the end: | |
| elif start_pos > end_pos: | |
| # do it in the end-->start order | |
| for i in range(end_pos,start_pos): | |
| child_rt.route[i] = parent1.route[i] # set the values to eachother | |
| # Cycles through the parent2. And fills in the child_rt | |
| # cycles through length of parent2: | |
| for i in range(len(parent2.route)): | |
| # if parent2 has a city that the child doesn't have yet: | |
| if not parent2.route[i] in child_rt.route: | |
| # it puts it in the first 'None' spot and breaks out of the loop. | |
| for x in range(len(child_rt.route)): | |
| if child_rt.route[x] == None: | |
| child_rt.route[x] = parent2.route[i] | |
| break | |
| # repeated until all the cities are in the child route | |
| # returns the child route (of type Route()) | |
| child_rt.recalc_rt_len() | |
| return child_rt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment