diff --git a/examples/cpp/linear_programming.cc b/examples/cpp/linear_programming.cc index ee34750904..9ada619906 100644 --- a/examples/cpp/linear_programming.cc +++ b/examples/cpp/linear_programming.cc @@ -25,7 +25,7 @@ namespace operations_research { MPVariable* const x = solver.MakeNumVar(0.0, infinity, "x"); MPVariable* const y = solver.MakeNumVar(0.0, infinity, "y"); - // Objectif function: Maximize 3x + 4y). + // Objectif function: Maximize 3x + 4y. MPObjective* const objective = solver.MutableObjective(); objective->SetCoefficient(x, 3); objective->SetCoefficient(y, 4); diff --git a/examples/python/linear_programming.py b/examples/python/linear_programming.py index 44c8ea1f50..8ab0b67af1 100644 --- a/examples/python/linear_programming.py +++ b/examples/python/linear_programming.py @@ -1,4 +1,6 @@ -# Copyright 2010-2017 Google +#!/usr/bin/env python +# This Python file uses the following encoding: utf-8 +# Copyright 2018 Google LLC # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -10,122 +12,68 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Linear programming examples that show how to use the APIs.""" +"""Linear optimization example""" + from __future__ import print_function -from ortools.linear_solver import linear_solver_pb2 from ortools.linear_solver import pywraplp +def main(): + """Entry point of the program""" + # Instantiate a Glop solver, naming it LinearExample. + solver = pywraplp.Solver('LinearExample', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) -def RunLinearExampleNaturalLanguageAPI(optimization_problem_type): - """Example of simple linear program with natural language API.""" - solver = pywraplp.Solver('RunLinearExampleNaturalLanguageAPI', - optimization_problem_type) - infinity = solver.infinity() - # x1, x2 and x3 are continuous non-negative variables. - x1 = solver.NumVar(0.0, infinity, 'x1') - x2 = solver.NumVar(0.0, infinity, 'x2') - x3 = solver.NumVar(0.0, infinity, 'x3') + # Create the two variables and let them take on any value. + x = solver.NumVar(-solver.infinity(), solver.infinity(), 'x') + y = solver.NumVar(-solver.infinity(), solver.infinity(), 'y') - solver.Maximize(10 * x1 + 6 * x2 + 4 * x3) - c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, 'ConstraintName0') - c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300) - sum_of_vars = sum([x1, x2, x3]) - c2 = solver.Add(sum_of_vars <= 100.0, 'OtherConstraintName') - - SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2]) - # Print a linear expression's solution value. - print(('Sum of vars: %s = %s' % (sum_of_vars, sum_of_vars.solution_value()))) - - -def RunLinearExampleCppStyleAPI(optimization_problem_type): - """Example of simple linear program with the C++ style API.""" - solver = pywraplp.Solver('RunLinearExampleCppStyle', - optimization_problem_type) - infinity = solver.infinity() - # x1, x2 and x3 are continuous non-negative variables. - x1 = solver.NumVar(0.0, infinity, 'x1') - x2 = solver.NumVar(0.0, infinity, 'x2') - x3 = solver.NumVar(0.0, infinity, 'x3') - - # Maximize 10 * x1 + 6 * x2 + 4 * x3. + # Objective function: Maximize 3x + 4y. objective = solver.Objective() - objective.SetCoefficient(x1, 10) - objective.SetCoefficient(x2, 6) - objective.SetCoefficient(x3, 4) + objective.SetCoefficient(x, 3) + objective.SetCoefficient(y, 4) objective.SetMaximization() - # x1 + x2 + x3 <= 100. - c0 = solver.Constraint(-infinity, 100.0, 'c0') - c0.SetCoefficient(x1, 1) - c0.SetCoefficient(x2, 1) - c0.SetCoefficient(x3, 1) + # Constraint 0: x + 2y <= 14. + constraint0 = solver.Constraint(-solver.infinity(), 14) + constraint0.SetCoefficient(x, 1) + constraint0.SetCoefficient(y, 2) - # 10 * x1 + 4 * x2 + 5 * x3 <= 600. - c1 = solver.Constraint(-infinity, 600.0, 'c1') - c1.SetCoefficient(x1, 10) - c1.SetCoefficient(x2, 4) - c1.SetCoefficient(x3, 5) + # Constraint 1: 3x - y >= 0. + constraint1 = solver.Constraint(0, solver.infinity()) + constraint1.SetCoefficient(x, 3) + constraint1.SetCoefficient(y, -1) - # 2 * x1 + 2 * x2 + 6 * x3 <= 300. - c2 = solver.Constraint(-infinity, 300.0, 'c2') - c2.SetCoefficient(x1, 2) - c2.SetCoefficient(x2, 2) - c2.SetCoefficient(x3, 6) + # Constraint 2: x - y <= 2. + constraint2 = solver.Constraint(-solver.infinity(), 2) + constraint2.SetCoefficient(x, 1) + constraint2.SetCoefficient(y, -1) - SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2]) + print('Number of variables =', solver.NumVariables()) + print('Number of constraints =', solver.NumConstraints()) + # Solve the system. + status = solver.Solve() + # Check that the problem has an optimal solution. + if status != pywraplp.Solver.OPTIMAL: + print("The problem does not have an optimal solution!") + exit(1) -def SolveAndPrint(solver, variable_list, constraint_list): - """Solve the problem and print the solution.""" - print(('Number of variables = %d' % solver.NumVariables())) - print(('Number of constraints = %d' % solver.NumConstraints())) - - result_status = solver.Solve() - - # The problem has an optimal solution. - assert result_status == pywraplp.Solver.OPTIMAL - - # The solution looks legit (when using solvers others than - # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!). - assert solver.VerifySolution(1e-7, True) - - print(('Problem solved in %f milliseconds' % solver.wall_time())) - - # The objective value of the solution. - print(('Optimal objective value = %f' % solver.Objective().Value())) - - # The value of each variable in the solution. - for variable in variable_list: - print(('%s = %f' % (variable.name(), variable.solution_value()))) - + print('Solution:') + print('x =', x.solution_value()) + print('y =', y.solution_value()) + print('Optimal objective value =', objective.Value()) + print('') print('Advanced usage:') - print(('Problem solved in %d iterations' % solver.iterations())) - for variable in variable_list: - print( - ('%s: reduced cost = %f' % (variable.name(), variable.reduced_cost()))) + print('Problem solved in ', solver.wall_time(), ' milliseconds') + print('Problem solved in ', solver.iterations(), ' iterations') + print('x: reduced cost =', x.reduced_cost()) + print('y: reduced cost =', y.reduced_cost()) activities = solver.ComputeConstraintActivities() - for i, constraint in enumerate(constraint_list): - print(('constraint %d: dual value = %f\n' - ' activity = %f' % (i, constraint.dual_value(), - activities[constraint.index()]))) - - -def main(): - all_names_and_problem_types = ( - list(linear_solver_pb2.MPModelRequest.SolverType.items())) - for name, problem_type in all_names_and_problem_types: - # Skip non-LP problem types. - if not name.endswith('LINEAR_PROGRAMMING'): - continue - # Skip problem types that aren't supported by the current binary. - if not pywraplp.Solver.SupportsProblemType(problem_type): - continue - print(('\n------ Linear programming example with %s ------' % name)) - print('\n*** Natural language API ***') - RunLinearExampleNaturalLanguageAPI(problem_type) - print('\n*** C++ style API ***') - RunLinearExampleCppStyleAPI(problem_type) - + print('constraint0: dual value =', constraint0.dual_value(), + ' activities =', activities[constraint0.index()]) + print('constraint1: dual value =', constraint1.dual_value(), + ' activities =', activities[constraint1.index()]) + print('constraint2: dual value =', constraint2.dual_value(), + ' activities =', activities[constraint2.index()]) if __name__ == '__main__': main() diff --git a/examples/tests/lp_test.py b/examples/tests/lp_test.py new file mode 100644 index 0000000000..44c8ea1f50 --- /dev/null +++ b/examples/tests/lp_test.py @@ -0,0 +1,131 @@ +# Copyright 2010-2017 Google +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Linear programming examples that show how to use the APIs.""" +from __future__ import print_function +from ortools.linear_solver import linear_solver_pb2 +from ortools.linear_solver import pywraplp + + +def RunLinearExampleNaturalLanguageAPI(optimization_problem_type): + """Example of simple linear program with natural language API.""" + solver = pywraplp.Solver('RunLinearExampleNaturalLanguageAPI', + optimization_problem_type) + infinity = solver.infinity() + # x1, x2 and x3 are continuous non-negative variables. + x1 = solver.NumVar(0.0, infinity, 'x1') + x2 = solver.NumVar(0.0, infinity, 'x2') + x3 = solver.NumVar(0.0, infinity, 'x3') + + solver.Maximize(10 * x1 + 6 * x2 + 4 * x3) + c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, 'ConstraintName0') + c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300) + sum_of_vars = sum([x1, x2, x3]) + c2 = solver.Add(sum_of_vars <= 100.0, 'OtherConstraintName') + + SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2]) + # Print a linear expression's solution value. + print(('Sum of vars: %s = %s' % (sum_of_vars, sum_of_vars.solution_value()))) + + +def RunLinearExampleCppStyleAPI(optimization_problem_type): + """Example of simple linear program with the C++ style API.""" + solver = pywraplp.Solver('RunLinearExampleCppStyle', + optimization_problem_type) + infinity = solver.infinity() + # x1, x2 and x3 are continuous non-negative variables. + x1 = solver.NumVar(0.0, infinity, 'x1') + x2 = solver.NumVar(0.0, infinity, 'x2') + x3 = solver.NumVar(0.0, infinity, 'x3') + + # Maximize 10 * x1 + 6 * x2 + 4 * x3. + objective = solver.Objective() + objective.SetCoefficient(x1, 10) + objective.SetCoefficient(x2, 6) + objective.SetCoefficient(x3, 4) + objective.SetMaximization() + + # x1 + x2 + x3 <= 100. + c0 = solver.Constraint(-infinity, 100.0, 'c0') + c0.SetCoefficient(x1, 1) + c0.SetCoefficient(x2, 1) + c0.SetCoefficient(x3, 1) + + # 10 * x1 + 4 * x2 + 5 * x3 <= 600. + c1 = solver.Constraint(-infinity, 600.0, 'c1') + c1.SetCoefficient(x1, 10) + c1.SetCoefficient(x2, 4) + c1.SetCoefficient(x3, 5) + + # 2 * x1 + 2 * x2 + 6 * x3 <= 300. + c2 = solver.Constraint(-infinity, 300.0, 'c2') + c2.SetCoefficient(x1, 2) + c2.SetCoefficient(x2, 2) + c2.SetCoefficient(x3, 6) + + SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2]) + + +def SolveAndPrint(solver, variable_list, constraint_list): + """Solve the problem and print the solution.""" + print(('Number of variables = %d' % solver.NumVariables())) + print(('Number of constraints = %d' % solver.NumConstraints())) + + result_status = solver.Solve() + + # The problem has an optimal solution. + assert result_status == pywraplp.Solver.OPTIMAL + + # The solution looks legit (when using solvers others than + # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!). + assert solver.VerifySolution(1e-7, True) + + print(('Problem solved in %f milliseconds' % solver.wall_time())) + + # The objective value of the solution. + print(('Optimal objective value = %f' % solver.Objective().Value())) + + # The value of each variable in the solution. + for variable in variable_list: + print(('%s = %f' % (variable.name(), variable.solution_value()))) + + print('Advanced usage:') + print(('Problem solved in %d iterations' % solver.iterations())) + for variable in variable_list: + print( + ('%s: reduced cost = %f' % (variable.name(), variable.reduced_cost()))) + activities = solver.ComputeConstraintActivities() + for i, constraint in enumerate(constraint_list): + print(('constraint %d: dual value = %f\n' + ' activity = %f' % (i, constraint.dual_value(), + activities[constraint.index()]))) + + +def main(): + all_names_and_problem_types = ( + list(linear_solver_pb2.MPModelRequest.SolverType.items())) + for name, problem_type in all_names_and_problem_types: + # Skip non-LP problem types. + if not name.endswith('LINEAR_PROGRAMMING'): + continue + # Skip problem types that aren't supported by the current binary. + if not pywraplp.Solver.SupportsProblemType(problem_type): + continue + print(('\n------ Linear programming example with %s ------' % name)) + print('\n*** Natural language API ***') + RunLinearExampleNaturalLanguageAPI(problem_type) + print('\n*** C++ style API ***') + RunLinearExampleCppStyleAPI(problem_type) + + +if __name__ == '__main__': + main()