435,584 Members | 3,458 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,584 IT Pros & Developers. It's quick & easy.

cyclic crossover in genetic algorithm (python implementation)

 P: 1 I have a python3 program that gives cycles for a given permutation. I want to know how to find a children out of it. This is what I have to create a cycles; Expand|Select|Wrap|Line Numbers def to_cycles(perm):     pi = {i+1: perm[i] for i in range(len(perm))}     cycles = []       while pi:         elem0 = next(iter(pi)) # arbitrary starting element         this_elem = pi[elem0]         next_item = pi[this_elem]           cycle = []         while True:             cycle.append(this_elem)             del pi[this_elem]             this_elem = next_item             if next_item in pi:                 next_item = pi[next_item]             else:                 break           cycles.append(cycle)       return cycles aa = to_cycles([4,1,6,2,3,5,8,9,7,10]) print("array: ", aa) dicts = {} keys = range(0, len(aa)-1) for i in keys:         dicts[i] = aa[i] print(dicts) Thanks for your input. Oct 20 '18 #1 