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

Wrong Numpy Array Type When Using Choice in MixedVariableMating #503

Merged
merged 3 commits into from
Nov 19, 2023

Conversation

Deathn0t
Copy link
Contributor

Hello,

When using the MixedVariableGA with MixedVariableMating (default) with a Choice(options=["const", "cosine"]) variable, I noticed that the following step

            mutation = self.mutation[clazz]
            _off = mutation(_problem, _off, **kwargs)

was generating "cosin" instead of "cosine", then I realised that the X[mut, k] = v affectation withing the _do of ChoiceRandomMutation was transforming "cosine" (correct sampled value) to "cosin".

This helped me understand that X was a numpy array, which was probably set with a default type not compatible with any object (looked like a fixed memory reservation looking at the truncation of "cosine" to "cosin").

Going back to the MixedVariableMating class, the _parents are by default created without dtype, I suggest to use dtype="O" for variables of type Choice (looking at the doc/code, it can be any object in the list passed to Choice(options=[...]):

            _parents = [
                [Individual(X=np.array([parent.X[var] for var in list_of_vars], dtype="O" if clazz is Choice else None)) 
                  for parent in parents] 
                for parents in pop
            ]

This resolved this quite problematic bug in my use of PyMOO.

@blankjul blankjul merged commit e43a069 into anyoptimization:main Nov 19, 2023
1 check passed
@blankjul blankjul self-assigned this Nov 19, 2023
@blankjul
Copy link
Collaborator

Such an easy and elegant fix!

This is an ugly bug I was looking into once before (and does not happen across all Python/Numpy versions and OS) and I was hoping it is already fixed.

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

Successfully merging this pull request may close these issues.

2 participants