Skip to content

Commit 3036164

Browse files
authored
Diamond cond with OrConjunction (#219)
* add check for pcs_new read * flake8 * simplify code and flake8 * hierarchical diamond condition * doc update
1 parent bbdb2aa commit 3036164

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

ConfigSpace/c_util.pyx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ cpdef np.ndarray change_hp_value(
267267
cdef Hyperparameter current
268268
cdef str current_name
269269
cdef list disabled
270+
cdef set hps_to_be_activate
270271
cdef set visited
271272
cdef dict activated_values
272273
cdef int active
@@ -287,6 +288,12 @@ cpdef np.ndarray change_hp_value(
287288
# Hyperparameters which are going to be set to inactive
288289
disabled = []
289290

291+
# Hyperparameters which are going to be set activate, we introduce this to resolve the conflict that might be raised
292+
# by OrConjunction:
293+
# Suppose that we have a parent HP_p whose possible values are A, B, C; a child HP_d is activate if
294+
# HP_p is A or B. Then when HP_p switches from A to B, HP_d needs to remain activate.
295+
hps_to_be_activate = set()
296+
290297
# Activate hyperparameters if their parent node got activated
291298
children = children_of[hp_name]
292299
if len(children) > 0:
@@ -301,7 +308,7 @@ cpdef np.ndarray change_hp_value(
301308
if current_name in visited:
302309
continue
303310
visited.add(current_name)
304-
if current_name in disabled:
311+
if current_name in hps_to_be_activate:
305312
continue
306313

307314
current_idx = configuration_space._hyperparameter_idx[current_name]
@@ -315,6 +322,16 @@ cpdef np.ndarray change_hp_value(
315322
active = False
316323
break
317324

325+
if active:
326+
hps_to_be_activate.add(current_idx)
327+
if current_value == current_value:
328+
children_ = children_of[current_name]
329+
if len(children_) > 0:
330+
to_visit.extendleft(children_)
331+
332+
if current_name in disabled:
333+
continue
334+
318335
if active and not current_value == current_value:
319336
default_value = current.normalized_default_value
320337
configuration_array[current_idx] = default_value
@@ -343,6 +360,7 @@ cpdef np.ndarray change_hp_value(
343360
to_disable.add(ch.name)
344361

345362
for idx in disabled:
346-
configuration_array[idx] = NaN
363+
if idx not in hps_to_be_activate:
364+
configuration_array[idx] = NaN
347365

348366
return configuration_array

test/test_util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,35 @@ def test_check_neighbouring_config_diamond(self):
277277

278278
np.testing.assert_almost_equal(new_array, expected_array)
279279

280+
def test_check_neighbouring_config_diamond_or_conjunction(self):
281+
diamond = ConfigurationSpace()
282+
top = CategoricalHyperparameter('top', [0, 1], 0)
283+
middle = CategoricalHyperparameter('middle', [0, 1], 1)
284+
bottom_left = CategoricalHyperparameter('bottom_left', [0, 1], 1)
285+
bottom_right = CategoricalHyperparameter('bottom_right', [0, 1, 2, 3], 1)
286+
287+
diamond.add_hyperparameters([top, bottom_left, bottom_right, middle])
288+
diamond.add_condition(EqualsCondition(middle, top, 0))
289+
diamond.add_condition(EqualsCondition(bottom_left, middle, 0))
290+
diamond.add_condition(OrConjunction(EqualsCondition(bottom_right, middle, 1),
291+
EqualsCondition(bottom_right, top, 1)))
292+
293+
config = Configuration(diamond, {'top': 0, 'middle': 1, 'bottom_right': 1})
294+
hp_name = "top"
295+
index = diamond.get_idx_by_hyperparameter_name(hp_name)
296+
neighbor_value = 1
297+
298+
new_array = ConfigSpace.c_util.change_hp_value(
299+
diamond,
300+
config.get_array(),
301+
hp_name,
302+
neighbor_value,
303+
index
304+
)
305+
expected_array = np.array([1, np.nan, np.nan, 1])
306+
307+
np.testing.assert_almost_equal(new_array, expected_array)
308+
280309
def test_check_neighbouring_config_diamond_str(self):
281310
diamond = ConfigurationSpace()
282311
head = CategoricalHyperparameter('head', ['red', 'green'])

0 commit comments

Comments
 (0)