-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
170 lines (123 loc) · 5.8 KB
/
util.py
File metadata and controls
170 lines (123 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import math
import json
import typing
import numpy
def distance2D(p1, p2):
d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])
return math.sqrt(d)
def weighted_int_distribution(available, quantity):
total_population = sum(available)
quantity = min(quantity, total_population)
adjusted_quantities = [0 for x in available]
for x in range(len(available)):
if available[x] == 0:
adjusted_quantities[x] = 0
else:
adjusted_quantities[x] = quantity / (total_population / available[x])
int_adjusted_quantities = [int(x) for x in adjusted_quantities]
reduced_quantities = [x - y for (x,y) in zip(available, int_adjusted_quantities)]
sum_adjusted = sum(int_adjusted_quantities)
if sum(int_adjusted_quantities) != quantity:
remaining = quantity - sum_adjusted
while remaining > 0:
largest_x = 0
largest_quant = 0
for x in range(len(reduced_quantities)):
if reduced_quantities[x] > largest_quant:
largest_x = x
largest_quant = reduced_quantities[x]
int_adjusted_quantities[largest_x] += 1
reduced_quantities[largest_x] -=1
remaining -= 1
return int_adjusted_quantities
def weighted_distribution_with_weights(available, quantity, weight_list):
total_population = sum(available)
quantity = min(quantity, total_population)
total_weight = sum(weight_list)
normalized_population = [p / total_population for p in available]
normalized_weights = [w / total_weight for w in weight_list]
quantity_weight = [w * quantity for w in normalized_weights]
weighted_available = [min(available[x], quantity_weight[x]) for x in range(len(available))]
def distribute_ints_from_weights(quantity, weight_list:list[float]):
if quantity == 0:
return numpy.zeros(len(weight_list), dtype=int)
weights_sum = sum(weight_list)
adjusted_weights = [w/weights_sum for w in weight_list]
int_quantities = [math.floor(aw*quantity) for aw in adjusted_weights]
if sum(int_quantities) == quantity:
return int_quantities
# adds remaining quantities
for x in range(quantity - sum(int_quantities)):
largest_index = 0;
largest_value = 0.0;
for i in range(len(adjusted_weights)):
if adjusted_weights[i] > largest_value:
largest_index = i;
largest_value = adjusted_weights[i];
#print(largest_index, len(int_quantities))
int_quantities[largest_index] += 1;
adjusted_weights[largest_index] -= 1.0/quantity;
return int_quantities
def distribute_ints_from_weights_with_limit(quantity, weight_list:list[float], limits:list[int]):
if quantity == 0:
return numpy.zeros(len(weight_list), dtype=int)
if quantity > sum(limits):
print("QUANTITY REQUESTED IS BIGGER THAN LIMIT SUM")
quantity = sum(limits)
weights_sum = sum(weight_list)
adjusted_weights = [w/weights_sum for w in weight_list]
int_quantities = [math.floor(aw*quantity) for aw in adjusted_weights]
int_quantities = [min(limits[i],int_quantities[i]) for i in range(len(int_quantities))]
if sum(int_quantities) == quantity:
return int_quantities
# adds remaining quantities
for x in range(quantity - sum(int_quantities)):
largest_index = 0;
largest_value = -10000.0;
for i in range(len(adjusted_weights)):
if int_quantities[i] < limits[i] and adjusted_weights[i] > largest_value:
largest_index = i;
largest_value = adjusted_weights[i];
#print(largest_index, len(int_quantities))
int_quantities[largest_index] += 1;
adjusted_weights[largest_index] -= 1.0/quantity;
return int_quantities
def weighted_int_distribution_with_weights(available, quantity, weight_list):
total_population = sum(available)
quantity = min(quantity, total_population)
total_weight = sum(weight_list)
#normalized_population = [p / total_population for p in available]
normalized_weights = [w / total_weight for w in weight_list]
quantity_weight = [int(w * quantity) for w in normalized_weights]
weighted_available = [min(available[x], quantity_weight[x]) for x in range(len(available))]
total_weighted_available = sum(weighted_available)
if total_weighted_available > 0:
weighted_available_ratio = [p / total_weighted_available for p in weighted_available]
return [int(w * quantity) for w in weighted_available_ratio]
#requests = [int(w * quantity) for w in weighted_available_ratio]
#print("QUEANTUTAEIFDFISADRTE",sum(requests), requests)
remaining_to_pick = quantity - sum(weighted_available)
reduced_available = [x - y for (x,y) in zip(available, weighted_available)]
if remaining_to_pick > 0:
while remaining_to_pick > 0:
largest_x = 0
largest_quant = 0
for x in range(len(reduced_available)):
if reduced_available[x] > largest_quant:
largest_x = x
largest_quant = reduced_available[x]
weighted_available[largest_x] += 1
reduced_available[largest_x] -=1
remaining_to_pick -= 1
return weighted_available
# never reuses an id for a given attribute
class IDGen:
stacks = {}
def __init__(self, attribute, current_id = 0):
self.attribute = attribute
if attribute not in IDGen.stacks:
IDGen.stacks[attribute] = current_id
def get_id(self) -> int:
current_id = IDGen.stacks[self.attribute]
IDGen.stacks[self.attribute] += 1
return current_id