Esempio n. 1
0
        public override Answer CheckVariableDisequality(Element /*!*/ e, IVariable /*!*/ var1, IVariable /*!*/ var2)
        {
            //Contract.Requires(var2 != null);
            //Contract.Requires(var1 != null);
            //Contract.Requires(e != null);
            PolyhedraLatticeElement /*!*/ ple = (PolyhedraLatticeElement)cce.NonNull(e);

            Contract.Assume(ple.lcs.Constraints != null);
            ArrayList /*LinearConstraint!*//*!*/ clist = (ArrayList /*LinearConstraint!*/)cce.NonNull(ple.lcs.Constraints.Clone());
            LinearConstraint /*!*/ lc = new LinearConstraint(LinearConstraint.ConstraintRelation.EQ);

            Contract.Assert(lc != null);
            lc.SetCoefficient(var1, Rational.ONE);
            lc.SetCoefficient(var2, Rational.MINUS_ONE);
            clist.Add(lc);
            LinearConstraintSystem newLcs = new LinearConstraintSystem(clist);

            if (newLcs.IsBottom())
            {
                return(Answer.Yes);
            }
            else
            {
                return(Answer.Maybe);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Adds to "lines" the lines implied by initial-variable columns not in basis
 /// (see section 3.4.2 of Cousot and Halbwachs), and adds to "constraints" the
 /// constraints to exclude those lines (see step 4.2 of section 3.4.3 of
 /// Cousot and Halbwachs).
 /// </summary>
 /// <param name="lines"></param>
 /// <param name="constraints"></param>
 public void ProduceLines(ArrayList /*FrameElement*//*!*/ lines, ArrayList /*LinearConstraint*//*!*/ constraints)
 {
     Contract.Requires(constraints != null);
     Contract.Requires(lines != null);
     // for every initial variable not in basis
     for (int i0 = 0; i0 < numInitialVars; i0++)
     {
         if (inBasis[i0] == -1)
         {
             FrameElement     fe = new FrameElement();
             LinearConstraint lc = new LinearConstraint(LinearConstraint.ConstraintRelation.EQ);
             for (int i = 0; i < numInitialVars; i++)
             {
                 if (i == i0)
                 {
                     fe.AddCoordinate((IVariable)cce.NonNull(dims[i]), Rational.ONE);
                     lc.SetCoefficient((IVariable)cce.NonNull(dims[i]), Rational.ONE);
                 }
                 else if (inBasis[i] != -1)
                 {
                     // i is a basis column
                     Contract.Assert(m[inBasis[i], i].HasValue(1));
                     Rational val = -m[inBasis[i], i0];
                     fe.AddCoordinate((IVariable)cce.NonNull(dims[i]), val);
                     lc.SetCoefficient((IVariable)cce.NonNull(dims[i]), val);
                 }
             }
             lines.Add(fe);
             constraints.Add(lc);
         }
     }
 }
Esempio n. 3
0
        public LinearConstraint ToConstraint(LinearConstraint.ConstraintRelation rel) /* throws ArithmeticException */
        {
            LinearConstraint constraint = new LinearConstraint(rel);

            for (Term t = terms; t != null; t = t.next)
            {
                constraint.SetCoefficient(t.var, t.coeff.ToRational);
            }
            BigNum rhs = -constant;

            constraint.rhs = rhs.ToRational;
            return(constraint);
        }
Esempio n. 4
0
 /// <summary>
 /// Adds to "lines" the lines implied by initial-variable columns not in basis
 /// (see section 3.4.2 of Cousot and Halbwachs), and adds to "constraints" the
 /// constraints to exclude those lines (see step 4.2 of section 3.4.3 of
 /// Cousot and Halbwachs).
 /// </summary>
 /// <param name="lines"></param>
 /// <param name="constraints"></param>
 public void ProduceLines(ArrayList /*FrameElement*//*!*/ lines, ArrayList /*LinearConstraint*//*!*/ constraints) {
   Contract.Requires(constraints != null);
   Contract.Requires(lines != null);
   // for every initial variable not in basis
   for (int i0 = 0; i0 < numInitialVars; i0++) {
     if (inBasis[i0] == -1) {
       FrameElement fe = new FrameElement();
       LinearConstraint lc = new LinearConstraint(LinearConstraint.ConstraintRelation.EQ);
       for (int i = 0; i < numInitialVars; i++) {
         if (i == i0) {
           fe.AddCoordinate((IVariable)cce.NonNull(dims[i]), Rational.ONE);
           lc.SetCoefficient((IVariable)cce.NonNull(dims[i]), Rational.ONE);
         } else if (inBasis[i] != -1) {
           // i is a basis column
           Contract.Assert(m[inBasis[i], i].HasValue(1));
           Rational val = -m[inBasis[i], i0];
           fe.AddCoordinate((IVariable)cce.NonNull(dims[i]), val);
           lc.SetCoefficient((IVariable)cce.NonNull(dims[i]), val);
         }
       }
       lines.Add(fe);
       constraints.Add(lc);
     }
   }
 }
    /// <summary>
    /// Tests the example in section 3.4.3 of Cousot and Halbwachs.
    /// </summary>
    public static void RunValidationB() {
      IVariable/*!*/ X = new TestVariable("X");
      IVariable/*!*/ Y = new TestVariable("Y");
      IVariable/*!*/ Z = new TestVariable("Z");
      Contract.Assert(X != null);
      Contract.Assert(Y != null);
      Contract.Assert(Z != null);
      ArrayList /*LinearConstraint*/ cs = new ArrayList /*LinearConstraint*/ ();

      LinearConstraint c = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      c.SetCoefficient(X, Rational.MINUS_ONE);
      c.SetCoefficient(Y, Rational.ONE);
      c.SetCoefficient(Z, Rational.MINUS_ONE);
      c.rhs = Rational.ZERO;
      cs.Add(c);

      c = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      c.SetCoefficient(X, Rational.MINUS_ONE);
      c.rhs = Rational.MINUS_ONE;
      cs.Add(c);

      c = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      c.SetCoefficient(X, Rational.MINUS_ONE);
      c.SetCoefficient(Y, Rational.MINUS_ONE);
      c.SetCoefficient(Z, Rational.ONE);
      c.rhs = Rational.ZERO;
      cs.Add(c);

      c = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      c.SetCoefficient(Y, Rational.MINUS_ONE);
      c.SetCoefficient(Z, Rational.ONE);
      c.rhs = Rational.FromInt(3);
      cs.Add(c);

      LinearConstraintSystem lcs = new LinearConstraintSystem(cs);
      Console.WriteLine("==================== The final linear constraint system ====================");
      lcs.Dump();
    }
    LinearConstraintSystem(FrameElement/*!*/ v) {
      Contract.Requires(v != null);
      IMutableSet/*!*/ frameDims = v.GetDefinedDimensions();
      Contract.Assert(frameDims != null);
      ArrayList /*LinearConstraint!*/ constraints = new ArrayList /*LinearConstraint!*/ ();
      foreach (IVariable/*!*/ dim in frameDims) {
        Contract.Assert(dim != null);
        LinearConstraint lc = new LinearConstraint(LinearConstraint.ConstraintRelation.EQ);
        lc.SetCoefficient(dim, Rational.ONE);
        lc.rhs = v[dim];
        constraints.Add(lc);
      }
      FrameDimensions = frameDims;
      Constraints = constraints;

      ArrayList /*FrameElement*/ frameVertices = new ArrayList /*FrameElement*/ ();
      frameVertices.Add(v);
      FrameVertices = frameVertices;

      FrameRays = new ArrayList /*FrameElement*/ ();
      FrameLines = new ArrayList /*FrameElement*/ ();

      //:base();
      CheckInvariant();
    }
    /// <summary>
    /// Adds a ray to the frame of "this" and updates Constraints accordingly, see
    /// Cousot and Halbwachs, section 3.3.1.1.  However, this method does not simplify
    /// Constraints after the operation; that remains the caller's responsibility (which
    /// gives the caller the opportunity to make multiple calls to AddVertex, AddRay,
    /// and AddLine before calling SimplifyConstraints).
    /// Assumes Constraints (and the frame fields) to be non-null.
    /// </summary>
    /// <param name="ray"></param>
    void AddRay(FrameElement/*!*/ ray) {
      Contract.Requires(ray != null);
      Contract.Requires(this.FrameRays != null);
#if DEBUG_PRINT
      Console.WriteLine("DEBUG: AddRay called on {0}", ray);
      Console.WriteLine("  Initial constraints:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif

      FrameRays.Add(ray.Clone());
#if FIXED_DESERIALIZER      
      Contract.Assert(Contract.ForAll(ray.GetDefinedDimensions() , var=> FrameDimensions.Contains(var)));
#endif

      // We use a new temporary dimension.
      IVariable/*!*/ lambda = new LambdaDimension();

      // We change the constraints A*X <= B into
      //     A*X - (A*ray)*lambda <= B.
      // That means that each row k in A (which corresponds to one LinearConstraint
      // in Constraints) is changed by subtracting
      //     (A*ray)[k] * lambda
      // from row k.
      // Note:
      //     (A*ray)[k]
      //  =      { A*ray is a row vector whose every row i is the dot-product of
      //           row i of A with the column vector "ray" }
      //     A[k]*ray
      foreach (LinearConstraint/*!*/ cc in cce.NonNull(Constraints)) {
        Contract.Assert(cc != null);
        Rational d = cc.EvaluateLhs(ray);
        cc.SetCoefficient(lambda, -d);
      }

      // We also add the constraints that lambda is at least 0.
      LinearConstraint la = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      la.SetCoefficient(lambda, Rational.MINUS_ONE);
      la.rhs = Rational.ZERO;
      Constraints.Add(la);
#if DEBUG_PRINT
      Console.WriteLine("  Constraints after addition:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif

      // Finally, project out the dummy dimension.
      Constraints = Project(lambda, Constraints);

#if DEBUG_PRINT
      Console.WriteLine("  Resulting constraints:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif
    }
    /// <summary>
    /// Adds a vertex to the frame of "this" and updates Constraints accordingly, see
    /// Cousot and Halbwachs, section 3.3.1.1.  However, this method does not simplify
    /// Constraints after the operation; that remains the caller's responsibility (which
    /// gives the caller the opportunity to make multiple calls to AddVertex, AddRay,
    /// and AddLine before calling SimplifyConstraints).
    /// Assumes Constraints (and the frame fields) to be non-null.
    /// </summary>
    /// <param name="vertex"></param>
    void AddVertex(FrameElement/*!*/ vertex) {
      Contract.Requires(vertex != null);
      Contract.Requires(this.FrameVertices != null);
#if DEBUG_PRINT
      Console.WriteLine("DEBUG: AddVertex called on {0}", vertex);
      Console.WriteLine("  Initial constraints:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif

      FrameVertices.Add(vertex.Clone());
#if FIXED_DESERIALIZER      
      Contract.Assert(Contract.ForAll(vertex.GetDefinedDimensions() , var=> FrameDimensions.Contains(var)));
#endif

      // We use a new temporary dimension.
      IVariable/*!*/ lambda = new LambdaDimension();

      // We change the constraints A*X <= B into
      //     A*X + (A*vector - B)*lambda <= A*vector.
      // That means that each row k in A (which corresponds to one LinearConstraint
      // in Constraints) is changed by adding
      //     (A*vector - B)[k] * lambda
      // to row k and changing the right-hand side of row k to
      //     (A*vector)[k]
      // Note:
      //     (A*vector - B)[k]
      //  =      { vector subtraction is pointwise }
      //     (A*vector)[k] - B[k]
      //  =      { A*vector is a row vector whose every row i is the dot-product of
      //           row i of A with the column vector "vector" }
      //     A[k]*vector - B[k]
      foreach (LinearConstraint/*!*/ cc in cce.NonNull(Constraints)) {
        Contract.Assert(cc != null);
        Rational d = cc.EvaluateLhs(vertex);
        cc.SetCoefficient(lambda, d - cc.rhs);
        cc.rhs = d;
      }

      // We also add the constraints that lambda lies between 0 ...
      LinearConstraint la = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      la.SetCoefficient(lambda, Rational.MINUS_ONE);
      la.rhs = Rational.ZERO;
      Constraints.Add(la);
      // ... and 1.
      la = new LinearConstraint(LinearConstraint.ConstraintRelation.LE);
      la.SetCoefficient(lambda, Rational.ONE);
      la.rhs = Rational.ONE;
      Constraints.Add(la);
#if DEBUG_PRINT
      Console.WriteLine("  Constraints after addition:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif

      // Finally, project out the dummy dimension.
      Constraints = Project(lambda, Constraints);

#if DEBUG_PRINT
      Console.WriteLine("  Resulting constraints:");
      foreach (LinearConstraint cc in Constraints) {
        Console.WriteLine("    {0}", cc);
      }
#endif
    }
 public LinearConstraint ToConstraint(LinearConstraint.ConstraintRelation rel) /* throws ArithmeticException */
 {
   LinearConstraint constraint = new LinearConstraint(rel);
   for (Term t = terms; t != null; t = t.next) {
     constraint.SetCoefficient(t.var, t.coeff.ToRational);
   }
   BigNum rhs = -constant;
   constraint.rhs = rhs.ToRational;
   return constraint;
 }
Esempio n. 10
0
 public override Answer CheckVariableDisequality(Element/*!*/ e, IVariable/*!*/ var1, IVariable/*!*/ var2) {
   //Contract.Requires(var2 != null);
   //Contract.Requires(var1 != null);
   //Contract.Requires(e != null);
   PolyhedraLatticeElement/*!*/ ple = (PolyhedraLatticeElement)cce.NonNull(e);
   Contract.Assume(ple.lcs.Constraints != null);
   ArrayList /*LinearConstraint!*//*!*/ clist = (ArrayList /*LinearConstraint!*/)cce.NonNull(ple.lcs.Constraints.Clone());
   LinearConstraint/*!*/ lc = new LinearConstraint(LinearConstraint.ConstraintRelation.EQ);
   Contract.Assert(lc != null);
   lc.SetCoefficient(var1, Rational.ONE);
   lc.SetCoefficient(var2, Rational.MINUS_ONE);
   clist.Add(lc);
   LinearConstraintSystem newLcs = new LinearConstraintSystem(clist);
   if (newLcs.IsBottom()) {
     return Answer.Yes;
   } else {
     return Answer.Maybe;
   }
 }