Skip to content

Commit

Permalink
Objective space now contains the search space.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed May 8, 2024
1 parent 32ab6b1 commit 7fb7597
Show file tree
Hide file tree
Showing 60 changed files with 415 additions and 326 deletions.
1 change: 1 addition & 0 deletions bindings/python/cconfigspace/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions bindings/python/cconfigspace/features_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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])
Expand Down
19 changes: 15 additions & 4 deletions bindings/python/cconfigspace/objective_space.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand All @@ -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:
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions bindings/python/cconfigspace/tree_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions bindings/python/cconfigspace/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/test/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions bindings/python/test/test_features_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
5 changes: 4 additions & 1 deletion bindings/python/test/test_objective_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down
Loading

0 comments on commit 7fb7597

Please sign in to comment.