Skip to content

Instantly share code, notes, and snippets.

@kmadac
Last active August 29, 2015 14:03
Show Gist options
  • Save kmadac/a74a581b4d2db9cc4cc8 to your computer and use it in GitHub Desktop.
Save kmadac/a74a581b4d2db9cc4cc8 to your computer and use it in GitHub Desktop.
Migration Change process simulation
__author__ = 'kmadac'
import random
from bisect import bisect
people = 5
max_changes_per_person_intime = 5
#
#change_complete_time_days = ([(14, 3), (27, 5), (41, 3), (60, 1), (90, 1)])
# real data from migration of real cluster
change_complete_time_days = ([(6, 5), (8, 4), (10, 5), (17, 6), (29, 4), (42, 4), (62, 5)])
working_days_to_simulate = 228
frozen_zones_working_days = 70
def weighted_choice(choices):
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random.random() * total
i = bisect(cum_weights, x)
return values[i]
class change(object):
def __init__(self, complete_time):
self.comlete_time = complete_time
self.current_duration = 0
def add_day(self):
self.current_duration += 1
def replan(self):
self.current_duration = -1
def is_complete(self):
if self.comlete_time == self.current_duration:
return True
else:
return False
def plan_changes():
for i in range((people * max_changes_per_person_intime) - len(change_list)):
days_to_complete = weighted_choice(change_complete_time_days)
print("New change created for {0} days".format(days_to_complete))
changei = change(days_to_complete)
change_list.append(changei)
def delete_changes():
return [x for x in change_list if not x.is_complete()]
iterations = 1000
completed_changes_total = 0
for iters in range(iterations):
completed_changes = 0
change_list = []
for i in range(working_days_to_simulate - frozen_zones_working_days):
plan_changes()
print("==============")
for onechange in change_list:
onechange.add_day()
if onechange.is_complete():
completed_changes += 1
change_list = delete_changes()
completed_changes_total += completed_changes
print('{0} changes completed in {1} business days'.format(completed_changes_total / iterations, working_days_to_simulate))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment