def test_encode(self): before = {"name": ["tqc", "dsy"], "type": {(1, 2): {1, 2, 3}}} encoded = smac_hdl._encode(before) print(encoded) after = smac_hdl._decode(encoded) print(after) self.assertEqual(before, after)
def __parse_dict_to_config(self, key, value): if isinstance(value, dict): _type = value.get("_type") _value = value.get("_value") _default = value.get("_default") assert _value is not None if _type == "choice": return smac_hdl.choice(key, _value, _default) else: return eval( f'''smac_hdl.{_type}("{key}",*_value,default=_default)''') else: return Constant(key, smac_hdl._encode(value))
def __condition(self, item: Dict, store: Dict): child = item["_child"] child = store[child] parent = item["_parent"] parent = store[parent] value = (item["_values"]) if (isinstance(value, list) and len(value) == 1): value = value[0] if isinstance(value, list): cond = InCondition(child, parent, list(map(smac_hdl._encode, value))) else: cond = EqualsCondition(child, parent, smac_hdl._encode(value)) return cond
def __forbidden(self, value: List, store: Dict, cs: ConfigurationSpace): assert isinstance(value, list) for item in value: assert isinstance(item, dict) clauses = [] for k, v in item.items(): if isinstance(v, list) and len(v) == 1: v = v[0] if isinstance(v, list): clauses.append( ForbiddenInClause(store[k], list(map(smac_hdl._encode, v)))) else: clauses.append( ForbiddenEqualsClause(store[k], smac_hdl._encode(v))) cs.add_forbidden_clause(ForbiddenAndConjunction(*clauses))
import unittest from autopipeline.hdl import smac as smac_hdl before = {"name": ["tqc", "dsy"], "type": {(1, 2): {1, 2, 3}}} encoded = smac_hdl._encode(before) print(encoded) after = smac_hdl._decode(encoded) print(after)
def replace_phps(phps: ConfigurationSpace, key, value): for hp in phps.get_hyperparameters(): if hp.__class__.__name__ == "Constant" and hp.name.endswith(key): hp.value = _encode(value)