# How to transform a (thermal) range constraint into the objective function

I have an mixed-integer linear optimization problem that includes a energetic difference equation for the temperature of a building for the time $$t$$:

$$T(t) = T(t-1) + \frac{E^{\rm Heating}(t)-E^{\rm Demand}(t)}{V\cdot \rho\cdot c}, t>1$$

I used to have the temperature in the constraints with a minimum and maximum value:

$$T_{\min}\leq T(t) \leq T_{\max}$$

Now I would like to transform it from a constraint to the objective function. So what I want is to quantify the violation of this "constrain" $$Z$$ and minimize it in the objective function. So the equations should actually look like this:

\begin{align}\text{if} \hspace{0,5cm}T(t) < T_{\min} &\implies Z(t) = T_{\min} - T(t)\\\text{if} \hspace{0,5cm}T(t) > T_{\max} &\implies Z(t) = T(t) - T_{\max}\end{align}

And in the objective function $$\min \sum_{t} Z(t)$$.

Now my question is, whether this is possible to define within a mixed-integer linear problem and if so, how can I do that?

Introduce nonnegative variables $$Z^+(t)$$ and $$Z^-(t)$$, impose linear constraints $$T_\min \le T(t)-Z^+(t)+Z^-(t) \le T_\max,$$ and minimize $$\sum_t (Z^+(t)+Z^-(t)).$$ Notice that this approach easily allows penalizing overage and underage differently, if you want, just by changing the objective coefficients.