2018-02-19 15:21:05 +01:00
|
|
|
# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
|
2010-10-27 05:50:44 +00:00
|
|
|
#
|
2012-01-16 10:40:52 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the 'License');
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
2010-10-27 05:50:44 +00:00
|
|
|
#
|
2012-01-16 10:40:52 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2010-10-27 05:50:44 +00:00
|
|
|
#
|
2012-01-16 10:40:52 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an 'AS IS' BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2010-10-27 05:50:44 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Safe cracking puzzle in Google CP Solver.
|
|
|
|
|
|
|
|
|
|
From the Oz Primer:
|
|
|
|
|
http://www.comp.nus.edu.sg/~henz/projects/puzzles/digits/index.html
|
|
|
|
|
'''
|
2012-01-16 10:40:52 +00:00
|
|
|
The code of Professor Smart's safe is a sequence of 9 distinct
|
2010-10-27 05:50:44 +00:00
|
|
|
nonzero digits C1 .. C9 such that the following equations and
|
|
|
|
|
inequations are satisfied:
|
|
|
|
|
|
|
|
|
|
C4 - C6 = C7
|
|
|
|
|
C1 * C2 * C3 = C8 + C9
|
|
|
|
|
C2 + C3 + C6 < C8
|
|
|
|
|
C9 < C8
|
|
|
|
|
|
|
|
|
|
and
|
2012-01-16 10:40:52 +00:00
|
|
|
|
2010-10-27 05:50:44 +00:00
|
|
|
C1 <> 1, C2 <> 2, ..., C9 <> 9
|
|
|
|
|
|
|
|
|
|
can you find the correct combination?
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
Compare with the following models:
|
|
|
|
|
* MiniZinc: http://www.hakank.org/minizinc/safe_cracking.mzn
|
|
|
|
|
* ECLiPSe : http://www.hakank.org/eclipse/safe_cracking.ecl
|
|
|
|
|
* SICStus : http://www.hakank.org/sicstus/safe_cracking.pl
|
|
|
|
|
* Gecode: http://hakank.org/gecode/safe_cracking.cpp
|
|
|
|
|
|
2018-02-19 15:21:05 +01:00
|
|
|
This model was created by Hakan Kjellerstrand (hakank@gmail.com)
|
2014-05-22 20:13:16 +00:00
|
|
|
Also see my other Google CP Solver models:
|
|
|
|
|
http://www.hakank.org/google_or_tools/
|
2010-10-27 05:50:44 +00:00
|
|
|
"""
|
2016-01-15 00:18:32 +01:00
|
|
|
from __future__ import print_function
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
2010-10-27 05:50:44 +00:00
|
|
|
|
|
|
|
|
|
2012-01-16 10:40:52 +00:00
|
|
|
def main():
|
|
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
# Create the solver.
|
|
|
|
|
solver = pywrapcp.Solver('Safe cracking puzzle')
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
#
|
|
|
|
|
# data
|
|
|
|
|
#
|
|
|
|
|
n = 9
|
2016-01-15 00:18:32 +01:00
|
|
|
digits = list(range(1, n + 1))
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
#
|
|
|
|
|
# variables
|
|
|
|
|
#
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
LD = [solver.IntVar(digits, 'LD[%i]' % i) for i in range(n)]
|
|
|
|
|
C1, C2, C3, C4, C5, C6, C7, C8, C9 = LD
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
#
|
|
|
|
|
# constraints
|
|
|
|
|
#
|
|
|
|
|
solver.Add(solver.AllDifferent(LD))
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
solver.Add(C4 - C6 == C7)
|
|
|
|
|
solver.Add(C1 * C2 * C3 == C8 + C9)
|
|
|
|
|
solver.Add(C2 + C3 + C6 < C8)
|
|
|
|
|
solver.Add(C9 < C8)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
solver.Add(LD[i] != i + 1)
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
#
|
|
|
|
|
# search and result
|
|
|
|
|
#
|
|
|
|
|
db = solver.Phase(LD,
|
|
|
|
|
solver.INT_VAR_DEFAULT,
|
|
|
|
|
solver.INT_VALUE_DEFAULT)
|
2012-01-16 10:40:52 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
solver.NewSearch(db)
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
num_solutions = 0
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
while solver.NextSolution():
|
|
|
|
|
num_solutions += 1
|
2016-01-15 00:18:32 +01:00
|
|
|
print('LD:', [LD[i].Value() for i in range(n)])
|
2012-01-16 10:40:52 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
solver.EndSearch()
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2016-01-15 00:18:32 +01:00
|
|
|
print()
|
|
|
|
|
print('num_solutions:', num_solutions)
|
|
|
|
|
print('failures:', solver.Failures())
|
|
|
|
|
print('branches:', solver.Branches())
|
|
|
|
|
print('WallTime:', solver.WallTime(), 'ms')
|
2010-10-27 05:50:44 +00:00
|
|
|
|
2012-01-16 10:40:52 +00:00
|
|
|
|
2010-10-27 05:50:44 +00:00
|
|
|
if __name__ == '__main__':
|
2014-05-22 20:13:16 +00:00
|
|
|
main()
|