Skip to content

Commit

Permalink
Better workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette-Gerbaux committed Aug 23, 2024
1 parent 9dccf80 commit 4f47103
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 481 deletions.
84 changes: 83 additions & 1 deletion src/andromede/hydro_heuristic/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
# This file is part of the Antares project.

from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional

import numpy as np
import pandas as pd

from andromede.study import (
ConstantData,
DataBase,
TimeIndex,
TimeScenarioSeriesData,
TimeSeriesData,
)


@dataclass(frozen=False)
Expand Down Expand Up @@ -164,3 +172,77 @@ def compute_weekly_target(all_daily_generation: List[float]) -> List[float]:
]

return weekly_target


def get_database(hydro_data: HydroHeuristicData, id: str = "H") -> DataBase:
database = DataBase()

database.add_data(id, "capacity", ConstantData(hydro_data.reservoir_data.capacity))
database.add_data(
id,
"initial_level",
ConstantData(hydro_data.reservoir_data.initial_level),
)

inflow_data = pd.DataFrame(
hydro_data.inflow,
index=[i for i in range(len(hydro_data.inflow))],
columns=[0],
)
database.add_data(id, "inflow", TimeScenarioSeriesData(inflow_data))

target_data = pd.DataFrame(
hydro_data.target,
index=[i for i in range(len(hydro_data.target))],
columns=[0],
)
database.add_data(id, "generating_target", TimeScenarioSeriesData(target_data))
database.add_data(id, "overall_target", ConstantData(sum(hydro_data.target)))

database.add_data(
id,
"lower_rule_curve",
TimeSeriesData(
{
TimeIndex(i): hydro_data.lower_rule_curve[i]
* hydro_data.reservoir_data.capacity
for i in range(len(hydro_data.lower_rule_curve))
}
),
)
database.add_data(
id,
"upper_rule_curve",
TimeSeriesData(
{
TimeIndex(i): hydro_data.upper_rule_curve[i]
* hydro_data.reservoir_data.capacity
for i in range(len(hydro_data.lower_rule_curve))
}
),
)
database.add_data(id, "min_generating", ConstantData(0))

database.add_data(
id,
"max_generating",
TimeSeriesData(
{
TimeIndex(i): hydro_data.max_generating[i]
for i in range(len(hydro_data.max_generating))
}
),
)

database.add_data(
id,
"max_epsilon",
TimeSeriesData(
{
TimeIndex(i): (hydro_data.reservoir_data.capacity if i == 0 else 0)
for i in range(len(hydro_data.max_generating))
}
),
)

return database
2 changes: 1 addition & 1 deletion src/andromede/hydro_heuristic/heuristic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
"gamma_s": 0 if horizon == "monthly" else -1 / 32,
}

def get_model(
def build_model(
self,
) -> Model:
if "level" not in self.hydro_model.variables.keys():
Expand Down
190 changes: 36 additions & 154 deletions src/andromede/hydro_heuristic/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,26 @@
# This file is part of the Antares project.

from dataclasses import dataclass

from typing import List, Tuple

import ortools.linear_solver.pywraplp as pywraplp
import pandas as pd

from andromede.hydro_heuristic.data import (
DataAggregatorParameters,
HydroHeuristicData,
HydroHeuristicParameters,
ReservoirParameters,
get_database,
)
from andromede.model import Model
from andromede.simulation import (
BlockBorderManagement,
OptimizationProblem,
OutputValues,
TimeBlock,
build_problem,
)
from andromede.study import (
ConstantData,
DataBase,
Network,
TimeIndex,
TimeScenarioSeriesData,
TimeSeriesData,
create_component,
)
from andromede.study import DataBase, Network, create_component


@dataclass(frozen=True)
Expand All @@ -53,153 +45,43 @@ class OutputHeuristic:
level: float


class HydroHeuristicProblem:
def __init__(self, hydro_data: HydroHeuristicData, heuristic_model: Model) -> None:
self.hydro_data = hydro_data
self.id = "H"
database = self.generate_database()

hydro = create_component(
model=heuristic_model,
id=self.id,
)

network = Network("test")
network.add_component(hydro)

problem = build_problem(
network,
database,
TimeBlock(1, [i for i in range(len(hydro_data.target))]),
1,
border_management=(BlockBorderManagement.CYCLE),
)

self.problem = problem

def solve_hydro_problem(self) -> Tuple[SolvingOutput, OutputHeuristic]:
parameters = pywraplp.MPSolverParameters()
parameters.SetIntegerParam(parameters.PRESOLVE, parameters.PRESOLVE_OFF)
parameters.SetIntegerParam(parameters.SCALING, 0)

status = self.problem.solver.Solve(parameters)

output = OutputValues(self.problem)

return (
SolvingOutput(status, self.problem.solver.Objective().Value()),
OutputHeuristic(
output.component(self.id).var("generating").value[0], # type:ignore
output.component(self.id).var("level").value[0][-1], # type:ignore
),
)

def generate_database(
self,
) -> DataBase:
database = DataBase()

database.add_data(
self.id, "capacity", ConstantData(self.hydro_data.reservoir_data.capacity)
)
database.add_data(
self.id,
"initial_level",
ConstantData(self.hydro_data.reservoir_data.initial_level),
)

inflow_data = pd.DataFrame(
self.hydro_data.inflow,
index=[i for i in range(len(self.hydro_data.inflow))],
columns=[0],
)
database.add_data(self.id, "inflow", TimeScenarioSeriesData(inflow_data))

target_data = pd.DataFrame(
self.hydro_data.target,
index=[i for i in range(len(self.hydro_data.target))],
columns=[0],
)
database.add_data(
self.id, "generating_target", TimeScenarioSeriesData(target_data)
)
database.add_data(
self.id, "overall_target", ConstantData(sum(self.hydro_data.target))
)

database.add_data(
self.id,
"lower_rule_curve",
TimeSeriesData(
{
TimeIndex(i): self.hydro_data.lower_rule_curve[i]
* self.hydro_data.reservoir_data.capacity
for i in range(len(self.hydro_data.lower_rule_curve))
}
),
)
database.add_data(
self.id,
"upper_rule_curve",
TimeSeriesData(
{
TimeIndex(i): self.hydro_data.upper_rule_curve[i]
* self.hydro_data.reservoir_data.capacity
for i in range(len(self.hydro_data.lower_rule_curve))
}
),
)
database.add_data(self.id, "min_generating", ConstantData(0))

database.add_data(
self.id,
"max_generating",
TimeSeriesData(
{
TimeIndex(i): self.hydro_data.max_generating[i]
for i in range(len(self.hydro_data.max_generating))
}
),
)

database.add_data(
self.id,
"max_epsilon",
TimeSeriesData(
{
TimeIndex(i): (
self.hydro_data.reservoir_data.capacity if i == 0 else 0
)
for i in range(len(self.hydro_data.max_generating))
}
),
)

return database


def optimize_target(
heuristic_parameters: HydroHeuristicParameters,
data_aggregator_parameters: DataAggregatorParameters,
reservoir_data: ReservoirParameters,
heuristic_model: Model,
) -> Tuple[SolvingOutput, OutputHeuristic]:
# Récupération des données
data = HydroHeuristicData(
data_aggregator_parameters,
reservoir_data,
)
# Calcul de la préallocation
data.compute_target(heuristic_parameters)
def build_hydro_heuristic_problem(
database: DataBase, heuristic_model: Model, timesteps: List[int], id: str = "H"
) -> OptimizationProblem:
hydro = create_component(model=heuristic_model, id=id)
network = Network("test")
network.add_component(hydro)

# Ajustement de la réapartition
heuristic_problem = HydroHeuristicProblem(
hydro_data=data, heuristic_model=heuristic_model
problem = build_problem(
network,
database,
TimeBlock(1, timesteps),
1,
border_management=(BlockBorderManagement.CYCLE),
)

solving_output, heuristic_output = heuristic_problem.solve_hydro_problem()
return problem


def get_default_solver_parameters() -> pywraplp.MPSolverParameters:
parameters = pywraplp.MPSolverParameters()
parameters.SetIntegerParam(parameters.PRESOLVE, parameters.PRESOLVE_OFF)
parameters.SetIntegerParam(parameters.SCALING, 0)
return parameters


def retrieve_important_heuristic_output(
heuristic_problem: OptimizationProblem,
id: str = "H",
) -> OutputHeuristic:
output = OutputValues(heuristic_problem)

heuristic_output = OutputHeuristic(
output.component(id).var("generating").value[0], # type:ignore
output.component(id).var("level").value[0][-1], # type:ignore
)

return solving_output, heuristic_output
return heuristic_output


def update_initial_level(
Expand Down
Loading

0 comments on commit 4f47103

Please sign in to comment.