def decision(self): """ This is the tock method for decision the institution. Here the institution determines the difference in supply and capacity and makes the the decision to deploy facilities or not. """ time = self.context.time for commod, proto_dict in self.commodity_dict.items(): diff, capacity, supply = self.calc_diff(commod, time) lib.record_time_series('calc_supply' + commod, self, supply) lib.record_time_series('calc_capacity' + commod, self, capacity) if diff < 0: if self.installed_cap: deploy_dict, self.commodity_dict = solver.deploy_solver( self.installed_capacity, self.commodity_dict, commod, diff, time) else: deploy_dict, self.commodity_dict = solver.deploy_solver( self.commodity_supply, self.commodity_dict, commod, diff, time) for proto, num in deploy_dict.items(): for i in range(num): self.context.schedule_build(self, proto) # update installed capacity dict self.installed_capacity[commod][time + 1] = \ self.installed_capacity[commod][time] for proto, num in deploy_dict.items(): self.installed_capacity[commod][time + 1] += \ self.commodity_dict[commod][proto]['cap'] * num else: self.installed_capacity[commod][ time + 1] = self.installed_capacity[commod][time] os_limit = self.commod_mins[commod] * self.os_int if diff > os_limit: self.commod_os[commod] += 1 else: self.commod_os[commod] = 0 if diff > os_limit and self.commod_os[commod] > self.os_time: solver.decommission_oldest(self, self.commodity_dict[commod], diff, commod, time) if self.record: out_text = "Time " + str(time) + \ " Deployed " + str(len(self.children)) out_text += " capacity " + \ str(self.commodity_capacity[commod][time]) out_text += " supply " + \ str(self.commodity_supply[commod][time]) + "\n" with open(commod + ".txt", 'a') as f: f.write(out_text) for child in self.children: if child.exit_time == time: itscommod = self.fac_commod[child.prototype] self.installed_capacity[itscommod][ time + 1] -= self.commodity_dict[itscommod][ child.prototype]['cap']
def test_min_deploy_solver(): """ Tests if the minimize_number_of_deployment function works correctly by deploying the smallest number of facilities while meeting diff""" for i in range(100): diff = -1.0 * random.uniform(0.01, 30.0) commod = {} for i in range(4): commod.update({str(i): {'cap': random.uniform(0.1, 9.9), 'pref': '0', 'constraint_commod': '0', 'constraint': 0, 'share': 0}}) deploy_dict, commodity_dict = solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=1) # actually deploy and see if it's good cap_list = [v['cap'] for k, v in commod.items()] final_diff = diff for key, val in deploy_dict.items(): final_diff += val * commod[key]['cap'] if final_diff > min(cap_list): raise ValueError( 'The difference after deployment exceeds ' + 'the capacity of the smallest deployable prototype') # if it didn't raise valueerror, we are good assert(True)
def test_pref_solver_eq(): """ Tests if the preference_deploy function works correctly when the preference values are given as an equation """ diff = -10 commod = {'1': {'cap': 2, 'pref': '1*t', 'constraint_commod': '0', 'constraint': 0, 'share': 0}, '2': {'cap': 4, 'pref': '10 - (1*t)', 'constraint_commod': '0', 'constraint': 0, 'share': 0} } for t in range(10): deploy_dict, commodity_dict = solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=t) if t < 5: assert(deploy_dict['2'] == 3) if t == 5: assert(deploy_dict['2'] == 2) assert(deploy_dict['1'] == 1) if t > 5: assert(deploy_dict['1'] == 5) assert(True)
def test_pref_solver_const(): """ Tests if the preference_deploy function works correctly for constant preference values by deploying only the most preferred prototype to meet diff """ for i in range(100): diff = -1.0 * random.uniform(0.01, 30.0) commod = {} for i in range(3): commod.update({str(i): {'cap': random.uniform(0.1, 9.9), 'pref': str(random.uniform(0.1, 9.9)), 'constraint_commod': '0', 'constraint': 0, 'share': 0}}) deploy_dict, commodity_dict = solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=1) # check if only one prototype is deployed assert (len(deploy_dict.keys()) == 1) # get highest preference value prototype highest_pref = max([v['pref'] for k, v in commod.items()]) most_preferred_proto = [ k for k, v in commod.items() if v['pref'] == highest_pref][0] # check if the deployed proto is the most preferred prototype for proto, deploy_num in deploy_dict.items(): assert (proto == most_preferred_proto) # check if deployed capacity is the right value deployed_cap = deploy_num * commod[proto]['cap'] if deployed_cap < diff: raise ValueError('Underdeploys') elif (deployed_cap + diff) > commod[proto]['cap']: raise ValueError('Overdeploys') assert(True)
def test_some_pref_negative_and_share(): """ Tests if the deploy_solver function works correctly when one preference is negative but the prototype has sharing percentages defines """ diff = -10.0 t = random.uniform(1.0, 10.0) commod = {'1': {'cap': 2, 'pref': '-1', 'constraint_commod': '0', 'constraint': 0, 'share': 20}, '2': {'cap': 4, 'pref': 't', 'constraint_commod': '0', 'constraint': 0, 'share': 80} } deploy_dict, commodity_dict = \ solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=t) print(deploy_dict) if bool(deploy_dict): if '1' in deploy_dict.keys(): raise ValueError('wrong deployment') if deploy_dict['2'] != 3: raise ValueError('wrong deployment') else: raise ValueError('wrong deployment') assert(True)
def test_equal_pref_then_share(): """ Tests if the deploy_solver function works correctly when the preferences are different and lower than 0 and the share percentages are different to 0 """ diff = -10.0 t = random.uniform(1.0, 10.0) commod = {'1': {'cap': 2, 'pref': '1', 'constraint_commod': '0', 'constraint': 0, 'share': 20}, '2': {'cap': 4, 'pref': '1', 'constraint_commod': '0', 'constraint': 0, 'share': 80} } deploy_dict, commodity_dict = \ solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=t) if bool(deploy_dict): if deploy_dict['1'] != 1: raise ValueError('wrong deployment') if deploy_dict['2'] != 2: raise ValueError('wrong deployment') else: raise ValueError('wrong deployment') assert(True)
def test_pref_positive_over_share(): """ Tests if the deploy_solver function works correctly when the preferences are different and greater than 0 and the share percentages are different to 0 """ diff = -1.0 * random.uniform(1.0, 10.0) t = random.uniform(1.0, 10.0) commod = {'1': {'cap': 2, 'pref': '2*t', 'constraint_commod': '0', 'constraint': 0, 'share': 10}, '2': {'cap': 4, 'pref': '1', 'constraint_commod': '0', 'constraint': 0, 'share': 90} } deploy_dict, commodity_dict = \ solver.deploy_solver(commodity_supply={}, commodity_dict={'commod': commod}, commod='commod', diff=diff, time=t) for key in deploy_dict.keys(): if key != '1': raise ValueError('wrong deployment') assert(True)
def decision(self): """ This is the tock method for decision the institution. Here the institution determines the difference in supply and demand and makes the the decision to deploy facilities or not. """ time = self.context.time for commod, proto_dict in self.commodity_dict.items(): diff, supply, demand = self.calc_diff(commod, time) lib.record_time_series('calc_supply' + commod, self, supply) lib.record_time_series('calc_demand' + commod, self, demand) if diff < 0: deploy_dict = solver.deploy_solver(self.commodity_supply, self.commodity_dict, commod, diff, time) for proto, num in deploy_dict.items(): for i in range(num): self.context.schedule_build(self, proto) if self.record: out_text = "Time " + str(time) + \ " Deployed " + str(len(self.children)) out_text += " supply " + \ str(self.commodity_supply[commod][time]) out_text += " demand " + \ str(self.commodity_demand[commod][time]) + "\n" with open(commod + ".txt", 'a') as f: f.write(out_text)