Skip to content

Commit

Permalink
Move unit out of objective function 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 Dec 9, 2024
1 parent d726fbb commit d588d48
Show file tree
Hide file tree
Showing 156 changed files with 492 additions and 279 deletions.
9 changes: 9 additions & 0 deletions commons/src/main/java/com/powsybl/openrao/commons/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public String toString() {
return symbol;
}

public static Unit getEnum(String value) {
for (Unit v : values()) {
if (v.toString().equals(value)) {
return v;
}
}
throw new IllegalArgumentException();
}

public void checkPhysicalParameter(PhysicalParameter requestedPhysicalParameter) {
if (!requestedPhysicalParameter.equals(physicalParameter)) {
throw new OpenRaoException(String.format("%s Unit is not suited to measure a %s value.", this.toString(), requestedPhysicalParameter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ 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);
raoParameters.getObjectiveFunctionParameters().setUnit(Unit.MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, raoResult, properties, CORE_CNE_EXPORT_PROPERTIES_PREFIX);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

Expand Down Expand Up @@ -272,7 +273,8 @@ 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);
raoParameters.getObjectiveFunctionParameters().setUnit(Unit.MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, raoResult, properties, CORE_CNE_EXPORT_PROPERTIES_PREFIX);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ And $PTDF_{zTos}(z1, c)$, the zone-to-slack PTDF of bidding zone $z1$ on CNEC $c

| Name | Symbol | Details |
|------------------------------------------------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [type](/parameters.md#type) | | This filler is only used if the objective function is MAX_MIN_MARGIN_IN_MEGAWATT, or MAX_MIN_MARGIN_IN_AMPERE. This parameter is also used to set the unit (AMPERE/MW) of the objective function |
| [type](/parameters.md#type) | | This filler is only used if the objective function is MAX_MIN_MARGIN. |
| [ptdf-sum-lower-bound](/parameters.md#ptdf-sum-lower-bound) | $\varepsilon_{PTDF}$ | zToz PTDF sum below this value are lifted to the ptdf-sum-lower-bound, to avoid a bad conditionning of the problem where the value of relative margins are very high. <br>*Its impact on the accuracy of the problem is insignificant, as high relative margins do not usually define the min. relative margin.* |

## Defined optimization variables
Expand Down
6 changes: 2 additions & 4 deletions docs/output-data/core-cne/java-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ properties.setProperty("rao-result.export.core-cne.receiver-id", "RECEIVER_ID");
properties.setProperty("rao-result.export.core-cne.receiver-role", "A36"); // CAPACITY_COORDINATOR
properties.setProperty("rao-result.export.core-cne.time-interval", "2021-10-30T22:00Z/2021-10-31T23:00Z");
switch (raoParameters.getObjectiveFunctionParameters().getType()) {
case MAX_MIN_RELATIVE_MARGIN_IN_AMPERE -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "true");
case MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "true");
case MAX_MIN_MARGIN_IN_AMPERE -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "false");
case MAX_MIN_MARGIN_IN_MEGAWATT -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "false");
case MAX_MIN_RELATIVE_MARGIN -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "true");
case MAX_MIN_MARGIN -> properties.setProperty("rao-result.export.core-cne.relative-positive-margins", "false");
}
if (raoParameters.hasExtension(LoopFlowParametersExtension.class)) {
properties.setProperty("rao-result.export.core-cne.with-loop-flows", "true");
Expand Down
38 changes: 21 additions & 17 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ These parameters should be always set in the RAO parameters file or object.
These parameters (objective-function) configure the remedial action optimisation's objective function.

#### type

- **Expected value**: one of the following:
- "MAX_MIN_MARGIN_IN_MEGAWATT"
- "MAX_MIN_MARGIN_IN_AMPERE"
- "MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT"
- "MAX_MIN_RELATIVE_MARGIN_IN_AMPERE"
- **Default value**: "MAX_MIN_MARGIN_IN_MEGAWATT"
- "MAX_MIN_MARGIN"
- "MAX_MIN_RELATIVE_MARGIN"
- **Default value**: "MAX_MIN_MARGIN"
- **Usage**: this parameter sets the objective function of the RAO. For now, the existing objective function are:
- **MAX_MIN_MARGIN_IN_MEGAWATT**: maximization of the min(margin), where min(margin) is the smallest margin of all
CNECs and the margins are considered in MW.
- **MAX_MIN_MARGIN_IN_AMPERE**: maximization of the min(margin), where min(margin) is the smallest margin of all CNECs
and the margins are considered in A. Note that this objective function is not just a homothety of the previous one,
as CNECs from different voltage levels will not have the same weight in the objective function depending on the unit
considered (MW or A). This objective function only works in AC-load-flow mode (see [sensitivity-parameters](#sensitivity-parameters)).
- **MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT**: same as MAX_MIN_MARGIN_IN_MEGAWATT, but the margins will be relative
- **MAX_MIN_MARGIN**: maximization of the min(margin), where min(margin) is the smallest margin of all
CNECs.
- **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.
- **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.

#### unit
- **Expected value**: one of the following:
- "MEGAWATT"
- "AMPERE"
- **Default value**: "MEGAWATT"
- **Usage**: this parameter sets the objective function unit of the RAO. For now, the existing objective function units are:
- **MEGAWATT**: the margins to maximize are considered in MW.
- **AMPERE**: the margins to maximize are considered in A.
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:
Expand Down Expand Up @@ -551,7 +553,8 @@ Zones are seperated by + or -.
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_RELATIVE_MARGIN_IN_AMPERE",
"type" : "MAX_MIN_RELATIVE_MARGIN",
"unit" : "A",
"curative-min-obj-improvement" : 0.0,
"preventive-stop-criterion" : "SECURE",
"enforce-curative-security" : true
Expand Down Expand Up @@ -669,7 +672,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_IN_AMPERE
type: MAX_MIN_MARGIN
unit: AMPERE
preventive-stop-criterion: SECURE

rao-range-actions-optimization:
Expand Down
6 changes: 4 additions & 2 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ raoParameters.setLoadFlowAndSensitivityParameters(loadFlowAndSensitivityParamete

// Ask the RAO to maximize minimum margin in MW, and to stop when network is secure (i.e. when margins are positive)
ObjectiveFunctionParameters objectiveFunctionParameters = new ObjectiveFunctionParameters();
objectiveFunctionParameters.setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN_IN_MEGAWATT);
objectiveFunctionParameters.setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN);
objectiveFunctionParameters.setUnit(Unit.MEGAWATT);
objectiveFunctionParameters.setPreventiveStopCriterion(ObjectiveFunctionParameters.PreventiveStopCriterion.SECURE);
objectiveFunctionParameters.setCurativeStopCriterion(ObjectiveFunctionParameters.CurativeStopCriterion.SECURE);
raoParameters.setObjectiveFunctionParameters(objectiveFunctionParameters);
Expand Down Expand Up @@ -577,7 +578,8 @@ public class Main {

// Ask the RAO to maximize minimum margin in MW, and to stop when network is secure (i.e. when margins are positive)
ObjectiveFunctionParameters objectiveFunctionParameters = new ObjectiveFunctionParameters();
objectiveFunctionParameters.setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN_IN_MEGAWATT);
objectiveFunctionParameters.setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_MARGIN);
objectiveFunctionParameters.setUnit(Unit.MEGAWATT);
objectiveFunctionParameters.setPreventiveStopCriterion(ObjectiveFunctionParameters.PreventiveStopCriterion.SECURE);
objectiveFunctionParameters.setCurativeStopCriterion(ObjectiveFunctionParameters.CurativeStopCriterion.SECURE);
raoParameters.setObjectiveFunctionParameters(objectiveFunctionParameters);
Expand Down
34 changes: 16 additions & 18 deletions python-util/rao_parameter_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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")):
Expand All @@ -25,7 +26,7 @@ def rao_parameters_file(file_path):
correct_version = True
if tag_by_file_type[ftype] in line:
score += 1
if "MAX_MIN_MARGIN_IN" in line or "MAX_MIN_RELATIVE_MARGIN_IN" in line:
if "MAX_MIN_MARGIN" in line or "MAX_MIN_RELATIVE_MARGIN" in line:
score += 1
if correct_version and score >= 2:
return True
Expand Down Expand Up @@ -82,24 +83,21 @@ def obj_function_as_str_lines(new_data, file_type):

def new_rao_param(data: dict, file_path: str, file_type: str) -> dict:
try:
old_obj_fun = data[tag_by_file_type[file_type]]
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 = {}
for key in old_obj_fun:
if key not in ("curative-stop-criterion", "optimize-curative-if-preventive-unsecure"):
new_obj_fun[key] = old_obj_fun[key]
prev_secure = "preventive-stop-criterion" not in old_obj_fun or old_obj_fun["preventive-stop-criterion"] == "SECURE"
if prev_secure:
if "optimize-curative-if-preventive-unsecure" in old_obj_fun:
new_obj_fun["enforce-curative-security"] = old_obj_fun["optimize-curative-if-preventive-unsecure"]
else:
cur_secure = "curative-stop-criterion" in old_obj_fun and old_obj_fun["curative-stop-criterion"] in ("SECURE", "PREVENTIVE_OBJECTIVE_AND_SECURE")
if cur_secure:
new_obj_fun["enforce-curative-security"] = True
else:
new_obj_fun["enforce-curative-security"] = False
cur_min = "curative-stop-criterion" in old_obj_fun and old_obj_fun["curative-stop-criterion"] == "MIN_OBJECTIVE"
if cur_min:
new_obj_fun["curative-min-obj-improvement"] = 10000.0 + (old_obj_fun["curative-min-obj-improvement"] if "curative-min-obj-improvement" in old_obj_fun else 0.0)
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]
except KeyError as ke:
raise KeyError("in file " + file_path) from ke
data[tag_by_file_type[file_type]] = new_obj_fun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private RaoParametersCommons() {
public static final String OBJECTIVE_FUNCTION = "objective-function";
public static final String OBJECTIVE_FUNCTION_SECTION = "rao-objective-function";
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.openrao.raoapi.json;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.commons.Unit;
import com.powsybl.openrao.raoapi.parameters.ObjectiveFunctionParameters;
import com.powsybl.openrao.raoapi.parameters.RaoParameters;
import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -27,6 +28,7 @@ private JsonObjectiveFunctionParameters() {
static void serialize(RaoParameters parameters, JsonGenerator jsonGenerator) throws IOException {
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());
Expand All @@ -39,6 +41,9 @@ static void deserialize(JsonParser jsonParser, RaoParameters raoParameters) thro
case TYPE:
raoParameters.getObjectiveFunctionParameters().setType(stringToObjectiveFunction(jsonParser.nextTextValue()));
break;
case UNIT:
raoParameters.getObjectiveFunctionParameters().setUnit(stringToObjectiveFunctionUnit(jsonParser.nextTextValue()));
break;
case PREVENTIVE_STOP_CRITERION:
raoParameters.getObjectiveFunctionParameters().setPreventiveStopCriterion(stringToPreventiveStopCriterion(jsonParser.nextTextValue()));
break;
Expand All @@ -64,6 +69,19 @@ private static ObjectiveFunctionParameters.ObjectiveFunctionType stringToObjecti
}
}

private static Unit stringToObjectiveFunctionUnit(String string) {
Unit unit;
try {
unit = Unit.getEnum(string);
} catch (IllegalArgumentException e) {
throw new OpenRaoException(String.format("Unknown objective function unit value: %s", string));
}
if (unit != Unit.MEGAWATT && unit != Unit.AMPERE) {
throw new OpenRaoException(String.format("Unknown objective function unit value: %s", string));
}
return unit;
}

private static ObjectiveFunctionParameters.PreventiveStopCriterion stringToPreventiveStopCriterion(String string) {
try {
return ObjectiveFunctionParameters.PreventiveStopCriterion.valueOf(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*/
public class ObjectiveFunctionParameters {
// Default values
private static final ObjectiveFunctionType DEFAULT_OBJECTIVE_FUNCTION = ObjectiveFunctionType.MAX_MIN_MARGIN_IN_MEGAWATT;
private static final ObjectiveFunctionType DEFAULT_OBJECTIVE_FUNCTION = ObjectiveFunctionType.MAX_MIN_MARGIN;
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;
Expand All @@ -30,26 +31,14 @@ public class ObjectiveFunctionParameters {
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 {
MAX_MIN_MARGIN_IN_MEGAWATT(Unit.MEGAWATT),
MAX_MIN_MARGIN_IN_AMPERE(Unit.AMPERE),
MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT(Unit.MEGAWATT),
MAX_MIN_RELATIVE_MARGIN_IN_AMPERE(Unit.AMPERE);

private final Unit unit;

ObjectiveFunctionType(Unit unit) {
this.unit = unit;
}

public Unit getUnit() {
return unit;
}

MAX_MIN_MARGIN,
MAX_MIN_RELATIVE_MARGIN;
public boolean relativePositiveMargins() {
return this.equals(MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT) || this.equals(MAX_MIN_RELATIVE_MARGIN_IN_AMPERE);
return this.equals(MAX_MIN_RELATIVE_MARGIN);
}
}

Expand All @@ -67,6 +56,14 @@ public void setType(ObjectiveFunctionType type) {
this.type = type;
}

public Unit getUnit() {
return unit;
}

public void setUnit(Unit unit) {
this.unit = unit;
}

public void setPreventiveStopCriterion(PreventiveStopCriterion preventiveStopCriterion) {
this.preventiveStopCriterion = preventiveStopCriterion;
}
Expand Down Expand Up @@ -94,6 +91,7 @@ public static ObjectiveFunctionParameters load(PlatformConfig platformConfig) {
.ifPresent(config -> {
parameters.setType(config.getEnumProperty(TYPE, ObjectiveFunctionType.class,
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));
Expand Down
Loading

0 comments on commit d588d48

Please sign in to comment.