Skip to content

Commit 5f4403b

Browse files
committed
feat: Rework variable and constraint format
Add ...
1 parent 5f3c385 commit 5f4403b

12 files changed

Lines changed: 457 additions & 92 deletions

File tree

‎examples/cobi.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from opltools import Implementation, Generator, Reference, Library, Variable, Constraint
2+
from pydantic_yaml import to_yaml_str
3+
4+
things = {}
5+
6+
things["cobi_impl"] = Implementation(
7+
name="COBI Implementation",
8+
description="Python library for COBI (COnstrained BI-objective optimization) problem generator",
9+
language="python",
10+
links=[
11+
{"type": "repository", "url": "https://github.com/numbbo/cobi-problem-generator/"},
12+
{"type": "v0.5.0", "url": "https://github.com/numbbo/cobi-problem-generator/releases/tag/v0.5.0"},
13+
],
14+
requirements="https://github.com/numbbo/cobi-problem-generator/blob/main/requirements.txt",
15+
)
16+
17+
things["cobi_problem"] = Generator(
18+
name="COBI Problem",
19+
description="Generator of COnstrained BI-objective optimization problems",
20+
tags={
21+
"constrained",
22+
"bi-objective",
23+
"continuous",
24+
"black-box",
25+
"location",
26+
"multi-peak",
27+
"convex-quadratic",
28+
},
29+
references=[
30+
Reference(
31+
title="Pareto Set Characterization in Constrained Multiobjective Optimization and the COBI Problem Generator",
32+
authors=["Anne Auger", "Dimo Brockhoff", "Luka Opravš", "Tea Tušar"],
33+
link={"type": "arxiv", "url": "https://arxiv.org/abs/2604.09131"},
34+
)
35+
],
36+
objectives={2},
37+
variables=[
38+
Variable(type="continuous", dim={"min": 1})
39+
],
40+
implementations={"cobi_impl"},
41+
can_evaluate_objectives_independently="no",
42+
constraints=[
43+
# FIXME: Chekc
44+
Constraint(type="box", number={"min": 1}),
45+
Constraint(type="linear", number={"min": 1}),
46+
Constraint(type="function", number={"min": 1}),
47+
],
48+
noise_type={"none"},
49+
dynamic_type=None,
50+
allows_partial_evaluation="no",
51+
modality={"multi-modal per objective"},
52+
fidelity_levels={1},
53+
source={"artificial"},
54+
code_examples={"./code_153.py"},
55+
)
56+
library = Library(things)
57+
58+
print(to_yaml_str(library))

‎examples/demo.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from opltools import Suite, Problem, Library, Link, Variables, ValueRange
1+
from opltools import Suite, Problem, Library, Link, Variable, ValueRange
22
from pydantic_yaml import to_yaml_str
33
from opltools.schema import Implementation
44

@@ -20,7 +20,9 @@
2020
things[f"fn_bbob_f{fnr}"] = Problem(
2121
name=f"BBOB F_{fnr}",
2222
objectives={1},
23-
variables=Variables(continuous=ValueRange(min=1, max=80)),
23+
variables={
24+
Variable(type="continuous", dim=ValueRange(min=1, max=80))
25+
},
2426
implementations=["impl_py_cocoex"]
2527
)
2628

‎pyproject.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ dependencies = [
1616
[project.scripts]
1717
opl = "opltools.cli:main"
1818

19+
[project.optional-dependencies]
20+
cli = [
21+
"rich>=15.0.0",
22+
]
23+
1924
[build-system]
2025
requires = ["uv_build>=0.11.7,<0.12.0"]
2126
build-backend = "uv_build"

‎src/opltools/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .schema import Problem, Suite, Generator, Implementation, Library, YesNoSome, Link, Reference, Variables, ValueRange, Constraints
1+
from .schema import Problem, Suite, Generator, Implementation, Library, YesNoSome, Link, Reference, Variable, ValueRange, Constraint
22

33
__all__ = [
44
"Problem",
@@ -9,7 +9,7 @@
99
"YesNoSome",
1010
"Link",
1111
"Reference",
12-
"Constraints",
13-
"Variables",
12+
"Constraint",
13+
"Variable",
1414
"ValueRange"
1515
]

‎src/opltools/schema.py‎

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing_extensions import Self
33
from pydantic import BaseModel, RootModel, ConfigDict, model_validator
44

5+
from .yesnosome import YesNoSome
56
from .utils import ValueRange, union_range
67

78

@@ -12,17 +13,13 @@ class OPLType(Enum):
1213
implementation = 'implementation'
1314

1415

15-
class YesNoSome(Enum):
16-
yes = "yes"
17-
no = "no"
18-
some = "some"
19-
unknown = "?"
20-
21-
2216
class Link(BaseModel):
2317
type: str | None = None
2418
url: str
2519

20+
def __hash__(self):
21+
return hash(self.type) + hash(self.url)
22+
2623

2724
class Thing(BaseModel):
2825
type: OPLType
@@ -37,37 +34,51 @@ def union(self, other: Self) -> Self:
3734
return self
3835

3936

40-
class Variables(BaseModel):
41-
continuous: int | set[int] | ValueRange = 0
42-
integer: int | set[int] | ValueRange = 0
43-
binary: int | set[int] | ValueRange = 0
44-
categorical: int | set[int] | ValueRange = 0
37+
class VariableType(Enum):
38+
continuous = "continuous"
39+
integer = "integer"
40+
binary = "binary"
41+
categorical = "categorical"
42+
unknown = "unknown"
4543

46-
def union(self, other: Self) -> Self:
47-
self.continuous = union_range(self.continuous, other.continuous)
48-
self.integer = union_range(self.integer, other.integer)
49-
self.binary = union_range(self.integer, other.binary)
50-
self.categorical = union_range(self.integer, other.categorical)
51-
return self
5244

45+
class Variable(BaseModel):
46+
type: VariableType = VariableType.unknown
47+
dim: int | set[int] | ValueRange | None = 0
5348

54-
class Constraints(BaseModel):
55-
box: int | set[int] | ValueRange = 0
56-
linear: int | set[int] | ValueRange = 0
57-
function: int | set[int] | ValueRange = 0
49+
def __hash__(self):
50+
if isinstance(self.dim, set):
51+
dim_hash = hash(frozenset(self.dim))
52+
else:
53+
dim_hash = hash(self.dim)
54+
return hash(self.type) + dim_hash
5855

59-
def union(self, other: Variables) -> Self:
60-
self.box = union_range(self.box, other.box)
61-
self.linear = union_range(self.linear, other.linear)
62-
self.function = union_range(self.function, other.function)
63-
return self
56+
57+
class ConstraintType(Enum):
58+
box = "box"
59+
linear = "linear"
60+
function = "function"
61+
unknown = "unknown"
62+
63+
class Constraint(BaseModel):
64+
type: ConstraintType = ConstraintType.unknown
65+
soft: YesNoSome | None = None
66+
equality: YesNoSome | None = None
67+
number: int | set[int] | ValueRange | None = None
68+
69+
def __hash__(self):
70+
number = frozenset(self.number) if isinstance(self.number, set) else self.number
71+
return hash((self.type, self.soft, self.equality, number))
6472

6573

6674
class Reference(BaseModel):
6775
title: str
6876
authors: list[str]
6977
link: Link | None = None
7078

79+
def __hash__(self):
80+
return hash(self.title) + sum([hash(author) for author in self.authors]) + hash(self.link)
81+
7182

7283
class Usage(BaseModel):
7384
language: str
@@ -92,9 +103,8 @@ class ProblemLike(Thing):
92103
references: set[Reference] | None = None
93104
implementations: set[str] | None = None
94105
objectives: set[int] | None = None
95-
variables: Variables | None = None
96-
constraints: Constraints | None = None
97-
soft_constraints: Constraints | None = None
106+
variables: set[Variable] | None = None
107+
constraints: set[Constraint] | None = None
98108
dynamic_type: set[str] | None = None
99109
noise_type: set[str] | None = None
100110
allows_partial_evaluation: YesNoSome | None = None
@@ -109,6 +119,9 @@ class Problem(ProblemLike):
109119
type:OPLType = OPLType.problem
110120
instances: ValueRange | list[str] | None = None
111121

122+
def __hash__(self):
123+
return hash((self.type, self.name))
124+
112125

113126
class Suite(ProblemLike):
114127
type:OPLType = OPLType.suite
@@ -120,26 +133,32 @@ class Generator(ProblemLike):
120133

121134

122135
class Library(RootModel):
123-
root: dict[str, Problem | Generator | Suite | Implementation] | None
136+
root: dict[str, Problem | Generator | Suite | Implementation] = {}
124137

125138
def _check_id_references(self, ids, type:OPLType) -> None:
126-
if not self.root:
127-
return
128139
for id in ids:
129140
if id in self.root:
130141
if self.root[id].type != type:
131142
raise ValueError(f"ID {id} is a {self.root[id].name}, expected a {type.name}")
132143
else:
133144
raise ValueError(f"Missing {type.name} with id '{id}'")
134145

146+
# For a given suite, make sure the fidelty_levels property contains
147+
# the fidelity_levels of all problems in the suite.
135148
def _fixup_fidelity(self, suite: Suite) -> Suite:
149+
if suite.problems:
150+
if not suite.fidelity_levels:
151+
suite.fidelity_levels = set()
152+
for pid in suite.problems:
153+
problem = self.root[pid]
154+
assert isinstance(problem, Problem)
155+
if problem.fidelity_levels:
156+
suite.fidelity_levels.update(problem.fidelity_levels)
157+
136158
return suite
137159

138160
@model_validator(mode="after")
139-
def validate(self) -> Self:
140-
if not self.root:
141-
return self
142-
161+
def _validate(self) -> Self:
143162
# Make sure all problems referenced in suites exists
144163
for id, thing in self.root.items():
145164
if isinstance(thing, Suite) and thing.problems:
@@ -148,15 +167,19 @@ def validate(self) -> Self:
148167
raise ValueError(f"Suite {id} references problem with undefined id '{problem_id}'.")
149168
if self.root[problem_id].type != OPLType.problem:
150169
raise ValueError(f"Suite {id} references problem with id '{problem_id}' but id is a {self.root[problem_id].type.name}.")
170+
self._fixup_fidelity(thing)
151171
return self
152172

173+
153174
__all__ = [
154-
"Problem",
155-
"Suite",
175+
"Constraint",
156176
"Generator",
157177
"Implementation",
158178
"Library",
159-
"YesNoSome",
160179
"Link",
161-
"Reference"
180+
"Problem",
181+
"Reference",
182+
"Suite",
183+
"Variable",
184+
"YesNoSome",
162185
]

‎src/opltools/utils.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
class ValueRange(BaseModel):
66
min: int | None
7-
max: int | None
7+
max: int | None = None
88

99
@model_validator(mode="after")
1010
def _check(self) -> Self:
11-
if not self.min and not self.max:
11+
if self.min is None and self.max is None:
1212
raise ValueError("Variable range should have at least a min or max value.")
1313
return self
1414

15+
def __hash__(self):
16+
return hash((self.min, self.max))
17+
1518

1619
def _none_min(a, b):
1720
if a and b:

‎src/opltools/yesnosome.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from enum import Enum
2+
3+
class YesNoSome(Enum):
4+
yes = "yes"
5+
no = "no"
6+
some = "some"
7+
unknown = "?"
8+
9+
def union(
10+
a: YesNoSome | set[YesNoSome],
11+
b: YesNoSome | set[YesNoSome]
12+
) -> YesNoSome | set[YesNoSome]:
13+
result = set()
14+
if isinstance(a, YesNoSome):
15+
result.add(a)
16+
else:
17+
result.update(a)
18+
19+
if isinstance(b, YesNoSome):
20+
result.add(b)
21+
else:
22+
result.update(b)
23+
24+
if len(result) == 1:
25+
return result.pop()
26+
else:
27+
return result

‎tests/test_constraints.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
from opltools.schema import Constraints
1+
from opltools.schema import Constraint, YesNoSome
22

33

4-
class TestConstraints:
5-
def test_defaults(self):
6-
c = Constraints()
7-
assert c.box == 0
8-
assert c.linear == 0
9-
assert c.function == 0
4+
class TestConstraint:
5+
def test_default(self):
6+
c = Constraint(type="box")
7+
assert c.soft is None
8+
assert c.equality is None
9+
assert c.number is None
1010

11-
def test_union(self):
12-
a = Constraints(box=1, linear=2, function=3)
13-
b = Constraints(box=4, linear=5, function=6)
14-
a.union(b)
15-
assert a.box == {1, 4}
16-
assert a.linear == {2, 5}
17-
assert a.function == {3, 6}
11+
def test_soft(self):
12+
c = Constraint(type="box", soft="yes")
13+
assert c.soft == YesNoSome.yes
14+
15+
def test_number(self):
16+
c = Constraint(type="linear", number=5)
17+
assert c.number == 5

0 commit comments

Comments
 (0)