Variables. A variable is a component of a {@linkplain Network constraint network}. A variable is constructed with an initial {@linkplain Domain domain} which specifies the set of Elements over which the variable ranges. See {@link Network} for example programs to construct variables and Add them to a constraint network.
Inheritance: ICloneable
Beispiel #1
0
 public Relation(Network net, Variable v0, bool[][] rel, Variable v1, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _rel = rel;
     _v0 = v0;
     _v1 = v1;
 }
Beispiel #2
0
 public Element(Network net, Variable v0, Variable v1, Variable[] v, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _v0 = v0;
     _v1 = v1;
     _v = (Variable[])v.Clone();
 }
Beispiel #3
0
 public Sequential(Network net, Variable[] v, int[] l, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _v = new Variable[v.Length];
     v.CopyTo(_v, 0);
     _l = new int[l.Length];
     l.CopyTo(_l, 0);
 }
Beispiel #4
0
 private IntArith(Network net, int a, Variable[] v, ConstraintTypes cType = ConstraintTypes.Hard)
     : this(net, a, v, cType, 0)
 {
 }
Beispiel #5
0
 public IntArith(Network net, int a, Variable v0, int x1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, v0, new IntVariable(net, x1), v2, cType, weight)
 {
 }
Beispiel #6
0
 public IntArith(Network net, int a, Variable v0, Variable v1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, new[] { v0, v1, v2 }, cType, weight)
 {
 }
Beispiel #7
0
 public IntArith(Network net, int a, int x0, Variable v1, Variable v2)
     : this(net, a, new IntVariable(net, x0), v1, v2)
 {
 }
Beispiel #8
0
 public IntArith(Network net, int a, Variable v0, int x1, Variable v2)
     : this(net, a, v0, new IntVariable(net, x1), v2)
 {
 }
Beispiel #9
0
 public IntArith(Network net, int a, Variable v0, Variable v1, Variable v2)
     : this(net, a, new[] { v0, v1, v2 })
 {
 }
Beispiel #10
0
 public IntComparison(Network net, int comp, Variable v0, int x1)
     : this(net, comp, v0, new IntVariable(net, x1))
 {
 }
Beispiel #11
0
 private static bool SatisfyLT(Variable v0, Variable v1, Trail trail)
 {
     var d0 = (IntDomain)v0.Domain;
     var d1 = (IntDomain)v1.Domain;
     d0 = d0.CapInterval(IntDomain.MinValue, d1.Maximum() - 1);
     if (d0.Empty)
         return false;
     if (trail != null)
     {
         v0.UpdateDomain(d0, trail);
     }
     d1 = d1.CapInterval(d0.Minimum() + 1, IntDomain.MaxValue);
     if (d1.Empty)
         return false;
     if (trail != null)
     {
         v1.UpdateDomain(d1, trail);
     }
     return true;
 }
Beispiel #12
0
 public VariableToken(Variable value)
 {
     _value = value;
 }
Beispiel #13
0
 public NotEquals(Network net, Variable[] va, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _v = new Variable[va.Length];
     va.CopyTo(_v, 0);
 }
Beispiel #14
0
 public NotEquals(Network net, Variable[] va, ConstraintTypes cType)
     : this(net, va, cType, 0)
 {
 }
Beispiel #15
0
 public NotEquals(Network net, Variable v0, Variable v1, ConstraintTypes cType, int weight)
     : this(net, new[] { v0, v1 }, cType, weight)
 {
 }
Beispiel #16
0
 private IntComparison(Network net, int comp, Variable[] v, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _comparison = comp;
     _v = v;
 }
Beispiel #17
0
        private static bool SatisfyAdd(Variable v0, Variable v1, Variable v2, Trail trail)
        {
            var d0 = (IntDomain) v0.Domain;
            var d1 = (IntDomain) v1.Domain;
            var d2 = (IntDomain) v2.Domain;

            if (d1.Size() == 1 && d2.Size() == 1)
            {
                // v0 = v1 + v2
                var value = d1.Value() + d2.Value();
                if (!d0.Contains(value))
                    return false;
                if (d0.Size() > 1)
                    if (trail != null)
                    {
                        v0.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }
            if (d0.Size() == 1 && d2.Size() == 1)
            {
                // v1 = v0 - v2
                var value = d0.Value() - d2.Value();
                if (!d1.Contains(value))
                    return false;
                if (d1.Size() > 1)
                    if (trail != null)
                    {
                        v1.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }
            if (d0.Size() == 1 && d1.Size() == 1)
            {
                // v2 = v0 - v1
                var value = d0.Value() - d1.Value();
                if (!d2.Contains(value))
                    return false;
                if (d2.Size() > 1)
                    if (trail != null)
                    {
                        v2.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }

            // v0 = v1 + v2
            d0 = d0.CapInterval(d1.Minimum() + d2.Minimum(), d1.Maximum() + d2.Maximum());
            if (d0.Empty)
                return false;
            if (trail != null)
            {
                v0.UpdateDomain(d0, trail);
            }
            // v1 = v0 - v2
            d1 = d1.CapInterval(d0.Minimum() - d2.Maximum(), d0.Maximum() - d2.Minimum());
            if (d1.Empty)
                return false;
            if (trail != null)
            {
                v1.UpdateDomain(d1, trail);
            }
            // v2 = v0 - v1
            d2 = d2.CapInterval(d0.Minimum() - d1.Maximum(), d0.Maximum() - d1.Minimum());
            if (d2.Empty)
                return false;
            if (trail != null)
            {
                v2.UpdateDomain(d2, trail);
            }

            return true;
        }
Beispiel #18
0
 public IntComparison(Network net, int comp, int x0, Variable v1)
     : this(net, comp, new IntVariable(net, x0), v1)
 {
 }
Beispiel #19
0
 public IntArith(Network net, int a, Variable v0, Variable v1, int x2)
     : this(net, a, v0, v1, new IntVariable(net, x2))
 {
 }
Beispiel #20
0
 public IntComparison(Network net, int comp, Variable v0, int x1, ConstraintTypes cType)
     : this(net, comp, v0, new IntVariable(net, x1), cType)
 {
 }
Beispiel #21
0
        private static bool SatisfyMIN(Variable v0, Variable v1, Variable v2, Trail trail)
        {
            var d0 = (IntDomain) v0.Domain;
            var d1 = (IntDomain) v1.Domain;
            var d2 = (IntDomain) v2.Domain;

            if (d1.Size() == 1 && d2.Size() == 1)
            {
                // v0 = Min(v1, v2)
                int value = Math.Min(d1.Value(), d2.Value());
                if (!d0.Contains(value))
                    return false;
                if (d0.Size() > 1)
                    if (trail != null)
                    {
                        v0.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }
            if (d0.Size() == 1)
            {
                int value = d0.Value();
                if (!d1.Contains(value) && !d2.Contains(value))
                    return false;
                // ???
                if (d1.Minimum() < value)
                {
                    d1.CapInterval(value, IntDomain.MaxValue);
                    if (d1.Empty)
                        return false;
                    if (trail != null)
                    {
                        v1.UpdateDomain(d1, trail);
                    }
                }
                if (d2.Minimum() < value)
                {
                    d2.CapInterval(value, IntDomain.MaxValue);
                    if (d2.Empty)
                        return false;
                    if (trail != null)
                    {
                        v2.UpdateDomain(d2, trail);
                    }
                }
                return true;
            }

            // v0 = Min(v1, v2)
            int min = Math.Min(d1.Minimum(), d2.Minimum());
            int max = Math.Min(d1.Maximum(), d2.Maximum());
            d0 = d0.CapInterval(min, max);
            if (d0.Empty)
                return false;
            if (trail != null)
            {
                v0.UpdateDomain(d0, trail);
            }
            //
            if (d1.Minimum() < d0.Minimum())
                d1 = d1.CapInterval(d0.Minimum(), IntDomain.MaxValue);
            if (d2.Minimum() < d0.Minimum())
                d2 = d2.CapInterval(d0.Minimum(), IntDomain.MaxValue);
            if (d1.Minimum() > d0.Maximum())
            {
                d0 = (IntDomain) d0.Cap(d2);
                d2 = d0;
            }
            if (d2.Minimum() > d0.Maximum())
            {
                d0 = (IntDomain) d0.Cap(d1);
                d1 = d0;
            }
            if (d0.Empty)
                return false;
            if (d1.Empty)
                return false;
            if (d2.Empty)
                return false;
            if (trail != null)
            {
                v0.UpdateDomain(d0, trail);
                v1.UpdateDomain(d1, trail);
                v2.UpdateDomain(d2, trail);
            }
            return true;
        }
Beispiel #22
0
 public IntComparison(Network net, int comp, int x0, Variable v1, ConstraintTypes cType)
     : this(net, comp, new IntVariable(net, x0), v1, cType)
 {
 }
Beispiel #23
0
        private static bool SatisfyMULTIPLY(Variable v0, Variable v1, Variable v2, Trail trail)
        {
            var d0 = (IntDomain) v0.Domain;
            var d1 = (IntDomain) v1.Domain;
            var d2 = (IntDomain) v2.Domain;

            if (d1.Size() == 1 && d2.Size() == 1)
            {
                // v0 = v1 * v2
                int value = ToInt(d1.Value() * (long) d2.Value());
                if (!d0.Contains(value))
                    return false;
                if (d0.Size() > 1)
                    if (trail != null)
                    {
                        v0.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }
            if (d0.Size() == 1 && d2.Size() == 1)
            {
                // v1 = v0 / v2
                int x = d0.Value();
                int y = d2.Value();
                if (y == 0)
                {
                    return x == 0;
                }
                if (x % y != 0)
                {
                    return false;
                }
                int value = x / y;
                if (!d1.Contains(value))
                    return false;
                if (d1.Size() > 1)
                    if (trail != null)
                    {
                        v1.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }
            if (d0.Size() == 1 && d1.Size() == 1)
            {
                // v2 = v0 / v1
                int x = d0.Value();
                int y = d1.Value();
                if (y == 0)
                {
                    return x == 0;
                }
                if (x % y != 0)
                {
                    return false;
                }
                int value = x / y;
                if (!d2.Contains(value))
                    return false;
                if (d2.Size() > 1)
                    if (trail != null)
                    {
                        v2.UpdateDomain(new IntDomain(value), trail);
                    }
                return true;
            }

            d0 = Multiply(d0, d1, d2);
            if (d0.Empty)
                return false;
            d1 = Divide(d1, d0, d2);
            if (d1.Empty)
                return false;
            d2 = Divide(d2, d0, d1);
            if (d2.Empty)
                return false;
            if (trail != null)
            {
                if (d0 != v0.Domain)
                    v0.UpdateDomain(d0, trail);
                if (d1 != v1.Domain)
                    v1.UpdateDomain(d1, trail);
                if (d2 != v2.Domain)
                    v2.UpdateDomain(d2, trail);
            }
            return true;
        }
Beispiel #24
0
 public IntComparison(Network net, int comp, Variable v0, Variable v1)
     : this(net, comp, new[] { v0, v1 })
 {
 }
Beispiel #25
0
 public IntArith(Network net, int a, Variable v0, Variable v1, int x2, ConstraintTypes cType, int weight)
     : this(net, a, v0, v1, new IntVariable(net, x2), cType, weight)
 {
 }
Beispiel #26
0
 public IntComparison(Network net, int comp, Variable v0, Variable v1, ConstraintTypes cType, int weight)
     : this(net, comp, new[] { v0, v1 }, cType, weight)
 {
 }
Beispiel #27
0
 public IntArith(Network net, int a, int x0, Variable v1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, new IntVariable(net, x0), v1, v2, cType, weight)
 {
 }
Beispiel #28
0
 private IntComparison(Network net, int comp, Variable[] v)
     : this(net, comp, v, ConstraintTypes.Hard, 0)
 {
 }
Beispiel #29
0
 private IntArith(Network net, int a, Variable[] v, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _arith = a;
     _v = v;
 }
Beispiel #30
0
 private IntComparison(Network net, int comp, Variable[] v, ConstraintTypes cType)
     : this(net, comp, v, cType, 0)
 {
 }