def execute(self, solution: CompositeSolution) -> CompositeSolution: Check.is_not_none(solution) mutated_solution_components = [] for i in range(solution.number_of_variables): mutated_solution_components.append(self.mutation_operators_list[i].execute(solution.variables[i])) return CompositeSolution(mutated_solution_components)
def __init__(self, mutation_operator_list:[Mutation]): super(CompositeMutation,self).__init__(probability=1.0) Check.is_not_none(mutation_operator_list) Check.collection_is_not_empty(mutation_operator_list) self.mutation_operators_list = [] for operator in mutation_operator_list: Check.that(issubclass(operator.__class__, Mutation), "Object is not a subclass of Mutation") self.mutation_operators_list.append(operator)
def __init__(self, crossover_operator_list:[Crossover]): super(CompositeCrossover, self).__init__(probability=1.0) Check.is_not_none(crossover_operator_list) Check.collection_is_not_empty(crossover_operator_list) self.crossover_operators_list = [] for operator in crossover_operator_list: Check.that(issubclass(operator.__class__, Crossover), "Object is not a subclass of Crossover") self.crossover_operators_list.append(operator)
def execute(self, solutions: List[CompositeSolution]) -> List[CompositeSolution]: Check.is_not_none(solutions) Check.that(len(solutions) == 2, "The number of parents is not two: " + str(len(solutions))) offspring1 = [] offspring2 = [] number_of_solutions_in_composite_solution = solutions[0].number_of_variables for i in range(number_of_solutions_in_composite_solution): parents = [solutions[0].variables[i], solutions[1].variables[i]] children = self.crossover_operators_list[i].execute(parents) offspring1.append(children[0]) offspring2.append(children[1]) return [CompositeSolution(offspring1), CompositeSolution(offspring2)]
def __init__(self, solutions: List[Solution]): super(CompositeSolution, self).__init__(len(solutions), solutions[0].number_of_objectives, solutions[0].number_of_constraints) Check.is_not_none(solutions) Check.collection_is_not_empty(solutions) for solution in solutions: Check.that( solution.number_of_objectives == solutions[0].number_of_objectives, "The solutions in the list must have the same number of objectives: " + str(solutions[0].number_of_objectives)) Check.that( solution.number_of_constraints == solutions[0].number_of_constraints, "The solutions in the list must have the same number of constraints: " + str(solutions[0].number_of_constraints)) self.variables = solutions
def test_should_is_not_null_raise_an_exception(self) -> None: with self.assertRaises(NoneParameterException): Check.is_not_none(None)
def get_neighbors(self, index: int, solution_list: List[Solution]) -> List[Solution]: Check.is_not_none(solution_list) Check.that(len(solution_list) != 0, "The list of solutions is empty") return self.__find_neighbors(solution_list, index, self.neighborhood)