Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Entity.cc
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 
15 
16 #include <core/types.hh>
17 
18 #include <utility/exit.hh>
19 #include <utility/pointer/ReferenceCount.hh>
20 #include <utility/pointer/owning_ptr.hh>
21 // AUTO-REMOVED #include <utility/pointer/access_ptr.hh>
22 // AUTO-REMOVED #include <utility/vector1.hh>
23 
24 #include <boost/functional/hash.hpp> // hash_range
25 
26 #include <ObjexxFCL/format.hh>
27 using namespace ObjexxFCL;
28 
29 #include <iostream>
30 #include <sstream>
31 
32 #include <utility/vector1.hh>
33 
34 
35 namespace protocols {
36 namespace genetic_algorithm {
37 
38 EntityElement::EntityElement() : index_( 0 ) {}
39 EntityElement::EntityElement( Size index ) : parent(), index_( index ) {}
41 {
42  if ( word == "" ) {
43  index_ = 0;
44  } else {
45  Size colon_pos = word.find( ":" );
46  if ( colon_pos == std::string::npos ) {
47  std::cerr << "ERROR: Could not find colon (:) in input word while constructing EntityElement. word='" << word << "'" << std::endl;
48  utility_exit_with_message( "EntityElement constructor failure" );
49  } else {
50  std::string indstring = word.substr( 0, colon_pos );
51  std::istringstream iss( indstring );
52  iss >> index_;
53  if ( colon_pos + 1 < word.size() ) {
54  word = word.substr( colon_pos + 1 );
55  } else {
56  word = "";
57  }
58  }
59  }
60 }
61 
63 
64 void EntityElement::index( Size ind ) { index_ = ind; }
66 
67 bool
69  return index_ < rhs.index_;
70 }
71 
72 bool
74  return index_ == rhs.index_;
75 }
76 
77 EntityElement const &
79 {
80  if ( this != &rhs ) {
81  index_ = rhs.index_;
82  }
83  return *this;
84 }
85 
87 {
88  std::ostringstream oss;
89  oss << name() << ":" << index_ << ":";
90  return oss.str();
91 }
92 
94 
95 /// Entity Element Factory
96 
98 
100 {
101  if ( ! instance_ ) {
103  }
104  return instance_;
105 }
106 
109 {
110  //std::cout << "entity element name: " << entity_element_name << std::endl;
111 
112  std::string type;
113  std::string desc;
114  core::Size colon_pos = entity_element_name.find( ":" );
115 
116  //std::cout << "colon pos: " << colon_pos << std::endl;
117 
118  if ( colon_pos != std::string::npos ) {
119  type = entity_element_name.substr( 0, colon_pos );
120  if ( colon_pos + 1 < entity_element_name.size() ) {
121  desc = entity_element_name.substr( colon_pos + 1 );
122  }
123  } else {
124  utility_exit_with_message( "Invalid EntityElementCreator input; could not locate a colon in the string \"" + entity_element_name + "\"" );
125  }
126 
127  //std::cout << "type: " << type << " desc: " << desc << std::endl;
128 
129  CreatorMap::const_iterator iter = creators().find( type );
130  if ( iter == creators().end() ) {
131  std::cerr << "ERROR: Could not find EntityElement type " << type << " in EntityElementFactory" << std::endl;
132  utility_exit_with_message( "EntityElementFactory type lookup failure" );
133  }
134  return iter->second->new_entity( desc );
135 }
136 
137 /*
138 void EntityElementFactory::register_creator( EntityElementCreatorOP creator )
139 {
140  CreatorMap::const_iterator iter = creators_.find( creator->entity_type() );
141  if ( iter != creators_.end() ) {
142  std::cerr << "ERROR: EntityElement type " << creator->entity_type() << " has already been registered in the EntityElementFactory" << std::endl;
143  utility_exit_with_message( "EntityElementFactory type duplication failure" );
144  }
145  creators_[ creator->entity_type() ] = creator;
146 }*/
147 
148 
150  return "EntityElementFactory";
151 }
152 
154 
156  utility::pointer::ReferenceCount(),
157  fitness_(0.),
158  fitness_valid_(false)
159 {}
160 
162 {
163  std::istringstream linestream( line );
164  if ( !read_checkpoint(linestream) ) utility_exit_with_message( "invalid string " + line );
165 }
166 
167 Entity::Entity( Entity const & entity ) :
168  utility::pointer::ReferenceCount(),
169  traits_( entity.traits_.size() ),
170  fitness_( entity.fitness_ ),
171  fitness_valid_( entity.fitness_valid_ )
172 {
173  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
174  if ( entity.traits_[ ii ] ) {
175  traits_[ ii ] = entity.traits_[ ii ]->clone();
176  } else {
177  traits_[ ii ] = 0;
178  }
179  }
180 }
181 
182 
183 Entity const &
184 Entity::operator = ( Entity const & rhs ) {
185  if ( this != & rhs ) {
186  if ( traits_.size() == rhs.traits_.size() ) {
187  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
188  if ( rhs.traits_[ ii ] && traits_[ ii ] ) {
189  (*traits_[ ii ]) = (*rhs.traits_[ ii ] ); // polymorphic assignment operator; assumption: both traits are the same type!
190  } else {
191  traits_[ ii ] = rhs.traits_[ ii ]->clone();
192  }
193  }
194  } else {
195  traits_.resize( rhs.traits_.size() );
196  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
197  if ( rhs.traits_[ ii ] ) {
198  traits_[ ii ] = rhs.traits_[ ii ]->clone();
199  } else {
200  traits_[ ii ] = 0;
201  }
202  }
203  }
204  }
205  return *this;
206 }
207 
208 
210 
211 Entity::OP Entity::clone() const { return new Entity(*this); }
212 
214  core::Size size/*,
215  EntityElementOP element*/
216 )
217 {
218  traits_.resize(size);
219  /*
220  for ( Size ii = 1; ii <= size; ++ii ) {
221  traits_[ ii ] = element->fresh_instance();
222  traits_[ ii ]->index( ii );
223  }*/
224  fitness_valid_ = false;
225 }
226 void Entity::set_traits( EntityElements const & traits )
227 {
228  if ( traits_.size() == traits.size() ) {
229  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
230  if ( traits_[ ii ] ) {
231  *traits_[ ii ] = *traits[ ii ]; /// Assumption: both traits are the same type.
232  } else {
233  traits_[ ii ] = traits[ ii ]->clone();
234  }
235  }
236  } else {
237  traits_.resize( traits.size() );
238  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
239  traits_[ ii ] = traits[ ii ]->clone();
240  }
241  }
242  fitness_valid_ = false;
243 }
244 
246  core::Size index,
247  EntityElementOP element
248 )
249 {
250  traits_[ index ] = element->clone();
251  fitness_valid_ = false;
252 }
253 
254 EntityElements const &
255 Entity::traits() const {
256  return traits_;
257 }
258 
260  fitness_ = val;
261  fitness_valid_ = true;
262 }
263 
265  return fitness_;
266 }
267 
268 bool Entity::fitness_valid() const {
269  return fitness_valid_;
270 }
271 
272 bool Entity::operator == ( Entity const & other ) const
273 {
274  if ( traits_.size() != other.traits_.size() ) return false;
275 
276  for ( Size ii = 1; ii <= traits_.size(); ++ii ) {
277  if ( ! traits_[ ii ] && ! other.traits_[ ii ] ) {
278  continue; /// apl -- this really shouldn't happen
279  }
280  if ( ! traits_[ ii ] || ! other.traits_[ ii ] ) {
281  return false; /// apl -- this really shouldn't happen
282  }
283  if ( ! ( *traits_[ ii ] == *other.traits_[ ii ] ) ) {
284  return false;
285  }
286  }
287  return true;
288 }
289 
290 bool Entity::operator < ( Entity const & other ) const
291 {
292  return ( fitness_ < other.fitness() );
293 }
294 
295 void Entity::show( std::ostream & os ) const
296 {
297  os << "Entity with traits:";
298  EntityElements const & seq( this->traits() );
299  for ( EntityElements::const_iterator
300  it( seq.begin() ), end( seq.end() );
301  it != end; ++it ) {
302  os << " " << (*it)->to_string();
303  }
304  os << " and fitness " << fmt::F(6,3,this->fitness());
305 }
306 
307 void
309  std::ostream & os
310 ) const
311 {
312  os << "traits";
313  for ( EntityElements::const_iterator
314  key( traits_.begin() ), end( traits_.end() ); key != end; ++key ) {
315  os << " " << (*key)->to_string();
316  }
317  os << " fitness " << fitness_;
318 }
319 
320 bool
322  std::istream & is
323 )
324 {
325  std::string word;
326  if (!(is >> word)) return false;
327  if ( word != "traits" ) return false;
328  while ( is >> word ) {
329  if ( word == "fitness" ) break;
330  traits_.push_back( EntityElementFactory::get_instance()->element_from_string( word ) );
331  }
332  if ( is >> fitness_) {
333  fitness_valid_ = true;
334  return true;
335  }
336  return false;
337 }
338 
339 std::ostream & operator << ( std::ostream & os, Entity const & entity )
340 {
341  entity.show(os);
342  return os;
343 }
344 
345 
346 } // namespace genetic_algorithm
347 } // namespace protocols
348