Optimizing the width and position of a group of rectangles so their edges are aligned

I'm unsure of how to formulate my problem as an optimisation, any suggestion would be greatly appreciated.

I have a group of axis aligned rectangles that are all bounded by one larger rectangle. Each rectangle has an initial (x,y) centroid, height (fixed) and width.

The larger rectangle has a number of guides lines (0/4, 1/4, 1/3, 1/2, 2/3, 3/4, 4/4 vertical lines) that can be activated, but for each activated guide line there is an additional cost.

For each of the bounded rectangles I want to allow small changes in the centroid's x parameter and the width parameter, so the the rectangle width edges align to a guide line but for as few guide lines are activated as possible.

Based on your description of the problem I've reached the following MILP. I only considered the horizontal axis, assuming that the vertical axis is fixed. Note that is not applicable in the current form.

Let $S$ be the set of rectangles and $G$ the set of guidelines. For each rectangle $s \in S$ let:

• $c^s$ be the center of $s$,
• $w^s_L$ and $w^s_L$ be left/right border of $s$.

For each guideline $g\in G$ let:

• $p^g$ be the position of guideline.
• $b^g$ be the binary variable that gives the activation of the guideline $g$

Finally, for each $g$ and each $s$, the binary variables $b^{s,g}_L$ and $b^{s,g}_R$ are the variables that indicated that the left/right border of $s$ has been assigned to the guideline $g$.

• (4) and (5), for each $s$ and border side (left/right) only guideline can be assigned.
• it is allowed to a rectangle to have width zero, therefore the solution will be only one guideline active. This can be avoided by adding a constraint $w^s_L-w_R^s\geq w^s_{\min}$.
• there is no cost for stretching the rectangles or moving the centroid, therefore the solution will be always two guidelines active. You could add some parameters that represent the initial position and width of the rectangles which allows you to calculate the variations and penalize it. Avoid using the $\ell_2$ norm to calculate the variations, otherwise your problem will become a MIQP. There are ways to model absolute value ($\ell_1$ norm) using linear constraints.