public long Value(LinearExpression e) { List <LinearExpression> exprs = new List <LinearExpression>(); List <long> coeffs = new List <long>(); exprs.Add(e); coeffs.Add(1L); long constant = 0; while (exprs.Count > 0) { LinearExpression 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 (LinearExpression 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 static long GetVarValueMap(LinearExpression e, long initial_coeff, Dictionary <IntVar, long> dict) { List <LinearExpression> exprs = new List <LinearExpression>(); List <long> coeffs = new List <long>(); exprs.Add(e); coeffs.Add(initial_coeff); long constant = 0; while (exprs.Count > 0) { LinearExpression 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 (LinearExpression 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); }