File: Solver.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (398 lines) | stat: -rw-r--r-- 14,641 bytes parent folder | download | duplicates (6)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//---------------------------------------------------------------------
// <copyright file="Solver.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Data.Common.Utils.Boolean
{
    using IfThenElseKey = Triple<Vertex, Vertex, Vertex>;
    using System.Diagnostics;

    /// <summary>
    /// Supports construction of canonical Boolean expressions as Reduced Ordered
    /// Boolean Decision Diagrams (ROBDD). As a side effect, supports simplification and SAT:
    /// 
    /// - The canonical form of a valid expression is Solver.One
    /// - The canonical form of an unsatisfiable expression is Solver.Zero
    /// - The lack of redundancy in the trees allows us to produce compact representations
    /// of expressions
    /// 
    /// Any method taking a Vertex argument requires that the argument is either
    /// a 'sink' (Solver.One or Solver.Zero) or generated by this Solver instance.
    /// </summary>
    internal sealed class Solver
    {
        #region Fields
        readonly Dictionary<IfThenElseKey, Vertex> _computedIfThenElseValues =
            new Dictionary<IfThenElseKey, Vertex>();
        readonly Dictionary<Vertex, Vertex> _knownVertices =
            new Dictionary<Vertex, Vertex>(VertexValueComparer.Instance);
        int _variableCount;
        // a standard Boolean variable has children '1' and '0'
        internal readonly static Vertex[] BooleanVariableChildren = new Vertex[] { Vertex.One, Vertex.Zero };
        #endregion

        #region Expression factory methods
        internal int CreateVariable()
        {
            return ++_variableCount;
        }

        internal Vertex Not(Vertex vertex)
        {
            // Not(v) iff. 'if v then 0 else 1'
            return IfThenElse(vertex, Vertex.Zero, Vertex.One);
        }

        internal Vertex And(IEnumerable<Vertex> children)
        {
            // assuming input vertices v1, v2, ..., vn:
            //
            //  v1
            //  0|\1
            //   |  v2
            //   |/0  \1
            //   |    ...
            //   |  /0  \1
            //   |        vn
            //   |     /0   \1
            //   FALSE       TRUE
            //
            // order the children to minimize churn when building tree bottom up
            return children
                .OrderByDescending(child => child.Variable)
                .Aggregate(Vertex.One, (left, right) => IfThenElse(left, right, Vertex.Zero));
        }

        internal Vertex And(Vertex left, Vertex right)
        {
            // left AND right iff. if 'left' then 'right' else '0'
            return IfThenElse(left, right, Vertex.Zero);
        }

        internal Vertex Or(IEnumerable<Vertex> children)
        {
            // assuming input vertices v1, v2, ..., vn:
            //
            //  v1
            //  1|\0
            //   |  v2
            //   |/1  \0
            //   |    ...
            //   |  /1  \0
            //   |        vn
            //   |     /1   \0
            //   TRUE       FALSE
            //
            // order the children to minimize churn when building tree bottom up
            return children
                .OrderByDescending(child => child.Variable)
                .Aggregate(Vertex.Zero, (left, right) => IfThenElse(left, Vertex.One, right));
        }

        /// <summary>
        /// Creates a leaf vertex; all children must be sinks
        /// </summary>
        internal Vertex CreateLeafVertex(int variable, Vertex[] children)
        {
            Debug.Assert(null != children, "children must be specified");
            Debug.Assert(2 <= children.Length, "must be at least 2 children");
            Debug.Assert(children.All(child => child != null), "children must not be null");
            Debug.Assert(children.All(child => child.IsSink()), "children must be sinks");
            Debug.Assert(variable <= _variableCount, "variable out of range");

            return GetUniqueVertex(variable, children);
        }
        #endregion

        #region Private helper methods
        /// <summary>
        /// Returns a Vertex with the given configuration. If this configuration
        /// is known, returns the existing vertex. Otherwise, a new
        /// vertex is created. This ensures the vertex is unique in the context
        /// of this solver.
        /// </summary>
        private Vertex GetUniqueVertex(int variable, Vertex[] children)
        {
            AssertVerticesValid(children);

            Vertex result = new Vertex(variable, children);

            // see if we know this vertex already
            Vertex canonicalResult;
            if (_knownVertices.TryGetValue(result, out canonicalResult))
            {
                return canonicalResult;
            }

            // remember the vertex (because it came first, it's canonical)
            _knownVertices.Add(result, result);

            return result;
        }

        /// <summary>
        /// Composes the given vertices to produce a new ROBDD.
        /// </summary>
        private Vertex IfThenElse(Vertex condition, Vertex then, Vertex @else)
        {
            AssertVertexValid(condition);
            AssertVertexValid(then);
            AssertVertexValid(@else);

            // check for terminal conditions in the recursion
            if (condition.IsOne())
            {
                // if '1' then 'then' else '@else' iff. 'then'
                return then;
            }
            if (condition.IsZero())
            {
                // if '0' then 'then' else '@else' iff. '@else'
                return @else;
            }
            if (then.IsOne() && @else.IsZero())
            {
                // if 'condition' then '1' else '0' iff. condition
                return condition;
            }
            if (then.Equals(@else))
            {
                // if 'condition' then 'x' else 'x' iff. x
                return then;
            }

            Vertex result;
            IfThenElseKey key = new IfThenElseKey(condition, then, @else);

            // check if we've already computed this result
            if (_computedIfThenElseValues.TryGetValue(key, out result))
            {
                return result;
            }

            int topVariableDomainCount;
            int topVariable = DetermineTopVariable(condition, then, @else, out topVariableDomainCount);

            // Recursively compute the new BDD node
            // Note that we preserve the 'ordered' invariant since the child nodes
            // cannot contain references to variables < topVariable, and
            // the topVariable is eliminated from the children through 
            // the call to EvaluateFor.
            Vertex[] resultCases = new Vertex[topVariableDomainCount];
            bool allResultsEqual = true;
            for (int i = 0; i < topVariableDomainCount; i++)
            {
                resultCases[i] = IfThenElse(
                    EvaluateFor(condition, topVariable, i),
                    EvaluateFor(then, topVariable, i),
                    EvaluateFor(@else, topVariable, i));

                if (i > 0 && // first vertex is equivalent to itself
                    allResultsEqual && // we've already found a mismatch
                    !resultCases[i].Equals(resultCases[0]))
                {
                    allResultsEqual = false;
                }
            }

            // if the results are identical, any may be returned
            if (allResultsEqual)
            {
                return resultCases[0];
            }

            // create new vertex
            result = GetUniqueVertex(topVariable, resultCases);

            // remember result so that we don't try to compute this if-then-else pattern again
            _computedIfThenElseValues.Add(key, result);

            return result;
        }

        /// <summary>
        /// Given parts of an if-then-else statement, determines the top variable (nearest
        /// root). Used to determine which variable forms the root of a composed Vertex.
        /// </summary>
        private static int DetermineTopVariable(Vertex condition, Vertex then, Vertex @else, out int topVariableDomainCount)
        {
            int topVariable;
            if (condition.Variable < then.Variable)
            {
                topVariable = condition.Variable;
                topVariableDomainCount = condition.Children.Length;
            }
            else
            {
                topVariable = then.Variable;
                topVariableDomainCount = then.Children.Length;
            }
            if (@else.Variable < topVariable)
            {
                topVariable = @else.Variable;
                topVariableDomainCount = @else.Children.Length;
            }
            return topVariable;
        }

        /// <summary>
        /// Returns 'vertex' evaluated for the given value of 'variable'. Requires that
        /// the variable is less than or equal to vertex.Variable.
        /// </summary>
        private static Vertex EvaluateFor(Vertex vertex, int variable, int variableAssigment)
        {
            if (variable < vertex.Variable)
            {
                // If the variable we're setting is less than the vertex variable, the
                // the Vertex 'ordered' invariant ensures that the vertex contains no reference
                // to that variable. Binding the variable is therefore a no-op.
                return vertex;
            }
            Debug.Assert(variable == vertex.Variable,
                "variable must be less than or equal to vertex.Variable");

            // If the 'vertex' is conditioned on the given 'variable', the children
            // represent the decompositions of the function for various assignments
            // to that variable.
            Debug.Assert(variableAssigment < vertex.Children.Length, "variable assignment out of range");
            return vertex.Children[variableAssigment];
        }

        /// <summary>
        /// Checks requirements for vertices. 
        /// </summary>
        [Conditional("DEBUG")]
        private void AssertVerticesValid(IEnumerable<Vertex> vertices)
        {
            Debug.Assert(null != vertices);
            foreach (Vertex vertex in vertices)
            {
                AssertVertexValid(vertex);
            }
        }

        /// <summary>
        /// Checks requirements for a vertex argument (must not be null, and must be in scope
        /// for this solver)
        /// </summary>
        [Conditional("DEBUG")]
        private void AssertVertexValid(Vertex vertex)
        {
            Debug.Assert(vertex != null, "vertex must not be null");

            // sinks are ok
            if (!vertex.IsSink())
            {
                // so are vertices created by this solver
                Vertex comparisonVertex;
                Debug.Assert(_knownVertices.TryGetValue(vertex, out comparisonVertex) &&
                    comparisonVertex.Equals(vertex), "vertex not created by this solver");
            }
        }
        #endregion

        /// <summary>
        /// Supports value comparison of vertices. In general, we use reference comparison
        /// since the Solver ensures a single instance of each canonical Vertex. The Solver
        /// needs this comparer to ensure a single instance of each canonical Vertex though...
        /// </summary>
        private class VertexValueComparer : IEqualityComparer<Vertex>
        {
            private VertexValueComparer() { }

            internal static readonly VertexValueComparer Instance = new VertexValueComparer();

            public bool Equals(Vertex x, Vertex y)
            {
                if (x.IsSink())
                {
                    // sync nodes '1' and '0' each have one static instance; use reference
                    return x.Equals(y);
                }

                if (x.Variable != y.Variable ||
                    x.Children.Length != y.Children.Length)
                {
                    return false;
                }
                for (int i = 0; i < x.Children.Length; i++)
                {
                    // use reference comparison for the children (they must be
                    // canonical already)
                    if (!x.Children[i].Equals(y.Children[i]))
                    {
                        return false;
                    }
                }
                return true;
            }

            public int GetHashCode(Vertex vertex)
            {
                // sync nodes '1' and '0' each have one static instance; use reference
                if (vertex.IsSink())
                {
                    return vertex.GetHashCode();
                }

                Debug.Assert(2 <= vertex.Children.Length, "internal vertices must have at least 2 children");
                unchecked
                {
                    return ((vertex.Children[0].GetHashCode() << 5) + 1) + vertex.Children[1].GetHashCode();
                }
            }
        }
    }

    /// <summary>
    /// Record structure containing three values.
    /// </summary>
    struct Triple<T1, T2, T3> : IEquatable<Triple<T1, T2, T3>>
        where T1 : IEquatable<T1>
        where T2 : IEquatable<T2>
        where T3 : IEquatable<T3>
    {
        readonly T1 _value1;
        readonly T2 _value2;
        readonly T3 _value3;

        internal Triple(T1 value1, T2 value2, T3 value3)
        {
            Debug.Assert(null != (object)value1, "null key element");
            Debug.Assert(null != (object)value2, "null key element");
            Debug.Assert(null != (object)value3, "null key element");
            _value1 = value1;
            _value2 = value2;
            _value3 = value3;
        }

        public bool Equals(Triple<T1, T2, T3> other)
        {
            return _value1.Equals(other._value1) &&
                _value2.Equals(other._value2) &&
                _value3.Equals(other._value3);
        }

        public override bool Equals(object obj)
        {
            Debug.Fail("used typed Equals");
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return _value1.GetHashCode() ^
                _value2.GetHashCode() ^
                _value3.GetHashCode();
        }
    }
}