Skip to content

Commit

Permalink
add a complete test
Browse files Browse the repository at this point in the history
remove some things to avoid parameter in sum bug
  • Loading branch information
ChouaneLouis committed May 31, 2024
1 parent 68ee8cb commit 22fd4ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
7 changes: 3 additions & 4 deletions tests/unittests/data/components_for_scenarization_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ study:

components:
- id: G
model: sd-generator
model: generator
parameters:
- name: cost
scenario-group: cost-group
type: timeseries
timeseries: gen-costs
type: constant
value: 100
- name: p_max
type: constant
value: 100
Expand Down
22 changes: 0 additions & 22 deletions tests/unittests/data/lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ library:
definition: generation
objective: expec(sum(cost * generation))

- id: sd-generator
description: A basic scenario dependent generator model
parameters:
- name: cost
time-dependent: true
scenario-dependent: true
- name: p_max
time-dependent: false
scenario-dependent: false
variables:
- name: generation
lower-bound: 0
upper-bound: p_max
ports:
- name: injection_port
type: flow
port-field-definitions:
- port: injection_port
field: flow
definition: generation
objective: expec(sum(cost * generation))

- id: node
description: A basic balancing node model
ports:
Expand Down
47 changes: 37 additions & 10 deletions tests/unittests/test_scenario_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
import pandas as pd
import pytest

from andromede.model.parsing import parse_yaml_library
from andromede.model.resolve_library import resolve_library
from andromede.simulation import OutputValues, TimeBlock, build_problem
from andromede.study import DataBase
from andromede.study.data import ComponentParameterIndex
from andromede.study.parsing import parse_yaml_components
from andromede.study.resolve_components import build_scenarized_data_base
from andromede.study.resolve_components import (
build_network,
build_scenarized_data_base,
consistency_check,
resolve_components_and_cnx,
)


@pytest.fixture
Expand All @@ -41,23 +49,42 @@ def database(data_dir: Path) -> DataBase:
)
builder = builder.reset_index()

study_path = data_dir / "components_for_scenarization_test.yml"
components_path = data_dir / "components_for_scenarization_test.yml"
ts_path = data_dir
with study_path.open() as components:
with components_path.open() as components:
return build_scenarized_data_base(
parse_yaml_components(components), builder, ts_path
)


def test_scenarized_data_base(database):
cost_index = ComponentParameterIndex("G", "cost")
assert database.get_value(cost_index, 0, 0) == 100
assert database.get_value(cost_index, 0, 1) == 100
assert database.get_value(cost_index, 0, 2) == 200
assert database.get_value(cost_index, 0, 3) == 200

# cost-group group isnt use in following test because sum can't take time dependant parameters
def test_scenarized_data_base(database: DataBase) -> None:
load_index = ComponentParameterIndex("D", "demand")
assert database.get_value(load_index, 0, 0) == 50
assert database.get_value(load_index, 0, 1) == 100
assert database.get_value(load_index, 0, 2) == 50
assert database.get_value(load_index, 0, 3) == 100


def test_solving(data_dir: Path, database: DataBase) -> None:
library_path = data_dir / "lib.yml"
with library_path.open("r") as file:
yaml_lib = parse_yaml_library(file)
models = resolve_library(yaml_lib)

components_path = data_dir / "components_for_scenarization_test.yml"
with components_path.open("r") as file:
yaml_comp = parse_yaml_components(file)
components = resolve_components_and_cnx(yaml_comp, models)

consistency_check(components.components, models.models)
network = build_network(components)

timeblock = TimeBlock(1, list(range(2)))
problem = build_problem(network, database, timeblock, 3)

status = problem.solver.Solve()
cost = problem.solver.Objective().Value()

assert status == 0
assert cost == pytest.approx(40000 / 3, abs=0.001)

0 comments on commit 22fd4ec

Please sign in to comment.