diff --git a/examples/python/labeled_dice.py b/examples/python/labeled_dice.py index add271afba..145f43d221 100644 --- a/examples/python/labeled_dice.py +++ b/examples/python/labeled_dice.py @@ -47,7 +47,7 @@ Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/ """ - +from __future__ import print_function from ortools.constraint_solver import pywrapcp @@ -62,7 +62,7 @@ def main(): n = 4 m = 24 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, Y = ( - range(m)) + list(range(m))) letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y"] @@ -120,27 +120,27 @@ def main(): num_solutions += 1 # print "dice:", [(letters[i],dice[i].Value()) for i in range(m)] for d in range(n): - print "die %i:" % d, + print("die %i:" % d, end=' ') for i in range(m): if dice[i].Value() == d: - print letters[i], - print + print(letters[i], end=' ') + print() - print "The words with the cube label:" + print("The words with the cube label:") for i in range(num_words): for j in range(n): - print "%s (%i)" % (letters[words[i][j]], dice[words[i][j]].Value()), - print + print("%s (%i)" % (letters[words[i][j]], dice[words[i][j]].Value()), end=' ') + print() - print + print() solver.EndSearch() - print - print "num_solutions:", num_solutions - print "failures:", solver.Failures() - print "branches:", solver.Branches() - print "WallTime:", solver.WallTime() + print() + print("num_solutions:", num_solutions) + print("failures:", solver.Failures()) + print("branches:", solver.Branches()) + print("WallTime:", solver.WallTime()) if __name__ == "__main__": main() diff --git a/examples/python/langford.py b/examples/python/langford.py index e8533660ef..115ca56fc7 100644 --- a/examples/python/langford.py +++ b/examples/python/langford.py @@ -44,7 +44,7 @@ Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/ """ - +from __future__ import print_function import sys from ortools.constraint_solver import pywrapcp @@ -58,8 +58,8 @@ def main(k=8, num_sol=0): # # data # - print "k:", k - p = range(2 * k) + print("k:", k) + p = list(range(2 * k)) # # declare variables @@ -90,18 +90,18 @@ def main(k=8, num_sol=0): solver.NewSearch(db) num_solutions = 0 while solver.NextSolution(): - print "solution:", ",".join([str(solution[i].Value()) for i in p]) + print("solution:", ",".join([str(solution[i].Value()) for i in p])) num_solutions += 1 if num_sol > 0 and num_solutions >= num_sol: break solver.EndSearch() - print - print "num_solutions:", num_solutions - print "failures:", solver.Failures() - print "branches:", solver.Branches() - print "WallTime:", solver.WallTime() + print() + print("num_solutions:", num_solutions) + print("failures:", solver.Failures()) + print("branches:", solver.Branches()) + print("WallTime:", solver.WallTime()) k = 8 num_sol = 0 diff --git a/examples/python/least_diff.py b/examples/python/least_diff.py index f48c47e657..b689cba70c 100644 --- a/examples/python/least_diff.py +++ b/examples/python/least_diff.py @@ -37,7 +37,7 @@ Also see my other Google CP Solver models: http://www.hakank.org/google_cp_solver/ """ - +from __future__ import print_function from ortools.constraint_solver import pywrapcp @@ -48,7 +48,7 @@ def main(unused_argv): # # declare variables # - digits = range(0, 10) + digits = list(range(0, 10)) a = solver.IntVar(digits, "a") b = solver.IntVar(digits, "b") c = solver.IntVar(digits, "c") @@ -63,9 +63,9 @@ def main(unused_argv): letters = [a, b, c, d, e, f, g, h, i, j] - x = solver.IntVar(range(0, 99999), "x") - y = solver.IntVar(range(0, 99999), "y") - diff = solver.IntVar(range(0, 99999), "y") + x = solver.IntVar(list(range(0, 99999)), "x") + y = solver.IntVar(list(range(0, 99999)), "y") + diff = solver.IntVar(list(range(0, 99999)), "y") # # constraints @@ -103,16 +103,16 @@ def main(unused_argv): xval = collector.Value(0, x) yval = collector.Value(0, y) diffval = collector.Value(0, diff) - print "x:", xval - print "y:", yval - print "diff:", diffval - print xval, "-", yval, "=", diffval - print [("abcdefghij"[i], collector.Value(0, letters[i])) for i in range(10)] - print - print "failures:", solver.Failures() - print "branches:", solver.Branches() - print "WallTime:", solver.WallTime() - print + print("x:", xval) + print("y:", yval) + print("diff:", diffval) + print(xval, "-", yval, "=", diffval) + print([("abcdefghij"[i], collector.Value(0, letters[i])) for i in range(10)]) + print() + print("failures:", solver.Failures()) + print("branches:", solver.Branches()) + print("WallTime:", solver.WallTime()) + print() if __name__ == "__main__": main("cp sample") diff --git a/examples/python/least_square.py b/examples/python/least_square.py index 2c8c54740d..8e867df766 100644 --- a/examples/python/least_square.py +++ b/examples/python/least_square.py @@ -25,7 +25,7 @@ Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/ """ - +from __future__ import print_function import sys from ortools.linear_solver import pywraplp @@ -83,11 +83,11 @@ def main(sol='GLPK'): solver.Solve() - print - print 'z = ', solver.Objective().Value() + print() + print('z = ', solver.Objective().Value()) for i in range(p + 1): - print a[i].SolutionValue(), - print + print(a[i].SolutionValue(), end=' ') + print() if __name__ == '__main__': @@ -96,7 +96,7 @@ if __name__ == '__main__': if len(sys.argv) > 1: sol = sys.argv[1] if sol != 'GLPK' and sol != 'CBC': - print 'Solver must be either GLPK or CBC' + print('Solver must be either GLPK or CBC') sys.exit(1) main(sol) diff --git a/examples/python/lectures.py b/examples/python/lectures.py index 252639d15b..86cf098d0b 100644 --- a/examples/python/lectures.py +++ b/examples/python/lectures.py @@ -44,7 +44,7 @@ Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/ """ - +from __future__ import print_function import sys from ortools.constraint_solver import pywrapcp @@ -119,14 +119,14 @@ def main(): num_solutions = 0 while solver.NextSolution(): num_solutions += 1 - print 'max_c:', max_c.Value() + 1, 'colors' - print 'v:', [v[i].Value() for i in range(n)] - print + print('max_c:', max_c.Value() + 1, 'colors') + print('v:', [v[i].Value() for i in range(n)]) + print() - print 'num_solutions:', num_solutions - print 'failures:', solver.Failures() - print 'branches:', solver.Branches() - print 'WallTime:', solver.WallTime(), 'ms' + print('num_solutions:', num_solutions) + print('failures:', solver.Failures()) + print('branches:', solver.Branches()) + print('WallTime:', solver.WallTime(), 'ms') if __name__ == '__main__': diff --git a/examples/python/linear_assignment_api.py b/examples/python/linear_assignment_api.py index f560b09a92..1407e14453 100644 --- a/examples/python/linear_assignment_api.py +++ b/examples/python/linear_assignment_api.py @@ -17,7 +17,7 @@ http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1] modified so the optimum solution is unique. """ - +from __future__ import print_function from ortools.graph import pywrapgraph @@ -41,20 +41,20 @@ def RunAssignmentOn4x4Matrix(): solve_status = assignment.Solve() if solve_status == assignment.OPTIMAL: - print 'Successful solve.' - print 'Total cost', assignment.OptimalCost(), '/', expected_cost + print('Successful solve.') + print('Total cost', assignment.OptimalCost(), '/', expected_cost) for i in range(0, assignment.NumNodes()): - print 'Left node %d assigned to right node %d with cost %d.' % ( + print('Left node %d assigned to right node %d with cost %d.' % ( i, assignment.RightMate(i), - assignment.AssignmentCost(i)) + assignment.AssignmentCost(i))) elif solve_status == assignment.INFEASIBLE: - print 'No perfect matching exists.' + print('No perfect matching exists.') elif solve_status == assignment.POSSIBLE_OVERFLOW: - print 'Some input costs are too large and may cause an integer overflow.' + print('Some input costs are too large and may cause an integer overflow.') -def main(unused_argv): +def main(): RunAssignmentOn4x4Matrix() if __name__ == '__main__': diff --git a/examples/python/linear_programming.py b/examples/python/linear_programming.py index 98f66d2396..20a1085ec3 100644 --- a/examples/python/linear_programming.py +++ b/examples/python/linear_programming.py @@ -12,8 +12,7 @@ # 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 @@ -36,7 +35,7 @@ def RunLinearExampleNaturalLanguageAPI(optimization_problem_type): 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())) + print(('Sum of vars: %s = %s' % (sum_of_vars, sum_of_vars.solution_value()))) def RunLinearExampleCppStyleAPI(optimization_problem_type): @@ -79,8 +78,8 @@ def RunLinearExampleCppStyleAPI(optimization_problem_type): 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()) + print(('Number of variables = %d' % solver.NumVariables())) + print(('Number of constraints = %d' % solver.NumConstraints())) result_status = solver.Solve() @@ -91,29 +90,29 @@ def SolveAndPrint(solver, variable_list, constraint_list): # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!). assert solver.VerifySolution(1e-7, True) - print('Problem solved in %f milliseconds' % solver.wall_time()) + print(('Problem solved in %f milliseconds' % solver.wall_time())) # The objective value of the solution. - print('Optimal objective value = %f' % solver.Objective().Value()) + 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(('%s = %f' % (variable.name(), variable.solution_value()))) print('Advanced usage:') - print('Problem solved in %d iterations' % solver.iterations()) + print(('Problem solved in %d iterations' % solver.iterations())) for variable in variable_list: - print('%s: reduced cost = %f' % (variable.name(), variable.reduced_cost())) + 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' + print(('constraint %d: dual value = %f\n' ' activity = %f' % - (i, constraint.dual_value(), activities[constraint.index()])) + (i, constraint.dual_value(), activities[constraint.index()]))) def main(): all_names_and_problem_types = ( - linear_solver_pb2.MPModelRequest.SolverType.items()) + 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'): @@ -121,7 +120,7 @@ def main(): # 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------ Linear programming example with %s ------' % name)) print('\n*** Natural language API ***') RunLinearExampleNaturalLanguageAPI(problem_type) print('\n*** C++ style API ***')