LAB 7OPEN ENDED LAB
NAME:TEHREEM AZEEZ
ROLL NO: 2022F-BCNS-007
TITLE:
Solving the Coin Toss Problem using Genetic Algorithm
-OBJECTIVE;
use a Genetic Algorithm (GA) to evolve a random sequence of coin toss outcomes (represented as
a binary string of 0s and 1s) to reach a target pattern — specifically, a string of all heads (111...1),
simulating the optimization behavior of GA on a binary domain.
SOFTWARE USED:
Language: Python 3.x
Platform: (PyCharm, )
METHODOLOGY:
We use a Genetic Algorithm with the following steps:
🧬 1. Encoding:
Each individual is a binary string (e.g., '0110101010'), representing outcomes of 10–20 coin
tosses.
🧪 2. Fitness Function:
Fitness is calculated as the number of matches with the target string (e.g., how many '1's
match '1' in the target).
For target = '1111111111', fitness is the count of 1s in the string.
🔁 3. Selection:
Tournament selection (randomly pick 5 individuals and choose the best among them).
🔀 4. Crossover:
Single-point crossover between two parents.
🔧 5. Mutation:
LAB 7OPEN ENDED LAB
NAME:TEHREEM AZEEZ
ROLL NO: 2022F-BCNS-007
Each bit has a small probability (1%) of flipping.
💯 6. Termination:
Repeat for a max number of generations or until the best individual matches the target
📈 FLOWCHART:
Here is a simple text-based flowchart:
pgsql
CopyEdit
┌──────────────────────────────┐
│ Initialize random population │
└──────────────┬───────────────┘
↓
┌────────────────────┐
│ Evaluate fitness │
└────────┬────────────┘
↓
┌────────────────────────────┐
│ Check if target is reached│◄───┐
└────────────┬───────────────┘ │
↓ │
┌─────────────────────┐ │
│ Select parents │ │
└────────┬─────────────┘ │
↓ │
┌─────────────────────┐ │
│ Crossover to produce │ │
│ offspring │ │
└────────┬─────────────┘ │
↓ │
┌─────────────────────┐ │
│ Apply mutation │ │
└────────┬─────────────┘ │
↓ │
┌──────────────────────┐ │
│ Create new generation│─────┘
└──────────────────────
Observations
1. The genetic algorithm gradually improved the population’s fitness over generations.
2. Initially random strings evolved to closely match the target string of all heads.
3. The optimal solution (all heads) was achieved within 30–50 generations consistently.
LAB 7OPEN ENDED LAB
NAME:TEHREEM AZEEZ
ROLL NO: 2022F-BCNS-007
CODE:
import matplotlib.pyplot as plt
fitness_history = []
def genetic_algorithm():
population = [random_individual() for _ in range(POP_SIZE)]
for generation in range(GENERATIONS):
population = sorted(population, key=fitness, reverse=True)
best = population[0]
best_fit = fitness(best)
fitness_history.append(best_fit)
print(f'Generation {generation}: Best = {best}, Fitness = {best_fit}')
if best_fit == len(TARGET):
print(f'\nTarget reached in generation {generation}!')
break
new_population = [best]
while len(new_population) < POP_SIZE:
p1 = select(population)
p2 = select(population)
child = mutate(crossover(p1, p2))
new_population.append(child)
population = new_population
# Plotting
LAB 7OPEN ENDED LAB
NAME:TEHREEM AZEEZ
ROLL NO: 2022F-BCNS-007
plt.plot(fitness_history)
plt.title("Fitness Over Generations")
plt.xlabel("Generation")
plt.ylabel("Best Fitness")
plt.grid(True)
plt.show()
RESULTS:
The Genetic Algorithm successfully evolved the population to reach the target
sequence of '11111111111111111111' (all heads).
The fitness improved over generations due to effective selection, crossover, and
mutation.
✅ CONCLUSION:
Genetic Algorithms are effective for solving binary optimization problems like this
coin toss problem.
The approach mimics natural evolution and can be tuned with parameters like
mutation rate, population size, and selection method.
The method shows how random search guided by fitness can quickly converge on
optimal solutions in a binary domain