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

com.powsybl.openrao.searchtreerao.commons.costevaluatorresult.AbstractStateWiseCostEvaluatorResult Maven / Gradle / Ivy

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

import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.data.crac.api.State;
import com.powsybl.openrao.data.crac.api.cnec.FlowCnec;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.DoubleStream;

/**
 * @author Thomas Bouquet {@literal }
 */
public abstract class AbstractStateWiseCostEvaluatorResult implements CostEvaluatorResult {
    private final Map costPerState;
    private final List costlyElements;

    protected AbstractStateWiseCostEvaluatorResult(Map costPerState, List costlyElements) {
        this.costPerState = costPerState;
        this.costlyElements = costlyElements;
    }

    @Override
    public double getCost(Set contingenciesToExclude) {
        DoubleStream filteredCosts = costPerState.entrySet().stream()
            .filter(entry -> statesContingencyMustBeKept(entry.getKey(), contingenciesToExclude))
            .mapToDouble(Map.Entry::getValue);
        return evaluateResultsWithSpecificStrategy(filteredCosts);
    }

    protected abstract double evaluateResultsWithSpecificStrategy(DoubleStream filteredCostsStream);

    @Override
    public List getCostlyElements(Set contingenciesToExclude) {
        return costlyElements.stream().filter(flowCnec -> statesContingencyMustBeKept(flowCnec.getState(), contingenciesToExclude)).toList();
    }

    private static boolean statesContingencyMustBeKept(State state, Set contingenciesToExclude) {
        Optional contingency = state.getContingency();
        return contingency.isEmpty() || !contingenciesToExclude.contains(contingency.get().getId());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy