All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.openrao.searchtreerao.commons.marginevaluator.MarginEvaluatorWithMarginDecreaseUnoptimizedCnecs Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.powsybl.openrao.searchtreerao.commons.marginevaluator;

import com.powsybl.openrao.commons.Unit;
import com.powsybl.openrao.data.crac.api.cnec.FlowCnec;
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.openrao.searchtreerao.result.api.FlowResult;

import java.util.*;

/**
 * It enables to evaluate the absolute margin of a FlowCnec
 * For cnecs belonging to operators that do not share RAs, margin is considered infinite
 * when cnecs'margin has increased
 *
 * @author Joris Mancini {@literal }
 * @author Peter Mitri {@literal }
 */
public class MarginEvaluatorWithMarginDecreaseUnoptimizedCnecs implements MarginEvaluator {
    private final MarginEvaluator marginEvaluator;
    private final Set countriesNotToOptimize;
    private final FlowResult prePerimeterFlowResult;

    public MarginEvaluatorWithMarginDecreaseUnoptimizedCnecs(MarginEvaluator marginEvaluator,
                                                             Set countriesNotToOptimize,
                                                             FlowResult prePerimeterFlowResult) {
        this.marginEvaluator = marginEvaluator;
        this.countriesNotToOptimize = countriesNotToOptimize;
        this.prePerimeterFlowResult = prePerimeterFlowResult;
    }

    @Override
    public double getMargin(FlowResult flowResult, FlowCnec flowCnec, Unit unit) {
        return flowCnec.getMonitoredSides().stream()
                .map(side -> getMargin(flowResult, flowCnec, side, unit))
                .min(Double::compareTo).orElseThrow();
    }

    @Override
    public double getMargin(FlowResult flowResult, FlowCnec flowCnec, TwoSides side, Unit unit) {
        double newMargin = marginEvaluator.getMargin(flowResult, flowCnec, side, unit);
        double prePerimeterMargin = marginEvaluator.getMargin(prePerimeterFlowResult, flowCnec, side, unit);
        return computeMargin(flowCnec, newMargin, prePerimeterMargin);
    }

    private double computeMargin(FlowCnec flowCnec, double newMargin, double prePerimeterMargin) {
        if (countriesNotToOptimize.contains(flowCnec.getOperator()) && newMargin > prePerimeterMargin - .0001 * Math.abs(prePerimeterMargin)) {
            return Double.MAX_VALUE;
        }
        return newMargin;
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy