Skip to content

Commit

Permalink
more work on modelbuilder C#; fix doc on modelbuilder java
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 31, 2023
1 parent eb32e1f commit fd55458
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
89 changes: 89 additions & 0 deletions ortools/linear_solver/csharp/ModelBuilderConstraint.cs
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions ortools/linear_solver/csharp/ModelBuilderExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,19 @@ public double LowerBound
get {
return helper_.VarLowerBound(index_);
}
set {
helper_.SetVarLowerBound(index_, value);
}
}

public double UpperBound
{
get {
return helper_.VarUpperBound(index_);
}
set {
helper_.SetVarUpperBound(index_, value);
}
}

public override string ToString()
Expand Down

0 comments on commit fd55458

Please sign in to comment.