## 3 Mathematical Formulation
### Optimization System
#### 3.2.2 Iao  
The IAO is responsible for the energy management of the industrial microgrid, ensuring the optimal operation of all its assets, including the second-life battery, photovoltaic generation, building demand, EV charging, and grid integration. The IAO is designed to minimize operational and environmental costs while maximizing self-consumption of renewable energy and extending battery lifetime. The optimization strategy used by the IAO is based on the model described in [38], where a DT and energy management-based approach is proposed to assess the impact of the SESS and PV systems in industrial environments. This strategy integrates forecasting models for energy prices, PV generation, and industrial consumption, enabling an adaptive energy management strategy. The IAO operates as a higher-level optimizer, incorporating inputs from the EVO to generate a global optimal dispatch for all industrial assets. The primary inputs for the IAO include the following:  
* PV generation forecast. * Industrial consumption forecast (aggregate consumption of the expected fixed consumption of the industry and the expected EV charging consumption). * Grid electricity price forecast. * Emission generation data from the grid, SESS, and PV system. * SESS battery data, such as initial SOC, operation, and degradation parameters. The IAO follows a multi-objective optimization approach, balancing economic and environmental criteria. The objective function is formulated as  
\[J=\alpha\cdot\frac{J_{cc}}{J_{cc}^{*}}+(1-\alpha)\cdot\frac{J_{en}}{J_{en}^{*}} \tag{30}\]  
\(J_{cc}\) are the economic operational costs, considering the energy exchanged with the grid, the power contracted, and the aging of the batteries. This yields a cost in monetary units through the following equation:  
\[J_{cc}=\sum_{k}\bigl{(}e_{p_{k}}\cdot C_{p_{k}}-e_{s_{k}}\cdot C_{s_{k}}\bigr{)} +C_{c}\cdot p_{c}+C_{a}\cdot\frac{\sum_{k}\bigl{(}e_{c_{k}}+e_{d_{k}}\bigr{)}}{ 2\cdot E_{b}}, \tag{31}\]  
where \(e_{p_{k}}\) is the energy purchased from the grid at time \(t\), \(C_{p_{k}}\) is the cost of electricity at time step \(k\), in EUR/kWh, \(e_{s_{k}}\) is the energy sold to the grid at time \(t\), in kWh, \(C_{s_{k}}\) is the electricity selling price to the grid at time \(t\), in EUR/kWh, \(C_{c}\) is the cost of the contracted power, in EUR/kW-day, \(p_{c}\) is the contracted power from the grid, in kW, \(e_{c_{k}}\) is the energy charged by the battery at time \(t\), in kWh, \(e_{d_{k}}\) is the energy discharged by the battery at time \(t\), \(E_{b}\) is the SESS capacity, in kWh, and \(C_{a}\) is a penalization cost, in EUR/cycle, associated with the battery number of cycles, which depends on the aging, the cyclability, and the total energy storage capacity of the batteries, expressed in  
\[C_{a}=\frac{E_{b}\cdot C_{bn}\cdot F_{br}}{N_{cyc}}, \tag{32}\]  
where \(C_{bn}\) is the battery nominal price, in EUR/kWh, \(F_{br}\) is the battery price reduction factor for second-life applications, in p.u., and \(N_{cyc}\) is the battery remaining cycles of the second-life application, in units. \(J_{en}\) corresponds to the environmental operational costs for the industry, formulated to minimize CO\({}_{2}\)-equivalent emissions. The calculation is based on emission factors aligned with the IPCC 100-year Global Warming Potential (GWP) methodology, which represent cradle-to-grave emissions in kgCO\({}_{2}\)/kWh for grid energy (\(F_{g_{k}}\)), battery usage (\(F_{b}\)), and PV generation (\(F_{pv}\)), being  
\[J_{en}=\sum_{t}(F_{g_{k}}\cdot e_{p_{k}}+F_{b}\cdot(e_{c_{k}}+e_{d_{k}})+F_{pv }\cdot E_{pv_{k}}), \tag{33}\]  
where \(E_{pv_{k}}\) is the photovoltaic energy generated at time \(t\), in kWh. The weighting factor \(\alpha\) defines the trade-off between economic (\(J_{cc}\)) and environmental (\(J_{en}\)) criteria. The terms \(J_{cc}^{*}\) and \(J_{en}^{*}\) denote the reference economic and environmental costs under pure cost minimization (\(\alpha=1\)) and pure environmental minimization (\(\alpha=0\)), respectively.