Skip to content

Commit

Permalink
rename multiple classes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBlanke committed Dec 30, 2023
1 parent 7738077 commit 2cdacf6
Show file tree
Hide file tree
Showing 31 changed files with 77 additions and 168 deletions.
81 changes: 1 addition & 80 deletions surfaces/_base_test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
# License: MIT License


import time
import numpy as np
import pandas as pd
from functools import reduce

from hyperactive import Hyperactive
from hyperactive.optimizers import GridSearchOptimizer

from ..machine_learning.data_collector import SurfacesDataCollector


class ObjectiveFunction:
class BaseTestFunction:
explanation = """ """

dimensions = " "
Expand All @@ -26,77 +15,9 @@ def __init__(self, metric="score", input_type="dictionary", sleep=0):
self.input_type = input_type
self.sleep = sleep

self.sql_data = SurfacesDataCollector()

def search_space(self, min=-5, max=5, step=0.1, value_typ="array"):
search_space_ = {}

for dim in range(self.n_dim):
dim_str = "x" + str(dim)

values = np.arange(min, max, step)
if value_typ == "list":
values = list(values)
search_space_[dim_str] = values

return search_space_

def collect_data(self, if_exists="append"):
para_names = list(self.search_space().keys())
search_data_cols = para_names + ["score"]
search_data = pd.DataFrame([], columns=search_data_cols)
search_data_length = 0

dim_sizes_list = [len(array) for array in self.search_space().values()]
search_space_size = reduce((lambda x, y: x * y), dim_sizes_list)

while search_data_length < search_space_size:
hyper = Hyperactive(verbosity=["progress_bar"])
hyper.add_search(
self.objective_function_dict,
self.search_space(value_typ="list"),
initialize={},
n_iter=search_space_size,
optimizer=GridSearchOptimizer(direction="orthogonal"),
memory_warm_start=search_data,
)
hyper.run()

search_data = pd.concat(
[search_data, hyper.search_data(self.objective_function_dict)],
ignore_index=True,
)

search_data = search_data.drop_duplicates(subset=para_names)
search_data_length = len(search_data)

self.sql_data.save(self.__name__, search_data, if_exists)

def load_search_data(self):
try:
dataframe = self.sql_data.load(self.__name__)
except:
print("Path 2 database: ", self.sql_data.path)
return dataframe

def return_metric(self, loss):
if self.metric == "score":
return -loss
elif self.metric == "loss":
return loss

def objective_function_np(self, *args):
para = {}
for i, arg in enumerate(args):
dim_str = "x" + str(i)
para[dim_str] = arg

return self.objective_function_dict(para)

def __call__(self, *input):
time.sleep(self.sleep)

if self.input_type == "dictionary":
return self.objective_function_dict(*input)
elif self.input_type == "arrays":
return self.objective_function_np(*input)
Binary file added surfaces/machine_learning.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from hyperactive import Hyperactive
from hyperactive.optimizers import GridSearchOptimizer
from ..data_collector import SurfacesDataCollector
from .._base_test_function import BaseTestFunction


class BaseMachineLearningFunction:
class MachineLearningFunction(BaseTestFunction):
def __init__(self, input_type="dictionary", sleep=0):
self.input_type = input_type
self.sleep = sleep
Expand Down Expand Up @@ -53,13 +54,6 @@ def collect_data(self, if_exists="append"):

self.sql_data.save(self.__name__, search_data, if_exists)

def load_search_data(self):
try:
dataframe = self.sql_data.load(self.__name__)
except:
print("Path 2 database: ", self.sql_data.path)
return dataframe

def objective_function_dict(self, params):
try:
parameter_d = params.para_dict
Expand Down
4 changes: 2 additions & 2 deletions surfaces/machine_learning_functions/tabular_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from sklearn.model_selection import cross_val_score
from .datasets import digits_data, wine_data, iris_data

from .base_machine_learning_function import BaseMachineLearningFunction
from .base_machine_learning_function import MachineLearningFunction


class KNeighborsClassifierFunction(BaseMachineLearningFunction):
class KNeighborsClassifierFunction(MachineLearningFunction):
__name__ = "k_neighbors_classifier"

def __init__(self, input_type="dictionary", sleep=0):
Expand Down
6 changes: 3 additions & 3 deletions surfaces/machine_learning_functions/tabular_regressors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from sklearn.model_selection import cross_val_score
from .datasets import diabetes_data

from .base_machine_learning_function import BaseMachineLearningFunction
from .base_machine_learning_function import MachineLearningFunction


class KNeighborsRegressorFunction(BaseMachineLearningFunction):
class KNeighborsRegressorFunction(MachineLearningFunction):
__name__ = "k_neighbors_regressor"

def __init__(self, input_type="dictionary", sleep=0):
Expand All @@ -33,7 +33,7 @@ def model(self, params):
return scores.mean()


class GradientBoostingRegressorFunction(BaseMachineLearningFunction):
class GradientBoostingRegressorFunction(MachineLearningFunction):
__name__ = "gradient_boosting_regressor"

def __init__(self, input_type="dictionary", sleep=0):
Expand Down
2 changes: 1 addition & 1 deletion surfaces/mathematical_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .rosenbrock_function import RosenbrockFunction
from .beale_function import BealeFunction
from .himmelblaus_function import HimmelblausFunction
from .hölder_table_function import HölderTableFunction
from .hoelder_table_function import HölderTableFunction
from .cross_in_tray_function import CrossInTrayFunction
from .simionescu_function import SimionescuFunction
from .easom_function import EasomFunction
Expand Down
10 changes: 2 additions & 8 deletions surfaces/mathematical_functions/_base_objective_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from hyperactive.optimizers import GridSearchOptimizer

from ..data_collector import SurfacesDataCollector
from .._base_test_function import BaseTestFunction


class ObjectiveFunction:
class MathematicalFunction(BaseTestFunction):
explanation = """ """

dimensions = " "
Expand Down Expand Up @@ -72,13 +73,6 @@ def collect_data(self, if_exists="append"):

self.sql_data.save(self.__name__, search_data, if_exists)

def load_search_data(self):
try:
dataframe = self.sql_data.load(self.__name__)
except:
print("Path 2 database: ", self.sql_data.path)
return dataframe

def return_metric(self, loss):
if self.metric == "score":
return -loss
Expand Down
4 changes: 2 additions & 2 deletions surfaces/mathematical_functions/ackley_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class AckleyFunction(ObjectiveFunction):
class AckleyFunction(MathematicalFunction):
name = "Ackley Function"
_name_ = "ackley_function"
__name__ = "AckleyFunction"
Expand Down
8 changes: 4 additions & 4 deletions surfaces/mathematical_functions/beale_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# License: MIT License


from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class BealeFunction(ObjectiveFunction):
class BealeFunction(MathematicalFunction):
name = "Beale Function"
_name_ = "beale_function"
__name__ = "BealeFunction"
Expand All @@ -26,8 +26,8 @@ def objective_function_dict(self, params):
y = params["x1"]

loss1 = (self.A - x + x * y) ** 2
loss2 = (self.B - x + x * y ** 2) ** 2
loss3 = (self.C - x + x * y ** 3) ** 2
loss2 = (self.B - x + x * y**2) ** 2
loss3 = (self.C - x + x * y**3) ** 2

loss = loss1 + loss2 + loss3

Expand Down
4 changes: 2 additions & 2 deletions surfaces/mathematical_functions/booth_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# License: MIT License


from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class BoothFunction(ObjectiveFunction):
class BoothFunction(MathematicalFunction):
name = "Booth Function"
_name_ = "booth_function"
__name__ = "BoothFunction"
Expand Down
6 changes: 3 additions & 3 deletions surfaces/mathematical_functions/bukin_function_n6.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class BukinFunctionN6(ObjectiveFunction):
class BukinFunctionN6(MathematicalFunction):
name = "Bukin Function N6"
_name_ = "bukin_function_n6"
__name__ = "BukinFunctionN6"
Expand All @@ -21,6 +21,6 @@ def objective_function_dict(self, params):
x = params["x0"]
y = params["x1"]

loss = 100 * np.sqrt(np.abs(y - 0.01 * x ** 2)) + 0.01 * np.abs(x + 10)
loss = 100 * np.sqrt(np.abs(y - 0.01 * x**2)) + 0.01 * np.abs(x + 10)

return self.return_metric(loss)
6 changes: 3 additions & 3 deletions surfaces/mathematical_functions/cross_in_tray_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class CrossInTrayFunction(ObjectiveFunction):
class CrossInTrayFunction(MathematicalFunction):
name = "Cross In Tray Function"
_name_ = "cross_in_tray_function"
__name__ = "CrossInTrayFunction"
Expand All @@ -34,7 +34,7 @@ def objective_function_dict(self, params):
y = params["x1"]

loss1 = np.sin(self.angle * x) * np.sin(self.angle * y)
loss2 = np.exp(abs(self.B - (np.sqrt(x ** 2 + y ** 2) / np.pi)) + 1)
loss2 = np.exp(abs(self.B - (np.sqrt(x**2 + y**2) / np.pi)) + 1)

loss = -self.A * (np.abs(loss1 * loss2)) ** 0.1

Expand Down
8 changes: 4 additions & 4 deletions surfaces/mathematical_functions/drop_wave_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class DropWaveFunction(ObjectiveFunction):
class DropWaveFunction(MathematicalFunction):
name = "Drop Wave Function"
_name_ = "drop_wave_function"
__name__ = "DropWaveFunction"
Expand All @@ -21,8 +21,8 @@ def objective_function_dict(self, params):
x = params["x0"]
y = params["x1"]

loss = -(1 + np.cos(12 * np.sqrt(x ** 2 + y ** 2))) / (
0.5 * (x ** 2 + y ** 2) + 2
loss = -(1 + np.cos(12 * np.sqrt(x**2 + y**2))) / (
0.5 * (x**2 + y**2) + 2
)

return self.return_metric(loss)
4 changes: 2 additions & 2 deletions surfaces/mathematical_functions/easom_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class EasomFunction(ObjectiveFunction):
class EasomFunction(MathematicalFunction):
name = "Easom Function"
_name_ = "easom_function"
__name__ = "EasomFunction"
Expand Down
4 changes: 2 additions & 2 deletions surfaces/mathematical_functions/eggholder_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class EggholderFunction(ObjectiveFunction):
class EggholderFunction(MathematicalFunction):
name = "Eggholder Function"
_name_ = "eggholder_function"
__name__ = "EggholderFunction"
Expand Down
8 changes: 4 additions & 4 deletions surfaces/mathematical_functions/goldstein_price_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# License: MIT License


from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class GoldsteinPriceFunction(ObjectiveFunction):
class GoldsteinPriceFunction(MathematicalFunction):
name = "Goldstein Price Function"
_name_ = "goldstein_price_function"
__name__ = "GoldsteinPriceFunction"
Expand All @@ -20,10 +20,10 @@ def objective_function_dict(self, params):
y = params["x1"]

loss1 = 1 + (x + y + 1) ** 2 * (
19 - 14 * x + 3 * x ** 2 - 14 * y + 6 * x * y + 3 * y ** 2
19 - 14 * x + 3 * x**2 - 14 * y + 6 * x * y + 3 * y**2
)
loss2 = 30 + (2 * x - 3 * y) ** 2 * (
18 - 32 * x + 12 * x ** 2 + 48 * y - 36 * x * y + 27 * y ** 2
18 - 32 * x + 12 * x**2 + 48 * y - 36 * x * y + 27 * y**2
)

loss = loss1 * loss2
Expand Down
4 changes: 2 additions & 2 deletions surfaces/mathematical_functions/gramacy_and_lee_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class GramacyAndLeeFunction(ObjectiveFunction):
class GramacyAndLeeFunction(MathematicalFunction):
name = "Gramacy And Lee Function"
_name_ = "gramacy_and_lee_function"
__name__ = "GramacyAndLeeFunction"
Expand Down
6 changes: 3 additions & 3 deletions surfaces/mathematical_functions/griewank_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import numpy as np

from ._base_objective_function import ObjectiveFunction
from ._base_objective_function import MathematicalFunction


class GriewankFunction(ObjectiveFunction):
class GriewankFunction(MathematicalFunction):
name = "Griewank Function"
_name_ = "griewank_function"
__name__ = "GriewankFunction"
Expand All @@ -24,7 +24,7 @@ def objective_function_dict(self, params):
dim_str = "x" + str(dim)
x = params[dim_str]

loss_sum += x ** 2 / 4000
loss_sum += x**2 / 4000
loss_product *= np.cos(x / np.sqrt(dim + 1))

loss = loss_sum - loss_product + 1
Expand Down
Loading

0 comments on commit 2cdacf6

Please sign in to comment.