diff --git a/python/integer_programming.py b/python/integer_programming.py index c598a736e1..49fe34b169 100644 --- a/python/integer_programming.py +++ b/python/integer_programming.py @@ -21,9 +21,9 @@ from google.apputils import app from linear_solver import pywraplp -def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type): +def RunIntegerExampleAlgebraicAPI(optimization_problem_type): """Example of simple integer program with natural language API.""" - solver = pywraplp.Solver('RunIntegerExampleNaturalLanguageAPI', + solver = pywraplp.Solver('RunIntegerExampleAlgebraicAPI', optimization_problem_type) infinity = solver.infinity() # x1 and x2 are integer non-negative variables. @@ -80,29 +80,35 @@ def SolveAndPrint(solver, variable_list): print 'Problem solved in %d branch-and-bound nodes' % solver.nodes() -def RunAllIntegerExampleNaturalLanguageAPI(): - print '---- Integer programming example with GLPK (natural language API) ----' - RunIntegerExampleNaturalLanguageAPI( - pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - print '---- Integer programming example with CBC (natural language API) ----' - RunIntegerExampleNaturalLanguageAPI( - pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) - print '---- Integer programming example with SCIP (natural language API) ----' - RunIntegerExampleNaturalLanguageAPI( - pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) +def RunAllIntegerExampleAlgebraicAPI(): + if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'): + print ('---- Integer programming example with GLPK (algebraic API) -----') + RunIntegerExampleAlgebraicAPI( + pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) + if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'): + print '---- Integer programming example with CBC (algebraic API) -----' + RunIntegerExampleAlgebraicAPI(pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'): + print '---- Integer programming example with SCIP (algebraic API) -----' + RunIntegerExampleAlgebraicAPI( + pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) def RunAllIntegerExampleCppStyleAPI(): - print '---- Integer programming example with GLPK (C++ style API) ----' - RunIntegerExampleCppStyleAPI(pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) - print '---- Integer programming example with CBC (C++ style API) ----' - RunIntegerExampleCppStyleAPI(pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) - print '---- Integer programming example with SCIP (C++ style API) ----' - RunIntegerExampleCppStyleAPI(pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) + if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'): + print '---- Integer programming example with GLPK (C++ style API) ----' + RunIntegerExampleCppStyleAPI(pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING) + if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'): + print '---- Integer programming example with CBC (C++ style API) ----' + RunIntegerExampleCppStyleAPI(pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) + if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'): + print '---- Integer programming example with SCIP (C++ style API) ----' + RunIntegerExampleCppStyleAPI( + pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING) def main(unused_argv): - RunAllIntegerExampleNaturalLanguageAPI() + RunAllIntegerExampleAlgebraicAPI() RunAllIntegerExampleCppStyleAPI() diff --git a/python/linear_programming.py b/python/linear_programming.py index 1dc92643fa..f1a92e93f9 100644 --- a/python/linear_programming.py +++ b/python/linear_programming.py @@ -21,9 +21,9 @@ from google.apputils import app from linear_solver import pywraplp -def RunLinearExampleNaturalLanguageAPI(optimization_problem_type): +def RunLinearExampleAlgebraicAPI(optimization_problem_type): """Example of simple linear program with natural language API.""" - solver = pywraplp.Solver('RunLinearExampleNaturalLanguageAPI', + solver = pywraplp.Solver('RunLinearExampleAlgebraicAPI', optimization_problem_type) infinity = solver.infinity() # x1, x2 and x3 are continuous non-negative variables. @@ -107,22 +107,26 @@ def SolveAndPrint(solver, variable_list, constraint_list): (i, constraint.dual_value(), constraint.activity())) -def RunAllLinearExampleNaturalLanguageAPI(): - print '---- Linear programming example with GLPK (natural language API) ----' - RunLinearExampleNaturalLanguageAPI(pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) - print '---- Linear programming example with CLP (natural language API) ----' - RunLinearExampleNaturalLanguageAPI(pywraplp.Solver.CLP_LINEAR_PROGRAMMING) +def RunAllLinearExampleAlgebraicAPI(): + if hasattr(pywraplp.Solver, 'GLPK_LINEAR_PROGRAMMING'): + print '---- Linear programming example with GLPK (algebraic API) ----' + RunLinearExampleAlgebraicAPI(pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) + if hasattr(pywraplp.Solver, 'CLP_LINEAR_PROGRAMMING'): + print '---- Linear programming example with CLP (algebraic API) ----' + RunLinearExampleAlgebraicAPI(pywraplp.Solver.CLP_LINEAR_PROGRAMMING) def RunAllLinearExampleCppStyleAPI(): - print '---- Linear programming example with GLPK (C++ style API) ----' - RunLinearExampleCppStyleAPI(pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) - print '---- Linear programming example with CLP (C++ style API) ----' - RunLinearExampleCppStyleAPI(pywraplp.Solver.CLP_LINEAR_PROGRAMMING) + if hasattr(pywraplp.Solver, 'GLPK_LINEAR_PROGRAMMING'): + print '---- Linear programming example with GLPK (C++ style API) ----' + RunLinearExampleCppStyleAPI(pywraplp.Solver.GLPK_LINEAR_PROGRAMMING) + if hasattr(pywraplp.Solver, 'CLP_LINEAR_PROGRAMMING'): + print '---- Linear programming example with CLP (C++ style API) ----' + RunLinearExampleCppStyleAPI(pywraplp.Solver.CLP_LINEAR_PROGRAMMING) def main(unused_argv): - RunAllLinearExampleNaturalLanguageAPI() + RunAllLinearExampleAlgebraicAPI() RunAllLinearExampleCppStyleAPI()