Skip to content

Commit

Permalink
rename to PEP 8 (#151)
Browse files Browse the repository at this point in the history
* rename 1

* rename 2

* rename 4

* rename 6

* rename 6
  • Loading branch information
kozinove authored Jul 10, 2023
1 parent 59daec7 commit 2639bb0
Show file tree
Hide file tree
Showing 118 changed files with 2,492 additions and 2,492 deletions.
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,26 @@ if __name__ == "__main__":
"""
Minimization of the Rastrigin test function with visualization
"""
#Create a test task
# Create a test task
problem = Rastrigin(2)
#Setup a solver options
params = SolverParameters(r=2.5, eps=0.01, itersLimit=300, refineSolution=True)
#Create the solver
# Setup a solver options
params = SolverParameters(r=2.5, eps=0.01, iters_limit=300, refine_solution=True)
# Create the solver
solver = Solver(problem, parameters=params)
#Print results to console while solving
# Print results to console while solving
cfol = ConsoleOutputListener(mode='full')
solver.AddListener(cfol)
#3D visualization at the end of the solution
spl = StaticPainterNDListener("rastrigin.png", "output", varsIndxs=[0,1], mode="surface", calc="interpolation")
solver.AddListener(spl)
#Run problem solution
sol = solver.Solve()
solver.add_listener(cfol)
# 3D visualization at the end of the solution
spl = StaticPainterNDListener("rastrigin.png", "output", vars_indxs=[0, 1], mode="surface", calc="interpolation")
solver.add_listener(spl)
# Run problem solution
sol = solver.solve()
```

# **Examples**

Let’s demonstrate the use of the iOpt framework when tuning the hyperparameters of one of the machine learning methods. In the support vector machine ([SVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)), we find the optimal hyperparameters (the regularization parameter **C**, the kernel coefficient **gamma**) in the problem of breast cancer classification ([detailed description of the data](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic))).


```python
import numpy as np
from sklearn.utils import shuffle
Expand Down Expand Up @@ -140,19 +139,19 @@ if __name__ == "__main__":

problem = SVC_2d.SVC_2D(x, y, regularization_value_bound, kernel_coefficient_bound)

method_params = SolverParameters(r=np.double(3.0), itersLimit=100)
method_params = SolverParameters(r=np.double(3.0), iters_limit=100)
solver = Solver(problem, parameters=method_params)

apl = AnimatePainterNDListener("svc2d_anim.png", "output", varsIndxs=[0, 1], toPaintObjFunc=False)
solver.AddListener(apl)
apl = AnimatePainterNDListener("svc2d_anim.png", "output", vars_indxs=[0, 1], to_paint_obj_func=False)
solver.add_listener(apl)

spl = StaticPainterNDListener("svc2d_stat.png", "output", vars_indxs=[0, 1], mode="surface", calc="interpolation")
solver.add_listener(spl)

spl = StaticPainterNDListener("svc2d_stat.png", "output", varsIndxs=[0, 1], mode="surface", calc="interpolation")
solver.AddListener(spl)

cfol = ConsoleOutputListener(mode='full')
solver.AddListener(cfol)
solver.add_listener(cfol)

solver_info = solver.Solve()
solver_info = solver.solve()

```

Expand Down
71 changes: 35 additions & 36 deletions README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ from iOpt.method.listener import StaticNDPaintListener, ConsoleFullOutputListene
from subprocess import Popen, PIPE, STDOUT

if __name__ == "__main__":
"""
Минимизация функции Растригина и визуализация
"""
#Создание тестовой задачи
problem = Rastrigin(2)
#Установка параметров поиска оптимального решения
params = SolverParameters(r=2.5, eps=0.01, itersLimit=300, refineSolution=True)
#Создание решателя
solver = Solver(problem, parameters=params)
#Вывод результатов на консоль в процессе решения
cfol = ConsoleFullOutputListener(mode='full')
solver.AddListener(cfol)
#3D визуализация по окончании решения
spl = StaticNDPaintListener("rastrigin.png", "output", varsIndxs=[0,1], mode="surface", calc="interpolation")
solver.AddListener(spl)
#Запуск решения задачи
sol = solver.Solve()
"""
Минимизация функции Растригина и визуализация
"""
# Создание тестовой задачи
problem = Rastrigin(2)
# Установка параметров поиска оптимального решения
params = SolverParameters(r=2.5, eps=0.01, iters_limit=300, refine_solution=True)
# Создание решателя
solver = Solver(problem, parameters=params)
# Вывод результатов на консоль в процессе решения
cfol = ConsoleFullOutputListener(mode='full')
solver.add_listener(cfol)
# 3D визуализация по окончании решения
spl = StaticNDPaintListener("rastrigin.png", "output", vars_indxs=[0, 1], mode="surface", calc="interpolation")
solver.add_listener(spl)
# Запуск решения задачи
sol = solver.solve()
```

# **Примеры**
Expand All @@ -90,7 +90,6 @@ if __name__ == "__main__":
вещественные гиперпараметры (**C** - параметр регуляризации, **gamma** - коэффициент ядра) для решения задачи классификации рака молочной железы
([подробное описание данных](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic))).


```python
import numpy as np
from sklearn.utils import shuffle
Expand All @@ -103,32 +102,32 @@ from examples.Machine_learning.SVC._2D.Problems import SVC_2d


def load_breast_cancer_data():
dataset = load_breast_cancer()
x_raw, y_raw = dataset['data'], dataset['target']
inputs, outputs = shuffle(x_raw, y_raw ^ 1, random_state=42)
return inputs, outputs
dataset = load_breast_cancer()
x_raw, y_raw = dataset['data'], dataset['target']
inputs, outputs = shuffle(x_raw, y_raw ^ 1, random_state=42)
return inputs, outputs


if __name__ == "__main__":
x, y = load_breast_cancer_data()
regularization_value_bound = {'low': 1, 'up': 6}
kernel_coefficient_bound = {'low': -7, 'up': -3}
x, y = load_breast_cancer_data()
regularization_value_bound = {'low': 1, 'up': 6}
kernel_coefficient_bound = {'low': -7, 'up': -3}

problem = SVC_2d.SVC_2D(x, y, regularization_value_bound, kernel_coefficient_bound)

problem = SVC_2d.SVC_2D(x, y, regularization_value_bound, kernel_coefficient_bound)
method_params = SolverParameters(r=np.double(3.0), iters_limit=100)
solver = Solver(problem, parameters=method_params)

method_params = SolverParameters(r=np.double(3.0), itersLimit=100)
solver = Solver(problem, parameters=method_params)
apl = AnimationNDPaintListener("svc2d_anim.png", "output", vars_indxs=[0, 1], to_paint_obj_func=False)
solver.add_listener(apl)

apl = AnimationNDPaintListener("svc2d_anim.png", "output", varsIndxs=[0, 1], toPaintObjFunc=False)
solver.AddListener(apl)
spl = StaticNDPaintListener("svc2d_stat.png", "output", vars_indxs=[0, 1], mode="surface", calc="interpolation")
solver.add_listener(spl)

spl = StaticNDPaintListener("svc2d_stat.png", "output", varsIndxs=[0, 1], mode="surface", calc="interpolation")
solver.AddListener(spl)

cfol = ConsoleFullOutputListener(mode='full')
solver.AddListener(cfol)
cfol = ConsoleFullOutputListener(mode='full')
solver.add_listener(cfol)

solver_info = solver.Solve()
solver_info = solver.solve()

```

Expand Down
92 changes: 46 additions & 46 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ ____________________________________________________________________________
import xml.etree.ElementTree as ET
import numpy as np
def load_TSPs_matrix(filename):
root = ET.parse(filename).getroot()
def load_TSPs_matrix(file_name):
root = ET.parse(file_name).getroot()
columns = root.findall('graph/vertex')
num_cols = len(columns)
trans_matrix = np.zeros((num_cols, num_cols))
Expand Down Expand Up @@ -139,8 +139,8 @@ ____________________________________________________________________________
import numpy as np
from sko.GA import GA_TSP
def load_TSPs_matrix(filename):
root = ET.parse(filename).getroot()
def load_TSPs_matrix(file_name):
root = ET.parse(file_name).getroot()
columns = root.findall('graph/vertex')
num_cols = len(columns)
trans_matrix = np.zeros((num_cols, num_cols))
Expand Down Expand Up @@ -189,22 +189,22 @@ ____________________________________________________________________________
population_size: int,
mutation_probability_bound: Dict[str, float]):
self.dimension = 1
self.numberOfFloatVariables = 1
self.numberOfDiscreteVariables = 0
self.numberOfObjectives = 1
self.numberOfConstraints = 0
self.number_of_float_variables = 1
self.number_of_discrete_variables = 0
self.number_of_objectives = 1
self.number_of_constraints = 0
self.costMatrix = cost_matrix
if num_iteration <= 0:
raise ValueError('The number of iterations cannot be zero or negative.')
if population_size <= 0:
raise ValueError('Population size cannot be negative or zero')
self.populationSize = population_size
self.numberOfIterations = num_iteration
self.floatVariableNames = np.array(["Mutation probability"],
self.float_variable_names = np.array(["Mutation probability"],
dtype=str)
self.lowerBoundOfFloatVariables =
self.lower_bound_of_float_variables =
np.array([mutation_probability_bound['low']], dtype=np.double)
self.upperBoundOfFloatVariables =
self.upper_bound_of_float_variables =
np.array([mutation_probability_bound['up']], dtype=np.double)
self.n_dim = cost_matrix.shape[0]
Expand All @@ -215,7 +215,7 @@ ____________________________________________________________________________
def Calculate(self, point: Point,
functionValue: FunctionValue) -> FunctionValue:
mutation_prob = point.floatVariables[0]
mutation_prob = point.float_variables[0]
ga_tsp = GA_TSP(func=self.calc_total_distance,
n_dim=self.n_dim, size_pop=self.populationSize,
max_iter=self.numberOfIterations,
Expand All @@ -240,8 +240,8 @@ ____________________________________________________________________________
import numpy as np
import xml.etree.ElementTree as ET
def load_TSPs_matrix(filename):
root = ET.parse(filename).getroot()
def load_TSPs_matrix(file_name):
root = ET.parse(file_name).getroot()
columns = root.findall('graph/vertex')
num_cols = len(columns)
trans_matrix = np.zeros((num_cols, num_cols))
Expand All @@ -258,7 +258,7 @@ ____________________________________________________________________________
mutation_probability_bound = {'low': 0.0, 'up': 1.0}
problem = ga_tsp_vary_mutation.GA_TSP_Vary_Mutation(tsp_matrix,
num_iteration, population_size, mutation_probability_bound)
method_params = SolverParameters(r=np.double(3.0), itersLimit=20)
method_params = SolverParameters(r=np.double(3.0), iters_limit=20)
solver = Solver(problem, parameters=method_params)
solver_info = solver.Solve()
Expand Down Expand Up @@ -397,26 +397,26 @@ ________________________________________________________________________________
kernel_coefficient_bound: Dict[str, float]):
self.dimension = 2
self.numberOfFloatVariables = 2
self.numberOfDiscreteVariables = 0
self.numberOfObjectives = 1
self.numberOfConstraints = 0
self.number_of_float_variables = 2
self.number_of_discrete_variables = 0
self.number_of_objectives = 1
self.number_of_constraints = 0
if x_dataset.shape[0] != y_dataset.shape[0]:
raise ValueError('The input and output sample sizes do not match.')
self.x = x_dataset
self.y = y_dataset
self.floatVariableNames = np.array(["Regularization parameter",
self.float_variable_names = np.array(["Regularization parameter",
"Kernel coefficient"], dtype=str)
self.lowerBoundOfFloatVariables =
self.lower_bound_of_float_variables =
np.array([regularization_bound['low'],
kernel_coefficient_bound['low']], dtype=np.double)
self.upperBoundOfFloatVariables =
self.upper_bound_of_float_variables =
np.array([regularization_bound['up'],
kernel_coefficient_bound['up']], dtype=np.double)
def Calculate(self, point: Point,
functionValue: FunctionValue) -> FunctionValue:
cs, gammas = point.floatVariables[0], point.floatVariables[1]
cs, gammas = point.float_variables[0], point.float_variables[1]
clf = SVC(C=10**cs, gamma=10**gammas)
clf.fit(self.x, self.y)
functionValue.value = -cross_val_score(clf, self.x, self.y,
Expand Down Expand Up @@ -456,24 +456,24 @@ ________________________________________________________________________________
problem = SVC_2d.SVC_2D(x, y, regularization_value_bound,
kernel_coefficient_bound)
method_params = SolverParameters(r=np.double(3.0), itersLimit=10)
method_params = SolverParameters(r=np.double(3.0), iters_limit=10)
solver = Solver(problem, parameters=method_params)
apl = AnimationNDPaintListener("svc2d_anim.png", "output",
varsIndxs=[0, 1], toPaintObjFunc=False)
vars_indxs=[0, 1], to_paint_obj_func=False)
solver.AddListener(apl)
spl = StaticNDPaintListener("svc2d_stat.png", "output", varsIndxs=[0, 1],
spl = StaticNDPaintListener("svc2d_stat.png", "output", vars_indxs=[0, 1],
mode="surface", calc="interpolation")
solver.AddListener(spl)
solver_info = solver.Solve()
print(solver_info.numberOfGlobalTrials)
print(solver_info.numberOfLocalTrials)
print(solver_info.solvingTime)
print(solver_info.number_of_global_trials)
print(solver_info.number_of_local_trials)
print(solver_info.solving_time)
print(solver_info.bestTrials[0].point.floatVariables)
print(solver_info.bestTrials[0].functionValues[0].value)
print(solver_info.best_trials[0].point.float_variables)
print(solver_info.best_trials[0].function_values[0].value)
После проведения эксперимента программа выводит общее время поиска оптимума, точку на сетке, в которой достигается
оптимум, найденное максимальное значение метрики f1-score, а также интерполирует график целевой функции.
Expand Down Expand Up @@ -546,25 +546,25 @@ ________________________________________________________________________________
):
super(SVC_3D, self).__init__()
self.dimension = 3
self.numberOfFloatVariables = 2
self.numberOfDiscreteVariables = 1
self.numberOfObjectives = 1
self.numberOfConstraints = 0
self.number_of_float_variables = 2
self.number_of_discrete_variables = 1
self.number_of_objectives = 1
self.number_of_constraints = 0
if x_dataset.shape[0] != y_dataset.shape[0]:
raise ValueError('The input and output sample sizes do not match.')
self.x = x_dataset
self.y = y_dataset
self.floatVariableNames = np.array(["Regularization parameter", "Kernel coefficient"], dtype=str)
self.lowerBoundOfFloatVariables = np.array([regularization_bound['low'], kernel_coefficient_bound['low']],
self.float_variable_names = np.array(["Regularization parameter", "Kernel coefficient"], dtype=str)
self.lower_bound_of_float_variables = np.array([regularization_bound['low'], kernel_coefficient_bound['low']],
dtype=np.double)
self.upperBoundOfFloatVariables = np.array([regularization_bound['up'], kernel_coefficient_bound['up']],
self.upper_bound_of_float_variables = np.array([regularization_bound['up'], kernel_coefficient_bound['up']],
dtype=np.double)
self.discreteVariableNames.append('kernel')
self.discreteVariableValues.append(kernel_type['kernel'])
self.discrete_variable_names.append('kernel')
self.discrete_variable_values.append(kernel_type['kernel'])
def Calculate(self, point: Point, functionValue: FunctionValue) -> FunctionValue:
cs, gammas = point.floatVariables[0], point.floatVariables[1]
kernel_type = point.discreteVariables[0]
cs, gammas = point.float_variables[0], point.float_variables[1]
kernel_type = point.discrete_variables[0]
clf = SVC(C=10 ** cs, gamma=10 ** gammas, kernel=kernel_type)
functionValue.value = -cross_val_score(clf, self.x, self.y, scoring='f1').mean()
return functionValue
Expand Down Expand Up @@ -641,7 +641,7 @@ ________________________________________________________________________________
kernel_coefficient_bound = {'low': -9, 'up': -6.7}
kernel_type = {'kernel': ['rbf', 'sigmoid', 'poly']}
problem = SVC_3D.SVC_3D(x, y, regularization_value_bound, kernel_coefficient_bound, kernel_type)
method_params = SolverParameters(itersLimit=400)
method_params = SolverParameters(iters_limit=400)
solver = Solver(problem, parameters=method_params)
apl = StaticDiscreteListener("experiment1.png", mode='analysis')
solver.AddListener(apl)
Expand Down Expand Up @@ -794,11 +794,11 @@ ________________________________________________________________________________
regularization_value_bound = {'low': 1, 'up': 10}
kernel_coefficient_bound = {'low': -8, 'up': -1}
problem = SVC_2d.SVC_2D(x, y, regularization_value_bound, kernel_coefficient_bound)
method_params = SolverParameters(r=np.double(2.0), itersLimit=200)
method_params = SolverParameters(r=np.double(2.0), iters_limit=200)
solver = Solver(problem, parameters=method_params)
apl = AnimationNDPaintListener(varsIndxs=[0, 1], toPaintObjFunc=False)
apl = AnimationNDPaintListener(vars_indxs=[0, 1], to_paint_obj_func=False)
solver.AddListener(apl)
spl = StaticNDPaintListener(varsIndxs=[0, 1], mode="surface", calc="interpolation")
spl = StaticNDPaintListener(vars_indxs=[0, 1], mode="surface", calc="interpolation")
solver.AddListener(spl)
cfol = ConsoleFullOutputListener(mode='full')
solver.AddListener(cfol)
Expand Down
Loading

0 comments on commit 2639bb0

Please sign in to comment.