Skip to content

Commit

Permalink
move multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBlanke committed Dec 30, 2023
1 parent 25002ab commit 7738077
Show file tree
Hide file tree
Showing 38 changed files with 122 additions and 13 deletions.
102 changes: 102 additions & 0 deletions surfaces/_base_test_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Author: Simon Blanke
# Email: [email protected]
# 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:
explanation = """ """

dimensions = " "
formula = r" "
global_minimum = r" "

def __init__(self, metric="score", input_type="dictionary", sleep=0):
self.metric = metric
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)
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions surfaces/machine_learning/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


import time

import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import cross_val_score
from ..datasets import digits_data, wine_data, iris_data
from .datasets import digits_data, wine_data, iris_data

from .base_machine_learning_function import BaseMachineLearningFunction

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sklearn.ensemble import GradientBoostingRegressor

from sklearn.model_selection import cross_val_score
from ..datasets import diabetes_data
from .datasets import diabetes_data

from .base_machine_learning_function import BaseMachineLearningFunction

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
from hyperactive import Hyperactive
from hyperactive.optimizers import GridSearchOptimizer

from ..machine_learning.data_collector import SurfacesDataCollector
from ..data_collector import SurfacesDataCollector


class ObjectiveFunction:
explanation = """ """

dimensions = " "
formula = r" "
global_minimum = r" "

def __init__(self, metric="score", input_type="dictionary", sleep=0):
self.metric = metric
self.input_type = input_type
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class SphereFunction(ObjectiveFunction):
_name_ = "sphere_function"
__name__ = "SphereFunction"

explanation = """The Sphere function has d local minima except for the global one. It is continuous, convex and unimodal. The plot shows its two-dimensional form."""

dimensions = "n"
formula = r"f(\vec{x}) = \sum^n_{i=1}x^2_i"
global_minimum = r"f(\vec{x}=0) = 0"

def __init__(self, n_dim, A=1, metric="score", input_type="dictionary", sleep=0):
super().__init__(metric, input_type, sleep)
self.n_dim = n_dim
Expand Down

0 comments on commit 7738077

Please sign in to comment.