davecom/ClassicComputerScienceProblemsInPython

send_more_money2 ValueError

Closed this issue · 3 comments

I'm trying to follow your logic in the generic algorithms chapter but I get the following error on send_more_money2.py:
`Generation 0 Best 0.2 Avg 0.0003496131148208891
Traceback (most recent call last):

File "", line 1, in
runfile('/datos/Scripts/Gitea/Classic_problems/Chapter_5__Genetic_algorithms/send_more_money2.py', wdir='/datos/Scripts/Gitea/Classic_problems/Chapter_5__Genetic_algorithms')

File "/datos/Scripts/Python/venv/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "/datos/Scripts/Python/venv/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/datos/Scripts/Gitea/Classic_problems/Chapter_5__Genetic_algorithms/send_more_money2.py", line 87, in
result: SendMoreMoney2 = ga.run()

File "/datos/Scripts/Gitea/Classic_problems/Chapter_5__Genetic_algorithms/genetic_algorithm.py", line 86, in run
highest: C = max(self._population, key=self._fitness_key)

File "/datos/Scripts/Gitea/Classic_problems/Chapter_5__Genetic_algorithms/send_more_money2.py", line 22, in fitness
s: int = self.letters.index("S")

ValueError: 'S' is not in list`

My solutions are not converging to the ones you described in chapter 5. I got the above error, with random letters, every time I run the code.

Any suggestions?

P.S.: Great book!

Hi David,
Thanks for reading the book. Are you getting this result using the source code downloaded from this GitHub repository? Please try the code from this repository. If it works with your set up then you can look at the difference between your code and it.

Best,
David

Thanks for the reply.
The code from the repo is running ok!
It seems that breaking the line to be PEP8 compatible I made a mistake.

Glad you figured out the issue; cheers!