79 #ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ 80 #define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ 88 #include "absl/memory/memory.h" 89 #include "ortools/base/basictypes.h" 90 #include "ortools/base/integral_types.h" 91 #include "ortools/base/logging.h" 92 #include "ortools/base/macros.h" 93 #include "ortools/util/time_limit.h" 115 class BaseKnapsackSolver;
124 KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER = 3,
127 #if defined(USE_SCIP) 128 KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER = 6,
137 void Init(
const std::vector<int64>& profits,
138 const std::vector<std::vector<int64> >& weights,
139 const std::vector<int64>& capacities);
156 time_limit_seconds_ = time_limit_seconds;
157 time_limit_ = absl::make_unique<TimeLimit>(time_limit_seconds_);
163 int ReduceCapacities(
int num_items,
164 const std::vector<std::vector<int64> >& weights,
165 const std::vector<int64>& capacities,
166 std::vector<std::vector<int64> >* reduced_weights,
167 std::vector<int64>* reduced_capacities);
168 int ReduceProblem(
int num_items);
169 void ComputeAdditionalProfit(
const std::vector<int64>& profits);
170 void InitReducedProblem(
const std::vector<int64>& profits,
171 const std::vector<std::vector<int64> >& weights,
172 const std::vector<int64>& capacities);
174 std::unique_ptr<BaseKnapsackSolver> solver_;
175 std::vector<bool> known_value_;
176 std::vector<bool> best_solution_;
177 bool is_solution_optimal_ =
false;
178 std::vector<int> mapping_reduced_item_id_;
179 bool is_problem_solved_;
180 int64 additional_profit_;
182 double time_limit_seconds_;
183 std::unique_ptr<TimeLimit> time_limit_;
239 ? static_cast<double>(
profit) / static_cast<double>(
weight)
240 : static_cast<double>(profit_max);
263 int depth()
const {
return depth_; }
287 int64 current_profit_;
288 int64 profit_upper_bound_;
339 void Init(
int number_of_items);
346 bool is_bound(
int id)
const {
return is_bound_.at(
id); }
347 bool is_in(
int id)
const {
return is_in_.at(
id); }
354 std::vector<bool> is_bound_;
355 std::vector<bool> is_in_;
374 void Init(
const std::vector<int64>& profits,
375 const std::vector<int64>& weights);
416 std::vector<bool>*
solution)
const = 0;
419 const std::vector<KnapsackItemPtr>&
items()
const {
return items_; }
425 std::vector<KnapsackItemPtr> items_;
426 int64 current_profit_;
427 int64 profit_lower_bound_;
428 int64 profit_upper_bound_;
470 std::vector<bool>*
solution)
const override;
481 int64 GetAdditionalProfit(int64 remaining_capacity,
int break_item_id)
const;
483 const int64 capacity_;
484 int64 consumed_capacity_;
486 std::vector<KnapsackItemPtr> sorted_items_;
497 : solver_name_(solver_name) {}
501 virtual void Init(
const std::vector<int64>& profits,
502 const std::vector<std::vector<int64> >& weights,
503 const std::vector<int64>& capacities) = 0;
513 virtual int64
Solve(TimeLimit* time_limit,
bool* is_solution_optimal) = 0;
518 virtual std::string
GetName()
const {
return solver_name_; }
521 const std::string solver_name_;
539 void Init(
const std::vector<int64>& profits,
540 const std::vector<std::vector<int64> >& weights,
541 const std::vector<int64>& capacities)
override;
545 int64* upper_bound)
override;
551 master_propagator_id_ = master_propagator_id;
555 int64
Solve(TimeLimit* time_limit,
bool* is_solution_optimal)
override;
558 return best_solution_.at(item_id);
574 void UpdateBestSolution();
581 int64 GetAggregatedProfitUpperBound()
const;
582 bool HasOnePropagator()
const {
return propagators_.size() == 1; }
583 int64 GetCurrentProfit()
const {
584 return propagators_.at(master_propagator_id_)->current_profit();
586 int64 GetNextItemId()
const {
587 return propagators_.at(master_propagator_id_)->GetNextItemId();
590 std::vector<KnapsackPropagator*> propagators_;
591 int master_propagator_id_;
592 std::vector<KnapsackSearchNode*> search_nodes_;
593 KnapsackState state_;
594 int64 best_solution_profit_;
595 std::vector<bool> best_solution_;
602 #endif // OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_ const KnapsackSearchNode & to() const
+
+
const KnapsackSearchNode & via() const
+
KnapsackItem(int _id, int64 _weight, int64 _profit)
+
int GetNumberOfItems() const
+
KnapsackAssignment(int _item_id, bool _is_in)
+
const KnapsackState & state() const
+
const std::vector< KnapsackItemPtr > & items() const
+
int GetNextItemId() const override
Returns the id of next item to assign.
void InitPropagator() override
Initializes KnapsackCapacityPropagator (e.g., sort items in decreasing order).
void Init(int number_of_items)
Initializes vectors with number_of_items set to false (i.e. not bound yet).
bool Update(bool revert, const KnapsackAssignment &assignment)
Updates data structure and then calls UpdatePropagator.
virtual void InitPropagator()=0
Initializes data structure.
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
Copies the current state into 'solution'.
virtual void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities)=0
Initializes the solver and enters the problem to be solved.
-
int64 profit_upper_bound() const
+
int64 profit_upper_bound() const
bool BestSolutionContains(int item_id) const
Returns true if the item 'item_id' is packed in the optimal knapsack.
-
const KnapsackSearchNode & from() const
-
void set_next_item_id(int id)
-
double GetEfficiency(int64 profit_max) const
-
--— KnapsackSearchNode --— KnapsackSearchNode is a class used to describe a decision in the decision ...
-
int GetNumberOfItems() const
-
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
-
virtual ~BaseKnapsackSolver()
-
void set_current_profit(int64 profit)
-
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
+
const KnapsackSearchNode & from() const
+
void set_next_item_id(int id)
+
double GetEfficiency(int64 profit_max) const
+
--— KnapsackSearchNode --— KnapsackSearchNode is a class used to describe a decision in the decision ...
+
int GetNumberOfItems() const
+
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
+
virtual ~BaseKnapsackSolver()
+
void set_current_profit(int64 profit)
+
bool best_solution(int item_id) const override
Returns true if the item 'item_id' is packed in the optimal knapsack.
~KnapsackGenericSolver() override
-
void set_master_propagator_id(int master_propagator_id)
Sets which propagator should be used to guide the search.
+
void set_master_propagator_id(int master_propagator_id)
Sets which propagator should be used to guide the search.
std::string GetName() const
-
--— KnapsackSearchPath --— KnapsackSearchPath is a small class used to represent the path between a n...
-
--— KnapsackGenericSolver --— KnapsackGenericSolver is the multi-dimensional knapsack solver class.
-
const int id
The 'id' field is used to retrieve the initial item in order to communicate with other propagators an...
+
--— KnapsackSearchPath --— KnapsackSearchPath is a small class used to represent the path between a n...
+
--— KnapsackGenericSolver --— KnapsackGenericSolver is the multi-dimensional knapsack solver class.
+
const int id
The 'id' field is used to retrieve the initial item in order to communicate with other propagators an...
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
Updates internal data structure incrementally.
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
virtual bool best_solution(int item_id) const =0
Returns true if the item 'item_id' is packed in the optimal knapsack.
-
-
int64 profit_upper_bound() const
-
void set_profit_upper_bound(int64 profit)
-
void set_profit_lower_bound(int64 profit)
-
int64 current_profit() const
+
+
int64 profit_upper_bound() const
+
void set_profit_upper_bound(int64 profit)
+
void set_profit_lower_bound(int64 profit)
+
int64 current_profit() const
void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities) override
Initializes the solver and enters the problem to be solved.
-
-
const KnapsackAssignment & assignment() const
-
+
+
const KnapsackAssignment & assignment() const
+
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
-
void set_use_reduction(bool use_reduction)
-
int64 current_profit() const
+
void set_use_reduction(bool use_reduction)
+
int64 current_profit() const
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound)
Gets the lower and upper bound when the item is in or out of the knapsack.
-
bool is_bound(int id) const
+
bool is_bound(int id) const
void Init(const std::vector< int64 > &profits, const std::vector< int64 > &weights)
Initializes data structure and then calls InitPropagator.
-
void set_profit_upper_bound(int64 profit)
-
const KnapsackSearchNode *const parent() const
+
void set_profit_upper_bound(int64 profit)
+
const KnapsackSearchNode *const parent() const
KnapsackSolver(const std::string &solver_name)
-
BaseKnapsackSolver(const std::string &solver_name)
+
BaseKnapsackSolver(const std::string &solver_name)
KnapsackCapacityPropagator(const KnapsackState &state, int64 capacity)
int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal) override
Solves the problem and returns the profit of the optimal solution.
void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities)
Initializes the solver and enters the problem to be solved.
~KnapsackCapacityPropagator() override
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
Updates internal data structure incrementally (i.e., 'consumed_capacity_') to avoid a O(number_of_ite...
-
+
virtual ~KnapsackPropagator()
-
--— KnapsackCapacityPropagator --— KnapsackCapacityPropagator is a KnapsackPropagator used to enforce...
-
--— KnapsackPropagator --— KnapsackPropagator is the base class for modeling and propagating a constr...
-
+
--— KnapsackCapacityPropagator --— KnapsackCapacityPropagator is a KnapsackPropagator used to enforce...
+
--— KnapsackPropagator --— KnapsackPropagator is the base class for modeling and propagating a constr...
+
virtual int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal)=0
Solves the problem and returns the profit of the optimal solution.
-
+
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
Copies the current state into 'solution'.
-
-
+
+
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
-
bool use_reduction() const
-
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
-
+
bool use_reduction() const
+
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
+
KnapsackGenericSolver(const std::string &solver_name)
-
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
-
--— KnapsackAssignement --— KnapsackAssignement is a small struct used to pair an item with its assig...
+
--— KnapsackAssignement --— KnapsackAssignement is a small struct used to pair an item with its assig...
bool UpdateState(bool revert, const KnapsackAssignment &assignment)
Updates the state by applying or reverting a decision.
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
Copies the current state into 'solution'.
virtual void ComputeProfitBounds()=0
ComputeProfitBounds should set 'profit_lower_bound_' and 'profit_upper_bound_' which are constraint s...
void ComputeProfitBounds() override
ComputeProfitBounds should set 'profit_lower_bound_' and 'profit_upper_bound_' which are constraint s...
virtual int GetNextItemId() const =0
Returns the id of next item to assign.
void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound) override
Gets the lower and upper bound when the item is in or out of the knapsack.
-
--— BaseKnapsackSolver --— This is the base class for knapsack solvers.
+
--— BaseKnapsackSolver --— This is the base class for knapsack solvers.
virtual ~KnapsackSolver()
KnapsackPropagator(const KnapsackState &state)
-
int64 profit_lower_bound() const
-
-
--— KnapsackItem --— KnapsackItem is a small struct to pair an item weight with its corresponding pro...
+
int64 profit_lower_bound() const
+
+
--— KnapsackItem --— KnapsackItem is a small struct to pair an item weight with its corresponding pro...
int64 Solve()
Solves the problem and returns the profit of the optimal solution.
-
KnapsackItem * KnapsackItemPtr
-
virtual std::string GetName() const
-
--— KnapsackState --— KnapsackState represents a partial solution to the knapsack problem.
-
+
KnapsackItem * KnapsackItemPtr
+
virtual std::string GetName() const
+
--— KnapsackState --— KnapsackState represents a partial solution to the knapsack problem.
+