Skip to content

Commit

Permalink
Add minValue and maxValue to compound scalables (#2720)
Browse files Browse the repository at this point in the history
* added minValue and maxValue to compound scalables, and bound the scaling with these values
* added UTs and fixed mistakes in scaling
* fix checkstyle
* added javadoc

Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com>

---------

Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com>
Signed-off-by: Nicolas Rol <135979730+rolnico@users.noreply.github.com>
Signed-off-by: Olivier Perrin <olivier.perrin@rte-france.com>
Co-authored-by: Philippe Edwards <philippe.edwards@rte-france.com>
Co-authored-by: Nicolas Rol <135979730+rolnico@users.noreply.github.com>
Co-authored-by: Olivier Perrin <olivier.perrin@rte-france.com>
  • Loading branch information
4 people authored Oct 25, 2023
1 parent b0c8cbd commit ecb61c4
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
abstract class AbstractCompoundScalable extends AbstractScalable {
protected double minValue = -Double.MAX_VALUE;
protected double maxValue = Double.MAX_VALUE;

abstract Collection<Scalable> getScalables();

Expand Down Expand Up @@ -52,7 +54,7 @@ public double maximumValue(Network n, ScalingConvention powerConvention) {
for (Scalable scalable : getScalables()) {
value += scalable.maximumValue(n, powerConvention);
}
return value;
return (powerConvention == ScalingConvention.GENERATOR) ? Math.min(maxValue, value) : Math.min(-minValue, value);
}

@Override
Expand All @@ -68,7 +70,7 @@ public double minimumValue(Network n, ScalingConvention powerConvention) {
for (Scalable scalable : getScalables()) {
value += scalable.minimumValue(n, powerConvention);
}
return value;
return (powerConvention == ScalingConvention.GENERATOR) ? Math.max(minValue, value) : Math.max(-maxValue, value);
}

@Override
Expand All @@ -77,4 +79,17 @@ public void filterInjections(Network n, List<Injection> injections, List<String>
scalable.filterInjections(n, injections, notFoundInjections);
}
}

/**
* Returns the value of scaling asked, bounded by the minValue and maxValue.
* @param variationAsked unbounded value of scaling asked on the scalable
* @param currentGlobalPower current global power in the network
* @param scalingConvention This is required because the minValue and maxValue are in GENERATOR convention, so we need to know wwhat convention we use for the scaling.
* @return the value of scaling asked bounded by the minValue and maxValue, according to the scalingConvention.
*/
protected double getBoundedVariation(double variationAsked, double currentGlobalPower, ScalingConvention scalingConvention) {
double minWithConvention = scalingConvention == ScalingConvention.GENERATOR ? minValue : -maxValue;
double maxWithConvention = scalingConvention == ScalingConvention.GENERATOR ? maxValue : -minValue;
return Math.min(maxWithConvention - currentGlobalPower, Math.max(minWithConvention - currentGlobalPower, variationAsked));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,24 @@ void setIterationPercentage(double iterationPercentage) {
private final List<ScalablePercentage> scalablePercentageList;

ProportionalScalable(List<Double> percentages, List<Scalable> scalables) {
this(percentages, scalables, -Double.MAX_VALUE, Double.MAX_VALUE);
}

ProportionalScalable(List<Double> percentages, List<Scalable> scalables, double minValue, double maxValue) {
checkPercentages(percentages, scalables);
this.scalablePercentageList = new ArrayList<>();
for (int i = 0; i < scalables.size(); i++) {
this.scalablePercentageList.add(new ScalablePercentage(scalables.get(i), percentages.get(i)));
}
this.minValue = minValue;
this.maxValue = maxValue;
}

public ProportionalScalable(List<? extends Injection> injections, DistributionMode distributionMode) {
this(injections, distributionMode, -Double.MAX_VALUE, Double.MAX_VALUE);
}

public ProportionalScalable(List<? extends Injection> injections, DistributionMode distributionMode, double minValue, double maxValue) {
// Create the scalable for each injection
List<Scalable> injectionScalables = injections.stream().map(ScalableAdapter::new).collect(Collectors.toList());

Expand Down Expand Up @@ -117,6 +127,9 @@ public ProportionalScalable(List<? extends Injection> injections, DistributionMo
for (int i = 0; i < injectionScalables.size(); i++) {
this.scalablePercentageList.add(new ScalablePercentage(injectionScalables.get(i), percentages.get(i)));
}

this.minValue = minValue;
this.maxValue = maxValue;
}

private double computeTotalDistribution(List<? extends Injection> injections, DistributionMode distributionMode) {
Expand Down Expand Up @@ -263,16 +276,18 @@ public double scale(Network n, double asked, ScalingParameters parameters) {
// Variation asked
double variationAsked = Scalable.getVariationAsked(parameters, asked, currentGlobalPower);

double boundedVariation = getBoundedVariation(variationAsked, currentGlobalPower, parameters.getScalingConvention());

// Adapt the asked value if needed - only used in RESPECT_OF_DISTRIBUTION mode
if (parameters.getPriority() == RESPECT_OF_DISTRIBUTION) {
variationAsked = resizeAskedForFixedDistribution(n, variationAsked, parameters);
boundedVariation = resizeAskedForFixedDistribution(n, boundedVariation, parameters);
}

reinitIterationPercentage();
if (parameters.getPriority() == RESPECT_OF_VOLUME_ASKED) {
return iterativeScale(n, variationAsked, parameters);
return iterativeScale(n, boundedVariation, parameters);
} else {
return scaleIteration(n, variationAsked, parameters);
return scaleIteration(n, boundedVariation, parameters);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,18 @@ static ProportionalScalable proportional(List<? extends Injection> injections, P
return new ProportionalScalable(injections, distributionMode);
}

static ProportionalScalable proportional(List<? extends Injection> injections, ProportionalScalable.DistributionMode distributionMode, double minValue, double maxValue) {
return new ProportionalScalable(injections, distributionMode, minValue, maxValue);
}

static ProportionalScalable proportional(List<Double> percentages, List<Scalable> scalables) {
return new ProportionalScalable(percentages, scalables);
}

static ProportionalScalable proportional(List<Double> percentages, List<Scalable> scalables, double minValue, double maxValue) {
return new ProportionalScalable(percentages, scalables, minValue, maxValue);
}

static ProportionalScalable proportional(double percentage, Scalable scalable) {
return new ProportionalScalable(Collections.singletonList(percentage), Collections.singletonList(scalable));
}
Expand Down Expand Up @@ -227,28 +235,52 @@ static StackScalable stack(Injection<?>... injections) {
return new StackScalable(injectionScalables);
}

static StackScalable stack(double minValue, double maxValue, Injection<?>... injections) {
List<Scalable> injectionScalables = Arrays.stream(injections).map(ScalableAdapter::new).collect(Collectors.toList());
return new StackScalable(injectionScalables, minValue, maxValue);
}

static StackScalable stack(List<? extends Injection<?>> injections) {
List<Scalable> injectionScalables = injections.stream().map(ScalableAdapter::new).collect(Collectors.toList());
return new StackScalable(injectionScalables);
}

static StackScalable stack(List<? extends Injection<?>> injections, double minValue, double maxValue) {
List<Scalable> injectionScalables = injections.stream().map(ScalableAdapter::new).collect(Collectors.toList());
return new StackScalable(injectionScalables, minValue, maxValue);
}

static StackScalable stack(Scalable... scalables) {
return new StackScalable(scalables);
}

static StackScalable stack(double minValue, double maxValue, Scalable... scalables) {
return new StackScalable(minValue, maxValue, scalables);
}

static StackScalable stack(String... ids) {
List<Scalable> identifierScalables = Arrays.stream(ids).map(ScalableAdapter::new).collect(Collectors.toList());
return new StackScalable(identifierScalables);
}

static StackScalable stack(double minValue, double maxValue, String... ids) {
List<Scalable> identifierScalables = Arrays.stream(ids).map(ScalableAdapter::new).collect(Collectors.toList());
return new StackScalable(identifierScalables, minValue, maxValue);
}

static UpDownScalable upDown(Scalable upScalable, Scalable downScalable) {
return new UpDownScalable(upScalable, downScalable);
}

static UpDownScalable upDown(Scalable upScalable, Scalable downScalable, double minValue, double maxValue) {
return new UpDownScalable(upScalable, downScalable, minValue, maxValue);
}

/**
* Returns the value that has to be added to the network, depending on the type of variation chosen in the parameters
* @param scalingParameters Scaling parameters including a variation type (DELTA_P or TARGET_P) and a variation value
* @param currentGlobalPower current global power
* @param scalingParameters Scaling parameters including a variation type (DELTA_P or TARGET_P)
* @param askedValue value of scaling asked on the scalable
* @param currentGlobalPower current global power in the network
* @return the variation value if the type is DELTA_P, else the difference between the variation value and the current global value sum
*/
static double getVariationAsked(ScalingParameters scalingParameters, double askedValue, double currentGlobalPower) {
Expand All @@ -260,6 +292,8 @@ static double getVariationAsked(ScalingParameters scalingParameters, double aske
/**
* Returns the current power value for the injections corresponding to this Scalable
* @param network Network in which the injections are defined
* @param asked value of scaling asked on the scalable. This is used to know in which direction we want to scale for UpDownScalables.
* @param scalingConvention The value is computed either with Generator or Load convention according to this parameter.
* @return the current power value
*/
double getSteadyStatePower(Network network, double asked, ScalingConvention scalingConvention);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ class StackScalable extends AbstractCompoundScalable {
private final List<Scalable> scalables;

StackScalable(Scalable... scalables) {
this(Arrays.asList(scalables));
this(Arrays.asList(scalables), -Double.MAX_VALUE, Double.MAX_VALUE);
}

StackScalable(double minValue, double maxValue, Scalable... scalables) {
this(Arrays.asList(scalables), minValue, maxValue);
}

StackScalable(List<Scalable> scalables) {
this(scalables, -Double.MAX_VALUE, Double.MAX_VALUE);
}

StackScalable(List<Scalable> scalables, double minValue, double maxValue) {
this.scalables = Objects.requireNonNull(scalables);
this.minValue = minValue;
this.maxValue = maxValue;
}

@Override
Expand All @@ -44,8 +54,10 @@ public double scale(Network n, double asked, ScalingParameters parameters) {
// Variation asked
double variationAsked = Scalable.getVariationAsked(parameters, asked, currentGlobalPower);

double boundedVariation = getBoundedVariation(variationAsked, currentGlobalPower, parameters.getScalingConvention());

double done = 0;
double remaining = variationAsked;
double remaining = boundedVariation;
for (Scalable scalable : scalables) {
if (Math.abs(remaining) > EPSILON) {
double v = scalable.scale(n, remaining, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
class UpDownScalable extends AbstractScalable {
private final Scalable upScalable;
private final Scalable downScalable;
private final double minValue;
private final double maxValue;

public UpDownScalable(Scalable upScalable, Scalable downScalable) {
this(upScalable, downScalable, -Double.MAX_VALUE, Double.MAX_VALUE);
}

public UpDownScalable(Scalable upScalable, Scalable downScalable, double minValue, double maxValue) {
this.upScalable = Objects.requireNonNull(upScalable);
this.downScalable = Objects.requireNonNull(downScalable);
this.minValue = minValue;
this.maxValue = maxValue;
}

@Override
Expand All @@ -38,18 +46,18 @@ public void reset(Network n) {
@Override
public double maximumValue(Network n, ScalingConvention scalingConvention) {
if (scalingConvention == ScalingConvention.LOAD) {
return downScalable.maximumValue(n, scalingConvention) - upScalable.initialValue(n);
return Math.min(downScalable.maximumValue(n, scalingConvention) - upScalable.initialValue(n), maxValue);
} else {
return upScalable.maximumValue(n, scalingConvention) + downScalable.initialValue(n);
return Math.min(upScalable.maximumValue(n, scalingConvention) + downScalable.initialValue(n), maxValue);
}
}

@Override
public double minimumValue(Network n, ScalingConvention scalingConvention) {
if (scalingConvention == ScalingConvention.LOAD) {
return upScalable.minimumValue(n, scalingConvention) - downScalable.initialValue(n);
return Math.max(upScalable.minimumValue(n, scalingConvention) - downScalable.initialValue(n), minValue);
} else {
return downScalable.minimumValue(n, scalingConvention) + upScalable.initialValue(n);
return Math.max(downScalable.minimumValue(n, scalingConvention) + upScalable.initialValue(n), minValue);
}
}

Expand All @@ -61,7 +69,12 @@ public void filterInjections(Network network, List<Injection> injections, List<S

@Override
public double scale(Network n, double asked, ScalingParameters parameters) {
return asked > 0 ? upScalable.scale(n, asked, parameters) : downScalable.scale(n, asked, parameters);
double minWithConvention = parameters.getScalingConvention() == ScalingConvention.GENERATOR ? minValue : -maxValue;
double maxWithConvention = parameters.getScalingConvention() == ScalingConvention.GENERATOR ? maxValue : -minValue;
double boundedAsked = asked > 0 ?
Math.min(asked, maxWithConvention - getSteadyStatePower(n, asked, parameters.getScalingConvention())) :
Math.max(asked, minWithConvention - getSteadyStatePower(n, asked, parameters.getScalingConvention()));
return asked > 0 ? upScalable.scale(n, boundedAsked, parameters) : downScalable.scale(n, boundedAsked, parameters);
}

@Override
Expand Down
Loading

0 comments on commit ecb61c4

Please sign in to comment.