Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Rao parameters new objective function #1111

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void testExportTwoPreventiveCnecs() {

mockCnecResult(cnec2, 800, -200, -999999999, .2);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

Expand Down Expand Up @@ -223,7 +223,7 @@ void testExportPreventivePureMnec() {

mockCnecResult(cnec1, 80, 20, 200, .1);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

Expand Down Expand Up @@ -261,7 +261,7 @@ void testExportPreventiveCnecAndMnec() {

mockCnecResult(cnec1, 80, 20, 200, .1);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

Expand Down Expand Up @@ -338,7 +338,7 @@ void testCurativeCnecs() {
mockCnecResult(cnecOutage, 85, 25, 205, .1);
mockCnecResult(cnecCur, 85, 28, 208, .1);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
when(raoResult.getActivatedNetworkActionsDuringState(crac.getState(cnecCur.getState().getContingency().orElseThrow(), curativeInstant))).thenReturn(Set.of(Mockito.mock(NetworkAction.class)));
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));
Expand Down Expand Up @@ -391,7 +391,7 @@ void testWithLoopFlow() {
mockCnecResult(cnec1, 80, 20, 200, .1);
Mockito.when(raoResult.getLoopFlow(any(), eq(cnec1), eq(TwoSides.TWO), eq(Unit.MEGAWATT))).thenReturn(123.);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
raoParameters.addExtension(LoopFlowParametersExtension.class, new LoopFlowParametersExtension());
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void testPstInitialSetpoint() {

Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

Expand Down Expand Up @@ -152,7 +152,7 @@ void testPstInitialSetpointUnused() {

Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(false);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

Expand All @@ -174,7 +174,7 @@ void testIgnorePstWithNoUsageRule() {
Mockito.when(raoResult.getActivatedRangeActionsDuringState(any())).thenReturn(Set.of(pstRangeAction));
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

Expand All @@ -200,7 +200,7 @@ void testPstUsedInPreventive() {
Mockito.when(raoResult.getOptimizedTapOnState(crac.getPreventiveState(), pstRangeAction)).thenReturn(16);
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);
Expand Down Expand Up @@ -253,7 +253,7 @@ void testPstUsedInCurative() {
Mockito.when(raoResult.getOptimizedTapOnState(crac.getState("contingency-id", curativeInstant), pstRangeAction)).thenReturn(16);
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);
Expand Down Expand Up @@ -301,7 +301,7 @@ void testIgnoreNetworkActionWithNoUsageRule() {

Mockito.when(raoResult.getActivatedNetworkActionsDuringState(any())).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

Expand All @@ -321,7 +321,7 @@ void testNetworkActionUsedInPreventive() {

Mockito.when(raoResult.getActivatedNetworkActionsDuringState(crac.getPreventiveState())).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);
Expand Down Expand Up @@ -365,7 +365,7 @@ void testNetworkActionUsedInCurative() {
Mockito.when(raoResult.getActivatedNetworkActionsDuringState(crac.getState("contingency-id", outageInstant))).thenReturn(new HashSet());
Mockito.when(raoResult.getActivatedNetworkActionsDuringState(crac.getState("contingency-id", curativeInstant))).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);
Expand Down Expand Up @@ -411,7 +411,7 @@ void testPstInitialSetpointInverted() {

Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
Expand Down Expand Up @@ -455,7 +455,7 @@ void testPstUsedInPreventiveInverted() {
Mockito.when(raoResult.getOptimizedTapOnState(crac.getPreventiveState(), pstRangeAction)).thenReturn(16);
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
Expand Down Expand Up @@ -511,7 +511,7 @@ void testPstUsedInCurativeInverted() {
Mockito.when(raoResult.getOptimizedTapOnState(crac.getState("contingency-id", curativeInstant), pstRangeAction)).thenReturn(16);
Mockito.when(raoResult.isActivatedDuringState(crac.getState("contingency-id", curativeInstant), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
Expand Down
2 changes: 1 addition & 1 deletion docs/output-data/rao-result/steps.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This field contains macro information about which steps the [CASTOR RAO](/castor.md#algorithm) executed.
(See also: [Forbidding cost increase](/parameters.md#forbid-cost-increase), [Second preventive RAO parameters](/parameters.md#second-preventive-rao-parameters))
(See also: [Second preventive RAO parameters](/parameters.md#second-preventive-rao-parameters))

| Value | Did CASTOR run a 1st preventive RAO? | Did CASTOR run a 2nd preventive RAO? | Did the RAO fall back to initial situation? | Did the RAO fall back to 1st preventive RAO result even though a 2nd was run? |
|----------------------------------------------------------|--------------------------------------|--------------------------------------|---------------------------------------------|-------------------------------------------------------------------------------|
Expand Down
12 changes: 0 additions & 12 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ These parameters (objective-function) configure the remedial action optimisation
- **MAX_MIN_RELATIVE_MARGIN_IN_AMPERE**: same as MAX_MIN_MARGIN_IN_AMPERE, but the margins will be relative (divided
by the absolute sum of PTDFs) when they are positive.

#### forbid-cost-increase
- **Expected value**: true/false
- **Default value**: false
- **Usage**: if this parameter is set to true, OpenRAO will post-check the results after optimisation. If the value of
the objective function is worse after optimisation than before optimisation, then it will return the initial
solution (i.e. no PRA and no CRA applied).
This can happen for example if the preventive RAO decreases the margin on a curative CNEC, which cannot be reverted
during curative RAO.
If this parameter is set to false, OpenRAO will return the real result of optimisation, which has a worse result
than the initial situation.

#### preventive-stop-criterion
- **Expected value**: one of the following:
- "MIN_OBJECTIVE"
Expand Down Expand Up @@ -584,7 +573,6 @@ Zones are seperated by + or -.
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_RELATIVE_MARGIN_IN_AMPERE",
"forbid-cost-increase" : false,
"curative-min-obj-improvement" : 0.0,
"preventive-stop-criterion" : "SECURE",
"curative-stop-criterion" : "PREVENTIVE_OBJECTIVE",
Expand Down
108 changes: 108 additions & 0 deletions python-util/rao_parameter_modification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright (c) 2024, RTE (http://www.rte-france.com)
# 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

import os
import json
from json import JSONDecodeError
import yaml

current_directory = os.getcwd()


tag_by_file_type = {"json": "objective-function", "yaml": "rao-objective-function"}

def rao_parameters_file(file_path):
# do not work with yaml yet
correct_version = False
score = 0
if "target" not in file_path and (file_path.endswith(".json") or file_path.endswith(".yml")):
ftype = "json" if file_path.endswith(".json") else "yaml"
with open(os.path.join(dirpath, filename), 'r') as file:
for line in file:
if '"version" : "2.4"' in line or '"version" : "2.5"' in line:
correct_version = True
if tag_by_file_type[ftype] in line:
score += 1
if "MAX_MIN_MARGIN" in line or "MAX_MIN_RELATIVE_MARGIN" in line:
score += 1
if correct_version and score >= 2:
return True
return False

def read_data(file_path) -> tuple[dict, str]:
if file_path.endswith(".json"):
with open(file_path, 'r') as file:
try:
return json.load(file), "json"
except JSONDecodeError as je:
print("in file " + file_path)
raise je
if file_path.endswith(".yml"):
with open(file_path, 'r') as file:
return yaml.safe_load(file), "yaml" # ["rao-parameters"]

def extract_leading_whitespace(line):
leading_whitespace = ""
for char in line:
if char.isspace():
leading_whitespace += char
else:
break
return leading_whitespace

def write_data(new_data, file_path, file_type):
with open(file_path, 'r') as file:
lines = file.readlines()
lines_to_write = []
inside_obj_fun_to_replace = False
for line in lines:
if tag_by_file_type[file_type] in line and tag_by_file_type[file_type] in new_data:
leading_whitespace = extract_leading_whitespace(line)
inside_obj_fun_to_replace = True
if inside_obj_fun_to_replace and ((file_type == "json" and "}" in line) or (file_type == "yaml" and line == "\n")):
obj_fun_str = f'"{tag_by_file_type[file_type]}" : ' + obj_function_as_str_lines(new_data, file_type)
for new_line in obj_fun_str.splitlines(True):
lines_to_write.append(leading_whitespace + new_line)
inside_obj_fun_to_replace = False
elif not inside_obj_fun_to_replace:
lines_to_write.append(line)
with open(file_path, 'w') as file:
file.writelines(lines_to_write)


def obj_function_as_str_lines(new_data, file_type):
if file_type == "json":
return json.dumps(new_data[tag_by_file_type[file_type]], indent=2, separators=(',', ' : ')) + ',\n'
else:
return yaml.dump(new_data[tag_by_file_type[file_type]], default_flow_style=False) + '\n'



def new_rao_param(data: dict, file_path: str, file_type: str) -> dict:
try:
obj_fun = data[tag_by_file_type[file_type]]
prev_secure = "preventive-stop-criterion" not in obj_fun or obj_fun["preventive-stop-criterion"] == "SECURE"
if prev_secure and ("type" in obj_fun or "preventive-stop-criterion" in obj_fun):
obj_fun["type"] = "SECURE_FLOW"
if "preventive-stop-criterion" in obj_fun:
del obj_fun["preventive-stop-criterion"]
except KeyError as ke:
raise KeyError("in file " + file_path) from ke
data[tag_by_file_type[file_type]] = obj_fun
return data



if __name__ == "__main__":
base_dir = os.path.join(current_directory, "..")
print(base_dir)
for dirpath, dirnames, filenames in os.walk(base_dir):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if rao_parameters_file(file_path):
data, file_type = read_data(file_path)
new_rao_params = new_rao_param(data, file_path, file_type)
write_data(new_rao_params, file_path, file_type)
Loading
Loading