Skip to content

Commit 55cedb4

Browse files
committed
remove name from MPSolver.CreateSolver API; simplify underlying code
1 parent c12c999 commit 55cedb4

43 files changed

Lines changed: 135 additions & 172 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/cpp/integer_programming.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
#include "ortools/linear_solver/linear_solver.h"
1919

2020
namespace operations_research {
21-
void RunIntegerProgrammingExample(
22-
const std::string& optimization_problem_type) {
23-
LOG(INFO) << "---- Integer programming example with "
24-
<< optimization_problem_type << " ----";
21+
void RunIntegerProgrammingExample(const std::string& solver_id) {
22+
LOG(INFO) << "---- Integer programming example with " << solver_id << " ----";
2523

26-
if (!MPSolver::ParseAndCheckSupportForProblemType(
27-
optimization_problem_type)) {
28-
LOG(INFO) << " support for solver not linked in.";
24+
MPSolver::OptimizationProblemType problem_type;
25+
if (!MPSolver::ParseSolverType(solver_id, &problem_type)) {
26+
LOG(INFO) << "Solver id " << solver_id << " not recognized";
2927
return;
3028
}
3129

32-
MPSolver solver("IntegerProgrammingExample",
33-
MPSolver::ParseSolverTypeOrDie(optimization_problem_type));
30+
if (!MPSolver::SupportsProblemType(problem_type)) {
31+
LOG(INFO) << "Supports for solver " << solver_id << " not linked in.";
32+
return;
33+
}
34+
35+
MPSolver solver("IntegerProgrammingExample", problem_type);
3436

3537
const double infinity = solver.infinity();
3638
// x and y are integer non-negative variables.
@@ -80,7 +82,6 @@ void RunAllExamples() {
8082
RunIntegerProgrammingExample("GUROBI");
8183
RunIntegerProgrammingExample("GLPK");
8284
RunIntegerProgrammingExample("CPLEX");
83-
RunIntegerProgrammingExample("XPRESS");
8485
}
8586
} // namespace operations_research
8687

examples/cpp/linear_programming.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
#include "ortools/linear_solver/linear_solver.pb.h"
2020

2121
namespace operations_research {
22-
void RunLinearProgrammingExample(const std::string& optimization_problem_type) {
23-
LOG(INFO) << "---- Linear programming example with "
24-
<< optimization_problem_type << " ----";
25-
if (!MPSolver::ParseAndCheckSupportForProblemType(
26-
optimization_problem_type)) {
27-
LOG(INFO) << " support for solver not linked in.";
22+
void RunLinearProgrammingExample(const std::string& solver_id) {
23+
LOG(INFO) << "---- Linear programming example with " << solver_id << " ----";
24+
MPSolver::OptimizationProblemType problem_type;
25+
if (!MPSolver::ParseSolverType(solver_id, &problem_type)) {
26+
LOG(INFO) << "Solver id " << solver_id << " not recognized";
2827
return;
2928
}
3029

31-
MPSolver solver("IntegerProgrammingExample",
32-
MPSolver::ParseSolverTypeOrDie(optimization_problem_type));
30+
if (!MPSolver::SupportsProblemType(problem_type)) {
31+
LOG(INFO) << "Supports for solver " << solver_id << " not linked in.";
32+
return;
33+
}
34+
35+
MPSolver solver("IntegerProgrammingExample", problem_type);
3336

3437
const double infinity = solver.infinity();
3538
// x1, x2 and x3 are continuous non-negative variables.

examples/dotnet/csintegerprogramming.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class CsIntegerProgramming
1818
{
1919
private static void RunIntegerProgrammingExample(String solverType)
2020
{
21-
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
21+
Solver solver = Solver.CreateSolver(solverType);
2222
if (solver == null)
2323
{
2424
Console.WriteLine("Could not create solver " + solverType);
@@ -65,7 +65,7 @@ private static void RunIntegerProgrammingExample(String solverType)
6565

6666
private static void RunIntegerProgrammingExampleNaturalApi(String solverType)
6767
{
68-
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
68+
Solver solver = Solver.CreateSolver(solverType);
6969
if (solver == null)
7070
{
7171
Console.WriteLine("Could not create solver " + solverType);
@@ -113,6 +113,8 @@ static void Main()
113113
RunIntegerProgrammingExample("SCIP");
114114
Console.WriteLine("---- Linear programming example with SAT ----");
115115
RunIntegerProgrammingExample("SAT");
116+
Console.WriteLine("---- Linear programming example with GUROBI ----");
117+
RunIntegerProgrammingExample("GUROBI");
116118
Console.WriteLine(
117119
"---- Integer programming example (Natural API) with GLPK ----");
118120
RunIntegerProgrammingExampleNaturalApi("GLPK");
@@ -125,5 +127,8 @@ static void Main()
125127
Console.WriteLine(
126128
"---- Linear programming example (Natural API) with SAT ----");
127129
RunIntegerProgrammingExampleNaturalApi("SAT");
130+
Console.WriteLine(
131+
"---- Linear programming example (Natural API) with GUROBI ----");
132+
RunIntegerProgrammingExampleNaturalApi("GUROBI");
128133
}
129134
}

examples/dotnet/cslinearprogramming.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static void RunLinearProgrammingExample(String solverType)
2020
{
2121
Console.WriteLine($"---- Linear programming example with {solverType} ----");
2222

23-
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
23+
Solver solver = Solver.CreateSolver(solverType);
2424
if (solver == null)
2525
{
2626
Console.WriteLine("Could not create solver " + solverType);
@@ -101,7 +101,7 @@ private static void RunLinearProgrammingExampleNaturalApi(
101101
Console.WriteLine(
102102
$"---- Linear programming example (Natural API) with {solverType} ----");
103103

104-
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
104+
Solver solver = Solver.CreateSolver(solverType);
105105
if (solver == null)
106106
{
107107
Console.WriteLine("Could not create solver " + solverType);

examples/java/IntegerProgramming.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class IntegerProgramming {
2929
}
3030

3131
private static void runIntegerProgrammingExample(String solverType) {
32-
MPSolver solver = MPSolver.createSolver("IntegerProgramming", solverType);
32+
MPSolver solver = MPSolver.createSolver(solverType);
3333
if (solver == null) {
3434
System.out.println("Could not create solver " + solverType);
3535
return;

examples/java/LinearProgramming.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class LinearProgramming {
2929
}
3030

3131
private static void runLinearProgrammingExample(String solverType, boolean printModel) {
32-
MPSolver solver = MPSolver.createSolver("IntegerProgramming", solverType);
32+
MPSolver solver = MPSolver.createSolver(solverType);
3333
if (solver == null) {
3434
System.out.println("Could not create solver " + solverType);
3535
return;

examples/python/integer_programming.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def Announce(solver, api_type):
2525
def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type):
2626
"""Example of simple integer program with natural language API."""
2727

28-
solver = pywraplp.Solver.CreateSolver('RunIntegerExampleNaturalLanguageAPI',
29-
optimization_problem_type)
28+
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
3029
if not solver:
3130
return
3231

@@ -45,8 +44,7 @@ def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type):
4544

4645
def RunIntegerExampleCppStyleAPI(optimization_problem_type):
4746
"""Example of simple integer program with the C++ style API."""
48-
solver = pywraplp.Solver.CreateSolver('RunIntegerExampleCppStyleAPI',
49-
optimization_problem_type)
47+
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
5048
if not solver:
5149
return
5250

examples/python/linear_programming.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def Announce(solver, api_type):
2424

2525
def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):
2626
"""Example of simple linear program with natural language API."""
27-
solver = pywraplp.Solver.CreateSolver('RunLinearExampleNaturalLanguageAPI',
28-
optimization_problem_type)
27+
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
2928

3029
if not solver:
3130
return
@@ -51,8 +50,7 @@ def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):
5150

5251
def RunLinearExampleCppStyleAPI(optimization_problem_type):
5352
"""Example of simple linear program with the C++ style API."""
54-
solver = pywraplp.Solver.CreateSolver('RunLinearExampleCppStyle',
55-
optimization_problem_type)
53+
solver = pywraplp.Solver.CreateSolver(optimization_problem_type)
5654
if not solver:
5755
return
5856

examples/tests/lp_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ def testBopInfeasible(self):
228228
result_status = solver.Solve()
229229
print(result_status) # outputs: 0
230230

231+
def testSolveFromProto(self):
232+
solver = pywraplp.Solver('', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
233+
solver.LoadSolutionFromProto(linear_solver_pb2.MPSolutionResponse())
234+
231235

232236
if __name__ == '__main__':
233237
unittest.main()

ortools/linear_solver/csharp/SolverHelper.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,6 @@ public Variable[] MakeBoolVarArray(int count, string var_name) {
180180
return matrix;
181181
}
182182

183-
public static int GetSolverEnum(String solverType) {
184-
System.Reflection.FieldInfo fieldInfo =
185-
typeof(Solver).GetField(solverType);
186-
if (fieldInfo != null) {
187-
return (int)fieldInfo.GetValue(null);
188-
} else {
189-
throw new System.ApplicationException("Solver not supported");
190-
}
191-
}
192-
193183
public Constraint Add(LinearConstraint constraint) {
194184
return constraint.Extract(this);
195185
}

0 commit comments

Comments
 (0)