Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ConfigSpace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,19 @@ def substitute_hyperparameters_in_forbiddens(

return new_forbiddens

def __copy__(self) -> ConfigurationSpace:
"""Creates a copy of the ConfigurationSpace."""
cs_copy = ConfigurationSpace(
name=self.name,
meta=self.meta.copy(),
space={hp.name: copy.copy(hp) for hp in self.values()},
)
for condition in self.conditions:
cs_copy.add(copy.copy(condition))
for forbidden in self.forbidden_clauses:
cs_copy.add(copy.copy(forbidden))
return cs_copy

def __eq__(self, other: Any) -> bool:
"""Override the default Equals behavior."""
if isinstance(other, self.__class__):
Expand Down
27 changes: 27 additions & 0 deletions test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,3 +1482,30 @@ def test_configuration_space_can_be_made_with_sequence_of_hyperparameters() -> N
assert len(cs) == 2
assert "a" in cs
assert "b" in cs


def test_copy() -> None:
import copy

cs = ConfigurationSpace(
name="myspace",
meta={"hello": "world"},
space=[
Float("a", (1.0, 10.0)),
Integer("b", (1, 10)),
CategoricalHyperparameter("c", ["a", "b", "c"]),
],
)
cs.add(EqualsCondition(cs["b"], cs["c"], "a"))
cs.add(ForbiddenEqualsClause(cs["a"], 4.2))

cs_copy = copy.copy(cs)
assert cs == cs_copy

# Verify that removing components from the copy
cs_copy.remove(cs["a"])
assert cs != cs_copy

# Re adding the hyperparameter will not bring forbidden back
cs_copy.add(cs["a"])
assert cs != cs_copy