Ejemplo n.º 1
0
 public static LinearConstraint MakeConstraint(LinearExpr /*!*/ le0, LinearExpr /*!*/ le1,
                                               LinearConstraint.ConstraintRelation rel, BigNum constantOffset) /* throws ArithmeticException */
 {
     Contract.Requires(le0 != null);
     Contract.Requires(le1 != null);
     le1.Negate();
     le0.Add(le1);
     le0.AddConstant(constantOffset);
     return(le0.ToConstraint(rel));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds a linear expression from "e", if possible; returns null if not possible.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static /*maybe null*/ LinearExpr AsExpr(IExpr /*!*/ e) /* throws ArithmeticException */
        {
            Contract.Requires(e != null);
            if (e is IVariable)
            {
                // Note, without a type for the variable, we don't know if the identifier is intended to hold an integer value.
                // However, it seems that no harm can be caused by here treating the identifier as if it held an
                // integer value, because other parts of this method will reject the expression as a linear expression
                // if non-numeric operations other than equality are applied to the identifier.
                return(new LinearExpr((IVariable)e));
            }
            else if (e is IFunApp)
            {
                IFunApp /*!*/ funapp = (IFunApp)e;
                Contract.Assert(funapp != null);
                IFunctionSymbol /*!*/ s = funapp.FunctionSymbol;
                Contract.Assert(s != null);

                if (s is IntSymbol)
                {
                    return(new LinearExpr(((IntSymbol)s).Value));
                }
                else if (s.Equals(Int.Negate))
                {
                    Contract.Assert(funapp.Arguments.Count == 1);
                    LinearExpr le = AsExpr((IExpr /*!*/)cce.NonNull(funapp.Arguments[0]));
                    if (le != null)
                    {
                        le.Negate();
                        return(le);
                    }
                }
                else if (s.Equals(Int.Add) || s.Equals(Int.Sub) || s.Equals(Int.Mul))
                {
                    Contract.Assert(funapp.Arguments.Count == 2);
                    IExpr /*!*/ arg0 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[0]);
                    IExpr /*!*/ arg1 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[1]);
                    LinearExpr  le0  = AsExpr(arg0);
                    if (le0 == null)
                    {
                        return(null);
                    }
                    LinearExpr le1 = AsExpr(arg1);
                    if (le1 == null)
                    {
                        return(null);
                    }

                    if (s.Equals(Int.Add))
                    {
                        le0.Add(le1);
                        return(le0);
                    }
                    else if (s.Equals(Int.Sub))
                    {
                        le1.Negate();
                        le0.Add(le1);
                        return(le0);
                    }
                    else if (s.Equals(Int.Mul))
                    {
                        BigNum x;
                        if (le0.AsConstant(out x))
                        {
                            le1.Multiply(x);
                            return(le1);
                        }
                        else if (le1.AsConstant(out x))
                        {
                            le0.Multiply(x);
                            return(le0);
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 3
0
 public static LinearConstraint MakeConstraint(LinearExpr/*!*/ le0, LinearExpr/*!*/ le1,
     LinearConstraint.ConstraintRelation rel, BigNum constantOffset) /* throws ArithmeticException */
 {
   Contract.Requires(le0 != null);
   Contract.Requires(le1 != null);
   le1.Negate();
   le0.Add(le1);
   le0.AddConstant(constantOffset);
   return le0.ToConstraint(rel);
 }