14#ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
15#define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
23#include "absl/memory/memory.h"
32class BaseKnapsackSolver;
174#if defined(USE_XPRESS)
180 KNAPSACK_MULTIDIMENSION_XPRESS_MIP_SOLVER = 7,
183#if defined(USE_CPLEX)
189 KNAPSACK_MULTIDIMENSION_CPLEX_MIP_SOLVER = 8,
208 void Init(
const std::vector<int64_t>& profits,
209 const std::vector<std::vector<int64_t> >& weights,
210 const std::vector<int64_t>& capacities);
236 time_limit_seconds_ = time_limit_seconds;
237 time_limit_ = absl::make_unique<TimeLimit>(time_limit_seconds_);
243 int ReduceCapacities(
int num_items,
244 const std::vector<std::vector<int64_t> >& weights,
245 const std::vector<int64_t>& capacities,
246 std::vector<std::vector<int64_t> >* reduced_weights,
247 std::vector<int64_t>* reduced_capacities);
248 int ReduceProblem(
int num_items);
249 void ComputeAdditionalProfit(
const std::vector<int64_t>& profits);
250 void InitReducedProblem(
const std::vector<int64_t>& profits,
251 const std::vector<std::vector<int64_t> >& weights,
252 const std::vector<int64_t>& capacities);
254 std::unique_ptr<BaseKnapsackSolver> solver_;
255 std::vector<bool> known_value_;
256 std::vector<bool> best_solution_;
257 bool is_solution_optimal_ =
false;
258 std::vector<int> mapping_reduced_item_id_;
259 bool is_problem_solved_;
260 int64_t additional_profit_;
262 double time_limit_seconds_;
263 std::unique_ptr<TimeLimit> time_limit_;
319 ?
static_cast<double>(
profit) /
static_cast<double>(
weight)
343 int depth()
const {
return depth_; }
367 int64_t current_profit_;
368 int64_t profit_upper_bound_;
419 void Init(
int number_of_items);
426 bool is_bound(
int id)
const {
return is_bound_.at(
id); }
427 bool is_in(
int id)
const {
return is_in_.at(
id); }
434 std::vector<bool> is_bound_;
435 std::vector<bool> is_in_;
454 void Init(
const std::vector<int64_t>& profits,
455 const std::vector<int64_t>& weights);
478 std::vector<bool>* solution)
const;
496 std::vector<bool>* solution)
const = 0;
499 const std::vector<KnapsackItemPtr>&
items()
const {
return items_; }
505 std::vector<KnapsackItemPtr> items_;
506 int64_t current_profit_;
507 int64_t profit_lower_bound_;
508 int64_t profit_upper_bound_;
550 std::vector<bool>* solution)
const override;
561 int64_t GetAdditionalProfit(int64_t remaining_capacity,
562 int break_item_id)
const;
564 const int64_t capacity_;
565 int64_t consumed_capacity_;
567 std::vector<KnapsackItemPtr> sorted_items_;
578 : solver_name_(solver_name) {}
582 virtual void Init(
const std::vector<int64_t>& profits,
583 const std::vector<std::vector<int64_t> >& weights,
584 const std::vector<int64_t>& capacities) = 0;
599 virtual std::string
GetName()
const {
return solver_name_; }
602 const std::string solver_name_;
620 void Init(
const std::vector<int64_t>& profits,
621 const std::vector<std::vector<int64_t> >& weights,
622 const std::vector<int64_t>& capacities)
override;
632 master_propagator_id_ = master_propagator_id;
639 return best_solution_.at(item_id);
655 void UpdateBestSolution();
662 int64_t GetAggregatedProfitUpperBound()
const;
663 bool HasOnePropagator()
const {
return propagators_.size() == 1; }
664 int64_t GetCurrentProfit()
const {
665 return propagators_.at(master_propagator_id_)->current_profit();
667 int64_t GetNextItemId()
const {
668 return propagators_.at(master_propagator_id_)->GetNextItemId();
671 std::vector<KnapsackPropagator*> propagators_;
672 int master_propagator_id_;
673 std::vector<KnapsackSearchNode*> search_nodes_;
674 KnapsackState state_;
675 int64_t best_solution_profit_;
676 std::vector<bool> best_solution_;
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64_t *lower_bound, int64_t *upper_bound)
virtual int64_t Solve(TimeLimit *time_limit, bool *is_solution_optimal)=0
virtual ~BaseKnapsackSolver()
virtual void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities)=0
BaseKnapsackSolver(const std::string &solver_name)
virtual std::string GetName() const
virtual bool best_solution(int item_id) const =0
KnapsackCapacityPropagator(const KnapsackState &state, int64_t capacity)
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
void ComputeProfitBounds() override
~KnapsackCapacityPropagator() override
void InitPropagator() override
int GetNextItemId() const override
~KnapsackGenericSolver() override
KnapsackGenericSolver(const std::string &solver_name)
void set_master_propagator_id(int master_propagator_id)
int GetNumberOfItems() const
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
int64_t Solve(TimeLimit *time_limit, bool *is_solution_optimal) override
bool best_solution(int item_id) const override
void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64_t *lower_bound, int64_t *upper_bound) override
void Init(const std::vector< int64_t > &profits, const std::vector< int64_t > &weights)
int64_t current_profit() const
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
const std::vector< KnapsackItemPtr > & items() const
virtual void InitPropagator()=0
virtual void ComputeProfitBounds()=0
virtual int GetNextItemId() const =0
int64_t profit_upper_bound() const
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
int64_t profit_lower_bound() const
void set_profit_lower_bound(int64_t profit)
virtual ~KnapsackPropagator()
KnapsackPropagator(const KnapsackState &state)
bool Update(bool revert, const KnapsackAssignment &assignment)
void set_profit_upper_bound(int64_t profit)
const KnapsackState & state() const
int64_t current_profit() const
void set_current_profit(int64_t profit)
const KnapsackSearchNode *const parent() const
void set_next_item_id(int id)
int64_t profit_upper_bound() const
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
const KnapsackAssignment & assignment() const
void set_profit_upper_bound(int64_t profit)
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
const KnapsackSearchNode & from() const
const KnapsackSearchNode & via() const
const KnapsackSearchNode & to() const
This library solves knapsack problems.
bool BestSolutionContains(int item_id) const
Returns true if the item 'item_id' is packed in the optimal knapsack.
KnapsackSolver(const std::string &solver_name)
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
int64_t Solve()
Solves the problem and returns the profit of the optimal solution.
SolverType
Enum controlling which underlying algorithm is used.
@ KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER
SCIP based solver.
@ KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER
Generic Solver.
@ KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
Dynamic Programming approach for single dimension problems.
@ KNAPSACK_DIVIDE_AND_CONQUER_SOLVER
Divide and Conquer approach for single dimension problems.
@ KNAPSACK_64ITEMS_SOLVER
Optimized method for single dimension small problems.
@ KNAPSACK_BRUTE_FORCE_SOLVER
Brute force method.
@ KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
CBC Based Solver.
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
std::string GetName() const
void set_use_reduction(bool use_reduction)
virtual ~KnapsackSolver()
bool use_reduction() const
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities)
Initializes the solver and enters the problem to be solved.
int GetNumberOfItems() const
void Init(int number_of_items)
bool is_bound(int id) const
bool UpdateState(bool revert, const KnapsackAssignment &assignment)
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
ModelSharedTimeLimit * time_limit
Collection of objects used to extend the Constraint Solver library.
KnapsackItem * KnapsackItemPtr
KnapsackAssignment(int _item_id, bool _is_in)
double GetEfficiency(int64_t profit_max) const
KnapsackItem(int _id, int64_t _weight, int64_t _profit)