Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Entity.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file Entity.hh
11 /// @brief the unit employed/optimized by GeneticAlgorithm
12 /// @author ashworth
13 
14 #ifndef INCLUDED_protocols_genetic_algorithm_Entity_hh
15 #define INCLUDED_protocols_genetic_algorithm_Entity_hh
16 
17 // Unit headers
19 
20 #include <core/types.hh>
21 
22 // Utility headers
23 // AUTO-REMOVED #include <utility/exit.hh>
24 #include <utility/vector1.hh>
25 #include <utility/factory/WidgetFactory.hh>
26 #include <utility/factory/WidgetRegistrator.hh>
27 #include <utility/pointer/ReferenceCount.hh>
28 #include <utility/pointer/owning_ptr.hh>
29 #include <utility/pointer/access_ptr.hh>
30 
31 // Boost headers
32 #include <boost/functional/hash.hpp> // hash_range
33 
34 ///#include <ObjexxFCL/format.hh> // apl needed?
35 ///using namespace ObjexxFCL;
36 
37 // C++ headers
38 // AUTO-REMOVED #include <iostream>
39 #include <map>
40 
41 namespace protocols {
42 namespace genetic_algorithm {
43 
44 /// Entity element
45 
47 public:
48  typedef core::Size Size;
50 
51 public:
52  EntityElement();
54  EntityElement( std::string & word ); // This constructor nibbles away at the input string
55  virtual ~EntityElement();
56 
57  virtual EntityElementOP clone() = 0;
58  virtual EntityElementOP fresh_instance() = 0;
59 
60  Size index() const;
61  void index( Size index );
62  virtual Size hash() const = 0;
63 
64  virtual bool operator < ( EntityElement const & rhs ) const;
65  virtual bool operator == ( EntityElement const & rhs ) const;
66  virtual EntityElement const & operator = ( EntityElement const & rhs );
67 
68  virtual std::string to_string() const;
69  virtual std::string name() const = 0; // Each entity element must have a distinct name
70 
71 private:
73 };
74 
75 /// Entity element creator
76 
78 public:
79  virtual ~EntityElementCreator();
80  virtual std::string widget_name() const = 0;
81  virtual EntityElementOP new_entity( std::string const & word ) = 0;
82 };
83 
84 /// Entity element factory
85 
86 class EntityElementFactory : public utility::factory::WidgetFactory< EntityElementCreator > {
87 public:
88  virtual ~EntityElementFactory() {};
89 
91 
93 
94  virtual std::string factory_name() const;
95 
96  //void register_creator( EntityElementCreatorOP creator );
97 
98 private:
101 
102 };
103 
104 template < class T >
105 class EntityElementRegistrator : public utility::factory::WidgetRegistrator< EntityElementFactory, T > {
106 public:
107  typedef typename utility::factory::WidgetRegistrator< EntityElementFactory, T > parent;
108 public:
110 };
111 
112 
113 /// Entity: a vector of EntityElements used to describe the state for a system
114 /// under optimization.
115 
117 
118 public:
124 
125 public:
126  Entity();
127  ////@brief construct a duplicate Entity from another entity
128  Entity( Entity const & entity );
129  Entity const & operator = ( Entity const & );
130 
131  virtual ~Entity();
132 
133  ////@brief construct Entity from std::string (e.g. from file)
134  Entity( std::string const & line );
135 
136  virtual OP clone() const;
137  virtual void set_traits_size( core::Size size /*, EntityElementOP element*/ );
138  virtual void set_traits( EntityElements const & traits );
139  virtual EntityElements const & traits() const;
140  virtual void set_entity_element( core::Size index, EntityElementOP element );
141  virtual void set_fitness( core::Real val );
142  virtual core::Real fitness() const;
143  virtual bool fitness_valid() const;
144  virtual bool operator == ( Entity const & other ) const;
145  virtual bool operator < ( Entity const & other ) const;
146  virtual void show( std::ostream & os ) const;
147 
148  virtual void write_checkpoint( std::ostream & os ) const;
149  virtual bool read_checkpoint( std::istream & is );
150 
151 private:
155 };
156 
157 std::ostream & operator << ( std::ostream & os, Entity const & entity );
158 
159 
160 ///@brief for sorting owning pointers by that to which they point
161 template <typename T>
165 )
166 {
167  if ( !a && !b ) return false;
168  if ( !a ) return true;
169  if ( !b ) return false;
170  return *a < *b;
171 }
172 
173 ///@brief for assessing equality between owning pointers by that to which they point
174 template <typename T>
178 )
179 {
180  if ( !a && !b ) return true;
181  if ( !a || !b ) return false;
182  return *a == *b;
183 }
184 
185 struct
186 Vec1Hash {
187  std::size_t operator() ( EntityElements const & vec1 ) const {
188  std::size_t seed = 0;
189  for ( EntityElements::const_iterator
190  iter = vec1.begin(), iter_end = vec1.end(); iter != iter_end; ++iter ) {
191  boost::hash_combine( seed, (*iter)->hash() );
192  }
193  return seed;
194  }
195 };
196 
197 struct
199  bool operator() (
200  EntityElements const & elems1,
201  EntityElements const & elems2
202  ) const {
203  if ( elems1.size() != elems2.size() ) return false;
204  for ( core::Size ii = 1; ii <= elems1.size(); ++ii ) {
205  if ( ! ((*elems1[ ii ]) == (*elems2[ ii ])) ) return false;
206  }
207  return true;
208  }
209 };
210 
211 } // namespace genetic_algorithm
212 } // namespace protocols
213 
214 #endif