Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow multiple conditions on the same parameter #380

Open
mdorier opened this issue Jul 19, 2024 · 2 comments
Open

Feature request: allow multiple conditions on the same parameter #380

mdorier opened this issue Jul 19, 2024 · 2 comments
Labels

Comments

@mdorier
Copy link
Contributor

mdorier commented Jul 19, 2024

When I try to add multiple conditions that affect the same hyperparameter, ConfigSpace complains that adding multiple conditions is ambiguous. For example:

from ConfigSpace import ConfigurationSpace, EqualsCondition, Integer
cs = ConfigurationSpace()
cs.add(Integer("x", (1, 10), default=1))
cs.add(Integer("y", (1, 10), default=1))
cs.add(Integer("z", (1, 10), default=1))
cs.add(EqualsCondition(cs["z"], cs["x"], 3))
cs.add(EqualsCondition(cs["z"], cs["y"], 3))

Gives me:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/Code/spack/var/spack/environments/bedrock-env/.spack-env/view/lib/python3.11/site-packages/ConfigSpace/configuration_space.py", line 330, in add
    self._dag.add_condition(condition)
  File "/home/ubuntu/Code/spack/var/spack/environments/bedrock-env/.spack-env/view/lib/python3.11/site-packages/ConfigSpace/_condition_tree.py", line 681, in add_condition
    raise AmbiguousConditionError(
ConfigSpace.exceptions.AmbiguousConditionError: Adding a second parent condition for a for a hyperparameter is ambiguous and therefore forbidden. Use an `OrConjunction` or `AndConjunction` to combine conditions instead.
Already inserted: z | x == 3
New one: z | y == 3

I don't understand why it would be ambiguous. When adding multiple conditions, it would seem obvious to me that they are in conjunction with each other, not in disjonction (if anything, simply because conditions that affect distinct parameters are already in conjunction with each other).

More practically speaking, the reason this is annoying in my use-case is that, as part of a library, I have a function that returns a ConfigurationSpace for the parameters available to the library. The user then has a chance to further constrain this space by adding conditions on parameters. But some of the parameters already have some conditions attached to them by the library itself.

As is, ConfigSpace doesn't seem to provide an API to remove a previously defined condition and add it back as part of an AndConjunction, or add more conditions to an existing AndConjunction.

@mdorier mdorier changed the title [question] Why not allow multiple conditions on the same parameter? Feature request: allow multiple conditions on the same parameter Jul 26, 2024
@mdorier
Copy link
Contributor Author

mdorier commented Jul 26, 2024

I edited the title to make it a feature request. I've been trying to get around this constraint in my code but it's becoming unmanageable. I really need a way to add multiple conditions to the same hyperparameter from different points in the code (which may be from potentially different libraries).

I looked at the code to see how this could be done. One way I would see this doable would be to allow adding conditions to an existing AndConjunction (and allow an AndConjunction to initially have a single subcondition -- checking that there are at least 2 conditions in the constructor right now isn't necessary, the code would work just the same with a single subcondition). But I'm not sure it's as simple as this... does the DAG need to be updated as well?

@eddiebergman
Copy link
Contributor

As long as everything is put into one AndConjunction, the DAG part of the code shouldn't need to be touched too much. There is an outstanding issue where diamond OR conditions don't work though which is something to be aware of.

I'm not sure when I'll have time to implement, test and document this but I'm happy to review a PR if you're up for it.

The primary alternative from your code to build around this fact would be for you to collect conditions from a user of your API and when it comes time to using the ConfigurationSpace, you would add all of the conditions into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants