(model_doc_price_elasticity)=
# Demand
Demand can be divided in two major categories: *price flexible* and *firm*. So far we have treated price flexible demand as a market step for which a fixed capacity and marginal value is defined.
The firm demand enters the right hand side of the power balance (as the parameter $ Load $).
## Price Elastic Firm Demand
Price elasticity can be added to the firm-demand by describing it as an exponential function or a piece-wise linear curve.
### Exponential function
Price elastic steps can be generated using an exponential function:
$$ W(\pi) = \Bigr( \frac{\pi} {\pi_{n}} \Bigr)^{e} $$
Where $ W $ is the firm demand in percentage of the normal consumption in decimal form, $ \pi $ is the current power price, $ \pi_n $ is the power price given normal consumption and $ e $ is the exponent.
### Defining the Price Elastic Demand
Four parameters are required: $ \pi_n $, $ \overline{\pi} $, $ \underline{\pi} $, $ e $.
We build one set of purchase options and one set of sales options to cover deviations from the firm load, as described in the following and illustrated in the figure below.

The part from $ \overline{\pi} $ to $ \pi_n $ is considered by establishing a set of additional purchase options. This can be seen as a set of fictitious thermal generators that can be started to reduce the load if prices are favorable, as illustrated by the blue steps in the fgure.
We generate $ N^+ = \lceil \frac{W(\pi_n) - W(\overline{\pi})} {D_w} \rceil $ steps of size $ \delta_{Load}^+ $:
$$ \delta_{Load}^+ = \frac{W(\pi_n) - W(\overline{\pi})}{N^+} $$
Where for each purchase step $ s = 1,2,...,N^+ $:
$$ C_s^+ = \pi_n * W(\pi_s)^{\frac{1}{e}} = \pi_n * \Bigr( W(\overline{\pi})+ \frac{1}{2}\bigr(2s-1 \bigr) * \delta_{Load}^+ \Bigr)^{\frac{1}{e}} $$
Any steps where $ C_s^+ \ge \textrm{MaxRationing} $ will be discarded.
The part from $ \pi_n $ to $ \underline{\pi} $ is considered by establishing a set of additional sales options. This can be seen as a set of fictitious demands that can be covered if prices are favorable, as illustrated by the orange steps in the fgure.
We generate $ N^- = \lceil 50 * \Bigr( W(\underline{\pi}) - W(\pi_n) \Bigr) \rceil $ steps of size $ \delta_{Load}^- $:
$$ \delta_{Load}^- = \frac{W(\underline{\pi}) - W(\pi_n)}{N^-} $$
Where for each sales step $ s = 1,2,...,N^- $:
$$ C_s^- = \pi_n * W(\pi_s)^{\frac{1}{e}} = \pi_n * \Bigr( W(\pi_n)+ \frac{1}{2}\bigr(2s-1 \bigr) * \delta_{Load}^- \Bigr)^{\frac{1}{e}} $$
### Piece-wise linear curve
Price elastic steps can be generated by using a piece-wise linear curve. The input is a set of pairs; price $ (P) $ and consumption $ (W) $, where W is the consumption percentage in decimal form relative to normal consumption.
E.g.
| Index | Price | W |
|-------|-------|------|
| 1 | 70.0 | 0.5 |
| 2 | 22.0 | 1.0 |
| 3 | 10.0 | 1.20 |
Where the set must:
* Be in increasing order.
* Include at least two entries.
* Include an entry where W = 1.0 (normal consumption).
Two constants $ D_w $ and $ D_p $ are used to influence the granularity of the generated steps. Default values are 0.02 and 0.005, respectively.
### Generating steps
We build one set of purchase options and one set of sales options to cover deviations from the firm load, as described in the following and illustrated in the figure below.

For each $ i = 1,2,...,\text{num_inputs}-1 $, we'll generate $ N(i) $ price elastic steps. Where:
$ N(i) = minimum(\lceil \Bigr( W(i+1) - W(i) \Bigr) * \frac{1} D_w \rceil , \lceil \Bigr( Price(i+1) - Price(i) \Bigr) * \frac{1} D_p \rceil) $
Where for each $ k = 1,2,...,N(i) $ the cost will be:
$$ C_n = \frac{((2*(N(i)-k)+1)*P(i)) + ((2 * k - 1 ) * P(i+1))} {2 * N(i)} $$
whereas the load fraction will be the same across each $ k $:
$$ \delta_{Load}(i) = \frac{W(i+1) - W(i)}{N(i)} $$
Where $ \delta_{Load}(i) $ negated, if $ W(i+1) \gt 1.0 $.
Any steps where $ C_n \ge \textrm{MaxRationing} $ will be discarded.
### Contribution to the LP problem
First we define the total capacity of each price elastic step:
$$ \forall k \in Timesteps $$
$$ \textrm{TotalPurchaseCapacity}(k) = \delta_{Load}^+ * Load(k) $$
$$ \textrm{TotalSalesCapacity}(k) = -\delta_{Load}^- * Load(k) \qquad $$
Sometimes it can be reasonable to assume that the consumer demand does not change immediately in response to higher or lower prices, but rather adjust gradually over time when the price level is high or low. To model this we have implemented three types of responses to changes in demand: momentary, asymptotic and linear.
We split the total capacity of each price elastic step into three parts:
| Part | Description |
|---------|-------------|
|$ \textrm{FlexibleCapacity} $| Part of the total capacity that can be used based on the current power price. |
|$ \textrm{InflexibleUsedDemand} $| Part of the total capacity that is consumed no matter how high the price becomes. |
|$ \textrm{InflexibleUnusedDemand} $| Part of the total capacity that is not consumed nomatter how low the price becomes. |
The flexible capacity constrains the purchase and sales variables:
$$ \forall \enspace s \in Steps,\ k \in Timesteps $$
$$ 0 \leq purchase (s,k) \leq \textrm{FlexibleCapacity}(s,k) $$
$$ \textrm{FlexibleCapacity}(s, k) \leq sales (s,k) \leq 0 $$
Inflexible used demand is subtracted from the right hand side of the power balance:
$$ \textrm{POWBAL}(a,k) [RHS]: -= \textrm{PriceElasticFixedDemand}(s,k) $$
The objective function is updated with cost and capacities:
$$ Cost: += \sum_{s,k} C_s^+*purchase(s,k) + \sum_{s,k} C_s^-*sales(s,k)$$
where $ purchase(s,k) $ or $ sales(s,k) $ is the variable representing how much energy is consumed by the flexible capacity, depending on whether the price elastic step is a purchase or sales option.
The three response types differ in how the three capacity parts are calculated. Before showing the equations we define the total consumption of a step to be either
$$ \textrm{TotalConsumption}(k) = \textrm{InflexibleUsedDemand}(k) + purchase(k) $$
or
$$ \textrm{TotalConsumption}(k) = \textrm{InflexibleUsedDemand}(k) + sales(k) $$
and $ \textrm{TotalCapacity} $ to be $ \textrm{TotalPurchaseCapacity} $ or $ \textrm{TotalSalesCapacity} $ depending on the step type.
#### Momentary
The price elastic demand immediately changes according to price level.
$$\textrm{InflexibleUsedDemand}(k) = 0$$
$$\textrm{InflexibleUnusedDemand}(k) = 0$$
$$\textrm{FlexibleCapacity}(k) = \textrm{TotalCapacity(k)}$$
#### Asymptotic
The price elastic demand approaches zero asymptotically when prices stay high over time and maximum capacity when prices stay low.
$$ \textrm{InflexibleUsedDemand}(k) = \alpha(k) \textrm{TotalConsumption}(k - \Delta k) $$
$$ \textrm{InflexibleUnusedDemand}(k) = \alpha(k) (\textrm{TotalCapacity}(k) - \textrm{TotalConsumption}(k - \Delta k) $$
$$ \textrm{FlexibleCapacity}(k) = \textrm{TotalCapacity}(k) (1-\alpha(k)) $$
where $ \alpha(k) \in [0,1] $ is an inertia parameter and $ \Delta k > \text{DecisionProblemLength} $ is how far back in time to find the previous consumption.
If $ \textrm{InflexibleUnusedDemand}(k) < 0 $ and the price elastic step is a purchase option or $ \textrm{InflexibleUnusedDemand}(k) < 0 $ and the step is a sales option we replace part of the above equations with
$$ \textrm{InflexibleUnusedDemand}(k) = 0 $$
$$ \textrm{InflexibleUsedDemand}(k) = \alpha(k) \textrm{TotalCapacity}(k) $$
#### Linear
The price elastic demand approaches zero linearly when prices stay high over time and maximum capacity when prices stay low.
$$ \textrm{InflexibleUsedDemand}(k) = \textrm{TotalConsumption}(k - \Delta k) - (1 - \beta(k)) \textrm{TotalCapacity(k)} $$
$$ \textrm{FlexibleCapacity}(k) = 2(1-\beta(k)) \textrm{TotalCapacity}(k) $$
where $ \beta(k) \in [0,1] $ is an inertia parameter and $ \Delta k > \text{DecisionProblemLength} $ is how far back in time to find the previous consumption.
If $ \textrm{InflexibleUsedDemand(k)} < 0 $ and the price elastic step is a purchase option or $ \textrm{InflexibleUsedDemand(k)} > 0 $ and the step is a sales option we replace the above equations with
$$ \textrm{InflexibleUsedDemand}(k) = 0 $$
$$ \textrm{FlexibleCapacity}(k) = (1 - \beta(k)) \textrm{TotalCapacity} $$
and if $ \textrm{TotalConsumption}(k) > \textrm{TotalCapacity} $ and the price elastic step is a purchase option or $ \textrm{TotalConsumption}(k) < \textrm{TotalCapacity} $ and the step is a sales option we use
$$ \textrm{FlexibleCapacity}(k) = \textrm{TotalCapacity(k)} - \textrm{InflexibleUsedDemand}(k) $$
### Example using exponential function
Four parameters are required: , , , $ e $.
$$ \pi_n = 37.3 $$
$$ \overline{\pi} = 900.0 $$
$$ \underline{\pi} = 7.0 $$
$$ e = -0.025 $$
$$ D_w = 0.02 $$
$$ D_p = 0.005 $$
$$ \textrm{MaxRationing} = 900.0 $$
leads to
$$ W(P_{\textrm{upper_limit}}) = 0.9235 $$
$$ W(P_{\textrm{lower_limit}}) = 1.0427 $$
$$ N^+ = 4 $$
$$ \delta^+ = 0.0191251 $$
$$ N^- = 3 $$
$$ \delta^+ = 0.0142381 $$
which creates the following steps:
| Price | Load part |
| ----------:| -------------:|
| 596.0570 | 0.0191 |
| 264.7376 | 0.0191 |
| 119.4959 | 0.0191 |
| 54.7806 | 0.0191 |
| 28.0852 | -0.0142 |
| 16.0179 | -0.0142 |
| 9.2068 | -0.0142 |
### Example using piece-wise linear function:
| Index | Price | W |
|------:|------:|-----:|
| 1 | 900.0 | 0.80 |
| 2 | 100.0 | 0.95 |
| 3 | 37.5 | 1.0 |
| 4 | 15.0 | 1.03 |
$$ D_w = 0.02 $$
$$ D_p = 0.005 $$
creates the following steps:
| Piece index | Price | Load part |
|------------:|----------:|----------:|
| 1 | 850.0000 | 0.01875 |
| 1 | 750.0000 | 0.01875 |
| 1 | 650.0000 | 0.01875 |
| 1 | 550.0000 | 0.01875 |
| 1 | 450.0000 | 0.01875 |
| 1 | 350.0000 | 0.01875 |
| 1 | 250.0000 | 0.01875 |
| 1 | 150.0000 | 0.01875 |
| 2 | 89.5833 | 0.01667 |
| 2 | 68.7500 | 0.01667 |
| 2 | 47.9167 | 0.01667 |
| 3 | 31.8750 | -0.01500 |
| 3 | 20.6250 | -0.01500 |