I want to set a variable $D$ to a certain value based on another variable $X$ depending on the range of $X$ as follows:
if $0<x \le100 , D =2$
if $100<x \le200 , D =5.3$
if $200<x \le300 , D =1.2$
I know how to do a value based on one range (as was answered for different question "Modelling a range constraint based on a value" but I do not know how to apply it for different ranges. please help.
We can denote each range of values for $x$ as binary variables $u_1, u_2$, and $u_3$ with the following constraint
$$u_1 + u_2 + u_3 =1$$
Then, we will need another constraint that represents $x$ with: $$x\le 100u_1 + 200u_2 + 300u_3$$ $$x > 0u_1 + 100u_2 + 200u_3$$ or, if we don’t want $x$ to be as free (in that we want the value for $x$ to determined by the picked ranges, and not by $x$ itself), then
$$x=100u_1 + 200u_2 + 300u_3$$
Lastly, our objective function will then become the following: $$D=2u_1 + 5.3u_2 + 1.2u_3$$
Here’s supplemental material that talks about modeling piecewise functions.