cry p arithmetic

4
2.2. The cryptarithmetic puzzle problem and a first model Now that your system is up and running (if not, see Getting started), let us solve a cryptarithmetic puzzle with the help of the or-tools library. In this section, we describe the problem and propose a first model to solve it. This model is by no means efficient but allows us a gentle introduction to the library. A better model is presented in the corresponding lab session. 2.2.1. Description of the problem A cryptarithmetic puzzle is a mathematical game where the digits of some numbers are represented by letters (or symbols). Each letter represents a unique digit. The goal is to find the digits such that a given mathematical equation is verified[1]. [1] This the mathematical term to specify that the equation is true. Here is an example: CP + IS + FUN --------- = TRUE One solution is C=2 P=3 I=7 S=4 F=9 U=6 N=8 T=1 R=0 E=5 because 23 + 74 + 968 --------- = 1065 Ideally, a good cryptarithmetic puzzle must have only one solution[2]. We derogate from this tradition. The above example has multiple solutions. We use it to show you how to collect all solutions of a problem. [2] Like the famous SEND + MORE = MONEY ... in base 10. Page 1 of 3 2.2. The cryptarithmetic puzzle problem and a first model or-tools User's Manual Mon,05-May-14 http://or-tools.googlecode.com/svn/trunk/documentation/user_manual/manual/first_s...

Upload: ashish-gope

Post on 06-May-2017

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Cry p Arithmetic

2.2. The cryptarithmetic puzzle problem and a first modelNow that your system is up and running (if not, see Getting started), let us solve a cryptarithmetic puzzle with the help of the or-tools library.

In this section, we describe the problem and propose a first model to solve it. This model is by no means efficient but allows us a gentle introduction to the library. A better model is presented in the corresponding lab session.

2.2.1. Description of the problem

A cryptarithmetic puzzle is a mathematical game where the digits of some numbers are represented by letters (or symbols). Each letter represents a unique digit. The goal is to find the digits such that a given mathematical equation is verified[1].

[1] This the mathematical term to specify that the equation is true.

Here is an example:

C P+ I S+ F U N---------= T R U E

One solution is C=2 P=3 I=7 S=4 F=9 U=6 N=8 T=1 R=0 E=5because

2 3+ 7 4+ 9 6 8---------= 1 0 6 5

Ideally, a good cryptarithmetic puzzle must have only one solution[2]. We derogate from this tradition. The above example has multiple solutions. We use it to show you how to collect all solutions of a problem.

[2] Like the famous SEND + MORE = MONEY ... in base 10.

Page 1 of 32.2. The cryptarithmetic puzzle problem and a first model — or-tools User's Manual

Mon,05-May-14http://or-tools.googlecode.com/svn/trunk/documentation/user_manual/manual/first_s...

Page 2: Cry p Arithmetic

2.2.2. How to solve the problem?

We follow the classical The three-stage method.

2.2.2.1. Describe

The first stage is to describe the problem, preferably in natural language. What is thegoal of the puzzle? To replace letters by digits such that the sumCP+IS+FUN=TRUE is verified.

What are the unknowns (decision variables)? The digits that the letters represent. In other words, for each letter we have one decision variable that can take any digit as value.

What are the constraints? The obvious constraint is the sum that has to be verified. But there are other - implicit - constraints. First, two different letters represent two different digits. This implies that all the variables must have different values in a feasible solution. Second, it is implicit that the first digit of a number can not be 0. Letters C, I, F and Tcan thus not represent 0. Third, there are 10 letters, so we need at least 10 different digits. The traditional decimal base is sufficient but let’s be more general and allow for a bigger base. We will use a constant kBase. The fact that we need at least 10 digits is not really a CP constraint. After all, the base is not a variable but a given integer that is chosen once and for all for the whole program[3].

[3] We could have chosen the base as a variable. For instance, to consider such a question as: “What are the bases for which this puzzle has less than x solutions?”

2.2.2.2. Model

For each letter, we have a decision variable (we keep the same letters to name the variables). Given a base b, digits range from 0 to b-1. Remember that variables corresponding to C, I, F and T should be different from 0. Thus C, I, F and T have

as domain and P, S, U, N, R and Ehave as domain. Another possibility is to keep the same domain for all variables and force C, I, F and T to be different from 0 by adding inequalities. However, restraining the domain to

is more efficient.

To model the sum constraint in any base b, we add the linear equation:

Page 2 of 32.2. The cryptarithmetic puzzle problem and a first model — or-tools User's Manual

Mon,05-May-14http://or-tools.googlecode.com/svn/trunk/documentation/user_manual/manual/first_s...

Page 3: Cry p Arithmetic

The global constraint AllDifferent springs to mind to model that variables must all have different values:

AllDifferent(C,P,I,S,F,U,N,T,R,E)

What is the AllDifferent[4] constraint?

The AllDifferent constraint enforces a set of variables to take distinct values. For instance, the solution C=2 P=3 I=7 S=4 F=9 U=6N=8 T=1 R=0 E=5 for our cryptarithmetic puzzle satisfies the AllDifferent constraint as all the values taken are pairwise different. There exist a variety of propagation algorithms for this constraint. The one used in or-tools is bound based (see [Lopez-Ortiz2003]).

[4] We talk here about the generic AllDifferent constraint. In or-tools, we use the method MakeAllDifferent().

[Lopez-Ortiz2003]

Alejandro Lopez-Ortiz, Claude-Guy Quimper, John Tromp and Peter Van Beek. A fast and simple algorithm for bounds consistency of the all different constraint, Proceedings of the 18th international joint conference on Artificial intelligence, Acapulco, Mexico, pp 245-250, 2003, Morgan Kaufmann Publishers Inc.

2.2.2.3. Solve

At this stage of our discovery of the library, we will not try to find a good search strategy to solve this model. A default basic strategy will do for the moment.

Chapter Defining search primitives in constraint programming is entirely devoted to the subject of search strategies.

Page 3 of 32.2. The cryptarithmetic puzzle problem and a first model — or-tools User's Manual

Mon,05-May-14http://or-tools.googlecode.com/svn/trunk/documentation/user_manual/manual/first_s...

Page 4: Cry p Arithmetic

�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������