def __init__(self, problem: FloatProblem, swarm_size: int, uniform_mutation: UniformMutation, non_uniform_mutation: NonUniformMutation, leaders: Optional[BoundedArchive], epsilon: float, termination_criterion: TerminationCriterion, swarm_generator: Generator = store.default_generator, swarm_evaluator: Evaluator = store.default_evaluator): """ This class implements the OMOPSO algorithm as described in todo Update this reference * SMPSO: A new PSO-based metaheuristic for multi-objective optimization The implementation of OMOPSO provided in jMetalPy follows the algorithm template described in the algorithm templates section of the documentation. :param problem: The problem to solve. :param swarm_size: Size of the swarm. :param leaders: Archive for leaders. """ super(OMOPSO, self).__init__(problem=problem, swarm_size=swarm_size) self.swarm_generator = swarm_generator self.swarm_evaluator = swarm_evaluator self.termination_criterion = termination_criterion self.observable.register(termination_criterion) self.uniform_mutation = uniform_mutation self.non_uniform_mutation = non_uniform_mutation self.leaders = leaders self.epsilon = epsilon self.epsilon_archive = NonDominatedSolutionListArchive( EpsilonDominanceComparator(epsilon)) self.c1_min = 1.5 self.c1_max = 2.0 self.c2_min = 1.5 self.c2_max = 2.0 self.r1_min = 0.0 self.r1_max = 1.0 self.r2_min = 0.0 self.r2_max = 1.0 self.weight_min = 0.1 self.weight_max = 0.5 self.change_velocity1 = -1 self.change_velocity2 = -1 self.dominance_comparator = DominanceComparator() self.speed = numpy.zeros( (self.swarm_size, self.problem.number_of_variables), dtype=float)
def __init__(self, problem: FloatProblem, swarm_size: int, mutation: Mutation, leaders: Optional[BoundedArchive], termination_criterion: TerminationCriterion = store. default_termination_criteria, swarm_generator: Generator = store.default_generator, swarm_evaluator: Evaluator = store.default_evaluator): """ This class implements the SMPSO algorithm as described in * SMPSO: A new PSO-based metaheuristic for multi-objective optimization * MCDM 2009. DOI: `<http://dx.doi.org/10.1109/MCDM.2009.4938830/>`_. The implementation of SMPSO provided in jMetalPy follows the algorithm template described in the algorithm templates section of the documentation. :param problem: The problem to solve. :param swarm_size: Size of the swarm. :param max_evaluations: Maximum number of evaluations/iterations. :param mutation: Mutation operator (see :py:mod:`jmetal.operator.mutation`). :param leaders: Archive for leaders. """ super(SMPSO, self).__init__(problem=problem, swarm_size=swarm_size) self.swarm_generator = swarm_generator self.swarm_evaluator = swarm_evaluator self.termination_criterion = termination_criterion self.observable.register(termination_criterion) self.mutation_operator = mutation self.leaders = leaders self.c1_min = 1.5 self.c1_max = 2.5 self.c2_min = 1.5 self.c2_max = 2.5 self.r1_min = 0.0 self.r1_max = 1.0 self.r2_min = 0.0 self.r2_max = 1.0 self.min_weight = 0.1 self.max_weight = 0.1 self.change_velocity1 = -1 self.change_velocity2 = -1 self.dominance_comparator = DominanceComparator() self.speed = numpy.zeros( (self.swarm_size, self.problem.number_of_variables), dtype=float) self.delta_max, self.delta_min = numpy.empty(problem.number_of_variables), \ numpy.empty(problem.number_of_variables)
def __init__( self, problem: Problem, population_size: int, mutation: Mutation, crossover: Crossover, number_of_cores: int, client: Client, selection: Selection = BinaryTournamentSelection( MultiComparator([ FastNonDominatedRanking.get_comparator(), CrowdingDistance.get_comparator() ])), termination_criterion: TerminationCriterion = store. default_termination_criteria, dominance_comparator: DominanceComparator = DominanceComparator()): super(DistributedNSGAII, self).__init__() self.problem = problem self.population_size = population_size self.mutation_operator = mutation self.crossover_operator = crossover self.selection_operator = selection self.dominance_comparator = dominance_comparator self.termination_criterion = termination_criterion self.observable.register(termination_criterion) self.number_of_cores = number_of_cores self.client = client
def __init__( self, problem: DynamicProblem[S], population_size: int, offspring_population_size: int, mutation: Mutation, crossover: Crossover, selection: Selection = BinaryTournamentSelection( MultiComparator([ FastNonDominatedRanking.get_comparator(), CrowdingDistance.get_comparator() ])), termination_criterion: TerminationCriterion = store. default_termination_criteria, population_generator: Generator = store.default_generator, population_evaluator: Evaluator = store.default_evaluator, dominance_comparator: DominanceComparator = DominanceComparator()): super(DynamicNSGAII, self).__init__( problem=problem, population_size=population_size, offspring_population_size=offspring_population_size, mutation=mutation, crossover=crossover, selection=selection, population_evaluator=population_evaluator, population_generator=population_generator, termination_criterion=termination_criterion, dominance_comparator=dominance_comparator) self.completed_iterations = 0 self.start_computing_time = 0 self.total_computing_time = 0
def __init__(self, max_population_size: int, reference_point: S, dominance_comparator: Comparator = DominanceComparator()): super(RankingAndFitnessSelection, self).__init__() self.max_population_size = max_population_size self.dominance_comparator = dominance_comparator self.reference_point = reference_point
def execute(self, front: List[S]) -> S: if front is None: raise Exception('The front is null') elif len(front) == 0: raise Exception('The front is empty') result = front[0] for solution in front[1:]: if DominanceComparator().compare(solution, result) < 0: result = solution return result
def __init__(self, problem: DynamicProblem, population_size: int, cr: float, f: float, termination_criterion: TerminationCriterion, k: float = 0.5, population_generator: Generator = store.default_generator, population_evaluator: Evaluator = store.default_evaluator, dominance_comparator: Comparator = DominanceComparator()): super(DynamicGDE3, self).__init__(problem, population_size, cr, f, termination_criterion, k, population_generator, population_evaluator, dominance_comparator) self.completed_iterations = 0
def default_comparator(self): return DominanceComparator()
def configurar_algoritmo(self, hora_show: datetime, hora_minima: datetime = datetime.time(0, 0), algoritmo: str = 'MOGA', mutation_probability: float = 0.25, max_evaluations: int = 500, population: int = 100): hora_minima, hora_show = self.restricciones_hora( hora_minima, hora_show) if (hora_minima == 0): hora_minima = hora_show - 180 restricciones_baja = list([hora_minima, -100, -100, 0, 0]) restricciones_alta = list([hora_show, 0, 0, 100, 100]) self.problem = HVAC(lower_bound=restricciones_baja, upper_bound=restricciones_alta, number_of_configurations=3) print("algoritmo: ", algoritmo) if algoritmo == 'MOGA': algorithm = MOGA(problem=self.problem, population_size=population, offspring_population_size=population, mutation=IntegerPolynomialMutationD( probability=mutation_probability, distribution_index=20), crossover=SBXCrossoverD( probability=mutation_probability, distribution_index=20), termination_criterion=StoppingByEvaluations( max=max_evaluations), dominance_comparator=DominanceComparator()) elif algoritmo == "NSGAII": algorithm = NSGAII(problem=self.problem, population_size=population, offspring_population_size=population, mutation=IntegerPolynomialMutationD( probability=mutation_probability, distribution_index=20), crossover=SBXCrossoverD( probability=mutation_probability, distribution_index=20), termination_criterion=StoppingByEvaluations( max=max_evaluations), dominance_comparator=DominanceComparator()) elif algoritmo == 'OMOPSO': algorithm = OMOPSO(problem=self.problem, swarm_size=population, epsilon=0.0075, uniform_mutation=UniformMutation( probability=mutation_probability, perturbation=0.5), non_uniform_mutation=NonUniformMutation( probability=mutation_probability, perturbation=0.5), leaders=CrowdingDistanceArchive(100), termination_criterion=StoppingByEvaluations( max=max_evaluations)) elif algoritmo == 'SMPSO': algorithm = SMPSO(problem=self.problem, swarm_size=population, mutation=IntegerPolynomialMutation( probability=mutation_probability, distribution_index=20), leaders=CrowdingDistanceArchive(100), termination_criterion=StoppingByEvaluations( max=max_evaluations)) elif algoritmo == 'SPEA2': algorithm = SPEA2(problem=self.problem, population_size=population, offspring_population_size=population, mutation=IntegerPolynomialMutationD( probability=mutation_probability, distribution_index=20), crossover=SBXCrossoverD( probability=mutation_probability, distribution_index=20), termination_criterion=StoppingByEvaluations( max=max_evaluations), dominance_comparator=DominanceComparator()) else: print("Algoritmo no válido. Creando MOGA por defecto...") algorithm = MOGA(problem=self.problem, population_size=population, offspring_population_size=population, mutation=IntegerPolynomialMutationD( probability=mutation_probability, distribution_index=20), crossover=SBXCrossoverD( probability=mutation_probability, distribution_index=20), termination_criterion=StoppingByEvaluations( max=max_evaluations), dominance_comparator=DominanceComparator()) self.algoritmo = algorithm self.algoritmo.observable.register(observer=ProgressBarObserver( max=max_evaluations)) return algorithm
def __init__(self, comparator: Comparator = DominanceComparator()): super(FastNonDominatedRanking, self).__init__(comparator)
def __init__(self, comparator: Comparator = DominanceComparator()): super(StrengthRanking, self).__init__(comparator)
def __init__(self, comparator: Comparator = DominanceComparator()): super(Ranking, self).__init__() self.number_of_comparisons = 0 self.ranked_sublists = [] self.comparator = comparator
class SMPSO(ParticleSwarmOptimization): def __init__(self, problem: FloatProblem, swarm_size: int, mutation: Mutation, leaders: Optional[BoundedArchive], termination_criterion: TerminationCriterion = store. default_termination_criteria, swarm_generator: Generator = store.default_generator, swarm_evaluator: Evaluator = store.default_evaluator): """ This class implements the SMPSO algorithm as described in * SMPSO: A new PSO-based metaheuristic for multi-objective optimization * MCDM 2009. DOI: `<http://dx.doi.org/10.1109/MCDM.2009.4938830/>`_. The implementation of SMPSO provided in jMetalPy follows the algorithm template described in the algorithm templates section of the documentation. :param problem: The problem to solve. :param swarm_size: Size of the swarm. :param max_evaluations: Maximum number of evaluations/iterations. :param mutation: Mutation operator (see :py:mod:`jmetal.operator.mutation`). :param leaders: Archive for leaders. """ super(SMPSO, self).__init__(problem=problem, swarm_size=swarm_size) self.swarm_generator = swarm_generator self.swarm_evaluator = swarm_evaluator self.termination_criterion = termination_criterion self.observable.register(termination_criterion) self.mutation_operator = mutation self.leaders = leaders self.c1_min = 1.5 self.c1_max = 2.5 self.c2_min = 1.5 self.c2_max = 2.5 self.r1_min = 0.0 self.r1_max = 1.0 self.r2_min = 0.0 self.r2_max = 1.0 self.min_weight = 0.1 self.max_weight = 0.1 self.change_velocity1 = -1 self.change_velocity2 = -1 self.dominance_comparator = DominanceComparator() self.speed = numpy.zeros( (self.swarm_size, self.problem.number_of_variables), dtype=float) self.delta_max, self.delta_min = numpy.empty(problem.number_of_variables), \ numpy.empty(problem.number_of_variables) def create_initial_solutions(self) -> List[FloatSolution]: return [ self.swarm_generator.new(self.problem) for _ in range(self.swarm_size) ] def evaluate(self, solution_list: List[FloatSolution]): return self.swarm_evaluator.evaluate(solution_list, self.problem) def stopping_condition_is_met(self) -> bool: return self.termination_criterion.is_met def initialize_global_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: self.leaders.add(copy(particle)) def initialize_particle_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: particle.attributes['local_best'] = copy(particle) def initialize_velocity(self, swarm: List[FloatSolution]) -> None: for i in range(self.problem.number_of_variables): self.delta_max[i] = (self.problem.upper_bound[i] - self.problem.lower_bound[i]) / 2.0 self.delta_min = -1.0 * self.delta_max def update_velocity(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): best_particle = copy(swarm[i].attributes['local_best']) best_global = self.select_global_best() r1 = round(random.uniform(self.r1_min, self.r1_max), 1) r2 = round(random.uniform(self.r2_min, self.r2_max), 1) c1 = round(random.uniform(self.c1_min, self.c1_max), 1) c2 = round(random.uniform(self.c2_min, self.c2_max), 1) wmax = self.max_weight wmin = self.min_weight for var in range(swarm[i].number_of_variables): self.speed[i][var] = \ self.__velocity_constriction( self.__constriction_coefficient(c1, c2) * ((self.__inertia_weight(wmax) * self.speed[i][var]) + (c1 * r1 * (best_particle.variables[var] - swarm[i].variables[var])) + (c2 * r2 * (best_global.variables[var] - swarm[i].variables[var])) ), self.delta_max, self.delta_min, var) def update_position(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): particle = swarm[i] for j in range(particle.number_of_variables): particle.variables[j] += self.speed[i][j] if particle.variables[j] < self.problem.lower_bound[j]: particle.variables[j] = self.problem.lower_bound[j] self.speed[i][j] *= self.change_velocity1 if particle.variables[j] > self.problem.upper_bound[j]: particle.variables[j] = self.problem.upper_bound[j] self.speed[i][j] *= self.change_velocity2 def update_global_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: self.leaders.add(copy(particle)) def update_particle_best(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): flag = self.dominance_comparator.compare( swarm[i], swarm[i].attributes['local_best']) if flag != 1: swarm[i].attributes['local_best'] = copy(swarm[i]) def perturbation(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): if (i % 6) == 0: self.mutation_operator.execute(swarm[i]) def select_global_best(self) -> FloatSolution: leaders = self.leaders.solution_list if len(leaders) > 2: particles = random.sample(leaders, 2) if self.leaders.comparator.compare(particles[0], particles[1]) < 1: best_global = copy(particles[0]) else: best_global = copy(particles[1]) else: best_global = copy(self.leaders.solution_list[0]) return best_global def __velocity_constriction(self, value: float, delta_max: [], delta_min: [], variable_index: int) -> float: result = value if value > delta_max[variable_index]: result = delta_max[variable_index] if value < delta_min[variable_index]: result = delta_min[variable_index] return result def __inertia_weight(self, wmax: float): return wmax def __constriction_coefficient(self, c1: float, c2: float) -> float: rho = c1 + c2 if rho <= 4: result = 1.0 else: result = 2.0 / (2.0 - rho - sqrt(pow(rho, 2.0) - 4.0 * rho)) return result def init_progress(self) -> None: self.evaluations = self.swarm_size self.leaders.compute_density_estimator() self.initialize_velocity(self.solutions) self.initialize_particle_best(self.solutions) self.initialize_global_best(self.solutions) def update_progress(self) -> None: self.evaluations += self.swarm_size self.leaders.compute_density_estimator() observable_data = self.get_observable_data() observable_data['SOLUTIONS'] = self.leaders.solution_list self.observable.notify_all(**observable_data) def get_result(self) -> List[FloatSolution]: return self.leaders.solution_list def get_name(self) -> str: return 'SMPSO'
def setUp(self): self.comparator = DominanceComparator()
def __init__(self, dominance_comparator=DominanceComparator()): super(NonDominatedSolutionListArchive, self).__init__() self.comparator = dominance_comparator
def __init__(self, max_population_size: int, dominance_comparator: Comparator = DominanceComparator()): super(RankingAndCrowdingDistanceSelection, self).__init__() self.max_population_size = max_population_size self.dominance_comparator = dominance_comparator
class OMOPSO(ParticleSwarmOptimization): def __init__(self, problem: FloatProblem, swarm_size: int, uniform_mutation: UniformMutation, non_uniform_mutation: NonUniformMutation, leaders: Optional[BoundedArchive], epsilon: float, termination_criterion: TerminationCriterion, swarm_generator: Generator = store.default_generator, swarm_evaluator: Evaluator = store.default_evaluator): """ This class implements the OMOPSO algorithm as described in todo Update this reference * SMPSO: A new PSO-based metaheuristic for multi-objective optimization The implementation of OMOPSO provided in jMetalPy follows the algorithm template described in the algorithm templates section of the documentation. :param problem: The problem to solve. :param swarm_size: Size of the swarm. :param leaders: Archive for leaders. """ super(OMOPSO, self).__init__(problem=problem, swarm_size=swarm_size) self.swarm_generator = swarm_generator self.swarm_evaluator = swarm_evaluator self.termination_criterion = termination_criterion self.observable.register(termination_criterion) self.uniform_mutation = uniform_mutation self.non_uniform_mutation = non_uniform_mutation self.leaders = leaders self.epsilon = epsilon self.epsilon_archive = NonDominatedSolutionListArchive( EpsilonDominanceComparator(epsilon)) self.c1_min = 1.5 self.c1_max = 2.0 self.c2_min = 1.5 self.c2_max = 2.0 self.r1_min = 0.0 self.r1_max = 1.0 self.r2_min = 0.0 self.r2_max = 1.0 self.weight_min = 0.1 self.weight_max = 0.5 self.change_velocity1 = -1 self.change_velocity2 = -1 self.dominance_comparator = DominanceComparator() self.speed = numpy.zeros( (self.swarm_size, self.problem.number_of_variables), dtype=float) def create_initial_solutions(self) -> List[FloatSolution]: return [ self.swarm_generator.new(self.problem) for _ in range(self.swarm_size) ] def evaluate(self, solution_list: List[FloatSolution]): return self.swarm_evaluator.evaluate(solution_list, self.problem) def stopping_condition_is_met(self) -> bool: return self.termination_criterion.is_met def initialize_global_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: if self.leaders.add(particle): self.epsilon_archive.add(copy(particle)) def initialize_particle_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: particle.attributes['local_best'] = copy(particle) def initialize_velocity(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): for j in range(self.problem.number_of_variables): self.speed[i][j] = 0.0 def update_velocity(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): best_particle = copy(swarm[i].attributes['local_best']) best_global = self.select_global_best() r1 = round(random.uniform(self.r1_min, self.r1_max), 1) r2 = round(random.uniform(self.r2_min, self.r2_max), 1) c1 = round(random.uniform(self.c1_min, self.c1_max), 1) c2 = round(random.uniform(self.c2_min, self.c2_max), 1) w = round(random.uniform(self.weight_min, self.weight_max), 1) for var in range(swarm[i].number_of_variables): self.speed[i][var] = w * self.speed[i][var] \ + (c1 * r1 * (best_particle.variables[var] - swarm[i].variables[var])) \ + (c2 * r2 * (best_global.variables[var] - swarm[i].variables[var])) def update_position(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): particle = swarm[i] for j in range(particle.number_of_variables): particle.variables[j] += self.speed[i][j] if particle.variables[j] < self.problem.lower_bound[j]: particle.variables[j] = self.problem.lower_bound[j] self.speed[i][j] *= self.change_velocity1 if particle.variables[j] > self.problem.upper_bound[j]: particle.variables[j] = self.problem.upper_bound[j] self.speed[i][j] *= self.change_velocity2 def update_global_best(self, swarm: List[FloatSolution]) -> None: for particle in swarm: if self.leaders.add(copy(particle)): self.epsilon_archive.add(copy(particle)) def update_particle_best(self, swarm: List[FloatSolution]) -> None: for i in range(self.swarm_size): flag = self.dominance_comparator.compare( swarm[i], swarm[i].attributes['local_best']) if flag != 1: swarm[i].attributes['local_best'] = copy(swarm[i]) def perturbation(self, swarm: List[FloatSolution]) -> None: self.non_uniform_mutation.set_current_iteration(self.evaluations / self.swarm_size) for i in range(self.swarm_size): if (i % 3) == 0: self.non_uniform_mutation.execute(swarm[i]) else: self.uniform_mutation.execute(swarm[i]) def select_global_best(self) -> FloatSolution: leaders = self.leaders.solution_list if len(leaders) > 2: particles = random.sample(leaders, 2) if self.leaders.comparator.compare(particles[0], particles[1]) < 1: best_global = copy(particles[0]) else: best_global = copy(particles[1]) else: best_global = copy(self.leaders.solution_list[0]) return best_global def __velocity_constriction(self, value: float, delta_max: [], delta_min: [], variable_index: int) -> float: result = value if value > delta_max[variable_index]: result = delta_max[variable_index] if value < delta_min[variable_index]: result = delta_min[variable_index] return result def __inertia_weight(self, wmax: float): return wmax def __constriction_coefficient(self, c1: float, c2: float) -> float: rho = c1 + c2 if rho <= 4: result = 1.0 else: result = 2.0 / (2.0 - rho - sqrt(pow(rho, 2.0) - 4.0 * rho)) return result def init_progress(self) -> None: self.evaluations = self.swarm_size self.leaders.compute_density_estimator() self.initialize_velocity(self.solutions) self.initialize_particle_best(self.solutions) self.initialize_global_best(self.solutions) def update_progress(self) -> None: self.evaluations += self.swarm_size self.leaders.compute_density_estimator() observable_data = self.get_observable_data() observable_data['SOLUTIONS'] = self.epsilon_archive.solution_list self.observable.notify_all(**observable_data) def get_result(self) -> List[FloatSolution]: return self.epsilon_archive.solution_list def get_name(self) -> str: return 'OMOPSO'
def __init__(self, comparator: Comparator = DominanceComparator()): super(BinaryTournamentSelection, self).__init__() self.comparator = comparator
from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == '__main__': problem = Rastrigin(10) max_evaluations = 50000 algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20.0), crossover=SBXCrossover(probability=0.9, distribution_index=20.0), termination_criterion=StoppingByEvaluations(max=max_evaluations), dominance_comparator=DominanceComparator()) algorithm.observable.register(observer=PrintObjectivesObserver(1000)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file( front, 'FUN.' + algorithm.get_name() + "-" + problem.get_name()) print_variables_to_file( front, 'VAR.' + algorithm.get_name() + "-" + problem.get_name()) print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time))
class DominanceComparatorTestCases(unittest.TestCase): def setUp(self): self.comparator = DominanceComparator() def test_should_dominance_comparator_raise_an_exception_if_the_first_solution_is_null( self): solution = None solution2 = Solution(2, 2) with self.assertRaises(Exception): self.comparator.compare(solution, solution2) def test_should_dominance_comparator_raise_an_exception_if_the_second_solution_is_null( self): solution = Solution(2, 3) solution2 = None with self.assertRaises(Exception): self.comparator.compare(solution, solution2) def test_should_dominance_comparator_return_zero_if_the_two_solutions_have_one_objective_with_the_same_value( self): solution = Solution(1, 1) solution2 = Solution(1, 1) solution.objectives = [1.0] solution2.objectives = [1.0] self.assertEqual(0, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_return_one_if_the_two_solutions_have_one_objective_and_the_second_one_is_lower( self): solution = Solution(1, 1) solution2 = Solution(1, 1) solution.objectives = [2.0] solution2.objectives = [1.0] self.assertEqual(1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_return_minus_one_if_the_two_solutions_have_one_objective_and_the_first_one_is_lower( self): solution = Solution(1, 1) solution2 = Solution(1, 1) solution.objectives = [1.0] solution2.objectives = [2.0] self.assertEqual(-1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_case_a(self): """ Case A: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [2.0, 6.0, 15.0] """ solution = Solution(1, 3) solution2 = Solution(1, 3) solution.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [2.0, 6.0, 15.0] self.assertEqual(-1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_case_b(self): """ Case b: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [-1.0, 5.0, 10.0] """ solution = Solution(1, 3) solution2 = Solution(1, 3) solution.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-1.0, 5.0, 10.0] self.assertEqual(-1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_case_c(self): """ Case c: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [-2.0, 5.0, 9.0] """ solution = Solution(1, 3) solution2 = Solution(1, 3) solution.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-2.0, 5.0, 9.0] self.assertEqual(1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_case_d(self): """ Case d: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [-1.0, 5.0, 8.0] """ solution = Solution(1, 3) solution2 = Solution(1, 3) solution.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-1.0, 5.0, 8.0] self.assertEqual(1, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_case_3(self): """ Case d: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [-2.0, 5.0, 10.0] """ solution = Solution(1, 3) solution2 = Solution(1, 3) solution.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-2.0, 5.0, 10.0] self.assertEqual(0, self.comparator.compare(solution, solution2)) def test_should_dominance_comparator_work_properly_with_constrains_case_1( self): """ Case 1: solution1 has a higher degree of constraint violation than solution 2 """ solution1 = Solution(1, 3, 1) solution2 = Solution(1, 3, 1) solution1.constraints[0] = -0.1 solution2.constraints[0] = -0.3 solution1.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-2.0, 5.0, 10.0] self.assertEqual(-1, self.comparator.compare(solution1, solution2)) def test_should_dominance_comparator_work_properly_with_constrains_case_2( self): """ Case 2: solution1 has a lower degree of constraint violation than solution 2 """ solution1 = Solution(1, 3, 1) solution2 = Solution(1, 3, 1) solution1.constraints[0] = -0.3 solution2.constraints[0] = -0.1 solution1.objectives = [-1.0, 5.0, 9.0] solution2.objectives = [-2.0, 5.0, 10.0] self.assertEqual(1, self.comparator.compare(solution1, solution2))