14 #ifndef OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
15 #define OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
30 class BaseKnapsackSolver;
163 #if defined(USE_SCIP)
172 #if defined(USE_XPRESS)
178 KNAPSACK_MULTIDIMENSION_XPRESS_MIP_SOLVER = 7,
181 #if defined(USE_CPLEX)
187 KNAPSACK_MULTIDIMENSION_CPLEX_MIP_SOLVER = 8,
206 void Init(
const std::vector<int64_t>& profits,
207 const std::vector<std::vector<int64_t> >& weights,
208 const std::vector<int64_t>& capacities);
234 time_limit_seconds_ = time_limit_seconds;
235 time_limit_ = std::make_unique<TimeLimit>(time_limit_seconds_);
241 int ReduceCapacities(
int num_items,
242 const std::vector<std::vector<int64_t> >& weights,
243 const std::vector<int64_t>& capacities,
244 std::vector<std::vector<int64_t> >* reduced_weights,
245 std::vector<int64_t>* reduced_capacities);
246 int ReduceProblem(
int num_items);
247 void ComputeAdditionalProfit(
const std::vector<int64_t>& profits);
248 void InitReducedProblem(
const std::vector<int64_t>& profits,
249 const std::vector<std::vector<int64_t> >& weights,
250 const std::vector<int64_t>& capacities);
252 std::unique_ptr<BaseKnapsackSolver> solver_;
253 std::vector<bool> known_value_;
254 std::vector<bool> best_solution_;
255 bool is_solution_optimal_ =
false;
256 std::vector<int> mapping_reduced_item_id_;
257 bool is_problem_solved_;
258 int64_t additional_profit_;
260 double time_limit_seconds_;
261 std::unique_ptr<TimeLimit> time_limit_;
317 ?
static_cast<double>(
profit) /
static_cast<double>(
weight)
341 int depth()
const {
return depth_; }
365 int64_t current_profit_;
366 int64_t profit_upper_bound_;
417 void Init(
int number_of_items);
424 bool is_bound(
int id)
const {
return is_bound_.at(
id); }
425 bool is_in(
int id)
const {
return is_in_.at(
id); }
432 std::vector<bool> is_bound_;
433 std::vector<bool> is_in_;
452 void Init(
const std::vector<int64_t>& profits,
453 const std::vector<int64_t>& weights);
476 std::vector<bool>* solution)
const;
494 std::vector<bool>* solution)
const = 0;
497 const std::vector<KnapsackItemPtr>&
items()
const {
return items_; }
503 std::vector<KnapsackItemPtr> items_;
504 int64_t current_profit_;
505 int64_t profit_lower_bound_;
506 int64_t profit_upper_bound_;
548 std::vector<bool>* solution)
const override;
559 int64_t GetAdditionalProfit(int64_t remaining_capacity,
560 int break_item_id)
const;
562 const int64_t capacity_;
563 int64_t consumed_capacity_;
565 std::vector<KnapsackItemPtr> sorted_items_;
576 : solver_name_(solver_name) {}
580 virtual void Init(
const std::vector<int64_t>& profits,
581 const std::vector<std::vector<int64_t> >& weights,
582 const std::vector<int64_t>& capacities) = 0;
597 virtual std::string
GetName()
const {
return solver_name_; }
600 const std::string solver_name_;
618 void Init(
const std::vector<int64_t>& profits,
619 const std::vector<std::vector<int64_t> >& weights,
620 const std::vector<int64_t>& capacities)
override;
630 primary_propagator_id_ = primary_propagator_id;
637 return best_solution_.at(item_id);
653 void UpdateBestSolution();
660 int64_t GetAggregatedProfitUpperBound()
const;
661 bool HasOnePropagator()
const {
return propagators_.size() == 1; }
662 int64_t GetCurrentProfit()
const {
663 return propagators_.at(primary_propagator_id_)->current_profit();
665 int64_t GetNextItemId()
const {
666 return propagators_.at(primary_propagator_id_)->GetNextItemId();
669 std::vector<KnapsackPropagator*> propagators_;
670 int primary_propagator_id_;
671 std::vector<KnapsackSearchNode*> search_nodes_;
672 KnapsackState state_;
673 int64_t best_solution_profit_;
674 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)
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 set_primary_propagator_id(int primary_propagator_id)
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
virtual void InitPropagator()=0
virtual void ComputeProfitBounds()=0
virtual int GetNextItemId() const =0
const KnapsackState & state() const
const std::vector< KnapsackItemPtr > & items() const
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)
int64_t current_profit() const
void set_current_profit(int64_t profit)
void set_next_item_id(int id)
int64_t profit_upper_bound() const
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
void set_profit_upper_bound(int64_t profit)
const KnapsackAssignment & assignment() const
const KnapsackSearchNode *const parent() const
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
const KnapsackSearchNode & via() const
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
const KnapsackSearchNode & from() 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)