Skip to content

Commit d229e48

Browse files
committed
polish model_builder code and samples; tweak C# wrapping code
1 parent 44948b1 commit d229e48

15 files changed

Lines changed: 70 additions & 37 deletions

File tree

ortools/constraint_solver/csharp/constraint_solver.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ DEFINE_ARGS_TO_R_CALLBACK(
238238
#undef DEFINE_ARGS_TO_R_CALLBACK
239239
#undef DEFINE_VOID_TO_STRING_CALLBACK
240240

241-
// RENAMING
241+
// Renaming
242242
namespace operations_research {
243243

244244
// Decision

ortools/dotnet/main.dotnet.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This repository contains several component:
1515
@li @ref Google.OrTools.Graph "Google.OrTools.Graph"
1616

1717
@li @ref Google.OrTools.LinearSolver "Google.OrTools.LinearSolver"
18-
@li @ref Google.OrTools.LinearSolver "Google.OrTools.ModelBuilder"
18+
@li @ref Google.OrTools.ModelBuilder "Google.OrTools.ModelBuilder"
1919
@li @ref Google.OrTools.Bop "Google.OrTools.Bop"
2020
@li @ref Google.OrTools.Glop "Google.OrTools.Glop"
2121
@li @ref Google.OrTools.Sat "Google.OrTools.SAT"

ortools/linear_solver/csharp/ModelBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ public ModelBuilderHelper Helper
329329
private ModelBuilderHelper helper_;
330330
private Dictionary<double, int> constantMap_;
331331

332-
// Used to process linear exppressions.
332+
// Used to process linear expressions.
333333
private SortedDictionary<int, double> tmp_var_value_map_;
334334
private Queue<Term> tmp_terms_;
335335
}
336336

337-
} // namespace Google.OrTools.ModelBuilder
337+
} // namespace Google.OrTools.ModelBuilder

ortools/linear_solver/csharp/ModelSolver.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public bool HasSolution()
125125
}
126126

127127
/// <summary>
128-
/// The best objective value found during search. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
128+
/// The best objective value found during search. This raises a ModelSolverException is no solution has been found,
129+
/// or if Solve() has not been called.
129130
/// </summary>
130131
public double ObjectiveValue
131132
{
@@ -140,7 +141,8 @@ public double ObjectiveValue
140141
}
141142

142143
/// <summary>
143-
/// The best objective bound found during search. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
144+
/// The best objective bound found during search. This raises a ModelSolverException is no solution has been found,
145+
/// or if Solve() has not been called.
144146
/// </summary>
145147
public double BestObjectiveBound
146148
{
@@ -155,19 +157,20 @@ public double BestObjectiveBound
155157
}
156158

157159
/// <summary>
158-
/// The value of a variable in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
160+
/// The value of a variable in the current solution. This raises a ModelSolverException is no solution has been
161+
/// found, or if Solve() has not been called.
159162
/// </summary>
160163
public double Value(Variable var)
161164
{
162165
if (!helper_.HasSolution())
163166
{
164-
throw new ModelSolverException("ModelSolver.Value())",
165-
"Solve() was not called or no solution was found");
167+
throw new ModelSolverException("ModelSolver.Value())", "Solve() was not called or no solution was found");
166168
}
167169
return helper_.VariableValue(var.Index);
168170
}
169171
/// <summary>
170-
/// The reduced cost of a variable in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
172+
/// The reduced cost of a variable in the current solution. This raises a ModelSolverException is no solution has
173+
/// been found, or if Solve() has not been called.
171174
/// </summary>
172175
public double ReducedCost(Variable var)
173176
{
@@ -180,7 +183,8 @@ public double ReducedCost(Variable var)
180183
}
181184

182185
/// <summary>
183-
/// The dual value of a linear constraint in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
186+
/// The dual value of a linear constraint in the current solution. This raises a ModelSolverException is no solution
187+
/// has been found, or if Solve() has not been called.
184188
/// </summary>
185189
public double DualValue(LinearConstraint ct)
186190
{
@@ -193,7 +197,8 @@ public double DualValue(LinearConstraint ct)
193197
}
194198

195199
/// <summary>
196-
/// The activity of a constraint in the current solution. This raises a ModelSolverException is no solution has been found, or if Solve() has not been called.
200+
/// The activity of a constraint in the current solution. This raises a ModelSolverException is no solution has been
201+
/// found, or if Solve() has not been called.
197202
/// </summary>
198203
public double Activity(LinearConstraint ct)
199204
{

ortools/linear_solver/proto_solver/gurobi_proto_solver.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515

1616
#include <algorithm>
1717
#include <cmath>
18+
#include <cstdint>
1819
#include <limits>
1920
#include <memory>
2021
#include <numeric>
2122
#include <string>
2223
#include <vector>
2324

25+
#include "absl/base/attributes.h"
26+
#include "absl/log/check.h"
2427
#include "absl/status/status.h"
2528
#include "absl/status/statusor.h"
2629
#include "absl/strings/str_cat.h"
2730
#include "absl/strings/str_format.h"
2831
#include "absl/strings/str_join.h"
2932
#include "absl/strings/str_split.h"
3033
#include "absl/strings/string_view.h"
34+
#include "absl/time/clock.h"
35+
#include "absl/time/time.h"
3136
#include "absl/types/optional.h"
3237
#include "ortools/base/cleanup.h"
38+
#include "ortools/base/logging.h"
3339
#include "ortools/base/status_macros.h"
3440
#include "ortools/base/timer.h"
3541
#include "ortools/gurobi/environment.h"

ortools/linear_solver/samples/AssignmentMb.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void Main()
3030
int numTasks = costs.GetLength(1);
3131
// [END data_model]
3232

33-
// [START model]
33+
// [START model]
3434
ModelBuilder model = new ModelBuilder();
3535
// [END model]
3636

@@ -58,7 +58,7 @@ static void Main()
5858
{
5959
assignedWork.Add(x[i, j]);
6060
}
61-
model.Add(assignedWork <= 1);
61+
model.Add(assignedWork <= 1);
6262
}
6363

6464
// Each task is assigned to exactly one worker.
@@ -69,7 +69,7 @@ static void Main()
6969
{
7070
assignedWorker.Add(x[i, j]);
7171
}
72-
model.Add(assignedWorker == 1);
72+
model.Add(assignedWorker == 1);
7373
}
7474
// [END constraints]
7575

@@ -89,9 +89,10 @@ static void Main()
8989
// [START solver]
9090
// Create the solver with the SCIP backend and check it is supported.
9191
ModelSolver solver = new ModelSolver("SCIP");
92-
if (!solver.SolverIsSupported()) return;
92+
if (!solver.SolverIsSupported())
93+
return;
9394
// [END solver]
94-
95+
9596
// Solve
9697
// [START solve]
9798
SolveStatus resultStatus = solver.Solve(model);

ortools/linear_solver/samples/AssignmentMb.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313

1414
// [START program]
1515
package com.google.ortools.linearsolver.samples;
16+
1617
// [START import]
1718
import com.google.ortools.Loader;
18-
import com.google.ortools.modelbuilder.LinearConstraint;
1919
import com.google.ortools.modelbuilder.LinearExpr;
2020
import com.google.ortools.modelbuilder.LinearExprBuilder;
2121
import com.google.ortools.modelbuilder.ModelBuilder;
2222
import com.google.ortools.modelbuilder.ModelSolver;
2323
import com.google.ortools.modelbuilder.SolveStatus;
2424
import com.google.ortools.modelbuilder.Variable;
25+
2526
// [END import]
2627

2728
/** MIP example that solves an assignment problem. */
2829
public class AssignmentMb {
2930
public static void main(String[] args) {
3031
Loader.loadNativeLibraries();
32+
3133
// Data
3234
// [START data_model]
3335
double[][] costs = {
@@ -92,7 +94,9 @@ public static void main(String[] args) {
9294
// [START solver]
9395
// Create the solver with the SCIP backend and check it is supported.
9496
ModelSolver solver = new ModelSolver("scip");
95-
if (!solver.solverIsSupported()) return;
97+
if (!solver.solverIsSupported()) {
98+
return;
99+
}
96100
// [END solver]
97101

98102
// [START solve]

ortools/linear_solver/samples/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ code_sample_cc(name = "stigler_diet")
3636

3737
# Model Builder
3838

39+
code_sample_java(name = "AssignmentMb")
40+
41+
code_sample_java(name = "BinPackingMb")
42+
3943
code_sample_java(name = "CloneModelMb")
4044

4145
code_sample_java(name = "SimpleLpProgramMb")

ortools/linear_solver/samples/BinPackingMb.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ public static void Main()
8686
// [START solver]
8787
// Create the solver with the SCIP backend and check it is supported.
8888
ModelSolver solver = new ModelSolver("SCIP");
89-
if (!solver.SolverIsSupported()) return;
89+
if (!solver.SolverIsSupported())
90+
return;
9091
// [END solver]
91-
92+
9293
// [START solve]
9394
SolveStatus resultStatus = solver.Solve(model);
9495
// [END solve]

ortools/linear_solver/samples/BinPackingMb.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
// MIP example that solves a bin packing problem.
1515
// [START program]
1616
package com.google.ortools.linearsolver.samples;
17+
1718
// [START import]
1819
import com.google.ortools.Loader;
19-
import com.google.ortools.modelbuilder.LinearConstraint;
2020
import com.google.ortools.modelbuilder.LinearExpr;
2121
import com.google.ortools.modelbuilder.LinearExprBuilder;
2222
import com.google.ortools.modelbuilder.ModelBuilder;
2323
import com.google.ortools.modelbuilder.ModelSolver;
2424
import com.google.ortools.modelbuilder.SolveStatus;
2525
import com.google.ortools.modelbuilder.Variable;
26+
2627
// [END import]
2728

2829
/** Bin packing problem. */
@@ -35,6 +36,7 @@ static class DataModel {
3536
public final int numBins = weights.length;
3637
public final int binCapacity = 100;
3738
}
39+
3840
// [END data_model]
3941

4042
public static void main(String[] args) throws Exception {
@@ -71,7 +73,7 @@ public static void main(String[] args) throws Exception {
7173
model.addEquality(oneCopy, 1);
7274
}
7375

74-
// The bin capacity contraint for bin j is
76+
// The bin capacity constraint for bin j is
7577
// sum_i w_i x_ij <= C*y_j
7678
// To define this constraint, first subtract the left side from the right to get
7779
// 0 <= C*y_j - sum_i w_i x_ij
@@ -95,7 +97,9 @@ public static void main(String[] args) throws Exception {
9597
// [START solver]
9698
// Create the solver with the SCIP backend and check it is supported.
9799
ModelSolver solver = new ModelSolver("scip");
98-
if (!solver.solverIsSupported()) return;
100+
if (!solver.solverIsSupported()) {
101+
return;
102+
}
99103
// [END solver]
100104

101105
// [START solve]

0 commit comments

Comments
 (0)