 |
OR-Tools
8.1
|
Go to the documentation of this file.
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;
168 #if defined(USE_SCIP)
177 #if defined(USE_XPRESS)
183 KNAPSACK_MULTIDIMENSION_XPRESS_MIP_SOLVER = 7,
186 #if defined(USE_CPLEX)
192 KNAPSACK_MULTIDIMENSION_CPLEX_MIP_SOLVER = 8,
203 void Init(
const std::vector<int64>& profits,
204 const std::vector<std::vector<int64> >& weights,
205 const std::vector<int64>& capacities);
231 time_limit_seconds_ = time_limit_seconds;
232 time_limit_ = absl::make_unique<TimeLimit>(time_limit_seconds_);
238 int ReduceCapacities(
int num_items,
239 const std::vector<std::vector<int64> >& weights,
240 const std::vector<int64>& capacities,
241 std::vector<std::vector<int64> >* reduced_weights,
242 std::vector<int64>* reduced_capacities);
243 int ReduceProblem(
int num_items);
244 void ComputeAdditionalProfit(
const std::vector<int64>& profits);
245 void InitReducedProblem(
const std::vector<int64>& profits,
246 const std::vector<std::vector<int64> >& weights,
247 const std::vector<int64>& capacities);
249 std::unique_ptr<BaseKnapsackSolver> solver_;
250 std::vector<bool> known_value_;
251 std::vector<bool> best_solution_;
252 bool is_solution_optimal_ =
false;
253 std::vector<int> mapping_reduced_item_id_;
254 bool is_problem_solved_;
255 int64 additional_profit_;
257 double time_limit_seconds_;
258 std::unique_ptr<TimeLimit> time_limit_;
314 ?
static_cast<double>(
profit) /
static_cast<double>(
weight)
338 int depth()
const {
return depth_; }
362 int64 current_profit_;
363 int64 profit_upper_bound_;
414 void Init(
int number_of_items);
421 bool is_bound(
int id)
const {
return is_bound_.at(
id); }
422 bool is_in(
int id)
const {
return is_in_.at(
id); }
429 std::vector<bool> is_bound_;
430 std::vector<bool> is_in_;
449 void Init(
const std::vector<int64>& profits,
450 const std::vector<int64>& weights);
473 std::vector<bool>* solution)
const;
491 std::vector<bool>* solution)
const = 0;
494 const std::vector<KnapsackItemPtr>&
items()
const {
return items_; }
500 std::vector<KnapsackItemPtr> items_;
501 int64 current_profit_;
502 int64 profit_lower_bound_;
503 int64 profit_upper_bound_;
545 std::vector<bool>* solution)
const override;
556 int64 GetAdditionalProfit(
int64 remaining_capacity,
int break_item_id)
const;
558 const int64 capacity_;
559 int64 consumed_capacity_;
561 std::vector<KnapsackItemPtr> sorted_items_;
572 : solver_name_(solver_name) {}
576 virtual void Init(
const std::vector<int64>& profits,
577 const std::vector<std::vector<int64> >& weights,
578 const std::vector<int64>& capacities) = 0;
593 virtual std::string
GetName()
const {
return solver_name_; }
596 const std::string solver_name_;
614 void Init(
const std::vector<int64>& profits,
615 const std::vector<std::vector<int64> >& weights,
616 const std::vector<int64>& capacities)
override;
620 int64* upper_bound)
override;
626 master_propagator_id_ = master_propagator_id;
633 return best_solution_.at(item_id);
649 void UpdateBestSolution();
656 int64 GetAggregatedProfitUpperBound()
const;
657 bool HasOnePropagator()
const {
return propagators_.size() == 1; }
658 int64 GetCurrentProfit()
const {
659 return propagators_.at(master_propagator_id_)->current_profit();
661 int64 GetNextItemId()
const {
662 return propagators_.at(master_propagator_id_)->GetNextItemId();
665 std::vector<KnapsackPropagator*> propagators_;
666 int master_propagator_id_;
667 std::vector<KnapsackSearchNode*> search_nodes_;
668 KnapsackState state_;
669 int64 best_solution_profit_;
670 std::vector<bool> best_solution_;
677 #endif // OR_TOOLS_ALGORITHMS_KNAPSACK_SOLVER_H_
KnapsackSearchPath(const KnapsackSearchNode &from, const KnapsackSearchNode &to)
bool IsSolutionOptimal() const
Returns true if the solution was proven optimal.
void InitPropagator() override
int GetNextItemId() const override
~KnapsackCapacityPropagator() override
KnapsackSearchNode(const KnapsackSearchNode *const parent, const KnapsackAssignment &assignment)
@ KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER
Generic Solver.
KnapsackPropagator(const KnapsackState &state)
@ KNAPSACK_MULTIDIMENSION_SCIP_MIP_SOLVER
SCIP based solver.
const KnapsackSearchNode & to() const
const KnapsackState & state() const
virtual void InitPropagator()=0
KnapsackItem(int _id, int64 _weight, int64 _profit)
const KnapsackSearchNode & from() const
@ KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
Dynamic Programming approach for single dimension problems.
virtual void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound)
double GetEfficiency(int64 profit_max) const
void Init(int number_of_items)
KnapsackCapacityPropagator(const KnapsackState &state, int64 capacity)
int64 profit_upper_bound() const
void GetLowerAndUpperBoundWhenItem(int item_id, bool is_item_in, int64 *lower_bound, int64 *upper_bound) override
bool best_solution(int item_id) const override
void set_master_propagator_id(int master_propagator_id)
virtual ~KnapsackSolver()
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const override
int64 profit_lower_bound() const
int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal) override
bool is_bound(int id) const
int GetNumberOfItems() const
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment) override
This library solves knapsack problems.
~KnapsackGenericSolver() override
bool Update(bool revert, const KnapsackAssignment &assignment)
const KnapsackSearchNode *const parent() const
int64 current_profit() const
bool UpdateState(bool revert, const KnapsackAssignment &assignment)
SharedTimeLimit * time_limit
void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities) override
const KnapsackAssignment & assignment() const
KnapsackAssignment(int _item_id, bool _is_in)
void ComputeProfitBounds() override
void set_profit_upper_bound(int64 profit)
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.
virtual bool UpdatePropagator(bool revert, const KnapsackAssignment &assignment)=0
virtual int GetNextItemId() const =0
@ KNAPSACK_MULTIDIMENSION_CBC_MIP_SOLVER
CBC Based Solver.
virtual void Init(const std::vector< int64 > &profits, const std::vector< std::vector< int64 > > &weights, const std::vector< int64 > &capacities)=0
int GetNumberOfItems() const
virtual bool best_solution(int item_id) const =0
@ KNAPSACK_BRUTE_FORCE_SOLVER
Brute force method.
BaseKnapsackSolver(const std::string &solver_name)
int64 profit_upper_bound() const
bool use_reduction() const
@ KNAPSACK_64ITEMS_SOLVER
Optimized method for single dimension small problems.
void CopyCurrentStateToSolution(bool has_one_propagator, std::vector< bool > *solution) const
int64 current_profit() const
virtual int64 Solve(TimeLimit *time_limit, bool *is_solution_optimal)=0
void set_time_limit(double time_limit_seconds)
Time limit in seconds.
void set_profit_lower_bound(int64 profit)
virtual ~BaseKnapsackSolver()
void set_next_item_id(int id)
bool BestSolutionContains(int item_id) const
Returns true if the item 'item_id' is packed in the optimal knapsack.
virtual std::string GetName() const
SolverType
Enum controlling which underlying algorithm is used.
const KnapsackSearchNode & via() const
KnapsackSolver(const std::string &solver_name)
std::string GetName() const
virtual void ComputeProfitBounds()=0
void Init(const std::vector< int64 > &profits, const std::vector< int64 > &weights)
const KnapsackSearchNode * MoveUpToDepth(const KnapsackSearchNode &node, int depth) const
virtual ~KnapsackPropagator()
void set_current_profit(int64 profit)
void set_use_reduction(bool use_reduction)
virtual void CopyCurrentStateToSolutionPropagator(std::vector< bool > *solution) const =0
KnapsackGenericSolver(const std::string &solver_name)
KnapsackItem * KnapsackItemPtr
const std::vector< KnapsackItemPtr > & items() const
void set_profit_upper_bound(int64 profit)
int64 Solve()
Solves the problem and returns the profit of the optimal solution.