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); }
public override string ToString() { string result = ""; for (int i = 0; i < expressions_.Count; ++i) { bool negated = false; IntegerExpression expr = expressions_[i]; if (i != 0) { if (expr is ProductCst && ((ProductCst)expr).Coeff < 0) { result += String.Format(" - "); negated = true; } else { result += String.Format(" + "); } } if (expr is IntVar) { result += expr.ShortString(); } else if (expr is ProductCst) { ProductCst p = (ProductCst)expr; long coeff = negated ? -p.Coeff : p.Coeff; IntegerExpression sub = p.Expr; if (coeff == 1) { result += sub.ShortString(); } else if (coeff == -1) { result += String.Format("-{0}", coeff, sub.ShortString()); } else { result += String.Format("{0}*{1}", coeff, sub.ShortString()); } } else { result += String.Format("({0})", expr.ShortString()); } } return(result); }
public void AddTerm(LinearExpr expr, long coeff, int index) { if (expr is ProductCst) { ProductCst p = (ProductCst)expr; expressions_[index] = p.Expr; coefficients_[index] = p.Coeff * coeff; } else { expressions_[index] = expr; coefficients_[index] = coeff; } }
public static LinearExpr Prod(LinearExpr e, long v) { if (v == 1) { return(e); } else if (e is ProductCst) { ProductCst p = (ProductCst)e; return(new ProductCst(p.Expr, p.Coeff * v)); } else { return(new ProductCst(e, v)); } }
public static IntegerExpression Prod(IntegerExpression e, long v) { if (v == 1) { return(e); } else if (e is ProductCst) { ProductCst p = (ProductCst)e; return(new ProductCst(p.Expr, p.Coeff * v)); } else { return(new ProductCst(e, 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 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); }