コード例 #1
0
 public SumArray(IntegerExpression a, long b)
 {
     expressions_.Add(a);
     constant_ = b;
 }
コード例 #2
0
 public ProductCst(IntegerExpression e, long v)
 {
     expr_  = e;
     coeff_ = v;
 }
コード例 #3
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);
        }
コード例 #4
0
ファイル: CpModel.cs プロジェクト: nicolasPozin/or-tools
 // Objective.
 public void Minimize(IntegerExpression obj)
 {
     SetObjective(obj, true);
 }
コード例 #5
0
ファイル: CpModel.cs プロジェクト: nicolasPozin/or-tools
 public void Maximize(IntegerExpression obj)
 {
     SetObjective(obj, false);
 }
コード例 #6
0
ファイル: CpModel.cs プロジェクト: nicolasPozin/or-tools
 public Constraint Add(BoundIntegerExpression lin)
 {
     switch (lin.CtType)
     {
       case BoundIntegerExpression.Type.BoundExpression:
     {
       Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
       long constant = IntegerExpression.GetVarValueMap(lin.Left, 1L, dict);
       Constraint ct = new Constraint(model_);
       LinearConstraintProto linear = new LinearConstraintProto();
       foreach (KeyValuePair<IntVar, long> term in dict)
       {
     linear.Vars.Add(term.Key.Index);
     linear.Coeffs.Add(term.Value);
       }
       linear.Domain.Add(lin.Lb == Int64.MinValue ? Int64.MinValue
                     : lin.Lb - constant);
       linear.Domain.Add(lin.Ub == Int64.MaxValue ? Int64.MaxValue
                     : lin.Ub - constant);
       ct.Proto.Linear = linear;
       return ct;
     }
       case BoundIntegerExpression.Type.VarEqVar:
     {
       Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
       long constant = IntegerExpression.GetVarValueMap(lin.Left, 1L, dict);
       constant +=  IntegerExpression.GetVarValueMap(lin.Right, -1L, dict);
       Constraint ct = new Constraint(model_);
       LinearConstraintProto linear = new LinearConstraintProto();
       foreach (KeyValuePair<IntVar, long> term in dict)
       {
     linear.Vars.Add(term.Key.Index);
     linear.Coeffs.Add(term.Value);
       }
       linear.Domain.Add(-constant);
       linear.Domain.Add(-constant);
       ct.Proto.Linear = linear;
       return ct;
     }
       case BoundIntegerExpression.Type.VarDiffVar:
     {
       Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
       long constant = IntegerExpression.GetVarValueMap(lin.Left, 1L, dict);
       constant +=  IntegerExpression.GetVarValueMap(lin.Right, -1L, dict);
       Constraint ct = new Constraint(model_);
       LinearConstraintProto linear = new LinearConstraintProto();
       foreach (KeyValuePair<IntVar, long> term in dict)
       {
     linear.Vars.Add(term.Key.Index);
     linear.Coeffs.Add(term.Value);
       }
       linear.Domain.Add(Int64.MinValue);
       linear.Domain.Add(-constant - 1);
       linear.Domain.Add(-constant + 1);
       linear.Domain.Add(Int64.MaxValue);
       ct.Proto.Linear = linear;
       return ct;
     }
       case BoundIntegerExpression.Type.VarEqCst:
     {
       Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
       long constant = IntegerExpression.GetVarValueMap(lin.Left, 1L, dict);
       Constraint ct = new Constraint(model_);
       LinearConstraintProto linear = new LinearConstraintProto();
       foreach (KeyValuePair<IntVar, long> term in dict)
       {
     linear.Vars.Add(term.Key.Index);
     linear.Coeffs.Add(term.Value);
       }
       linear.Domain.Add(lin.Lb - constant);
       linear.Domain.Add(lin.Lb - constant);
       ct.Proto.Linear = linear;
       return ct;
     }
       case BoundIntegerExpression.Type.VarDiffCst:
     {
       Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
       long constant = IntegerExpression.GetVarValueMap(lin.Left, 1L, dict);
       Constraint ct = new Constraint(model_);
       LinearConstraintProto linear = new LinearConstraintProto();
       foreach (KeyValuePair<IntVar, long> term in dict)
       {
     linear.Vars.Add(term.Key.Index);
     linear.Coeffs.Add(term.Value);
       }
       linear.Domain.Add(Int64.MinValue);
       linear.Domain.Add(lin.Lb - constant - 1);
       linear.Domain.Add(lin.Lb - constant + 1);
       linear.Domain.Add(Int64.MaxValue);
       ct.Proto.Linear = linear;
       return ct;
     }
     }
     return null;
 }