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

com.powsybl.openrao.searchtreerao.commons.parameters.UnoptimizedCnecParameters Maven / Gradle / Ivy

/*
 * Copyright (c) 2021, 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.parameters;

import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.rangeaction.PstRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.RangeAction;
import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod;
import com.powsybl.openrao.raoapi.parameters.NotOptimizedCnecsParameters;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.powsybl.openrao.commons.logs.OpenRaoLoggerProvider.TECHNICAL_LOGS;

/**
 * @author Joris Mancini {@literal }
 * @author Godelaine de Montmorillon {@literal }
 */
public class UnoptimizedCnecParameters {
    private final Set operatorNotToOptimize;
    private final Map> unoptimizedCnecsInSeriesWithPsts;

    public UnoptimizedCnecParameters(Set operatorNotToOptimize, Map> unoptimizedCnecsInSeriesWithPsts) {
        this.operatorNotToOptimize = operatorNotToOptimize;
        this.unoptimizedCnecsInSeriesWithPsts = unoptimizedCnecsInSeriesWithPsts;
    }

    public Map> getDoNotOptimizeCnecsSecuredByTheirPst() {
        return unoptimizedCnecsInSeriesWithPsts;
    }

    public Set getOperatorsNotToOptimize() {
        return operatorNotToOptimize;
    }

    // unoptimizedCnecsInSeriesWithPsts and operatorNotToOptimize cannot be activated together.
    public static UnoptimizedCnecParameters build(NotOptimizedCnecsParameters parameters, Set operatorsNotSharingCras, Crac crac) {
        if (parameters.getDoNotOptimizeCurativeCnecsForTsosWithoutCras()
                && !parameters.getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) {
            throw new OpenRaoException("SearchTreeRaoParameters : unoptimizedCnecsInSeriesWithPsts and operatorNotToOptimize cannot be activated together");
        } else if (parameters.getDoNotOptimizeCurativeCnecsForTsosWithoutCras()) {
            return new UnoptimizedCnecParameters(
                operatorsNotSharingCras,
                null);
        } else if (!parameters.getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) {
            return new UnoptimizedCnecParameters(
                    null,
                    getDoNotOptimizeCnecsSecuredByTheirPstFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac));
        } else {
            return null;
        }
    }

    public static Map> getDoNotOptimizeCnecsSecuredByTheirPst(NotOptimizedCnecsParameters parameters, Crac crac) {
        if (!parameters.getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) {
            return getDoNotOptimizeCnecsSecuredByTheirPstFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac);
        } else {
            return Collections.emptyMap();
        }
    }

    private static Map> getDoNotOptimizeCnecsSecuredByTheirPstFromIds(Map ids, Crac crac) {
        Map> mapOfUnoptimizedCnecsAndPsts = new HashMap<>();
        // Create map elements for all cnecs with network element id in ids.keySet()
        for (Map.Entry entrySet : ids.entrySet()) {
            String cnecId = entrySet.getKey();
            String pstId = entrySet.getValue();

            Set flowCnecs = crac.getFlowCnecs().stream().filter(flowCnec -> flowCnec.getNetworkElement().getId().equals(cnecId)).collect(Collectors.toSet());
            Set pstRangeActions = crac.getPstRangeActions().stream().filter(pstRangeAction -> pstRangeAction.getNetworkElement().getId().equals(pstId)).collect(Collectors.toSet());

            if (skipEntry(cnecId, pstId, flowCnecs, pstRangeActions)) {
                continue;
            }

            for (FlowCnec flowCnec : flowCnecs) {
                Set availablePstRangeActions = pstRangeActions.stream().filter(pstRangeAction ->
                    pstRangeAction.getUsageMethod(flowCnec.getState()).equals(UsageMethod.AVAILABLE) ||
                        pstRangeAction.getUsageMethod(flowCnec.getState()).equals(UsageMethod.FORCED)).collect(Collectors.toSet());

                if (skipFlowCnec(availablePstRangeActions, pstId)) {
                    continue;
                }

                mapOfUnoptimizedCnecsAndPsts.put(flowCnec, availablePstRangeActions.iterator().next());
            }
        }
        return mapOfUnoptimizedCnecsAndPsts;
    }

    private static boolean skipEntry(String cnecId, String pstId, Set flowCnecs, Set pstRangeActions) {
        if (flowCnecs.isEmpty()) {
            TECHNICAL_LOGS.debug("No flowCnec with network element id {} exists in unoptimized-cnecs-in-series-with-psts parameter", cnecId);
            return true;
        }

        if (pstRangeActions.isEmpty()) {
            TECHNICAL_LOGS.debug("No pst range actions are defined with network element {}", pstId);
            return true;
        }
        return false;
    }

    private static boolean skipFlowCnec(Set availablePstRangeActions, String pstId) {
        if (availablePstRangeActions.size() > 1) {
            TECHNICAL_LOGS.debug("{} pst range actions are defined with network element {} instead of 1", availablePstRangeActions.size(), pstId);
            return true;
        }

        return availablePstRangeActions.isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy