Series emission_factor with expression as an objective function

Dear community,

I am adding a variable emission_factor as a dataframe or series for a source component which acts as the grid. However, when I try to add an expression for an objective function to minimize the total emissions in the energy system I get an error: ‘Series’ object has no attribute ‘is_expression_type’.

This is my line of code:

my_series = data['emission_factor'].squeeze()
energysystem.add(
    solph.Source(
        label="grid",
        outputs={bel: solph.Flow(nominal_value=10000000000, emission_factor=my_series)},
    )
)
myblock = po.Block()
myblock.COMMODITYFLOWS = [
    k for (k, v) in model.flows.items() if hasattr(v, "emission_factor")
]
model.add_component("MyBlock", myblock)

expr=(
    sum(model.flow[i, o, t] * model.flows[i,o].emission_factor for (i, o) in myblock.COMMODITYFLOWS for t in model.TIMESTEPS)
)

model.objective = po.Objective(expr=expr, sense=po.minimize)

Hi @Houssam , welcome to the community.

Did you have a look at the existing constraints. There is a constraint generic_integral_limit which does exactly what you need.

You can either pass all Flow objects you want to take into account or you can just take all Flow objects into account that have the attribute emission_factor.

In most cases the second variant is the easiest one. In your case the code would look like this.

model = solph.constraints.generic_integral_limit(
    model, "emission_factor", limit=777)

Thank you for your greetings and answer @uwe.krien.
Yes, I actually did take a look at the existing constraints but in my model I want to minimize the emissions in an objective function and not as a constraint. Is there a way to do that?

Hallo @Houssam, nice to have you here. Actually, the variable_costs do not have to be money. It’s rather the term for whatever should be optimised. So, if you want to minimise emissions, just give them as costs. If you also want to optimise for economical costs, you’d have to add some carbon price to weight between the optimisation goals. (Only one quantity can be optimised.)

Thank you @pschoen, it’s nice to join you in this forum as well. How exactly should I add the carbon price to weight between the optimization goals and give a certain priority?

It is easiest when you add a shadow price for carbon. It directly gives the weighting between emissions and economic costs. The following snippet should make the implementation clearer:

(The numbers are reasonable assuming your are using hourly time steps and that my_series is in t/kWh. Change it accordingly, if your parameters differ.)