def huge_transportation_model(): # Define large random datasets num_nodes = 1_000 upper_lim = 100 source_nodes = {None: [f"S{x}" for x in range(num_nodes)]} dest_nodes = {None: [f"D{x}" for x in range(num_nodes)]} supply = {s: 5 for s in source_nodes[None]} demand = {d: 5 for d in dest_nodes[None]} ship_costs = {(s, d): random.randint(0, upper_lim) for s in source_nodes[None] for d in dest_nodes[None]} my_data = {"Sources": source_nodes, "Destinations": dest_nodes, "Supply": supply, "Demand": demand, "ShippingCosts": ship_costs} # Define model, load data in it model = transportation_model() inst = model.create_instance(data={None: my_data}) # Start timer and solve start_time = time.time() instance, results = solve_instance(inst) end_time = time.time() # Print termination status and solving time print(results.solver.termination_condition) print(end_time - start_time)
def test_agg_planning(): model = scheduling(prob_class="agg_planning") instance1 = model.create_instance(AGG_PLANNING_DATA) instance2 = scheduling(prob_class="agg_planning", filename=AGG_PLANNING_DATA) for inst in [instance1, instance2]: instance, results = solve_instance(inst) assert instance.OBJ() == 865_650 for v in instance.component_objects(pyo.Var, active=True): if v.name == "Produce": assert {index: v[index].value for index in v} == { 'Jan': 1350.0, 'Feb': 1400.0, 'Mar': 1000.0, 'Apr': 550.0, 'May': 1700.0, 'Jun': 2000.0 } else: assert {index: v[index].value for index in v} == { 'Jan': 250.0, 'Feb': 250.0, 'Mar': 250.0, 'Apr': 0.0, 'May': 0.0, 'Jun': 100.0 }
def test_print_sol_with_data(): instance = resource_allocation(filename=SIMPLE_RES_DATA) instance, results = solve_instance(instance) # Redirect output to StringIO object captured_output = io.StringIO() sys.stdout = captured_output print_sol(instance, money_obj=True) sys.stdout = sys.__stdout__ # reset stdout test_string = ("Objective Value: $7,343.75\n" "Variable component: NumActivity\n" " Q 31.25\n" " W 75.0\n") assert captured_output.getvalue() == test_string
def test_blending(): model = blending() instance1 = model.create_instance(BLENDING_DATA) instance2 = blending(filename=BLENDING_DATA) for inst in [instance1, instance2]: instance, results = solve_instance(inst) for v in instance.component_objects(pyo.Var, active=True): if v.name == "Blend": assert {index: round(v[index].value, 2) for index in v} == { "Banana": 0.02, "Milk": 0.76, "Yogurt": 0.22 } assert round(instance.OBJ(), 2) == 37.61
def test_simple_resource_allocation_with_data(): model = resource_allocation() instance1 = model.create_instance(SIMPLE_RES_DATA) instance2 = resource_allocation(filename=SIMPLE_RES_DATA) for inst in [instance1, instance2]: instance, results = solve_instance(inst) # Check all variable values for v in instance.component_objects(pyo.Var, active=True): if v.name == "NumActivity": assert {index: v[index].value for index in v} == { "Q": 31.25, "W": 75 } assert instance.OBJ() == 7343.75
def test_complex_resource_allocation_with_data(): instance = resource_allocation(filename=COMPLEX_RES_DATA, mult_res=True, max_activity=False) instance, results = solve_instance(instance) # Check all variable values for v in instance.component_objects(pyo.Var, active=True): if v.name == "NumActivity": assert {index: round(v[index].value, 2) for index in v} == { "Q": 123.08, "W": 0, "E": 0, "R": 0, "T": 46.15, "Y": 0 } assert round(instance.OBJ(), 0) == 2692
def test_employee_simple(): model = scheduling("employee") instance1 = model.create_instance(EMPLOYEE_DATA) instance2 = scheduling("employee", filename=EMPLOYEE_DATA) for inst in [instance1, instance2]: instance, results = solve_instance(inst) assert instance.OBJ() == 215 for v in instance.component_objects(pyo.Var, active=True): assert {index: v[index].value for index in v} == { 1: 0, 2: 65, 3: 70, 4: 30, 5: 0, 6: 45, 7: 5 }
def test_transportation_model(): model = transportation_model() instance1 = model.create_instance(TRANSPORTATION_DATA) instance2 = transportation_model(filename=TRANSPORTATION_DATA) graph = Graph() graph.add_arcs(ARC_DATA) instance_graph = graph.transportation(SUPPLY, DEMAND) for inst in [instance1, instance2, instance_graph]: instance, results = solve_instance(inst) assert instance.OBJ() == 475 for v in instance.component_objects(pyo.Var, active=True): assert {index: v[index].value for index in v} == { ('S1', 'D1'): 0.0, ('S1', 'D2'): 5.0, ('S1', 'D3'): 5.0, ('S1', 'D4'): 5.0, ('S1', 'D5'): 0.0, ('S2', 'D1'): 0.0, ('S2', 'D2'): 5.0, ('S2', 'D3'): 0.0, ('S2', 'D4'): 0.0, ('S2', 'D5'): 10.0, ('S3', 'D1'): 5.0, ('S3', 'D2'): 0.0, ('S3', 'D3'): 10.0, ('S3', 'D4'): 0.0, ('S3', 'D5'): 0.0}
def test_sensitivity_analysis(): instance = resource_allocation(filename=COMPLEX_RES_DATA, mult_res=True, max_activity=False) instance, results = solve_instance(instance) sens_analysis_df = sensitivity_analysis(instance) test_df = pd.DataFrame( { "Dual Value": [9.29, 0.00, 5.22, 0.00], "Lower": [None, None, None, None], "Upper": [200.0, 280.0, 160.0, 320.0], "Slack": [0, 113.85, 0, 107.69], "Active": [True, False, True, False] }, index=[ "ResourceConstraint[A]", "ResourceConstraint[B]", "ResourceConstraint[C]", "ResourceConstraint[D]" ]) sens_analysis_df["Dual Value"] = \ sens_analysis_df["Dual Value"].round(decimals=2) sens_analysis_df["Slack"] = \ sens_analysis_df["Slack"].round(decimals=2) assert sens_analysis_df.equals(test_df)
def test_rental_simple(): model = scheduling(prob_class="rental") instance1 = model.create_instance(RENTAL_DATA) instance2 = scheduling(prob_class="rental", filename=RENTAL_DATA) for inst in [instance1, instance2]: instance, results = solve_instance(inst) assert instance.OBJ() == 1830.0 for v in instance.component_objects(pyo.Var, active=True): assert {index: v[index].value for index in v} == { ('Mon', 'DailyWeekDay'): 1.0, ('Tue', 'DailyWeekDay'): 0.0, ('Wed', 'DailyWeekDay'): 3.0, ('Thu', 'DailyWeekDay'): 0.0, ('Fri', 'DailyWeekDay'): 2.0, ('Sat', 'DailyWeekend'): 0.0, ('Sun', 'DailyWeekend'): 0.0, ('Mon', 'ThreeWeekDay'): 1.0, ('Tue', 'ThreeWeekDay'): 0.0, ('Wed', 'ThreeWeekDay'): 0.0, ('Sat', 'Weekend'): 0.0, ('Mon', 'AllWeekDay'): 4.0, ('Sat', 'AllWeek'): 4.0 }