From fd55458966ef341b58fc8959cc64a65b928b4d4e Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 31 Oct 2023 12:38:42 +0100 Subject: [PATCH] more work on modelbuilder C#; fix doc on modelbuilder java --- .../modelbuilder/LinearConstraint.java | 16 ++-- .../csharp/ModelBuilderConstraint.cs | 89 +++++++++++++++++++ .../linear_solver/csharp/ModelBuilderExpr.cs | 6 ++ 3 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 ortools/linear_solver/csharp/ModelBuilderConstraint.cs diff --git a/ortools/java/com/google/ortools/modelbuilder/LinearConstraint.java b/ortools/java/com/google/ortools/modelbuilder/LinearConstraint.java index a7f2d522b7c..152a2003726 100644 --- a/ortools/java/com/google/ortools/modelbuilder/LinearConstraint.java +++ b/ortools/java/com/google/ortools/modelbuilder/LinearConstraint.java @@ -35,39 +35,39 @@ public ModelBuilderHelper getHelper() { return helper; } - /** Returns the lower bound of the variable. */ + /** Returns the lower bound of the constraint. */ public double getLowerBound() { return helper.getConstraintLowerBound(index); } - /** Returns the lower bound of the variable. */ + /** Returns the lower bound of the constraint. */ public void setLowerBound(double lb) { helper.setConstraintLowerBound(index, lb); } - /** Returns the upper bound of the variable. */ + /** Returns the upper bound of the constraint. */ public double getUpperBound() { return helper.getConstraintUpperBound(index); } - /** Returns the upper bound of the variable. */ + /** Returns the upper bound of the constraint. */ public void setUpperBound(double ub) { helper.setConstraintUpperBound(index, ub); } - /** Returns the name of the variable given upon creation. */ + /** Returns the name of the constraint given upon creation. */ public String getName() { return helper.getConstraintName(index); } - // Sets the name of the variable. */ + // Sets the name of the constraint. */ public void setName(String name) { helper.setConstraintName(index, name); } // Adds var * coeff to the constraint. - public void addTerm(Variable var, double coeff) { - helper.addConstraintTerm(index, var.getIndex(), coeff); + public void addTerm(Variable v, double coeff) { + helper.addConstraintTerm(index, v.getIndex(), coeff); } /** Inline setter */ diff --git a/ortools/linear_solver/csharp/ModelBuilderConstraint.cs b/ortools/linear_solver/csharp/ModelBuilderConstraint.cs new file mode 100644 index 00000000000..84110aef967 --- /dev/null +++ b/ortools/linear_solver/csharp/ModelBuilderConstraint.cs @@ -0,0 +1,89 @@ +// Copyright 2010-2022 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(); + } + + 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. + public void AddTerm(Variable var, double coeff) + { + helper_.AddConstraintTerm(index_, var.Index, coeff); + } + + /** Inline setter */ + public LinearConstraint WithName(String name) + { + Name = name; + return this; + } + + private readonly int index_; + private ModelBuilderHelper helper_; + } + +} // namespace Google.OrTools.ModelBuilder \ No newline at end of file diff --git a/ortools/linear_solver/csharp/ModelBuilderExpr.cs b/ortools/linear_solver/csharp/ModelBuilderExpr.cs index 67961cfef32..8e85c192384 100644 --- a/ortools/linear_solver/csharp/ModelBuilderExpr.cs +++ b/ortools/linear_solver/csharp/ModelBuilderExpr.cs @@ -515,6 +515,9 @@ public double LowerBound get { return helper_.VarLowerBound(index_); } + set { + helper_.SetVarLowerBound(index_, value); + } } public double UpperBound @@ -522,6 +525,9 @@ public double UpperBound get { return helper_.VarUpperBound(index_); } + set { + helper_.SetVarUpperBound(index_, value); + } } public override string ToString()