Esempio n. 1
0
def OptimStockage(n,i=4):
    prixBatterie=200 #€/kWh On peut changer le prix pour voir à partir de quel prix le stockage est rentable
    P=[] #liste pour stocker le coût du système avec stockage
    Pui=[] #liste pour stocker les différentes puissances de stockage
    for i in range(n):
        PrixTotal={}
        Consommation={}
        LMultipliers={}
        DeltaPrix={}
        Deltazz={}
        CostFunction={}
        TotalCols={}
        zz={}
        #p_max_out=100.; p_max_in=100.; c_max=10.;
        p_max=i*40.;  c_max=4.*p_max;
        Pui.append(p_max)
        areaConsumption["NewConsumption"]=areaConsumption["areaConsumption"]
        nbTime=len(areaConsumption["areaConsumption"])
        cpt=0
        for i in model.areaConsumption:  model.areaConsumption[i]=areaConsumption.NewConsumption[i-1]
        for cpt in range(2):
            print(cpt)
            if (cpt==0): zz[cpt]=[0]*nbTime
            else : zz[cpt]=areaConsumption["Storage"]
            results=opt.solve(model)
            Constraints= getConstraintsDual_panda(model)
            TotalCols[cpt]=getVariables_panda(model)['energyCosts'].sum()[1]
            Prix=Constraints["energyCtr"].assign(Prix=lambda x: x.energyCtr *10**6).Prix.to_numpy()
            valueAtZero=Prix*(TotalCols[cpt]/sum(Prix*Prix)-zz[cpt])
            tmpCost=GenCostFunctionFromMarketPrices_dict(Prix,r_in=0.95,r_out=0.95,valueAtZero=valueAtZero)
            if (cpt==0): CostFunction[cpt]=GenCostFunctionFromMarketPrices(Prix,r_in=0.95,r_out=0.95,valueAtZero=valueAtZero)
            else:
                tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=0.95, r_out=0.95, valueAtZero=valueAtZero)
                tmpCost2 = CostFunction[cpt-1]
                tmpCost2.Maxf_2Breaks_withO(tmpCost['S1'],tmpCost['S2'],tmpCost['B1'],tmpCost['B2'],tmpCost['f0']) ### etape clé, il faut bien comprendre ici l'utilisation du max de deux fonction de coût
                CostFunction[cpt]=tmpCost2
            LMultipliers[cpt]=Prix
            if cpt>0:
                DeltaPrix[cpt]=sum(abs(LMultipliers[cpt]-LMultipliers[cpt-1]))
                Deltazz[cpt]=sum(abs(zz[cpt]-zz[cpt-1]))
            areaConsumption["Storage"]=CostFunction[cpt].OptimMargInt([-p_max]*nbTime,[p_max]*nbTime,[0]*nbTime,[c_max]*nbTime)
            areaConsumption["NewConsumption"]=areaConsumption["areaConsumption"]+areaConsumption["Storage"]
            PrixTotal[cpt]=Prix.sum()
            Consommation[cpt]=areaConsumption.NewConsumption
            for i in model.areaConsumption:  model.areaConsumption[i]=areaConsumption.NewConsumption[i-1]
        cout=(areaConsumption['NewConsumption']*Prix*0.001).sum()
        P.append(cout)
    fig=go.Figure()
    fig.add_trace(
        go.Scatter(x=list(Pui), y=list(P), name="Dépenses"))
    fig.update_layout(
        title_text="Dépenses en euros",xaxis_title="Puissance du stockage")
    fig.update_layout(
        xaxis=dict(
            rangeselector=dict(
                buttons=list([
                    dict(count=1,
                        label="en heures",
                        step="hour",
                        stepmode="backward")
                ])
            ),
            rangeslider=dict(
                visible=True
            ),
            type="-"
        )
    )
    fig.show()
    return()
    
    ##### Loop
    PrixTotal = {}
    Consommation = {}
    LMultipliers = {}
    DeltaPrix = {}
    Deltazz = {}
    CostFunction = {}
    TotalCols = {}
    zz = {}
    # p_max_out=100.; p_max_in=100.; c_max=10.;

    areaConsumption["NewConsumption"] = areaConsumption["areaConsumption"]
    nbTime = len(areaConsumption["areaConsumption"])
    cpt = 0
    for i in model.areaConsumption:  model.areaConsumption[i] = areaConsumption.NewConsumption[i - 1]
    DeltaPrix_=tol+1
    while ((DeltaPrix_ >  tol)&(n<cpt)) :
        print(cpt)
        if (cpt == 0):
            zz[cpt] = [0] * nbTime
        else:
            zz[cpt] = areaConsumption["Storage"]
        results = opt.solve(model)
        Constraints = getConstraintsDual_panda(model)
        TotalCols[cpt] = getVariables_panda(model)['energyCosts'].sum()[1]
        Prix = Constraints["energyCtr"].assign(Prix=lambda x: x.energyCtr * 10 ** 6).Prix.to_numpy()
        valueAtZero = Prix * (TotalCols[cpt] / sum(Prix * Prix) - zz[cpt])
        tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=efficiency_in, r_out=efficiency_out, valueAtZero=valueAtZero)
        if (cpt == 0):
            CostFunction[cpt] = GenCostFunctionFromMarketPrices(Prix, r_in=efficiency_in, r_out=efficiency_out, valueAtZero=valueAtZero)
        else:
            tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=efficiency_in, r_out=efficiency_out, valueAtZero=valueAtZero)
            tmpCost2 = CostFunction[cpt - 1]
            if efficiency_in*efficiency_out==1:
                tmpCost2.Maxf_1Breaks_withO(tmpCost['S1'], tmpCost['B1'], tmpCost[
                    'f0'])
            else:
                tmpCost2.Maxf_2Breaks_withO(tmpCost['S1'], tmpCost['S2'], tmpCost['B1'], tmpCost['B2'], tmpCost[
                'f0'])  ### etape clé, il faut bien comprendre ici l'utilisation du max de deux fonction de coût
            CostFunction[cpt] = tmpCost2
        LMultipliers[cpt] = Prix
        if cpt > 0:
            DeltaPrix[cpt] = sum(abs(LMultipliers[cpt] - LMultipliers[cpt - 1]))/sum(abs(LMultipliers[cpt]))
            Deltazz[cpt] = sum(abs(zz[cpt] - zz[cpt - 1]))/sum(abs(zz[cpt]))
            DeltaPrix_= DeltaPrix[cpt]

        areaConsumption["Storage"] = CostFunction[cpt].OptimMargInt([-p_max] * nbTime, [p_max] * nbTime, [0] * nbTime,
                                                                    [c_max] * nbTime)
        areaConsumption["NewConsumption"] = areaConsumption["areaConsumption"] + areaConsumption["Storage"]
        for i in model.areaConsumption:  model.areaConsumption[i] = areaConsumption.NewConsumption[i - 1]
        cpt=cpt+1
    stats = {"DeltaPrix" : DeltaPrix,"Deltazz" : Deltazz}
    return {"areaConsumption" : areaConsumption, "model" : model, "stats" : stats};
Esempio n. 2
0
def GetElectricSystemModel_PlaningMultiNode_with1Storage(areaConsumption,availabilityFactor,
                                                          TechParameters,ExchangeParameters,StorageParameters,isAbstract=False,
                                                          solver='mosek',n=10,tol=exp(-4)):
    """
    This function takes storage caracteristics, system caracteristics and optimise operation Set values
    :param areaConsumption: panda table with consumption
    :param availabilityFactor: panda table
    :param isAbstract: boolean true is the model should be abstract. ConcreteModel otherwise
    :param StorageParameters is a panda with p_max (maximal power), c_max (energy capacity in the storage : maximal energy),
    :efficiency_in (input efficiency of storage),
    :efficiency_out (output efficiency of storage).
    :return: a dictionary with model : pyomo model without storage, storage : storage infos
    """
    import pandas as pd
    import numpy as np
    from dynprogstorage.wrappers import GenCostFunctionFromMarketPrices
    from dynprogstorage.wrappers import GenCostFunctionFromMarketPrices_dict
    #isAbstract=False
    AREAS = areaConsumption["AREAS"].unique().tolist()
    model = GetElectricSystemModel_PlaningMultiNode(areaConsumption, availabilityFactor, TechParameters,ExchangeParameters,isAbstract=isAbstract)
    opt = SolverFactory(solver)
   # results = opt.solve(model)
   # Variables = getVariables_panda(model) #### obtain optimized variables in panda form
   # Constraints = getConstraintsDual_panda(model)  #### obtain lagrange multipliers in panda form

    ##### Loop
    Consommation = {};
    for AREA in AREAS : Consommation[AREA]={}
    LMultipliers = {};
    for AREA in AREAS : LMultipliers[AREA]={}
    CostFunction = {};
    for AREA in AREAS : CostFunction[AREA] = {}
    zz = {};
    for AREA in AREAS: zz[AREA] = {}

    OptimControl=pd.DataFrame(columns=["Step","AREAS","TotalCols","DeltaPrix","Deltazz"])

    areaConsumption["NewConsumption"] = areaConsumption["areaConsumption"]
    nbTime = len(areaConsumption["TIMESTAMP"].unique())
    cpt = 0
    areaConsumption=areaConsumption.set_index(["AREAS","TIMESTAMP"])
    for i in model.areaConsumption:
        model.areaConsumption[i] = areaConsumption.NewConsumption[i]
    DeltaPrix_=tol+1
    while ( (DeltaPrix_ >  tol) & (n>cpt) ) :
        print(cpt)

        if (cpt == 0):
            for AREA in AREAS: zz[AREA][cpt] = [0] * nbTime
            areaConsumption["Storage"] = 0
        else:
            DeltaPrix_ = 0
            for AREA in AREAS:
                zz[AREA][cpt] = areaConsumption.loc[areaConsumption.index.get_level_values('AREAS') == AREA,"Storage"]

        results = opt.solve(model)
        Constraints = getConstraintsDual_panda(model)
        Variables = getVariables_panda(model)['energyCosts']


        for AREA in AREAS:
            indexStorage =  StorageParameters["AREA"]==AREA
            TotalCols = Variables[Variables.AREAS==AREA].energyCosts.sum()
            #Constraints["energyCtr"]=Constraints["energyCtr"].set_index(["AREAS","TIMESTAMP"])

            Prix = Constraints["energyCtr"][Constraints["energyCtr"].AREAS==AREA].assign(Prix=lambda x: x.energyCtr * 10 ** 6).Prix.to_numpy()
            Prix[Prix<=0]=0.0000000001
            valueAtZero = Prix * (TotalCols / sum(Prix * Prix) - zz[AREA][cpt])
            tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=StorageParameters[indexStorage].efficiency_in.tolist()[0],
                                                       r_out=StorageParameters[indexStorage].efficiency_out.tolist()[0],
                                                       valueAtZero=valueAtZero)
            if (cpt == 0):
                CostFunction[AREA][cpt] = GenCostFunctionFromMarketPrices(Prix,
                                                                          r_in=StorageParameters[indexStorage].efficiency_in.tolist()[0],
                                                                          r_out=StorageParameters[indexStorage].efficiency_out.tolist()[0],
                                                                          valueAtZero=valueAtZero)
            else:
                tmpCost = GenCostFunctionFromMarketPrices_dict(Prix,
                                                               r_in=StorageParameters[indexStorage].efficiency_in.tolist()[0],
                                                               r_out=StorageParameters[indexStorage].efficiency_out.tolist()[0],
                                                               valueAtZero=valueAtZero)
                tmpCost2 = CostFunction[AREA][cpt - 1]
                if StorageParameters[indexStorage].efficiency_in.tolist()[0]*StorageParameters[indexStorage].efficiency_out.tolist()[0]==1:
                    tmpCost2.Maxf_1Breaks_withO(tmpCost['S1'], tmpCost['B1'], tmpCost['f0'])
                else:
                    tmpCost2.Maxf_2Breaks_withO(tmpCost['S1'], tmpCost['S2'], tmpCost['B1'], tmpCost['B2'], tmpCost['f0'])  ### etape clé, il faut bien comprendre ici l'utilisation du max de deux fonction de coût
                CostFunction[AREA][cpt] = tmpCost2
            LMultipliers[AREA][cpt] = Prix
            if cpt > 0:
                DeltaPrix = sum(abs(LMultipliers[AREA][cpt] - LMultipliers[AREA][cpt - 1]))/sum(abs(LMultipliers[AREA][cpt]))
                if (sum(abs(zz[AREA][cpt]))==0) & (sum(abs(zz[AREA][cpt] - zz[AREA][cpt - 1]))==0) :
                    Deltazz = 0
                else:
                    Deltazz = sum(abs(zz[AREA][cpt] - zz[AREA][cpt - 1]))/sum(abs(zz[AREA][cpt]))
                DeltaPrix_= DeltaPrix_+ DeltaPrix
                OptimControl_tmp=pd.DataFrame([cpt, AREA, TotalCols, DeltaPrix, Deltazz]).transpose()
                OptimControl_tmp.columns=["Step", "AREAS", "TotalCols", "DeltaPrix", "Deltazz"]
                OptimControl=pd.concat([OptimControl,OptimControl_tmp],axis=0)


            areaConsumption.loc[areaConsumption.index.get_level_values('AREAS') == AREA,"Storage"] = CostFunction[AREA][cpt].OptimMargInt([-StorageParameters[indexStorage].p_max.tolist()[0]] * nbTime,
                                                                    [StorageParameters[indexStorage].p_max.tolist()[0]] * nbTime, [0] * nbTime,
                                                                    [StorageParameters[indexStorage].c_max.tolist()[0]] * nbTime)

            areaConsumption.loc[areaConsumption.index.get_level_values('AREAS') == AREA,"NewConsumption"] = areaConsumption.loc[areaConsumption.index.get_level_values('AREAS') == AREA,"areaConsumption"] + areaConsumption.loc[areaConsumption.index.get_level_values('AREAS') == AREA,"Storage"]
        for i in model.areaConsumption:  model.areaConsumption[i] = areaConsumption.NewConsumption[i]
        cpt=cpt+1

    stats = {"DeltaPrix" : DeltaPrix,"Deltazz" : Deltazz}
    return {"areaConsumption" : areaConsumption, "model" : model, "stats" : stats};
Esempio n. 3
0
def GetElectricSystemModel_PlaningSingleNode_with1Storage(areaConsumption,availabilityFactor,
                                                          TechParameters,StorageParameters,solverpath=-1,isAbstract=False,
                                                          solver='mosek',n=10,tol=exp(-4)):
    """
    This function takes storage caracteristics, system caracteristics and optimise operation Set values
    :param areaConsumption: panda table with consumption
    :param availabilityFactor: panda table
    :param isAbstract: boolean true is the model should be abstract. ConcreteModel otherwise
    :param StorageParameters is a dictionary with p_max (maximal power), c_max (energy capacity in the storage : maximal energy),
    :efficiency_in (input efficiency of storage),
    :efficiency_out (output efficiency of storage).
    :return: a dictionary with model : pyomo model without storage, storage : storage infos
    """
    import pandas as pd
    import numpy as np
    from dynprogstorage.wrappers import GenCostFunctionFromMarketPrices
    from dynprogstorage.wrappers import GenCostFunctionFromMarketPrices_dict
    #isAbstract=False

    model = GetElectricSystemModel_PlaningSingleNode(areaConsumption, availabilityFactor, TechParameters,isAbstract=isAbstract)
    if solverpath==-1 : opt = SolverFactory(solver)
    else : opt = MySolverFactory(solver, solverpath)
   # results = opt.solve(model)
   # Variables = getVariables_panda(model) #### obtain optimized variables in panda form
   # Constraints = getConstraintsDual_panda(model)  #### obtain lagrange multipliers in panda form

    ##### Loop
    PrixTotal = {}
    Consommation = {}
    LMultipliers = {}
    DeltaPrix = {}
    Deltazz = {}
    CostFunction = {}
    TotalCols = {}
    zz = {}
    # p_max_out=100.; p_max_in=100.; c_max=10.;

    areaConsumption["NewConsumption"] = areaConsumption["areaConsumption"]
    nbTime = len(areaConsumption["areaConsumption"])
    cpt = 0
    for i in model.areaConsumption:  model.areaConsumption[i] = areaConsumption.NewConsumption[i - 1]
    DeltaPrix_=tol+1
    while ( (DeltaPrix_ >  tol) & (n>cpt) ) :
        print(cpt)
        if (cpt == 0):
            zz[cpt] = [0] * nbTime
        else:
            zz[cpt] = areaConsumption["Storage"]

        #if solver=="mosek" :
        #    results = opt.solve(model, options= {"dparam.optimizer_max_time":  100.0, "iparam.outlev" : 2,                                                 "iparam.optimizer":     mosek.optimizertype.primal_simplex},tee=True)
        #else :
        if (solver=='cplex')| (solver=='cbc'):
            results = opt.solve(model,warmstart = True)
        else : results = opt.solve(model)

        Constraints = getConstraintsDual_panda(model)
        #if solver=='cbc':
        #    Variables = getVariables_panda(model)['energy'].set_index(['TIMESTAMP','TECHNOLOGIES'])
        #    for i in model.energy:  model.energy[i] = Variables.energy[i]


        TotalCols[cpt] = getVariables_panda(model)['energyCosts'].sum()[1]
        Prix = Constraints["energyCtr"].assign(Prix=lambda x: x.energyCtr * 10 ** 6).Prix.to_numpy()
        Prix[Prix<=0]=0.0000000001
        valueAtZero = Prix * (TotalCols[cpt] / sum(Prix * Prix) - zz[cpt])
        tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=StorageParameters['efficiency_in'],
                                                       r_out=StorageParameters['efficiency_out'],
                                                       valueAtZero=valueAtZero)
        if (cpt == 0):
            CostFunction[cpt] = GenCostFunctionFromMarketPrices(Prix, r_in=StorageParameters['efficiency_in'],
                                                                r_out=StorageParameters['efficiency_out'], valueAtZero=valueAtZero)
        else:
            tmpCost = GenCostFunctionFromMarketPrices_dict(Prix, r_in=StorageParameters['efficiency_in'],
                                                           r_out=StorageParameters['efficiency_out'], valueAtZero=valueAtZero)
            tmpCost2 = CostFunction[cpt - 1]
            if StorageParameters['efficiency_in']*StorageParameters['efficiency_out']==1:
                tmpCost2.Maxf_1Breaks_withO(tmpCost['S1'], tmpCost['B1'], tmpCost[
                    'f0'])
            else:
                tmpCost2.Maxf_2Breaks_withO(tmpCost['S1'], tmpCost['S2'], tmpCost['B1'], tmpCost['B2'], tmpCost[
                'f0'])  ### etape clé, il faut bien comprendre ici l'utilisation du max de deux fonction de coût
            CostFunction[cpt] = tmpCost2
        LMultipliers[cpt] = Prix
        if cpt > 0:
            DeltaPrix[cpt] = sum(abs(LMultipliers[cpt] - LMultipliers[cpt - 1]))/sum(abs(LMultipliers[cpt]))
            Deltazz[cpt] = sum(abs(zz[cpt] - zz[cpt - 1]))/sum(abs(zz[cpt]))
            DeltaPrix_= DeltaPrix[cpt]

        areaConsumption["Storage"] = CostFunction[cpt].OptimMargInt([-StorageParameters['p_max']] * nbTime,
                                                                    [StorageParameters['p_max']] * nbTime, [0] * nbTime,
                                                                    [StorageParameters['c_max']] * nbTime)
        areaConsumption["NewConsumption"] = areaConsumption["areaConsumption"] + areaConsumption["Storage"]
        for i in model.areaConsumption:  model.areaConsumption[i] = areaConsumption.NewConsumption[i-1]
        cpt=cpt+1

    stats = {"DeltaPrix" : DeltaPrix,"Deltazz" : Deltazz}
    return {"areaConsumption" : areaConsumption, "model" : model, "stats" : stats};