internal LinearExpressionProto GetLinearExpressionProto(LinearExpr expr, bool negate = false) { var dict = var_value_map_; dict.Clear(); long constant = LinearExpr.GetVarValueMap(expr, dict, terms_); LinearExpressionProto linear = new LinearExpressionProto(); var dictCount = dict.Count; linear.Vars.Capacity = dictCount; linear.Vars.AddRange(dict.Keys); linear.Coeffs.Capacity = dictCount; if (negate) { foreach (var coeff in dict.Values) { linear.Coeffs.Add(-coeff); } linear.Offset = -constant; } else { linear.Coeffs.AddRange(dict.Values); linear.Offset = constant; } return(linear); }
public Constraint AddLinearExpressionInDomain(LinearExpr linear_expr, Domain domain) { Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpr.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); }
// Internal methods. void SetObjective(LinearExpr obj, bool minimize) { CpObjectiveProto objective = new CpObjectiveProto(); if (obj is null) { objective.Offset = 0L; objective.ScalingFactor = minimize ? 1L : -1; } else if (obj is IntVar intVar) { objective.Offset = 0L; objective.Vars.Capacity = 1; objective.Vars.Add(intVar.Index); objective.Coeffs.Capacity = 1; if (minimize) { objective.Coeffs.Add(1L); objective.ScalingFactor = 1L; } else { objective.Coeffs.Add(-1L); objective.ScalingFactor = -1L; } } else { var dict = var_value_map_; dict.Clear(); long constant = LinearExpr.GetVarValueMap(obj, dict, terms_); var dictCount = dict.Count; objective.Vars.Capacity = dictCount; objective.Vars.AddRange(dict.Keys); objective.Coeffs.Capacity = dictCount; if (minimize) { objective.Coeffs.AddRange(dict.Values); objective.ScalingFactor = 1L; objective.Offset = constant; } else { foreach (var coeff in dict.Values) { objective.Coeffs.Add(-coeff); } objective.ScalingFactor = -1L; objective.Offset = -constant; } } model_.Objective = objective; }
// Internal methods. void SetObjective(LinearExpr obj, bool minimize) { CpObjectiveProto objective = new CpObjectiveProto(); if (obj is null) { objective.Offset = 0L; objective.ScalingFactor = minimize ? 1L : -1; } else if (obj is IntVar intVar) { objective.Offset = 0L; objective.Vars.Capacity = 1; objective.Vars.Add(intVar.Index); objective.Coeffs.Capacity = 1; if (minimize) { objective.Coeffs.Add(1L); objective.ScalingFactor = 1L; } else { objective.Coeffs.Add(-1L); objective.ScalingFactor = -1L; } } else { Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpr.GetVarValueMap(obj, 1L, dict, terms_); if (minimize) { objective.ScalingFactor = 1L; objective.Offset = constant; } else { objective.ScalingFactor = -1L; objective.Offset = -constant; } var dictCount = dict.Count; objective.Vars.Capacity = dictCount; objective.Coeffs.Capacity = dictCount; foreach (KeyValuePair <IntVar, long> it in dict) { objective.Vars.Add(it.Key.Index); objective.Coeffs.Add(minimize ? it.Value : -it.Value); } } model_.Objective = objective; }
private LinearExpressionProto GetLinearExpressionProto(LinearExpr expr, bool negate = false) { Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpr.GetVarValueMap(expr, 1L, dict); long mult = negate ? -1 : 1; LinearExpressionProto linear = new LinearExpressionProto(); foreach (KeyValuePair <IntVar, long> term in dict) { linear.Vars.Add(term.Key.Index); linear.Coeffs.Add(term.Value * mult); } linear.Offset = constant * mult; return(linear); }
private long FillLinearConstraint(LinearExpr expr, out LinearConstraintProto linear) { var dict = var_value_map_; dict.Clear(); long constant = LinearExpr.GetVarValueMap(expr, dict, terms_); var count = dict.Count; linear = new LinearConstraintProto(); linear.Vars.Capacity = count; linear.Vars.AddRange(dict.Keys); linear.Coeffs.Capacity = count; linear.Coeffs.AddRange(dict.Values); return(constant); }
// Internal methods. void SetObjective(LinearExpr 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 = LinearExpr.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; }
private long FillLinearConstraint(LinearExpr expr, out LinearConstraintProto linear) { linear = new LinearConstraintProto(); Dictionary <IntVar, long> dict = new Dictionary <IntVar, long>(); long constant = LinearExpr.GetVarValueMap(expr, 1L, dict, terms_); var count = dict.Count; linear.Vars.Capacity = count; linear.Coeffs.Capacity = count; foreach (KeyValuePair <IntVar, long> term in dict) { linear.Vars.Add(term.Key.Index); linear.Coeffs.Add(term.Value); } return(constant); }