Hacker News new | past | comments | ask | show | jobs | submit login
Solving a Combination Lock Puzzle with JuMP and Julia (iaindunning.com)
28 points by idunning on Sept 22, 2013 | hide | past | favorite | 10 comments



Haskell Solution:

  [(a,b,c,d,e,f) | a <- [39, 90, 75, 15, 57], b <- [9, 2, 58, 68, 48, 64], c <- [91, 29, 55, 16, 67, 8], d <- [22, 32, 25, 40, 54, 66], e <- [41, 14, 30, 49, 01, 17], f <- [44, 63, 10, 83, 46, 03], a+b+c+d+e+f == 419 ]


Both this and the Mathematica solution linked below [1] are brute force, which strikes me as far less interesting than using linear programming. Of course, at this size of problem brute force works fine. You can write a brute force version easily in any language, including, of course, Julia:

    julia> function brute_force(P,t)
             for x1=P[1,:], x2=P[2,:], x3=P[3,:], x4=P[4,:], x5=P[5,:], x6=P[6,:]
               x1 + x2 + x3 + x4 + x5 + x6 == t || continue
               println("solution: $x1 $x2 $x3 $x4 $x5 $x6")
             end
           end
    brute_force (generic function with 1 method)

    julia> brute_force(P,419)
    solution: 90 68 91 66 41 63
    solution: 90 48 91 66 41 83
    solution: 90 64 67 66 49 83
    solution: 88 68 91 40 49 83
The interesting thing about Iain's linear programming approach is that you can write the problem down the way it is defined and get a solution efficiently.

[1] https://news.ycombinator.com/item?id=6425633


There is a hidden scrollbar there for me. This could go on multiple lines, too.


What if you want to solve another matrix?

Do you have to write another program?


Could just take all the numbers as command-line arguments or something similar.


I also find it fascinating to read the algebraic solutions of others. This algorithm seems to rely on an open source integer problem solver called "CBC" [1]. But is it necessary for a general 'searching solver' of that kind to be used for this particular problem?

The way in which the problem is framed suggests there is a unique solution. The model is presented as a square matrix subject to linear constraints. There are 36 unknowns, being x[1][1] through to x[36][36], each of which is 0 or 1. There is the constraint that the sum of the unknowns is 419. And there are two constraints to ensure that only one number is chosen from each row and each column, which together give rise to 36 constraint equations.

If any one of the latter constraints is replaced by the sum constraint, there is a linear system of 36 equations in 36 unknowns. Is it possible for that system to be constructed and solved directly through matrix inversion?

(For those trying the exercise at home, the number at P[1,2] appears as 90 but from the full problem statement I think it should be 6.)

[1]: https://projects.coin-or.org/Cbc


Unfortunately not. Matrix inversion (or any type of linear algebra, really) deals with real numbers. This problem is nearly a linear program, which can be solved very efficiently, except that some variables are constrained to { 0, 1 }. As a result, it's an integer program, which is NP-hard. Intuitively, this is hard because there are no derivatives (e.g. gradient, Hessian) to exploit in discrete optimization problems.

One heuristic is to solve an IP is to relax the integer constraint to inequality constraints, solve the LP, and round the results. However, this can do arbitrarily poorly on most problems.


Algebraic solutions are fascinating. I was hooked on Sage for a week or two solving similar problems. I solved this one using brute force though:

    array<array<int, 6>, 6> tumblers
      = {{{39, 90, 75, 88, 15, 57}, {9,   2, 58, 68, 48, 64},
          {29, 55, 16, 67,  8, 91}, {40, 54, 66, 22, 32, 25},
          {49,  1, 17, 41, 14, 30}, {44, 63, 10, 83, 46,  3}}};

    template <typename It> bool
    solve (It tumbler, It end, int const sum) {
        if (tumbler == end)
            return (sum == 0);
        for (auto const pin: *tumbler) {
            if (solve (next(tumbler), end, sum - pin)) {
                std::cout << pin << std::endl;
                return true;
            }
        }
        return false;
    }

    int main() {
        solve (tumblers.rbegin(), tumblers.rend(), 419);
    }


As a note, this kind of problem is NP complete in the number of locks because the subset sum problem can be embedded into it.


not quite sure i understand the problem, but here's one Mathematica approach:

  m = {{39, 90, 75, 88, 15, 57},
     {9, 2, 58, 68, 48, 64},
     {29, 55, 16, 67, 8, 91},
     {40, 54, 66, 22, 32, 25},
     {49, 1, 17, 41, 14, 30},
     {44, 63, 10, 83, 46, 3}};

  Select[
   Extract[m, #] & /@ Transpose[{Range[6], #}] & /@ Tuples[Range[6], 6],
   Total[#] == 419 &]




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: