from random import randint
goal = "Hello, World!"
allowed_characters = list("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM ,!")
def get_random_entity(n, string_length):
entities = []
for _ in range(0, n):
entity = ""
for _ in range(0, string_length):
entity += allowed_characters[randint(0, len(allowed_characters)-1)]
entities.append(entity)
return entities
def get_fitness(entity):
points = 0
for i in range(0, len(entity)):
if goal[i] == entity[i]:
points += 1
return points
# Get offspring from two parents.
# First, we choose random division point. Genes
# before the division point come from first parent and
# genes after the division point come from second parent.
# If the gene is to be mutated, random character is selected instead.
def get_offspring(first_parent, second_parent, mutation_chance):
new_entity = ""
for i in range(0, len(first_parent)):
if randint(0, 100) < mutation_chance:
new_entity += allowed_characters[randint(0, len(allowed_characters)-1)]
else:
if randint(0, 1) == 0:
new_entity += first_parent[i]
else:
new_entity += second_parent[i]
return new_entity
entities = get_random_entity(100, 13)
generation = 0
while True:
generation += 1
# Fitness
score = []
for e in entities:
score.append(get_fitness(e))
print(f"Generation {generation}, best score: {max(score)} ::: {entities[0]}")
if entities[0] == goal:
print(f"SOLUTION FOUND IN {generation} GENERATIONS")
break
# Get 90 offsprings.
# Chance to be selected as parent is increased for entities with higher score
total_chance = sum(score)
offsprings = []
while len(offsprings) < 90:
parent1_random = randint(0, total_chance - 1)
parent2_random = randint(0, total_chance - 1)
parent1 = -1
parent2 = -1
for i in range(0, len(score)):
parent1_random -= score[i]
parent2_random -= score[i]
if parent1_random <= 0 and parent1 == -1:
parent1 = i
if parent2_random <= 0 and parent2 == -1:
parent2 = i
offsprings.append(get_offspring(entities[parent1], entities[parent2], 5))
# Kill 90 worst entities
# sort entities based on score
entities_sorted = [entities for _,entities in sorted(zip(score,entities))]
entities_sorted.reverse()
entities = entities_sorted[:10]
entities += offsprings
Gentic Algoirthms | Hello World (Python v3.6.2) : August 2023
The most user friendly and intuitive Gentic Algoirthms | Hello World (version Python v3.6.2) helps you to Edit, Run and Share your Python3 Code directly from your browser. This development environment provides you the latest version Python v3.6.2 as of August 2023.
How to give program Input?
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Keyboard Shortcuts
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
How to Save Python3 Project
You can save your Python3 Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
How to Share Python3 Project
You can use this feature to share your Python3 Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.