Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Jun 16, 2024
1 parent 2ab8f57 commit 9150869
Show file tree
Hide file tree
Showing 22 changed files with 634 additions and 369 deletions.
61 changes: 61 additions & 0 deletions issue17a.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
\Problem name:

Minimize
OBJROW: 7740 item0_in_bin1 + 489 item1_in_bin1 + 152 item2_in_bin1 + 3410 item3_in_bin1 + 9948 item4_in_bin1 + 7862 item5_in_bin1 + 8519 item6_in_bin1 + 3212 item7_in_bin1 + 8798 item8_in_bin1 + 4979 item9_in_bin1
+ 2178 item10_in_bin1 + 5984 item11_in_bin1 + 2518 item12_in_bin1 + 3270 item13_in_bin1 + 7183 item14_in_bin1
Subject To
constr(0): item0_in_bin0 >= 0
constr(1): item1_in_bin0 >= 0
constr(2): item2_in_bin0 >= 0
constr(3): item3_in_bin0 >= 0
constr(4): item4_in_bin0 >= 0
constr(5): item5_in_bin0 >= 0
constr(6): item6_in_bin0 >= 0
constr(7): item7_in_bin0 >= 0
constr(8): item8_in_bin0 >= 0
constr(9): item9_in_bin0 >= 0
constr(10): item10_in_bin0 >= 0
constr(11): item11_in_bin0 >= 0
constr(12): item12_in_bin0 >= 0
constr(13): item13_in_bin0 >= 0
constr(14): item14_in_bin0 >= 0
constr(15): item0_in_bin1 >= 0
constr(16): item1_in_bin1 >= 0
constr(17): item2_in_bin1 >= 0
constr(18): item3_in_bin1 >= 0
constr(19): item4_in_bin1 >= 0
constr(20): item5_in_bin1 >= 0
constr(21): item6_in_bin1 >= 0
constr(22): item7_in_bin1 >= 0
constr(23): item8_in_bin1 >= 0
constr(24): item9_in_bin1 >= 0
constr(25): item10_in_bin1 >= 0
constr(26): item11_in_bin1 >= 0
constr(27): item12_in_bin1 >= 0
constr(28): item13_in_bin1 >= 0
constr(29): item14_in_bin1 >= 0
constr(30): item0_in_bin0 + item0_in_bin1 = 1
constr(31): item1_in_bin0 + item1_in_bin1 = 1
constr(32): item2_in_bin0 + item2_in_bin1 = 1
constr(33): item3_in_bin0 + item3_in_bin1 = 1
constr(34): item4_in_bin0 + item4_in_bin1 = 1
constr(35): item5_in_bin0 + item5_in_bin1 = 1
constr(36): item6_in_bin0 + item6_in_bin1 = 1
constr(37): item7_in_bin0 + item7_in_bin1 = 1
constr(38): item8_in_bin0 + item8_in_bin1 = 1
constr(39): item9_in_bin0 + item9_in_bin1 = 1
constr(40): item10_in_bin0 + item10_in_bin1 = 1
constr(41): item11_in_bin0 + item11_in_bin1 = 1
constr(42): item12_in_bin0 + item12_in_bin1 = 1
constr(43): item13_in_bin0 + item13_in_bin1 = 1
constr(44): item14_in_bin0 + item14_in_bin1 = 1
constr(45): -7740 item0_in_bin0 + 7740 item0_in_bin1 -489 item1_in_bin0 + 489 item1_in_bin1 -152 item2_in_bin0 + 152 item2_in_bin1 -3410 item3_in_bin0 + 3410 item3_in_bin1 -9948 item4_in_bin0 + 9948 item4_in_bin1
-7862 item5_in_bin0 + 7862 item5_in_bin1 -8519 item6_in_bin0 + 8519 item6_in_bin1 -3212 item7_in_bin0 + 3212 item7_in_bin1 -8798 item8_in_bin0 + 8798 item8_in_bin1 -4979 item9_in_bin0 + 4979 item9_in_bin1
-2178 item10_in_bin0 + 2178 item10_in_bin1 -5984 item11_in_bin0 + 5984 item11_in_bin1 -2518 item12_in_bin0 + 2518 item12_in_bin1 -3270 item13_in_bin0 + 3270 item13_in_bin1 -7183 item14_in_bin0 + 7183 item14_in_bin1
>= -0
Bounds
Integers
item0_in_bin0 item0_in_bin1 item1_in_bin0 item1_in_bin1 item2_in_bin0 item2_in_bin1 item3_in_bin0 item3_in_bin1 item4_in_bin0 item4_in_bin1
item5_in_bin0 item5_in_bin1 item6_in_bin0 item6_in_bin1 item7_in_bin0 item7_in_bin1 item8_in_bin0 item8_in_bin1 item9_in_bin0 item9_in_bin1
item10_in_bin0 item10_in_bin1 item11_in_bin0 item11_in_bin1 item12_in_bin0 item12_in_bin1 item13_in_bin0 item13_in_bin1 item14_in_bin0 item14_in_bin1
End
30 changes: 30 additions & 0 deletions issue17a.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
item0_in_bin0 = 1
item1_in_bin0 = 1
item2_in_bin0 = 1
item3_in_bin0 = 1
item4_in_bin0 = 1
item5_in_bin0 = 1
item6_in_bin0 = 1
item7_in_bin0 = 0
item8_in_bin0 = 0
item9_in_bin0 = 0
item10_in_bin0 = 0
item11_in_bin0 = 0
item12_in_bin0 = 0
item13_in_bin0 = 0
item14_in_bin0 = 0
item0_in_bin1 = 0
item1_in_bin1 = 0
item2_in_bin1 = 0
item3_in_bin1 = 0
item4_in_bin1 = 0
item5_in_bin1 = 0
item6_in_bin1 = 0
item7_in_bin1 = 1
item8_in_bin1 = 1
item9_in_bin1 = 1
item10_in_bin1 = 1
item11_in_bin1 = 1
item12_in_bin1 = 1
item13_in_bin1 = 1
item14_in_bin1 = 1
61 changes: 61 additions & 0 deletions issue17b.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
\Problem name:

Minimize
OBJROW: 152 item0_in_bin1 + 489 item1_in_bin1 + 2178 item2_in_bin1 + 2518 item3_in_bin1 + 3212 item4_in_bin1 + 3270 item5_in_bin1 + 3410 item6_in_bin1 + 4979 item7_in_bin1 + 5984 item8_in_bin1 + 7183 item9_in_bin1
+ 7740 item10_in_bin1 + 7862 item11_in_bin1 + 8519 item12_in_bin1 + 8798 item13_in_bin1 + 9948 item14_in_bin1
Subject To
constr(0): item0_in_bin0 >= 0
constr(1): item1_in_bin0 >= 0
constr(2): item2_in_bin0 >= 0
constr(3): item3_in_bin0 >= 0
constr(4): item4_in_bin0 >= 0
constr(5): item5_in_bin0 >= 0
constr(6): item6_in_bin0 >= 0
constr(7): item7_in_bin0 >= 0
constr(8): item8_in_bin0 >= 0
constr(9): item9_in_bin0 >= 0
constr(10): item10_in_bin0 >= 0
constr(11): item11_in_bin0 >= 0
constr(12): item12_in_bin0 >= 0
constr(13): item13_in_bin0 >= 0
constr(14): item14_in_bin0 >= 0
constr(15): item0_in_bin1 >= 0
constr(16): item1_in_bin1 >= 0
constr(17): item2_in_bin1 >= 0
constr(18): item3_in_bin1 >= 0
constr(19): item4_in_bin1 >= 0
constr(20): item5_in_bin1 >= 0
constr(21): item6_in_bin1 >= 0
constr(22): item7_in_bin1 >= 0
constr(23): item8_in_bin1 >= 0
constr(24): item9_in_bin1 >= 0
constr(25): item10_in_bin1 >= 0
constr(26): item11_in_bin1 >= 0
constr(27): item12_in_bin1 >= 0
constr(28): item13_in_bin1 >= 0
constr(29): item14_in_bin1 >= 0
constr(30): item0_in_bin0 + item0_in_bin1 = 1
constr(31): item1_in_bin0 + item1_in_bin1 = 1
constr(32): item2_in_bin0 + item2_in_bin1 = 1
constr(33): item3_in_bin0 + item3_in_bin1 = 1
constr(34): item4_in_bin0 + item4_in_bin1 = 1
constr(35): item5_in_bin0 + item5_in_bin1 = 1
constr(36): item6_in_bin0 + item6_in_bin1 = 1
constr(37): item7_in_bin0 + item7_in_bin1 = 1
constr(38): item8_in_bin0 + item8_in_bin1 = 1
constr(39): item9_in_bin0 + item9_in_bin1 = 1
constr(40): item10_in_bin0 + item10_in_bin1 = 1
constr(41): item11_in_bin0 + item11_in_bin1 = 1
constr(42): item12_in_bin0 + item12_in_bin1 = 1
constr(43): item13_in_bin0 + item13_in_bin1 = 1
constr(44): item14_in_bin0 + item14_in_bin1 = 1
constr(45): -152 item0_in_bin0 + 152 item0_in_bin1 -489 item1_in_bin0 + 489 item1_in_bin1 -2178 item2_in_bin0 + 2178 item2_in_bin1 -2518 item3_in_bin0 + 2518 item3_in_bin1 -3212 item4_in_bin0 + 3212 item4_in_bin1
-3270 item5_in_bin0 + 3270 item5_in_bin1 -3410 item6_in_bin0 + 3410 item6_in_bin1 -4979 item7_in_bin0 + 4979 item7_in_bin1 -5984 item8_in_bin0 + 5984 item8_in_bin1 -7183 item9_in_bin0 + 7183 item9_in_bin1
-7740 item10_in_bin0 + 7740 item10_in_bin1 -7862 item11_in_bin0 + 7862 item11_in_bin1 -8519 item12_in_bin0 + 8519 item12_in_bin1 -8798 item13_in_bin0 + 8798 item13_in_bin1 -9948 item14_in_bin0 + 9948 item14_in_bin1
>= -0
Bounds
Integers
item0_in_bin0 item0_in_bin1 item1_in_bin0 item1_in_bin1 item2_in_bin0 item2_in_bin1 item3_in_bin0 item3_in_bin1 item4_in_bin0 item4_in_bin1
item5_in_bin0 item5_in_bin1 item6_in_bin0 item6_in_bin1 item7_in_bin0 item7_in_bin1 item8_in_bin0 item8_in_bin1 item9_in_bin0 item9_in_bin1
item10_in_bin0 item10_in_bin1 item11_in_bin0 item11_in_bin1 item12_in_bin0 item12_in_bin1 item13_in_bin0 item13_in_bin1 item14_in_bin0 item14_in_bin1
End
30 changes: 30 additions & 0 deletions issue17b.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
item0_in_bin0 = 0
item1_in_bin0 = 0
item2_in_bin0 = 0
item3_in_bin0 = 1
item4_in_bin0 = 0
item5_in_bin0 = 0
item6_in_bin0 = 1
item7_in_bin0 = 0
item8_in_bin0 = 1
item9_in_bin0 = 0
item10_in_bin0 = 1
item11_in_bin0 = 0
item12_in_bin0 = 1
item13_in_bin0 = 0
item14_in_bin0 = 1
item0_in_bin1 = 1
item1_in_bin1 = 1
item2_in_bin1 = 1
item3_in_bin1 = 0
item4_in_bin1 = 1
item5_in_bin1 = 1
item6_in_bin1 = 0
item7_in_bin1 = 1
item8_in_bin1 = 0
item9_in_bin1 = 1
item10_in_bin1 = 0
item11_in_bin1 = 1
item12_in_bin1 = 0
item13_in_bin1 = 1
item14_in_bin1 = 0
17 changes: 8 additions & 9 deletions prtpy/binners.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class Binner(ABC):
All arrays created by the same binner share the following two variables:
* numbins - the total number of bins.
* valueof - a function that maps an item to its value.
* copiesof - a function that maps an item to the number of copies.
"""
def __init__(self, valueof: Callable = lambda x:x):
def __init__(self, valueof: Callable = lambda x:x, copiesof: Callable=lambda x:1):
self.valueof = valueof
self.copiesof = copiesof

@abstractmethod
def new_bins(self, numbins:int)->BinsArray:
Expand Down Expand Up @@ -186,8 +188,8 @@ class BinnerKeepingSums(Binner):
Bin #1: sum=3.0
"""

def __init__(self, valueof: Callable = lambda x:x):
super().__init__(valueof)
def __init__(self, valueof: Callable = lambda x:x, copiesof: Callable=lambda x:1):
super().__init__(valueof,copiesof)

BinsArray = np.ndarray # Here, the bins-array is simply an array of the sums.

Expand Down Expand Up @@ -330,8 +332,8 @@ class BinnerKeepingContents(BinnerKeepingSums):
Bin #1: ['a'], sum=3.0
"""

def __init__(self, valueof: Callable = lambda x:x):
super().__init__(valueof)
def __init__(self, valueof: Callable = lambda x:x, copiesof: Callable=lambda x:1):
super().__init__(valueof, copiesof)

BinsArray = Tuple[np.ndarray, List[List]] # Here, each bins-array is a tuple: sums,lists. sums is an array of sums; lists is a list of lists of items.

Expand Down Expand Up @@ -451,7 +453,4 @@ def printbins(bins:BinsArray):

if __name__ == "__main__":
import doctest, sys
(failures, tests) = doctest.testmod(report=True, optionflags=doctest.FAIL_FAST)
print("{} failures, {} tests".format(failures, tests))
if failures>0:
sys.exit(1)
print(doctest.testmod(report=True, optionflags=doctest.FAIL_FAST))
10 changes: 5 additions & 5 deletions prtpy/outputtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class OutputType(ABC):
@classmethod
def create_binner(cls, valueof: Callable) -> Binner:
def create_binner(cls, valueof: Callable, copiesof: Callable=None) -> Binner:
"""
Construct and return a Bins structure. Used at the initialization phase of an algorithm.
"""
Expand All @@ -30,8 +30,8 @@ def extract_output_from_binsarray(cls, bins: BinsArray) -> List:
class Sums(OutputType):
""" Output the list of sums of all bins (but not the bins' contents). """
@classmethod
def create_binner(cls, valueof: Callable) -> List:
return BinnerKeepingSums(valueof)
def create_binner(cls, valueof: Callable, copiesof: Callable=None) -> List:
return BinnerKeepingSums(valueof,copiesof)

@classmethod
def extract_output_from_sums(cls, sums: List[float]) -> List:
Expand Down Expand Up @@ -94,8 +94,8 @@ class Partition(OutputType):
""" Output the set of all bins. """

@classmethod
def create_binner(cls, valueof: Callable) -> List:
return BinnerKeepingContents(valueof)
def create_binner(cls, valueof: Callable, copiesof: Callable=None) -> List:
return BinnerKeepingContents(valueof,copiesof)

@classmethod
def extract_output_from_sums_and_lists(cls, sums: List[float], lists: List[List[Any]]) -> List:
Expand Down
28 changes: 26 additions & 2 deletions prtpy/partitioning/adaptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
from prtpy import outputtypes as out, objectives as obj
from prtpy.binners import Binner
from typing import Callable, List, Any
from numbers import Number

def partition(
algorithm: Callable,
numbins: int,
items: Any,
valueof: Callable[[Any], float] = None,
copies: Any = 1,
outputtype: out.OutputType = out.Partition,
**kwargs
):
Expand All @@ -32,6 +34,7 @@ def partition(
numbins (int),
items (list),
valueof (callable),
copiesof (callable),
outputtype (OutputType).
:param numbins: int - how many parts should be in the partition?
Expand All @@ -42,6 +45,7 @@ def partition(
* A list of strings (in this case, valueof should also be defined, and map each item name to its value);
:param valueof: optional; required only if `items` is a list of item-names.
:param copiesof: optional; required only if `items` is a list of item-names.
:param outputtype: what output to return. See `outputtypes.py'.
Expand Down Expand Up @@ -73,11 +77,31 @@ def partition(
item_names = items.keys()
if valueof is None:
valueof = items.__getitem__
else: # items is a list

# copiesof:
if isinstance(copies,dict):
copiesof = copies.__getitem__
elif isinstance(copies,Number):
copiesof = lambda item: copies
else:
raise TypeError(f"copies parameter {copies} is of wrong type {type(copies)}")
else: # items is a list of values
item_names = items
if valueof is None:
valueof = lambda item: item
binner = outputtype.create_binner(valueof)

# copiesof:
if isinstance(copies,list):
if len(set(items))==len(items):
copies_dict = dict(zip(items,copies)) # Works only when each item appears once
copiesof = copies_dict.__getitem__
else:
raise ValueError(f"copies argument can only be used when item names are unique, but they are not: {item_names}")
elif isinstance(copies,Number):
copiesof = lambda item: copies
else:
raise TypeError(f"copies parameter {copies} is of wrong type {type(copies)}")
binner = outputtype.create_binner(valueof,copiesof)
bins = algorithm(binner, numbins, item_names, **kwargs)
return outputtype.extract_output_from_binsarray(bins)

Expand Down
Loading

0 comments on commit 9150869

Please sign in to comment.