I think the question you are trying to ask is this: If we have a set of linear constraints involving a variable $x$, how can we introduce $|x|$ (the absolute value of $x$) into the objective function?
Here is the trick. Add a constraint of the form
$$t_1 - t_2 = x$$
where $t_i \ge 0$.
The Simplex Algorithm will set $t_1 = x$ and $t_2 = 0$ if $x \ge 0$; otherwise, $t_1 = 0$ and $t_2 = -x$. So $t_1 + t_2 =|x|$ in either case.
On the face of it, this trick shouldn't work, because if we have $x = -3$, for example, there are seemingly many possibilities for $t_1$ and $t_2$ other than $t_1 = 0$ and $t_2 = 3$; for example, $t_1 = 1$ and $t_2 = 4$ seems to be a possibility. But the Simplex Algorithm will never choose one of these "bad" solutions because it always chooses a vertex of the feasible region, even if there are other possibilities.
EDIT added Mar 29, 2019
For this trick to work, the coefficient of the absolute value in the objective function must be positive and you must be minimizing, as in
min $2(t_1+t_2)+\dots$
or the coefficient can be negative if you are maximizing, as in
max $-2(t_1+t_2)+\dots$
Otherwise, you end up with an unbounded objective function, and the problem must be solved by other methods, e.g. mixed integer-linear programming.
(If I knew this before, I had forgotten. Thanks to Discretelizard for pointing this out to me.)
Let $m = \min_{i \in \{1,\dots,n\}} a_i$. For $p\in\{1,\dots,m\}$, let binary variable $z_p$ indicate whether $p$ is the GCD. The problem is to maximize $\sum_{p=1}^m p\ z_p$ subject to
\begin{align}
\sum_{p=1}^m z_p &= 1 \\
(a_i-p\ a_i)(1 - z_p) \le a_i - p\ q_i &\le a_i (1 - z_p) &&\text{for $i \in \{1,\dots,n\}$} \\
q_i &\in [0,a_i] \cap \mathbb{N} &&\text{for $i \in \{1,\dots,n\}$}\\
z_p &\in \{0,1\} &&\text{for $p \in \{1,\dots,m\}$}\\
\end{align}
Best Answer
Here's a simple example to get you started:
$\min | x-3 | $
subject to
$x \leq 2$.
The key to formulating these problems is introducing auxilliary variables and additional constraints. Add the constraints
$t \geq (x-3)$
and
$t \geq -(x-3)$.
Since $|x-3|$ is either $+(x-3)$ or $-(x-3)$, these constraints ensure that
$t \geq | x-3|$.
Now, minimize $t$. This will ensure that $t$ is no bigger than $|x-3|$.
So, our problem is
$\min t$
subject to
$t \geq (x-3)$
$t \geq -(x-3)$
$x \leq 2$.
It's important to understand that this technique doesn't work to maximize $|x-3|$.
You can easily extend this to more than one absolute value term, but you'll need an additional variable for each term.