Skip to content

Commit

Permalink
Merge content of preventive stop criterion into type
Browse files Browse the repository at this point in the history
Signed-off-by: Pauline Jean-Marie <pauline.jean-marie@artelys.com>
  • Loading branch information
Pauline Jean-Marie committed Nov 19, 2024
1 parent 8cadd8c commit 4e59f6b
Show file tree
Hide file tree
Showing 137 changed files with 205 additions and 308 deletions.
35 changes: 11 additions & 24 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ These parameters (objective-function) configure the remedial action optimisation

#### type
- **Expected value**: one of the following:
- "SECURE_FLOW"
- "MAX_MIN_MARGIN"
- "MAX_MIN_RELATIVE_MARGIN"
- **Default value**: "MAX_MIN_MARGIN"
- **Default value**: "SECURE_FLOW"
- **Usage**: this parameter sets the objective function of the RAO. For now, the existing objective function are:
- **MAX_MIN_MARGIN**: maximization of the min(margin), where min(margin) is the smallest margin of all
CNECs.
- **SECURE_FLOW**: The search-tree will stop as soon as it finds a solution where the minimum margin is positive.
- **MAX_MIN_MARGIN**: the search-tree will maximize the minimum margin until it converges to a
maximum value, or until another stop criterion has been reached (e.g. [max-preventive-search-tree-depth](#max-preventive-search-tree-depth)).
- **MAX_MIN_RELATIVE_MARGIN**: same as MAX_MIN_MARGIN, but the margins will be relative
(divided by the absolute sum of PTDFs) when they are positive.

Expand All @@ -42,17 +44,6 @@ These parameters (objective-function) configure the remedial action optimisation
Note that CNECs from different voltage levels will not have the same weight in the objective function depending on the unit
considered (MW or A). Ampere unit only works in AC-load-flow mode (see [sensitivity-parameters](#sensitivity-parameters)).

#### preventive-stop-criterion
- **Expected value**: one of the following:
- "MIN_OBJECTIVE"
- "SECURE"
- **Default value**: "SECURE"
- **Usage**: Stop criterion of the preventive RAO search-tree.
- **MIN_OBJECTIVE**: the search-tree will maximize the minimum margin until it converges to a
maximum value, or until another stop criterion has been reached (e.g. [max-preventive-search-tree-depth](#max-preventive-search-tree-depth)).
- **SECURE**: the search-tree will stop as soon as it finds a solution where the minimum margin is positive.
*Note: if the best possible minimum margin is negative, both stop criterion will return the same solution.*

#### curative-min-obj-improvement
- **Expected value**: numeric value, where the unit is that of the objective function
- **Default value**: 0
Expand All @@ -67,7 +58,7 @@ These parameters (objective-function) configure the remedial action optimisation
If this parameter is set to false, OpenRAO will stop after preventive if preventive state is unsecure and won't try to
improve curative states.

*Note: Only applied when ["preventive-stop-criterion"](#preventive-stop-criterion) is set to SECURE. In this case, if preventive was unsecure,
*Note: Only applied when ["type"](#type) is set to SECURE_FLOW. In this case, if preventive was unsecure,
second preventive won't be run, even if curative cost is higher, in order to save computation time*

### Range actions optimisation parameters
Expand Down Expand Up @@ -295,12 +286,10 @@ These parameters (second-preventive-rao) tune the behaviour of the [second preve
during curative RAO
- **POSSIBLE_CURATIVE_IMPROVEMENT**: a 2nd preventive RAO is run only if it is possible to improve a curative perimeter,
i.e. if the curative RAO stop criterion on at least one contingency is not reached.
This depends on the value of parameter [preventive-stop-criterion](#preventive-stop-criterion):
- **SECURE**: 2nd preventive RAO is run if one curative perimeter is not secure after optimisation
- **PREVENTIVE_OBJECTIVE**: 2nd preventive RAO is run if one curative perimeter reached an objective function value
This depends on the value of parameter [type](#type):
- **SECURE_FLOW**: 2nd preventive RAO is run if one curative perimeter is not secure after optimisation
- **MAX_MIN_MARGIN** or **MAX_MIN_RELATIVE_MARGIN**: 2nd preventive RAO is run if one curative perimeter reached an objective function value
after optimisation that is worse than the preventive perimeter's (decreased by [curative-min-obj-improvement](#curative-min-obj-improvement))
- **PREVENTIVE_OBJECTIVE_AND_SECURE**: 2nd preventive RAO is run if one of the two conditions above is satisfied
- **MIN_OBJECTIVE**: 2nd preventive RAO is always run, trying to improve the minimum margin even more

#### re-optimize-curative-range-actions
- **Expected value**: true/false
Expand Down Expand Up @@ -553,10 +542,9 @@ Zones are seperated by + or -.
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_RELATIVE_MARGIN",
"type" : "SECURE_FLOW",
"unit" : "A",
"curative-min-obj-improvement" : 0.0,
"preventive-stop-criterion" : "SECURE",
"enforce-curative-security" : true
},
"range-actions-optimization" : {
Expand Down Expand Up @@ -672,9 +660,8 @@ Zones are seperated by + or -.
Based on PowSyBl's [configuration mechanism](inv:powsyblcore:std:doc#user/configuration/index).
~~~yaml
rao-objective-function:
type: MAX_MIN_MARGIN
type: SECURE_FLOW
unit: AMPERE
preventive-stop-criterion: SECURE

rao-range-actions-optimization:
max-mip-iterations: 5
Expand Down
23 changes: 8 additions & 15 deletions python-util/rao_parameter_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,16 @@ def obj_function_as_str_lines(new_data, file_type):
def new_rao_param(data: dict, file_path: str, file_type: str) -> dict:
try:
obj_fun = data[tag_by_file_type[file_type]]
# new_obj_fun to have type and unit on top of obj fun
new_obj_fun = {}
if "type" in obj_fun:
if "MAX_MIN_MARGIN" in obj_fun["type"]:
new_obj_fun["type"] = "MAX_MIN_MARGIN"
elif "MAX_MIN_RELATIVE_MARGIN" in obj_fun["type"]:
new_obj_fun["type"] = "MAX_MIN_RELATIVE_MARGIN"
if "MEGAWATT" in obj_fun["type"]:
new_obj_fun["unit"] = "MW"
elif "AMPERE" in obj_fun["type"]:
new_obj_fun["unit"] = "A"
for key in obj_fun:
if key not in new_obj_fun:
new_obj_fun[key] = obj_fun[key]
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"
elif not prev_secure and "type" not in obj_fun:
obj_fun["type"] = "MAX_MIN_MARGIN"
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]] = new_obj_fun
data[tag_by_file_type[file_type]] = obj_fun
return data


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ private RaoParametersCommons() {
public static final String TYPE = "type";
public static final String UNIT = "unit";
public static final String CURATIVE_MIN_OBJ_IMPROVEMENT = "curative-min-obj-improvement";
public static final String PREVENTIVE_STOP_CRITERION = "preventive-stop-criterion";
public static final String ENFORCE_CURATIVE_SECURITY = "enforce-curative-security";

// range actions optimization parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static void serialize(RaoParameters parameters, JsonGenerator jsonGenerator) thr
jsonGenerator.writeObjectFieldStart(OBJECTIVE_FUNCTION);
jsonGenerator.writeObjectField(TYPE, parameters.getObjectiveFunctionParameters().getType());
jsonGenerator.writeObjectField(UNIT, parameters.getObjectiveFunctionParameters().getUnit());
jsonGenerator.writeObjectField(PREVENTIVE_STOP_CRITERION, parameters.getObjectiveFunctionParameters().getPreventiveStopCriterion());
jsonGenerator.writeNumberField(CURATIVE_MIN_OBJ_IMPROVEMENT, parameters.getObjectiveFunctionParameters().getCurativeMinObjImprovement());
jsonGenerator.writeBooleanField(ENFORCE_CURATIVE_SECURITY, parameters.getObjectiveFunctionParameters().getEnforceCurativeSecurity());
jsonGenerator.writeEndObject();
Expand All @@ -44,9 +43,6 @@ static void deserialize(JsonParser jsonParser, RaoParameters raoParameters) thro
case UNIT:
raoParameters.getObjectiveFunctionParameters().setUnit(stringToObjectiveFunctionUnit(jsonParser.nextTextValue()));
break;
case PREVENTIVE_STOP_CRITERION:
raoParameters.getObjectiveFunctionParameters().setPreventiveStopCriterion(stringToPreventiveStopCriterion(jsonParser.nextTextValue()));
break;
case CURATIVE_MIN_OBJ_IMPROVEMENT:
jsonParser.nextToken();
raoParameters.getObjectiveFunctionParameters().setCurativeMinObjImprovement(jsonParser.getValueAsDouble());
Expand Down Expand Up @@ -82,12 +78,4 @@ private static Unit stringToObjectiveFunctionUnit(String string) {
return unit;
}

private static ObjectiveFunctionParameters.PreventiveStopCriterion stringToPreventiveStopCriterion(String string) {
try {
return ObjectiveFunctionParameters.PreventiveStopCriterion.valueOf(string);
} catch (IllegalArgumentException e) {
throw new OpenRaoException(String.format("Unknown preventive stop criterion: %s", string));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,26 @@
*/
public class ObjectiveFunctionParameters {
// Default values
private static final ObjectiveFunctionType DEFAULT_OBJECTIVE_FUNCTION = ObjectiveFunctionType.MAX_MIN_MARGIN;
private static final ObjectiveFunctionType DEFAULT_OBJECTIVE_FUNCTION = ObjectiveFunctionType.SECURE_FLOW;
private static final Unit DEFAULT_UNIT = Unit.MEGAWATT;
private static final double DEFAULT_CURATIVE_MIN_OBJ_IMPROVEMENT = 0;
private static final PreventiveStopCriterion DEFAULT_PREVENTIVE_STOP_CRITERION = PreventiveStopCriterion.SECURE;
private static final boolean DEFAULT_ENFORCE_CURATIVE_SECURITY = false;
// Attributes
private ObjectiveFunctionType type = DEFAULT_OBJECTIVE_FUNCTION;
private double curativeMinObjImprovement = DEFAULT_CURATIVE_MIN_OBJ_IMPROVEMENT;
private PreventiveStopCriterion preventiveStopCriterion = DEFAULT_PREVENTIVE_STOP_CRITERION;
private boolean enforceCurativeSecurity = DEFAULT_ENFORCE_CURATIVE_SECURITY;
private Unit unit = DEFAULT_UNIT;

// Enum
public enum ObjectiveFunctionType {
SECURE_FLOW,
MAX_MIN_MARGIN,
MAX_MIN_RELATIVE_MARGIN;
public boolean relativePositiveMargins() {
return this.equals(MAX_MIN_RELATIVE_MARGIN);
}
}

public enum PreventiveStopCriterion {
MIN_OBJECTIVE,
SECURE
}

// Getters and setters
public ObjectiveFunctionType getType() {
return type;
Expand All @@ -64,18 +58,10 @@ public void setUnit(Unit unit) {
this.unit = unit;
}

public void setPreventiveStopCriterion(PreventiveStopCriterion preventiveStopCriterion) {
this.preventiveStopCriterion = preventiveStopCriterion;
}

public double getCurativeMinObjImprovement() {
return curativeMinObjImprovement;
}

public PreventiveStopCriterion getPreventiveStopCriterion() {
return preventiveStopCriterion;
}

public boolean getEnforceCurativeSecurity() {
return enforceCurativeSecurity;
}
Expand All @@ -93,8 +79,6 @@ public static ObjectiveFunctionParameters load(PlatformConfig platformConfig) {
DEFAULT_OBJECTIVE_FUNCTION));
parameters.setUnit(config.getEnumProperty(UNIT, Unit.class, DEFAULT_UNIT));
parameters.setCurativeMinObjImprovement(config.getDoubleProperty(CURATIVE_MIN_OBJ_IMPROVEMENT, DEFAULT_CURATIVE_MIN_OBJ_IMPROVEMENT));
parameters.setPreventiveStopCriterion(config.getEnumProperty(PREVENTIVE_STOP_CRITERION, PreventiveStopCriterion.class,
DEFAULT_PREVENTIVE_STOP_CRITERION));
parameters.setEnforceCurativeSecurity(config.getBooleanProperty(ENFORCE_CURATIVE_SECURITY, DEFAULT_ENFORCE_CURATIVE_SECURITY));
});
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ void roundTrip() throws IOException {
// Objective Function parameters
parameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN);
parameters.getObjectiveFunctionParameters().setUnit(Unit.AMPERE);
parameters.getObjectiveFunctionParameters().setPreventiveStopCriterion(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE);
parameters.getObjectiveFunctionParameters().setCurativeMinObjImprovement(983);
parameters.getObjectiveFunctionParameters().setEnforceCurativeSecurity(true);
// RangeActionsOptimization parameters
Expand Down Expand Up @@ -123,7 +122,7 @@ void update() {
assertEquals(1, parameters.getExtensions().size());
JsonRaoParameters.update(parameters, getClass().getResourceAsStream("/RaoParameters_update_v2.json"));
assertEquals(2, parameters.getExtensions().size());
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, parameters.getObjectiveFunctionParameters().getPreventiveStopCriterion());
assertEquals(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN, parameters.getObjectiveFunctionParameters().getType());
assertEquals(5, parameters.getTopoOptimizationParameters().getMaxPreventiveSearchTreeDepth(), DOUBLE_TOLERANCE);
assertEquals(2, parameters.getTopoOptimizationParameters().getMaxAutoSearchTreeDepth(), DOUBLE_TOLERANCE);
assertEquals(5, parameters.getTopoOptimizationParameters().getMaxCurativeSearchTreeDepth(), DOUBLE_TOLERANCE);
Expand Down Expand Up @@ -177,7 +176,7 @@ void testFailOnOldVersion() {
}

@ParameterizedTest
@ValueSource(strings = {"LoopFlowError", "PrevStopCriterionError", "WrongField"})
@ValueSource(strings = {"LoopFlowError", "ObjFuncTypeError", "WrongField"})
void importNokTest(String source) {
InputStream inputStream = getClass().getResourceAsStream("/RaoParametersWith" + source + "_v2.json");
assertThrows(OpenRaoException.class, () -> JsonRaoParameters.read(inputStream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void checkObjectiveFunctionConfig() {
objectiveFunctionModuleConfig.setStringProperty("type", "MAX_MIN_RELATIVE_MARGIN");
objectiveFunctionModuleConfig.setStringProperty("unit", "AMPERE");
objectiveFunctionModuleConfig.setStringProperty("curative-min-obj-improvement", Objects.toString(123.0));
objectiveFunctionModuleConfig.setStringProperty("preventive-stop-criterion", "MIN_OBJECTIVE");
objectiveFunctionModuleConfig.setStringProperty("enforce-curative-security", "false");

RaoParameters parameters = new RaoParameters();
Expand All @@ -54,7 +53,6 @@ void checkObjectiveFunctionConfig() {
assertEquals(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN, objectiveFunctionParameters.getType());
assertEquals(Unit.AMPERE, objectiveFunctionParameters.getUnit());
assertEquals(123, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertFalse(objectiveFunctionParameters.getEnforceCurativeSecurity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void testConfigWithExtensions() throws IOException {
assertEquals(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN, objectiveFunctionParameters.getType());
assertEquals(Unit.AMPERE, objectiveFunctionParameters.getUnit());
assertEquals(3, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertFalse(objectiveFunctionParameters.getEnforceCurativeSecurity());

RangeActionsOptimizationParameters rangeActionsOptimizationParameters = parameters.getRangeActionsOptimizationParameters();
Expand Down Expand Up @@ -130,7 +129,6 @@ void testConfigWithoutExtensions() throws IOException {
assertEquals(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN, objectiveFunctionParameters.getType());
assertEquals(Unit.AMPERE, objectiveFunctionParameters.getUnit());
assertEquals(3, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertFalse(objectiveFunctionParameters.getEnforceCurativeSecurity());

RangeActionsOptimizationParameters rangeActionsOptimizationParameters = parameters.getRangeActionsOptimizationParameters();
Expand Down Expand Up @@ -200,7 +198,6 @@ void testConfigWithPartialExtensions() throws IOException {
assertEquals(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN, objectiveFunctionParameters.getType());
assertEquals(Unit.MEGAWATT, objectiveFunctionParameters.getUnit());
assertEquals(3, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertFalse(objectiveFunctionParameters.getEnforceCurativeSecurity());

RangeActionsOptimizationParameters rangeActionsOptimizationParameters = parameters.getRangeActionsOptimizationParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"objective-function" : {
"type" : "MAX_MIN_MARGIN",
"unit" : "A",
"preventive-stop-criterion" : "MIN_OBJECTIVE",
"curative-min-obj-improvement" : 983.0,
"enforce-curative-security" : true
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.5",
"objective-function" : {
"type" : "MAX_MIN_MARGIN",
"type" : "SECURE_FLOW",
"unit" : "MW",
"preventive-stop-criterion" : "SECURE",
"curative-min-obj-improvement" : 0.0,
"enforce-curative-security" : false
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.5",
"objective-function" : {
"type" : "MAX_MIN_MARGIN",
"type" : "SECURE_FLOW",
"unit" : "MW",
"preventive-stop-criterion" : "SECURE",
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
Expand Down
Loading

0 comments on commit 4e59f6b

Please sign in to comment.