14 #ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ 15 #define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ 23 #include "absl/memory/memory.h" 32 class BaseKnapsackSolver;
165 #if defined(USE_SCIP) 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_;
683 #endif // OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ int64_t profit_upper_bound() const
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
const KnapsackSearchNode *const parent() const
bool is_bound(int id) const
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
KnapsackItem(int _id, int64_t _weight, int64_t _profit)
void set_profit_lower_bound(int64_t profit)
~KnapsackCapacityPropagator() override
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.
SolverType
Enum controlling which underlying algorithm is used.
ModelSharedTimeLimit * time_limit
KnapsackPropagator(const KnapsackState &state)
virtual void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities)=0
void set_current_profit(int64_t profit)
KnapsackItem * KnapsackItemPtr
int64_t Solve()
Solves the problem and returns the profit of the optimal solution.
This library solves knapsack problems.
bool BestSolutionContains(int item_id) const
Returns true if the item 'item_id' is packed in the optimal knapsack.
virtual int64_t Solve(TimeLimit *time_limit, bool *is_solution_optimal)=0
void Init(int number_of_items)
Dynamic Programming approach for single dimension problems.
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64_t *lower_bound, int64_t *upper_bound)
int64_t current_profit() const
int64_t profit_upper_bound() const
~KnapsackGenericSolver() override
void set_next_item_id(int id)
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
int GetNumberOfItems() const
KnapsackSolver(const std::string &solver_name)
void InitPropagator() override
void ComputeProfitBounds() override
virtual std::string GetName() const
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
KnapsackGenericSolver(const std::string &solver_name)
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
double GetEfficiency(int64_t profit_max) const
bool Update(bool revert, const KnapsackAssignment &assignment)
void set_profit_upper_bound(int64_t profit)
virtual bool best_solution(int item_id) const =0
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
virtual void InitPropagator()=0
const KnapsackSearchNode & via() const
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
virtual ~KnapsackPropagator()
const KnapsackState & state() const
virtual ~KnapsackSolver()
void set_profit_upper_bound(int64_t profit)
void Init(const std::vector< int64_t > &profits, const std::vector< std::vector< int64_t > > &weights, const std::vector< int64_t > &capacities) override
void set_master_propagator_id(int master_propagator_id)
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
int GetNumberOfItems() const
void Init(const std::vector< int64_t > &profits, const std::vector< int64_t > &weights)
int64_t profit_lower_bound() const
int GetNextItemId() const override
KnapsackAssignment(int _item_id, bool _is_in)
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
BaseKnapsackSolver(const std::string &solver_name)
Collection of objects used to extend the Constraint Solver library.
int64_t Solve(TimeLimit *time_limit, bool *is_solution_optimal) override
const std::vector< KnapsackItemPtr > & items() const
KnapsackCapacityPropagator(const KnapsackState &state, int64_t capacity)
bool use_reduction() const
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
Divide and Conquer approach for single dimension problems.
const KnapsackSearchNode & to() const
virtual void ComputeProfitBounds()=0
Optimized method for single dimension small problems.
int64_t current_profit() const
const KnapsackSearchNode & from() const
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
std::string GetName() const
void set_use_reduction(bool use_reduction)
const KnapsackAssignment & assignment() const
virtual int GetNextItemId() const =0
virtual ~BaseKnapsackSolver()
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
bool UpdateState(bool revert, const KnapsackAssignment &assignment)