Skip to content

Commit

Permalink
Extend default name generation (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth authored Aug 15, 2023
1 parent 6d569fc commit a765eb7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
17 changes: 8 additions & 9 deletions opvious/modeling/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,22 @@ def __init__(self, labels: Mapping[GlobalIdentifier, Label]) -> None:

def _format_dimension(self, label: Label, env: Environment) -> Name:
i = _last_capital_index(label)
if i is None:
return _first_available(label[0].upper(), env)
return f"{label[i]}^\\mathrm{{{label[:i]}}}" if i > 0 else label[i]
r = label[i or 0].upper()
n = r if i is None else f"{r}^\\mathrm{{{label[:i]}}}"
return _first_available(n, env)

def _format_parameter(self, label: Label, env: Environment) -> Name:
i = _last_capital_index(label)
if not i:
return _first_available(label[0].lower(), env)
return f"{label[i].lower()}^\\mathrm{{{label[:i]}}}"
r = label[i or 0].lower()
n = r if i is None else f"{r}^\\mathrm{{{label[:i]}}}"
return _first_available(n, env)

def _format_variable(self, label: Label, env: Environment) -> Name:
i = _last_capital_index(label)
r = label[i or 0].lower()
g = _greek_letters.get(r, r)
if not i:
return _first_available(g, env)
return f"{g}^\\mathrm{{{label[:i]}}}"
n = g if i is None else f"{g}^\\mathrm{{{label[:i]}}}"
return _first_available(n, env)

def format_quantifier(
self, identifier: QuantifierIdentifier, env: Environment
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "opvious"
version = "0.17.1rc1"
version = "0.17.2rc1"
description = "Opvious Python SDK"
authors = ["Opvious Engineering <[email protected]>"]
readme = "README.md"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ async def test_definition_counts(self):
counts = model.definition_counts()
assert counts["PARAMETER"].iloc[0] == 4

@pytest.mark.asyncio
async def test_global_name_collision(self):
class _Model(om.Model):
step_count = om.Variable.natural()
step_cost = om.Variable.non_negative()
step_color = om.Variable.non_negative()

@om.objective
def minimize_cost(self):
return self.step_count() + self.step_cost() + self.step_color()

model = _Model()
spec = await client.annotate_specification(model.specification())
assert spec.annotation.issue_count == 0
assert r"\chi^\mathrm{step}''" in spec.sources[0].text

@pytest.mark.asyncio
async def test_annotate_markdown_repr(self):
model = InvalidSetCoverModel()
Expand Down

0 comments on commit a765eb7

Please sign in to comment.