public SumArray(LinearExpression a, LinearExpression b) { expressions_ = new List <LinearExpression>(); expressions_.Add(a); expressions_.Add(b); constant_ = 0L; }
public Constraint AddLinearExpressionInDomain(LinearExpression linear_expr, Domain domain) { Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpression.GetVarValueMap(linear_expr, 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); } foreach (long value in domain.FlattenedIntervals()) { if (value == Int64.MinValue || value == Int64.MaxValue) { linear.Domain.Add(value); } else { linear.Domain.Add(value - constant); } } ct.Proto.Linear = linear; return(ct); }
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 BoundedLinearExpression(LinearExpression left, long v, bool equality) { left_ = left; right_ = null; lb_ = v; ub_ = 0; type_ = equality ? Type.VarEqCst : Type.VarDiffCst; }
public BoundedLinearExpression(long lb, LinearExpression expr, long ub) { left_ = expr; right_ = null; lb_ = lb; ub_ = ub; type_ = Type.BoundExpression; }
public BoundedLinearExpression(LinearExpression left, LinearExpression right, bool equality) { left_ = left; right_ = right; lb_ = 0; ub_ = 0; type_ = equality ? Type.VarEqVar : Type.VarDiffVar; }
public static SumArray ScalProd(this IntVar[] vars, long[] coeffs) { LinearExpression[] exprs = new LinearExpression[vars.Length]; for (int i = 0; i < vars.Length; ++i) { exprs[i] = vars[i] * coeffs[i]; } return(new SumArray(exprs)); }
public override string ToString() { string result = ""; for (int i = 0; i < expressions_.Count; ++i) { bool negated = false; LinearExpression 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; LinearExpression 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); }
// Internal methods. void SetObjective(LinearExpression obj, bool minimize) { CpObjectiveProto objective = new CpObjectiveProto(); if (obj is IntVar) { objective.Coeffs.Add(1L); objective.Offset = 0L; if (minimize) { objective.Vars.Add(obj.Index); objective.ScalingFactor = 1L; } else { objective.Vars.Add(Negated(obj.Index)); objective.ScalingFactor = -1L; } } else { Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpression.GetVarValueMap(obj, 1L, dict); if (minimize) { objective.ScalingFactor = 1L; objective.Offset = constant; } else { objective.ScalingFactor = -1L; objective.Offset = -constant; } foreach (KeyValuePair <IntVar, long> it in dict) { objective.Coeffs.Add(it.Value); if (minimize) { objective.Vars.Add(it.Key.Index); } else { objective.Vars.Add(Negated(it.Key.Index)); } } } model_.Objective = objective; }
public static LinearExpression Prod(LinearExpression 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 Constraint AddLinearConstraint(LinearExpression linear_expr, long lb, long ub) { return(AddLinearExpressionInDomain(linear_expr, new Domain(lb, ub))); }
public void Maximize(LinearExpression obj) { SetObjective(obj, false); }
// Objective. public void Minimize(LinearExpression obj) { SetObjective(obj, true); }
public SumArray(LinearExpression a, long b) { expressions_ = new List <LinearExpression>(); expressions_.Add(a); constant_ = b; }
public ProductCst(LinearExpression e, long v) { expr_ = e; coeff_ = v; }
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); }