A somewhat odd (to me) question was asked on a forum recently. Assume
that you have continuous variables that are subject
to some constraints. For simplicity, I'll just write .
I'm going to assume that is compact, and so in particular
the are bounded. The questioner wanted to know how to model
the problem of minimizing the median of the values .
I don't know why that was the goal, but the formulation is mildly
interesting and fairly straightforward, with one wrinkle.
The wrinkle has to do with whether is odd or even. Suppose that
we sort the components of some solution , resulting in what
is sometimes called the "order statistic": .
For odd , the median is For even ,
it is usually defined as
The odd case is easier, so we'll start there. Introduce new binary
variables and a new continuous variable ,
which represents the median value. The objective will be to minimize
. In addition to the constraint , we use "big-M"
constraints to force to be at least as large as half the sample
(rounding "half" up). Those constraints are:
with the sufficiently large positive constants. The last
constraint forces for exactly of the indices
, which in turn forces for of the
. Since the objective minimizes , will be 0 for
the smallest of the and will be no
larger than the smallest of them. In other words, we are guaranteed
that in the optimal solution , i.e., it
is the median of the optimal .
If is even and if we are going to use the standard definition
of median, we need twice as many added variables (or at least that's
the formulation that comes to mind). In addition to the ,
let also be binary variables, and replace
with a pair of continuous variables , . The objective
becomes minimization of their average subject to
the constraints
where the are as before. The constraints force to
be at least as large as of the and
to be at least as large as of them. The minimum objective
value will occur when and .