Skip to content

Commit

Permalink
Added the ability to calculate all criteria using one model
Browse files Browse the repository at this point in the history
  • Loading branch information
LebedevIlyaG committed May 8, 2024
1 parent a12ad34 commit 391a42f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
26 changes: 26 additions & 0 deletions examples/Machine_learning/SVC/_2D/Problems/mco_breast_cancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,29 @@ def calculate(self, point: Point, function_value: FunctionValue) -> FunctionValu

function_value.value = result
return function_value

def calculateAllFunction(self, point: Point, function_values: np.ndarray(shape=(1), dtype=FunctionValue)) -> \
np.ndarray(shape=(1), dtype=FunctionValue):
"""
Вычисление значения выбранной функции в заданной точке.
:param point: координаты точки испытания, в которой будет вычислено значение функции
:param function_values: массив объектов, определяющих номера функций в задаче и хранящий значения функций
:return: массив вычисленных значений функций в точке point
"""
x = point.float_variables

svc_c = 10 ** x[0]
gamma = 10 ** x[1]

classifier_obj = SVC(C=svc_c, gamma=gamma)
classifier_obj.fit(self.X_train, self.y_train)

# OBJECTIV 1
function_values[0].value = - cross_val_score(classifier_obj, self.X, self.y, n_jobs=4,
scoring='precision').mean()
# OBJECTIV 2
function_values[1].value = - cross_val_score(classifier_obj, self.X, self.y, n_jobs=4,
scoring='recall').mean()

return function_values
2 changes: 1 addition & 1 deletion iOpt/method/mco_method_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def calculate_functionals(self, point: SearchDataItem) -> SearchDataItem:

for i in range(self.task.problem.number_of_objectives):
point.function_values[number_of_constraints + i] = FunctionValue(FunctionType.OBJECTIV, i)
point = self.task.calculate(point, number_of_constraints + i)
point = self.task.calculate(point, -1)

point = self.task.calculate(point, -1, TypeOfCalculation.CONVOLUTION)
point.set_index(number_of_constraints)
Expand Down
16 changes: 12 additions & 4 deletions iOpt/method/mco_optim_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,18 @@ def calculate(self,
) -> SearchDataItem:
"""Compute selected function by number."""
if calculation_type == TypeOfCalculation.FUNCTION:
data_item.function_values[self.perm[function_index]] = \
self.problem.calculate(data_item.point, data_item.function_values[self.perm[function_index]])
if not np.isfinite(data_item.function_values[self.perm[function_index]].value):
raise Exception("Infinity values")
if function_index == -1: # Calculate all criteria
data_item.function_values = self.problem.calculateAllFunction(data_item.point,
data_item.function_values)
for i in range(self.problem.number_of_objectives):
if not np.isfinite(data_item.function_values[self.perm[self.problem.number_of_constraints +
i]].value):
raise Exception("Infinity values")
else:
data_item.function_values[self.perm[function_index]] = \
self.problem.calculate(data_item.point, data_item.function_values[self.perm[function_index]])
if not np.isfinite(data_item.function_values[self.perm[function_index]].value):
raise Exception("Infinity values")

elif calculation_type == TypeOfCalculation.CONVOLUTION:
data_item = self.convolution.calculate_convolution(data_item, self.min_value, self.max_value)
Expand Down
13 changes: 13 additions & 0 deletions iOpt/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def calculate(self, point: Point, function_value: FunctionValue) -> FunctionValu
:return: Calculated value of the function."""
pass

def calculateAllFunction(self, point: Point, function_values: np.ndarray(shape=(1), dtype=FunctionValue)) -> \
np.ndarray(shape=(1), dtype=FunctionValue):
"""
Calculate all functions at a given point.
For any new problem statement that inherits from :class:`Problem`, this method should be overloaded
:return: Calculated values of the functions."""
for i in range(self.number_of_objectives):
function_values[i].value = self.calculate(point, function_values[i])

return function_values


# @abstractmethod
def get_name(self):
"""
Expand Down
15 changes: 15 additions & 0 deletions problems/mco_test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ def calculate(self, point: Point, function_value: FunctionValue) -> FunctionValu
function_value.value = result
return function_value

def calculateAllFunction(self, point: Point, function_values: np.ndarray(shape=(1), dtype=FunctionValue)) -> \
np.ndarray(shape=(1), dtype=FunctionValue):
"""
Calculate all function at a given point.
For any new problem statement that inherits from :class:`Problem`, this method should be overloaded
:return: Calculated values of the functions."""
x = point.float_variables

# OBJECTIVE 1
function_values[0].value = np.double((x[0]-1)*x[1]*x[1]+1)
# OBJECTIVE 2
function_values[1].value = np.double(x[1])

return function_values

0 comments on commit 391a42f

Please sign in to comment.