I’m a newbie with Excel solver and only just learned about it after picking up a data science book. I’d like to get more familiar with this tool, so I’ve been trying my hand at solving different problems. I’m stuck on one though, and I’m not even sure if it’s possible to use solver for? Basically, the constraint I need to check for is whether two cells are adjacent.

My problem: I have a bunch of bags containing different numbers of marbles. I want to maximize the number of marbles I get by picking bags, but they can’t be adjacent to each other.

This is what I have in the spreadsheet:

- Value = number of marbles in bag
- Choose = whether to choose the bag or not (binary)
- Violation = (Choose*Bag Number) of Bag 1 – (Choose*Bag Number) of Bag 2

In the case where I pick up two adjacent bags, violation will = -1.

```
+------------+----+----+----+---+---+-------------+
| Bag Number | 1 | 2 | 3 | 4 | 5 | Total Value |
+------------+----+----+----+---+---+-------------+
| Value | 10 | 20 | 30 | 40| 50| 150|
| Choose | 0 | 0 | 0 | 0 | 0 | 0|
| Violation | 0 | 0 | 0 | 0 | | |
+------------+----+----+----+---+---+-------------+
```

Optimal solution:

```
+------------+----+----+----+---+---+-------------+
| Bag Number | 1 | 2 | 3 | 4 | 5 | Total Value |
+------------+----+----+----+---+---+-------------+
| Value | 10 | 20 | 30 | 40| 50| 150|
| Choose | 1 | 0 | 1 | 0 | 1 | 90|
| Violation | 1 | -3 | 3 |-5 | | |
+------------+----+----+----+---+---+-------------+
```

I’ve tried a combination of a few constraits:

- Putting a binary constraint on the choose row
- Violation >=0 and Violation <=-2
- Total target value <= Total possible value (150)

I made up this problem for myself. Is this even feasible?

Yes, the problem is well-posed.

I would suggest a different way to formulate the adjacency constraints. In particular, I would use the following:

```
choose_1 + choose_2 <= 1
choose_2 + choose_3 <= 1
choose_3 + choose_4 <= 1
choose_4 + choose_5 <= 1
```

These indicate that at most one from each pair of `(1,2), (2,3), (3,4)`

and `(4,5)`

can be selected. It has the advantage that it does not utilize the bag number, which in general can be a bag name (i.e., a string instead of a number). It also has another benefit: We do not need to define the variables as binary, but only as continuous and between 0 and 1: `0 <= choose_i <= 1`

, for all `i = 1,...,5`

. This is because the resulting constraint matrix is Totally Unimodular, which means that solving the linear programming relaxation of the binary problem gives an optimal solution in which `choose_i`

are all either `0`

or `1`

.

Here is my **spreadsheet layout**:

Note that it is good practice to use different colors to differentiate between variables (green), constraints (red) and data (blue). I also mark the objective cell with green font.

Here are the **formulas**:

And here is the **solver model**:

**Solution**:

Note that the fact that the matrix is totally unimodular is a guarantee that the optimal solution will have binary values. In general this is not true and we need to define the variables as binary and resort to branch and bound.

I hope this helps. Happy modeling!

Tags: excelexcel