// Copyright 2010-2025 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Google.OrTools.ModelBuilder { using Google.OrTools.Util; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using Google.Protobuf.Collections; /// /// Wrapper around a linear constraint stored in the ModelBuilderHelper instance. /// public class LinearConstraint { public LinearConstraint(ModelBuilderHelper helper) { helper_ = helper; index_ = helper_.AddLinearConstraint(); } public LinearConstraint(ModelBuilderHelper helper, int index) { helper_ = helper; index_ = index; } /// /// Returns the index of the constraint in the model. /// public int Index { get { return index_; } } /// /// Returns the constraint builder. /// public ModelBuilderHelper Helper { get { return helper_; } } /// /// The lower bound of the constraint. /// public double LowerBound { get { return helper_.ConstraintLowerBound(index_); } set { helper_.SetConstraintLowerBound(index_, value); } } /// /// The upper bound of the constraint. /// public double UpperBound { get { return helper_.ConstraintUpperBound(index_); } set { helper_.SetConstraintUpperBound(index_, value); } } /// /// The name of the variable given upon creation. /// public String Name { get { return helper_.ConstraintName(index_); } set { helper_.SetConstraintName(index_, value); } } /// /// Adds var * coeff to the constraint. /// /// the variable of the term to add /// the coefficient of the term to add public void AddTerm(Variable var, double coeff) { helper_.SafeAddConstraintTerm(index_, var.Index, coeff); } /// /// Sets the coefficient of var to coeff, adding or removing a term if needed. /// /// the variable of the term to set /// the coefficient of the term to set public void SetVariableCoefficient(Variable var, double coeff) { helper_.SetConstraintCoefficient(index_, var.Index, coeff); } /// /// Clear all terms of the constraint. /// public void ClearTerms() { helper_.ClearConstraintTerms(index_); } /// /// Inline name setter. /// /// the new name of the variable /// this public LinearConstraint WithName(String name) { Name = name; return this; } private readonly int index_; private ModelBuilderHelper helper_; } /// /// Wrapper around an enforced linear constraint stored in the ModelBuilderHelper instance. /// public class EnforcedLinearConstraint { public EnforcedLinearConstraint(ModelBuilderHelper helper) { helper_ = helper; index_ = helper_.AddEnforcedLinearConstraint(); } public EnforcedLinearConstraint(ModelBuilderHelper helper, int index) { if (!helper.IsEnforcedConstraint(index)) { throw new ArgumentException("the given index does not refer to an enforced linear constraint"); } helper_ = helper; index_ = index; } /// /// Returns the index of the constraint in the model. /// public int Index { get { return index_; } } /// /// Returns the constraint builder. /// public ModelBuilderHelper Helper { get { return helper_; } } /// /// The lower bound of the constraint. /// public double LowerBound { get { return helper_.EnforcedConstraintLowerBound(index_); } set { helper_.SetEnforcedConstraintLowerBound(index_, value); } } /// /// The upper bound of the constraint. /// public double UpperBound { get { return helper_.EnforcedConstraintUpperBound(index_); } set { helper_.SetEnforcedConstraintUpperBound(index_, value); } } /// /// The indicator variable of the constraint. /// public Variable IndicatorVariable { get { return new Variable(helper_, helper_.EnforcedIndicatorVariableIndex(index_)); } set { helper_.SetEnforcedIndicatorVariableIndex(index_, value.Index); } } /// /// The indicator value of the constraint. /// public bool IndicatorValue { get { return helper_.EnforcedIndicatorValue(index_); } set { helper_.SetEnforcedIndicatorValue(index_, value); } } /// /// The name of the variable given upon creation. /// public String Name { get { return helper_.EnforcedConstraintName(index_); } set { helper_.SetEnforcedConstraintName(index_, value); } } /// /// Adds var * coeff to the constraint. /// /// the variable of the term to add /// the coefficient of the term to add public void AddTerm(Variable var, double coeff) { helper_.SafeAddEnforcedConstraintTerm(index_, var.Index, coeff); } /// /// Sets the coefficient of var to coeff, adding or removing a term if needed. /// /// the variable of the term to set /// the coefficient of the term to set public void SetVariableCoefficient(Variable var, double coeff) { helper_.SetEnforcedConstraintCoefficient(index_, var.Index, coeff); } /// /// Clear all terms of the constraint. /// public void ClearTerms() { helper_.ClearEnforcedConstraintTerms(index_); } /// /// Inline setter /// /// The name to assign /// this public EnforcedLinearConstraint WithName(String name) { Name = name; return this; } private readonly int index_; private ModelBuilderHelper helper_; } } // namespace Google.OrTools.ModelBuilder