Skip to content

Instantly share code, notes, and snippets.

@JonahTzuChi
Last active March 11, 2024 03:49
Show Gist options
  • Save JonahTzuChi/12b696a9a14308c81b8a94b977565242 to your computer and use it in GitHub Desktop.
Save JonahTzuChi/12b696a9a14308c81b8a94b977565242 to your computer and use it in GitHub Desktop.
Achieve even distribution in sampling method through mini sorted sampling
import random as rn
from multiprocessing.pool import Pool
from queue import Queue
import matplotlib.pyplot as plt
def sampling(n: int, minIndex: int, maxIndex: int) -> list[int]:
lst = [rn.randint(minIndex, maxIndex-1) for _ in range(n)]
return lst
def extract(main_list: list[int], index_list: list[int]) -> list[tuple[int, int]]:
lst = list()
try:
for i in index_list:
p = main_list[i]
lst.append((i, p))
return lst
except Exception as e:
print(f"{e}\n{i}", flush=True)
raise e
def preprocessing(data: list[int]) -> list[int]:
lst = list()
for i in range(len(data)):
value = data[i]
if value == 0:
continue
for _ in range(value):
lst.append(i)
return lst
def draw_histogram(data, n_bin:int, export_path: str = None):
fg, ax = plt.subplots(1, 1)
ax.hist(data, bins=50)
if export_path:
fg.savefig(export_path)
def mini_luck(C: int, G: int, T: int, N: int) -> tuple:
lst = [0 for i in range(T)]
for gen in range(G):
try:
min_sample = sampling(N, 0, T)
sub_list = extract(lst, min_sample)
sub_list.sort(key=lambda x: x[1], reverse=False)
subject = sub_list[0]
subject_index, _ = subject
lst[subject_index] += 1
except Exception as e:
print(f"[{gen}]\n")
raise e
x = preprocessing(lst)
draw_histogram(x, T, f"./mini_{C}.jpg")
SmallestTribeSize = min(lst)
LargestTribeSize = max(lst)
AverageTribeSize = sum(lst) / TRIBE_SIZE
return (SmallestTribeSize, LargestTribeSize, AverageTribeSize)
def uniform_luck(C: int, G: int, T: int, N: int = 1):
lst = [0 for i in range(T)]
for gen in range(G):
try:
subject_index = int(rn.uniform(0, 1) * (T-1))
lst[subject_index] += 1
except Exception as e:
print(f"[{gen}]\n")
raise e
x = preprocessing(lst)
draw_histogram(x, T, f"./uniform_{C}.jpg")
SmallestTribeSize = min(lst)
LargestTribeSize = max(lst)
AverageTribeSize = sum(lst) / TRIBE_SIZE
return (SmallestTribeSize, LargestTribeSize, AverageTribeSize)
if __name__ == "__main__":
N = 10
GENERATION = 10000
TRIBE_SIZE = 3000
CYCLE = 10
PROCESSES = 1
tasks = Queue()
with Pool(processes=PROCESSES) as pool:
for cycle in range(CYCLE):
task = pool.apply_async(
uniform_luck,
args=(
cycle,
GENERATION,
TRIBE_SIZE,
N
),
callback=lambda x: None,
error_callback=lambda x: print(x, flush=True)
)
tasks.put(task)
tasks.put(None)
pool.close()
pool.join()
metrics = []
while True:
task = tasks.get()
if task is None:
break
task = task.get()
s, l, a = task
metrics.append((s, l, a))
SmallestTribeSize = min(list(map(lambda x: x[0], metrics)))
LargestTribeSize = max(list(map(lambda x: x[1], metrics)))
AverageTribeSize = sum(list(map(lambda x: x[2], metrics))) / CYCLE
print(f"SmallestTribeSize: {SmallestTribeSize}")
print(f"LargestTribeSize: {LargestTribeSize}")
print(f"AverageTribeSize: {AverageTribeSize}")
@JonahTzuChi
Copy link
Author

JonahTzuChi commented Mar 11, 2024

Histogram of Mini Luck

mini_luck

@JonahTzuChi
Copy link
Author

Histogram of Uniform Luck

uniform_0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment