A trait is a characteristic assigned to an organism or computational individual. In an evolutionary simulation, traits influence fitness or health and can be passed to offspring. section 2.3 section 2.4.1
A simple model stores traits as fields on a cell. The simulation evaluates those fields, selects fitter parents, combines parental values, and occasionally mutates the resulting offspring. section 2.3 section 2.4.1
A trait participates in the evolutionary loop through several implementation choices:
- Represent traits as numeric cell attributes such as strength, or as several values such as a, b, and c. section 2.3 section 2.4.1
- Translate trait values into health or fitness with an evaluation function. section 2.3 section 2.4.1
- Create offspring by averaging parental traits, then apply a random mutation within configured bounds. section 2.4.1 section 2.4.1
Listing 2.5 EDL_2_3_Simulating_Life.ipynb: Installing PlotLosses
!pip install livelossplot –quiet #1 from livelossplot import PlotLosses #2
Use the notebook listing to run the simulation environment and inspect trait-driven population behavior with live plots. section 2.3
Listing 2.9 EDL_2_4_Simulating_Evolution.ipynb: Updating create_cell
def create_cell(parent1, parent2): #1
if parent1 is None or parent2 is None:
return dict(
fitness = 0, #2
a = random.randint(1, 100), #3
b = random.randint(1, 100),
c = random.randint(1, 100)
)
else:
return dict(
fitness = 0, #2
a = (parent1[“a”] + parent2[“a”])/2, #4
b = (parent1[« b »] + parent2[« b »])/2, #4
c = (parent1[« c »] + parent2[« c »])/2, #4
)
Inspect the offspring constructor to see how traits begin as random values or inherit averaged values from two parents. section 2.4.1
Listing 2.11 EDL_2_4_Simulating_Evolution.ipynb: The mutate function
def mutate(cells):
for cell in cells:
if random.randint(1,100) < MUTATE_RATE: #1
cell“"”"] = clamp(
cell“"”"] + random.randint
(-MUTATE_RNG, MUTATE_RNG), 1, 100) #2
cell“"”"] = clamp(
cell“"”"] + random.randint
(-MUTATE_RNG, MUTATE_RNG), 1, 100) #2
cell“"”"] = clamp(
cell“"”"] + random.randint
(-MUTATE_RNG, MUTATE_RNG), 1, 100) #2
return cells
Inspect mutation as a controlled random change to offspring traits, bounded by a mutation rate and adjustment range. section 2.4.1
