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 INTERNSHIP] Add cost as new objective function for linear problem #1133

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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 @@ -76,4 +76,6 @@ default Set<Optional<Country>> getLocation(Network network) {
}

OnContingencyStateAdderToRemedialAction<I> newOnStateUsageRule();

double getActivationCost();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface RemedialActionAdder<T extends RemedialActionAdder<T>> extends I

T withSpeed(Integer speed);

T withActivationCost(double activationCost);

RemedialAction<?> add();

OnInstantAdder<T> newOnInstantUsageRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public String getId() {
public static class NetworkActionImplTest implements NetworkAction {

private final Set<Action> elementaryActions;
private final double activationCost = 0;

public NetworkActionImplTest(Set<Action> elementaryActions) {
this.elementaryActions = new HashSet<>(elementaryActions);
Expand Down Expand Up @@ -97,6 +98,11 @@ public UsageMethod getUsageMethod(State state) {
return null;
}

@Override
public double getActivationCost() {
return activationCost;
}

@Override
public Optional<Integer> getSpeed() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public abstract class AbstractRangeAction<T extends RangeAction<T>> extends Abst
this.groupId = groupId;
}

AbstractRangeAction(String id, String name, String operator, Set<UsageRule> usageRules, String groupId, Integer speed, double activationCost) {
super(id, name, operator, usageRules, speed, activationCost);
this.groupId = groupId;
}

@Override
public Optional<String> getGroupId() {
return Optional.ofNullable(groupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class AbstractRemedialAction<I extends RemedialAction<I>> extend
private boolean computedUsageMethods = false;
private Map<State, UsageMethod> usageMethodPerState;
private Map<Instant, UsageMethod> usageMethodPerInstant;
private double activationCost = 0;

protected AbstractRemedialAction(String id, String name, String operator, Set<UsageRule> usageRules, Integer speed) {
super(id, name);
Expand All @@ -40,6 +41,14 @@ protected AbstractRemedialAction(String id, String name, String operator, Set<Us
this.speed = speed;
}

protected AbstractRemedialAction(String id, String name, String operator, Set<UsageRule> usageRules, Integer speed, double activationCost) {
super(id, name);
this.operator = operator;
this.usageRules = usageRules;
this.speed = speed;
this.activationCost = activationCost;
}

void addUsageRule(UsageRule usageRule) {
computedUsageMethods = false;
this.usageRules.add(usageRule);
Expand Down Expand Up @@ -79,6 +88,11 @@ public UsageMethod getUsageMethod(State state) {
usageMethodPerInstant.getOrDefault(state.getInstant(), UsageMethod.UNDEFINED)));
}

@Override
public double getActivationCost() {
return activationCost;
}

private void computeUsageMethodPerStateAndInstant() {
usageMethodPerState = new HashMap<>();
usageMethodPerInstant = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
*/
public abstract class AbstractRemedialActionAdder<T extends RemedialActionAdder<T>> extends AbstractIdentifiableAdder<T> implements RemedialActionAdder<T> {

private final CracImpl crac;
protected String operator;
protected Integer speed;
protected Set<UsageRule> usageRules = new HashSet<>();
private final CracImpl crac;
protected double activationCost;

AbstractRemedialActionAdder(CracImpl crac) {
Objects.requireNonNull(crac);
Expand All @@ -42,6 +43,12 @@ public T withSpeed(Integer speed) {
return (T) this;
}

@Override
public T withActivationCost(double activationCost) {
this.activationCost = activationCost;
return (T) this;
}

@Override
public OnInstantAdder<T> newOnInstantUsageRule() {
return new OnInstantAdderImpl(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public CounterTradeRangeAction add() {
BUSINESS_WARNS.warn("CounterTradeRangeAction {} does not contain any usage rule, by default it will never be available", id);
}

CounterTradeRangeAction counterTradeRangeAction = new CounterTradeRangeActionImpl(this.id, this.name, this.operator, this.groupId, this.usageRules, this.ranges, this.initialSetpoint, speed, this.exportingCountry, this.importingCountry);
CounterTradeRangeAction counterTradeRangeAction = new CounterTradeRangeActionImpl(this.id, this.name, this.operator, this.groupId, this.usageRules, this.ranges, this.initialSetpoint, speed, this.exportingCountry, this.importingCountry, activationCost);
getCrac().addCounterTradeRangeAction(counterTradeRangeAction);
return counterTradeRangeAction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public class CounterTradeRangeActionImpl extends AbstractRangeAction<CounterTrad
this.importingCountry = importingCountry;
}

CounterTradeRangeActionImpl(String id, String name, String operator, String groupId, Set<UsageRule> usageRules,
List<StandardRange> ranges, double initialSetpoint, Integer speed, Country exportingCountry, Country importingCountry, double activationCost) {
super(id, name, operator, usageRules, groupId, speed, activationCost);
this.ranges = ranges;
this.initialSetpoint = initialSetpoint;
this.exportingCountry = exportingCountry;
this.importingCountry = importingCountry;
}

@Override
public List<StandardRange> getRanges() {
return ranges;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public HvdcRangeAction add() {
}

NetworkElement networkElement = this.getCrac().addNetworkElement(networkElementId, networkElementName);
HvdcRangeActionImpl hvdcWithRange = new HvdcRangeActionImpl(this.id, this.name, this.operator, this.usageRules, ranges, initialSetpoint, networkElement, groupId, speed);
HvdcRangeActionImpl hvdcWithRange = new HvdcRangeActionImpl(this.id, this.name, this.operator, this.usageRules, ranges, initialSetpoint, networkElement, groupId, speed, activationCost);
this.getCrac().addHvdcRangeAction(hvdcWithRange);
return hvdcWithRange;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public class HvdcRangeActionImpl extends AbstractRangeAction<HvdcRangeAction> im
this.initialSetpoint = initialSetpoint;
}

HvdcRangeActionImpl(String id, String name, String operator, Set<UsageRule> usageRules, List<StandardRange> ranges,
double initialSetpoint, NetworkElement networkElement, String groupId, Integer speed, double activationCost) {
super(id, name, operator, usageRules, groupId, speed, activationCost);
this.networkElement = networkElement;
this.ranges = ranges;
this.initialSetpoint = initialSetpoint;
}

@Override
public NetworkElement getNetworkElement() {
return networkElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public InjectionRangeAction add() {
}

Map<NetworkElement, Double> neAndDk = addNetworkElements();
InjectionRangeAction injectionRangeAction = new InjectionRangeActionImpl(this.id, this.name, this.operator, this.groupId, this.usageRules, this.ranges, this.initialSetpoint, neAndDk, speed);
InjectionRangeAction injectionRangeAction = new InjectionRangeActionImpl(this.id, this.name, this.operator, this.groupId, this.usageRules, this.ranges, this.initialSetpoint, neAndDk, speed, activationCost);
this.getCrac().addInjectionRangeAction(injectionRangeAction);
return injectionRangeAction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public class InjectionRangeActionImpl extends AbstractRangeAction<InjectionRange
this.injectionDistributionKeys = injectionDistributionKeys;
}

InjectionRangeActionImpl(String id, String name, String operator, String groupId, Set<UsageRule> usageRules,
List<StandardRange> ranges, double initialSetpoint, Map<NetworkElement, Double> injectionDistributionKeys, Integer speed, double activationCost) {
super(id, name, operator, usageRules, groupId, speed, activationCost);
this.ranges = ranges;
this.initialSetpoint = initialSetpoint;
this.injectionDistributionKeys = injectionDistributionKeys;
}

@Override
public Set<NetworkElement> getNetworkElements() {
return injectionDistributionKeys.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public PstRangeAction add() {
}

NetworkElement networkElement = this.getCrac().addNetworkElement(networkElementId, networkElementName);
PstRangeActionImpl pstWithRange = new PstRangeActionImpl(this.id, this.name, this.operator, this.usageRules, validRanges, networkElement, groupId, initialTap, tapToAngleConversionMap, speed);
PstRangeActionImpl pstWithRange = new PstRangeActionImpl(this.id, this.name, this.operator, this.usageRules, validRanges, networkElement, groupId, initialTap, tapToAngleConversionMap, speed, activationCost);
this.getCrac().addPstRangeAction(pstWithRange);
return pstWithRange;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public final class PstRangeActionImpl extends AbstractRangeAction<PstRangeAction
this.smallestAngleStep = computeSmallestAngleStep();
}

PstRangeActionImpl(String id, String name, String operator, Set<UsageRule> usageRules, List<TapRange> ranges,
NetworkElement networkElement, String groupId, int initialTap, Map<Integer, Double> tapToAngleConversionMap, Integer speed, double activationCost) {
super(id, name, operator, usageRules, groupId, speed, activationCost);
this.networkElement = networkElement;
this.ranges = ranges;
this.initialTapPosition = initialTap;
this.tapToAngleConversionMap = tapToAngleConversionMap;
this.lowTapPosition = Collections.min(tapToAngleConversionMap.keySet());
this.highTapPosition = Collections.max(tapToAngleConversionMap.keySet());
this.smallestAngleStep = computeSmallestAngleStep();
}

@Override
public NetworkElement getNetworkElement() {
return networkElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private JsonSerializationConstants() {

public static final String GROUP_ID = "groupId";
public static final String SPEED = "speed";
public static final String COST = "cost";

public static final String CONTINGENCIES = "contingencies";
public static final String CONTINGENCY_ID = "contingencyId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public static void deserialize(JsonParser jsonParser, String version, Crac crac,
jsonParser.nextToken();
pstRangeActionAdder.withSpeed(jsonParser.getIntValue());
break;
case COST:
jsonParser.nextToken();
pstRangeActionAdder.withActivationCost(jsonParser.getDoubleValue());
break;
default:
throw new OpenRaoException("Unexpected field in PstRangeAction: " + jsonParser.getCurrentName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public static boolean addCommonElement(StandardRangeActionAdder<?> standardRange
jsonParser.nextToken();
standardRangeActionAdder.withSpeed(jsonParser.getIntValue());
break;
case COST:
jsonParser.nextToken();
standardRangeActionAdder.withActivationCost(jsonParser.getDoubleValue());
break;
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void serialize(PstRangeAction value, JsonGenerator gen, SerializerProvide
gen.writeObjectField(TAP_TO_ANGLE_CONVERSION_MAP, value.getTapToAngleConversionMap());
serializeRemedialActionSpeed(value, gen);
serializeRanges(value, gen);
gen.writeNumberField(COST, value.getActivationCost());
gen.writeEndObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static void serializeCommon(StandardRangeAction<?> value, JsonGenerator g
serializeGroupId(value, gen);
gen.writeNumberField(INITIAL_SETPOINT, value.getInitialSetpoint());
serializeRanges(value, gen);
gen.writeNumberField(COST, value.getActivationCost());
}

private static void serializeGroupId(StandardRangeAction<?> value, JsonGenerator gen) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.powsybl.openrao.data.cracapi.range.RangeType;
import com.powsybl.openrao.data.cracapi.range.StandardRange;
import com.powsybl.openrao.data.cracapi.range.TapRange;
import com.powsybl.openrao.data.cracapi.rangeaction.PstRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.RangeAction;
import com.powsybl.openrao.data.cracapi.threshold.BranchThreshold;
import com.powsybl.openrao.data.cracapi.threshold.Threshold;
Expand Down Expand Up @@ -746,23 +747,27 @@ private void testContentOfV2Point2Crac(Crac crac) {
private void testContentOfV2Point3Crac(Crac crac) {
testContentOfV2Point2Crac(crac);

PstRangeAction pstRangeAction4 = crac.getPstRangeAction("pstRange4Id");
// check that RangeAction4 is present with new range relative to previous instant
assertNotNull(crac.getRangeAction("pstRange4Id"));
assertEquals(2, crac.getPstRangeAction("pstRange4Id").getRanges().size());
TapRange absRange = crac.getPstRangeAction("pstRange4Id").getRanges().stream()
.filter(tapRange -> tapRange.getRangeType().equals(RangeType.ABSOLUTE))
.findAny().orElse(null);
TapRange relTimeStepRange = crac.getPstRangeAction("pstRange4Id").getRanges().stream()
.filter(tapRange -> tapRange.getRangeType().equals(RangeType.RELATIVE_TO_PREVIOUS_TIME_STEP))
.findAny().orElse(null);
assertNotNull(pstRangeAction4);

assertNotNull(absRange);
assertEquals(-2, absRange.getMinTap());
assertEquals(7, absRange.getMaxTap());
assertNotNull(relTimeStepRange);
assertEquals(-1, relTimeStepRange.getMinTap());
assertEquals(4, relTimeStepRange.getMaxTap());
assertEquals(Unit.TAP, relTimeStepRange.getUnit());
// check Tap Range
assertEquals(2, pstRangeAction4.getRanges().size());

TapRange pstAbsRange = pstRangeAction4.getRanges().stream()
.filter(tapRange -> tapRange.getRangeType().equals(RangeType.ABSOLUTE))
.findAny().orElse(null);
TapRange pstRelTimeStepRange = pstRangeAction4.getRanges().stream()
.filter(tapRange -> tapRange.getRangeType().equals(RangeType.RELATIVE_TO_PREVIOUS_TIME_STEP))
.findAny().orElse(null);
assertNotNull(pstAbsRange);
assertEquals(-2, pstAbsRange.getMinTap());
assertEquals(7, pstAbsRange.getMaxTap());
assertNotNull(pstRelTimeStepRange);
assertEquals(-1, pstRelTimeStepRange.getMinTap());
assertEquals(4, pstRelTimeStepRange.getMaxTap());
assertEquals(Unit.TAP, pstRelTimeStepRange.getUnit());
assertEquals(3.2, pstRangeAction4.getActivationCost());

// check new border attribute
assertEquals("border1", crac.getCnec("cnec1outageId").getBorder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
"min" : -1,
"max" : 4,
"rangeType" : "relativeToPreviousTimeStep"
} ]
} ],
"cost" : 3.2
} ],
"hvdcRangeActions" : [ {
"id" : "hvdcRange1Id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
"min" : -1,
"max" : 4,
"rangeType" : "relativeToPreviousTimeStep"
} ]
} ],
"cost" : 3.2
} ],
"hvdcRangeActions" : [ {
"id" : "hvdcRange1Id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@
"min" : -1,
"max" : 4,
"rangeType" : "relativeToPreviousTimeStep"
} ]
} ],
"cost" : 3.2
} ],
"hvdcRangeActions" : [ {
"id" : "hvdcRange1Id",
Expand Down
1 change: 1 addition & 0 deletions docs/castor/linear-problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
linear-problem/core-problem-filler.md
linear-problem/max-min-margin-filler.md
linear-problem/max-min-relative-margin-filler.md
linear-problem/min-cost-filler.md
linear-problem/max-loop-flow-filler.md
linear-problem/mnec-filler.md
linear-problem/continuous-range-action-group-filler.md
Expand Down
Loading
Loading