Alps 1.5.12
Loading...
Searching...
No Matches
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-2019, 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
26class KnapModel : public AlpsModel {
27
28 private:
29
32
34 std::vector< std::pair<int, int> > items_;
35
38
41
42 public:
43
45 KnapModel(int cap, std::vector<std::pair<int, int> > items, int* seq)
46 :
47 capacity_(cap),
48 sequence_(seq),
50 { items_.insert(items_.begin(), items.begin(), items.end()); }
51
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
102
104 virtual AlpsEncoded* encode() const;
105
108 virtual void decodeToSelf(AlpsEncoded&);
109};
110
111//#############################################################################
112
113#endif
This data structure is to contain the packed form of an encodable knowledge.
Definition AlpsEncoded.h:25
AlpsParams * AlpsPar_
The parameter set that is used in Alps.
Definition AlpsModel.h:52
AlpsModel(const AlpsModel &)
@ msgLevel
The level of printing messages on screen.
Definition AlpsParams.h:113
std::pair< int, int > getItem(int i) const
Get the size of item i.
Definition KnapModel.h:83
void setCapacity(int capacity)
Set the capacity of the knapsack.
Definition KnapModel.h:88
int getCapacity() const
Get the capacity of the knapsack.
Definition KnapModel.h:61
virtual AlpsEncoded * encode() const
The method that encodes the solution into a buffer.
virtual void readParameters(const int argnum, const char *const *arglist)
Read in Alps and Knap parameters.
Definition KnapModel.h:72
void readInstance(const char *dataFile)
Read in the problem data.
KnapParams * KnapPar_
Knap parameters.
Definition KnapModel.h:40
KnapModel(int cap, std::vector< std::pair< int, int > > items, int *seq)
Definition KnapModel.h:45
void addItem(int size, int cost)
Set the size of item i.
Definition KnapModel.h:94
int capacity_
Capacity of the knapsack.
Definition KnapModel.h:31
int * getSequence() const
Get the sequence of items in the knapsack.
Definition KnapModel.h:67
void setSequence(const int *seq)
Set the sequence of items in the knapsack.
void orderItems()
Order the items based on their cost/size.
int getNumItems() const
Get the number of items in the knapsack.
Definition KnapModel.h:64
int * sequence_
The descent sequence based on ratio: profit/size.
Definition KnapModel.h:37
virtual void decodeToSelf(AlpsEncoded &)
The method that decodes model data from the encoded form and fill member data.
std::vector< std::pair< int, int > > items_
List of sizes and profits of the items.
Definition KnapModel.h:34