# Objective Function¶

There are two possible choices of objective function for urbs problems, either the costs (default option) or the total CO2-emissions can be minimized.

If the total CO2-emissions are minimized, the objective function takes the form:

In script `model.py`

the global CO2 emissions are defined and calculated by
the following code fragment:

```
def co2_rule(m):
co2_output_sum = 0
for stf in m.stf:
for tm in m.tm:
for sit in m.sit:
# minus because negative commodity_balance represents
# creation of that commodity.
if m.mode['int']:
co2_output_sum += (- commodity_balance(m, tm, stf, sit, 'CO2') *
m.weight * stf_dist(stf, m))
else:
co2_output_sum += (- commodity_balance(m, tm, stf, sit, 'CO2') *
m.weight)
return (co2_output_sum)
```

In the default case the total system costs are minimized. These variable total system costs \(\zeta\) are calculated by the cost function. The cost function is the objective function of the optimization model. Minimizing the value of the variable total system cost would give the most reasonable solution for the modelled energy system. The formula of the cost function expressed in mathematical notation is as following:

The calculation of the variable total system cost is given in `model.py`

by
the following code fragment.

```
def cost_rule(m):
return pyomo.summation(m.costs)
```

The variable total system cost \(\zeta\) is basically calculated by the
summation of every type of total costs. As previously mentioned in section
Cost Types, these cost types are : `Invest`

, `Fixed`

,
`Variable`

, `Fuel`

, `Environmental`

, `Revenue`

, `Purchase`

.

In script `model.py`

the individual cost functions are calculated by
the following code fragment:

```
def def_costs_rule(m, cost_type):
#Calculate total costs by cost type.
#Sums up process activity and capacity expansions
#and sums them in the cost types that are specified in the set
#m.cost_type. To change or add cost types, add/change entries
#there and modify the if/elif cases in this function accordingly.
#Cost types are
# - Investment costs for process power, storage power and
# storage capacity. They are multiplied by the investment
# factors. Rest values of units are subtracted.
# - Fixed costs for process power, storage power and storage
# capacity.
# - Variables costs for usage of processes, storage and transmission.
# - Fuel costs for stock commodity purchase.
if cost_type == 'Invest':
cost = \
sum(m.cap_pro_new[p] *
m.process_dict['inv-cost'][p] *
m.process_dict['invcost-factor'][p]
for p in m.pro_tuples)
if m.mode['int']:
cost -= \
sum(m.cap_pro_new[p] *
m.process_dict['inv-cost'][p] *
m.process_dict['overpay-factor'][p]
for p in m.pro_tuples)
if m.mode['tra']:
# transmission_cost is defined in transmission.py
cost += transmission_cost(m, cost_type)
if m.mode['sto']:
# storage_cost is defined in storage.py
cost += storage_cost(m, cost_type)
return m.costs[cost_type] == cost
elif cost_type == 'Fixed':
cost = \
sum(m.cap_pro[p] * m.process_dict['fix-cost'][p] *
m.process_dict['cost_factor'][p]
for p in m.pro_tuples)
if m.mode['tra']:
cost += transmission_cost(m, cost_type)
if m.mode['sto']:
cost += storage_cost(m, cost_type)
return m.costs[cost_type] == cost
elif cost_type == 'Variable':
cost = \
sum(m.tau_pro[(tm,) + p] * m.weight *
m.process_dict['var-cost'][p] *
m.process_dict['cost_factor'][p]
for tm in m.tm
for p in m.pro_tuples)
if m.mode['tra']:
cost += transmission_cost(m, cost_type)
if m.mode['sto']:
cost += storage_cost(m, cost_type)
return m.costs[cost_type] == cost
elif cost_type == 'Fuel':
return m.costs[cost_type] == sum(
m.e_co_stock[(tm,) + c] * m.weight *
m.commodity_dict['price'][c] *
m.commodity_dict['cost_factor'][c]
for tm in m.tm for c in m.com_tuples
if c[2] in m.com_stock)
elif cost_type == 'Environmental':
return m.costs[cost_type] == sum(
- commodity_balance(m, tm, stf, sit, com) * m.weight *
m.commodity_dict['price'][(stf, sit, com, com_type)] *
m.commodity_dict['cost_factor'][(stf, sit, com, com_type)]
for tm in m.tm
for stf, sit, com, com_type in m.com_tuples
if com in m.com_env)
# Revenue and Purchase costs defined in BuySellPrice.py
elif cost_type == 'Revenue':
return m.costs[cost_type] == revenue_costs(m)
elif cost_type == 'Purchase':
return m.costs[cost_type] == purchase_costs(m)
else:
raise NotImplementedError("Unknown cost type.")
```