How to organize an office so everyone working there can be comfortable and productive is the topic of much discussion (and the subject of a previous post). One of the key questions is, how do we allocate people to desks (or offices)?

A common strategy is to seat people by their team or sub-team membership (for example, the Algorithms team at Stitch Fix is broadly classed into four sub-teams: Client, Merchandise, Styling, and Platform). This has obvious advantages but potentially misses spontaneous collaboration amongst people on different teams ^{1}. Another strategy which we have been employing is to simply allocate people randomly. This has many benefits, mainly that it is unlikely to be anywhere near an extremum.

If we are to do something other than random, how would it work? There are many considerations, which at a high-level are quite subjective. Things like, how do we organise the different sub-teams? What about the fact some people should be seated near each other? What if individuals prefer certain parts of the office? Although these decisions are subjective, once they have been decided upon, we can bring a lot of objectivity to the problem.

To solve this problem at Stitch Fix, we have built upon our previous seating arrangement efforts and developed a new seating allocation tool - “seetd”. It works by casting the allocation of people to seats (or offices) as an optimization problem, with several different cost-terms which we will discuss below. The ultimate goal is to then find an arrangement which minimises our overall cost function.

## Teams and sub-teams

One big consideration in the allocation is how to arrange sub-teams. Do we want everyone to be evenly dispersed or do we want every sub-team to sit together? An initial model is to consider if neighbouring people are on the same team or not. The term is given by:

where if person sits next to person and is the vector representation of the persons team (i.e. if person is on the same team as person and 0 otherwise). If people are not neighbours, the term will be 0 (due to ), and if they are, the term will only be non-zero if they share the same team vectors . Interestingly, this term has the exact same form as that for the Hamiltonian of a spin-glass (disordered magnetic material), where the magnetic spins (team membership) of atoms at lattice points (think seats), is given by:

where is the interaction term for spins and it is calculated of nearest neighbours.

What does our team-term look like in practice? Below is an animation demonstrating what happens when we have a configuration that minimises the above cost term that promotes moving members of the same team away from each other. In the animation, each block represents a person in a seat (ignoring the fact some people may struggle to reach theirs) and the color represents team membership. Think of it being 4 tables of 9 people, with each table initially consisting of people on the same team. The initial state has all members seated together, and the final state has them mixed.

In the end we actually modify the neighbour term to take into account, not just nearest neighbours, but all neighbours by weighting them by the distance from one another. Now we have,

where

and is the location of person , is the location of seat , modulates the strength of the distance term, and determines which distance norm to use ( is euclidean, manhatten). Additionally, we also break the sum out by team. This allows us to have different interaction terms on a team-by-team basis (i.e. one team sitting together while the others are dispersed). It now looks as follows:

where is a small positive constant (roughly analogous to a gravitational softening parameter). We scale by the number of people in a team (), and take the sum over the set of team-members () for team which allows us to drop the dot product term (taking the sum over the team implies ). The values allow for pushing teams away from each other (), or making them agglomerate ().

Below, we can see what happens when we break the team-term out by sub-team. The animation has three of the teams with , while one team has . The opposite sign promotes agglomeration for that particular team, while the positive sign promotes separation. This aglomeration/dispersion process shares some similiarites with urban segregation models, as discussed in a previous post.

## Preferences and distances

The second term we are going to consider is people’s personal preferences. We could imagine that different people prefer different parts of the office. For example, people may prefer to sit near windows. We can construct a matrix that encapsulates seat assignments at a particular time.

We will do this by constructing an matrix. Each row () of the matrix represents a person, and each column () a seat. The entries of dictate if person is sitting at location (and subsequently take on a value of either 0 or 1). If we want one person per seat and one seat per person then we also require:

We can now also encode people’s preferences for a particular seat with another matrix: . This matrix doesn’t have the same constraints as since people can have multiple preferences, although it may be necessary to constrain the sum of the matrix (meaning people only have a finite amount to allocate for preferences),

If we only consider that we want to maximise people’s preferences then we can develop a cost function that simply consists of maximising the following:

It should be noted that there needs to be heterogeneity in people’s preferences for this term to do anything, otherwise it just ends up being a constant. Maximising (or minimizing) functions such as this are (now) easily solved using linear programming. What happens if we want to consider more things?

One consideration is that people should be seated further away from their current location. For each person, we can fill a row in a matrix () with the distance from each seat to that person’s current location . The aim here is to maximise the distance from the current location, which can also be written as minimizing

where is a small positive constant. It takes exactly the same form as the term for personal preferences, and these can be combined to yield:

where is a constant dictating the strength of the term ( if we want to minimise instead of maximise).

Below is an animation demonstrating what happens when we have a configuration that minimises the above cost term (with ). In the animation, each block represents a person just as the previous example. At the initial stage, everyone is ordered by team and at the final stage the ordering persists, but we can see that the location of each team has moved. This is consistent with the cost term, moving people as far from their original position as possible. We can see the teams on diagonally opposing tables have swapped locations as this is the configuration that minimises this cost term.

## New neighbours

The final factor that goes into our objective function is that people should be seated farther from their current neighbours in the new arrangement. If we take our original seating configuration and calculate an matrix of distances between person and person given by,

where are the locations of person . The goal is then to minimise the following:

where is the person-to-person distances of the original configuration and is the person-to-person distances for the current configuration.

This is then minimised when closer people are moved further away, and farther away people are moved closer.

## Think global. Act local

The next step is to construct our cost function out of the three terms as follows:

where is the scalar weighting of term . One important factor is how to weight the relative strength of the three terms. For our case, we wanted them to be roughly equal. To determine the appropriate ’s, () random configurations were generated to get an average value for each term

The scaling then becomes

Although this is not perfect, it at least puts each term on equal footing for random configurations. Depending on the layout, it may be necessary to alter the power scaling paramter for the distances (i.e. to reduce its effect).

It might also be advantageous to include cost terms related to past configurations. This is easily done by modifying our cost function to now be:

which calculates the cost of the current configuration to past configurations through the term and the strength of these is dictated by (generally ).

## Optimization

So how do we actually get the seating assignments that minimise our cost function? We are going to solve this using a combination of simulated annealing (SA) and local optimization.

SA is probabilistic optimization algorithm (and a variant on the Metropolis-Hastings algorithm) that can be very good at finding good solutions to some difficult problems. SA comes from statistical mechanics and is inspired by the way materials find low-energy atomic configurations. SA is good because it allows us to solve a huge range of problems that may be hard to optimize otherwise, and in this case, allows us the flexibility to add arbitrarily complex constraints to our seating in the future. It is able to find approximate global solutions by moving in directions that increase the cost function.

SA works by changing current potential solutions into a neighbouring state. Rather than simply selecting neighbouring states that lower our cost function, SA accepts worse states with some finite probability. This property allows it to escape minima and explore more of the space. The acceptance probability is related to the difference in the cost function as well as a temperature parameter. A high temperature will result in worse moves being accepted more regularly, while a low temp will rarely result in worse moves being accepted. A common strategy is to *anneal* the temperature as the iterations go on, decreasing the probability of accepting worse solutions with iteration number. To find better solutions, we also include a local search step, which allows us to
improve a solution in some neighbourhood with relative ease (akin to basin hopping).

In the case here, we simply iterate through each seat and its immediate neighbours and accept any change that lowers the cost function. The local optimization part is performed after any new solution is accepted during the SA procedure. Here is some simple python to demonstrate:

```
for it in range(iterations):
X_candidate = change_iterate(X)
new_cost = cost(X_candidate)
accept = prob_f(new_cost, cur_cost, T) >= np.random.uniform(0,1)
if accept:
X = local_optimize(X_candidate)
cur_cost = cost(X)
```

`change_iterate`

is a function that moves the current seating arrangement into a neighbouring state. `prob_f`

is a function
that returns the probability of accepting the new state based on the current cost of this configurtion, the previous configuration cost, and the temperature. A common form is

`local_optimize`

acts as a greedy optimizer, moving into neighbouring states but only ever accepting configurations that lower the cost function. This process is repeated for a pre-defined number of iterations.

Below is an animation of seetd in action, optimizing the cost function we built (ignoring peoples preferences). Each color represents an individual and each block is a seat (sub-team membership is not shown but exists). The green space are aisles (or empty seats).

## Conclusions

Although optimizing seating is something of a curiosity at first, it becomes apparent that the optimization shares a lot of similarities with real-world problems, from economics and logistics to urban segregation and magnetism. Here we have developed a fun tool to help us optimally allocate people. The full working code can be found here [2].