# 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 abc import abstractmethod
from typing import Any, Dict, Tuple
from discrete_optimization.generic_tools.do_problem import Problem, Solution
[docs]
class LocalMove:
[docs]
@abstractmethod
def apply_local_move(self, solution: Solution) -> Solution:
...
[docs]
@abstractmethod
def backtrack_local_move(self, solution: Solution) -> Solution:
...
[docs]
class LocalMoveDefault(LocalMove):
"""
Not clever local move
If you're lazy or don't have the choice,
don't do in place modification of the previous solution, so that you can retrieve it directly.
So the backward operator is then obvious.
"""
def __init__(self, prev_solution: Solution, new_solution: Solution):
self.prev_solution = prev_solution
self.new_solution = new_solution
[docs]
def apply_local_move(self, solution: Solution) -> Solution:
return self.new_solution
[docs]
def backtrack_local_move(self, solution: Solution) -> Solution:
return self.prev_solution
[docs]
class Mutation:
[docs]
@staticmethod
def build(problem: Problem, solution: Solution, **kwargs: Any) -> "Mutation":
raise NotImplementedError("Please implement it !")
[docs]
@abstractmethod
def mutate(self, solution: Solution) -> Tuple[Solution, LocalMove]:
...
[docs]
@abstractmethod
def mutate_and_compute_obj(
self, solution: Solution
) -> Tuple[Solution, LocalMove, Dict[str, float]]:
...