BcpsObjectPool.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Branch, Constrain and Price Software (BiCePS) *
3  * *
4  * BiCePS 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  * Ted Ralphs, Lehigh University *
11  * *
12  * Conceptual Design: *
13  * *
14  * Yan Xu, Lehigh University *
15  * Ted Ralphs, Lehigh University *
16  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
17  * Matthew Saltzman, Clemson University *
18  * *
19  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs. *
20  * All Rights Reserved. *
21  *===========================================================================*/
22 
23 #ifndef BcpsObjectPool_h_
24 #define BcpsObjectPool_h_
25 
26 #include <vector>
27 
28 #include "AlpsKnowledgePool.h"
29 
30 #include "BcpsObject.h"
31 
32 //#############################################################################
34 //#############################################################################
35 
37 
38  protected:
39 
40  std::vector<AlpsKnowledge *> objects_;
41 
42  public:
43 
46  virtual ~BcpsObjectPool() {
47  if (! objects_.empty()) {
48  freeGuts();
49  }
50  }
51 
53  inline void freeGuts() {
54  for (int i = static_cast<int> (objects_.size() - 1); i > -1; --i) {
55  delete objects_[i];
56  }
57  objects_.clear();
58  }
59 
61  inline void clear(){ objects_.clear(); }
62 
64  virtual void addKnowledge(AlpsKnowledge * nk, double priority) {
65  objects_.push_back(nk);
66  }
67 
69  virtual int getNumKnowledges() const {
70  return static_cast<int>(objects_.size());
71  }
72 
74  virtual std::pair<AlpsKnowledge*, double> getKnowledge() const {
75  return std::make_pair(objects_[0], 0.0);
76  }
77 
79  virtual bool hasKnowledge() const
80  { return objects_.empty() ? false : true; }
81 
83  void deleteObject(int k) {
84  assert(k > -1 && k < ((int)objects_.size()));
85 
86  AlpsKnowledge *objectK = getObject(k);
87  std::vector<AlpsKnowledge *>::iterator pos;
88  pos = objects_.begin() + k;
89  objects_.erase(pos);
90 
91  // Free memory of object k.
92  delete objectK;
93  }
94 
96  const std::vector<AlpsKnowledge *>& getObjects() const { return objects_; }
97 
99  AlpsKnowledge *getObject(int k) const { return objects_[k]; }
100 };
101 
102 //#############################################################################
103 
105  public:
107  virtual ~BcpsConstraintPool() {}
108 
110  void addConstraint(BcpsConstraint * con) { objects_.push_back(con); }
111 
113  void deleteConstraint(int k) { deleteObject(k); }
114 
116  int getNumConstraints() const { return getNumKnowledges(); }
117 
119  const std::vector<AlpsKnowledge *>& getConstraints() const {return objects_;}
120 
122  AlpsKnowledge *getConstraint(int k) const {return getObject(k);}
123 };
124 
125 //#############################################################################
126 
128  public:
130  virtual ~BcpsVariablePool() {}
131 
133  void addVariable(BcpsVariable * var) { objects_.push_back(var); }
134 
136  void deleteVariable(int k) { deleteObject(k); }
137 
139  int getNumVariables() const { return getNumKnowledges(); }
140 
142  const std::vector<AlpsKnowledge *>& getVariables() const {return objects_;}
143 
145  AlpsKnowledge *getVariable(int k) const {return getObject(k);}
146 };
147 
148 //#############################################################################
149 
150 #endif // End of file
int getNumConstraints() const
Query how many constraints are in the pool.
BcpsObjectPool()
Default construct.
void deleteConstraint(int k)
Delete constraint k from pool.
void freeGuts()
Free object pointers.
void deleteObject(int k)
Delete object k from pool.
virtual bool hasKnowledge() const
Check whether the pool has knowledge.
virtual std::pair< AlpsKnowledge *, double > getKnowledge() const
Query a knowledge, but doesn&#39;t remove it from the pool.
const std::vector< AlpsKnowledge * > & getVariables() const
Get the vector of variables.
virtual ~BcpsConstraintPool()
AlpsKnowledge * getObject(int k) const
Get a object.
const std::vector< AlpsKnowledge * > & getObjects() const
Get all objects.
int getNumVariables() const
Query how many variables are in the pool.
Object pool is used to store objects.
virtual ~BcpsObjectPool()
const std::vector< AlpsKnowledge * > & getConstraints() const
Get the vector of constraints.
virtual ~BcpsVariablePool()
AlpsKnowledge * getConstraint(int k) const
Get a constraints.
AlpsKnowledge * getVariable(int k) const
Get the vector of variables.
void clear()
Reset to empty.
The abstract base class of any user-defined class that Alps has to know about in order to encode/deco...
Definition: AlpsKnowledge.h:51
void addVariable(BcpsVariable *var)
Add a variable to pool.
std::vector< AlpsKnowledge * > objects_
void deleteVariable(int k)
Delete variable k from pool.
void addConstraint(BcpsConstraint *con)
Add a constraint to pool.
virtual int getNumKnowledges() const
Query how many knowledges are in the pool.
virtual void addKnowledge(AlpsKnowledge *nk, double priority)
Add a knowledge to pool.