def dummy_organization(scope="function"): c1 = BaseTeam("c1") w11 = BaseWorker("w11", cost_per_time=10.0) w12 = BaseWorker("w12", cost_per_time=5.0) w11.start_time_list = [0, 5] w11.finish_time_list = [2, 8] w12.start_time_list = [9] w12.finish_time_list = [11] c1.worker_list = [w11, w12] c2 = BaseTeam("c2") w2 = BaseWorker("w2", cost_per_time=5.0) w2.start_time_list = [9] w2.finish_time_list = [11] c2.worker_list = [w2] c2.parent_team = c1 f = BaseFacility("f", cost_per_time=20.0) f.start_time_list = [9] f.finish_time_list = [11] factory = BaseFactory("factory", facility_list=[f]) dummy_factory = BaseFactory("dummy") factory.parent_factory = dummy_factory organization = BaseOrganization(team_list=[c1, c2], factory_list=[factory, dummy_factory]) return organization
def test_init(): factory = BaseFactory("factory") assert factory.name == "factory" assert len(factory.ID) > 0 assert factory.facility_list == [] assert factory.targeted_task_list == [] assert factory.parent_factory is None assert factory.max_space_size == 1.0 assert factory.cost_list == [] factory.cost_list.append(1) assert factory.cost_list == [1.0] w1 = BaseFacility("w1") t1 = BaseTask("task1") factory1 = BaseFactory( "factory1", parent_factory=factory, targeted_task_list=[t1], facility_list=[w1], max_space_size=2.0, cost_list=[10], placed_component_list=[BaseComponent("c")], placed_component_id_record=["xxxx"], ) assert factory1.facility_list == [w1] assert factory1.targeted_task_list == [t1] assert factory1.parent_factory == factory assert factory1.max_space_size == 2.0 assert factory1.cost_list == [10] assert factory1.placed_component_list[0].name == "c" assert factory1.placed_component_id_record == ["xxxx"]
def test_create_data_for_cost_history_plotly(): factory = BaseFactory("factory") w1 = BaseFacility("w1", cost_per_time=10.0) w1.cost_list = [0, 0, 10, 10, 0, 10] w2 = BaseFacility("w2", cost_per_time=5.0) w2.cost_list = [5, 5, 0, 0, 5, 5] factory.facility_list = [w1, w2] factory.cost_list = list(map(sum, zip(w1.cost_list, w2.cost_list))) init_datetime = datetime.datetime(2020, 4, 1, 8, 0, 0) timedelta = datetime.timedelta(days=1) data = factory.create_data_for_cost_history_plotly(init_datetime, timedelta) x = [(init_datetime + time * timedelta).strftime("%Y-%m-%d %H:%M:%S") for time in range(len(factory.cost_list))] # w1 assert data[0].name == w1.name assert data[0].x == tuple(x) assert data[0].y == tuple(w1.cost_list) # w2 assert data[1].name == w2.name assert data[1].x == tuple(x) assert data[1].y == tuple(w2.cost_list)
def test_extend_targeted_task_list(): factory = BaseFactory("factory") task1 = BaseTask("task1") task2 = BaseTask("task2") factory.extend_targeted_task_list([task1, task2]) assert factory.targeted_task_list == [task1, task2] assert task1.allocated_factory_list == [factory] assert task2.allocated_factory_list == [factory]
def test_check_removing_placed_factory(): c1 = BaseComponent("c1") task1 = BaseTask("task1") c1.append_targeted_task(task1) c2 = BaseComponent("c2") task2 = BaseTask("task2") c2.append_targeted_task(task2) product = BaseProduct([c1, c2]) f1 = BaseFactory("f1") f2 = BaseFactory("f2") c1.placed_factory = f1 c2.placed_factory = f2 f1.set_placed_component(c1) f2.set_placed_component(c2) # case1 task1.state = BaseTaskState.WORKING task2.state = BaseTaskState.FINISHED product.check_removing_placed_factory() assert c1.placed_factory.name == "f1" assert c2.placed_factory is None # case2 task1.state = BaseTaskState.FINISHED task2.state = BaseTaskState.FINISHED c1.append_child_component(c2) c2.placed_factory = BaseFactory("f2") product.check_removing_placed_factory() assert c1.placed_factory is None assert c2.placed_factory is None
def test_can_put(): c1 = BaseComponent("c1", space_size=2.0) c2 = BaseComponent("c2", space_size=2.0) factory = BaseFactory("f", max_space_size=1.0) assert factory.can_put(c1) is False assert factory.can_put(c2) is False factory.max_space_size = 3.0 assert factory.can_put(c1) is True assert factory.can_put(c2) is True factory.set_placed_component(c1) assert factory.can_put(c2) is False factory.max_space_size = 4.0 assert factory.can_put(c2) is True
def test_init(): c1 = BaseComponent("c1") assert c1.name == "c1" assert len(c1.ID) > 0 c2 = BaseComponent("c2") task = BaseTask("task") c = BaseComponent( "c", ID="xx88xx", child_component_list=[c1], parent_component_list=[c2], targeted_task_list=[task], space_size=2.0, placed_factory=BaseFactory("t"), placed_factory_id_record=["fff"], ) assert c.name == "c" assert c.ID == "xx88xx" assert c.child_component_list == [c1] assert c.parent_component_list == [c2] assert c.targeted_task_list == [task] assert c.space_size == 2.0 assert c.placed_factory.name == "t" assert c.placed_factory_id_record == ["fff"]
def test_create_gantt_plotly(): factory = BaseFactory("factory") w1 = BaseFacility("w1", cost_per_time=10.0) w1.start_time_list = [0, 5] w1.finish_time_list = [2, 8] w2 = BaseFacility("w2", cost_per_time=5.0) w2.start_time_list = [9] w2.finish_time_list = [11] factory.facility_list = [w1, w2] init_datetime = datetime.datetime(2020, 4, 1, 8, 0, 0) timedelta = datetime.timedelta(days=1) factory.create_gantt_plotly(init_datetime, timedelta, save_fig_path="test.png") if os.path.exists("test.png"): os.remove("test.png")
def test_remove_placed_component(): c = BaseComponent("c") factory = BaseFactory("factory") factory.set_placed_component(c) assert factory.placed_component_list == [c] factory.remove_placed_component(c) assert factory.placed_component_list == []
def test_create_data_for_gantt_plotly(): factory = BaseFactory("factory") w1 = BaseFacility("w1", cost_per_time=10.0) w1.start_time_list = [0, 5] w1.finish_time_list = [2, 8] w2 = BaseFacility("w2", cost_per_time=5.0) w2.start_time_list = [9] w2.finish_time_list = [11] factory.facility_list = [w1, w2] init_datetime = datetime.datetime(2020, 4, 1, 8, 0, 0) timedelta = datetime.timedelta(days=1) df = factory.create_data_for_gantt_plotly(init_datetime, timedelta) # w1 part1 assert df[0]["Task"] == factory.name + ": " + w1.name assert df[0]["Start"] == ( init_datetime + w1.start_time_list[0] * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[0]["Finish"] == (init_datetime + (w1.finish_time_list[0] + 1.0) * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[0]["Type"] == "Facility" # w1 part2 assert df[1]["Task"] == factory.name + ": " + w1.name assert df[1]["Start"] == ( init_datetime + w1.start_time_list[1] * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[1]["Finish"] == (init_datetime + (w1.finish_time_list[1] + 1.0) * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[1]["Type"] == "Facility" # w2 assert df[2]["Start"] == ( init_datetime + w2.start_time_list[0] * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[2]["Finish"] == (init_datetime + (w2.finish_time_list[0] + 1.0) * timedelta).strftime("%Y-%m-%d %H:%M:%S") assert df[2]["Type"] == "Facility"
def test_initialize(): team = BaseFactory("team") w = BaseFacility("w1", factory_id=team.ID) w.state = BaseFacilityState.WORKING w.cost_list = [9.0, 7.2] w.start_time_list = [0] w.finish_time_list = [1] w.assigned_task_list = [BaseTask("task")] w.initialize() assert w.state == BaseFacilityState.FREE assert w.cost_list == [] assert w.start_time_list == [] assert w.finish_time_list == [] assert w.assigned_task_list == []
def test_create_cost_history_plotly(): factory = BaseFactory("factory") w1 = BaseFacility("w1", cost_per_time=10.0) w1.cost_list = [0, 0, 10, 10, 0, 10] w2 = BaseFacility("w2", cost_per_time=5.0) w2.cost_list = [5, 5, 0, 0, 5, 5] factory.facility_list = [w1, w2] factory.cost_list = list(map(sum, zip(w1.cost_list, w2.cost_list))) init_datetime = datetime.datetime(2020, 4, 1, 8, 0, 0) timedelta = datetime.timedelta(days=1) factory.create_cost_history_plotly(init_datetime, timedelta) factory.create_cost_history_plotly(init_datetime, timedelta, title="bbbbbbb", save_fig_path="test.png") if os.path.exists("test.png"): os.remove("test.png")
def test_add_labor_cost(): factory = BaseFactory("factory") w1 = BaseFacility("w1", cost_per_time=10.0) w2 = BaseFacility("w2", cost_per_time=5.0) factory.facility_list = [w2, w1] w1.state = BaseFacilityState.WORKING w2.state = BaseFacilityState.FREE factory.add_labor_cost() assert w1.cost_list == [10.0] assert w2.cost_list == [0.0] assert factory.cost_list == [10.0] factory.add_labor_cost(only_working=False) assert factory.cost_list == [10.0, 15.0] assert w1.cost_list == [10.0, 10.0] assert w2.cost_list == [0.0, 5.0]
def test_set_placed_factory(): c = BaseComponent("c") c1 = BaseComponent("c1") c2 = BaseComponent("c2") c.append_child_component(c1) c1.append_child_component(c2) factory = BaseFactory("factory") c.set_placed_factory(factory, set_to_all_children=False) assert c.placed_factory == factory assert c1.placed_factory is None assert c2.placed_factory is None c.set_placed_factory(factory, set_to_all_children=True) assert c.placed_factory == factory assert c1.placed_factory == factory assert c2.placed_factory == factory
def test_initialize(): factory = BaseFactory("factory") factory.cost_list = [9.0, 7.2] w = BaseFacility("w1") factory.facility_list = [w] w.state = BaseFacilityState.WORKING w.cost_list = [9.0, 7.2] w.start_time_list = [0] w.finish_time_list = [1] w.assigned_task_list = [BaseTask("task")] factory.initialize() assert factory.cost_list == [] assert w.state == BaseFacilityState.FREE assert w.cost_list == [] assert w.start_time_list == [] assert w.finish_time_list == [] assert w.assigned_task_list == []
def test_str(): print(BaseFactory("aaaaaaaa"))
def dummy_project2(scope="function"): # BaseComponents in BaseProduct c3 = BaseComponent("c3") c1 = BaseComponent("c1") c2 = BaseComponent("c2") c3.extend_child_component_list([c1, c2]) # BaseTasks in BaseWorkflow task1_1 = BaseTask("task1_1", need_facility=True) task1_2 = BaseTask("task1_2") task2_1 = BaseTask("task2_1") task3 = BaseTask("task3", due_time=30) task3.extend_input_task_list([task1_2, task2_1]) task1_2.append_input_task(task1_1) task0 = BaseTask("auto", auto_task=True, due_time=20) c1.extend_targeted_task_list([task1_1, task1_2]) c2.append_targeted_task(task2_1) c3.append_targeted_task(task3) # Facilities in factory f1 = BaseFacility("f1") f1.workamount_skill_mean_map = { task1_1.name: 1.0, } # factory.facility_list.append(f1) # Factory in BaseOrganization factory = BaseFactory("factory", facility_list=[f1]) factory.extend_targeted_task_list([task1_1, task1_2, task2_1, task3]) # BaseTeams in BaseOrganization team = BaseTeam("team") team.extend_targeted_task_list([task1_1, task1_2, task2_1, task3]) # BaseResources in each BaseTeam w1 = BaseWorker("w1", team_id=team.ID, cost_per_time=10.0) w1.workamount_skill_mean_map = { task1_1.name: 1.0, task1_2.name: 1.0, task2_1.name: 0.0, task3.name: 1.0, } w1.facility_skill_map = {f1.name: 1.0} team.worker_list.append(w1) w2 = BaseWorker("w2", team_id=team.ID, cost_per_time=6.0) w2.solo_working = True w2.workamount_skill_mean_map = { task1_1.name: 1.0, task1_2.name: 0.0, task2_1.name: 1.0, task3.name: 1.0, } w2.facility_skill_map = {f1.name: 1.0} team.worker_list.append(w2) # BaseProject including BaseProduct, BaseWorkflow and Organization project = BaseProject( init_datetime=datetime.datetime(2020, 4, 1, 8, 0, 0), unit_timedelta=datetime.timedelta(days=1), product=BaseProduct([c3, c1, c2]), workflow=BaseWorkflow([task1_1, task1_2, task2_1, task3, task0]), organization=BaseOrganization(team_list=[team], factory_list=[factory]), time=10, cost_list=[10], ) project.initialize() # project.product = BaseProduct([c3, c1, c2]) # project.workflow = BaseWorkflow([task1_1, task1_2, task2_1, task3]) # project.organization = BaseOrganization(team_list=[team], factory_list=[factory]) return project
def dummy_place_check(): c3 = BaseComponent("c3", space_size=1.0) c1 = BaseComponent("c1", space_size=1.0) c2 = BaseComponent("c2", space_size=1.0) task1 = BaseTask("t1", need_facility=True) task2 = BaseTask("t2", need_facility=True) task3 = BaseTask("t3", need_facility=True) c1.append_targeted_task(task1) c2.append_targeted_task(task2) c3.append_targeted_task(task3) # Facilities in factory f1 = BaseFacility("f1") f1.solo_working = True f1.workamount_skill_mean_map = { task1.name: 1.0, task2.name: 1.0, task3.name: 1.0, } f2 = BaseFacility("f2") f2.solo_working = True f2.workamount_skill_mean_map = { task1.name: 1.0, task2.name: 1.0, task3.name: 1.0, } # Factory in BaseOrganization factory = BaseFactory("factory", facility_list=[f1, f2]) factory.extend_targeted_task_list([task1, task2, task3]) # BaseTeams in BaseOrganization team = BaseTeam("team") team.extend_targeted_task_list([task1, task2, task3]) # BaseResources in each BaseTeam w1 = BaseWorker("w1", team_id=team.ID, cost_per_time=10.0) w1.workamount_skill_mean_map = { task1.name: 1.0, task2.name: 1.0, task3.name: 1.0, } w1.facility_skill_map = {f1.name: 1.0} team.worker_list.append(w1) w2 = BaseWorker("w2", team_id=team.ID, cost_per_time=6.0) w2.workamount_skill_mean_map = { task1.name: 1.0, task2.name: 1.0, task3.name: 1.0, } w2.facility_skill_map = {f2.name: 1.0} team.worker_list.append(w2) # BaseProject including BaseProduct, BaseWorkflow and Organization project = BaseProject( init_datetime=datetime.datetime(2020, 4, 1, 8, 0, 0), unit_timedelta=datetime.timedelta(days=1), product=BaseProduct([c1, c2, c3]), workflow=BaseWorkflow([task1, task2, task3]), organization=BaseOrganization(team_list=[team], factory_list=[factory]), ) return project
def test_set_parent_factory(): factory = BaseFactory("factory") factory.set_parent_factory(BaseFactory("xxx")) assert factory.parent_factory.name == "xxx"
def test_add_facility(): factory = BaseFactory("factory") facility = BaseFacility("facility") factory.add_facility(facility) assert len(factory.facility_list) == 1 assert facility.factory_id == factory.ID
def dummy_project(scope="function"): # Components in Product c3 = Component("c3") c1 = Component("c1") c2 = Component("c2") c3.extend_child_component_list([c1, c2]) # Tasks in Workflow task1_1 = Task("task1_1", need_facility=True) task1_2 = Task("task1_2") task2_1 = Task("task2_1") task3 = Task("task3") task3.extend_input_task_list([task1_2, task2_1]) task1_2.append_input_task(task1_1) c1.extend_targeted_task_list([task1_1, task1_2]) c2.append_targeted_task(task2_1) c3.append_targeted_task(task3) # Facilities in factory f1 = BaseFacility("f1") f1.workamount_skill_mean_map = { task1_1.name: 1.0, } # factory.facility_list.append(f1) # Factory in BaseOrganization factory = BaseFactory("factory", facility_list=[f1]) factory.extend_targeted_task_list([task1_1, task1_2, task2_1, task3]) # Teams in Organization team = Team("team") team.extend_targeted_task_list([task1_1, task1_2, task2_1, task3]) # Workers in each Team w1 = Worker("w1", team_id=team.ID, cost_per_time=10.0) w1.workamount_skill_mean_map = { task1_1.name: 1.0, task1_2.name: 1.0, task2_1.name: 0.0, task3.name: 1.0, } w1.facility_skill_map = {f1.name: 1.0} team.worker_list.append(w1) w2 = Worker("w2", team_id=team.ID, cost_per_time=6.0) w2.workamount_skill_mean_map = { task1_1.name: 1.0, task1_2.name: 0.0, task2_1.name: 1.0, task3.name: 1.0, } w2.facility_skill_map = {f1.name: 1.0} team.worker_list.append(w2) # Project including Product, Workflow and Organization project = Project( init_datetime=datetime.datetime(2020, 4, 1, 8, 0, 0), unit_timedelta=datetime.timedelta(days=1), ) project.product = Product([c3, c1, c2]) project.workflow = Workflow([task1_1, task1_2, task2_1, task3]) project.organization = Organization(team_list=[team], factory_list=[factory]) return project