Skip to content

Commit

Permalink
polish on model_builder code
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Nov 6, 2023
1 parent 3cb6743 commit fcd64e6
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setName(String name) {
helper.setEnforcedConstraintName(index, name);
}

/** Adds var * coeff to the constraint.*/
/** Adds var * coeff to the constraint. */
public void addTerm(Variable v, double coeff) {
helper.safeAddEnforcedConstraintTerm(index, v.getIndex(), coeff);
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public boolean getIndicatorValue() {
/** Sets the indicator value of the constraint. */
public void setIndicatorValue(boolean b) {
helper.setEnforcedIndicatorValue(index, b);
}
}

/** Inline setter */
public EnforcedLinearConstraint withName(String name) {
Expand Down
40 changes: 25 additions & 15 deletions ortools/java/com/google/ortools/modelbuilder/ModelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ public LinearConstraint constraintFromIndex(int index) {

// Enforced Linear constraints.

/** Adds {@code lb <= expr <= ub}. */
public EnforcedLinearConstraint addEnforcedLinearConstraint(LinearArgument expr, double lb, double ub, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => b <= expr <= ub}. */
public EnforcedLinearConstraint addEnforcedLinearConstraint(
LinearArgument expr, double lb, double ub, Variable iVar, boolean iValue) {
EnforcedLinearConstraint lin = new EnforcedLinearConstraint(helper);
lin.setIndicatorVariable(iVar);
lin.setIndicatorValue(iValue);
Expand All @@ -186,39 +187,45 @@ public EnforcedLinearConstraint addEnforcedLinearConstraint(LinearArgument expr,
return lin;
}

/** Adds {@code expr == value}. */
public EnforcedLinearConstraint addEnforcedEquality(LinearArgument expr, double value, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => expr == value}. */
public EnforcedLinearConstraint addEnforcedEquality(
LinearArgument expr, double value, Variable iVar, boolean iValue) {
return addEnforcedLinearConstraint(expr, value, value, iVar, iValue);
}

/** Adds {@code left == right}. */
public EnforcedLinearConstraint addEnforcedEquality(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => left == right}. */
public EnforcedLinearConstraint addEnforcedEquality(
LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
LinearExprBuilder difference = LinearExpr.newBuilder();
difference.addTerm(left, 1);
difference.addTerm(right, -1);
return addEnforcedLinearConstraint(difference, 0.0, 0.0, iVar, iValue);
}

/** Adds {@code expr <= value}. */
public EnforcedLinearConstraint addEnforcedLessOrEqual(LinearArgument expr, double value, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => expr <= value}. */
public EnforcedLinearConstraint addEnforcedLessOrEqual(
LinearArgument expr, double value, Variable iVar, boolean iValue) {
return addEnforcedLinearConstraint(expr, Double.NEGATIVE_INFINITY, value, iVar, iValue);
}

/** Adds {@code left <= right}. */
public EnforcedLinearConstraint addEnforcedLessOrEqual(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => left <= right}. */
public EnforcedLinearConstraint addEnforcedLessOrEqual(
LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
LinearExprBuilder difference = LinearExpr.newBuilder();
difference.addTerm(left, 1);
difference.addTerm(right, -1);
return addEnforcedLinearConstraint(difference, Double.NEGATIVE_INFINITY, 0.0, iVar, iValue);
}

/** Adds {@code expr >= value}. */
public EnforcedLinearConstraint addEnforcedGreaterOrEqual(LinearArgument expr, double value, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => expr >= value}. */
public EnforcedLinearConstraint addEnforcedGreaterOrEqual(
LinearArgument expr, double value, Variable iVar, boolean iValue) {
return addEnforcedLinearConstraint(expr, value, Double.POSITIVE_INFINITY, iVar, iValue);
}

/** Adds {@code left >= right}. */
public EnforcedLinearConstraint addEnforcedGreaterOrEqual(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
/** Adds {@code ivar == iValue => left >= right}. */
public EnforcedLinearConstraint addEnforcedGreaterOrEqual(
LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
LinearExprBuilder difference = LinearExpr.newBuilder();
difference.addTerm(left, 1);
difference.addTerm(right, -1);
Expand Down Expand Up @@ -272,7 +279,10 @@ void clearHints() {
helper.clearHints();
}

/** 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.
*/
void addHint(Variable v, double value) {
helper.addHint(v.getIndex(), value);
}
Expand Down
14 changes: 5 additions & 9 deletions ortools/linear_solver/csharp/ModelSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public double ObjectiveValue
get {
if (!helper_.HasSolution())
{
throw new SolverException("Solver.ObjectiveValue",
"Solve() was not called or no solution was found");
throw new SolverException("Solver.ObjectiveValue", "Solve() was not called or no solution was found");
}
return helper_.ObjectiveValue();
}
Expand All @@ -150,7 +149,7 @@ public double BestObjectiveBound
if (!helper_.HasSolution())
{
throw new SolverException("Solver.BestObjectiveBound",
"Solve() was not called or no solution was found");
"Solve() was not called or no solution was found");
}
return helper_.BestObjectiveBound();
}
Expand All @@ -176,8 +175,7 @@ public double ReducedCost(Variable var)
{
if (!helper_.HasSolution())
{
throw new SolverException("Solver.ReducedCost())",
"Solve() was not called or no solution was found");
throw new SolverException("Solver.ReducedCost())", "Solve() was not called or no solution was found");
}
return helper_.ReducedCost(var.Index);
}
Expand All @@ -190,8 +188,7 @@ public double DualValue(LinearConstraint ct)
{
if (!helper_.HasSolution())
{
throw new SolverException("Solver.DualValue())",
"Solve() was not called or no solution was found");
throw new SolverException("Solver.DualValue())", "Solve() was not called or no solution was found");
}
return helper_.DualValue(ct.Index);
}
Expand All @@ -204,8 +201,7 @@ public double Activity(LinearConstraint ct)
{
if (!helper_.HasSolution())
{
throw new SolverException("Solver.Activity())",
"Solve() was not called or no solution was found");
throw new SolverException("Solver.Activity())", "Solve() was not called or no solution was found");
}
return helper_.Activity(ct.Index);
}
Expand Down
4 changes: 2 additions & 2 deletions ortools/linear_solver/java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

# Description: java wrapping of the C++ code at ../

load("//bazel:swig_java.bzl", "ortools_java_wrap_cc")
load("@rules_jvm_external//:defs.bzl", "artifact")
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test")
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//bazel:swig_java.bzl", "ortools_java_wrap_cc")

ortools_java_wrap_cc(
name = "modelbuilder",
Expand Down
Loading

0 comments on commit fcd64e6

Please sign in to comment.