diff --git a/bindings/python/cconfigspace/base.py b/bindings/python/cconfigspace/base.py index 8cd365eb..e8f6f6bf 100644 --- a/bindings/python/cconfigspace/base.py +++ b/bindings/python/cconfigspace/base.py @@ -28,6 +28,7 @@ def __str__(self): ccs_expression = ccs_object ccs_context = ccs_object ccs_distribution_space = ccs_object +ccs_search_space = ccs_object ccs_configuration_space = ccs_object ccs_binding = ccs_object ccs_configuration = ccs_object diff --git a/bindings/python/cconfigspace/features_tuner.py b/bindings/python/cconfigspace/features_tuner.py index c5e8108a..67e97548 100644 --- a/bindings/python/cconfigspace/features_tuner.py +++ b/bindings/python/cconfigspace/features_tuner.py @@ -143,14 +143,14 @@ def suggest(self, features): Error.check(res) return Configuration(handle = config, retain = False) -ccs_create_random_features_tuner = _ccs_get_function("ccs_create_random_features_tuner", [ct.c_char_p, ccs_configuration_space, ccs_feature_space, ccs_objective_space, ct.POINTER(ccs_features_tuner)]) +ccs_create_random_features_tuner = _ccs_get_function("ccs_create_random_features_tuner", [ct.c_char_p, ccs_feature_space, ccs_objective_space, ct.POINTER(ccs_features_tuner)]) class RandomFeaturesTuner(FeaturesTuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", configuration_space = None, feature_space = None, objective_space = None): + name = "", feature_space = None, objective_space = None): if handle is None: handle = ccs_features_tuner() - res = ccs_create_random_features_tuner(str.encode(name), configuration_space.handle, feature_space.handle, objective_space.handle, ct.byref(handle)) + res = ccs_create_random_features_tuner(str.encode(name), feature_space.handle, objective_space.handle, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) else: @@ -178,7 +178,7 @@ class UserDefinedFeaturesTunerVector(ct.Structure): ('serialize', ccs_user_defined_features_tuner_serialize_type), ('deserialize', ccs_user_defined_features_tuner_deserialize_type) ] -ccs_create_user_defined_features_tuner = _ccs_get_function("ccs_create_user_defined_features_tuner", [ct.c_char_p, ccs_configuration_space, ccs_feature_space, ccs_objective_space, ct.POINTER(UserDefinedFeaturesTunerVector), ct.py_object, ct.POINTER(ccs_features_tuner)]) +ccs_create_user_defined_features_tuner = _ccs_get_function("ccs_create_user_defined_features_tuner", [ct.c_char_p, ccs_feature_space, ccs_objective_space, ct.POINTER(UserDefinedFeaturesTunerVector), ct.py_object, ct.POINTER(ccs_features_tuner)]) ccs_user_defined_features_tuner_get_tuner_data = _ccs_get_function("ccs_user_defined_features_tuner_get_tuner_data", [ccs_features_tuner, ct.POINTER(ct.c_void_p)]) def _wrap_user_defined_features_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize): @@ -346,7 +346,7 @@ def deserialize_wrapper(tun, size_history, p_history, num_optima, p_optima, stat class UserDefinedFeaturesTuner(FeaturesTuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", configuration_space = None, feature_space = None, objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): + name = "", feature_space = None, objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): if handle is None: if ask is None or tell is None or get_optima is None or get_history is None: raise Error(Result(Result.ERROR_INVALID_VALUE)) @@ -381,7 +381,7 @@ def __init__(self, handle = None, retain = False, auto_release = True, c_tuner_data = ct.py_object(tuner_data) else: c_tuner_data = None - res = ccs_create_user_defined_features_tuner(str.encode(name), configuration_space.handle, feature_space.handle, objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) + res = ccs_create_user_defined_features_tuner(str.encode(name), feature_space.handle, objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) _register_vector(handle, [delete_wrapper, ask_wrapper, tell_wrapper, get_optima_wrapper, get_history_wrapper, suggest_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, ask_wrapper_func, tell_wrapper_func, get_optima_wrapper_func, get_history_wrapper_func, suggest_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tuner_data]) diff --git a/bindings/python/cconfigspace/objective_space.py b/bindings/python/cconfigspace/objective_space.py index b114f883..43965fd5 100644 --- a/bindings/python/cconfigspace/objective_space.py +++ b/bindings/python/cconfigspace/objective_space.py @@ -1,5 +1,5 @@ import ctypes as ct -from .base import Object, Error, CEnumeration, Result, _ccs_get_function, ccs_context, ccs_parameter, ccs_configuration_space, ccs_configuration, ccs_evaluation, ccs_rng, ccs_distribution, ccs_expression, Datum, ccs_objective_space, ccs_bool +from .base import Object, Error, CEnumeration, Result, _ccs_get_function, ccs_context, ccs_parameter, ccs_configuration_space, ccs_configuration, ccs_evaluation, ccs_rng, ccs_distribution, ccs_expression, Datum, ccs_objective_space, ccs_bool, ccs_search_space from .context import Context from .parameter import Parameter from .expression import Expression @@ -12,14 +12,15 @@ class ObjectiveType(CEnumeration): ('MINIMIZE', 0), 'MAXIMIZE' ] -ccs_create_objective_space = _ccs_get_function("ccs_create_objective_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType), ct.POINTER(ccs_objective_space)]) +ccs_create_objective_space = _ccs_get_function("ccs_create_objective_space", [ct.c_char_p, ccs_search_space, ct.c_size_t, ct.POINTER(ccs_parameter), ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType), ct.POINTER(ccs_objective_space)]) +ccs_objective_space_get_search_space = _ccs_get_function("ccs_objective_space_get_search_space", [ccs_objective_space, ct.POINTER(ccs_search_space)]) ccs_objective_space_get_objective = _ccs_get_function("ccs_objective_space_get_objective", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType)]) ccs_objective_space_get_objectives = _ccs_get_function("ccs_objective_space_get_objectives", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType), ct.POINTER(ct.c_size_t)]) ccs_objective_space_check_evaluation = _ccs_get_function("ccs_objective_space_check_evaluation", [ccs_objective_space, ccs_evaluation, ct.POINTER(ccs_bool)]) class ObjectiveSpace(Context): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", parameters = [], objectives = [], types = None): + name = "", search_space = None, parameters = [], objectives = [], types = None): if handle is None: count = len(parameters) ctx = dict(zip([x.name for x in parameters], parameters)) @@ -37,7 +38,7 @@ def __init__(self, handle = None, retain = False, auto_release = True, parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters]) objectives = (ccs_expression * sz)(*[x.handle.value for x in objectives]) handle = ccs_objective_space() - res = ccs_create_objective_space(str.encode(name), count, parameters, sz, objectives, types, ct.byref(handle)) + res = ccs_create_objective_space(str.encode(name), search_space.handle, count, parameters, sz, objectives, types, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) else: @@ -47,6 +48,16 @@ def __init__(self, handle = None, retain = False, auto_release = True, def from_handle(cls, handle, retain = True, auto_release = True): return cls(handle = handle, retain = retain, auto_release = auto_release) + @property + def search_space(self): + if hasattr(self, "_search_space"): + return self._search_space + s = ccs_search_space() + res = ccs_objective_space_get_search_space(self.handle, ct.byref(s)) + Error.check(res) + self._search_space = Object.from_handle(s) + return self._search_space + def get_objective(self, index): v = ccs_expression() t = ObjectiveType() diff --git a/bindings/python/cconfigspace/tree_tuner.py b/bindings/python/cconfigspace/tree_tuner.py index 9561b0db..32bf48c2 100644 --- a/bindings/python/cconfigspace/tree_tuner.py +++ b/bindings/python/cconfigspace/tree_tuner.py @@ -127,14 +127,14 @@ def suggest(self): Error.check(res) return TreeConfiguration(handle = config, retain = False) -ccs_create_random_tree_tuner = _ccs_get_function("ccs_create_random_tree_tuner", [ct.c_char_p, ccs_tree_space, ccs_objective_space, ct.POINTER(ccs_tree_tuner)]) +ccs_create_random_tree_tuner = _ccs_get_function("ccs_create_random_tree_tuner", [ct.c_char_p, ccs_objective_space, ct.POINTER(ccs_tree_tuner)]) class RandomTreeTuner(TreeTuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", tree_space = None, objective_space = None): + name = "", objective_space = None): if handle is None: handle = ccs_tree_tuner() - res = ccs_create_random_tree_tuner(str.encode(name), tree_space.handle, objective_space.handle, ct.byref(handle)) + res = ccs_create_random_tree_tuner(str.encode(name), objective_space.handle, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) else: @@ -162,7 +162,7 @@ class UserDefinedTreeTunerVector(ct.Structure): ('serialize', ccs_user_defined_tree_tuner_serialize_type), ('deserialize', ccs_user_defined_tree_tuner_deserialize_type) ] -ccs_create_user_defined_tree_tuner = _ccs_get_function("ccs_create_user_defined_tree_tuner", [ct.c_char_p, ccs_tree_space, ccs_objective_space, ct.POINTER(UserDefinedTreeTunerVector), ct.py_object, ct.POINTER(ccs_tree_tuner)]) +ccs_create_user_defined_tree_tuner = _ccs_get_function("ccs_create_user_defined_tree_tuner", [ct.c_char_p, ccs_objective_space, ct.POINTER(UserDefinedTreeTunerVector), ct.py_object, ct.POINTER(ccs_tree_tuner)]) ccs_user_defined_tree_tuner_get_tuner_data = _ccs_get_function("ccs_user_defined_tree_tuner_get_tuner_data", [ccs_tree_tuner, ct.POINTER(ct.c_void_p)]) def _wrap_user_defined_tree_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize): @@ -330,7 +330,7 @@ def deserialize_wrapper(tun, size_history, p_history, num_optima, p_optima, stat class UserDefinedTreeTuner(TreeTuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", tree_space = None, objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): + name = "", objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): if handle is None: if ask is None or tell is None or get_optima is None or get_history is None: raise Error(Result(Result.ERROR_INVALID_VALUE)) @@ -365,7 +365,7 @@ def __init__(self, handle = None, retain = False, auto_release = True, c_tuner_data = ct.py_object(tuner_data) else: c_tuner_data = None - res = ccs_create_user_defined_tree_tuner(str.encode(name), tree_space.handle, objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) + res = ccs_create_user_defined_tree_tuner(str.encode(name), objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) _register_vector(handle, [delete_wrapper, ask_wrapper, tell_wrapper, get_optima_wrapper, get_history_wrapper, suggest_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, ask_wrapper_func, tell_wrapper_func, get_optima_wrapper_func, get_history_wrapper_func, suggest_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tuner_data]) diff --git a/bindings/python/cconfigspace/tuner.py b/bindings/python/cconfigspace/tuner.py index d15b0c0d..3359ae38 100644 --- a/bindings/python/cconfigspace/tuner.py +++ b/bindings/python/cconfigspace/tuner.py @@ -127,14 +127,14 @@ def suggest(self): Error.check(res) return Configuration(handle = config, retain = False) -ccs_create_random_tuner = _ccs_get_function("ccs_create_random_tuner", [ct.c_char_p, ccs_configuration_space, ccs_objective_space, ct.POINTER(ccs_tuner)]) +ccs_create_random_tuner = _ccs_get_function("ccs_create_random_tuner", [ct.c_char_p, ccs_objective_space, ct.POINTER(ccs_tuner)]) class RandomTuner(Tuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", configuration_space = None, objective_space = None): + name = "", objective_space = None): if handle is None: handle = ccs_tuner() - res = ccs_create_random_tuner(str.encode(name), configuration_space.handle, objective_space.handle, ct.byref(handle)) + res = ccs_create_random_tuner(str.encode(name), objective_space.handle, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) else: @@ -162,7 +162,7 @@ class UserDefinedTunerVector(ct.Structure): ('serialize', ccs_user_defined_tuner_serialize_type), ('deserialize', ccs_user_defined_tuner_deserialize_type) ] -ccs_create_user_defined_tuner = _ccs_get_function("ccs_create_user_defined_tuner", [ct.c_char_p, ccs_configuration_space, ccs_objective_space, ct.POINTER(UserDefinedTunerVector), ct.py_object, ct.POINTER(ccs_tuner)]) +ccs_create_user_defined_tuner = _ccs_get_function("ccs_create_user_defined_tuner", [ct.c_char_p, ccs_objective_space, ct.POINTER(UserDefinedTunerVector), ct.py_object, ct.POINTER(ccs_tuner)]) ccs_user_defined_tuner_get_tuner_data = _ccs_get_function("ccs_user_defined_tuner_get_tuner_data", [ccs_tuner, ct.POINTER(ct.c_void_p)]) def _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize): @@ -330,7 +330,7 @@ def deserialize_wrapper(tun, size_history, p_history, num_optima, p_optima, stat class UserDefinedTuner(Tuner): def __init__(self, handle = None, retain = False, auto_release = True, - name = "", configuration_space = None, objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): + name = "", objective_space = None, delete = None, ask = None, tell = None, get_optima = None, get_history = None, suggest = None, serialize = None, deserialize = None, tuner_data = None ): if handle is None: if ask is None or tell is None or get_optima is None or get_history is None: raise Error(Result(Result.ERROR_INVALID_VALUE)) @@ -365,7 +365,7 @@ def __init__(self, handle = None, retain = False, auto_release = True, c_tuner_data = ct.py_object(tuner_data) else: c_tuner_data = None - res = ccs_create_user_defined_tuner(str.encode(name), configuration_space.handle, objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) + res = ccs_create_user_defined_tuner(str.encode(name), objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle)) Error.check(res) super().__init__(handle = handle, retain = False) _register_vector(handle, [delete_wrapper, ask_wrapper, tell_wrapper, get_optima_wrapper, get_history_wrapper, suggest_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, ask_wrapper_func, tell_wrapper_func, get_optima_wrapper_func, get_history_wrapper_func, suggest_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tuner_data]) diff --git a/bindings/python/test/test_evaluation.py b/bindings/python/test/test_evaluation.py index cd002ec7..49f7d9b0 100644 --- a/bindings/python/test/test_evaluation.py +++ b/bindings/python/test/test_evaluation.py @@ -15,7 +15,7 @@ def test_create(self): v2 = ccs.NumericalParameter.Float() e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) + os = ccs.ObjectiveSpace(name = "ospace", search_space = cs, parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) ev1 = ccs.Evaluation(objective_space = os, configuration = cs.sample(), values = [0.5, 0.6]) self.assertEqual( (0.5, 0.6), ev1.values ) self.assertEqual( (0.5, 0.6), ev1.objective_values ) @@ -43,7 +43,7 @@ def test_serialize(self): v2 = ccs.NumericalParameter.Float() e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) + os = ccs.ObjectiveSpace(name = "ospace", search_space = cs, parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) evref = ccs.Evaluation(objective_space = os, configuration = cs.sample(), values = [0.5, 0.6]) buff = evref.serialize() handle_map = ccs.Map() diff --git a/bindings/python/test/test_features_tuner.py b/bindings/python/test/test_features_tuner.py index a11db5e0..94ecc40e 100644 --- a/bindings/python/test/test_features_tuner.py +++ b/bindings/python/test/test_features_tuner.py @@ -16,14 +16,14 @@ def create_tuning_problem(self): v2 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf')) e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = [e1, e2]) + os = ccs.ObjectiveSpace(name = "ospace", search_space = cs, parameters = [v1, v2], objectives = [e1, e2]) f1 = ccs.CategoricalParameter(values = [True, False]) fs = ccs.FeatureSpace(name = "fspace", parameters = [f1]) - return (cs, fs, os) + return (fs, os) def test_create_random(self): - (cs, fs, os) = self.create_tuning_problem() - t = ccs.RandomFeaturesTuner(name = "tuner", configuration_space = cs, feature_space = fs, objective_space = os) + (fs, os) = self.create_tuning_problem() + t = ccs.RandomFeaturesTuner(name = "tuner", feature_space = fs, objective_space = os) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.FeaturesTunerType.RANDOM, t.type) @@ -118,14 +118,14 @@ def suggest(tuner, features): else: return choice(optis).configuration - (cs, fs, os) = self.create_tuning_problem() - t = ccs.UserDefinedFeaturesTuner(name = "tuner", configuration_space = cs, feature_space = fs, objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) + (fs, os) = self.create_tuning_problem() + t = ccs.UserDefinedFeaturesTuner(name = "tuner", feature_space = fs, objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.FeaturesTunerType.USER_DEFINED, t.type) - self.assertEqual(cs.handle.value, t.configuration_space.handle.value) self.assertEqual(fs.handle.value, t.feature_space.handle.value) self.assertEqual(os.handle.value, t.objective_space.handle.value) + self.assertEqual(os.search_space.handle.value, t.configuration_space.handle.value) func = lambda x, y, z: [(x-2)*(x-2), sin(z+y)] features_on = ccs.Features(feature_space = fs, values = [True]) features_off = ccs.Features(feature_space = fs, values = [False]) diff --git a/bindings/python/test/test_objective_space.py b/bindings/python/test/test_objective_space.py index 9d3e4efd..a987b516 100644 --- a/bindings/python/test/test_objective_space.py +++ b/bindings/python/test/test_objective_space.py @@ -7,13 +7,16 @@ class TestObjectiveSpace(unittest.TestCase): def test_create(self): + h = ccs.NumericalParameter.Float() + cs = ccs.ConfigurationSpace(name = "cs", parameters = [h]) h1 = ccs.NumericalParameter.Float() h2 = ccs.NumericalParameter.Float() h3 = ccs.NumericalParameter.Float() e1 = ccs.Expression.Add(left = h1, right = h2) e2 = ccs.Expression.Variable(parameter = h3) - os = ccs.ObjectiveSpace(name = "space", parameters = [h1, h2, h3], objectives = [e1, e2], types = [ccs.ObjectiveType.MINIMIZE, ccs.ObjectiveType.MAXIMIZE]) + os = ccs.ObjectiveSpace(name = "space", search_space = cs, parameters = [h1, h2, h3], objectives = [e1, e2], types = [ccs.ObjectiveType.MINIMIZE, ccs.ObjectiveType.MAXIMIZE]) self.assertEqual( "space", os.name ) + self.assertEqual( cs.handle.value, os.search_space.handle.value ) self.assertEqual( 3, os.num_parameters ) self.assertEqual( h1, os.parameter(0) ) self.assertEqual( h2, os.parameter(1) ) diff --git a/bindings/python/test/test_tree_evaluation.py b/bindings/python/test/test_tree_evaluation.py index c3c5f0fd..c2a0774f 100644 --- a/bindings/python/test/test_tree_evaluation.py +++ b/bindings/python/test/test_tree_evaluation.py @@ -22,7 +22,7 @@ def test_create(self): v2 = ccs.NumericalParameter.Float() e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) + os = ccs.ObjectiveSpace(name = "ospace", search_space = ts, parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) ev1 = ccs.TreeEvaluation(objective_space = os, configuration = ts.sample(), values = [0.5, 0.6]) self.assertEqual( (0.5, 0.6), ev1.values ) self.assertEqual( (0.5, 0.6), ev1.objective_values ) @@ -48,7 +48,7 @@ def test_serialize(self): v2 = ccs.NumericalParameter.Float() e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) + os = ccs.ObjectiveSpace(name = "ospace", search_space = ts, parameters = [v1, v2], objectives = { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE }) evref = ccs.TreeEvaluation(objective_space = os, configuration = ts.sample(), values = [0.5, 0.6]) buff = evref.serialize() handle_map = ccs.Map() diff --git a/bindings/python/test/test_tree_tuner.py b/bindings/python/test/test_tree_tuner.py index 7595869f..bc5f59c4 100644 --- a/bindings/python/test/test_tree_tuner.py +++ b/bindings/python/test/test_tree_tuner.py @@ -27,12 +27,12 @@ def create_tuning_problem(self): ts = ccs.StaticTreeSpace(name = 'space', tree = tree) v1 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf')) e1 = ccs.Expression.Variable(parameter = v1) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1], objectives = {e1: ccs.ObjectiveType.MAXIMIZE}) - return (ts, os) + os = ccs.ObjectiveSpace(name = "ospace", search_space = ts, parameters = [v1], objectives = {e1: ccs.ObjectiveType.MAXIMIZE}) + return os def test_create_random(self): - (ts, os) = self.create_tuning_problem() - t = ccs.RandomTreeTuner(name = "tuner", tree_space = ts, objective_space = os) + os = self.create_tuning_problem() + t = ccs.RandomTreeTuner(name = "tuner", objective_space = os) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.TreeTunerType.RANDOM, t.type) @@ -107,8 +107,8 @@ def suggest(tuner): else: return choice(tuner.tuner_data.optima).configuration - (ts, os) = self.create_tuning_problem() - t = ccs.UserDefinedTreeTuner(name = "tuner", tree_space = ts, objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) + os = self.create_tuning_problem() + t = ccs.UserDefinedTreeTuner(name = "tuner", objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.TreeTunerType.USER_DEFINED, t.type) diff --git a/bindings/python/test/test_tuner.py b/bindings/python/test/test_tuner.py index 0604636e..946d1360 100644 --- a/bindings/python/test/test_tuner.py +++ b/bindings/python/test/test_tuner.py @@ -17,12 +17,12 @@ def create_tuning_problem(self): v2 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf')) e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = [e1, e2]) - return (cs, os) + os = ccs.ObjectiveSpace(name = "ospace", search_space = cs, parameters = [v1, v2], objectives = [e1, e2]) + return os def test_create_random(self): - (cs, os) = self.create_tuning_problem() - t = ccs.RandomTuner(name = "tuner", configuration_space = cs, objective_space = os) + os = self.create_tuning_problem() + t = ccs.RandomTuner(name = "tuner", objective_space = os) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.TunerType.RANDOM, t.type) @@ -101,13 +101,13 @@ def suggest(tuner): else: return choice(tuner.tuner_data.optima).configuration - (cs, os) = self.create_tuning_problem() - t = ccs.UserDefinedTuner(name = "tuner", configuration_space = cs, objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) + os = self.create_tuning_problem() + t = ccs.UserDefinedTuner(name = "tuner", objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, suggest = suggest, tuner_data = TunerData()) t2 = ccs.Object.from_handle(t.handle) self.assertEqual("tuner", t.name) self.assertEqual(ccs.TunerType.USER_DEFINED, t.type) - self.assertEqual(cs.handle.value, t.configuration_space.handle.value) self.assertEqual(os.handle.value, t.objective_space.handle.value) + self.assertEqual(os.search_space.handle.value, t.configuration_space.handle.value) func = lambda x, y, z: [(x-2)*(x-2), sin(z+y)] evals = [ccs.Evaluation(objective_space = os, configuration = c, values = func(*(c.values))) for c in t.ask(100)] t.tell(evals) diff --git a/bindings/ruby/lib/cconfigspace/base.rb b/bindings/ruby/lib/cconfigspace/base.rb index ca333e57..22c75f7e 100644 --- a/bindings/ruby/lib/cconfigspace/base.rb +++ b/bindings/ruby/lib/cconfigspace/base.rb @@ -97,6 +97,7 @@ class Version < FFI::Struct typedef :ccs_object_t, :ccs_expression_t typedef :ccs_object_t, :ccs_context_t typedef :ccs_object_t, :ccs_distribution_space_t + typedef :ccs_object_t, :ccs_search_space_t typedef :ccs_object_t, :ccs_configuration_space_t typedef :ccs_object_t, :ccs_binding_t typedef :ccs_object_t, :ccs_configuration_t @@ -123,6 +124,7 @@ class MemoryPointer alias read_ccs_expression_t read_ccs_object_t alias read_ccs_context_t read_ccs_object_t alias read_ccs_distribution_space_t read_ccs_object_t + alias read_ccs_search_space_t read_ccs_object_t alias read_ccs_configuration_space_t read_ccs_object_t alias read_ccs_binding_t read_ccs_object_t alias read_ccs_configuration_t read_ccs_object_t diff --git a/bindings/ruby/lib/cconfigspace/features_tuner.rb b/bindings/ruby/lib/cconfigspace/features_tuner.rb index 6030cef1..fbf2bf89 100644 --- a/bindings/ruby/lib/cconfigspace/features_tuner.rb +++ b/bindings/ruby/lib/cconfigspace/features_tuner.rb @@ -20,7 +20,6 @@ def read_ccs_features_tuner_type_t attach_function :ccs_features_tuner_get_optima, [:ccs_features_tuner_t, :ccs_features_t, :size_t, :pointer, :pointer], :ccs_result_t attach_function :ccs_features_tuner_get_history, [:ccs_features_tuner_t, :ccs_features_t, :size_t, :pointer, :pointer], :ccs_result_t attach_function :ccs_features_tuner_suggest, [:ccs_features_tuner_t, :ccs_features_t, :pointer], :ccs_result_t - attach_function :ccs_create_random_features_tuner, [:string, :ccs_configuration_space_t, :ccs_feature_space_t, :ccs_objective_space_t, :pointer], :ccs_result_t class FeaturesTuner < Object add_property :type, :ccs_features_tuner_type_t, :ccs_features_tuner_get_type, memoize: true @@ -99,14 +98,16 @@ def suggest(features) end + attach_function :ccs_create_random_features_tuner, [:string, :ccs_feature_space_t, :ccs_objective_space_t, :pointer], :ccs_result_t + class RandomFeaturesTuner < FeaturesTuner def initialize(handle = nil, retain: false, auto_release: true, - name: "", configuration_space: nil, feature_space: nil, objective_space: nil) + name: "", feature_space: nil, objective_space: nil) if handle super(handle, retain: retain, auto_release: auto_release) else ptr = MemoryPointer::new(:ccs_features_tuner_t) - CCS.error_check CCS.ccs_create_random_features_tuner(name, configuration_space, feature_space, objective_space, ptr) + CCS.error_check CCS.ccs_create_random_features_tuner(name, feature_space, objective_space, ptr) super(ptr.read_ccs_features_tuner_t, retain: false) end end @@ -257,13 +258,13 @@ def self.wrap_user_defined_features_tuner_callbacks(del, ask, tell, get_optima, return [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper] end - attach_function :ccs_create_user_defined_features_tuner, [:string, :ccs_configuration_space_t, :ccs_feature_space_t, :ccs_objective_space_t, UserDefinedFeaturesTunerVector.by_ref, :value, :pointer], :ccs_result_t + attach_function :ccs_create_user_defined_features_tuner, [:string, :ccs_feature_space_t, :ccs_objective_space_t, UserDefinedFeaturesTunerVector.by_ref, :value, :pointer], :ccs_result_t attach_function :ccs_user_defined_features_tuner_get_tuner_data, [:ccs_features_tuner_t, :pointer], :ccs_result_t class UserDefinedFeaturesTuner < FeaturesTuner add_property :tuner_data, :value, :ccs_user_defined_features_tuner_get_tuner_data, memoize: true def initialize(handle = nil, retain: false, auto_release: true, - name: "", configuration_space: nil, feature_space: nil, objective_space: nil, + name: "", feature_space: nil, objective_space: nil, del: nil, ask: nil, tell: nil, get_optima: nil, get_history: nil, suggest: nil, serialize: nil, deserialize: nil, tuner_data: nil) if handle super(handle, retain: retain, auto_release: auto_release) @@ -281,7 +282,7 @@ def initialize(handle = nil, retain: false, auto_release: true, vector[:serialize] = serializewrapper vector[:deserialize] = deserializewrapper ptr = MemoryPointer::new(:ccs_features_tuner_t) - CCS.error_check CCS.ccs_create_user_defined_features_tuner(name, configuration_space, feature_space, objective_space, vector, tuner_data, ptr) + CCS.error_check CCS.ccs_create_user_defined_features_tuner(name, feature_space, objective_space, vector, tuner_data, ptr) handle = ptr.read_ccs_features_tuner_t super(handle, retain: false) CCS.register_vector(handle, [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper, tuner_data]) diff --git a/bindings/ruby/lib/cconfigspace/objective_space.rb b/bindings/ruby/lib/cconfigspace/objective_space.rb index ea8e97f8..4638addc 100644 --- a/bindings/ruby/lib/cconfigspace/objective_space.rb +++ b/bindings/ruby/lib/cconfigspace/objective_space.rb @@ -18,15 +18,17 @@ def read_array_of_ccs_objective_type_t(count) end end - attach_function :ccs_create_objective_space, [:string, :size_t, :pointer, :size_t, :pointer, :pointer, :pointer], :ccs_result_t + attach_function :ccs_create_objective_space, [:string, :pointer, :size_t, :pointer, :size_t, :pointer, :pointer, :pointer], :ccs_result_t + attach_function :ccs_objective_space_get_search_space, [:ccs_objective_space_t, :pointer], :ccs_result_t attach_function :ccs_objective_space_get_objective, [:ccs_objective_space_t, :size_t, :pointer, :pointer], :ccs_result_t attach_function :ccs_objective_space_get_objectives, [:ccs_objective_space_t, :size_t, :pointer, :pointer, :pointer], :ccs_result_t attach_function :ccs_objective_space_check_evaluation, [:ccs_objective_space_t, :ccs_evaluation_t, :pointer], :ccs_result_t class ObjectiveSpace < Context + add_handle_property :search_space, :ccs_search_space_t, :ccs_objective_space_get_search_space, memoize: true def initialize(handle = nil, retain: false, auto_release: true, - name: "", parameters: [], objectives: [], types: nil) + name: "", search_space: nil, parameters: [], objectives: [], types: nil) if handle super(handle, retain: retain, auto_release: auto_release) else @@ -57,7 +59,7 @@ def initialize(handle = nil, retain: false, auto_release: true, p_parameters = MemoryPointer::new(:ccs_parameter_t, count) p_parameters.write_array_of_pointer(parameters.collect(&:handle)) ptr = MemoryPointer::new(:ccs_objective_space_t) - CCS.error_check CCS.ccs_create_objective_space(name, count, p_parameters, ocount, p_objs, p_types, ptr) + CCS.error_check CCS.ccs_create_objective_space(name, search_space.handle, count, p_parameters, ocount, p_objs, p_types, ptr) super(ptr.read_ccs_objective_space_t, retain: false) end end diff --git a/bindings/ruby/lib/cconfigspace/tree_tuner.rb b/bindings/ruby/lib/cconfigspace/tree_tuner.rb index 2e84fc66..37e7a2e9 100644 --- a/bindings/ruby/lib/cconfigspace/tree_tuner.rb +++ b/bindings/ruby/lib/cconfigspace/tree_tuner.rb @@ -96,16 +96,16 @@ def suggest end - attach_function :ccs_create_random_tree_tuner, [:string, :ccs_tree_space_t, :ccs_objective_space_t, :pointer], :ccs_result_t + attach_function :ccs_create_random_tree_tuner, [:string, :ccs_objective_space_t, :pointer], :ccs_result_t class RandomTreeTuner < TreeTuner def initialize(handle = nil, retain: false, auto_release: true, - name: "", tree_space: nil, objective_space: nil) + name: "", objective_space: nil) if handle super(handle, retain: retain, auto_release: auto_release) else ptr = MemoryPointer::new(:ccs_tree_tuner_t) - CCS.error_check CCS.ccs_create_random_tree_tuner(name, tree_space, objective_space, ptr) + CCS.error_check CCS.ccs_create_random_tree_tuner(name, objective_space, ptr) super(ptr.read_ccs_tree_tuner_t, retain: false) end end @@ -256,14 +256,14 @@ def self.wrap_user_defined_tree_tuner_callbacks(del, ask, tell, get_optima, get_ return [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper] end - attach_function :ccs_create_user_defined_tree_tuner, [:string, :ccs_tree_space_t, :ccs_objective_space_t, UserDefinedTreeTunerVector.by_ref, :value, :pointer], :ccs_result_t + attach_function :ccs_create_user_defined_tree_tuner, [:string, :ccs_objective_space_t, UserDefinedTreeTunerVector.by_ref, :value, :pointer], :ccs_result_t attach_function :ccs_user_defined_tree_tuner_get_tuner_data, [:ccs_tree_tuner_t, :pointer], :ccs_result_t class UserDefinedTreeTuner < TreeTuner add_property :tuner_data, :value, :ccs_user_defined_tree_tuner_get_tuner_data, memoize: true def initialize(handle = nil, retain: false, auto_release: true, - name: "", tree_space: nil, objective_space: nil, + name: "", objective_space: nil, del: nil, ask: nil, tell: nil, get_optima: nil, get_history: nil, suggest: nil, serialize: nil, deserialize: nil, tuner_data: nil) if handle super(handle, retain: retain, auto_release: auto_release) @@ -281,7 +281,7 @@ def initialize(handle = nil, retain: false, auto_release: true, vector[:serialize] = serializewrapper vector[:deserialize] = deserializewrapper ptr = MemoryPointer::new(:ccs_tree_tuner_t) - CCS.error_check CCS.ccs_create_user_defined_tree_tuner(name, tree_space, objective_space, vector, tuner_data, ptr) + CCS.error_check CCS.ccs_create_user_defined_tree_tuner(name, objective_space, vector, tuner_data, ptr) handle = ptr.read_ccs_tree_tuner_t super(handle, retain: false) CCS.register_vector(handle, [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper, tuner_data]) diff --git a/bindings/ruby/lib/cconfigspace/tuner.rb b/bindings/ruby/lib/cconfigspace/tuner.rb index 3bcd36d3..7c041de9 100644 --- a/bindings/ruby/lib/cconfigspace/tuner.rb +++ b/bindings/ruby/lib/cconfigspace/tuner.rb @@ -96,16 +96,16 @@ def suggest end - attach_function :ccs_create_random_tuner, [:string, :ccs_configuration_space_t, :ccs_objective_space_t, :pointer], :ccs_result_t + attach_function :ccs_create_random_tuner, [:string, :ccs_objective_space_t, :pointer], :ccs_result_t class RandomTuner < Tuner def initialize(handle = nil, retain: false, auto_release: true, - name: "", configuration_space: nil, objective_space: nil) + name: "", objective_space: nil) if handle super(handle, retain: retain, auto_release: auto_release) else ptr = MemoryPointer::new(:ccs_tuner_t) - CCS.error_check CCS.ccs_create_random_tuner(name, configuration_space, objective_space, ptr) + CCS.error_check CCS.ccs_create_random_tuner(name, objective_space, ptr) super(ptr.read_ccs_tuner_t, retain: false) end end @@ -256,14 +256,14 @@ def self.wrap_user_defined_tuner_callbacks(del, ask, tell, get_optima, get_histo return [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper] end - attach_function :ccs_create_user_defined_tuner, [:string, :ccs_configuration_space_t, :ccs_objective_space_t, UserDefinedTunerVector.by_ref, :value, :pointer], :ccs_result_t + attach_function :ccs_create_user_defined_tuner, [:string, :ccs_objective_space_t, UserDefinedTunerVector.by_ref, :value, :pointer], :ccs_result_t attach_function :ccs_user_defined_tuner_get_tuner_data, [:ccs_tuner_t, :pointer], :ccs_result_t class UserDefinedTuner < Tuner add_property :tuner_data, :value, :ccs_user_defined_tuner_get_tuner_data, memoize: true def initialize(handle = nil, retain: false, auto_release: true, - name: "", configuration_space: nil, objective_space: nil, + name: "", objective_space: nil, del: nil, ask: nil, tell: nil, get_optima: nil, get_history: nil, suggest: nil, serialize: nil, deserialize: nil, tuner_data: nil) if handle super(handle, retain: retain, auto_release: auto_release) @@ -281,7 +281,7 @@ def initialize(handle = nil, retain: false, auto_release: true, vector[:serialize] = serializewrapper vector[:deserialize] = deserializewrapper ptr = MemoryPointer::new(:ccs_tuner_t) - CCS.error_check CCS.ccs_create_user_defined_tuner(name, configuration_space, objective_space, vector, tuner_data, ptr) + CCS.error_check CCS.ccs_create_user_defined_tuner(name, objective_space, vector, tuner_data, ptr) handle = ptr.read_ccs_tuner_t super(handle, retain: false) CCS.register_vector(handle, [delwrapper, askwrapper, tellwrapper, get_optimawrapper, get_historywrapper, suggestwrapper, serializewrapper, deserializewrapper, tuner_data]) diff --git a/bindings/ruby/test/test_evaluation.rb b/bindings/ruby/test/test_evaluation.rb index 9d188b09..4dab77a2 100644 --- a/bindings/ruby/test/test_evaluation.rb +++ b/bindings/ruby/test/test_evaluation.rb @@ -15,7 +15,7 @@ def test_create v2 = CCS::NumericalParameter::Float.new e1 = CCS::Expression::Variable::new(parameter: v1) e2 = CCS::Expression::Variable::new(parameter: v2) - os = CCS::ObjectiveSpace::new(name: "ospace", parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) + os = CCS::ObjectiveSpace::new(name: "ospace", search_space: cs, parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) ev1 = CCS::Evaluation::new(objective_space: os, configuration: cs.sample, values: [0.5, 0.6]) assert_equal( [0.5, 0.6], ev1.values ) assert_equal( [0.5, 0.6], ev1.objective_values ) @@ -49,7 +49,7 @@ def test_serialize v2 = CCS::NumericalParameter::Float.new e1 = CCS::Expression::Variable::new(parameter: v1) e2 = CCS::Expression::Variable::new(parameter: v2) - os = CCS::ObjectiveSpace::new(name: "ospace", parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) + os = CCS::ObjectiveSpace::new(name: "ospace", search_space: cs, parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) evref = CCS::Evaluation::new(objective_space: os, configuration: cs.sample, values: [0.5, 0.6]) buff = evref.serialize handle_map = CCS::Map::new() diff --git a/bindings/ruby/test/test_features_tuner.rb b/bindings/ruby/test/test_features_tuner.rb index 100e3866..73d8b9b7 100644 --- a/bindings/ruby/test/test_features_tuner.rb +++ b/bindings/ruby/test/test_features_tuner.rb @@ -15,16 +15,16 @@ def create_tuning_problem v2 = CCS::NumericalParameter::Float.new(lower: -Float::INFINITY, upper: Float::INFINITY) e1 = CCS::Expression::Variable::new(parameter: v1) e2 = CCS::Expression::Variable::new(parameter: v2) - os = CCS::ObjectiveSpace::new(name: "ospace", parameters: [v1, v2], objectives: [e1, e2]) + os = CCS::ObjectiveSpace::new(name: "ospace", search_space: cs, parameters: [v1, v2], objectives: [e1, e2]) f1 = CCS::CategoricalParameter::new(values: [true, false]) fs = CCS::FeatureSpace::new(name: "fspace", parameters: [f1]) - [cs, fs, os] + [fs, os] end def test_create_random - cs, fs, os = create_tuning_problem - t = CCS::RandomFeaturesTuner::new(name: "tuner", configuration_space: cs, feature_space: fs, objective_space: os) + fs, os = create_tuning_problem + t = CCS::RandomFeaturesTuner::new(name: "tuner", feature_space: fs, objective_space: os) t2 = CCS::Object::from_handle(t) assert_equal( t.class, t2.class) assert_equal( "tuner", t.name ) @@ -126,8 +126,8 @@ def test_user_defined optis.sample.configuration end } - cs, fs, os = create_tuning_problem - t = CCS::UserDefinedFeaturesTuner::new(name: "tuner", configuration_space: cs, feature_space: fs, objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TunerData.new) + fs, os = create_tuning_problem + t = CCS::UserDefinedFeaturesTuner::new(name: "tuner", feature_space: fs, objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TunerData.new) t2 = CCS::Object::from_handle(t) assert_equal( t.class, t2.class) assert_equal( "tuner", t.name ) diff --git a/bindings/ruby/test/test_objective_space.rb b/bindings/ruby/test/test_objective_space.rb index a5a26ee3..1f74ee66 100644 --- a/bindings/ruby/test/test_objective_space.rb +++ b/bindings/ruby/test/test_objective_space.rb @@ -7,14 +7,17 @@ def setup end def test_create + h = CCS::NumericalParameter::Float.new + cs = CCS::ConfigurationSpace::new(name: "cs", parameters: [h]) h1 = CCS::NumericalParameter::Float.new h2 = CCS::NumericalParameter::Float.new h3 = CCS::NumericalParameter::Float.new e1 = CCS::Expression::Add.new(left: h1, right: h2) e2 = CCS::Expression::Variable.new(parameter: h3) - os = CCS::ObjectiveSpace::new(name: "space", parameters: [h1, h2, h3], objectives: [e1, e2], types: [:CCS_OBJECTIVE_TYPE_MINIMIZE, :CCS_OBJECTIVE_TYPE_MAXIMIZE]) + os = CCS::ObjectiveSpace::new(name: "space", search_space: cs, parameters: [h1, h2, h3], objectives: [e1, e2], types: [:CCS_OBJECTIVE_TYPE_MINIMIZE, :CCS_OBJECTIVE_TYPE_MAXIMIZE]) assert_equal( :CCS_OBJECT_TYPE_OBJECTIVE_SPACE, os.object_type ) assert_equal( "space", os.name ) + assert_equal( os.search_space.handle, cs.handle) assert_equal( 3, os.num_parameters ) assert_equal( h1, os.parameter(0) ) assert_equal( h2, os.parameter(1) ) diff --git a/bindings/ruby/test/test_tree_evaluation.rb b/bindings/ruby/test/test_tree_evaluation.rb index ce804fe3..293fbc5f 100644 --- a/bindings/ruby/test/test_tree_evaluation.rb +++ b/bindings/ruby/test/test_tree_evaluation.rb @@ -25,7 +25,7 @@ def test_create v2 = CCS::NumericalParameter::Float.new e1 = CCS::Expression::Variable.new(parameter: v1) e2 = CCS::Expression::Variable.new(parameter: v2) - os = CCS::ObjectiveSpace.new(name: 'ospace', parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) + os = CCS::ObjectiveSpace.new(name: 'ospace', search_space: ts, parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) ev1 = CCS::TreeEvaluation.new(objective_space: os, configuration: ts.sample, values: [0.5, 0.6]) assert_equal( [0.5, 0.6], ev1.values ) assert_equal( [0.5, 0.6], ev1.objective_values ) @@ -52,7 +52,7 @@ def test_serialize v2 = CCS::NumericalParameter::Float.new e1 = CCS::Expression::Variable.new(parameter: v1) e2 = CCS::Expression::Variable.new(parameter: v2) - os = CCS::ObjectiveSpace.new(name: 'ospace', parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) + os = CCS::ObjectiveSpace.new(name: 'ospace', search_space: ts, parameters: [v1, v2], objectives: { e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE, e2 => :CCS_OBJECTIVE_TYPE_MINIMIZE }) evref = CCS::TreeEvaluation.new(objective_space: os, configuration: ts.sample, values: [0.5, 0.6]) buff = evref.serialize handle_map = CCS::Map.new diff --git a/bindings/ruby/test/test_tree_tuner.rb b/bindings/ruby/test/test_tree_tuner.rb index 89bb5b97..96223993 100644 --- a/bindings/ruby/test/test_tree_tuner.rb +++ b/bindings/ruby/test/test_tree_tuner.rb @@ -23,13 +23,12 @@ def create_tuning_problem ts = CCS::StaticTreeSpace.new(name: 'space', tree: tree) v1 = CCS::NumericalParameter::Float.new(lower: -Float::INFINITY, upper: Float::INFINITY) e1 = CCS::Expression::Variable.new(parameter: v1) - os = CCS::ObjectiveSpace.new(name: 'ospace', parameters: [v1], objectives: {e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE}) - [ts, os] + os = CCS::ObjectiveSpace.new(name: 'ospace', search_space: ts, parameters: [v1], objectives: {e1 => :CCS_OBJECTIVE_TYPE_MAXIMIZE}) end def test_create_random - ts, os = create_tuning_problem - t = CCS::RandomTreeTuner.new(name: "tuner", tree_space: ts, objective_space: os) + os = create_tuning_problem + t = CCS::RandomTreeTuner.new(name: "tuner", objective_space: os) t2 = CCS::Object.from_handle(t) assert_equal(t.class, t2.class) assert_equal("tuner", t.name) @@ -116,8 +115,8 @@ def test_user_defined tuner.tuner_data.optima.sample.configuration end } - ts, os = create_tuning_problem - t = CCS::UserDefinedTreeTuner.new(name: "tuner", tree_space: ts, objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TreeTunerData.new) + os = create_tuning_problem + t = CCS::UserDefinedTreeTuner.new(name: "tuner", objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TreeTunerData.new) t2 = CCS::Object::from_handle(t) assert_equal( t.class, t2.class) assert_equal( "tuner", t.name ) diff --git a/bindings/ruby/test/test_tuner.rb b/bindings/ruby/test/test_tuner.rb index f353af6a..ba590193 100644 --- a/bindings/ruby/test/test_tuner.rb +++ b/bindings/ruby/test/test_tuner.rb @@ -15,13 +15,12 @@ def create_tuning_problem v2 = CCS::NumericalParameter::Float.new(lower: -Float::INFINITY, upper: Float::INFINITY) e1 = CCS::Expression::Variable::new(parameter: v1) e2 = CCS::Expression::Variable::new(parameter: v2) - os = CCS::ObjectiveSpace::new(name: "ospace", parameters: [v1, v2], objectives: [e1, e2]) - [cs, os] + os = CCS::ObjectiveSpace::new(name: "ospace", search_space: cs, parameters: [v1, v2], objectives: [e1, e2]) end def test_create_random - cs, os = create_tuning_problem - t = CCS::RandomTuner::new(name: "tuner", configuration_space: cs, objective_space: os) + os = create_tuning_problem + t = CCS::RandomTuner::new(name: "tuner", objective_space: os) t2 = CCS::Object::from_handle(t) assert_equal( t.class, t2.class) assert_equal( "tuner", t.name ) @@ -107,8 +106,8 @@ def test_user_defined tuner.tuner_data.optima.sample.configuration end } - cs, os = create_tuning_problem - t = CCS::UserDefinedTuner::new(name: "tuner", configuration_space: cs, objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TunerData.new) + os = create_tuning_problem + t = CCS::UserDefinedTuner::new(name: "tuner", objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, suggest: suggest, tuner_data: TunerData.new) t2 = CCS::Object::from_handle(t) assert_equal( t.class, t2.class) assert_equal( "tuner", t.name ) diff --git a/connectors/kokkos/ccs-kokkos-connector.cpp b/connectors/kokkos/ccs-kokkos-connector.cpp index 49e82854..ba908c48 100644 --- a/connectors/kokkos/ccs-kokkos-connector.cpp +++ b/connectors/kokkos/ccs-kokkos-connector.cpp @@ -544,7 +544,8 @@ kokkosp_request_values( otype = CCS_OBJECTIVE_TYPE_MINIMIZE; CCS_CHECK(ccs_create_objective_space( ("os (region: " + std::to_string(regionCounter) + ")").c_str(), - 1, &htime, 1, &expression, &otype, &os)); + (ccs_search_space_t)cs, 1, &htime, 1, &expression, &otype, + &os)); CCS_CHECK(ccs_release_object(expression)); CCS_CHECK(ccs_release_object(htime)); @@ -552,7 +553,7 @@ kokkosp_request_values( ("random tuner (region: " + std::to_string(regionCounter) + ")") .c_str(), - cs, fs, os, &tuner)); + fs, os, &tuner)); CCS_CHECK(ccs_release_object(cs)); CCS_CHECK(ccs_release_object(fs)); CCS_CHECK(ccs_release_object(os)); diff --git a/include/cconfigspace/base.h b/include/cconfigspace/base.h index 7722c1e0..41ad5286 100644 --- a/include/cconfigspace/base.h +++ b/include/cconfigspace/base.h @@ -105,6 +105,10 @@ typedef struct _ccs_context_s *ccs_context_t; * An opaque type defining a CCS distribution space. */ typedef struct _ccs_distribution_space_s *ccs_distribution_space_t; +/** + * An opaque type defining a CCS search space. + */ +typedef struct _ccs_search_space_s *ccs_search_space_t; /** * An opaque type defining a CCS configuration space. */ diff --git a/include/cconfigspace/configuration_space.h b/include/cconfigspace/configuration_space.h index c9ec08b7..aac9ae08 100644 --- a/include/cconfigspace/configuration_space.h +++ b/include/cconfigspace/configuration_space.h @@ -7,10 +7,10 @@ extern "C" { /** * @file configuration_space.h - * A configuration space is a context (see context.h) defining a set of - * parameters. Configuration space also offer as constraints system to - * describe conditional parameter activation as well as forbidden - * parameter configurations. + * A configuration space is a context (see context.h) defining a search + * space on a set of parameters. Configuration space also offer as + * constraints system to describe conditional parameter activation as + * well as forbidden parameter configurations. */ /** diff --git a/include/cconfigspace/features_tuner.h b/include/cconfigspace/features_tuner.h index 71273573..6909eb50 100644 --- a/include/cconfigspace/features_tuner.h +++ b/include/cconfigspace/features_tuner.h @@ -280,15 +280,15 @@ ccs_features_tuner_get_history( * viewed as a baseline for evaluating features tuners, and as a tool for * developing interfaces. * @param[in] name the name of the features tuner - * @param[in] configuration_space the configuration space to explore * @param[in] feature_space the feature space * @param[in] objective_space the objective space to potimize * @param[out] features_tuner_ret a pointer to the variable that will contain * the newly created features tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a - * valid CCS configuration space; or if \p objective_space is not a valid CCS - * objective space; or if \p feature_space is not a valid CCS feature space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not + * a valid CCS objective space; or if \p objective_space search space is + * not a valid CCS configuration space; or if \p feature_space is not a + * valid CCS feature space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p features * tuner_ret is NULL * @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was not enough memory to @@ -298,11 +298,10 @@ ccs_features_tuner_get_history( */ extern ccs_result_t ccs_create_random_features_tuner( - const char *name, - ccs_configuration_space_t configuration_space, - ccs_feature_space_t feature_space, - ccs_objective_space_t objective_space, - ccs_features_tuner_t *features_tuner_ret); + const char *name, + ccs_feature_space_t feature_space, + ccs_objective_space_t objective_space, + ccs_features_tuner_t *features_tuner_ret); /** * A structure that define the callbacks the user must provide to create a user @@ -392,7 +391,6 @@ typedef struct ccs_user_defined_features_tuner_vector_s /** * Create a new user defined features tuner. * @param[in] name the name of the features tuner - * @param[in] configuration_space the configuration space to explore * @param[in] feature_space the feature space * @param[in] objective_space the objective space to optimize * @param[in] vector the vector of callbacks implementing the features tuner @@ -402,9 +400,10 @@ typedef struct ccs_user_defined_features_tuner_vector_s * @param[out] features_tuner_ret a pointer to the variable that will contain * the newly created features tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a - * valid CCS configuration space; or if \p objective_space is not a valid CCS - * objective space; or if \p feature_space is not a valid CCS feature space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not + * a valid CCS objective space; or if \p objective_space search space is + * not a valid CCS configuration space; or if \p feature_space is not a + * valid CCS feature space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p * features_tuner_ret is NULL; or if \p vector is NULL; or if any interface * pointer except suggest is NULL @@ -416,7 +415,6 @@ typedef struct ccs_user_defined_features_tuner_vector_s extern ccs_result_t ccs_create_user_defined_features_tuner( const char *name, - ccs_configuration_space_t configuration_space, ccs_feature_space_t feature_space, ccs_objective_space_t objective_space, ccs_user_defined_features_tuner_vector_t *vector, diff --git a/include/cconfigspace/objective_space.h b/include/cconfigspace/objective_space.h index 78067d89..90e5a283 100644 --- a/include/cconfigspace/objective_space.h +++ b/include/cconfigspace/objective_space.h @@ -8,8 +8,9 @@ extern "C" { /** * @file objective_space.h * An objective space is a context (see context.h) defining a set of - * parameters. Objective space also define a list of expressions (see - * expression.h) over those parameters called objectives. + * result parameters of a search space. Objective spaces also define a + * list of expressions (see expression.h) over those parameters called + * objectives. */ /** @@ -45,10 +46,10 @@ typedef enum ccs_objective_type_e ccs_objective_type_t; * the newly created objective space * @return #CCS_RESULT_SUCCESS on success * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p - * objective_space_ret is NULL; or if \p parameters is NULL; or if \p - * num_parameters is NULL; or if \p objectives is NULL and \p - * num_objectives is greater than 0; of if \p types is NULL and \p - * num_objectives is greater than 0 + * search_space is null; or if \p objective_space_ret is NULL; or if \p + * parameters is NULL; or if \p num_parameters is NULL; or if \p + * objectives is NULL and \p num_objectives is greater than 0; of if \p + * types is NULL and \p num_objectives is greater than 0 * @return #CCS_RESULT_ERROR_INVALID_OBJECT if a parameter is not a valid CCS * parameter; or if an expressions is not a valid CCS expression * @return #CCS_RESULT_ERROR_INVALID_PARAMETER if a parameter appears more than @@ -62,6 +63,7 @@ typedef enum ccs_objective_type_e ccs_objective_type_t; extern ccs_result_t ccs_create_objective_space( const char *name, + ccs_search_space_t search_space, size_t num_parameters, ccs_parameter_t *parameters, size_t num_objectives, @@ -70,45 +72,35 @@ ccs_create_objective_space( ccs_objective_space_t *objective_space_ret); /** - * Check that a evaluation is a valid in a objective space. + * Get the search space of an objective space. * @param[in] objective_space - * @param[in] evaluation - * @param[out] is_valid_ret a pointer to a variable that will hold the result - * of the check. Result will be #CCS_TRUE if the - * evaluation is valid. Result will be #CCS_FALSE - * if an active parameter value is not a valid value - * for this parameter; or if an inactive parameter - * value is not inactive; or if a forbidden clause - * would be evaluating to #ccs_true + * @param[out] search_space_ret a pointer to the variable that will + * contain the returned search space * @return #CCS_RESULT_SUCCESS on success * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not a - * valid CCS objective space; or if \p evaluation is not a valid CCS - * evaluation binding - * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p is_valid_ret is NULL - * @return #CCS_RESULT_ERROR_INVALID_EVALUATION_BINDING if \p evaluation - * is not associated to the objective space + * valid CCS objective space + * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p search_space_ret is NULL * @remarks * This function is thread-safe */ extern ccs_result_t -ccs_objective_space_check_evaluation( - ccs_objective_space_t objective_space, - ccs_evaluation_binding_t evaluation, - ccs_bool_t *is_valid_ret); +ccs_objective_space_get_search_space( + ccs_objective_space_t objective_space, + ccs_search_space_t *search_space_ret); /** * Get the objective of rank index in a objective space. * @param[in] objective_space * @param[in] index the index of the objective to retrieve - * @param[out] expression_ret a pointer to the variable that will contain the - * returned expression - * @param[out] type_ret a pointer to the variable that will contain the returned - * objective type + * @param[out] expression_ret a pointer to the variable that will contain + * the returned expression + * @param[out] type_ret a pointer to the variable that will contain the + * returned objective type * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not a valid - * CCS objective space - * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p expression_ret or \p type_ret - * are NULL + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not a + * valid CCS objective space + * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p expression_ret or \p + * type_ret are NULL * @return #CCS_RESULT_ERROR_OUT_OF_BOUNDS if \p index is greater than the * number of objectives in the objective space * @remarks @@ -154,6 +146,33 @@ ccs_objective_space_get_objectives( ccs_objective_type_t *types, size_t *num_objectives_ret); +/** + * Check that a evaluation is a valid in a objective space. + * @param[in] objective_space + * @param[in] evaluation + * @param[out] is_valid_ret a pointer to a variable that will hold the result + * of the check. Result will be #CCS_TRUE if the + * evaluation is valid. Result will be #CCS_FALSE + * if an active parameter value is not a valid value + * for this parameter; or if an inactive parameter + * value is not inactive; or if a forbidden clause + * would be evaluating to #ccs_true + * @return #CCS_RESULT_SUCCESS on success + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not a + * valid CCS objective space; or if \p evaluation is not a valid CCS + * evaluation binding + * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p is_valid_ret is NULL + * @return #CCS_RESULT_ERROR_INVALID_EVALUATION_BINDING if \p evaluation + * is not associated to the objective space + * @remarks + * This function is thread-safe + */ +extern ccs_result_t +ccs_objective_space_check_evaluation( + ccs_objective_space_t objective_space, + ccs_evaluation_binding_t evaluation, + ccs_bool_t *is_valid_ret); + #ifdef __cplusplus } #endif diff --git a/include/cconfigspace/tree_tuner.h b/include/cconfigspace/tree_tuner.h index 1d439811..1649b854 100644 --- a/include/cconfigspace/tree_tuner.h +++ b/include/cconfigspace/tree_tuner.h @@ -232,13 +232,13 @@ ccs_tree_tuner_get_history( * Create a new random tree tuner. The random tuner should be viewed as a * baseline for evaluating tuners, and as a tool for developing interfaces. * @param[in] name the name of the tree tuner - * @param[in] tree_space the configuration space to explore * @param[in] objective_space the objective space to potimize * @param[out] tuner_ret a pointer to the variable that will contain the * newly created tree tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p tree_space is not a valid CCS - * tree space; or if \p objective_space is not a valid CCS objective space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not + * a valid CCS objective space; or if \p objective_space search space is + * not a valid CCS tree space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p * tuner_ret is NULL * @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was not enough memory to @@ -249,7 +249,6 @@ ccs_tree_tuner_get_history( extern ccs_result_t ccs_create_random_tree_tuner( const char *name, - ccs_tree_space_t tree_space, ccs_objective_space_t objective_space, ccs_tree_tuner_t *tuner_ret); @@ -336,7 +335,6 @@ typedef struct ccs_user_defined_tree_tuner_vector_s /** * Create a new user defined tree tuner. * @param[in] name the name of the tuner - * @param[in] tree_space the tree space to explore * @param[in] objective_space the objective space to optimize * @param[in] vector the vector of callbacks implementing the tuner interface * @param[in] tuner_data a pointer to the tuner internal data structures. Can be @@ -344,9 +342,9 @@ typedef struct ccs_user_defined_tree_tuner_vector_s * @param[out] tuner_ret a pointer to the variable that will contain the newly * created tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p tree_space is not a valid CCS - * tree space; or if \p objective_space is - * not a valid CCS objective space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT \p objective_space is not a + * valid CCS objective space; or if \p objective_space search space is + * not a valid CCS tree space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p * tuner_ret is NULL; or if \p vector is NULL; or if any interface pointer * except suggest is NULL @@ -358,7 +356,6 @@ typedef struct ccs_user_defined_tree_tuner_vector_s extern ccs_result_t ccs_create_user_defined_tree_tuner( const char *name, - ccs_tree_space_t tree_space, ccs_objective_space_t objective_space, ccs_user_defined_tree_tuner_vector_t *vector, void *tuner_data, diff --git a/include/cconfigspace/tuner.h b/include/cconfigspace/tuner.h index 3dc4c9ac..49abc9a5 100644 --- a/include/cconfigspace/tuner.h +++ b/include/cconfigspace/tuner.h @@ -223,14 +223,13 @@ ccs_tuner_get_history( * Create a new random tuner. The random tuner should be viewed as a baseline * for evaluating tuners, and as a tool for developing interfaces. * @param[in] name the name of the tuner - * @param[in] configuration_space the configuration space to explore - * @param[in] objective_space the objective space to potimize + * @param[in] objective_space the objective space to optimize * @param[out] tuner_ret a pointer to the variable that will contain the newly * created tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a - * valid CCS configuration space; or if \p objective_space is not a valid CCS - * objective space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not + * a valid CCS objective space; or if \p objective_space search space is + * not a valid CCS configuration space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p * tuner_ret is NULL * @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was not enough memory to @@ -240,10 +239,9 @@ ccs_tuner_get_history( */ extern ccs_result_t ccs_create_random_tuner( - const char *name, - ccs_configuration_space_t configuration_space, - ccs_objective_space_t objective_space, - ccs_tuner_t *tuner_ret); + const char *name, + ccs_objective_space_t objective_space, + ccs_tuner_t *tuner_ret); /** * A structure that define the callbacks the user must provide to create a user @@ -320,7 +318,6 @@ typedef struct ccs_user_defined_tuner_vector_s ccs_user_defined_tuner_vector_t; /** * Create a new user defined tuner. * @param[in] name the name of the tuner - * @param[in] configuration_space the configuration space to explore * @param[in] objective_space the objective space to optimize * @param[in] vector the vector of callbacks implementing the tuner interface * @param[in] tuner_data a pointer to the tuner internal data structures. Can be @@ -328,9 +325,9 @@ typedef struct ccs_user_defined_tuner_vector_s ccs_user_defined_tuner_vector_t; * @param[out] tuner_ret a pointer to the variable that will contain the newly * created tuner * @return #CCS_RESULT_SUCCESS on success - * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a - * valid CCS configuration space; or if \p objective_space is not a valid CCS - * objective space + * @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p objective_space is not + * a valid CCS objective space; or if \p objective_space search space is + * not a valid CCS configuration space * @return #CCS_RESULT_ERROR_INVALID_VALUE if \p name is NULL; or if \p * tuner_ret is NULL; or if \p vector is NULL; or if any non optional interface * pointer is NULL @@ -342,7 +339,6 @@ typedef struct ccs_user_defined_tuner_vector_s ccs_user_defined_tuner_vector_t; extern ccs_result_t ccs_create_user_defined_tuner( const char *name, - ccs_configuration_space_t configuration_space, ccs_objective_space_t objective_space, ccs_user_defined_tuner_vector_t *vector, void *tuner_data, diff --git a/samples/test_python.c b/samples/test_python.c index febca37b..a7b87f1b 100644 --- a/samples/test_python.c +++ b/samples/test_python.c @@ -17,7 +17,7 @@ create_numerical(const char *name, double lower, double upper) } void -create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) +create_problem(ccs_objective_space_t *os) { ccs_parameter_t parameter1, parameter2; ccs_parameter_t parameters[2]; @@ -41,7 +41,8 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) otype = CCS_OBJECTIVE_TYPE_MINIMIZE; err = ccs_create_objective_space( - "height", 1, ¶meter3, 1, &expression, &otype, &ospace); + "height", (ccs_search_space_t)cspace, 1, ¶meter3, 1, + &expression, &otype, &ospace); assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(parameter1); @@ -52,8 +53,9 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(expression); assert(err == CCS_RESULT_SUCCESS); + err = ccs_release_object(cspace); + assert(err == CCS_RESULT_SUCCESS); - *cs = cspace; *os = ospace; } @@ -113,12 +115,11 @@ test_tuner(ccs_tuner_t tuner, ccs_objective_space_t ospace) void test(void) { - ccs_tuner_t t; - ccs_configuration_space_t cs; - ccs_objective_space_t os; - ccs_result_t err; + ccs_tuner_t t; + ccs_objective_space_t os; + ccs_result_t err; - create_problem(&cs, &os); + create_problem(&os); PyObject *pName, *pModule, *pFunc; PyObject *pArgs, *pValue, *pHandle, *pAddr; @@ -153,11 +154,9 @@ test(void) if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, "create_test_tuner"); - pArgs = PyTuple_New(2); - pValue = PyLong_FromVoidPtr(cs); - PyTuple_SetItem(pArgs, 0, pValue); + pArgs = PyTuple_New(1); pValue = PyLong_FromVoidPtr(os); - PyTuple_SetItem(pArgs, 1, pValue); + PyTuple_SetItem(pArgs, 0, pValue); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); Py_DECREF(pFunc); @@ -179,8 +178,6 @@ test(void) } err = ccs_release_object(os); assert(err == CCS_RESULT_SUCCESS); - err = ccs_release_object(cs); - assert(err == CCS_RESULT_SUCCESS); Py_Finalize(); return; #if PY_VERSION_HEX >= 0x030b0000 diff --git a/samples/test_python.py b/samples/test_python.py index 83083220..3992eb9a 100644 --- a/samples/test_python.py +++ b/samples/test_python.py @@ -4,7 +4,7 @@ from math import sin class TestTuner(ccs.UserDefinedTuner): - def __init__(self, cs, os): + def __init__(self, os): data = [[], []] def delete(tuner): @@ -44,15 +44,13 @@ def get_history(tuner): def get_optima(tuner): return tuner.tuner_data[1] - super().__init__(name = "tuner", configuration_space = cs, objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, tuner_data = data) + super().__init__(name = "tuner", objective_space = os, delete = delete, ask = ask, tell = tell, get_optima = get_optima, get_history = get_history, tuner_data = data) -def create_test_tuner(cs_ptr, os_ptr): - cs_handle = ccs.ccs_configuration_space(cs_ptr) +def create_test_tuner(os_ptr): os_handle = ccs.ccs_objective_space(os_ptr) - cs = ccs.ConfigurationSpace.from_handle(cs_handle) os = ccs.ObjectiveSpace.from_handle(os_handle) - t = TestTuner(cs, os) + t = TestTuner(os) return t def create_tuning_problem(): @@ -64,18 +62,18 @@ def create_tuning_problem(): v2 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf')) e1 = ccs.Expression.Variable(parameter = v1) e2 = ccs.Expression.Variable(parameter = v2) - os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2], objectives = [e1, e2]) - return (cs, os) + os = ccs.ObjectiveSpace(name = "ospace", search_space = cs, parameters = [v1, v2], objectives = [e1, e2]) + return os class Test(unittest.TestCase): def test_user_defined(self): - (cs, os) = create_tuning_problem() - t = TestTuner(cs, os) + os = create_tuning_problem() + t = TestTuner(os) self.assertEqual("tuner", t.name) self.assertEqual(ccs.TunerType.USER_DEFINED, t.type) - self.assertEqual(cs.handle.value, t.configuration_space.handle.value) self.assertEqual(os.handle.value, t.objective_space.handle.value) + self.assertEqual(os.search_space.handle.value, t.configuration_space.handle.value) func = lambda x, y, z: [(x-2)*(x-2), sin(z+y)] evals = [ccs.Evaluation(objective_space = os, configuration = c, values = func(*(c.values))) for c in t.ask(100)] t.tell(evals) diff --git a/samples/test_ruby.c b/samples/test_ruby.c index dec7eb90..ba865279 100644 --- a/samples/test_ruby.c +++ b/samples/test_ruby.c @@ -26,7 +26,7 @@ create_numerical(const char *name, double lower, double upper) } void -create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) +create_problem(ccs_objective_space_t *os) { ccs_parameter_t parameter1, parameter2; ccs_parameter_t parameters[2]; @@ -50,7 +50,8 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) otype = CCS_OBJECTIVE_TYPE_MINIMIZE; err = ccs_create_objective_space( - "height", 1, ¶meter3, 1, &expression, &otype, &ospace); + "height", (ccs_search_space_t)cspace, 1, ¶meter3, 1, + &expression, &otype, &ospace); assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(parameter1); @@ -61,8 +62,9 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os) assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(expression); assert(err == CCS_RESULT_SUCCESS); + err = ccs_release_object(cspace); + assert(err == CCS_RESULT_SUCCESS); - *cs = cspace; *os = ospace; } @@ -122,14 +124,13 @@ test_tuner(ccs_tuner_t tuner, ccs_objective_space_t ospace) void test(void) { - ccs_tuner_t t; - ccs_configuration_space_t cs; - ccs_objective_space_t os; - ccs_result_t err; - int state; - VALUE ruby_stack_start; + ccs_tuner_t t; + ccs_objective_space_t os; + ccs_result_t err; + int state; + VALUE ruby_stack_start; - create_problem(&cs, &os); + create_problem(&os); ruby_init_stack(&ruby_stack_start); ruby_init(); @@ -152,8 +153,8 @@ test(void) } VALUE tuner; tuner = rb_funcall( - rb_current_receiver(), rb_intern("create_test_tuner"), 2, - ULL2NUM((uintptr_t)cs), ULL2NUM((uintptr_t)os)); + rb_current_receiver(), rb_intern("create_test_tuner"), 1, + ULL2NUM((uintptr_t)os)); t = (ccs_tuner_t)NUM2ULL(rb_funcall( rb_funcall(tuner, rb_intern("handle"), 0), rb_intern("to_i"), 0)); @@ -163,8 +164,6 @@ test(void) assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(os); assert(err == CCS_RESULT_SUCCESS); - err = ccs_release_object(cs); - assert(err == CCS_RESULT_SUCCESS); ruby_cleanup(0); } diff --git a/samples/test_ruby.rb b/samples/test_ruby.rb index a990a668..66191a5a 100644 --- a/samples/test_ruby.rb +++ b/samples/test_ruby.rb @@ -1,7 +1,7 @@ require_relative '../bindings/ruby/lib/cconfigspace' class TestTuner < CCS::UserDefinedTuner - def initialize(cs, os) + def initialize(os) @history = [] @optima = [] del = lambda { |tuner| nil } @@ -42,15 +42,13 @@ def initialize(cs, os) get_optima = lambda { |tuner| tuner.tuner_data[1] } - super(name: "tuner", configuration_space: cs, objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, tuner_data: [[],[]]) + super(name: "tuner", objective_space: os, del: del, ask: ask, tell: tell, get_optima: get_optima, get_history: get_history, tuner_data: [[],[]]) end end -def create_test_tuner(cs_ptr, os_ptr) - cs_handle = FFI::Pointer::new(cs_ptr) +def create_test_tuner(os_ptr) os_handle = FFI::Pointer::new(os_ptr) - TestTuner::new(CCS::ConfigurationSpace.from_handle(cs_handle), - CCS::ObjectiveSpace.from_handle(os_handle)) + TestTuner::new(CCS::ObjectiveSpace.from_handle(os_handle)) end def create_tuning_problem @@ -62,8 +60,7 @@ def create_tuning_problem v2 = CCS::NumericalParameter::Float.new(lower: -Float::INFINITY, upper: Float::INFINITY) e1 = CCS::Expression::Variable::new(parameter: v1) e2 = CCS::Expression::Variable::new(parameter: v2) - os = CCS::ObjectiveSpace::new(name: "ospace", parameters: [v1, v2], objectives: [e1, e2]) - [cs, os] + os = CCS::ObjectiveSpace::new(name: "ospace", search_space: cs, parameters: [v1, v2], objectives: [e1, e2]) end diff --git a/src/Makefile.am b/src/Makefile.am index 7cc24ba8..e0aebcdd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -60,6 +60,7 @@ libcconfigspace_la_SOURCES = \ context.c \ context_internal.h \ context_deserialize.h \ + search_space_internal.h \ configuration_space.c \ configuration_space_internal.h \ configuration_space_deserialize.h \ diff --git a/src/cconfigspace_deserialize.h b/src/cconfigspace_deserialize.h index 13f84f2c..f37666f6 100644 --- a/src/cconfigspace_deserialize.h +++ b/src/cconfigspace_deserialize.h @@ -11,6 +11,15 @@ _ccs_object_deserialize_with_opts_check( const char **buffer, _ccs_object_deserialize_options_t *opts); +static inline ccs_result_t +_ccs_object_deserialize_with_opts( + ccs_object_t *object_ret, + ccs_serialize_format_t format, + uint32_t version, + size_t *buffer_size, + const char **buffer, + _ccs_object_deserialize_options_t *opts); + #include "rng_deserialize.h" #include "distribution_deserialize.h" #include "parameter_deserialize.h" diff --git a/src/cconfigspace_internal.h b/src/cconfigspace_internal.h index 238c0b34..32856d6c 100644 --- a/src/cconfigspace_internal.h +++ b/src/cconfigspace_internal.h @@ -220,7 +220,9 @@ _ccs_do_nothing(void) #define CCS_OBJ_IS_VALID(o) ((o) && ((_ccs_object_template_t *)(o))->data) -#define CCS_OBJ_IS_TYPE(o, t) (((_ccs_object_template_t *)(o))->obj.type == (t)) +#define CCS_OBJ_TYPE(o) (((_ccs_object_template_t *)(o))->obj.type) + +#define CCS_OBJ_IS_TYPE(o, t) (CCS_OBJ_TYPE(o) == (t)) #define CCS_CHECK_OBJ(o, t) \ CCS_REFUTE_MSG( \ @@ -269,6 +271,15 @@ _ccs_do_nothing(void) CCS_RESULT_ERROR_INVALID_OBJECT, \ "Invalid CCS binding '%s' == %p supplied", #b, b) +#define CCS_CHECK_SEARCH_SPACE(s) \ + CCS_REFUTE_MSG( \ + !CCS_OBJ_IS_VALID(s) || \ + !(CCS_OBJ_IS_TYPE(s, CCS_OBJECT_TYPE_TREE_SPACE) || \ + CCS_OBJ_IS_TYPE( \ + s, CCS_OBJECT_TYPE_CONFIGURATION_SPACE)), \ + CCS_RESULT_ERROR_INVALID_OBJECT, \ + "Invalid CCS search space '%s' == %p supplied", #s, s) + #define CCS_CHECK_PTR(p) \ CCS_REFUTE_MSG( \ !(p), CCS_RESULT_ERROR_INVALID_VALUE, \ @@ -1236,6 +1247,20 @@ _ccs_deserialize_bin_ccs_object_internal( return CCS_RESULT_SUCCESS; } +static inline ccs_result_t +_ccs_peek_bin_ccs_object_internal( + _ccs_object_internal_t *obj, + size_t *buffer_size, + const char **buffer, + ccs_object_t *handle_ret) +{ + size_t buff_size = *buffer_size; + const char *buff = *buffer; + CCS_VALIDATE(_ccs_deserialize_bin_ccs_object_internal( + obj, &buff_size, &buff, handle_ret)); + return CCS_RESULT_SUCCESS; +} + static inline ccs_result_t _ccs_object_handle_check_add( ccs_map_t map, diff --git a/src/features_tuner_deserialize.h b/src/features_tuner_deserialize.h index f80ad7d4..20631863 100644 --- a/src/features_tuner_deserialize.h +++ b/src/features_tuner_deserialize.h @@ -24,11 +24,6 @@ _ccs_deserialize_bin_ccs_features_tuner_common_data( &data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_deserialize_bin_string(&data->name, buffer_size, buffer)); - CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( - (ccs_object_t *)&data->configuration_space, - CCS_OBJECT_TYPE_CONFIGURATION_SPACE, - CCS_SERIALIZE_FORMAT_BINARY, version, buffer_size, buffer, - opts)); CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( (ccs_object_t *)&data->objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE, CCS_SERIALIZE_FORMAT_BINARY, @@ -131,9 +126,7 @@ _ccs_deserialize_bin_random_features_tuner( CCS_VALIDATE_ERR_GOTO( res, ccs_create_random_features_tuner( - data.common_data.name, - data.common_data.configuration_space, - data.common_data.feature_space, + data.common_data.name, data.common_data.feature_space, data.common_data.objective_space, features_tuner_ret), features_evaluations); odata = (_ccs_random_features_tuner_data_clone_t @@ -150,8 +143,6 @@ _ccs_deserialize_bin_random_features_tuner( for (size_t i = 0; i < data.size_history; i++) ccs_release_object(data.history[i]); end: - if (data.common_data.configuration_space) - ccs_release_object(data.common_data.configuration_space); if (data.common_data.objective_space) ccs_release_object(data.common_data.objective_space); if (data.common_data.feature_space) @@ -212,7 +203,6 @@ _ccs_deserialize_bin_user_defined_features_tuner( res, ccs_create_user_defined_features_tuner( data.base_data.common_data.name, - data.base_data.common_data.configuration_space, data.base_data.common_data.feature_space, data.base_data.common_data.objective_space, vector, opts->data, features_tuner_ret), @@ -244,9 +234,6 @@ _ccs_deserialize_bin_user_defined_features_tuner( for (size_t i = 0; i < data.base_data.size_history; i++) ccs_release_object(data.base_data.history[i]); end: - if (data.base_data.common_data.configuration_space) - ccs_release_object( - data.base_data.common_data.configuration_space); if (data.base_data.common_data.objective_space) ccs_release_object(data.base_data.common_data.objective_space); if (data.base_data.common_data.feature_space) diff --git a/src/features_tuner_internal.h b/src/features_tuner_internal.h index 74326887..d5d9933b 100644 --- a/src/features_tuner_internal.h +++ b/src/features_tuner_internal.h @@ -68,9 +68,6 @@ _ccs_serialize_bin_size_ccs_features_tuner_common_data( *cum_size += _ccs_serialize_bin_size_ccs_features_tuner_type(data->type); *cum_size += _ccs_serialize_bin_size_string(data->name); - CCS_VALIDATE(data->configuration_space->obj.ops->serialize_size( - data->configuration_space, CCS_SERIALIZE_FORMAT_BINARY, - cum_size, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize_size( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, cum_size, opts)); @@ -91,9 +88,6 @@ _ccs_serialize_bin_ccs_features_tuner_common_data( data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_serialize_bin_string(data->name, buffer_size, buffer)); - CCS_VALIDATE(data->configuration_space->obj.ops->serialize( - data->configuration_space, CCS_SERIALIZE_FORMAT_BINARY, - buffer_size, buffer, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, buffer_size, buffer, opts)); diff --git a/src/features_tuner_random.c b/src/features_tuner_random.c index 475ebc93..a71ee62d 100644 --- a/src/features_tuner_random.c +++ b/src/features_tuner_random.c @@ -463,16 +463,18 @@ static const UT_icd _evaluation_icd = { } ccs_result_t ccs_create_random_features_tuner( - const char *name, - ccs_configuration_space_t configuration_space, - ccs_feature_space_t feature_space, - ccs_objective_space_t objective_space, - ccs_features_tuner_t *tuner_ret) + const char *name, + ccs_feature_space_t feature_space, + ccs_objective_space_t objective_space, + ccs_features_tuner_t *tuner_ret) { + ccs_configuration_space_t configuration_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_OBJ(feature_space, CCS_OBJECT_TYPE_FEATURE_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&configuration_space)); + CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_PTR(tuner_ret); uintptr_t mem = (uintptr_t)calloc( diff --git a/src/features_tuner_user_defined.c b/src/features_tuner_user_defined.c index 7d260043..b02c93cd 100644 --- a/src/features_tuner_user_defined.c +++ b/src/features_tuner_user_defined.c @@ -323,17 +323,19 @@ static _ccs_features_tuner_ops_t _ccs_features_tuner_user_defined_ops = { ccs_result_t ccs_create_user_defined_features_tuner( const char *name, - ccs_configuration_space_t configuration_space, ccs_feature_space_t feature_space, ccs_objective_space_t objective_space, ccs_user_defined_features_tuner_vector_t *vector, void *tuner_data, ccs_features_tuner_t *tuner_ret) { + ccs_configuration_space_t configuration_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_OBJ(feature_space, CCS_OBJECT_TYPE_FEATURE_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&configuration_space)); + CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_PTR(tuner_ret); CCS_CHECK_PTR(vector); CCS_CHECK_PTR(vector->del); diff --git a/src/objective_space.c b/src/objective_space.c index de24297b..9c71d14c 100644 --- a/src/objective_space.c +++ b/src/objective_space.c @@ -1,4 +1,5 @@ #include "cconfigspace_internal.h" +#include "search_space_internal.h" #include "objective_space_internal.h" #include "evaluation_binding_internal.h" #include "expression_internal.h" @@ -7,11 +8,15 @@ static ccs_result_t _ccs_objective_space_del(ccs_object_t object) { ccs_objective_space_t objective_space = (ccs_objective_space_t)object; - size_t num_parameters = objective_space->data->num_parameters; - ccs_parameter_t *parameters = objective_space->data->parameters; - size_t num_objectives = objective_space->data->num_objectives; - _ccs_objective_t *objectives = objective_space->data->objectives; + _ccs_objective_space_data_t *data = objective_space->data; + ccs_search_space_t search_space = data->search_space; + size_t num_parameters = data->num_parameters; + ccs_parameter_t *parameters = data->parameters; + size_t num_objectives = data->num_objectives; + _ccs_objective_t *objectives = data->objectives; + if (search_space) + ccs_release_object(search_space); for (size_t i = 0; i < num_parameters; i++) if (parameters[i]) ccs_release_object(parameters[i]); @@ -31,12 +36,17 @@ _ccs_serialize_bin_size_ccs_objective_space_data( size_t *cum_size, _ccs_object_serialize_options_t *opts) { - size_t num_parameters = data->num_parameters; - ccs_parameter_t *parameters = data->parameters; - size_t num_objectives = data->num_objectives; - _ccs_objective_t *objectives = data->objectives; + ccs_search_space_t search_space = data->search_space; + size_t num_parameters = data->num_parameters; + ccs_parameter_t *parameters = data->parameters; + size_t num_objectives = data->num_objectives; + _ccs_objective_t *objectives = data->objectives; *cum_size += _ccs_serialize_bin_size_string(data->name); + + CCS_VALIDATE(search_space->obj.ops->serialize_size( + search_space, CCS_SERIALIZE_FORMAT_BINARY, cum_size, opts)); + *cum_size += _ccs_serialize_bin_size_size(num_parameters); *cum_size += _ccs_serialize_bin_size_size(num_objectives); @@ -65,13 +75,19 @@ _ccs_serialize_bin_ccs_objective_space_data( char **buffer, _ccs_object_serialize_options_t *opts) { - size_t num_parameters = data->num_parameters; - ccs_parameter_t *parameters = data->parameters; - size_t num_objectives = data->num_objectives; - _ccs_objective_t *objectives = data->objectives; + ccs_search_space_t search_space = data->search_space; + size_t num_parameters = data->num_parameters; + ccs_parameter_t *parameters = data->parameters; + size_t num_objectives = data->num_objectives; + _ccs_objective_t *objectives = data->objectives; CCS_VALIDATE( _ccs_serialize_bin_string(data->name, buffer_size, buffer)); + + CCS_VALIDATE(search_space->obj.ops->serialize( + search_space, CCS_SERIALIZE_FORMAT_BINARY, buffer_size, buffer, + opts)); + CCS_VALIDATE( _ccs_serialize_bin_size(num_parameters, buffer_size, buffer)); CCS_VALIDATE( @@ -210,6 +226,7 @@ _ccs_objective_space_add_objectives( ccs_result_t ccs_create_objective_space( const char *name, + ccs_search_space_t search_space, size_t num_parameters, ccs_parameter_t *parameters, size_t num_objectives, @@ -218,6 +235,7 @@ ccs_create_objective_space( ccs_objective_space_t *objective_space_ret) { CCS_CHECK_PTR(name); + CCS_CHECK_SEARCH_SPACE(search_space); CCS_CHECK_PTR(objective_space_ret); CCS_CHECK_ARY(num_parameters, parameters); CCS_CHECK_ARY(num_objectives, objectives); @@ -261,7 +279,9 @@ ccs_create_objective_space( _ccs_objective_space_add_objectives( obj_space, num_objectives, objectives, types), errparams); - *objective_space_ret = obj_space; + CCS_VALIDATE_ERR_GOTO(err, ccs_retain_object(search_space), errparams); + obj_space->data->search_space = search_space; + *objective_space_ret = obj_space; return CCS_RESULT_SUCCESS; errparams: _ccs_objective_space_del(obj_space); @@ -270,6 +290,17 @@ ccs_create_objective_space( return err; } +ccs_result_t +ccs_objective_space_get_search_space( + ccs_objective_space_t objective_space, + ccs_search_space_t *search_space_ret) +{ + CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_CHECK_PTR(search_space_ret); + *search_space_ret = objective_space->data->search_space; + return CCS_RESULT_SUCCESS; +} + static inline ccs_result_t _check_evaluation( ccs_objective_space_t objective_space, diff --git a/src/objective_space_deserialize.h b/src/objective_space_deserialize.h index 2e48fda6..5a84819a 100644 --- a/src/objective_space_deserialize.h +++ b/src/objective_space_deserialize.h @@ -4,6 +4,8 @@ struct _ccs_objective_space_data_mock_s { const char *name; + ccs_object_t search_space_handle; + ccs_search_space_t search_space; size_t num_parameters; size_t num_objectives; ccs_parameter_t *parameters; @@ -20,10 +22,17 @@ _ccs_deserialize_bin_ccs_objective_space_data( const char **buffer, _ccs_object_deserialize_options_t *opts) { - uintptr_t mem; + uintptr_t mem; + _ccs_object_internal_t obj; CCS_VALIDATE( _ccs_deserialize_bin_string(&data->name, buffer_size, buffer)); + CCS_VALIDATE(_ccs_peek_bin_ccs_object_internal( + &obj, buffer_size, buffer, &data->search_space_handle)); + CCS_VALIDATE(_ccs_object_deserialize_with_opts( + (ccs_object_t *)&data->search_space, + CCS_SERIALIZE_FORMAT_BINARY, version, buffer_size, buffer, + opts)); CCS_VALIDATE(_ccs_deserialize_bin_size( &data->num_parameters, buffer_size, buffer)); CCS_VALIDATE(_ccs_deserialize_bin_size( @@ -82,7 +91,8 @@ _ccs_deserialize_bin_objective_space( new_opts.map_values = CCS_TRUE; CCS_VALIDATE(ccs_create_map(&new_opts.handle_map)); - _ccs_objective_space_data_mock_t data = {NULL, 0, 0, NULL, NULL, NULL}; + _ccs_objective_space_data_mock_t data = {NULL, NULL, NULL, 0, + 0, NULL, NULL, NULL}; CCS_VALIDATE_ERR_GOTO( res, _ccs_deserialize_bin_ccs_objective_space_data( @@ -91,23 +101,32 @@ _ccs_deserialize_bin_objective_space( CCS_VALIDATE_ERR_GOTO( res, ccs_create_objective_space( - data.name, data.num_parameters, data.parameters, - data.num_objectives, data.objectives, + data.name, data.search_space, data.num_parameters, + data.parameters, data.num_objectives, data.objectives, data.objective_types, objective_space_ret), end); - if (opts && opts->map_values && opts->handle_map) + if (opts && opts->map_values && opts->handle_map) { + CCS_VALIDATE_ERR_GOTO( + res, + _ccs_object_handle_check_add( + opts->handle_map, data.search_space_handle, + (ccs_object_t)data.search_space), + err_objective_space); CCS_VALIDATE_ERR_GOTO( res, _ccs_object_handle_check_add( opts->handle_map, handle, (ccs_object_t)*objective_space_ret), err_objective_space); + } goto end; err_objective_space: ccs_release_object(*objective_space_ret); *objective_space_ret = NULL; end: + if (data.search_space) + ccs_release_object(data.search_space); if (data.parameters) for (size_t i = 0; i < data.num_parameters; i++) if (data.parameters[i]) diff --git a/src/objective_space_internal.h b/src/objective_space_internal.h index a7b0f7a0..ed7c44d3 100644 --- a/src/objective_space_internal.h +++ b/src/objective_space_internal.h @@ -31,6 +31,7 @@ struct _ccs_objective_space_data_s { _ccs_parameter_index_hash_t *handle_hash; size_t num_objectives; _ccs_objective_t *objectives; + ccs_search_space_t search_space; }; #endif //_OBJECTIVE_SPACE_INTERNAL_H diff --git a/src/search_space_internal.h b/src/search_space_internal.h new file mode 100644 index 00000000..9aba11fe --- /dev/null +++ b/src/search_space_internal.h @@ -0,0 +1,12 @@ +#ifndef _SEARCH_SPACE_INTERNAL_H +#define _SEARCH_SPACE_INTERNAL_H + +struct _ccs_search_space_data_s; +typedef struct _ccs_search_space_data_s _ccs_search_space_data_t; + +struct _ccs_search_space_s { + _ccs_object_internal_t obj; + _ccs_search_space_data_t *data; +}; + +#endif //_SEARCH_SPACE_INTERNAL_H diff --git a/src/tree_tuner_deserialize.h b/src/tree_tuner_deserialize.h index 1a747021..9b1f6f2b 100644 --- a/src/tree_tuner_deserialize.h +++ b/src/tree_tuner_deserialize.h @@ -24,10 +24,6 @@ _ccs_deserialize_bin_size_ccs_tree_tuner_common_data( &data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_deserialize_bin_string(&data->name, buffer_size, buffer)); - CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( - (ccs_object_t *)&data->tree_space, CCS_OBJECT_TYPE_TREE_SPACE, - CCS_SERIALIZE_FORMAT_BINARY, version, buffer_size, buffer, - opts)); CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( (ccs_object_t *)&data->objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE, CCS_SERIALIZE_FORMAT_BINARY, @@ -122,8 +118,8 @@ _ccs_deserialize_bin_random_tree_tuner( CCS_VALIDATE_ERR_GOTO( res, ccs_create_random_tree_tuner( - data.common_data.name, data.common_data.tree_space, - data.common_data.objective_space, tuner_ret), + data.common_data.name, data.common_data.objective_space, + tuner_ret), evaluations); odata = (_ccs_random_tree_tuner_data_clone_t *)((*tuner_ret)->data); for (size_t i = 0; i < data.size_history; i++) @@ -140,8 +136,6 @@ _ccs_deserialize_bin_random_tree_tuner( if (data.history[i]) ccs_release_object(data.history[i]); end: - if (data.common_data.tree_space) - ccs_release_object(data.common_data.tree_space); if (data.common_data.objective_space) ccs_release_object(data.common_data.objective_space); if (data.history) @@ -196,7 +190,6 @@ _ccs_deserialize_bin_user_defined_tree_tuner( res, ccs_create_user_defined_tree_tuner( data.base_data.common_data.name, - data.base_data.common_data.tree_space, data.base_data.common_data.objective_space, vector, opts->data, tuner_ret), end); @@ -222,8 +215,6 @@ _ccs_deserialize_bin_user_defined_tree_tuner( ccs_release_object(*tuner_ret); *tuner_ret = NULL; end: - if (data.base_data.common_data.tree_space) - ccs_release_object(data.base_data.common_data.tree_space); if (data.base_data.common_data.objective_space) ccs_release_object(data.base_data.common_data.objective_space); if (data.base_data.history) { diff --git a/src/tree_tuner_internal.h b/src/tree_tuner_internal.h index d8eef86a..5672c635 100644 --- a/src/tree_tuner_internal.h +++ b/src/tree_tuner_internal.h @@ -60,8 +60,6 @@ _ccs_serialize_bin_size_ccs_tree_tuner_common_data( { *cum_size += _ccs_serialize_bin_size_ccs_tree_tuner_type(data->type); *cum_size += _ccs_serialize_bin_size_string(data->name); - CCS_VALIDATE(data->tree_space->obj.ops->serialize_size( - data->tree_space, CCS_SERIALIZE_FORMAT_BINARY, cum_size, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize_size( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, cum_size, opts)); @@ -79,9 +77,6 @@ _ccs_serialize_bin_ccs_tree_tuner_common_data( data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_serialize_bin_string(data->name, buffer_size, buffer)); - CCS_VALIDATE(data->tree_space->obj.ops->serialize( - data->tree_space, CCS_SERIALIZE_FORMAT_BINARY, buffer_size, - buffer, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, buffer_size, buffer, opts)); diff --git a/src/tree_tuner_random.c b/src/tree_tuner_random.c index 4286ba69..ffcbe062 100644 --- a/src/tree_tuner_random.c +++ b/src/tree_tuner_random.c @@ -355,13 +355,15 @@ static const UT_icd _evaluation_icd = { ccs_result_t ccs_create_random_tree_tuner( const char *name, - ccs_tree_space_t tree_space, ccs_objective_space_t objective_space, ccs_tree_tuner_t *tuner_ret) { + ccs_tree_space_t tree_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(tree_space, CCS_OBJECT_TYPE_TREE_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&tree_space)); + CCS_CHECK_OBJ(tree_space, CCS_OBJECT_TYPE_TREE_SPACE); CCS_CHECK_PTR(tuner_ret); uintptr_t mem = (uintptr_t)calloc( diff --git a/src/tree_tuner_user_defined.c b/src/tree_tuner_user_defined.c index 903511a5..c160290a 100644 --- a/src/tree_tuner_user_defined.c +++ b/src/tree_tuner_user_defined.c @@ -304,15 +304,17 @@ static _ccs_tree_tuner_ops_t _ccs_tree_tuner_user_defined_ops = { ccs_result_t ccs_create_user_defined_tree_tuner( const char *name, - ccs_tree_space_t tree_space, ccs_objective_space_t objective_space, ccs_user_defined_tree_tuner_vector_t *vector, void *tuner_data, ccs_tree_tuner_t *tuner_ret) { + ccs_tree_space_t tree_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(tree_space, CCS_OBJECT_TYPE_TREE_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&tree_space)); + CCS_CHECK_OBJ(tree_space, CCS_OBJECT_TYPE_TREE_SPACE); CCS_CHECK_PTR(tuner_ret); CCS_CHECK_PTR(vector); CCS_CHECK_PTR(vector->del); diff --git a/src/tuner_deserialize.h b/src/tuner_deserialize.h index 456074a2..deac4b35 100644 --- a/src/tuner_deserialize.h +++ b/src/tuner_deserialize.h @@ -23,11 +23,6 @@ _ccs_deserialize_bin_size_ccs_tuner_common_data( &data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_deserialize_bin_string(&data->name, buffer_size, buffer)); - CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( - (ccs_object_t *)&data->configuration_space, - CCS_OBJECT_TYPE_CONFIGURATION_SPACE, - CCS_SERIALIZE_FORMAT_BINARY, version, buffer_size, buffer, - opts)); CCS_VALIDATE(_ccs_object_deserialize_with_opts_check( (ccs_object_t *)&data->objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE, CCS_SERIALIZE_FORMAT_BINARY, @@ -120,9 +115,8 @@ _ccs_deserialize_bin_random_tuner( CCS_VALIDATE_ERR_GOTO( res, ccs_create_random_tuner( - data.common_data.name, - data.common_data.configuration_space, - data.common_data.objective_space, tuner_ret), + data.common_data.name, data.common_data.objective_space, + tuner_ret), evaluations); odata = (_ccs_random_tuner_data_clone_t *)((*tuner_ret)->data); for (size_t i = 0; i < data.size_history; i++) @@ -195,7 +189,6 @@ _ccs_deserialize_bin_user_defined_tuner( res, ccs_create_user_defined_tuner( data.base_data.common_data.name, - data.base_data.common_data.configuration_space, data.base_data.common_data.objective_space, vector, opts->data, tuner_ret), end); @@ -221,9 +214,6 @@ _ccs_deserialize_bin_user_defined_tuner( ccs_release_object(*tuner_ret); *tuner_ret = NULL; end: - if (data.base_data.common_data.configuration_space) - ccs_release_object( - data.base_data.common_data.configuration_space); if (data.base_data.common_data.objective_space) ccs_release_object(data.base_data.common_data.objective_space); if (data.base_data.history) { diff --git a/src/tuner_internal.h b/src/tuner_internal.h index 86311cd4..83edc1d2 100644 --- a/src/tuner_internal.h +++ b/src/tuner_internal.h @@ -60,9 +60,6 @@ _ccs_serialize_bin_size_ccs_tuner_common_data( { *cum_size += _ccs_serialize_bin_size_ccs_tuner_type(data->type); *cum_size += _ccs_serialize_bin_size_string(data->name); - CCS_VALIDATE(data->configuration_space->obj.ops->serialize_size( - data->configuration_space, CCS_SERIALIZE_FORMAT_BINARY, - cum_size, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize_size( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, cum_size, opts)); @@ -80,9 +77,6 @@ _ccs_serialize_bin_ccs_tuner_common_data( data->type, buffer_size, buffer)); CCS_VALIDATE( _ccs_serialize_bin_string(data->name, buffer_size, buffer)); - CCS_VALIDATE(data->configuration_space->obj.ops->serialize( - data->configuration_space, CCS_SERIALIZE_FORMAT_BINARY, - buffer_size, buffer, opts)); CCS_VALIDATE(data->objective_space->obj.ops->serialize( data->objective_space, CCS_SERIALIZE_FORMAT_BINARY, buffer_size, buffer, opts)); diff --git a/src/tuner_random.c b/src/tuner_random.c index 2dbd0067..05c1217c 100644 --- a/src/tuner_random.c +++ b/src/tuner_random.c @@ -347,14 +347,16 @@ static const UT_icd _evaluation_icd = { } ccs_result_t ccs_create_random_tuner( - const char *name, - ccs_configuration_space_t configuration_space, - ccs_objective_space_t objective_space, - ccs_tuner_t *tuner_ret) + const char *name, + ccs_objective_space_t objective_space, + ccs_tuner_t *tuner_ret) { + ccs_configuration_space_t configuration_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&configuration_space)); + CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_PTR(tuner_ret); uintptr_t mem = (uintptr_t)calloc( diff --git a/src/tuner_user_defined.c b/src/tuner_user_defined.c index 8de44e67..b5bc346b 100644 --- a/src/tuner_user_defined.c +++ b/src/tuner_user_defined.c @@ -298,15 +298,17 @@ static _ccs_tuner_ops_t _ccs_tuner_user_defined_ops = { ccs_result_t ccs_create_user_defined_tuner( const char *name, - ccs_configuration_space_t configuration_space, ccs_objective_space_t objective_space, ccs_user_defined_tuner_vector_t *vector, void *tuner_data, ccs_tuner_t *tuner_ret) { + ccs_configuration_space_t configuration_space; CCS_CHECK_PTR(name); - CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_OBJ(objective_space, CCS_OBJECT_TYPE_OBJECTIVE_SPACE); + CCS_VALIDATE(ccs_objective_space_get_search_space( + objective_space, (ccs_search_space_t *)&configuration_space)); + CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE); CCS_CHECK_PTR(tuner_ret); CCS_CHECK_PTR(vector); CCS_CHECK_PTR(vector->del); diff --git a/tests/test_random_features_tuner.c b/tests/test_random_features_tuner.c index 97205279..bc2f691c 100644 --- a/tests/test_random_features_tuner.c +++ b/tests/test_random_features_tuner.c @@ -19,11 +19,11 @@ test(void) ccs_map_t map; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); fspace = create_knobs(&features_on, &features_off); err = ccs_create_random_features_tuner( - "problem", cspace, fspace, ospace, &tuner); + "problem", fspace, ospace, &tuner); assert(err == CCS_RESULT_SUCCESS); for (size_t i = 0; i < 50; i++) { @@ -205,7 +205,7 @@ test_evaluation_deserialize(void) int cmp; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); fspace = create_knobs(&features_on, NULL); err = ccs_configuration_space_sample( diff --git a/tests/test_random_tree_tuner.c b/tests/test_random_tree_tuner.c index 9b9ef9a9..b8b18e5f 100644 --- a/tests/test_random_tree_tuner.c +++ b/tests/test_random_tree_tuner.c @@ -44,7 +44,9 @@ create_tree_tuning_problem( otype = CCS_OBJECTIVE_TYPE_MAXIMIZE; err = ccs_create_objective_space( - "ospace", 1, ¶meter, 1, &expression, &otype, ospace); + "ospace", (ccs_search_space_t)*tree_space, 1, ¶meter, 1, + &expression, &otype, ospace); + print_ccs_error_stack(); assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(root); @@ -69,8 +71,7 @@ test(void) create_tree_tuning_problem(&tree_space, &ospace); - err = ccs_create_random_tree_tuner( - "problem", tree_space, ospace, &tuner); + err = ccs_create_random_tree_tuner("problem", ospace, &tuner); assert(err == CCS_RESULT_SUCCESS); for (size_t i = 0; i < 100; i++) { diff --git a/tests/test_random_tuner.c b/tests/test_random_tuner.c index 5863e870..f0bc20b3 100644 --- a/tests/test_random_tuner.c +++ b/tests/test_random_tuner.c @@ -17,9 +17,9 @@ test(void) ccs_map_t map; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); - err = ccs_create_random_tuner("problem", cspace, ospace, &tuner); + err = ccs_create_random_tuner("problem", ospace, &tuner); assert(err == CCS_RESULT_SUCCESS); for (size_t i = 0; i < 100; i++) { @@ -135,7 +135,7 @@ test_evaluation_deserialize(void) int cmp; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); err = ccs_configuration_space_sample( cspace, NULL, NULL, &configuration); diff --git a/tests/test_user_defined_features_tuner.c b/tests/test_user_defined_features_tuner.c index bcfaf24e..1f0cca12 100644 --- a/tests/test_user_defined_features_tuner.c +++ b/tests/test_user_defined_features_tuner.c @@ -163,15 +163,15 @@ test(void) ccs_map_t map; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); fspace = create_knobs(&features_on, &features_off); tuner_data = (tuner_last_t *)calloc(1, sizeof(tuner_last_t)); assert(tuner_data); err = ccs_create_user_defined_features_tuner( - "problem", cspace, fspace, ospace, &tuner_last_vector, - tuner_data, &tuner); + "problem", fspace, ospace, &tuner_last_vector, tuner_data, + &tuner); assert(err == CCS_RESULT_SUCCESS); ccs_features_evaluation_t last_evaluation; diff --git a/tests/test_user_defined_tree_tuner.c b/tests/test_user_defined_tree_tuner.c index 30b94d6b..e2912dc1 100644 --- a/tests/test_user_defined_tree_tuner.c +++ b/tests/test_user_defined_tree_tuner.c @@ -44,7 +44,8 @@ create_tree_tuning_problem( otype = CCS_OBJECTIVE_TYPE_MAXIMIZE; err = ccs_create_objective_space( - "ospace", 1, ¶meter, 1, &expression, &otype, ospace); + "ospace", (ccs_search_space_t)*tree_space, 1, ¶meter, 1, + &expression, &otype, ospace); assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(root); @@ -202,8 +203,7 @@ test(void) assert(tuner_data); err = ccs_create_user_defined_tree_tuner( - "problem", tree_space, ospace, &tuner_last_vector, tuner_data, - &tuner); + "problem", ospace, &tuner_last_vector, tuner_data, &tuner); assert(err == CCS_RESULT_SUCCESS); ccs_tree_evaluation_t last_evaluation; diff --git a/tests/test_user_defined_tuner.c b/tests/test_user_defined_tuner.c index b95652f5..1615ba47 100644 --- a/tests/test_user_defined_tuner.c +++ b/tests/test_user_defined_tuner.c @@ -147,14 +147,13 @@ test(void) ccs_map_t map; cspace = create_2d_plane(); - ospace = create_height_objective(); + ospace = create_height_objective(cspace); tuner_data = (tuner_last_t *)calloc(1, sizeof(tuner_last_t)); assert(tuner_data); err = ccs_create_user_defined_tuner( - "problem", cspace, ospace, &tuner_last_vector, tuner_data, - &tuner); + "problem", ospace, &tuner_last_vector, tuner_data, &tuner); assert(err == CCS_RESULT_SUCCESS); ccs_evaluation_t last_evaluation; diff --git a/tests/test_utils.c b/tests/test_utils.c index 40ec591e..64c300f8 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -65,7 +65,7 @@ create_2d_plane(void) } ccs_objective_space_t -create_height_objective(void) +create_height_objective(ccs_configuration_space_t cspace) { ccs_parameter_t parameter; ccs_objective_space_t ospace; @@ -78,7 +78,8 @@ create_height_objective(void) assert(err == CCS_RESULT_SUCCESS); otype = CCS_OBJECTIVE_TYPE_MINIMIZE; err = ccs_create_objective_space( - "height", 1, ¶meter, 1, &expression, &otype, &ospace); + "height", (ccs_search_space_t)cspace, 1, ¶meter, 1, + &expression, &otype, &ospace); assert(err == CCS_RESULT_SUCCESS); err = ccs_release_object(expression); diff --git a/tests/test_utils.h b/tests/test_utils.h index 704792a3..72516fc5 100644 --- a/tests/test_utils.h +++ b/tests/test_utils.h @@ -10,7 +10,7 @@ extern ccs_configuration_space_t create_2d_plane(void); extern ccs_objective_space_t -create_height_objective(void); +create_height_objective(ccs_configuration_space_t cspace); extern ccs_feature_space_t create_knobs(ccs_features_t *features_on, ccs_features_t *features_off);