-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from Deltares/feature/108-apply-a-measure-sel…
…ection-strategy-to-all-locations Feature/108 apply a measure selection strategy to all locations
- Loading branch information
Showing
21 changed files
with
335 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
from dataclasses import field, dataclass | ||
|
||
from koswat.cost_report.multi_location_profile import MultiLocationProfileCostReport | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.strategies.strategy_location_matrix import StrategyLocationReinforcements | ||
|
||
|
||
@dataclass | ||
class KoswatSummary: | ||
locations_profile_report_list: List[MultiLocationProfileCostReport] | ||
available_locations: List[PointSurroundings] | ||
|
||
def __init__(self) -> None: | ||
self.locations_profile_report_list = [] | ||
self.available_locations = [] | ||
locations_profile_report_list: list[MultiLocationProfileCostReport] = field(default_factory=lambda: []) | ||
available_locations: list[PointSurroundings] = field(default_factory=lambda: []) | ||
reinforcement_per_locations: list[StrategyLocationReinforcements] = field(default_factory=lambda: []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
koswat/cost_report/summary/koswat_summary_location_matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
from typing import Type | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.dike_reinforcements.reinforcement_profile.reinforcement_profile import ( | ||
ReinforcementProfile, | ||
) | ||
|
||
|
||
class KoswatSummaryLocationMatrix: | ||
locations_matrix: dict[PointSurroundings, list[Type[ReinforcementProfile]]] | ||
|
||
def __init__(self) -> None: | ||
self.locations_matrix = [] | ||
|
||
def sort_by_traject_order(self): | ||
self.locations_matrix = dict( | ||
sorted(self.locations_matrix.items(), key=lambda x: x[0].traject_order) | ||
) | ||
|
||
@classmethod | ||
def from_point_surroundings_list(cls, point_surroundings: list[PointSurroundings]): | ||
_this_cls = cls() | ||
_this_cls.locations_matrix = dict((_ps, []) for _ps in point_surroundings) | ||
return _this_cls |
80 changes: 80 additions & 0 deletions
80
koswat/cost_report/summary/koswat_summary_location_matrix_builder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import logging | ||
from koswat.core.protocols.builder_protocol import BuilderProtocol | ||
from koswat.cost_report.multi_location_profile.multi_location_profile_cost_report import ( | ||
MultiLocationProfileCostReport, | ||
) | ||
from koswat.cost_report.summary.koswat_summary_location_matrix import ( | ||
KoswatSummaryLocationMatrix, | ||
) | ||
from koswat.dike.surroundings.point.point_surroundings import PointSurroundings | ||
from koswat.dike_reinforcements.reinforcement_profile.reinforcement_profile import ( | ||
ReinforcementProfile, | ||
) | ||
|
||
|
||
class KoswatSummaryLocationMatrixBuilder(BuilderProtocol): | ||
""" | ||
NOTE: Although this 'problem' could be easily solved with `pandas` | ||
or `numpy`, I prefer not to include external (heavy) dependencies unless | ||
strictily necessary. | ||
If / when performance would become a problem, then this builder could (perhaps) | ||
benefit from using the aforementioned libraries. | ||
""" | ||
|
||
locations_profile_report_list: list[MultiLocationProfileCostReport] | ||
available_locations: list[PointSurroundings] | ||
|
||
def __init__( | ||
self, | ||
locations_profile: list[MultiLocationProfileCostReport], | ||
available_locations: list[PointSurroundings], | ||
) -> None: | ||
self.locations_profile_report_list = locations_profile | ||
self.available_locations = available_locations | ||
|
||
def _get_multi_location_profile_to_dict_matrix( | ||
self, locations_profile: MultiLocationProfileCostReport | ||
) -> dict[PointSurroundings, ReinforcementProfile]: | ||
return dict( | ||
(_location, type(locations_profile.profile_cost_report.reinforced_profile)) | ||
for _location in locations_profile.locations | ||
) | ||
|
||
def _get_list_summary_matrix_for_locations_with_reinforcements( | ||
self, | ||
) -> list[dict[PointSurroundings, ReinforcementProfile]]: | ||
return list( | ||
map( | ||
self._get_multi_location_profile_to_dict_matrix, | ||
self.locations_profile_report_list, | ||
) | ||
) | ||
|
||
def build(self) -> KoswatSummaryLocationMatrix: | ||
# 1. First we get all the possible reinforcements per point. | ||
|
||
logging.info("Initalizing locations-reinforcements matrix.") | ||
_reinforce_matrix_dict_list = ( | ||
self._get_list_summary_matrix_for_locations_with_reinforcements() | ||
) | ||
|
||
# 2. Then we initialize the matrix with all available locations, | ||
# but no reinforcements. | ||
|
||
_summary_matrix = KoswatSummaryLocationMatrix.from_point_surroundings_list( | ||
self.available_locations | ||
) | ||
|
||
# 3. Last, we merge the reinforcements dictionary into the matrix. | ||
for _location in _summary_matrix.locations_matrix.keys(): | ||
for _reinforce_matrix_dict in _reinforce_matrix_dict_list: | ||
if _location in _reinforce_matrix_dict: | ||
_summary_matrix.locations_matrix[_location].append( | ||
_reinforce_matrix_dict[_location] | ||
) | ||
|
||
_summary_matrix.sort_by_traject_order() | ||
|
||
logging.info("Finalized locations-reinforcements matrix.") | ||
|
||
return _summary_matrix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.