Beispiel #1
0
def test_two_scenarios(simple_linear_model, ):
    """Basic test of Scenario functionality"""
    model = simple_linear_model  # Convenience renaming

    scenario_input = Scenario(model, 'Inflow', size=2)
    model.nodes["Input"].max_flow = ConstantScenarioParameter(
        scenario_input, [5.0, 10.0])

    scenario_outflow = Scenario(model, 'Outflow', size=2)
    model.nodes["Output"].max_flow = ConstantScenarioParameter(
        scenario_outflow, [3.0, 8.0])
    model.nodes["Output"].cost = -2.0

    # add numpy recorders to input and output nodes
    NumpyArrayNodeRecorder(model, model.nodes["Input"], "input")
    NumpyArrayNodeRecorder(model, model.nodes["Output"], "output")

    expected_node_results = {
        "Input": [3.0, 5.0, 3.0, 8.0],
        "Link": [3.0, 5.0, 3.0, 8.0],
        "Output": [3.0, 5.0, 3.0, 8.0],
    }

    assert_model(model, expected_node_results)

    model.run()

    # combine recorder outputs to a single dataframe
    df = model.to_dataframe()
    assert (df.shape == (365, 2 * 2 * 2))
    assert_allclose(df["input", 0, 0].iloc[0], 3.0)
    assert_allclose(df["input", 0, 1].iloc[0], 5.0)
    assert_allclose(df["input", 1, 0].iloc[0], 3.0)
    assert_allclose(df["input", 1, 1].iloc[0], 8.0)
Beispiel #2
0
def test_scenario(simple_linear_model, ):
    """Basic test of Scenario functionality"""
    model = simple_linear_model  # Convenience renaming

    scenario = Scenario(model, 'Inflow', size=2)
    model.nodes["Input"].max_flow = ConstantScenarioParameter(model, scenario, [5.0, 10.0])

    model.nodes["Output"].max_flow = 5.0
    model.nodes["Output"].cost = -2.0

    expected_node_results = {
        "Input": [5.0, 5.0],
        "Link": [5.0, 5.0],
        "Output": [5.0, 5.0],
    }

    assert_model(model, expected_node_results)
Beispiel #3
0
def test_linear_model(simple_linear_model, in_flow, out_flow, benefit):
    """
    Test the simple_linear_model with different basic input and output values
    """

    simple_linear_model.node["Input"].max_flow = in_flow
    simple_linear_model.node["Output"].min_flow = out_flow
    simple_linear_model.node["Output"].cost = -benefit

    expected_sent = in_flow if benefit > 1.0 else out_flow

    expected_node_results = {
        "Input": expected_sent,
        "Link": expected_sent,
        "Output": expected_sent,
    }
    assert_model(simple_linear_model, expected_node_results)
Beispiel #4
0
def test_linear_model(simple_linear_model, in_flow, out_flow, benefit):
    """
    Test the simple_linear_model with different basic input and output values
    """

    simple_linear_model.nodes["Input"].max_flow = in_flow
    simple_linear_model.nodes["Output"].min_flow = out_flow
    simple_linear_model.nodes["Output"].cost = -benefit

    expected_sent = in_flow if benefit > 1.0 else out_flow

    expected_node_results = {
        "Input": expected_sent,
        "Link": expected_sent,
        "Output": expected_sent,
    }
    assert_model(simple_linear_model, expected_node_results)
Beispiel #5
0
def test_scenario_two_parameter(simple_linear_model, ):
    """Basic test of Scenario functionality"""
    model = simple_linear_model  # Convenience renaming

    scenario_input = pywr.core.Scenario(model, 'Inflow', size=2)
    model.node["Input"].max_flow = pywr.core.ParameterConstantScenario(scenario_input, [5.0, 10.0])

    model.node["Output"].max_flow = pywr.core.ParameterConstantScenario(scenario_input, [8.0, 3.0])
    model.node["Output"].cost = -2.0

    expected_node_results = {
        "Input": [5.0, 3.0],
        "Link": [5.0, 3.0],
        "Output": [5.0, 3.0],
    }

    assert_model(model, expected_node_results)
Beispiel #6
0
def test_two_scenarios(simple_linear_model, ):
    """Basic test of Scenario functionality"""
    model = simple_linear_model  # Convenience renaming

    scenario_input = Scenario(model, 'Inflow', size=2)
    model.nodes["Input"].max_flow = ConstantScenarioParameter(
        model, scenario_input, [5.0, 10.0])

    scenario_outflow = Scenario(model,
                                'Outflow',
                                size=2,
                                ensemble_names=['High', 'Low'])
    model.nodes["Output"].max_flow = ConstantScenarioParameter(
        model, scenario_outflow, [3.0, 8.0])
    model.nodes["Output"].cost = -2.0

    # Check ensemble names are provided in the multi-index
    index = model.scenarios.multiindex
    assert index.levels[0].name == 'Inflow'
    assert index.levels[1].name == 'Outflow'
    assert np.all(index.levels[1] == ['High', 'Low'])

    # add numpy recorders to input and output nodes
    NumpyArrayNodeRecorder(model, model.nodes["Input"], "input")
    NumpyArrayNodeRecorder(model, model.nodes["Output"], "output")

    expected_node_results = {
        "Input": [3.0, 5.0, 3.0, 8.0],
        "Link": [3.0, 5.0, 3.0, 8.0],
        "Output": [3.0, 5.0, 3.0, 8.0],
    }

    assert_model(model, expected_node_results)

    model.run()

    # combine recorder outputs to a single dataframe
    df = model.to_dataframe()
    assert (df.shape == (365, 2 * 2 * 2))
    assert_allclose(df["input", 0, 'High'].iloc[0], 3.0)
    assert_allclose(df["input", 0, 'Low'].iloc[0], 5.0)
    assert_allclose(df["input", 1, 'High'].iloc[0], 3.0)
    assert_allclose(df["input", 1, 'Low'].iloc[0], 8.0)
Beispiel #7
0
def test_simple_linear_inline_model(simple_linear_inline_model, in_flow_1, out_flow_0, link_flow):
    """
    Test the test_simple_linear_inline_model with different flow constraints
    """
    model = simple_linear_inline_model
    model.node["Input 0"].max_flow = 10.0
    model.node["Input 1"].max_flow = in_flow_1
    model.node["Link"].max_flow = link_flow
    model.node["Output 0"].max_flow = out_flow_0
    model.node["Input 1"].cost = 1.0
    model.node["Output 0"].cost = -10.0
    model.node["Output 1"].cost = -5.0

    expected_sent = min(link_flow, 10+in_flow_1)

    expected_node_results = {
        "Input 0": 10.0,
        "Input 1": max(expected_sent-10.0, 0.0),
        "Link": expected_sent,
        "Output 0": min(expected_sent, out_flow_0),
        "Output 1": max(expected_sent - out_flow_0, 0.0),
    }
    assert_model(model, expected_node_results)
Beispiel #8
0
def test_bidirectional_model(bidirectional_model):
    """
    Test the simple_linear_model with different basic input and output values
    """
    model = bidirectional_model
    model.node["Input 0"].max_flow = 10.0
    model.node["Input 1"].max_flow = 10.0
    model.node["Output 0"].max_flow = 10.0
    model.node["Output 1"].max_flow = 15.0
    model.node["Output 0"].cost = -5.0
    model.node["Output 1"].cost = -10.0
    model.node["Link 0"].cost = 1.0
    model.node["Link 1"].cost = 1.0

    expected_node_results = {
        "Input 0": 10.0,
        "Input 1": 10.0,
        "Link 0": 10.0,
        "Link 1": 15.0,
        "Output 0": 5.0,
        "Output 1": 15.0,
    }
    assert_model(model, expected_node_results)
Beispiel #9
0
def test_bidirectional_model(bidirectional_model):
    """
    Test the simple_linear_model with different basic input and output values
    """
    model = bidirectional_model
    model.nodes["Input 0"].max_flow = 10.0
    model.nodes["Input 1"].max_flow = 10.0
    model.nodes["Output 0"].max_flow = 10.0
    model.nodes["Output 1"].max_flow = 15.0
    model.nodes["Output 0"].cost = -5.0
    model.nodes["Output 1"].cost = -10.0
    model.nodes["Link 0"].cost = 1.0
    model.nodes["Link 1"].cost = 1.0

    expected_node_results = {
        "Input 0": 10.0,
        "Input 1": 10.0,
        "Link 0": 10.0,
        "Link 1": 15.0,
        "Output 0": 5.0,
        "Output 1": 15.0,
    }
    assert_model(model, expected_node_results)
Beispiel #10
0
def test_simple_linear_inline_model(simple_linear_inline_model, in_flow_1,
                                    out_flow_0, link_flow):
    """
    Test the test_simple_linear_inline_model with different flow constraints
    """
    model = simple_linear_inline_model
    model.nodes["Input 0"].max_flow = 10.0
    model.nodes["Input 1"].max_flow = in_flow_1
    model.nodes["Link"].max_flow = link_flow
    model.nodes["Output 0"].max_flow = out_flow_0
    model.nodes["Input 1"].cost = 1.0
    model.nodes["Output 0"].cost = -10.0
    model.nodes["Output 1"].cost = -5.0

    expected_sent = min(link_flow, 10 + in_flow_1)

    expected_node_results = {
        "Input 0": 10.0,
        "Input 1": max(expected_sent - 10.0, 0.0),
        "Link": expected_sent,
        "Output 0": min(expected_sent, out_flow_0),
        "Output 1": max(expected_sent - out_flow_0, 0.0),
    }
    assert_model(model, expected_node_results)
Beispiel #11
0
def test_two_cross_domain_output_single_input(
        two_cross_domain_output_single_input):
    # TODO This test currently fails because of the simple way in which the cross
    # domain paths work. It can not cope with two Outputs connected to one
    # input.
    assert_model(*two_cross_domain_output_single_input)
Beispiel #12
0
def test_two_domain_linear_model(two_domain_linear_model):
    assert_model(*two_domain_linear_model)
Beispiel #13
0
def test_piecewise_model(simple_piecewise_model):
    assert_model(*simple_piecewise_model)
Beispiel #14
0
def test_river_split_gauge(simple_river_split_gauge_model):
    assert_model(*simple_river_split_gauge_model)
Beispiel #15
0
def test_two_domain_linear_model(two_domain_linear_model):
    assert_model(*two_domain_linear_model)
Beispiel #16
0
def test_piecewise_model(simple_gauge_model):
    assert_model(*simple_gauge_model)
Beispiel #17
0
def test_linear_model_with_storage(linear_model_with_storage):
    assert_model(*linear_model_with_storage)
Beispiel #18
0
def test_linear_model_with_storage(linear_model_with_storage):
    assert_model(*linear_model_with_storage)
Beispiel #19
0
def test_two_cross_domain_output_single_input(two_cross_domain_output_single_input):
    # TODO This test currently fails because of the simple way in which the cross
    # domain paths work. It can not cope with two Outputs connected to one
    # input.
    assert_model(*two_cross_domain_output_single_input)