Skip to content

Commit

Permalink
adding basic example of mathopt for knapsack
Browse files Browse the repository at this point in the history
  • Loading branch information
g-poveda committed Sep 24, 2024
1 parent e8df030 commit 8c722c7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/knapsack/knapsack_mathopt_solver.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8c722c7

Please sign in to comment.