Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 16, 2021 17:35
Show Gist options
  • Save Ifihan/e04febddc8f619a95c8cdbf8de5e02ab to your computer and use it in GitHub Desktop.
Save Ifihan/e04febddc8f619a95c8cdbf8de5e02ab to your computer and use it in GitHub Desktop.
Cross-over genetic algorithm implementation in Python
import random
# prb_score = int(input("Enter the probability score: "))
def cross_over(parent1, parent2):
child1 = []
child2 = []
for i in range(len(parent1)):
if random.random() < 0.5:
child1.append(parent1[i])
child2.append(parent2[i])
else:
child1.append(parent2[i])
child2.append(parent1[i])
return child1, child2
print(cross_over([1,0, 0, 1], [0, 1, 0, 0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment