Пример #1
0
        public static LinearExpr RebuildLinearExprFromLinearExpressionProto(LinearExpressionProto proto,
                                                                            CpModelProto model)
        {
            int  numElements = proto.Vars.Count;
            long offset      = proto.Offset;

            if (numElements == 0)
            {
                return(LinearExpr.Constant(offset));
            }
            else
            if (numElements == 1)
            {
                IntVar var   = new IntVar(model, proto.Vars[0]);
                long   coeff = proto.Coeffs[0];
                return(LinearExpr.Affine(var, coeff, offset));
            }
            else
            {
                LinearExpr[] exprs = new LinearExpr[numElements];
                for (int i = 0; i < numElements; ++i)
                {
                    IntVar var   = new IntVar(model, proto.Vars[i]);
                    long   coeff = proto.Coeffs[i];
                    exprs[i] = Prod(var, coeff);
                }
                SumArray sum = new SumArray(exprs);
                sum.Offset = sum.Offset + offset;
                return(sum);
            }
        }
Пример #2
0
        public long Value(IntegerExpression e)
        {
            List <IntegerExpression> exprs = new List <IntegerExpression>();
            List <long> coeffs             = new List <long>();

            exprs.Add(e);
            coeffs.Add(1L);
            long constant = 0;

            while (exprs.Count > 0)
            {
                IntegerExpression expr = exprs[0];
                exprs.RemoveAt(0);
                long coeff = coeffs[0];
                coeffs.RemoveAt(0);
                if (coeff == 0)
                {
                    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 (IntegerExpression sub in a.Expressions)
                    {
                        exprs.Add(sub);
                        coeffs.Add(coeff);
                    }
                }
                else if (expr is IntVar)
                {
                    int  index = expr.Index;
                    long value = index >= 0 ? response_.Solution[index]
                                  : -response_.Solution[-index - 1];
                    constant += coeff * value;
                }
                else if (expr is NotBooleanVariable)
                {
                    throw new ArgumentException(
                              "Cannot evaluate a literal in an integer expression.");
                }
                else
                {
                    throw new ArgumentException("Cannot evaluate '" + expr.ToString() +
                                                "' in an integer expression");
                }
            }
            return(constant);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public static long GetVarValueMap(IntegerExpression e,
                                          long initial_coeff,
                                          Dictionary <IntVar, long> dict)
        {
            List <IntegerExpression> exprs = new List <IntegerExpression>();
            List <long> coeffs             = new List <long>();

            exprs.Add(e);
            coeffs.Add(initial_coeff);
            long constant = 0;

            while (exprs.Count > 0)
            {
                IntegerExpression expr = exprs[0];
                exprs.RemoveAt(0);
                long coeff = coeffs[0];
                coeffs.RemoveAt(0);
                if (coeff == 0)
                {
                    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 (IntegerExpression sub in a.Expressions)
                    {
                        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)
                {
                    throw new ArgumentException(
                              "Cannot interpret a literal in an integer expression.");
                }
                else
                {
                    throw new ArgumentException("Cannot interpret '" + expr.ToString() +
                                                "' in an integer expression");
                }
            }
            return(constant);
        }