Skip to content

Commit

Permalink
[ModelBuilder] Add clear_terms and set_coefficient everywhere; finish…
Browse files Browse the repository at this point in the history
… indicator constraints in C#
  • Loading branch information
lperron committed Nov 5, 2023
1 parent ce9f1ed commit 686b530
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public EnforcedLinearConstraint(ModelBuilderHelper helper) {
}

EnforcedLinearConstraint(ModelBuilderHelper helper, int index) {
if (!helper.isEnforcedConstraint(index)) {
throw new IllegalArgumentException(
"the given index does not refer to an enforced linear constraint");
}
this.helper = helper;
this.index = index;
}
Expand Down Expand Up @@ -65,27 +69,37 @@ public void setName(String name) {
helper.setEnforcedConstraintName(index, name);
}

// Adds var * coeff to the constraint.
public void addEnforcedTerm(Variable v, double coeff) {
/** Adds var * coeff to the constraint.*/
public void addTerm(Variable v, double coeff) {
helper.safeAddEnforcedConstraintTerm(index, v.getIndex(), coeff);
}

// Returns the indicator variable of the constraint.
/** Sets the coefficient of v to coeff, adding or removing a term if needed. */
public void setCoefficient(Variable v, double coeff) {
helper.setEnforcedConstraintCoefficient(index, v.getIndex(), coeff);
}

/** Clear all terms. */
public void clearTerms() {
helper.clearEnforcedConstraintTerms(index);
}

/** Returns the indicator variable of the constraint/ */
public Variable getIndicatorVariable() {
return new Variable(helper, helper.getEnforcedIndicatorVariableIndex(index));
}

// Sets the indicator variable of the constraint.
/** Sets the indicator variable of the constraint. */
public void setIndicatorVariable(Variable v) {
helper.setEnforcedIndicatorVariable(index, v.index);
helper.setEnforcedIndicatorVariableIndex(index, v.index);
}

// Returns the indicator value of the constraint.
/** Returns the indicator value of the constraint. */
public boolean getIndicatorValue() {
return helper.getEnforcedIndicatorValue(index);
}

// Sets the indicator value of the constraint.
/** Sets the indicator value of the constraint. */
public void setIndicatorValue(boolean b) {
helper.setEnforcedIndicatorValue(index, b);
}
Expand Down
14 changes: 12 additions & 2 deletions ortools/java/com/google/ortools/modelbuilder/LinearConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,26 @@ public String getName() {
return helper.getConstraintName(index);
}

// Sets the name of the constraint. */
/** Sets the name of the constraint. */
public void setName(String name) {
helper.setConstraintName(index, name);
}

// Adds var * coeff to the constraint.
/** Adds var * coeff to the constraint. */
public void addTerm(Variable v, double coeff) {
helper.addConstraintTerm(index, v.getIndex(), coeff);
}

/** Sets the coefficient of v to coeff, adding or removing a term if needed. */
public void setCoefficient(Variable v, double coeff) {
helper.setConstraintCoefficient(index, v.getIndex(), coeff);
}

/** Clear all terms. */
public void clearTerms() {
helper.clearConstraintTerms(index);
}

/** Inline setter */
public LinearConstraint withName(String name) {
setName(name);
Expand Down
98 changes: 88 additions & 10 deletions ortools/linear_solver/csharp/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,88 @@ public LinearConstraint AddLinearConstraint(LinearExpr expr, double lb, double u
return lin;
}

/// Rebuilds a linear constraint from its index.
public LinearConstraint ConstraintFromIndex(int index)
{
return new LinearConstraint(helper_, index);
}

/// <summary>
/// Returns the number of variables in the model.
/// Adds an enforced Linear constraint to the model.
/// </summary>
public int VariablesCount()
/// <param name="lin">A bounded linear expression</param>
/// <param name="iVar>The indicator variable of the constraint.</param>

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net

XML comment has badly formed XML -- 'Missing closing quotation mark for string literal.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net

XML comment has badly formed XML -- 'Expected '>' or '/>' to close tag 'param'.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • SCIP OFF

XML comment has badly formed XML -- 'Missing closing quotation mark for string literal.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • SCIP OFF

XML comment has badly formed XML -- 'Expected '>' or '/>' to close tag 'param'.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • SCIP OFF

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • CoinOR OFF

XML comment has badly formed XML -- 'Missing closing quotation mark for string literal.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • CoinOR OFF

XML comment has badly formed XML -- 'Expected '>' or '/>' to close tag 'param'.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / Linux • CMake • .Net • CoinOR OFF

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Unix Makefiles • .Net

XML comment has badly formed XML -- 'Missing closing quotation mark for string literal.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Unix Makefiles • .Net

XML comment has badly formed XML -- 'Expected '>' or '/>' to close tag 'param'.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Unix Makefiles • .Net

XML comment has badly formed XML -- 'End tag was not expected at this location.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Xcode • .Net

XML comment has badly formed XML -- 'Missing closing quotation mark for string literal.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Xcode • .Net

XML comment has badly formed XML -- 'Expected '>' or '/>' to close tag 'param'.'

Check warning on line 200 in ortools/linear_solver/csharp/ModelBuilder.cs

View workflow job for this annotation

GitHub Actions / MacOS • Xcode • .Net

XML comment has badly formed XML -- 'End tag was not expected at this location.'
/// <param name="iValue>The indicator value of the constraint.</param>
/// <returns>A linear expression</returns>
/// <exception cref="ArgumentException">Throw when the constraint is not supported by the linear solver</exception>
public EnforcedLinearConstraint AddEnforced(BoundedLinearExpression lin, Variable iVar, bool iValue)
{
return helper_.VariablesCount();
switch (lin.CtType)
{
case BoundedLinearExpression.Type.BoundExpression: {
return AddEnforcedLinearConstraint(lin.Left, lin.Lb, lin.Ub, iVar, iValue);
}
case BoundedLinearExpression.Type.VarEqVar: {
return AddEnforcedLinearConstraint(lin.Left - lin.Right, 0, 0, iVar, iValue);
}
case BoundedLinearExpression.Type.VarEqCst: {
return AddEnforcedLinearConstraint(lin.Left, lin.Lb, lin.Lb, iVar, iValue);
}
default: {
throw new ArgumentException("Cannot use '" + lin.ToString() + "' as a linear constraint.");
}
}
}

/// <summary>
/// Returns the number of constraints in the model.
/// Adds the constraint iVar == iValue => expr in [lb, ub].
/// </summary>
public int ConstraintsCount()
/// <param name="expr">The constrained expression</param>
/// <param name="lb">the lower bound of the constraint</param>
/// <param name="ub">the upper bound of the constraint</param>
/// <param name="iVar">the indicator variable of the constraint</param>
/// <param name="iValue">the indicator value of the constraint</param>
/// <returns>the enforced linear constraint</returns>
public EnforcedLinearConstraint AddEnforcedLinearConstraint(LinearExpr expr, double lb, double ub, Variable iVar,
bool iValue)
{
return helper_.ConstraintsCount();
var dict = tmp_var_value_map_;
dict.Clear();
double offset = LinearExpr.GetVarValueMap(expr, dict, tmp_terms_);
EnforcedLinearConstraint lin = new EnforcedLinearConstraint(helper_);
lin.IndicatorVariable = iVar;
lin.IndicatorValue = iValue;
foreach (KeyValuePair<int, double> term in dict)
{
helper_.AddEnforcedConstraintTerm(lin.Index, term.Key, term.Value);
}
if (lb == Double.NegativeInfinity || lb == Double.PositiveInfinity)
{
lin.LowerBound = lb;
}
else
{
lin.LowerBound = lb - offset;
}
if (ub == Double.NegativeInfinity || ub == Double.PositiveInfinity)
{
lin.UpperBound = ub;
}
else
{
lin.UpperBound = ub - offset;
}
return lin;
}

/// Rebuilds a linear constraint from its index.
public LinearConstraint ConstraintFromIndex(int index)
public EnforcedLinearConstraint EnforcedConstraintFromIndex(int index)
{
return new LinearConstraint(helper_, index);
return new EnforcedLinearConstraint(helper_, index);
}

// Objective.

/// <summary>
/// Minimize expression.
/// </summary>
Expand Down Expand Up @@ -265,18 +325,36 @@ public double ObjectiveOffset
/// <summary>
/// Remove all hints from the model.
/// </summary>
public void ClearHints() {
public void ClearHints()
{
helper_.ClearHints();
}

/// <summary>
/// Adds var == value as a hint to the model. Note that variables must not appear more than once in the list of hints.
/// Adds var == value as a hint to the model. Note that variables must not appear more than once in the list of
/// hints.
/// </summary>
public void AddHint(Variable var, double value)
{
helper_.AddHint(var.Index, value);
}

/// <summary>
/// Returns the number of variables in the model.
/// </summary>
public int VariablesCount()
{
return helper_.VariablesCount();
}

/// <summary>
/// Returns the number of constraints in the model.
/// </summary>
public int ConstraintsCount()
{
return helper_.ConstraintsCount();
}

/// <summary>
/// The name of the model.
/// </summary>
Expand Down
Loading

0 comments on commit 686b530

Please sign in to comment.