Source code for jmetal.problem.multiobjective.unconstrained
import random
from math import sqrt, exp, pow, sin
from jmetal.core.problem import FloatProblem, BinaryProblem
from jmetal.core.solution import FloatSolution, BinarySolution
"""
.. module:: constrained
:platform: Unix, Windows
:synopsis: Unconstrained test problems for multi-objective optimization
.. moduleauthor:: Antonio J. Nebro <antonio@lcc.uma.es>
"""
[docs]class Kursawe(FloatProblem):
""" Class representing problem Kursawe. """
def __init__(self, number_of_variables: int = 3):
super(Kursawe, self).__init__()
self.number_of_objectives = 2
self.number_of_variables = number_of_variables
self.number_of_constraints = 0
self.obj_directions = [self.MINIMIZE, self.MINIMIZE]
self.obj_labels = ['f(x)', 'f(y)']
self.lower_bound = [-5.0 for _ in range(number_of_variables)]
self.upper_bound = [5.0 for _ in range(number_of_variables)]
FloatSolution.lower_bound = self.lower_bound
FloatSolution.upper_bound = self.upper_bound
[docs] def evaluate(self, solution: FloatSolution) -> FloatSolution:
fx = [0.0 for _ in range(self.number_of_objectives)]
for i in range(self.number_of_variables - 1):
xi = solution.variables[i] * solution.variables[i]
xj = solution.variables[i + 1] * solution.variables[i + 1]
aux = -0.2 * sqrt(xi + xj)
fx[0] += -10 * exp(aux)
fx[1] += pow(abs(solution.variables[i]), 0.8) + 5.0 * sin(pow(solution.variables[i], 3.0))
solution.objectives[0] = fx[0]
solution.objectives[1] = fx[1]
return solution
[docs] def get_name(self):
return 'Kursawe'
[docs]class Fonseca(FloatProblem):
def __init__(self):
super(Fonseca, self).__init__()
self.number_of_variables = 3
self.number_of_objectives = 2
self.number_of_constraints = 0
self.obj_directions = [self.MINIMIZE, self.MINIMIZE]
self.obj_labels = ['f(x)', 'f(y)']
self.lower_bound = self.number_of_variables * [-4]
self.upper_bound = self.number_of_variables * [4]
FloatSolution.lower_bound = self.lower_bound
FloatSolution.upper_bound = self.upper_bound
[docs] def evaluate(self, solution: FloatSolution) -> FloatSolution:
n = self.number_of_variables
solution.objectives[0] = 1 - exp(-sum([(x - 1.0 / n ** 0.5) ** 2 for x in solution.variables]))
solution.objectives[1] = 1 - exp(-sum([(x + 1.0 / n ** 0.5) ** 2 for x in solution.variables]))
return solution
[docs] def get_name(self):
return 'Fonseca'
[docs]class Schaffer(FloatProblem):
def __init__(self):
super(Schaffer, self).__init__()
self.number_of_variables = 1
self.number_of_objectives = 2
self.number_of_constraints = 0
self.obj_directions = [self.MINIMIZE, self.MINIMIZE]
self.obj_labels = ['f(x)', 'f(y)']
self.lower_bound = [-100000]
self.upper_bound = [100000]
FloatSolution.lower_bound = self.lower_bound
FloatSolution.upper_bound = self.upper_bound
[docs] def evaluate(self, solution: FloatSolution) -> FloatSolution:
value = solution.variables[0]
solution.objectives[0] = value ** 2
solution.objectives[1] = (value - 2) ** 2
return solution
[docs] def get_name(self):
return 'Schaffer'
[docs]class Viennet2(FloatProblem):
def __init__(self):
super(Viennet2, self).__init__()
self.number_of_variables = 2
self.number_of_objectives = 3
self.number_of_constraints = 0
self.obj_directions = [self.MINIMIZE, self.MINIMIZE, self.MINIMIZE]
self.obj_labels = ['f(x)', 'f(y)', 'f(z)']
self.lower_bound = self.number_of_variables * [-4]
self.upper_bound = self.number_of_variables * [4]
FloatSolution.lower_bound = self.lower_bound
FloatSolution.upper_bound = self.upper_bound
[docs] def evaluate(self, solution: FloatSolution) -> FloatSolution:
x0 = solution.variables[0]
x1 = solution.variables[1]
f1 = (x0 - 2) * (x0 - 2) / 2.0 + (x1 + 1) * (x1 + 1) / 13.0 + 3.0
f2 = (x0 + x1 - 3.0) * (x0 + x1 - 3.0) / 36.0 + (-x0 + x1 + 2.0) * (-x0 + x1 + 2.0) / 8.0 - 17.0
f3 = (x0 + 2 * x1 - 1) * (x0 + 2 * x1 - 1) / 175.0 + (2 * x1 - x0) * (2 * x1 - x0) / 17.0 - 13.0
solution.objectives[0] = f1
solution.objectives[1] = f2
solution.objectives[2] = f3
return solution
[docs] def get_name(self):
return 'Viennet2'
[docs]class SubsetSum(BinaryProblem):
def __init__(self, C: int, W: list):
""" The goal is to find a subset S of W whose elements sum is closest to (without exceeding) C.
:param C: Large integer.
:param W: Set of non-negative integers."""
super(SubsetSum, self).__init__()
self.C = C
self.W = W
self.number_of_bits = len(self.W)
self.number_of_objectives = 2
self.number_of_variables = 1
self.number_of_constraints = 0
self.obj_directions = [self.MAXIMIZE, self.MINIMIZE]
self.obj_labels = ['Sum', 'No. of Objects']
[docs] def evaluate(self, solution: BinarySolution) -> BinarySolution:
total_sum = 0.0
number_of_objects = 0
for index, bits in enumerate(solution.variables[0]):
if bits:
total_sum += self.W[index]
number_of_objects += 1
if total_sum > self.C:
total_sum = self.C - total_sum * 0.1
if total_sum < 0.0:
total_sum = 0.0
solution.objectives[0] = -1.0 * total_sum
solution.objectives[1] = number_of_objects
return solution
[docs] def create_solution(self) -> BinarySolution:
new_solution = BinarySolution(number_of_variables=self.number_of_variables,
number_of_objectives=self.number_of_objectives)
new_solution.variables[0] = \
[True if random.randint(0, 1) == 0 else False for _ in range(self.number_of_bits)]
return new_solution
[docs] def get_name(self) -> str:
return 'Subset Sum'
[docs]class OneZeroMax(BinaryProblem):
def __init__(self, number_of_bits: int = 256):
super(OneZeroMax, self).__init__()
self.number_of_bits = number_of_bits
self.number_of_objectives = 2
self.number_of_variables = 1
self.number_of_constraints = 0
self.obj_directions = [self.MINIMIZE]
self.obj_labels = ['Ones']
[docs] def evaluate(self, solution: BinarySolution) -> BinarySolution:
counter_of_ones = 0
counter_of_zeroes = 0
for bits in solution.variables[0]:
if bits:
counter_of_ones += 1
else:
counter_of_zeroes += 1
solution.objectives[0] = -1.0 * counter_of_ones
solution.objectives[1] = -1.0 * counter_of_zeroes
return solution
[docs] def create_solution(self) -> BinarySolution:
new_solution = BinarySolution(number_of_variables=self.number_of_variables,
number_of_objectives=self.number_of_objectives)
new_solution.variables[0] = \
[True if random.randint(0, 1) == 0 else False for _ in range(self.number_of_bits)]
return new_solution
[docs] def get_name(self) -> str:
return 'OneZeroMax'