Skip to content

Commit

Permalink
Remove functions from second test and filter ts on scenarios and time…
Browse files Browse the repository at this point in the history
…steps
  • Loading branch information
Juliette-Gerbaux committed Jun 28, 2024
1 parent 5b5c2b0 commit 412e62c
Show file tree
Hide file tree
Showing 10 changed files with 18,571 additions and 389 deletions.
15 changes: 14 additions & 1 deletion src/andromede/study/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional
from typing import Dict, Optional, List

import pandas as pd

Expand Down Expand Up @@ -116,6 +116,19 @@ def load_ts_from_txt(
raise Exception(f"An error has arrived when processing '{ts_path}'")


def filter_ts_on_scenarios_and_timesteps(
raw_ts: pd.DataFrame,
scenarios: Optional[List[int]],
timesteps: Optional[List[int]],
) -> pd.DataFrame:
filtered_ts = raw_ts
if scenarios is not None:
filtered_ts = filtered_ts.filter(items=scenarios, axis=1)
if timesteps is not None:
filtered_ts = filtered_ts.filter(items=timesteps, axis=0)
return filtered_ts.reset_index(drop=True)


@dataclass(frozen=True)
class TimeScenarioSeriesData(AbstractDataStructure):
"""
Expand Down
21 changes: 18 additions & 3 deletions src/andromede/study/resolve_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
TimeScenarioIndex,
TimeScenarioSeriesData,
load_ts_from_txt,
filter_ts_on_scenarios_and_timesteps,
)
from andromede.study.parsing import (
InputComponent,
Expand Down Expand Up @@ -142,13 +143,21 @@ def build_network(comp_network: NetworkComponents) -> Network:


def build_data_base(
input_comp: InputComponents, timeseries_dir: Optional[Path]
input_comp: InputComponents,
timeseries_dir: Optional[Path],
scenarios: Optional[List[int]] = None,
timesteps: Optional[List[int]] = None,
) -> DataBase:
database = DataBase()
for comp in input_comp.components:
for param in comp.parameters or []:
param_value = _evaluate_param_type(
param.type, param.value, param.timeseries, timeseries_dir
param.type,
param.value,
param.timeseries,
timeseries_dir,
scenarios,
timesteps,
)
database.add_data(comp.id, param.name, param_value)

Expand All @@ -160,11 +169,17 @@ def _evaluate_param_type(
param_value: Optional[float],
timeseries_name: Optional[str],
timeseries_dir: Optional[Path],
scenarios: Optional[List[int]],
timesteps: Optional[List[int]],
) -> AbstractDataStructure:
if param_type == "constant" and param_value is not None:
return ConstantData(float(param_value))

elif param_type == "timeseries":
return TimeScenarioSeriesData(load_ts_from_txt(timeseries_name, timeseries_dir))
return TimeScenarioSeriesData(
filter_ts_on_scenarios_and_timesteps(
load_ts_from_txt(timeseries_name, timeseries_dir), scenarios, timesteps
)
)

raise ValueError(f"Data should be either constant or timeseries ")
29 changes: 24 additions & 5 deletions src/andromede/thermal_heuristic/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pandas as pd
import numpy as np
import ortools.linear_solver.pywraplp as pywraplp
from typing import List, Dict
from typing import List, Dict, Optional

from andromede.libs.standard import (
BALANCE_PORT_TYPE,
Expand Down Expand Up @@ -88,7 +88,13 @@ def create_main_problem(
],
)

network, database = get_network_and_database(data_dir, lib, "components.yml")
network, database = get_network_and_database(
data_dir,
lib,
"components.yml",
scenarios=[scenario],
timesteps=list(range(week * number_hours, (week + 1) * number_hours)),
)

modify_parameters_of_cluster(
lower_bound,
Expand All @@ -110,6 +116,7 @@ def create_main_problem(
time_block,
scenarios,
border_management=BlockBorderManagement.CYCLE,
solver_id="XPRESS",
)

return problem
Expand Down Expand Up @@ -177,7 +184,11 @@ def get_cluster_id(network: Network, cluster_model_id: str) -> list[str]:


def get_network_and_database(
data_dir: Path, lib: Library, yml_file: str
data_dir: Path,
lib: Library,
yml_file: str,
scenarios: Optional[List[int]],
timesteps: Optional[List[int]],
) -> tuple[Network, DataBase]:
compo_file = data_dir / yml_file

Expand All @@ -186,7 +197,9 @@ def get_network_and_database(
components_input = resolve_components_and_cnx(components_file, lib)
network = build_network(components_input)

database = build_data_base(components_file, data_dir)
database = build_data_base(
components_file, data_dir, scenarios=scenarios, timesteps=timesteps
)
return network, database


Expand Down Expand Up @@ -219,7 +232,11 @@ def create_problem_accurate_heuristic(
)

network, database = get_network_and_database(
data_dir, lib, "components_heuristic.yml"
data_dir,
lib,
"components_heuristic.yml",
scenarios=[scenario],
timesteps=list(range(week * number_hours, (week + 1) * number_hours)),
)

modify_parameters_of_cluster(
Expand All @@ -242,6 +259,7 @@ def create_problem_accurate_heuristic(
time_block,
scenarios,
border_management=BlockBorderManagement.CYCLE,
solver_id="XPRESS",
)

return problem
Expand Down Expand Up @@ -367,6 +385,7 @@ def create_problem_fast_heuristic(
parameters = pywraplp.MPSolverParameters()
parameters.SetIntegerParam(parameters.PRESOLVE, parameters.PRESOLVE_OFF)
parameters.SetIntegerParam(parameters.SCALING, 0)
parameters.SetDoubleParam(parameters.RELATIVE_MIP_GAP, 1e-7)
problem.solver.EnableOutput()

status = problem.solver.Solve(parameters)
Expand Down
181 changes: 181 additions & 0 deletions tests/functional/data_complex_case/components.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.
study:
nodes:
- id: N
model: NODE_BALANCE_MODEL

components:
- id: D
model: FIXED_DEMAND
parameters:
- name: demand
type: timeseries
timeseries: demand-ts
- id: S
model: SPI
parameters:
- name: cost
type: constant
value: 1
- id: U
model: UNSP
parameters:
- name: cost
type: constant
value: 10000
- id: G1
model: GEN
parameters:
- name: p_max
type: constant
value: 410
- name: p_min
type: constant
value: 180
- name: cost
type: constant
value: 96
- name: startup_cost
type: constant
value: 100500
- name: fixed_cost
type: constant
value: 1
- name: d_min_up
type: constant
value: 8
- name: d_min_down
type: constant
value: 8
- name: nb_units_min
type: constant
value: 0
- name: nb_units_max
type: constant
value: 1
- name: min_generating
type: constant
value: 0
- name: max_generating
type: timeseries
timeseries: series_G1
- id: G2
model: GEN
parameters:
- name: p_max
type: constant
value: 90
- name: p_min
type: constant
value: 60
- name: cost
type: constant
value: 137
- name: startup_cost
type: constant
value: 24500
- name: fixed_cost
type: constant
value: 1
- name: d_min_up
type: constant
value: 11
- name: d_min_down
type: constant
value: 11
- name: nb_units_min
type: constant
value: 0
- name: nb_units_max
type: constant
value: 3
- name: min_generating
type: constant
value: 0
- name: max_generating
type: timeseries
timeseries: series_G2
- id: G3
model: GEN
parameters:
- name: p_max
type: constant
value: 275
- name: p_min
type: constant
value: 150
- name: cost
type: constant
value: 107
- name: startup_cost
type: constant
value: 69500
- name: fixed_cost
type: constant
value: 1
- name: d_min_up
type: constant
value: 9
- name: d_min_down
type: constant
value: 9
- name: nb_units_min
type: constant
value: 0
- name: nb_units_max
type: constant
value: 4
- name: min_generating
type: constant
value: 0
- name: max_generating
type: timeseries
timeseries: series_G3



connections:
- component1: N
port_1: balance_port
component2: D
port_2: balance_port

- component1: N
port_1: balance_port
component2: G1
port_2: balance_port

- component1: N
port_1: balance_port
component2: G2
port_2: balance_port

- component1: N
port_1: balance_port
component2: G3
port_2: balance_port

- component1: N
port_1: balance_port
component2: U
port_2: balance_port

- component1: N
port_1: balance_port
component2: S
port_2: balance_port





Loading

0 comments on commit 412e62c

Please sign in to comment.