473,480 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cyclic crossover in genetic algorithm (python implementation)

1 New Member
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
  1. def to_cycles(perm):
  2.     pi = {i+1: perm[i] for i in range(len(perm))}
  3.     cycles = []
  4.  
  5.     while pi:
  6.         elem0 = next(iter(pi)) # arbitrary starting element
  7.         this_elem = pi[elem0]
  8.         next_item = pi[this_elem]
  9.  
  10.         cycle = []
  11.         while True:
  12.             cycle.append(this_elem)
  13.             del pi[this_elem]
  14.             this_elem = next_item
  15.             if next_item in pi:
  16.                 next_item = pi[next_item]
  17.             else:
  18.                 break
  19.  
  20.         cycles.append(cycle)
  21.  
  22.     return cycles
  23. aa = to_cycles([4,1,6,2,3,5,8,9,7,10])
  24. print("array: ", aa)
  25. dicts = {}
  26. keys = range(0, len(aa)-1)
  27. for i in keys:
  28.         dicts[i] = aa[i]
  29. print(dicts)
Thanks for your input.
Oct 20 '18 #1
0 2485

Sign in to post your reply or Sign up for a free account.

Similar topics

0
998
by: Wolfgang Keller | last post by:
Hello, Does anyone have any recent information about the status of the (Stackless) Python implementation for the PalmOS? (Yes, I know about Pippy, but that's O-L-D) TIA, Best regards,
2
3811
by: SNAKE | last post by:
hey somebody have any experience using genetic algorithm? i have a problem, I'll try to write game "circle and cross" based on the genetic algorithm anybody can help me? /Snake
4
2156
by: Akihiro KAYAMA | last post by:
Hi all. I would like to ask how I can implement string-like class using tuple or list. Does anyone know about some example codes of pure python implementation of string-like class? Because I...
0
1332
by: Vishal Patil | last post by:
Check out a new and simple Python base genetic algorithm framework at http://vishpat.googlepages.com/pgap -- Motivation will almost always beat mere talent.
1
2028
by: DumRat | last post by:
Hi, I wanted to create a genetic algorithm for this question : Suppose you are out shopping. You have to buy things from different n number of stores. And have to bring home the goods...
0
1098
by: samuraisam | last post by:
Has anyone done any recent testing as to which current python implementation is the quickest? Perhaps for Django development - though the current one certainly is fast (and I doubt micro...
3
3798
by: mfareed | last post by:
Can somebody help me with the CRC (Cyclic Redundancy Code) algorithm implementation given below: There is a three byte packet header. <--------1st byte-----><--------2nd...
0
1466
by: calvindon0820 | last post by:
Hi all, I am stuck in a bin packing problem. Basically, I have different sizes of items with different weights. I am trying to put those items into the bins with a maximum number of full and...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.