Write code for a visualized evolution simulator in Python.
- User
Write code for a visualized evolution simulator in Python.
- Assistant
Write code for a visualized evolution simulator in Python.
Sure! Creating a simple evolution simulator can be both educational and fun. Below, I'll provide a Python script that simulates a basic evolutionary process using a population of organisms with specific traits. We'll use the pygame library for visualization, which allows us to see the population evolve over time.
pygame libraryYou can install pygame using pip:
pip install pygame
import pygame
import random
import math
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Evolution Simulator")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
FOOD_COLOR = (0, 255, 0)
# Clock for controlling frame rate
clock = pygame.time.Clock()
FPS = 60
# Parameters
INITIAL_POPULATION = 50
FOOD_COUNT = 100
ENERGY_LOSS = 0.1
FOOD_ENERGY = 20
REPRODUCTION_THRESHOLD = 100
# Organism class
class Organism:
def __init__(self, x, y, speed, reproduction_rate, energy=50):
self.x = x
self.y = y
self.speed = speed
self.reproduction_rate = reproduction_rate
self.energy = energy
self.size = max(2, int(self.speed)) # Size based on speed
def move(self):
angle = random.uniform(0, 2 * math.pi)
dx = math.cos(angle) * self.speed
dy = math.sin(angle) * self.speed
self.x = (self.x + dx) % WIDTH
self.y = (self.y + dy) % HEIGHT
self.energy -= ENERGY_LOSS
def draw(self, surface):
pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), self.size)
def reproduce(self):
if self.energy >= REPRODUCTION_THRESHOLD:
self.energy /= 2
# Mutate traits slightly
new_speed = max(0.5, self.speed + random.uniform(-0.5, 0.5))
new_reproduction_rate = max(0.1, self.reproduction_rate + random.uniform(-0.1, 0.1))
return Organism(self.x, self.y, new_speed, new_reproduction_rate, self.energy)
return None
# Food class
class Food:
def __init__(self, x, y):
self.x = x
self.y = y
self.size = 4
def draw(self, surface):
pygame.draw.circle(surface, FOOD_COLOR, (int(self.x), int(self.y)), self.size)
# Initialize population
population = []
for _ in range(INITIAL_POPULATION):
x = random.uniform(0, WIDTH)
y = random.uniform(0, HEIGHT)
speed = random.uniform(1, 5)
reproduction_rate = random.uniform(0.1, 1.0)
population.append(Organism(x, y, speed, reproduction_rate))
# Initialize food
food_items = []
for _ in range(FOOD_COUNT):
x = random.uniform(0, WIDTH)
y = random.uniform(0, HEIGHT)
food_items.append(Food(x, y))
# Main loop
running = True
generation = 1
font = pygame.font.SysFont(None, 24)
while running:
clock.tick(FPS)
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update and draw food
for food in food_items:
food.draw(screen)
# Shuffle population for fairness
random.shuffle(population)
# Update and draw organisms
new_organisms = []
for organism in population:
organism.move()
# Check for collision with food
for food in food_items:
dist = math.hypot(organism.x - food.x, organism.y - food.y)
if dist < organism.size + food.size:
organism.energy += FOOD_ENERGY
food.x = random.uniform(0, WIDTH)
food.y = random.uniform(0, HEIGHT)
break
# Reproduce if possible
offspring = organism.reproduce()
if offspring:
new_organisms.append(offspring)
# Draw organism
organism.draw(screen)
# Add new organisms
population.extend(new_organisms)
# Remove dead organisms
population = [org for org in population if org.energy > 0]
# Display stats
text = font.render(f'Generation: {generation} | Population: {len(population)}', True, WHITE)
screen.blit(text, (10, 10))
# Update display
pygame.display.flip()
# Increment generation (for simplicity, every 100 frames is a generation)
if clock.get_time() < 1000:
generation += 1
# Quit Pygame
pygame.quit()
sys.exit()
Install Dependencies:
Ensure you have Python installed. Install pygame using:
pip install pygame
Save the Script:
Save the provided code in a file named evolution_simulator.py.
Run the Script:
Navigate to the directory containing evolution_simulator.py and run:
python evolution_simulator.py
Interact: A window will open displaying the simulation. Close the window or press the close button to exit.
Feel free to tweak the parameters to observe different evolutionary behaviors:
You can also enhance the simulation by adding more traits, complex behaviors, predators, or different food sources.
This simple evolution simulator provides a foundational framework to understand basic evolutionary principles like natural selection, variation, and inheritance. By observing how organisms adapt over time, you can gain insights into the dynamics of populations and evolutionary processes.
Feel free to expand upon this foundation to create more sophisticated simulations!
Not yet included in a published release.