Files
ortools-clone/ortools/algorithms/python/knapsack_solver.cc

89 lines
4.2 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 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
//
// 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.
// A pybind11 wrapper for knapsack_solver.
#include "ortools/algorithms/knapsack_solver.h"
#include <string>
#include "ortools/algorithms/python/knapsack_solver_doc.h"
#include "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "pybind11/stl.h"
using ::operations_research::KnapsackSolver;
namespace py = pybind11;
using ::py::arg;
PYBIND11_MODULE(knapsack_solver, m) {
2023-07-02 08:11:12 +02:00
py::class_<KnapsackSolver>(m, "KnapsackSolver",
DOC(operations_research, KnapsackSolver))
.def(py::init<KnapsackSolver::SolverType, const std::string&>())
2023-07-02 08:11:12 +02:00
.def("init", &KnapsackSolver::Init,
DOC(operations_research, KnapsackSolver, Init), arg("profits"),
arg("weights"), arg("capacities"))
.def("solve", &KnapsackSolver::Solve,
DOC(operations_research, KnapsackSolver, Solve))
2023-07-01 06:08:49 +02:00
.def("best_solution_contains", &KnapsackSolver::BestSolutionContains,
2023-07-02 08:11:12 +02:00
DOC(operations_research, KnapsackSolver, BestSolutionContains),
arg("item_id"))
2023-07-02 08:11:12 +02:00
.def("is_solution_optimal", &KnapsackSolver::IsSolutionOptimal,
DOC(operations_research, KnapsackSolver, IsSolutionOptimal))
.def("set_time_limit", &KnapsackSolver::set_time_limit,
2023-07-02 08:11:12 +02:00
DOC(operations_research, KnapsackSolver, set_time_limit),
arg("time_limit_seconds"))
.def("set_use_reduction", &KnapsackSolver::set_use_reduction,
2023-07-02 08:11:12 +02:00
DOC(operations_research, KnapsackSolver, set_use_reduction),
arg("use_reduction"));
2023-07-02 08:11:12 +02:00
py::enum_<KnapsackSolver::SolverType>(
m, "SolverType", DOC(operations_research, KnapsackSolver, SolverType))
.value("KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER",
KnapsackSolver::SolverType::
2023-07-02 08:11:12 +02:00
KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER))
.value("KNAPSACK_BRUTE_FORCE_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_BRUTE_FORCE_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_BRUTE_FORCE_SOLVER))
.value("KNAPSACK_64ITEMS_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_64ITEMS_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER))
.value("KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER))
.value("KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER))
.value(
"KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER))
.value("KNAPSACK_DIVIDE_AND_CONQUER_SOLVER",
2023-07-02 08:11:12 +02:00
KnapsackSolver::SolverType::KNAPSACK_DIVIDE_AND_CONQUER_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_DIVIDE_AND_CONQUER_SOLVER))
2024-01-29 15:10:26 +01:00
.value("KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER",
KnapsackSolver::SolverType::KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER,
DOC(operations_research, KnapsackSolver, SolverType,
KNAPSACK_MULTIDIMENSION_CP_SAT_SOLVER))
.export_values();
}