Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IndexError in the genetic_algorithm.py when creating a new generation #16

Open
gnypit opened this issue Dec 27, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working Urgent

Comments

@gnypit
Copy link
Owner

gnypit commented Dec 27, 2024

While debugging labyrinth_test.py, in the create_new_generation method in the Population class there is a problem with the following loop:

"""Secondly, we create the new generation with children being a result od selection and crossover operators
        on the current population:"""
        new_generation = Generation(size=self.no_parents_pairs * 2, fitness_function=self.fit_fun)

        for pair in self.current_children[0].get('children'):
            new_generation.add_member(genome=pair[0])
            new_generation.add_member(genome=pair[1])

Error traceback:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2023.2.4\plugins\python\helpers\pydev\pydevd.py", line 1535, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm 2023.2.4\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\jg\PycharmProjects\pyqkd\genal\labyrinth_test.py", line 370, in <module>
    main_pyqkd()
  File "C:\Users\jg\PycharmProjects\pyqkd\genal\labyrinth_test.py", line 366, in main_pyqkd
    test.run()
  File "C:\Users\jg\PycharmProjects\pyqkd\genal\genetic_algorithm.py", line 317, in run
    self.create_new_generation()
  File "C:\Users\jg\PycharmProjects\pyqkd\genal\genetic_algorithm.py", line 263, in create_new_generation
    for pair in self.current_children[0].get('children'):
                ~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

Fix it.

@gnypit gnypit self-assigned this Dec 27, 2024
@gnypit gnypit added the bug Something isn't working label Dec 27, 2024
@gnypit
Copy link
Owner Author

gnypit commented Dec 27, 2024

The current_parents list was empty, and so the children_candidates list and current_children dict were too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Urgent
Projects
None yet
Development

No branches or pull requests

1 participant