forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.cs
167 lines (154 loc) · 5.22 KB
/
crypto.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Copyright 2012 Hakan Kjellerstrand
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
using System;
using Google.OrTools.ConstraintSolver;
public class Crypto
{
/**
*
* Crypto problem.
*
* This is the standard benchmark "crypto" problem.
*
* From GLPK:s model cryto.mod.
*
* """
* This problem comes from the newsgroup rec.puzzle.
* The numbers from 1 to 26 are assigned to the letters of the alphabet.
* The numbers beside each word are the total of the values assigned to
* the letters in the word (e.g. for LYRE: L, Y, R, E might be to equal
* 5, 9, 20 and 13, or any other combination that add up to 47).
* Find the value of each letter under the equations:
*
* BALLET 45 GLEE 66 POLKA 59 SONG 61
* CELLO 43 JAZZ 58 QUARTET 50 SOPRANO 82
* CONCERT 74 LYRE 47 SAXOPHONE 134 THEME 72
* FLUTE 30 OBOE 53 SCALE 51 VIOLIN 100
* FUGUE 50 OPERA 65 SOLO 37 WALTZ 34
*
* Solution:
* A, B,C, D, E,F, G, H, I, J, K,L,M, N, O, P,Q, R, S,T,U, V,W, X, Y, Z
* 5,13,9,16,20,4,24,21,25,17,23,2,8,12,10,19,7,11,15,3,1,26,6,22,14,18
*
* Reference:
* Koalog Constraint Solver <http://www.koalog.com/php/jcs.php>,
* Simple problems, the crypto-arithmetic puzzle ALPHACIPHER.
* """
*
* Also see http://hakank.org/or-tools/crypto.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Crypto");
int num_letters = 26;
int BALLET = 45;
int CELLO = 43;
int CONCERT = 74;
int FLUTE = 30;
int FUGUE = 50;
int GLEE = 66;
int JAZZ = 58;
int LYRE = 47;
int OBOE = 53;
int OPERA = 65;
int POLKA = 59;
int QUARTET = 50;
int SAXOPHONE = 134;
int SCALE = 51;
int SOLO = 37;
int SONG = 61;
int SOPRANO = 82;
int THEME = 72;
int VIOLIN = 100;
int WALTZ = 34;
//
// Decision variables
//
IntVar[] LD = solver.MakeIntVarArray(num_letters, 1, num_letters, "LD");
// Note D is not used in the constraints below
IntVar A = LD[0];
IntVar B = LD[1];
IntVar C = LD[2]; // IntVar D = LD[3];
IntVar E = LD[4];
IntVar F = LD[5];
IntVar G = LD[6];
IntVar H = LD[7];
IntVar I = LD[8];
IntVar J = LD[9];
IntVar K = LD[10];
IntVar L = LD[11];
IntVar M = LD[12];
IntVar N = LD[13];
IntVar O = LD[14];
IntVar P = LD[15];
IntVar Q = LD[16];
IntVar R = LD[17];
IntVar S = LD[18];
IntVar T = LD[19];
IntVar U = LD[20];
IntVar V = LD[21];
IntVar W = LD[22];
IntVar X = LD[23];
IntVar Y = LD[24];
IntVar Z = LD[25];
//
// Constraints
//
solver.Add(LD.AllDifferent());
solver.Add(B + A + L + L + E + T == BALLET);
solver.Add(C + E + L + L + O == CELLO);
solver.Add(C + O + N + C + E + R + T == CONCERT);
solver.Add(F + L + U + T + E == FLUTE);
solver.Add(F + U + G + U + E == FUGUE);
solver.Add(G + L + E + E == GLEE);
solver.Add(J + A + Z + Z == JAZZ);
solver.Add(L + Y + R + E == LYRE);
solver.Add(O + B + O + E == OBOE);
solver.Add(O + P + E + R + A == OPERA);
solver.Add(P + O + L + K + A == POLKA);
solver.Add(Q + U + A + R + T + E + T == QUARTET);
solver.Add(S + A + X + O + P + H + O + N + E == SAXOPHONE);
solver.Add(S + C + A + L + E == SCALE);
solver.Add(S + O + L + O == SOLO);
solver.Add(S + O + N + G == SONG);
solver.Add(S + O + P + R + A + N + O == SOPRANO);
solver.Add(T + H + E + M + E == THEME);
solver.Add(V + I + O + L + I + N == VIOLIN);
solver.Add(W + A + L + T + Z == WALTZ);
//
// Search
//
DecisionBuilder db = solver.MakePhase(LD, Solver.CHOOSE_MIN_SIZE_LOWEST_MIN, Solver.ASSIGN_CENTER_VALUE);
solver.NewSearch(db);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (solver.NextSolution())
{
for (int i = 0; i < num_letters; i++)
{
Console.WriteLine("{0}: {1,2}", str[i], LD[i].Value());
}
Console.WriteLine();
}
Console.WriteLine("\nWallTime: " + solver.WallTime() + "ms ");
Console.WriteLine("Failures: " + solver.Failures());
Console.WriteLine("Branches: " + solver.Branches());
solver.EndSearch();
}
public static void Main(String[] args)
{
Solve();
}
}