I assume that your weight functions are nonlinear. If you can set
a priori bounds on the magnitudes of the $a$ and $b$ variables,
you can solve this as a mixed integer linear program.
Assume that $a_{i}\in\lbrace0,\dots,A_{i}\rbrace$ and $b_{i}\in\lbrace B_{i,0},\dots,B_{i,N_{i}}\rbrace.$
Create binary variables $y_{i,j},\,j=0,\dots,A_{i}$ and $z_{i,j},\,j=0,\dots,N_{i}$
to select the values of $a_{i}$ and $b_{j},$ along with auxiliary
continuous variables $s_{i},$ $t_{i},$ $u_{i}$ and $v_{i}.$ Add
the following constraints.
- Select exactly one value for each $a$ and $b$ variable.
$$\sum_{j=0}^{A_{i}}y_{i,j}=1\quad\forall i$$
$$\sum_{j=0}^{N_{i}}z_{i,j}=1\quad\forall i$$
- The $a$ and $b$ variables are determined by the $y$ and $z$ variables
respectively.
$$a_{i}=\sum_{j=0}^{A_{i}}j\cdot y_{i,j}\quad\forall i$$
$$b_{i}=\sum_{j=0}^{N_{i}}B_{i,j}\cdot z_{i,j}\quad\forall i$$
- $u_{i}=w_{1}(a_{i}).$
$$u_{i}=\sum_{j=0}^{A_{i}}w_{1}(j)\cdot y_{i,j}\quad\forall i$$
- $v_{i}=w_{2}(b_{i}).$
$$v_{i}=\sum_{j=0}^{N_{i}}w_{2}(B_{i,j})\cdot z_{i,j}\quad\forall i$$
- $t_{i}=a_{i}x.$
$$y_{i,j}=1\implies t_{i}=j\cdot x\quad\forall i,\forall j=0,\dots,A_{i}$$
- $\hat{n}_{i}=a_i x + b_i.$
$$\hat{n}_{i}=t_i + b_i\quad\forall i$$
- $s_i=\vert n_i - \hat{n}_i\vert.$
$$s_i\ge n_i - \hat{n}_i\quad\forall i$$
$$s_i\ge \hat{n}_i -n_i\quad\forall i$$
The objective function is $\sum_{i}\left(s_{i}+u_{i}+v_{i}\right).$
There are two things to note. First, the final set of constraints
actually says $$s_i \ge \vert n_i - \hat{n}_i \vert .$$
We get equality because the objective function will prevent the solver from inflating the value of $s_i.$ Second, while many modern solvers support implication constraints, if necessary the implication constraint
$$y_{i,j} = 1\implies t_{i}=j \cdot x$$
can be linearized using the "big M" approach, substituting for
the implication the constraints
$$t_i\ge j\cdot x-M_{i,j}(1-y_{i,j})$$
$$t_i\le j\cdot x+M_{i,j}(1-y_{i,j})$$
where $M_{i,j}$ is an upper bound on $\vert a_{i}x-j\cdot x\vert.$