public ProductCst(LinearExpr e, long v)
 {
     expr_  = e;
     coeff_ = v;
 }
        public static long GetVarValueMap(LinearExpr e,
                                          long initial_coeff,
                                          Dictionary <IntVar, long> dict)
        {
            List <LinearExpr> exprs  = new List <LinearExpr>();
            List <long>       coeffs = new List <long>();

            if ((Object)e != null)
            {
                exprs.Add(e);
                coeffs.Add(initial_coeff);
            }
            long constant = 0;

            while (exprs.Count > 0)
            {
                LinearExpr expr = exprs[0];
                exprs.RemoveAt(0);
                long coeff = coeffs[0];
                coeffs.RemoveAt(0);
                if (coeff == 0 || (Object)expr == null)
                {
                    continue;
                }

                if (expr is ProductCst)
                {
                    ProductCst p = (ProductCst)expr;
                    if (p.Coeff != 0)
                    {
                        exprs.Add(p.Expr);
                        coeffs.Add(p.Coeff * coeff);
                    }
                }
                else if (expr is SumArray)
                {
                    SumArray a = (SumArray)expr;
                    constant += coeff * a.Constant;
                    foreach (LinearExpr sub in a.Expressions)
                    {
                        if (sub is IntVar)
                        {
                            IntVar i = (IntVar)sub;
                            if (dict.ContainsKey(i))
                            {
                                dict[i] += coeff;
                            }
                            else
                            {
                                dict.Add(i, coeff);
                            }
                        }
                        else if (sub is ProductCst && ((ProductCst)sub).Expr is IntVar)
                        {
                            ProductCst sub_prod  = (ProductCst)sub;
                            IntVar     i         = (IntVar)sub_prod.Expr;
                            long       sub_coeff = sub_prod.Coeff;

                            if (dict.ContainsKey(i))
                            {
                                dict[i] += coeff * sub_coeff;
                            }
                            else
                            {
                                dict.Add(i, coeff * sub_coeff);
                            }
                        }
                        else
                        {
                            exprs.Add(sub);
                            coeffs.Add(coeff);
                        }
                    }
                }
                else if (expr is IntVar)
                {
                    IntVar i = (IntVar)expr;
                    if (dict.ContainsKey(i))
                    {
                        dict[i] += coeff;
                    }
                    else
                    {
                        dict.Add(i, coeff);
                    }
                }
                else if (expr is NotBooleanVariable)
                {
                    IntVar i = ((NotBooleanVariable)expr).NotVar();
                    if (dict.ContainsKey(i))
                    {
                        dict[i] -= coeff;
                    }
                    else
                    {
                        dict.Add(i, -coeff);
                    }
                    constant += coeff;
                }
                else
                {
                    throw new ArgumentException("Cannot interpret '" + expr.ToString() +
                                                "' in an integer expression");
                }
            }
            return(constant);
        }
 public static LinearExpr Sum(this IntVar[] vars)
 {
     return(LinearExpr.Sum(vars));
 }
예제 #4
0
 public void Maximize(LinearExpr obj)
 {
     SetObjective(obj, false);
 }
예제 #5
0
 public Constraint AddLinearConstraint(LinearExpr linear_expr, long lb,
                                       long ub)
 {
     return(AddLinearExpressionInDomain(linear_expr, new Domain(lb, ub)));
 }
예제 #6
0
 public LinearExpr StartExpr()
 {
     return(LinearExpr.RebuildLinearExprFromLinearExpressionProto(interval_.StartView, model_));
 }
예제 #7
0
 /** <summary> Adds <c>expr</c> to the builder.</summary> */
 public LinearExprBuilder Add(LinearExpr expr)
 {
     return(AddTerm(expr, 1));
 }
예제 #8
0
 public SumArray(LinearExpr a, LinearExpr b)
 {
     AddExpr(a);
     AddExpr(b);
     constant_ = 0L;
 }
예제 #9
0
 public SumArray(LinearExpr a, long b)
 {
     AddExpr(a);
     constant_ = b;
 }
예제 #10
0
 void Init(int size)
 {
     expressions_  = new LinearExpr[size];
     coefficients_ = new long[size];
 }
예제 #11
0
        /**
         * <summary>
         * Adds <c>target == left * right</c>.
         * </summary>
         */
        public Constraint AddMultiplicationEquality(LinearExpr target, LinearExpr left, LinearExpr right)
        {
            LinearArgumentProto prod = new LinearArgumentProto();

            prod.Target         = GetLinearExpressionProto(target);
            prod.Exprs.Capacity = 2;
            prod.Exprs.Add(GetLinearExpressionProto(left));
            prod.Exprs.Add(GetLinearExpressionProto(right));

            Constraint ct = new Constraint(model_);

            ct.Proto.IntProd = prod;
            return(ct);
        }
예제 #12
0
 public SumArray(LinearExpr a, long b)
 {
     Init(1);
     AddExpr(a, 0);
     constant_ = b;
 }
예제 #13
0
 public Term(LinearExpr e, long c)
 {
     this.expr        = e;
     this.coefficient = c;
 }
예제 #14
0
 /** <summary> Adds <c>expr * coefficient</c> to the builder.</summary> */
 public LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
 {
     terms_.Add(new Term(expr, coefficient));
     return(this);
 }
 public static LinearExpr ScalProd(this IntVar[] vars, long[] coeffs)
 {
     return(LinearExpr.ScalProd(vars, coeffs));
 }
예제 #16
0
 public void AddExpr(LinearExpr expr)
 {
     expressions_.Add(expr);
 }
 public SumArray(LinearExpr a, long b)
 {
     expressions_ = new List <LinearExpr>();
     AddExpr(a);
     constant_ = b;
 }
예제 #18
0
 // Objective.
 public void Minimize(LinearExpr obj)
 {
     SetObjective(obj, true);
 }
예제 #19
0
 public LinearExpr EndExpr()
 {
     return(LinearExpr.RebuildLinearExprFromLinearExpressionProto(interval_.EndView, model_));
 }
예제 #20
0
 /** <summary> Creates <c>expr * coeff</c>.</summary> */
 public static LinearExpr Term(LinearExpr expr, long coeff)
 {
     return(Prod(expr, coeff));
 }