Alps  2.0.2
KnapModel.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS). *
3  * *
4  * ALPS is distributed under the Eclipse Public License as part of the *
5  * COIN-OR repository (http://www.coin-or.org). *
6  * *
7  * Authors: *
8  * *
9  * Yan Xu, Lehigh University *
10  * Aykut Bulut, Lehigh University *
11  * Ted Ralphs, Lehigh University *
12  * *
13  * Conceptual Design: *
14  * *
15  * Yan Xu, Lehigh University *
16  * Ted Ralphs, Lehigh University *
17  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
18  * Matthew Saltzman, Clemson University *
19  * *
20  * *
21  * Copyright (C) 2001-2019, Lehigh University, Yan Xu, Aykut Bulut, and *
22  * Ted Ralphs. *
23  * All Rights Reserved. *
24  *===========================================================================*/
25 
26 
27 #ifndef KnapModel_h_
28 #define KnapModel_h_
29 
30 
31 #include "AlpsModel.h"
32 
33 #include "KnapParams.h"
34 
35 #include "KnapTreeNode.h"
36 
37 //#############################################################################
38 
39 class KnapModel : public AlpsModel {
41  int capacity_;
43  std::vector< std::pair<int, int> > items_;
45  int* sequence_;
48 
49  public:
50 
52  KnapModel(int cap, std::vector<std::pair<int, int> > items, int* seq)
53  :
54  capacity_(cap),
55  sequence_(seq),
56  KnapPar_(new KnapParams)
57  { items_.insert(items_.begin(), items.begin(), items.end()); }
58 
59  virtual ~KnapModel() {
60  if (sequence_ != 0) {
61  delete [] sequence_;
62  sequence_ = 0;
63  }
64  delete KnapPar_;
65  }
66 
68  inline int getCapacity() const { return capacity_; }
69 
71  inline int getNumItems() const { return static_cast<int> (items_.size()); }
72 
74  inline int* getSequence() const { return sequence_; }
75 
76 //############################################################################
77 
79  virtual void readParameters(const int argnum, const char * const * arglist){
80  AlpsPar_->readFromArglist(argnum, arglist);
81  int msgLevel = AlpsPar_->entry(AlpsParams::msgLevel);
82  if (msgLevel > 0) {
83  std::cout << "Reading in KNAP parameters ..." << std::endl;
84  std::cout << "Reading in ALPS parameters ..." << std::endl;
85  }
86  KnapPar_->readFromArglist(argnum, arglist);
87  }
88 
89  virtual AlpsTreeNode * createRoot() {
90  return (new KnapTreeNode(this));
91  }
92 
93 
95  inline std::pair<int, int> getItem(int i) const {
96  return(items_[sequence_[i]]);
97  }
98 
100  inline void setCapacity(int capacity) { capacity_ = capacity; }
101 
103  void setSequence(const int * seq);
104 
106  inline void addItem(int size, int cost)
107  { items_.push_back(std::pair<int, int>(size, cost)); }
108 
110  void readInstance(const char* dataFile);
111 
113  void orderItems();
115  using AlpsKnowledge::encode;
117  virtual AlpsReturnStatus encode(AlpsEncoded * encoded) const;
119  virtual AlpsReturnStatus decodeToSelf(AlpsEncoded & encoded);
120  virtual AlpsKnowledge * decode(AlpsEncoded & encoded) const;
121 private:
122  KnapModel(KnapModel const &);
123  KnapModel & operator=(KnapModel const &);
124 };
125 
126 //#############################################################################
127 
128 #endif
KnapModel::getNumItems
int getNumItems() const
Get the number of items in the knapsack.
Definition: KnapModel.h:71
AlpsModel.h
AlpsParameterSet::readFromArglist
void readFromArglist(const int argnum, const char *const *arglist)
Read parameters from the command line.
KnapModel::addItem
void addItem(int size, int cost)
Set the size of item i.
Definition: KnapModel.h:106
KnapModel::setSequence
void setSequence(const int *seq)
Set the sequence of items in the knapsack.
AlpsModel
AlpsModel is a base class for user application problem data.
Definition: AlpsModel.h:132
KnapModel::getSequence
int * getSequence() const
Get the sequence of items in the knapsack.
Definition: KnapModel.h:74
AlpsReturnStatus
AlpsReturnStatus
Definition: Alps.h:261
KnapParams.h
KnapTreeNode.h
AlpsEncoded
Definition: AlpsEncoded.h:64
AlpsParams::msgLevel
@ msgLevel
The level of printing messages on screen.
Definition: AlpsParams.h:117
AlpsTreeNode
This class holds one node of the search tree.
Definition: AlpsTreeNode.h:52
KnapModel::createRoot
virtual AlpsTreeNode * createRoot()
Create the root node.
Definition: KnapModel.h:89
KnapModel::readInstance
void readInstance(const char *dataFile)
Read in the problem data.
KnapModel::KnapModel
KnapModel()
Definition: KnapModel.h:51
KnapModel::setCapacity
void setCapacity(int capacity)
Set the capacity of the knapsack.
Definition: KnapModel.h:100
KnapModel::operator=
KnapModel & operator=(KnapModel const &)
KnapModel
Definition: KnapModel.h:39
KnapModel::~KnapModel
virtual ~KnapModel()
Definition: KnapModel.h:59
KnapParams
Definition: KnapParams.h:37
AlpsModel::AlpsPar_
AlpsParams * AlpsPar_
The parameter set that is used in Alps.
Definition: AlpsModel.h:143
KnapModel::getCapacity
int getCapacity() const
Get the capacity of the knapsack.
Definition: KnapModel.h:68
AlpsKnowledge
The abstract base class of Alps knowledges generated during the search.
Definition: AlpsKnowledge.h:63
AlpsParams::entry
bool entry(const boolParams key) const
Definition: AlpsParams.h:300
KnapModel::getItem
std::pair< int, int > getItem(int i) const
Get the size of item i.
Definition: KnapModel.h:95
KnapModel::readParameters
virtual void readParameters(const int argnum, const char *const *arglist)
Read in Alps and Knap parameters.
Definition: KnapModel.h:79
AlpsKnowledge::encode
AlpsEncoded * encode() const
Encode the content of this into an AlpsEncoded object and return a pointer to it.
KnapModel::items_
std::vector< std::pair< int, int > > items_
List of sizes and profits of the items.
Definition: KnapModel.h:43
KnapModel::decodeToSelf
virtual AlpsReturnStatus decodeToSelf(AlpsEncoded &encoded)
Decode the given AlpsEncoded object into this.
KnapModel::sequence_
int * sequence_
The descent sequence based on ratio: profit/size.
Definition: KnapModel.h:45
KnapTreeNode
Holds a Knapsack tree node.
Definition: KnapTreeNode.h:42
KnapModel::orderItems
void orderItems()
Order the items based on their cost/size.
KnapModel::KnapPar_
KnapParams * KnapPar_
Knap parameters.
Definition: KnapModel.h:47
KnapModel::decode
virtual AlpsKnowledge * decode(AlpsEncoded &encoded) const
Decode the given AlpsEncoded object into a new AlpsKnowledge object and return a pointer to it.
KnapModel::capacity_
int capacity_
Capacity of the knapsack.
Definition: KnapModel.h:41
KnapModel::KnapModel
KnapModel(int cap, std::vector< std::pair< int, int > > items, int *seq)
Definition: KnapModel.h:52