コード例 #1
0
    public LinearConstraintSystem/*!*/ Join(LinearConstraintSystem/*!*/ lcs) {
      Contract.Requires(lcs != null);
      Contract.Ensures(Contract.Result<LinearConstraintSystem>() != null);
#endif

      if (this.IsBottom()) {
        return cce.NonNull(lcs.Clone());
      } else if (lcs.IsBottom()) {
        return cce.NonNull(this.Clone());
      } else if (this.IsTop() || lcs.IsTop()) {
        return new LinearConstraintSystem(new ArrayList /*LinearConstraint*/ ());
      } else {
        LinearConstraintSystem/*!*/ z;
        // Start from the "larger" of the two frames (this is just a heuristic measure intended
        // to save work).
        Contract.Assume(this.FrameVertices != null);
        Contract.Assume(this.FrameRays != null);
        Contract.Assume(this.FrameLines != null);
        Contract.Assume(lcs.FrameVertices != null);
        Contract.Assume(lcs.FrameRays != null);
        Contract.Assume(lcs.FrameLines != null);
        if (this.FrameVertices.Count + this.FrameRays.Count + this.FrameLines.Count - this.FrameDimensions.Count <
          lcs.FrameVertices.Count + lcs.FrameRays.Count + lcs.FrameLines.Count - lcs.FrameDimensions.Count) {
          z = cce.NonNull(lcs.Clone());
          lcs = this;
        } else {
          z = cce.NonNull(this.Clone());
        }
#if DEBUG_PRINT
        Console.WriteLine("DEBUG: LinearConstraintSystem.Join ---------------");
        Console.WriteLine("z:");
        z.Dump();
        Console.WriteLine("lcs:");
        lcs.Dump();
#endif

        // Start by explicating the implicit lines of z for the dimensions dims(lcs)-dims(z).
        foreach (IVariable/*!*/ dim in lcs.FrameDimensions) {
          Contract.Assert(dim != null);
          if (!z.FrameDimensions.Contains(dim)) {
            z.FrameDimensions.Add(dim);
            FrameElement line = new FrameElement();
            line.AddCoordinate(dim, Rational.ONE);
            // Note:  AddLine is not called (because the line already exists in z--it's just that
            // it was represented implicitly).  Instead, just tack the explicit representation onto
            // FrameLines.
            Contract.Assume(z.FrameLines != null);
            z.FrameLines.Add(line);
#if DEBUG_PRINT
            Console.WriteLine("Join: After explicating line: {0}", line);
            z.Dump();
#endif
          }
        }

        // Now, the vertices, rays, and lines can be added.
        foreach (FrameElement/*!*/ v in lcs.FrameVertices) {
          Contract.Assert(v != null);
          z.AddVertex(v);
#if DEBUG_PRINT
          Console.WriteLine("Join: After adding vertex: {0}", v);
          z.Dump();
#endif
        }
        foreach (FrameElement/*!*/ r in lcs.FrameRays) {
          Contract.Assert(r != null);
          z.AddRay(r);
#if DEBUG_PRINT
          Console.WriteLine("Join: After adding ray: {0}", r);
          z.Dump();
#endif
        }
        foreach (FrameElement/*!*/ l in lcs.FrameLines) {
          Contract.Assert(l != null);
          z.AddLine(l);
#if DEBUG_PRINT
          Console.WriteLine("Join: After adding line: {0}", l);
          z.Dump();
#endif
        }
        // also add to z the implicit lines of lcs
        foreach (IVariable/*!*/ dim in z.FrameDimensions) {
          Contract.Assert(dim != null);
          if (!lcs.FrameDimensions.Contains(dim)) {
            // "dim" is a dimension that's explicit in "z" but implicit in "lcs"
            FrameElement line = new FrameElement();
            line.AddCoordinate(dim, Rational.ONE);
            z.AddLine(line);
#if DEBUG_PRINT
            Console.WriteLine("Join: After adding lcs's implicit line: {0}", line);
            z.Dump();
#endif
          }
        }

        z.SimplifyFrame();
        z.SimplifyConstraints();
        z.CheckInvariant();
#if DEBUG_PRINT
        Console.WriteLine("Join: Returning z:");
        z.Dump();
        Console.WriteLine("----------------------------------------");
#endif
        return z;
      }
    }
コード例 #2
0
 public LinearConstraintSystem Widen(LinearConstraintSystem lcs)
 {
   Console.WriteLine("===================================================================================");
   Console.WriteLine("DEBUG: Widen");
   Console.WriteLine("Widen: this=");
   Dump();
   Console.WriteLine("Widen: lcs=");
   lcs.Dump();
   LinearConstraintSystem z = WidenX(lcs);
   Console.WriteLine("----------Widen------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
   Console.WriteLine("Widen: result=");
   z.Dump();
   Console.WriteLine("===================================================================================");
   return z;
 }
コード例 #3
0
 public LinearConstraintSystem Join(LinearConstraintSystem lcs)
 {
   Console.WriteLine("===================================================================================");
   Console.WriteLine("DEBUG: Join");
   Console.WriteLine("Join: this=");
   Dump();
   Console.WriteLine("Join: lcs=");
   lcs.Dump();
   LinearConstraintSystem z = JoinX(lcs);
   Console.WriteLine("----------Join------------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
   Console.WriteLine("Join: result=");
   z.Dump();
   Console.WriteLine("===================================================================================");
   return z;
 }
コード例 #4
0
    /* IsSubset(): determines if 'lcs' is a subset of 'this'
     * -- See Cousot/Halbwachs 1978, section 
     */
    public bool IsSubset(LinearConstraintSystem/*!*/ lcs) {
      Contract.Requires(lcs != null);
      if (lcs.IsBottom()) {
        return true;
      } else if (this.IsBottom()) {
        return false;
#if DEBUG
#else
      } else if (this.IsTop()) {  // optimization -- this case not needed for correctness
        return true;
      } else if (lcs.IsTop()) {  // optimization -- this case not needed for correctness
        return false;
#endif
      } else {
        // phase 0: check if frame dimensions are a superset of the constraint dimensions
        ISet /*IVariable!*//*!*/ frameDims = lcs.GetDefinedDimensions();
        Contract.Assert(frameDims != null);
#if DEBUG_PRINT
        Console.WriteLine("DEBUG: IsSubset:");
        Console.WriteLine("  --- this:");
        this.Dump();
        Console.WriteLine("  --- lcs:");
        lcs.Dump();
        Console.WriteLine("  ---");
#endif
        foreach (LinearConstraint/*!*/ cc in cce.NonNull(this.Constraints)) {
          Contract.Assert(cc != null);
#if DEBUG_PRINT
          Console.WriteLine("   cc: {0}", cc);
          Console.WriteLine("   cc.GetDefinedDimensions(): {0}", cc.GetDefinedDimensions());
#endif

          if (!Contract.ForAll(cc.GetDefinedDimensionsGeneric(), var => frameDims.Contains(var))) {
#if DEBUG_PRINT
            Console.WriteLine("  ---> phase 0 subset violated, return false from IsSubset");
#endif
            return false;
          }
        }
      }

      // phase 1: check frame vertices against each constraint...
      foreach (FrameElement/*!*/ v in cce.NonNull(lcs.FrameVertices)) {
        Contract.Assert(v != null);
        foreach (LinearConstraint/*!*/ cc in this.Constraints) {
          Contract.Assert(cc != null);
          Rational q = cc.EvaluateLhs(v);
          if (cc.Relation == LinearConstraint.ConstraintRelation.LE) {
            if (!(q <= cc.rhs)) {
#if DEBUG_PRINT
              Console.WriteLine("  ---> phase 1a subset violated, return false from IsSubset");
#endif
              return false;
            }
          } else {
            if (!(q == cc.rhs)) {
#if DEBUG_PRINT
              Console.WriteLine("  ---> phase 1b subset violated, return false from IsSubset");
#endif
              return false;
            }
          }
        }
      }

      // phase 2: check frame rays against each constraint...
      // To check if a ray "r" falls within a constraint "cc", we add the vector "r" to
      // any point "p" on the side of the half-space or plane described by constraint, and
      // then check if the resulting point satisfies the constraint.  That is, we check (for
      // an inequality constraint with coefficients a1,a2,...,an and right-hand side
      // constant C):
      //     a1*(r1+p1) + a2*(r2+p2) + ... + an*(rn+pn) <= C
      // Equivalently:
      //     a1*r1 + a2*r2 + ... + an*rn + a1*p1 + a2*p2 + ... + an*pn <= C
      // To find a point "p", we can pick out a coordinate, call it 1, with a non-zero
      // coefficient in the constraint, and then choose "p" as the point that has the
      // value C/a1 in coordinate 1 and has 0 in all other coordinates.  We then check:
      //     a1*r1 + a2*r2 + ... + an*rn + a1*(C/a1) + a2*0 + ... + an*0 <= C
      // which simplifies to:
      //     a1*r1 + a2*r2 + ... + an*rn + C <= C
      // which in turn simplifies to:
      //     a1*r1 + a2*r2 + ... + an*rn <= 0
      // If the constraint is an equality constraint, we simply replace "<=" with "=="
      // above.
      foreach (FrameElement/*!*/ r in cce.NonNull(lcs.FrameRays)) {
        Contract.Assert(r != null);
        System.Diagnostics.Debug.Assert(r != null, "encountered a null ray...");
        foreach (LinearConstraint/*!*/ cc in this.Constraints) {
          Contract.Assert(cc != null);
          System.Diagnostics.Debug.Assert(cc != null, "encountered an null constraint...");
          Rational q = cc.EvaluateLhs(r);
          if (cc.Relation == LinearConstraint.ConstraintRelation.LE) {
            if (q.IsPositive) {
#if DEBUG_PRINT
              Console.WriteLine("  ---> phase 2a subset violated, return false from IsSubset");
#endif
              return false;
            }
          } else {
            if (q.IsNonZero) {
#if DEBUG_PRINT
              Console.WriteLine("  ---> phase 2b subset violated, return false from IsSubset");
#endif
              return false;
            }
          }
        }
      }

      // phase 3: check frame lines against each constraint...
      // To check if a line "L" falls within a constraint "cc", we check if both the
      // vector "L" and "-L", interpreted as rays, fall within the constraint.  From
      // the discussion above, this means we check the following two properties:
      //     a1*L1 + a2*L2 + ... + an*Ln <= 0                 (*)
      //     a1*(-L1) + a2*(-L2) + ... + an*(-Ln) <= 0
      // The second of these lines can be rewritten as:
      //     - a1*L1 - a2*L2 - ... - an*Ln <= 0
      // which is equivalent to:
      //     -1 * (a1*L1 + a2*L2 + ... + an*Ln) <= 0
      // Multiplying both sides by -1 and flipping the direction of the inequality,
      // we have:
      //     a1*L1 + a2*L2 + ... + an*Ln >= 0                 (**)
      // Putting (*) and (**) together, we conclude that we need to check:
      //     a1*L1 + a2*L2 + ... + an*Ln == 0
      // If the constraint is an equality constraint, we end up with the same equation.
      foreach (FrameElement/*!*/ line in cce.NonNull(lcs.FrameLines)) {
        Contract.Assert(line != null);
        System.Diagnostics.Debug.Assert(line != null, "encountered a null line...");
        foreach (LinearConstraint/*!*/ cc in this.Constraints) {
          Contract.Assert(cc != null);
          System.Diagnostics.Debug.Assert(cc != null, "encountered an null constraint...");
          Rational q = cc.EvaluateLhs(line);
          if (q.IsNonZero) {
#if DEBUG_PRINT
            Console.WriteLine("  ---> phase 3 subset violated, return false from IsSubset");
#endif
            return false;
          }
        }
      }

#if DEBUG_PRINT
      Console.WriteLine("  ---> IsSubset returns true");
#endif
      return true;
    }
コード例 #5
0
    public static void RunValidationC() {
      // Run the example in section 3.4.3 of Cousot and Halbwachs backwards, that is, from
      // from to constraints.
      IVariable/*!*/ dim1 = new TestVariable("X");
      IVariable/*!*/ dim2 = new TestVariable("Y");
      IVariable/*!*/ dim3 = new TestVariable("Z");
      Contract.Assert(dim1 != null);
      Contract.Assert(dim2 != null);
      Contract.Assert(dim3 != null);

      FrameElement s0 = new FrameElement();
      s0.AddCoordinate(dim1, Rational.ONE);
      s0.AddCoordinate(dim2, Rational.FromInts(1, 2));
      s0.AddCoordinate(dim3, Rational.FromInts(-1, 2));

      FrameElement s1 = new FrameElement();
      s1.AddCoordinate(dim1, Rational.ONE);
      s1.AddCoordinate(dim2, Rational.FromInts(-1, 2));
      s1.AddCoordinate(dim3, Rational.FromInts(1, 2));

      FrameElement s2 = new FrameElement();
      s2.AddCoordinate(dim1, Rational.FromInt(3));
      s2.AddCoordinate(dim2, Rational.FromInts(-3, 2));
      s2.AddCoordinate(dim3, Rational.FromInts(3, 2));

      FrameElement r0 = new FrameElement();
      r0.AddCoordinate(dim1, Rational.ONE);
      r0.AddCoordinate(dim2, Rational.FromInts(1, 2));
      r0.AddCoordinate(dim3, Rational.FromInts(-1, 2));

      FrameElement r1 = new FrameElement();
      r1.AddCoordinate(dim1, Rational.ONE);
      r1.AddCoordinate(dim2, Rational.ZERO);
      r1.AddCoordinate(dim3, Rational.ZERO);

      FrameElement d0 = new FrameElement();
      d0.AddCoordinate(dim1, Rational.ZERO);
      d0.AddCoordinate(dim2, Rational.ONE);
      d0.AddCoordinate(dim3, Rational.ONE);

      LinearConstraintSystem lcs = new LinearConstraintSystem(s0);
      lcs.Dump();

      lcs.AddVertex(s1);
      lcs.Dump();

      lcs.AddVertex(s2);
      lcs.Dump();

      lcs.AddRay(r0);
      lcs.Dump();

      lcs.AddRay(r1);
      lcs.Dump();

      lcs.AddLine(d0);
      lcs.Dump();

      lcs.SimplifyConstraints();
      lcs.Dump();

#if LATER
      lcs.GenerateFrameFromConstraints(); // should give us back the original frame...
#endif
    }
コード例 #6
0
    /// <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();
    }
コード例 #7
0
    public static void RunValidationA() {
      IVariable/*!*/ dim1 = new TestVariable("X");
      IVariable/*!*/ dim2 = new TestVariable("Y");
      IVariable/*!*/ dim3 = new TestVariable("Z");
      Contract.Assert(dim1 != null);
      Contract.Assert(dim2 != null);
      Contract.Assert(dim3 != null);

      FrameElement s1 = new FrameElement();
      s1.AddCoordinate(dim1, Rational.ONE);
      s1.AddCoordinate(dim2, Rational.MINUS_ONE);
      s1.AddCoordinate(dim3, Rational.ZERO);
      FrameElement s2 = new FrameElement();
      s2.AddCoordinate(dim1, Rational.MINUS_ONE);
      s2.AddCoordinate(dim2, Rational.ONE);
      s2.AddCoordinate(dim3, Rational.ZERO);
      FrameElement r1 = new FrameElement();
      r1.AddCoordinate(dim1, Rational.ZERO);
      r1.AddCoordinate(dim2, Rational.ZERO);
      r1.AddCoordinate(dim3, Rational.ONE);
      FrameElement d1 = new FrameElement();
      d1.AddCoordinate(dim1, Rational.ONE);
      d1.AddCoordinate(dim2, Rational.ONE);
      d1.AddCoordinate(dim3, Rational.ZERO);

      // create lcs from frame -- cf. Cousot/Halbwachs 1978, section 3.3.1.1
      LinearConstraintSystem lcs = new LinearConstraintSystem(s1);
      lcs.Dump();

      lcs.AddVertex(s2);
      lcs.Dump();

      lcs.AddRay(r1);
      lcs.Dump();

      lcs.AddLine(d1);
      lcs.Dump();

      lcs.SimplifyConstraints();
      lcs.Dump();

#if LATER
      lcs.GenerateFrameFromConstraints(); // should give us back the original frame...
#endif
      Console.WriteLine("IsSubset? {0}", lcs.IsSubset(lcs.Clone()));
      lcs.Dump();
    }