# How to score a given arrangement of windows on a screen to produce good layouts

(this is related to my other question, see here)

I would like to write a function that scores a given arrangement of windows on a screen.

The purpose of this function is to determine whether a particular layout is good and by going over other possible layouts, finding the one with the highest score.

Here are some characteristics that I think make a good layout:

1. maximizing amount of space used by windows (or in other words, the free space on the screen should be minimized)
2. windows are (more or less) evenly sized

Bonus: assigning each window a priority and giving a higher score for layouts where windows with a higher priority take more space.

Here's an example: Suppose our screen is 11x11 and we want to put two windows on it. Window A's initial size is 1x1 and window B is 2x1.

When we resize windows, we preserve their aspect ratio. So here are two possible layout:

The function should give the one on the right a higher score.

Another nice thing to have is the option to 'dock' a window to one or more sides of the screen. Then suppose we want to dock A to the bottom-left of the screen, the scoring function should prefer this layout than the above one on the right:

• Nice problem. It sounds like a variation of en.wikipedia.org/wiki/Cutting_stock_problem or en.wikipedia.org/wiki/Bin_packing_problem to me. – Joe Apr 14 '12 at 20:40
• @daniel.jackson This question doesn't seem very Math-focused if you're looking to write a function. I see that you already got help with designing the algorithm in your previous question, so are you just looking for someone to write the code for you here? You may get a better response by posting to Stack Overflow and including any code you have so far. – Adam Lear Apr 16 '12 at 5:43
• @AnnaLear: Where in any of my questions here have I asked for code? – daniel.jackson Apr 16 '12 at 9:42
• @daniel.jackson This question starts with "I would like to write a function" and the rest seems to be specifying requirements. From there, I figured you were looking for code. If you're not, can you clarify what you are looking for instead please? Thanks! – Adam Lear Apr 16 '12 at 16:36
• @AnnaLear: I believe you misunderstood. I asked how to determine if a given layout is a good one according to some variables I thought are related. If it's easier for someone to convey an idea through actual code, great. – daniel.jackson Apr 16 '12 at 17:03

I will expand on my comment on the other question and propose a target function that ensures that no very small windows occur and that windows are not unnecessarily far away from screen edges. Let $\mathcal{W} = \{1,\dots,n\}$ a set of windows and $T = \mathcal{W} \to \mathbb{N}^4$ a tiling; if $T(W) = (x,y,w,h)$ window $W$'s top-left corner is positioned at $(x,y)$ and $W$ has width $w$ and height $h$. Now let $c : (\mathbb{N}^4)^\mathcal{W} \to \mathbb{R}$ a cost function from the space of all tilings to the reals:
$\qquad \displaystyle c(T) = \left[ \min_{W \in \mathcal{W}} T(W)_3\cdot T(W)_4 \right] - \sum\limits_{W \in \mathcal{W}} \operatorname{empty}(W)$
where $\operatorname{emtpy}(W)$ the maximum number of empty tiles between one of $W$'s edges and the respective nearest screen edges. The objective is maximisation.
$\qquad \displaystyle c(T) = \left( \min_{W \in \mathcal{W}} T(W)_3\cdot T(W)_4 \ ,\ -\sum\limits_{W \in \mathcal{W}} \operatorname{empty}(W)\right)$.