diff --git a/examples/knapsack/knapsack_mathopt_solver.py b/examples/knapsack/knapsack_mathopt_solver.py new file mode 100644 index 00000000..59564cb4 --- /dev/null +++ b/examples/knapsack/knapsack_mathopt_solver.py @@ -0,0 +1,32 @@ +# Copyright (c) 2024 AIRBUS and its affiliates. +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +"""Playing with the new Mathopt api from ortools""" + +from ortools.math_opt.python import mathopt + +from discrete_optimization.knapsack.knapsack_parser import ( + get_data_available, + parse_file, +) +from discrete_optimization.knapsack.solvers.lp_solvers import LPKnapsackMathOpt + + +def run_mathopt_knapsack(): + file = [f for f in get_data_available() if "ks_1000_0" in f][0] + knapsack_model = parse_file(file) + solver = LPKnapsackMathOpt(problem=knapsack_model) + solver.init_model() + res = solver.solve( + time_limit=30, + mathopt_enable_output=True, + mathopt_solver_type=mathopt.SolverType.GSCIP, + ) + print(solver.termination) + sol = res.get_best_solution_fit()[0] + print(sol) + + +if __name__ == "__main__": + run_mathopt_knapsack()