Skip to content

Commit

Permalink
Add MinimizeTheDistanceFromAvg to objectives.py
Browse files Browse the repository at this point in the history
  • Loading branch information
8onlichtman authored Mar 31, 2024
1 parent fa3ecd3 commit f16edb6
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions prtpy/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,44 @@ def lower_bound(self, sums:list, sum_of_remaining_items:float, are_sums_in_ascen



class MinimizeTheDistanceFromAvg(Objective):
def value_to_minimize(self, sums:list, are_sums_in_ascending_order:bool=False)->float:
avg = sum(sums) / len(sums)
diff_from_avg = 0
for s in sums:
if (s > avg):
diff_from_avg = diff_from_avg + (s - avg)
return diff_from_avg
def __str__(self) -> str:
return "minimize-the-distance-from-avg"
def lower_bound(self, sums:list, sum_of_remaining_items:float, are_sums_in_ascending_order:bool=False)->float:
"""
>>> MinimizeDistAvg.lower_bound([10,20,30,40,50], sum_of_remaining_items=5)
28.0
>>> MinimizeDistAvg.lower_bound([10,20,30,40,50], sum_of_remaining_items=20)
22.0
>>> MinimizeDistAvg.lower_bound([10,20,30,40,50], sum_of_remaining_items=45)
12.0
>>> MinimizeDistAvg.lower_bound([10,20,30,40,50], sum_of_remaining_items=200)
0.0
>>> MinimizeDistAvg.lower_bound([0,0,0,0,0], sum_of_remaining_items=54)
0.8
"""
remaining = sum_of_remaining_items
avg = (sum(sums) + remaining) / len(sums)
diff_from_avg = 0
for s in sums:
if (s < avg and remaining > 0):
remaining = remaining - min(remaining, int(avg - s))
if(s > avg):
diff_from_avg = diff_from_avg + (s - avg)
return diff_from_avg + ((remaining % len(sums)) / len(sums))

MinimizeDistAvg = MinimizeTheDistanceFromAvg()



if __name__ == "__main__":
import doctest

Expand Down

0 comments on commit f16edb6

Please sign in to comment.