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

load_state not working with functions in custom module #156

Open
MihaiBabiac opened this issue May 4, 2022 · 0 comments
Open

load_state not working with functions in custom module #156

MihaiBabiac opened this issue May 4, 2022 · 0 comments

Comments

@MihaiBabiac
Copy link

When using custom functions for mutation, initialisation, etc. that are stored in a separate directory, PonyGE2 fails to load them from a saved state.

For example, the state saved by the following call can't be loaded because the mutation function custom.mutation.custom_mutation_per_ind is stored in the state as just "custom_mutation_per_ind", ignoring the rest of the path.

python ponyge.py --grammar_file ../grammars/custom_grammar.pybnf --fitness_function custom_fitness --mutation custom.mutation.custom_mutation_per_ind --save_state

My fix for this is below:

--- a/PonyGE2/src/utilities/algorithm/initialise_run.py
+++ b/PonyGE2/src/utilities/algorithm/initialise_run.py
@@ -237,7 +237,11 @@ def get_fit_func_imports():
         attr_name = split_name[-1]
 
         # Get module name.
-        module_name = ".".join(["fitness", params[op]])
+        if not params[op].startswith("fitness."):
+            module_name = ".".join(["fitness", params[op]])
+        else:
+            # Special case when loading from a saved state
+            module_name = ".".join(split_name[:-1])
 
         # Import module and attribute and save.
         params[op] = return_attr_from_module(module_name, attr_name)
--- a/PonyGE2/src/utilities/algorithm/state.py
+++ b/PonyGE2/src/utilities/algorithm/state.py
@@ -142,8 +154,12 @@ def check_name(obj):
     :param obj: An object for which we want to find the name.
     :return: The name of the object
     """
+    try:
+        prefix = obj.__module__ + "."
+    except AttributeError:
+        prefix = ""
 
     try:
-        return obj.__name__
+        return prefix + obj.__name__
     except AttributeError:
-        return obj.__class__.__name__
+        return prefix + obj.__class__.__name__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant