Genetic Algorithms in Elixir: Ch 4 portfolio example pp66

The average stock in a portfolio will have ROI of 5 and RISK of 5 (:rand.uniform(10)) and a portfolio size of 100 stocks.

This would give a weighted sum of 2 * ROI - RISK * 100, or an average fitness for each Chromosome portfolio of 500.

A terminate function that checks for a portfolio fitness of exactly 20 is probably going to find one, but there will be many, more profitable portfolios, that will be missed.

If the portfolio size is reduced to 10 stocks (max fitness 190), then a terminate that checks for fitness >180 should find a suitable candidate.

The terminate code should also be changed to

Enum.max_by(population, &Portfolio.fitness_fn/1).fitness > my limit

Important to add the check on the fitness attribute.

1 Like