Skip to content
Merged
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
61 changes: 52 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: Release on PyPI

on:
push:
Expand All @@ -9,18 +9,61 @@ permissions:
contents: write

jobs:
pypi-publish:
name: Upload release to PyPI
build:
runs-on: ubuntu-latest
environment: release
permissions:
# IMPORTANT: this permission is mandatory for trusted publishing
contents: read
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Install uv
uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
with:
enable-cache: false

- name: Build
run: uv build

# Optional, but recommended: run smoke tests on distributions
- name: Smoke test (wheel)
run: uv run --isolated --no-project --with dist/*.whl tests/smoke_test.py
- name: Smoke test (source distribution)
run: uv run --isolated --no-project --with dist/*.tar.gz tests/smoke_test.py

- name: Upload distributions as artifacts
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: dist
path: dist/

publish:
needs:
- build
runs-on: ubuntu-latest
environment:
name: release
permissions:
id-token: write
steps:
- uses: actions/checkout@v3
- uses: pdm-project/setup-pdm@v3
- name: Publish package distributions to PyPI
run: pdm publish
- name: Install uv
uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
with:
enable-cache: false

- name: Download distributions artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: dist
path: dist/

- name: Generate PEP 740 attestations
uses: astral-sh/attest-action@f589a42a7efb6fe400b4f400de60b4bc90390027 # v0.0.6

- name: Publish
run: uv publish

release:
name: Create Github release
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.11
3.14
31 changes: 11 additions & 20 deletions docs/instructor/problem/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ like this

```py
class Instance(InstanceModel):

rectangles: list[tuple[int, int, int, int]]
```

Expand All @@ -26,15 +25,13 @@ from algobattle.util import BaseModel


class Rectangle(BaseModel):

x: int
y: int
width: int
height: int


class Instance(InstanceModel):

rectangles: list[Rectangle]
```

Expand Down Expand Up @@ -106,6 +103,7 @@ creating the Python object. We can use it either as the default value of the att
```py
from pydantic import Field


class Instance(InstanceModel):
"""An instance of the Some Example problem."""

Expand Down Expand Up @@ -167,7 +165,6 @@ directionless. Both graph's size is the number of vertices in it.
def size(self) -> int:
"""A graph's size is the number of vertices in it."""
return self.num_vertices

```

!!! tip "Associated Annotation Types"
Expand All @@ -184,17 +181,14 @@ indexed with the type of the weights you want to use.
would be done like this:

```py
class Instance(DirectedGraph, VertexWeights[str], EdgeWeights[float]):

...
class Instance(DirectedGraph, VertexWeights[str], EdgeWeights[float]): ...
```

Both are encoded as lists of the weights where the nth entry corresponds to the weight of the nth vertex or edge.
I.e. the above is equivalent to

```py
class Instance(DirectedGraph):

vertex_weights: Annotated[list[str], SizeLen]
edge_weights: Annotated[list[float], EdgeLen]

Expand Down Expand Up @@ -293,11 +287,11 @@ class Instance(DirectedGraph):
class Solution(SolutionModel):
"""Solutions of the 3 Shortest Paths problem."""

paths: tuple[Path, Path, Path] # (2)!
paths: tuple[Path, Path, Path] # (2)!

@minimize
def score(self, role: Role) -> float:
...
def score(self, role: Role) -> float: ...


Problem(
name="3 Shortest Paths",
Expand Down Expand Up @@ -332,16 +326,13 @@ class Solution(SolutionModel):
paths: tuple[Path, Path, Path]


def compare_each_path(
instance: Instance,
generator_solution: Solution,
solver_solution: Solution
) -> float:
gen_lens = sorted(len(path) for path in generator_solution.paths) # (1)!
def compare_each_path(instance: Instance, generator_solution: Solution, solver_solution: Solution) -> float:
gen_lens = sorted(len(path) for path in generator_solution.paths) # (1)!
sol_lens = sorted(len(path) for path in solver_solution.paths)
ratios = [len(gen) / len(sol) for gen, sol in zip(gen_lens, sol_lens)] # (2)!
ratios = [min(1, max(0, num)) for num in ratios] # (3)!
return sum(ratios) / 3 # (4)!
ratios = [len(gen) / len(sol) for gen, sol in zip(gen_lens, sol_lens)] # (2)!
ratios = [min(1, max(0, num)) for num in ratios] # (3)!
return sum(ratios) / 3 # (4)!


Problem(
name="3 Shortest Paths",
Expand Down
18 changes: 6 additions & 12 deletions docs/instructor/problem/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type aliases, which basically just are variables but for types. For example, con

```py
class Example(InstanceModel):

edges: list[tuple[int, int]]
matchings: list[set[tuple[int, int]]]
```
Expand All @@ -35,8 +34,8 @@ version and how explicit you want (and have) to be, but they all do the same thi
Edge = tuple[int, int]
Matching = set[Edge]

class Example(InstanceModel):

class Example(InstanceModel):
edges: list[Edge]
matchings: list[Matching]
```
Expand All @@ -47,8 +46,8 @@ version and how explicit you want (and have) to be, but they all do the same thi
Edge: TypeAlias = tuple[int, int]
Matching: TypeAlias = set[Edge]

class Example(InstanceModel):

class Example(InstanceModel):
edges: list[Edge]
matchings: list[Matching]
```
Expand All @@ -59,8 +58,8 @@ version and how explicit you want (and have) to be, but they all do the same thi
type Edge = tuple[int, int]
type Matching = set[Edge]

class Example(InstanceModel):

class Example(InstanceModel):
edges: list[Edge]
matchings: list[Matching]
```
Expand Down Expand Up @@ -128,9 +127,9 @@ problem definition we can usually prevent them altogether by just reordering the

```py
class Example(InstanceModel):

some_attr: "CoolNumber"


CoolNumber = int
```

Expand All @@ -155,9 +154,7 @@ would then specify this using its `Field` specifier like this

```py
class Example(InstanceModel):

positive_int: int = Field(gt=0)

```

where the `gt` key tells Pydantic that it should validate this field as being greater than 0. This works great when you
Expand All @@ -172,13 +169,12 @@ several fields should be validated with a single `Field`.
```py
PositiveInt = Annotated[int, Field(gt=0)]

class Example(InstanceModel):

class Example(InstanceModel):
first: PositiveInt
second: PositiveInt
third: PositiveInt
fourth: PositiveInt

```

The Python standard library `annotated_types` also contains a collection of basic metadata types such as `Gt`, `Ge`,
Expand All @@ -189,7 +185,6 @@ The Python standard library `annotated_types` also contains a collection of basi

```py
class Example(InstanceModel):

first: int = Field(ge=3, lt=18)
second: Annotated[int, Field(ge=3, lt=18)]
third: Annotated[int, Ge(3), Lt(18)]
Expand Down Expand Up @@ -248,12 +243,10 @@ will be found in the instance or solution. Our example problem then becomes simp

```py
class Instance(InstanceModel):

numbers: list[int]


class Solution(InstanceModel):

biggest: Annotated[int, In(InstanceRef.numbers)]
```

Expand Down Expand Up @@ -304,6 +297,7 @@ the validation function also receives the value of a referenced attribute.
if not val.startswith(" " * indent_level):
raise ValueError


IndentedLine = Annotated[str, AttributeReferenceValidator(check_indentation, InstanceRef.indentation)]
```

Expand Down
Loading
Loading