Skip to content

Commit

Permalink
Refactor rao parameters objective function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauline Jean-Marie committed Aug 13, 2024
1 parent 56f3ed6 commit ddbef67
Show file tree
Hide file tree
Showing 137 changed files with 562 additions and 682 deletions.
99 changes: 87 additions & 12 deletions python-util/rao_parameter_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,101 @@

import os
import json
import re
from json import JSONDecodeError

root_directory = os.getcwd()
# import yaml

current_directory = os.getcwd()


def rao_parameters_file(file_path):
if file_path.endswith(".json") or file_path.endswith(".yml"):
if "target" not in file_path and file_path.endswith(".json"): #or file_path.endswith(".yml"):
with open(os.path.join(dirpath, filename), 'r') as file:
for line in file:
if "objective-function" in line:
return True
return False

def read_data(file_path) -> dict:
if file_path.endswith(".json"):
with open(file_path, 'r') as file:
try:
return json.load(file)
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)["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):
if file_path.endswith(".json"):
with open(file_path, 'r') as file:
lines = file.readlines()
lines_to_write = []
inside_obj_fun_to_replace = False
for line in lines:
if "objective-function" in line and "objective-function" in new_data:
leading_whitespace = extract_leading_whitespace(line)
inside_obj_fun_to_replace = True
if inside_obj_fun_to_replace and "}" in line:
obj_fun_str = '"objective-function" : ' + json.dumps(new_data["objective-function"], indent=2, separators=(',', ' : ')) + ',\n'
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)
#if file_path.endswith(".yml"):
# with open(file_path, 'r') as file:
# all_data = yaml.safe_load(file)
# all_data["rao-parameters"] = datagit diff -
# with open(file_path, 'w') as file:
# yaml.dump(all_data, file, default_flow_style=False)

def new_rao_param(data: dict, file_path: str) -> dict:
try:
old_obj_fun = data["objective-function"]
new_obj_fun = {}
if "preventive-stop-criterion" not in old_obj_fun or old_obj_fun["preventive-stop-criterion"] == "SECURE":
new_obj_fun["type"] = "SECURE_FLOW"
new_obj_fun["enforce-curative-security"] = old_obj_fun["optimize-curative-if-preventive-unsecure"]
elif old_obj_fun["type"] in ("MAX_MIN_MARGIN_IN_MEGAWATT", "MAX_MIN_MARGIN_IN_AMPERE"):
new_obj_fun["type"] = "MAX_MIN_FLOW_MARGIN"
elif old_obj_fun["type"] in ("MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT", "MAX_MIN_RELATIVE_MARGIN_IN_AMPERE"):
new_obj_fun["type"] = "MAX_MIN_RELATIVE_FLOW_MARGIN"
if "curative-stop-criterion" in old_obj_fun:
if "SECURE" in old_obj_fun["curative-stop-criterion"]:
new_obj_fun["enforce-curative-security"] = True
else:
new_obj_fun["enforce-curative-security"] = False
if "curative-min-obj-improvement" in old_obj_fun:
new_obj_fun["curative-min-obj-improvement"] = old_obj_fun["curative-min-obj-improvement"]
except KeyError as ke:
raise KeyError("in file " + file_path) from ke
data["objective-function"] = new_obj_fun
return data



for dirpath, dirnames, filenames in os.walk(root_directory):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if rao_parameters_file(file_path):
if file_path.endswith(".json"):
with open(file_path, 'r') as file:
data = json.load(file)
lines = [line for line in lines if "forbid-cost-increase" not in line]
with open(file_path, 'w') as file:
file.writelines(lines)
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 = read_data(file_path)
new_rao_params = new_rao_param(data, file_path)
write_data(new_rao_params, file_path)
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ private JsonObjectiveFunctionParameters() {
static void serialize(RaoParameters parameters, JsonGenerator jsonGenerator) throws IOException {
jsonGenerator.writeObjectFieldStart(OBJECTIVE_FUNCTION);
jsonGenerator.writeObjectField(TYPE, parameters.getObjectiveFunctionParameters().getType());
jsonGenerator.writeObjectField(PREVENTIVE_STOP_CRITERION, parameters.getObjectiveFunctionParameters().getPreventiveStopCriterion());
jsonGenerator.writeObjectField(CURATIVE_STOP_CRITERION, parameters.getObjectiveFunctionParameters().getCurativeStopCriterion());
jsonGenerator.writeNumberField(CURATIVE_MIN_OBJ_IMPROVEMENT, parameters.getObjectiveFunctionParameters().getCurativeMinObjImprovement());
jsonGenerator.writeBooleanField(OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE, parameters.getObjectiveFunctionParameters().getOptimizeCurativeIfPreventiveUnsecure());
jsonGenerator.writeBooleanField(OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE, parameters.getObjectiveFunctionParameters().getEnforceCurativeSecurity());
jsonGenerator.writeEndObject();
}

Expand All @@ -40,19 +38,13 @@ static void deserialize(JsonParser jsonParser, RaoParameters raoParameters) thro
case TYPE:
raoParameters.getObjectiveFunctionParameters().setType(stringToObjectiveFunction(jsonParser.nextTextValue()));
break;
case PREVENTIVE_STOP_CRITERION:
raoParameters.getObjectiveFunctionParameters().setPreventiveStopCriterion(stringToPreventiveStopCriterion(jsonParser.nextTextValue()));
break;
case CURATIVE_STOP_CRITERION:
raoParameters.getObjectiveFunctionParameters().setCurativeStopCriterion(stringToCurativeStopCriterion(jsonParser.nextTextValue()));
break;
case CURATIVE_MIN_OBJ_IMPROVEMENT:
jsonParser.nextToken();
raoParameters.getObjectiveFunctionParameters().setCurativeMinObjImprovement(jsonParser.getValueAsDouble());
break;
case OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE:
jsonParser.nextToken();
raoParameters.getObjectiveFunctionParameters().setOptimizeCurativeIfPreventiveUnsecure(jsonParser.getBooleanValue());
raoParameters.getObjectiveFunctionParameters().setEnforceCurativeSecurity(jsonParser.getBooleanValue());
break;
default:
throw new OpenRaoException(String.format("Cannot deserialize objective function parameters: unexpected field in %s (%s)", OBJECTIVE_FUNCTION, jsonParser.getCurrentName()));
Expand All @@ -67,21 +59,4 @@ private static ObjectiveFunctionParameters.ObjectiveFunctionType stringToObjecti
throw new OpenRaoException(String.format("Unknown objective function type value: %s", string));
}
}

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));
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.powsybl.openrao.raoapi.parameters;

import com.powsybl.openrao.commons.Unit;
import com.powsybl.commons.config.PlatformConfig;

import java.util.Objects;
Expand All @@ -21,50 +20,19 @@
*/
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.SECURE_FLOW;
private static final double DEFAULT_CURATIVE_MIN_OBJ_IMPROVEMENT = 0;
private static final PreventiveStopCriterion DEFAULT_PREVENTIVE_STOP_CRITERION = PreventiveStopCriterion.SECURE;
private static final CurativeStopCriterion DEFAULT_CURATIVE_STOP_CRITERION = CurativeStopCriterion.MIN_OBJECTIVE;
private static final boolean DEFAULT_OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE = false;
private static final boolean 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 CurativeStopCriterion curativeStopCriterion = DEFAULT_CURATIVE_STOP_CRITERION;
private boolean optimizeCurativeIfPreventiveUnsecure = DEFAULT_OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE;
private boolean enforceCurativeSecurity = ENFORCE_CURATIVE_SECURITY;

// 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;
}

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

public enum PreventiveStopCriterion {
MIN_OBJECTIVE,
SECURE
}

public enum CurativeStopCriterion {
MIN_OBJECTIVE, // only stop after minimizing objective
SECURE, //stop when objective is strictly negative
PREVENTIVE_OBJECTIVE, // stop when preventive objective is reached, or bested by curativeRaoMinObjImprovement
PREVENTIVE_OBJECTIVE_AND_SECURE // stop when preventive objective is reached or bested by curativeRaoMinObjImprovement, and the situation is secure
SECURE_FLOW,
MAX_MIN_FLOW_MARGIN,
MAX_MIN_RELATIVE_FLOW_MARGIN
}

// Getters and setters
Expand All @@ -76,32 +44,16 @@ public void setType(ObjectiveFunctionType type) {
this.type = type;
}

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

public double getCurativeMinObjImprovement() {
return curativeMinObjImprovement;
}

public PreventiveStopCriterion getPreventiveStopCriterion() {
return preventiveStopCriterion;
}

public CurativeStopCriterion getCurativeStopCriterion() {
return curativeStopCriterion;
}

public void setCurativeStopCriterion(CurativeStopCriterion curativeStopCriterion) {
this.curativeStopCriterion = curativeStopCriterion;
}

public boolean getOptimizeCurativeIfPreventiveUnsecure() {
return optimizeCurativeIfPreventiveUnsecure;
public boolean getEnforceCurativeSecurity() {
return enforceCurativeSecurity;
}

public void setOptimizeCurativeIfPreventiveUnsecure(boolean optimizeCurativeIfPreventiveUnsecure) {
this.optimizeCurativeIfPreventiveUnsecure = optimizeCurativeIfPreventiveUnsecure;
public void setEnforceCurativeSecurity(boolean enforceCurativeSecurity) {
this.enforceCurativeSecurity = enforceCurativeSecurity;
}

public static ObjectiveFunctionParameters load(PlatformConfig platformConfig) {
Expand All @@ -112,11 +64,7 @@ public static ObjectiveFunctionParameters load(PlatformConfig platformConfig) {
parameters.setType(config.getEnumProperty(TYPE, ObjectiveFunctionType.class,
DEFAULT_OBJECTIVE_FUNCTION));
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.setCurativeStopCriterion(config.getEnumProperty(CURATIVE_STOP_CRITERION, CurativeStopCriterion.class,
DEFAULT_CURATIVE_STOP_CRITERION));
parameters.setOptimizeCurativeIfPreventiveUnsecure(config.getBooleanProperty(OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE, DEFAULT_OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE));
parameters.setEnforceCurativeSecurity(config.getBooleanProperty(OPTIMIZE_CURATIVE_IF_PREVENTIVE_UNSECURE, ENFORCE_CURATIVE_SECURITY));
});
return parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void roundTrip() throws IOException {
parameters.getObjectiveFunctionParameters().setPreventiveStopCriterion(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE);
parameters.getObjectiveFunctionParameters().setCurativeStopCriterion(ObjectiveFunctionParameters.CurativeStopCriterion.PREVENTIVE_OBJECTIVE_AND_SECURE);
parameters.getObjectiveFunctionParameters().setCurativeMinObjImprovement(983);
parameters.getObjectiveFunctionParameters().setOptimizeCurativeIfPreventiveUnsecure(true);
parameters.getObjectiveFunctionParameters().setEnforceCurativeSecurity(true);
// RangeActionsOptimization parameters
parameters.getRangeActionsOptimizationParameters().setMaxMipIterations(30);
parameters.getRangeActionsOptimizationParameters().setPstPenaltyCost(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void checkObjectiveFunctionConfig() {
assertEquals(123, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertEquals(ObjectiveFunctionParameters.CurativeStopCriterion.PREVENTIVE_OBJECTIVE, objectiveFunctionParameters.getCurativeStopCriterion());
assertTrue(objectiveFunctionParameters.getOptimizeCurativeIfPreventiveUnsecure());
assertTrue(objectiveFunctionParameters.getEnforceCurativeSecurity());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void testConfigWithoutExtensions() throws IOException {
assertEquals(3, objectiveFunctionParameters.getCurativeMinObjImprovement(), DOUBLE_TOLERANCE);
assertEquals(ObjectiveFunctionParameters.PreventiveStopCriterion.MIN_OBJECTIVE, objectiveFunctionParameters.getPreventiveStopCriterion());
assertEquals(ObjectiveFunctionParameters.CurativeStopCriterion.PREVENTIVE_OBJECTIVE, objectiveFunctionParameters.getCurativeStopCriterion());
assertTrue(objectiveFunctionParameters.getOptimizeCurativeIfPreventiveUnsecure());
assertTrue(objectiveFunctionParameters.getEnforceCurativeSecurity());

RangeActionsOptimizationParameters rangeActionsOptimizationParameters = parameters.getRangeActionsOptimizationParameters();
assertEquals(2, rangeActionsOptimizationParameters.getMaxMipIterations(), DOUBLE_TOLERANCE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_AMPERE",
"preventive-stop-criterion" : "MIN_OBJECTIVE",
"curative-stop-criterion" : "PREVENTIVE_OBJECTIVE_AND_SECURE",
"curative-min-obj-improvement" : 983.0,
"optimize-curative-if-preventive-unsecure" : true
"type" : "MAX_MIN_FLOW_MARGIN",
"enforce-curative-security" : true,
"curative-min-obj-improvement" : 983.0
},
"range-actions-optimization" : {
"max-mip-iterations" : 30,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_MEGAWATT",
"preventive-stop-criterion" : "WRONG",
"curative-stop-criterion" : "MIN_OBJECTIVE",
"type" : "MAX_MIN_FLOW_MARGIN",
"enforce-curative-security" : false,
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_MEGAWATT",
"preventive-stop-criterion" : "SECURE",
"curative-stop-criterion" : "MIN_OBJECTIVE",
"curative-min-obj-improvement" : 0.0,
"optimize-curative-if-preventive-unsecure" : false
"type" : "SECURE_FLOW",
"enforce-curative-security" : false,
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
"max-mip-iterations" : 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_MEGAWATT",
"preventive-stop-criterion" : "SECURE",
"curative-stop-criterion" : "MIN_OBJECTIVE",
"type" : "SECURE_FLOW",
"enforce-curative-security" : false,
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_MEGAWATT",
"preventive-stop-criterion" : "SECURE",
"curative-stop-criterion" : "WRONG",
"type" : "SECURE_FLOW",
"enforce-curative-security" : false,
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"version" : "2.4",
"objective-function" : {
"type" : "MAX_MIN_MARGIN_IN_MEGAWATT",
"preventive-stop-criterion" : "SECURE",
"curative-stop-criterion" : "MIN_OBJECTIVE",
"type" : "SECURE_FLOW",
"enforce-curative-security" : false,
"curative-min-obj-improvement" : 0.0
},
"range-actions-optimization" : {
Expand Down
Loading

0 comments on commit ddbef67

Please sign in to comment.