funkelab / ilpy

Unified python wrappers for popular ILP solvers
https://funkelab.github.io/ilpy/
MIT License
3 stars 2 forks source link

Add `from_coefficients` class methods to Constraint and Objective #21

Closed tlambert03 closed 1 year ago

tlambert03 commented 1 year ago

this adds two new convenience constructors to Constraint and Objective ... moving the logic that was in the expressions.py module down to a lower level so it can be reused more easily.

It lets you more easily build these objects from a list or dict of coefficients without having to do the for loop yourself:

LinearCoeffs = Sequence[float] | Mapping[int, float]
QCoeffs = Mapping[tuple[int, int], float] | Iterable[tuple[tuple[int, int], float]]

class Constraint:
    @classmethod
    def from_coefficients(
        cls,
        coefficients: LinearCoeffs = (),
        quadratic_coefficients: QCoeffs = (),
        relation: Relation = Relation.LessEqual,
        value: float = 0,
    ) -> Constraint: ...
funkey commented 1 year ago

Thanks a lot! :)

codecov-commenter commented 1 year ago

Codecov Report

Merging #21 (4a9307c) into main (5a2b221) will decrease coverage by 0.14%. The diff coverage is 88.23%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main      #21      +/-   ##
==========================================
- Coverage   86.46%   86.33%   -0.14%     
==========================================
  Files           3        3              
  Lines         303      322      +19     
==========================================
+ Hits          262      278      +16     
- Misses         41       44       +3     
Impacted Files Coverage Δ
ilpy/wrapper.pyx 76.04% <85.71%> (+3.91%) :arrow_up:
ilpy/expressions.py 91.16% <93.75%> (+0.68%) :arrow_up: