Join us on our TWC Discord channel

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 GA
#1
Lightbulb 
Hey fellow software engineers and programmers.
I have been assigned a task at my university where I need to create a genetic algorithm in python and adapt it to a practical problem that I haven't been assigned yet.

Before tackling that project my tutor asked me to create a simple crossover function where we can see that I choose two parents, so that a random crossover point between those two parents happens and that I get two offspring.
I am quite loss with that.
Has someone any idea of how to create such a small program?
The language it has to be written in is python 3 Smile (as title states)

Cheers guys and girls. Smile

Lou

Edit: Put in words it's like I need two parents such as Parent 1: 010010101 and parent 2: 0111101010 and then by doing this crossover I need two offspring, from these two parents, offspring 1 and offspring 2
Reply
#2
(28-09-2019, 09:56 AM)LouLou Wrote: Hey fellow software engineers and programmers.
I have been assigned a task at my university where I need to create a genetic algorithm in python and adapt it to a practical problem that I haven't been assigned yet.

Before tackling that project my tutor asked me to create a simple crossover function where we can see that I choose two parents, so that a random crossover point between those two parents happens and that I get two offspring.
I am quite loss with that.
Has someone any idea of how to create such a small program?
The language it has to be written in is python 3 Smile (as title states)

Cheers guys and girls. Smile

Lou

Edit: Put in words it's like I need two parents such as Parent 1: 010010101 and parent 2: 0111101010 and then by doing this crossover I need two offspring, from these two parents, offspring 1 and offspring 2


Good morning Lou, glad to see you are busy with school !.

Can you please explain me in french in PM's ? Maybe I can help you out.
Reply
#3
What are possible crossovers?

Can you select the "genes" from it's parents just at random for example:
parent 1: 010010101
parent 2: 111101010
child: 110110001

or is it done by taking slices?
parent 1: 010010101
parent 2: 111101010
child: 010001010

Red are genes taken by parent 2 and green by parent 1.

this could implemented like this:

def crossover(parent1, parent2):
rand_pos = random.randint(len(parent1))
return np.concatenate((parent1[:rand_pos],parent2[rand_pos:]))

if you need two childs over the same crossover point it's like this
child1 = np.concatenate((parent1[:rand_pos],parent2[rand_pos:]))
child2 = np.concatenate((parent2[:rand_pos],parent1[rand_pos:]))

not sure it's 100% syntactically correct, i didn't test it

are genes from parents always same length?

In short, What's the definition of a crossover exactly?
Reply
#4
Smart drakenSmile
[Image: Davinci%202_d331aecb128a4ce38b4721925194469e.jpg]

el nuestra viva nuestra
Reply
#5
wow draken nice. Yes actually its a random point crossover which means i should take the genes of two parents and cross them over to get TWO offspring with the size of the genes being the same in length.
So basically the goal would be

parent1: 0100010101 (length of 10)
parent2: 0010100101 (length of 10)

Def

Crossover

end goal print( child1, child2)
Child 1: 0101001101 (length of 10)
Child 2: 0101010101 (length of 10)
where the sequence was randomly taken from the parents
Reply
#6
yeah, ok I understand. you probably can write a more advanced function where you can give number of crossovers point you want to do. Because with one crossover point this isn't possible:

p1: 010|0010|101
p2: 001|0100|101
ch1: 0100100101

where | are 2 crossover points. But i don't know if it's in scope of your project. Because a simple crossover function is probably just 1 crossover point Wink.

Are you following a genetic algorithms course? Then you should probably google a bit, I think there will be some good sources about this as it is a popular topic nowadays
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)