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  *          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  * Copyright (C) 2001-2019, Lehigh University, Yan Xu, and Ted Ralphs.       *
21  * All Rights Reserved.                                                      *
22  *===========================================================================*/
23 
24 #ifndef BcpsNodeDesc_h_
25 #define BcpsNodeDesc_h_
26 
27 #include "BcpsModel.h"
28 
29 #include "Alps.h"
30 #include "AlpsNodeDesc.h"
31 
32 #include "BcpsObject.h"
33 
34 
35 //#############################################################################
36 
37 /** This class contains modifications for a single
38     <code>std::vector<T></code> object. */
39 
40 template <class T> struct BcpsFieldListMod {
41     /** How the modification is stored, explicit means complete
42         replacement, relative means relative to some other explicit
43         object vector ("explicit" is a key word so we can't
44         name the field that.) */
45     bool relative;
46 
47     /** The number of entries to be modified. */
48     int  numModify;
49 
50     /** The positions to be modified. */
51     int* posModify;
52 
53     /** Values*/
54     T*   entries;
55 };
56 
57 
58 //#############################################################################
59 
60 
61 /** Here is the set of <code>vectorMod_</code> objects that represent the
62     list of objects of a particular type (either in relative or explicit
63     form). If <code>numRemove</code> is positive, then
64     <code>posRemove</code> contains the positions of the objects that are
65     to be deleted from all the lists (that are stored relatively). */
66 
67 struct BcpsObjectListMod {
68 
69     /** The number of entries to be deleted. If this is -1, then all
70         objects should be deleted, i.e., we have an explicit list. */
71     int  numRemove;
72 
73     /** The positions of the entries to be deleted. */
74     int* posRemove;
75 
76     /** The number of objects that are to added. */
77     int  numAdd;
78     /** The objects to be added. */
79     BcpsObject **objects;
80 
81     /** These are the data structures that store the changes in the
82         individual fields. */
83     /** @{ */
84     BcpsFieldListMod<double> lbHard;
85     BcpsFieldListMod<double> ubHard;
86     BcpsFieldListMod<double> lbSoft;
87     BcpsFieldListMod<double> ubSoft;
88     BcpsFieldListMod<int> status;
89     /** @} */
90 };
91 
92 
93 //#############################################################################
94 
95 /**
96    For a given type, the objectVecStorage_ structure holds the
97    description. This description is explicit if the \c numRemoved member is
98    -1. In this case, the \c objects member holds the full list of objects of
99    the given type. If an explicit description of the parent is given together
100    with a relative description of the current node then the process of
101    reconstructing the explicit description of the current node is as follows:
102    <ol>
103    <li> initialize the explicit list <b>L</b> to be the parent's list;
104    <li> remove the objects on the indicated positions from <b>L</b>;
105    <li> modify the appropriate members of the objects in <b>L</b> according to
106    the vectorMod_ members of the objectVecStorage_ structure;
107    <li> append the objects to be added to <b>L</b>.
108    </ol>
109 
110    If the numRemoved field is -1, that means that the current description is
111    explicit. In this case, the contents of the vectorMod_ members are largely
112    irrelevant, except for the \c relative fields which indicate whether the
113    appropriate \c vectorMod_ member can ever be expressed as a relative
114    description or not.
115 
116    If the \c numRemoved field is >=0, then the current description is
117    considered relative even if every vectorMod_ member contains explicit data.
118 */
119 
120 //#############################################################################
121 
122 /** The description of a node can be either explicit or relative to its
123     parent. In the node there are a number of object types and for each object
124     type the explicit/relative description is considered separately.
125 
126     If the information on an object type is relative, it means that at least
127     one of the fields (lbHard, ubHard, etc.) has a relative description.
128 */
129 class BcpsNodeDesc : public AlpsNodeDesc {
130 
131  protected:
132 
133     /** Variable objects. */
134     BcpsObjectListMod *vars_;
135 
136     /** Constraint objects. */
137     BcpsObjectListMod *cons_;
138 
139  public:
140 
141     /** Default constructor. */
BcpsNodeDesc()142     BcpsNodeDesc() { initToNull(); }
143 
144     /** Useful constructor. */
BcpsNodeDesc(BcpsModel * m)145     BcpsNodeDesc(BcpsModel* m)
146         :
147         AlpsNodeDesc() { initToNull(); }
148 
149     /** Destructor. */
150     virtual ~BcpsNodeDesc();
151 
152     /** Initialize member data. */
153     void initToNull();
154 
155     /** Set variable objects. */
156     void setVars(int numRem,
157                  const int    *posRem,
158                  int numAdd,
159                  const BcpsObject **objects,
160                  bool relvlh,
161                  int numvlh,
162                  const int    *vlhp,
163                  const double *vlhe,
164                  bool relvuh,
165                  int numvuh,
166                  const int    *vuhp,
167                  const double *vuhe,
168                  bool relvls,
169                  int numvls,
170                  const int    *vlsp,
171                  const double *vlse,
172                  bool relvus,
173                  int numvus,
174                  const int    *vusp,
175                  const double *vuse);
176 
177     /** Assign variable objects. Take over memory ownership. */
178     void assignVars(int numRem,
179                     int    *&posRem,
180                     int numAdd,
181                     BcpsObject **&objects,
182                     bool relvlh,
183                     int numvlh,
184                     int    *&vlhp,
185                     double *&vlhe,
186                     bool relvuh,
187                     int numvuh,
188                     int    *&vuhp,
189                     double *&vuhe,
190                     bool relvls,
191                     int numvls,
192                     int    *&vlsp,
193                     double *&vlse,
194                     bool relvus,
195                     int numvus,
196                     int    *&vusp,
197                     double *&vuse);
198 
199     /** Set constraint objects. */
200     void setCons(int numRem,
201                  const int    *posRem,
202                  int numAdd,
203                  const BcpsObject **objects,
204                  bool relclh,
205                  int numclh,
206                  const int    *clhp,
207                  const double *clhe,
208                  bool relcuh,
209                  int numcuh,
210                  const int    *cuhp,
211                  const double *cuhe,
212                  bool relcls,
213                  int numcls,
214                  const int    *clsp,
215                  const double *clse,
216                  bool relcus,
217                  int numcus,
218                  const int    *cusp,
219                  const double *cuse);
220 
221     /** Assign constraint objects. Take over memory ownership. */
222     void assignCons(int numRem,
223                     int    *&posRem,
224                     int numAdd,
225                     BcpsObject **&objects,
226                     bool relclh,
227                     int numclh,
228                     int    *&clhp,
229                     double *&clhe,
230                     bool relcuh,
231                     int numcuh,
232                     int    *&cuhp,
233                     double *&cuhe,
234                     bool relcls,
235                     int numcls,
236                     int    *&clsp,
237                     double *&clse,
238                     bool relcus,
239                     int numcus,
240                     int    *&cusp,
241                     double *&cuse);
242 
243     /** Get variable objects. */
getVars()244     BcpsObjectListMod *getVars() const { return vars_; }
245 
246     /** Get constraint objects. */
getCons()247     BcpsObjectListMod *getCons() const { return cons_; }
248 
249     /** Accesss varaibles. */
vars()250     BcpsObjectListMod *vars() { return vars_; }
251 
252     /** Accesss constraints. */
cons()253     BcpsObjectListMod *cons() { return cons_; }
254 
255     /** Set variable soft bounds. Take ownerships of arraies. */
256     void assignVarSoftBound(int numModSoftVarLB,
257                             int *&varLBi,
258                             double *&varLBv,
259                             int numModSoftVarUB,
260                             int *&varUBi,
261                             double *&varUBv);
262 
263     /** Set variable soft bounds. Don't take ownerships of arraies. */
264     void setVarSoftBound(int numModSoftVarLB,
265                          const int *varLBi,
266                          const double *varLBv,
267                          int numModSoftVarUB,
268                          const int *varUBi,
269                          const double *varUBv);
270 
271     /** Set variable hard bounds. Take ownerships of arraies. */
272     void assignVarHardBound(int numModHardVarLB,
273                             int *&varLBi,
274                             double *&varLBv,
275                             int numModHardVarUB,
276                             int *&varUBi,
277                             double *&varUBv);
278 
279     /** Set constraint soft bounds.  Don't take ownerships of arraies. */
280     void setConSoftBound(int numModSoftConLB,
281                          const int *conLBi,
282                          const double *conLBv,
283                          int numModSoftConUB,
284                          const int *conUBi,
285                          const double *conUBv);
286 
287     /** Set variable hard bounds. Don't take ownerships of arraies. */
288     void setVarHardBound(int numModHardVarLB,
289                          const int *varLBi,
290                          const double *varLBv,
291                          int numModHardVarUB,
292                          const int *varUBi,
293                          const double *varUBv);
294 
295     /** Set constraint hard bounds. Don't take ownerships of arraies. */
296     void setConHardBound(int numModHardConLB,
297                          const int *conLBi,
298                          const double *conLBv,
299                          int numModHardConUB,
300                          const int *conUBi,
301                          const double *conUBv);
302 
303     /** Recode the added constraints. Take over the memory ownship of
304         aguments. Append to previous constraints. */
appendAddedConstraints(int numAdd,BcpsObject ** addCons)305     void appendAddedConstraints(int numAdd, BcpsObject **addCons){
306         int numAll = numAdd + cons_->numAdd;
307         BcpsObject ** allAdd = new BcpsObject* [numAll];
308         int k;
309 
310         for (k = 0; k < cons_->numAdd; ++k) {
311             allAdd[k] = cons_->objects[k];
312         }
313         for (k = 0; k < numAdd; ++k) {
314             allAdd[(k+cons_->numAdd)] = addCons[k];
315         }
316 
317         delete [] cons_->objects;
318         cons_->numAdd = numAll;
319         cons_->objects = allAdd;
320     }
321 
322     /** Recode the added constraints. Take over the memory ownship of
323         aguments. Delete already added constraints. */
setAddedConstraints(int numAdd,BcpsObject ** addCons)324     void setAddedConstraints(int numAdd, BcpsObject **addCons){
325         // NOTE: make sure objects can be safely deleted.
326         if (cons_->objects) {
327             for (int k = 0; k < cons_->numAdd; ++k) {
328                 delete cons_->objects[k];
329             }
330             delete [] cons_->objects;
331         }
332         cons_->numAdd = numAdd;
333         cons_->objects = addCons;
334     }
335 
336     /** Record the constraints are deleted. Take over the memory
337         ownship of arguments. */
delConstraints(int numDel,int * indices)338     void delConstraints(int numDel, int *indices){
339         if ( cons_->posRemove) delete []  cons_->posRemove;
340         cons_->numRemove = numDel;
341         cons_->posRemove = indices;
342     }
343 
344     /** Record added variables. Take over the memory ownship of arguments. */
addVariables(int numAdd,BcpsObject ** addVars)345     void addVariables(int numAdd, BcpsObject **addVars){
346         if (vars_->objects) {
347             for (int k = 0; k < vars_->numAdd; ++k) {
348                 delete vars_->objects[k];
349             }
350             delete [] vars_->objects;
351         }
352 
353         vars_->numAdd = numAdd;
354         vars_->objects = addVars;
355     }
356 
357     /** Record deleted variables. Take over the memory ownship of arguemnts. */
delVariables(int numDel,int * indices)358     void delVariables(int numDel, int *indices){
359         if (vars_->posRemove) delete []  vars_->posRemove;
360         vars_->numRemove = numDel;
361         vars_->posRemove = indices;
362     }
363 
364  protected:
365 
366     /** Pack a double field into an encoded object. */
367     AlpsReturnStatus encodeDblFieldMods(AlpsEncoded *encoded,
368                                       BcpsFieldListMod<double> * field) const;
369 
370     /** Pack a integer field into an encoded object. */
371     AlpsReturnStatus encodeIntFieldMods(AlpsEncoded *encoded,
372                                       BcpsFieldListMod<int> * field) const;
373 
374     /** Pack object modifications to an encoded object. */
375     AlpsReturnStatus encodeObjectMods(AlpsEncoded *encoded,
376                                     BcpsObjectListMod *objMod) const;
377 
378     /** Unpack a double field from an encoded object. */
379     AlpsReturnStatus decodeDblFieldMods(AlpsEncoded &encoded,
380                                       BcpsFieldListMod<double>*field);
381 
382     /** Unpack a integer field from an encoded object. */
383     AlpsReturnStatus decodeIntFieldMods(AlpsEncoded &encoded,
384                                       BcpsFieldListMod<int> *field);
385 
386     /** Unpack object modifications to an encoded object. */
387     AlpsReturnStatus decodeObjectMods(AlpsEncoded &encoded,
388                                     BcpsObjectListMod *objMod);
389 
390  public:
391 
392     /** Pack bcps node description into an encoded. */
393     AlpsReturnStatus encodeBcps(AlpsEncoded *encoded) const;
394 
395     /** Unpack bcps node description into an encoded. */
396     AlpsReturnStatus decodeBcps(AlpsEncoded &encoded);
397 
398 };
399 
400 #endif /* End of file */
401