- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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: Yan Xu, Lehigh University *
8 * Ted Ralphs, Lehigh University *
9 * Laszlo Ladanyi, IBM T.J. Watson Research Center *
10 * Matthew Saltzman, Clemson University *
11 * *
12 * *
13 * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs. *
14 *===========================================================================*/
15
16 #ifndef KnapModel_h_
17 #define KnapModel_h_
18
19
20 #include "AlpsKnowledge.h"
21 #include "AlpsModel.h"
22 #include "KnapParams.h"
23
24 //#############################################################################
25
26 class KnapModel : public AlpsModel {
27
28 private:
29
31 int capacity_;
32
34 std::vector< std::pair<int, int> > items_;
35
37 int* sequence_;
38
40 KnapParams *KnapPar_;
41
42 public:
43
44 KnapModel() : capacity_(0), sequence_(0), KnapPar_(new KnapParams) {}
45 KnapModel(int cap, std::vector<std::pair<int, int> > items, int* seq)
46 :
47 capacity_(cap),
48 sequence_(seq),
49 KnapPar_(new KnapParams)
50 { items_.insert(items_.begin(), items.begin(), items.end()); }
51
52 ~KnapModel() {
53 if (sequence_ != 0) {
54 delete [] sequence_;
55 sequence_ = 0;
56 }
57 delete KnapPar_;
58 }
59
61 inline int getCapacity() const { return capacity_; }
62
64 inline int getNumItems() const { return static_cast<int> (items_.size()); }
65
67 inline int* getSequence() const { return sequence_; }
68
69 //############################################################################
70
72 virtual void readParameters(const int argnum, const char * const * arglist){
73 AlpsPar_->readFromArglist(argnum, arglist);
74 int msgLevel = AlpsPar_->entry(AlpsParams::msgLevel);
75 if (msgLevel > 0) {
76 std::cout << "Reading in KNAP parameters ..." << std::endl;
77 std::cout << "Reading in ALPS parameters ..." << std::endl;
78 }
79 KnapPar_->readFromArglist(argnum, arglist);
80 }
81
83 inline std::pair<int, int> getItem(int i) const {
84 return(items_[sequence_[i]]);
85 }
86
88 inline void setCapacity(int capacity) { capacity_ = capacity; }
89
91 void setSequence(const int * seq);
92
94 inline void addItem(int size, int cost)
95 { items_.push_back(std::pair<int, int>(size, cost)); }
96
98 void readInstance(const char* dataFile);
99
101 void orderItems();
102
104 virtual AlpsEncoded* encode() const;
105
108 virtual void decodeToSelf(AlpsEncoded&);
109 };
110
111 //#############################################################################
112
113 #endif
KnapModel::items_
std::vector< std::pair< int, int > > items_
List of sizes and profits of the items.
Definition: KnapModel.h:34
KnapModel::getCapacity
int getCapacity() const
Get the capacity of the knapsack.
Definition: KnapModel.h:61
KnapParams.h
KnapModel::decodeToSelf
virtual void decodeToSelf(AlpsEncoded &)
The method that decodes model data from the encoded form and fill member data.
KnapModel::~KnapModel
~KnapModel()
Definition: KnapModel.h:52
AlpsModel.h
KnapModel::setSequence
void setSequence(const int *seq)
Set the sequence of items in the knapsack.
AlpsModel
Definition: AlpsModel.h:36
KnapModel::sequence_
int * sequence_
The descent sequence based on ratio: profit/size.
Definition: KnapModel.h:37
KnapModel::addItem
void addItem(int size, int cost)
Set the size of item i.
Definition: KnapModel.h:94
AlpsEncoded
This data structure is to contain the packed form of an encodable knowledge.
Definition: AlpsEncoded.h:25
KnapModel::readParameters
virtual void readParameters(const int argnum, const char *const *arglist)
Read in Alps and Knap parameters.
Definition: KnapModel.h:72
KnapModel::getSequence
int * getSequence() const
Get the sequence of items in the knapsack.
Definition: KnapModel.h:67
KnapModel::getItem
std::pair< int, int > getItem(int i) const
Get the size of item i.
Definition: KnapModel.h:83
KnapModel::KnapModel
KnapModel(int cap, std::vector< std::pair< int, int > > items, int *seq)
Definition: KnapModel.h:45
AlpsParameterSet::readFromArglist
void readFromArglist(const int argnum, const char *const *arglist)
Read parameters from the command line.
AlpsKnowledge.h
AlpsModel::AlpsPar_
AlpsParams * AlpsPar_
The parameter set that is used in Alps.
Definition: AlpsModel.h:52
AlpsParams::entry
bool entry(const boolParams key) const
Definition: AlpsParams.h:293
KnapModel::readInstance
void readInstance(const char *dataFile)
Read in the problem data.
KnapModel::orderItems
void orderItems()
Order the items based on their cost/size.
KnapModel::setCapacity
void setCapacity(int capacity)
Set the capacity of the knapsack.
Definition: KnapModel.h:88
KnapModel::encode
virtual AlpsEncoded * encode() const
The method that encodes the solution into a buffer.
KnapModel::KnapPar_
KnapParams * KnapPar_
Knap parameters.
Definition: KnapModel.h:40
KnapModel::getNumItems
int getNumItems() const
Get the number of items in the knapsack.
Definition: KnapModel.h:64
KnapModel::capacity_
int capacity_
Capacity of the knapsack.
Definition: KnapModel.h:31
KnapModel::KnapModel
KnapModel()
Definition: KnapModel.h:44
KnapParams
Definition: KnapParams.h:26
KnapModel
Definition: KnapModel.h:26
AlpsParams::msgLevel
The level of printing messages on screen.
Definition: AlpsParams.h:110