# Copyright (c) 2022 AIRBUS and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from collections.abc import MutableSequence, Sequence
from typing import Any, TypeVar
from discrete_optimization.generic_tools.do_mutation import Mutation
from discrete_optimization.generic_tools.do_problem import Problem, Solution
T = TypeVar("T")
[docs]
def generic_mutate_wrapper(
individual: MutableSequence[T],
problem: Problem,
encoding_name: str,
indpb: Any,
solution_fn: type[Solution],
custom_mutation: Mutation,
) -> tuple[MutableSequence[T]]:
kwargs: dict[str, Any] = {encoding_name: individual, "problem": problem}
custom_sol: Solution = solution_fn(**kwargs) # type: ignore
new_custom_sol = custom_mutation.mutate(custom_sol)[0]
new_individual = individual
tmp_vector: Sequence = getattr(new_custom_sol, encoding_name)
for i in range(len(tmp_vector)):
new_individual[i] = tmp_vector[i]
return (new_individual,)