Skip to content

Commit

Permalink
complete PowerGradientConstraintFiller + fix style
Browse files Browse the repository at this point in the history
Signed-off-by: CHEN Roxane <[email protected]>
  • Loading branch information
CHEN Roxane committed Jan 8, 2025
1 parent fdf58bd commit 23ab854
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
*/
public class PowerGradientConstraint {
public final class PowerGradientConstraint {
private final String networkElementId;
private final Double minPowerGradient;
private final Double maxPowerGradient;
Expand All @@ -45,7 +45,7 @@ public static PowerGradientConstraintBuilder builder() {
return new PowerGradientConstraintBuilder();
}

static public class PowerGradientConstraintBuilder {
public static final class PowerGradientConstraintBuilder {
private String networkElementId;
private Double minPowerGradient;
private Double maxPowerGradient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.crac.api.rangeaction.VariationDirection;
import org.junit.jupiter.api.Test;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.powsybl.openrao.commons.TemporalData;
import com.powsybl.openrao.commons.TemporalDataImpl;
import com.powsybl.openrao.data.crac.api.rangeaction.VariationDirection;
import com.powsybl.openrao.data.intertemporalconstraint.PowerGradientConstraint;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.crac.api.NetworkElement;
import com.powsybl.openrao.data.crac.api.rangeaction.InjectionRangeAction;
import com.powsybl.openrao.data.crac.api.usagerule.UsageMethod;
import com.powsybl.openrao.data.intertemporalconstraint.PowerGradientConstraint;
import com.powsybl.openrao.raoapi.InterTemporalRaoInput;
import com.powsybl.openrao.raoapi.RaoInput;
import com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.linearproblem.LinearProblem;
Expand All @@ -25,6 +27,9 @@

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
Expand All @@ -38,52 +43,44 @@ public PowerGradientConstraintFiller(InterTemporalRaoInput input) {

@Override
public void fill(LinearProblem linearProblem, FlowResult flowResult, SensitivityResult sensitivityResult, RangeActionActivationResult rangeActionActivationResult) {
input.getPowerGradientConstraints().forEach(
powerGradientConstraint -> input.getRaoInputs().getTimestamps().forEach(
timestamp -> linearProblem.addGeneratorPowerVariable(powerGradientConstraint.getNetworkElementId(), timestamp)
)
);

List<OffsetDateTime> timestampsToRun = input.getTimestampsToRun().stream().sorted().toList();
int numberOfTimestamps = input.getTimestampsToRun().size();

for (int timestampIndex = 1; timestampIndex < numberOfTimestamps; timestampIndex++) {
OffsetDateTime currentTimestamp = timestampsToRun.get(timestampIndex);
OffsetDateTime previousTimestamp = timestampsToRun.get(timestampIndex - 1);
RaoInput raoInput = input.getRaoInputs().getData(currentTimestamp).orElseThrow();

input.getPowerGradientConstraints().forEach(powerGradientConstraint -> {
OpenRaoMPVariable currentGeneratorPowerVariable = linearProblem.getGeneratorPowerVariable(powerGradientConstraint.getNetworkElementId(), currentTimestamp);
OpenRaoMPVariable previousGeneratorPowerVariable = linearProblem.getGeneratorPowerVariable(powerGradientConstraint.getNetworkElementId(), previousTimestamp);

OpenRaoMPConstraint generatorPowerConstraint = linearProblem.addGeneratorPowerConstraint(powerGradientConstraint.getNetworkElementId(), getP0(powerGradientConstraint.getNetworkElementId(), raoInput.getNetwork()), currentTimestamp);
generatorPowerConstraint.setCoefficient(currentGeneratorPowerVariable, 1.0);
generatorPowerConstraint.setCoefficient(previousGeneratorPowerVariable, -1.0);
List<OffsetDateTime> timestamps = input.getRaoInputs().getTimestamps();
for (int timestampIndex = 0; timestampIndex < timestamps.size(); timestampIndex++) {
OffsetDateTime timestamp = timestamps.get(timestampIndex);
RaoInput raoInput = input.getRaoInputs().getData(timestamp).orElseThrow();
Set<InjectionRangeAction> preventiveInjectionRangeActions = raoInput.getCrac().getRangeActions(raoInput.getCrac().getPreventiveState(), UsageMethod.AVAILABLE)
.stream().filter(InjectionRangeAction.class::isInstance).map(InjectionRangeAction.class::cast).collect(Collectors.toSet());
int finalTimestampIndex = timestampIndex;
input.getPowerGradientConstraints().forEach(constraint -> {
String generatorId = constraint.getNetworkElementId();
OpenRaoMPVariable generatorPowerVariable = linearProblem.addGeneratorPowerVariable(generatorId, timestamp);
addPowerConstraint(linearProblem, preventiveInjectionRangeActions, raoInput, generatorId, generatorPowerVariable, timestamp);
if (finalTimestampIndex > 0) {
addPowerGradientConstraint(linearProblem, constraint, timestamp, timestamps.get(finalTimestampIndex - 1), generatorPowerVariable);
}
});
}
}

// TODO: optimize this
raoInput.getCrac().getRangeActions(raoInput.getCrac().getPreventiveState(), UsageMethod.AVAILABLE)
.stream()
.filter(InjectionRangeAction.class::isInstance)
.map(InjectionRangeAction.class::cast)
.filter(injectionRangeAction -> injectionRangeAction.getNetworkElements().stream().anyMatch(networkElement -> powerGradientConstraint.getNetworkElementId().equals(networkElement.getId())))
.forEach(injectionRangeAction -> {
OpenRaoMPVariable upwardSetPointVariationVariable = linearProblem.getRangeActionVariationVariable(injectionRangeAction, raoInput.getCrac().getPreventiveState(), LinearProblem.VariationDirectionExtension.UPWARD); // TODO: add timestamp
OpenRaoMPVariable downwardSetPointVariationVariable = linearProblem.getRangeActionVariationVariable(injectionRangeAction, raoInput.getCrac().getPreventiveState(), LinearProblem.VariationDirectionExtension.DOWNWARD); // TODO: add timestamp
double distributionKey = 0;
generatorPowerConstraint.setCoefficient(upwardSetPointVariationVariable, -distributionKey);
generatorPowerConstraint.setCoefficient(downwardSetPointVariationVariable, distributionKey);
});
private static void addPowerGradientConstraint(LinearProblem linearProblem, PowerGradientConstraint constraint, OffsetDateTime currentTimestamp, OffsetDateTime previousTimestamp, OpenRaoMPVariable generatorPowerVariable) {
OpenRaoMPVariable previousGeneratorPowerVariable = linearProblem.addGeneratorPowerVariable(constraint.getNetworkElementId(), previousTimestamp);
OpenRaoMPConstraint generatorPowerGradientConstraint = linearProblem.addGeneratorPowerGradientConstraint(constraint, currentTimestamp, previousTimestamp);
generatorPowerGradientConstraint.setCoefficient(generatorPowerVariable, 1.0);
generatorPowerGradientConstraint.setCoefficient(previousGeneratorPowerVariable, -1.0);
}

OpenRaoMPConstraint interTemporalPowerGradientConstraint = linearProblem.addGeneratorPowerGradientConstraint(
powerGradientConstraint.getNetworkElementId(),
powerGradientConstraint.getMinPowerGradient().orElse(-linearProblem.infinity()),
powerGradientConstraint.getMaxPowerGradient().orElse(linearProblem.infinity()),
currentTimestamp,
previousTimestamp);
interTemporalPowerGradientConstraint.setCoefficient(currentGeneratorPowerVariable, 1.0);
interTemporalPowerGradientConstraint.setCoefficient(previousGeneratorPowerVariable, -1.0);
private static void addPowerConstraint(LinearProblem linearProblem, Set<InjectionRangeAction> preventiveInjectionRangeActions, RaoInput raoInput, String generatorId, OpenRaoMPVariable generatorPowerVariable, OffsetDateTime timestamp) {
OpenRaoMPConstraint generatorPowerConstraint = linearProblem.addGeneratorPowerConstraint(generatorId, getP0(generatorId, raoInput.getNetwork()), timestamp);
generatorPowerConstraint.setCoefficient(generatorPowerVariable, 1.0);
preventiveInjectionRangeActions.stream()
.filter(injectionRangeAction -> injectionRangeAction.getInjectionDistributionKeys().keySet().stream().map(NetworkElement::getId).anyMatch(networkElementId -> generatorId.equals(networkElementId)))
.forEach(injectionRangeAction -> {
double injectionKey = injectionRangeAction.getInjectionDistributionKeys().entrySet().stream().filter(entry -> generatorId.equals(entry.getKey().getId())).map(Map.Entry::getValue).findFirst().get();
// TODO: Handle timestamp !
OpenRaoMPVariable upwardVariationVariable = linearProblem.getRangeActionVariationVariable(injectionRangeAction, raoInput.getCrac().getPreventiveState(), LinearProblem.VariationDirectionExtension.UPWARD);
OpenRaoMPVariable downwardVariationVariable = linearProblem.getRangeActionVariationVariable(injectionRangeAction, raoInput.getCrac().getPreventiveState(), LinearProblem.VariationDirectionExtension.DOWNWARD);
generatorPowerConstraint.setCoefficient(upwardVariationVariable, -injectionKey);
generatorPowerConstraint.setCoefficient(downwardVariationVariable, injectionKey);
});
}
}

private static double getP0(String generatorId, Network network) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.openrao.data.crac.api.rangeaction.PstRangeAction;
import com.powsybl.openrao.data.crac.api.rangeaction.RangeAction;
import com.powsybl.openrao.data.intertemporalconstraint.PowerGradientConstraint;
import com.powsybl.openrao.raoapi.parameters.RangeActionsOptimizationParameters;
import com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.fillers.ProblemFiller;
import com.powsybl.openrao.searchtreerao.result.api.FlowResult;
Expand Down Expand Up @@ -486,9 +487,12 @@ public OpenRaoMPConstraint getGeneratorPowerConstraint(String generatorId, Offse
return solver.getConstraint(generatorPowerConstraintId(generatorId, timestamp));
}

public OpenRaoMPConstraint addGeneratorPowerGradientConstraint(String generatorId, double minPowerGradient, double maxPowerGradient, OffsetDateTime currentTimestamp, OffsetDateTime previousTimestamp) {
public OpenRaoMPConstraint addGeneratorPowerGradientConstraint(PowerGradientConstraint powerGradientConstraint, OffsetDateTime currentTimestamp, OffsetDateTime previousTimestamp) {
double timeGap = previousTimestamp.until(currentTimestamp, ChronoUnit.HOURS);
return solver.makeConstraint(timeGap * minPowerGradient, timeGap * maxPowerGradient, generatorPowerGradientConstraintId(generatorId, currentTimestamp, previousTimestamp));
double lb = powerGradientConstraint.getMinPowerGradient().isPresent() ? powerGradientConstraint.getMinPowerGradient().get() * timeGap : -infinity();
double ub = powerGradientConstraint.getMaxPowerGradient().isPresent() ? powerGradientConstraint.getMaxPowerGradient().get() * timeGap : infinity();
String generatorId = powerGradientConstraint.getNetworkElementId();
return solver.makeConstraint(lb, ub, generatorPowerGradientConstraintId(generatorId, currentTimestamp, previousTimestamp));
}

public OpenRaoMPConstraint getGeneratorPowerGradientConstraint(String generatorId, OffsetDateTime currentTimestamp, OffsetDateTime previousTimestamp) {
Expand Down

0 comments on commit 23ab854

Please sign in to comment.