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 AlpsKnowledge_h_
28 #define AlpsKnowledge_h_
29 
30 #include <map>
31 #include <memory>
32 #include <typeinfo>
33 
34 #include "Alps.h"
35 #include "AlpsEncoded.h"
36 
37 class AlpsKnowledgeBroker;
38 
39 /*!
40   A function object to perform lexicographic lexicographic comparison
41   between two C style strings.
42 */
43 
44 struct AlpsStrLess {
operatorAlpsStrLess45   inline bool operator()(const char* s1, const char* s2) const {
46     return strcmp(s1, s2) < 0;
47   }
48 };
49 
50 
51 /*!
52 
53   The abstract base class of Alps knowledges generated during the search. It is
54   inherited by #AlpsModel, #AlpsTreeNode, #AlpsSolution and #AlpsSubTree.
55 
56   It provides the API for encoding and decoding functions. ::encode/::decode
57   functions implemented here will work on simple classes (stored in contigious
58   memory), that does not have pointers or STL containers. Complicated classes
59   are required to implement their versions of encode/decode functions.
60 
61 */
62 
63 class AlpsKnowledge {
64   AlpsKnowledgeType type_;
65 public:
66   AlpsKnowledgeBroker * broker_;
67 
68 public:
69   ///@name Constructor and Destructor
70   //@{
71   /// Default constructor
AlpsKnowledge()72   AlpsKnowledge(): type_(AlpsKnowledgeTypeUndefined), broker_(0) {}
AlpsKnowledge(AlpsKnowledgeType type)73   AlpsKnowledge(AlpsKnowledgeType type): type_(type), broker_(0) {}
74   AlpsKnowledge(AlpsKnowledgeType type, AlpsKnowledgeBroker * broker);
75   /// Destructor
~AlpsKnowledge()76   virtual ~AlpsKnowledge() {}
77   //@}
78 
79   ///@name Get type and set type functions
80   //@{
81   /// Get knowledge type.
getType()82   AlpsKnowledgeType getType() const { return type_; }
83   /// Set knowledge type.
setType(AlpsKnowledgeType t)84   void setType(AlpsKnowledgeType t) { type_ = t; }
85   /// Get pointer to the knowledge broker
broker()86   AlpsKnowledgeBroker * broker() { return broker_; }
87   /// Get pointer to the knowledge broker
broker()88   AlpsKnowledgeBroker const * broker() const { return broker_; }
89   /// Set knowledge broker
setBroker(AlpsKnowledgeBroker * broker)90   void setBroker(AlpsKnowledgeBroker * broker) { broker_=broker; }
91   //@}
92 
93   ///@name Encoding and Decoding functions
94   //@{
95   /// Encode the content of this into an AlpsEncoded object and return a
96   /// pointer to it.
97   AlpsEncoded * encode() const;
98   /// Encode the content of this into the given AlpsEncoded object.
99   /// Implementation given in this class can not be used when the memory of
100   /// data members is not contiguous, i.e., some data members are pointers to
101   /// heap locations, STL set, map, etc. These type of user application
102   /// sub-classes should implement their own version of this.
103   virtual AlpsReturnStatus encode(AlpsEncoded * encoded) const;
104   /// Decode the given AlpsEncoded object into a new AlpsKnowledge object and
105   /// return a pointer to it. User application sub-classes should implement this
106   /// since the returned pointer will point to user sub-class instances.
107   virtual AlpsKnowledge * decode(AlpsEncoded & encoded) const = 0;
108   /// Decode the given AlpsEncoded object into this.  Implementation given in
109   /// this class can not be used when the memory of data members is not
110   /// contiguous, i.e., some data members are pointers to heap locations, STL
111   /// set, map, etc. These type of user application sub-classes should
112   /// implement their own version of this.
113   virtual AlpsReturnStatus decodeToSelf(AlpsEncoded & encoded);
114   //@}
115 
116 private:
117   /// Disable copy constructor.
118   AlpsKnowledge(AlpsKnowledge const &);
119   /// Disable copy assignment operator.
120   AlpsKnowledge & operator=(AlpsKnowledge const &);
121 
122 };
123 
124 #endif
125