Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Jan 26, 2025
1 parent 673d161 commit 13340c1
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 91 deletions.
3 changes: 3 additions & 0 deletions examples/_pweave.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ def publish_to_markdown(python_file: str, output_file: str):
if python_file != os.path.basename(__file__):
output_file = python_file.replace(".py", ".md")
publish_to_markdown(python_file, output_file)
else:
print(glob.glob("*.py"))

30 changes: 15 additions & 15 deletions examples/anytime_algorithm_demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ for time_limit in [0.25,0.5,1,2,4,8]:
```

```
Greedy: (4667.990849194712, 5767.517683956478)
0.00033339999999970615 seconds
Greedy: (4822.668239457016, 5783.838801253794)
0.0010743999882834032 seconds
Complete greedy:
0.25: (4763.3417890516785, 5727.287117998592)
0.2506885999999997 seconds
0.5: (4787.022525659784, 5500.643023962724)
0.5009939999999999 seconds
1: (4808.442668794849, 5487.193441478978)
1.0018127000000003 seconds
2: (4831.974064872426, 5421.068259104535)
2.0104259000000004 seconds
4: (4831.974064872426, 5421.068259104535)
4.023130400000001 seconds
8: (4841.696699437434, 5411.345624539528)
8.032873499999999 seconds
0.25: (4960.0, 5635.0)
0.2506347000016831 seconds
0.5: (5022.0, 5632.0)
0.5011180000001332 seconds
1: (4975.0, 5505.0)
1.0044468000123743 seconds
2: (4979.0, 5501.0)
2.007786900008796 seconds
4: (5042.0, 5420.0)
4.018622699994012 seconds
8: (5051.0, 5420.0)
8.044986099994276 seconds
```


---
Markdown generated automatically from [anytime_algorithm_demo.py](anytime_algorithm_demo.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [anytime_algorithm_demo.py](anytime_algorithm_demo.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
2 changes: 1 addition & 1 deletion examples/covering_algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ print(prtpy.pack(algorithm=prtpy.covering.threequarters, binsize=60, items=items


---
Markdown generated automatically from [covering_algorithms.py](covering_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [covering_algorithms.py](covering_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
8 changes: 4 additions & 4 deletions examples/input_formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ print(prtpy.partition(algorithm=prtpy.partitioning.greedy, numbins=3, items=valu
```

```
[23 89 39 13 79 57 39 56 50 97 56 9 74 88 38 91 44 38 94 36]
[[97, 79, 74, 44, 38, 36], [94, 88, 56, 50, 39, 38, 9], [91, 89, 57,
56, 39, 23, 13]]
[26 68 35 95 87 50 94 24 16 42 49 76 45 67 16 60 26 77 78 93]
[[95, 77, 76, 49, 45, 24], [94, 78, 68, 60, 35, 26, 16], [93, 87, 67,
50, 42, 26, 16]]
```


---
Markdown generated automatically from [input_formats.py](input_formats.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [input_formats.py](input_formats.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
10 changes: 5 additions & 5 deletions examples/maximin_share_demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def maximin_share_partition(c:int, valuation:list, items:Collection[Any]=None, n
items=items,
valueof=lambda item: valuation[item],
objective=prtpy.obj.MaximizeKSmallestSums(numerator),
outputtype=prtpy.out.PartitionAndSums,
outputtype=prtpy.out.PartitionAndSumsTuple,
**kwargs
)
return (lists, list(sums), sum(sorted(sums)[:numerator]))
Expand Down Expand Up @@ -106,16 +106,16 @@ mms_demo(3, [29, 29, 28, 16, 2])
Valuation: [5, 5, 5, 7, 7, 7, 11, 17, 23, 23, 23, 31, 31, 31, 65]
1-out-of-3 partition = [[11, 14], [2, 3, 4, 5, 7, 8, 12], [0, 1, 6, 9,
10, 13]], values = [96.0, 97.0, 98.0], min-value = 96.0
2-out-of-3 partition = [[11, 14], [0, 2, 3, 6, 8, 9, 10], [1, 4, 5, 7,
12, 13]], values = [96.0, 97.0, 98.0], min-value = 193.0
2-out-of-3 partition = [[12, 14], [2, 3, 8, 11, 13], [0, 1, 4, 5, 6,
7, 9, 10]], values = [96.0, 97.0, 98.0], min-value = 193.0
Valuation: [29, 29, 28, 16, 2]
1-out-of-3 partition = [[0], [2, 4], [1, 3]], values = [29.0, 30.0,
45.0], min-value = 29.0
2-out-of-3 partition = [[1], [0, 4], [2, 3]], values = [29.0, 31.0,
2-out-of-3 partition = [[0], [1, 4], [2, 3]], values = [29.0, 31.0,
44.0], min-value = 60.0
```


---
Markdown generated automatically from [maximin_share_demo.py](maximin_share_demo.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [maximin_share_demo.py](maximin_share_demo.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
26 changes: 19 additions & 7 deletions examples/objectives.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, outputtype=Partition
```

```
(array([62., 62., 53.]), [[46, 16], [39, 13, 10], [27, 26]])
Bin #0: [46, 16], sum=62.0
Bin #1: [39, 13, 10], sum=62.0
Bin #2: [27, 26], sum=53.0
```


Expand All @@ -35,7 +37,9 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, outputtype=Partition
```

```
(array([56., 56., 65.]), [[46, 10], [27, 16, 13], [39, 26]])
Bin #0: [46, 10], sum=56.0
Bin #1: [27, 16, 13], sum=56.0
Bin #2: [39, 26], sum=65.0
```


Expand All @@ -47,7 +51,9 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, outputtype=Partition
```

```
(array([55., 59., 63.]), [[39, 16], [46, 13], [27, 26, 10]])
Bin #0: [39, 16], sum=55.0
Bin #1: [46, 13], sum=59.0
Bin #2: [27, 26, 10], sum=63.0
```


Expand All @@ -64,7 +70,9 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, outputtype=Partition
```

```
(array([ 0., 49., 128.]), [[], [26, 13, 10], [46, 39, 27, 16]])
Bin #0: [], sum=0.0
Bin #1: [26, 13, 10], sum=49.0
Bin #2: [46, 39, 27, 16], sum=128.0
```


Expand All @@ -84,7 +92,9 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, objective=MaximizeKS
```

```
(array([62., 62., 53.]), [[46, 16], [39, 13, 10], [27, 26]])
Bin #0: [46, 16], sum=62.0
Bin #1: [39, 13, 10], sum=62.0
Bin #2: [27, 26], sum=53.0
```


Expand All @@ -96,9 +106,11 @@ print(prtpy.partition(algorithm=dp, numbins=3, items=items, objective=MinimizeKL
```

```
(array([56., 56., 65.]), [[46, 10], [27, 16, 13], [39, 26]])
Bin #0: [46, 10], sum=56.0
Bin #1: [27, 16, 13], sum=56.0
Bin #2: [39, 26], sum=65.0
```


---
Markdown generated automatically from [objectives.py](objectives.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [objectives.py](objectives.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
27 changes: 24 additions & 3 deletions examples/output_formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,34 @@ print(prtpy.partition(algorithm=greedy, numbins=2, items=values, outputtype=prtp
Return both the partition and the sum of each bin:

```python
prtpy.partition(algorithm=greedy, numbins=2, items=values, outputtype=prtpy.out.PartitionAndSums)
sums, lists = prtpy.partition(algorithm=greedy, numbins=2, items=values, outputtype=prtpy.out.PartitionAndSumsTuple)
print("sums: ",sums)
print("lists: ",lists)
```

```
(array([20., 23.]), [[8, 7, 5], [8, 6, 5, 4]])
sums: [20. 23.]
lists: [[8, 7, 5], [8, 6, 5, 4]]
```



Return both the partition and the sum of each bin, in a pretty-printable struct:

```python
result = prtpy.partition(algorithm=greedy, numbins=2, items=values, outputtype=prtpy.out.PartitionAndSums)
print(result)
print("sums: ",result.sums)
print("lists: ",result.lists)
```

```
Bin #0: [8, 7, 5], sum=20.0
Bin #1: [8, 6, 5, 4], sum=23.0
sums: [20. 23.]
lists: [[8, 7, 5], [8, 6, 5, 4]]
```


---
Markdown generated automatically from [output_formats.py](output_formats.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [output_formats.py](output_formats.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
43 changes: 42 additions & 1 deletion examples/packing_algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,46 @@ print(prtpy.pack(algorithm=prtpy.packing.bin_completion, binsize=61, items=items
```



More FFD examples from a recent paper:

```python
itemsA = [51, 28, 27, 27, 27, 26, 12, 12, 11, 11, 11, 11, 11, 11, 10]
print("\nA, 75: ",prtpy.pack(algorithm=prtpy.packing.first_fit_decreasing, binsize=75, items=itemsA, outputtype=prtpy.out.PartitionAndSums))
print("A, 76: ",prtpy.pack(algorithm=prtpy.packing.first_fit_decreasing, binsize=76, items=itemsA, outputtype=prtpy.out.PartitionAndSums))
itemsB = [51, 28, 27, 27, 27, 24, 21, 20, 10, 10, 10, 9, 9, 9, 9]
print("B, 75: ",prtpy.pack(algorithm=prtpy.packing.first_fit_decreasing, binsize=75, items=itemsB, outputtype=prtpy.out.PartitionAndSums))
itemsW = [51, 28, 28, 28, 27, 25, 12, 12, 10, 10, 10, 10, 10, 10, 10, 10]
print("\nW, 75: ",prtpy.pack(algorithm=prtpy.packing.first_fit_decreasing, binsize=75, items=itemsW, outputtype=prtpy.out.PartitionAndSums))
print("W, 76: ",prtpy.pack(algorithm=prtpy.packing.first_fit_decreasing, binsize=76, items=itemsW, outputtype=prtpy.out.PartitionAndSums))
```

```
A, 75: Bin #0: [51, 12, 12], sum=75.0
Bin #1: [28, 27, 11], sum=66.0
Bin #2: [27, 27, 11, 10], sum=75.0
Bin #3: [26, 11, 11, 11, 11], sum=70.0
A, 76: Bin #0: [51, 12, 12], sum=75.0
Bin #1: [28, 27, 11, 10], sum=76.0
Bin #2: [27, 27, 11, 11], sum=76.0
Bin #3: [26, 11, 11, 11], sum=59.0
B, 75: Bin #0: [51, 24], sum=75.0
Bin #1: [28, 27, 20], sum=75.0
Bin #2: [27, 27, 21], sum=75.0
Bin #3: [10, 10, 10, 9, 9, 9, 9], sum=66.0
W, 75: Bin #0: [51, 12, 12], sum=75.0
Bin #1: [28, 28, 10], sum=66.0
Bin #2: [28, 27, 10, 10], sum=75.0
Bin #3: [25, 10, 10, 10, 10, 10], sum=75.0
W, 76: Bin #0: [51, 25], sum=76.0
Bin #1: [28, 28, 12], sum=68.0
Bin #2: [28, 27, 12], sum=67.0
Bin #3: [10, 10, 10, 10, 10, 10, 10], sum=70.0
Bin #4: [10], sum=10.0
```


---
Markdown generated automatically from [packing_algorithms.py](packing_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [packing_algorithms.py](packing_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
61 changes: 18 additions & 43 deletions examples/partitioning_algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ print(f"\t {perf_counter()-start} seconds")
```

```
[55605.0, 55604.0, 55604.0, 55604.0, 55604.0, 55604.0, 55604.0,
55604.0, 55604.0]
0.23440219999999812 seconds
[55505.0, 55504.0, 55504.0, 55504.0, 55504.0, 55504.0, 55504.0,
55504.0, 55504.0]
0.2455579000088619 seconds
```


Expand All @@ -36,9 +36,9 @@ print(f"\t {perf_counter()-start} seconds")
```

```
[5567.0, 5567.0, 5567.0, 5567.0, 5567.0, 5567.0, 5567.0, 5567.0,
5520.0]
0.4883513999999991 seconds
[5592.0, 5592.0, 5592.0, 5592.0, 5592.0, 5592.0, 5592.0, 5592.0,
5547.0]
0.3740479999978561 seconds
```


Expand All @@ -55,8 +55,8 @@ print(f"\t {perf_counter()-start} seconds")
```

```
[118.0, 119.0, 119.0, 119.0]
0.18726790000000193 seconds
[129.0, 129.0, 130.0, 130.0]
0.3462842000008095 seconds
```


Expand All @@ -71,8 +71,8 @@ print(f"\t {perf_counter()-start} seconds")
```

```
[35.0, 35.0, 34.0]
0.15508069999999918 seconds
[34.0, 35.0, 34.0]
0.19563360000029206 seconds
```


Expand All @@ -81,40 +81,15 @@ The *complete greedy* algorithm (Korf, 1995) allows you to determine how much ti

```python
start = perf_counter()
values = np.random.randint(1,1000, 10000)
values = np.random.randint(1,1000, 100)
print(prtpy.partition(algorithm=prtpy.partitioning.complete_greedy, numbins=9, items=values, outputtype=prtpy.out.Sums, time_limit=1))
print(f"\t {perf_counter()-start} seconds")
```

```
---------------------------------------------------------------------------TypeError
Traceback (most recent call last)Input In [1], in <module>
1 start = perf_counter()
2 values = np.random.randint(1,1000, 10000)
----> 3
print(prtpy.partition(algorithm=prtpy.partitioning.complete_greedy,
numbins=9, items=values, outputtype=prtpy.out.Sums, time_limit=1))
4 print(f"\t {perf_counter()-start} seconds")
File
d:\dropbox\papers\electricitydivision__dinesh\code\prtpy\prtpy\partitioning\adaptors.py:82,
in partition(algorithm, numbins, items, valueof, outputtype, **kwargs)
80 binner = outputtype.create_binner(valueof)
81 bins = algorithm(binner, numbins, item_names, **kwargs)
---> 82 return outputtype.extract_output_from_binsarray(bins)
File
d:\dropbox\papers\electricitydivision__dinesh\code\prtpy\prtpy\outputtypes.py:47,
in Sums.extract_output_from_binsarray(cls, bins)
45 except:
46 sums = bins # If it fails, it means that bins is
a singleton: sums.
---> 47 return cls.extract_output_from_sums(sums)
File
d:\dropbox\papers\electricitydivision__dinesh\code\prtpy\prtpy\outputtypes.py:38,
in Sums.extract_output_from_sums(cls, sums)
36 @classmethod
37 def extract_output_from_sums(cls, sums: List[float]) -> List:
---> 38 return list(sums)
TypeError: 'NoneType' object is not iterable
[5819.0, 5822.0, 5827.0, 5833.0, 5843.0, 5852.0, 5871.0, 5875.0,
5876.0]
1.002595799989649 seconds
```


Expand All @@ -123,16 +98,16 @@ The *sequential number partitioning* algorithm (Korf, 2009) is an advanced optim

```python
start = perf_counter()
values = np.random.randint(1,1000, 10)
values = np.random.randint(1,100, 10)
print(prtpy.partition(algorithm=prtpy.partitioning.sequential_number_partitioning, numbins=9, items=values, outputtype=prtpy.out.Sums))
print(f"\t {perf_counter()-start} seconds")
```

```
[254.0, 299.0, 403.0, 496.0, 553.0, 579.0, 828.0, 937.0, 943.0]
10.203577499999998 seconds
[18.0, 19.0, 25.0, 33.0, 61.0, 65.0, 72.0, 90.0, 99.0]
50.17736359999981 seconds
```


---
Markdown generated automatically from [partitioning_algorithms.py](partitioning_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2022-07-11.
Markdown generated automatically from [partitioning_algorithms.py](partitioning_algorithms.py) using [Pweave](http://mpastell.com/pweave) 0.30.3 on 2025-01-26.
4 changes: 2 additions & 2 deletions examples/partitioning_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

#' The *complete greedy* algorithm (Korf, 1995) allows you to determine how much time you are willing to spend to find an optimal solution.
start = perf_counter()
values = np.random.randint(1,1000, 10000)
values = np.random.randint(1,1000, 100)
print(prtpy.partition(algorithm=prtpy.partitioning.complete_greedy, numbins=9, items=values, outputtype=prtpy.out.Sums, time_limit=1))
print(f"\t {perf_counter()-start} seconds")

#' The *sequential number partitioning* algorithm (Korf, 2009) is an advanced optimal partitioning algorithm. Programmed by Shmuel and Jonathan.
start = perf_counter()
values = np.random.randint(1,1000, 10)
values = np.random.randint(1,100, 10)
print(prtpy.partition(algorithm=prtpy.partitioning.sequential_number_partitioning, numbins=9, items=values, outputtype=prtpy.out.Sums))
print(f"\t {perf_counter()-start} seconds")
Loading

0 comments on commit 13340c1

Please sign in to comment.