Skip to content

Commit

Permalink
Fix anyoptimization#368: Multi-objective Optimization With Mixed Vari…
Browse files Browse the repository at this point in the history
…ables (multipl)

Dealing with numpy arrays of fixed length has caused an issue when setting longer strings.
  • Loading branch information
blankjul committed Jan 27, 2023
1 parent 5850f20 commit 4158d47
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
12 changes: 5 additions & 7 deletions pymoo/core/mixed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from copy import deepcopy

import numpy as np

Expand All @@ -10,7 +11,7 @@
from pymoo.core.population import Population
from pymoo.core.problem import Problem
from pymoo.core.sampling import Sampling
from pymoo.core.variable import Choice, Real, Integer, Binary
from pymoo.core.variable import Choice, Real, Integer, Binary, BoundedVariable
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.crossover.ux import UX
from pymoo.operators.mutation.bitflip import BFM
Expand Down Expand Up @@ -96,12 +97,9 @@ def _do(self, problem, pop, n_offsprings, parents=False, **kwargs):
_parents = [[Individual(X=np.array([parent.X[var] for var in list_of_vars])) for parent in parents] for
parents in pop]

_vars = [vars[e] for e in list_of_vars]
_xl, _xu = None, None

if clazz in [Real, Integer]:
_xl, _xu = np.array([v.bounds for v in _vars]).T

_vars = {e: vars[e] for e in list_of_vars}
_xl = np.array([vars[e].lb if hasattr(vars[e], "lb") else None for e in list_of_vars])
_xu = np.array([vars[e].ub if hasattr(vars[e], "ub") else None for e in list_of_vars])
_problem = Problem(vars=_vars, xl=_xl, xu=_xu)

_off = crossover(_problem, _parents, **kwargs)
Expand Down
7 changes: 5 additions & 2 deletions pymoo/core/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ def __init__(self,
if vars is not None:
self.vars = vars
self.n_var = len(vars)
self.xl = {name: var.lb if hasattr(var, "lb") else None for name, var in vars.items()}
self.xu = {name: var.ub if hasattr(var, "ub") else None for name, var in vars.items()}

if self.xl is None:
self.xl = {name: var.lb if hasattr(var, "lb") else None for name, var in vars.items()}
if self.xu is None:
self.xu = {name: var.ub if hasattr(var, "ub") else None for name, var in vars.items()}

# the variable type (only as a type hint at this point)
self.vtype = vtype
Expand Down
10 changes: 7 additions & 3 deletions pymoo/operators/mutation/rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ class ChoiceRandomMutation(Mutation):
def _do(self, problem, X, **kwargs):
assert problem.vars is not None

# ensure the type object (fixed string length <UX can cause issues)
X = X.astype(object)

prob_var = self.get_prob_var(problem, size=len(X))

for k in range(problem.n_var):
var = problem.vars[k]
for k, (_, var) in enumerate(problem.vars.items()):
mut = np.where(np.random.random(len(X)) < prob_var)[0]
X[mut, k] = var.sample(len(mut))

v = var.sample(len(mut))
X[mut, k] = v

return X

0 comments on commit 4158d47

Please sign in to comment.