Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EntityRandomizer.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 EntityRandomizer.hh
11 /// @brief controls the alteration of the traits that define Entity
12 /// @author ashworth
13 
14 // Unit headers
16 
18 
19 #include <utility/pointer/ReferenceCount.hh>
20 
21 #include <core/types.hh>
22 // AUTO-REMOVED #include <basic/Tracer.hh>
23 #include <utility/vector1.hh>
24 #include <numeric/random/random.hh>
25 
26 #include <numeric/random/random.fwd.hh>
27 
28 #include <algorithm> // std::copy
29 
30 #include <utility/exit.hh>
31 
32 
33 namespace protocols {
34 namespace genetic_algorithm {
35 
36 EntityRandomizer::EntityRandomizer() : utility::pointer::ReferenceCount(), entity_length_(0), mutation_rate_(1.), entity_template_(0) {}
40 
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////////////////////////
45 
47 void DiscreteRandomizer::add_choice( EntityElementOP const & choice ) { choices_.push_back( choice ); }
50 
51 ////////////////////////////////////////////////////////////////////////////////////////////////////
55 
56 ////////////////////////////////////////////////////////////////////////////////////////////////////
57 // method definitions
58 
61 {
62  runtime_assert( entity_length_ );
63  EntityOP entity;
64  if (entity_template_) {
65  entity = entity_template_->clone();
66  } else {
67  entity = new Entity;
68  }
69  entity->set_traits_size( entity_length_ );
70  // temporarily increase mutation rate to 100%
71  core::Real current_rate( mutation_rate_ );
72  set_mutation_rate( 1. );
73  mutate( *entity );
74  // restore mutation rate
75  set_mutation_rate( current_rate );
76 
77  return entity;
78 }
79 
80 /// @brief randomly swap [1, N-1] traits between two entities
81 void
82 EntityRandomizer::crossover( Entity & entity1, Entity & entity2 )
83 {
84  EntityElements traits1( entity1.traits() ), traits2( entity2.traits() );
85 
86  // track of the total number of traits that can potentially be swapped
87  core::Size num_remaining(traits1.size());
88  // pick number of traits to swap that is in the interval [1, N-1]
89  core::Size num_to_swap(numeric::random::random_range(1, traits1.size()-1));
90 
91  for ( EntityElements::iterator
92  it1( traits1.begin() ), it2( traits2.begin() ),
93  end1( traits1.end() ), end2( traits2.end() );
94  (it1 != end1) && (it2 != end2); ++it1, ++it2 ) {
95  core::Real probability_of_swap = static_cast<core::Real>(num_to_swap)/static_cast<core::Real>(num_remaining);
96  if ( probability_of_swap >= numeric::random::uniform() ) {
97  EntityElementOP temp = *it1;
98  *it1 = *it2;
99  *it2 = temp;
100  --num_to_swap;
101  }
102  --num_remaining;
103  }
104 
105  entity1.set_traits(traits1);
106  entity2.set_traits(traits2);
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////////////////////////
110 void
112 {
113  EntityElements traits( entity.traits() );
114  core::Size const size_choices( choices_.size() );
115  for ( EntityElements::iterator it( traits.begin() ), end( traits.end() );
116  it != end; ++it ) {
117  if ( this->mutation_rate() < numeric::random::uniform() ) continue;
118  *it = choices_[ static_cast< core::Size >( numeric::random::uniform() * size_choices ) + 1 ];
119  }
120  entity.set_traits(traits);
121 }
122 
125 {
126  core::Size size( choices_.size() );
127  for ( core::Size i(1), e( this->entity_length() ); i < e; ++i ) size *= size;
128  return size;
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////////////////////////
132 void
134 {
135  choices_.push_back( choices );
136  this->set_entity_length( choices_.size() );
137 }
138 
139 void
141 {
142  EntityElements traits( entity.traits() );
143  assert( traits.size() == choices_.size() );
145  for ( EntityElements::iterator
146  it( traits.begin() ), end( traits.end() ); it != end;
147  ++it, ++choice_it ) {
148  if ( this->mutation_rate() < numeric::random::uniform() ) continue;
149  core::Size const size_choices( choice_it->size() );
150  *it = (*choice_it)[ static_cast< core::Size >( numeric::random::uniform() * size_choices ) + 1 ];
151  }
152  entity.set_traits(traits);
153 }
154 
157 {
158  core::Size size( choices_.front().size() );
160  it( choices_.begin() ), end( choices_.end() ); it != end; ++it ) {
161  if ( it == choices_.begin() ) continue;
162  size *= it->size();
163  }
164  return size;
165 }
166 
167 } // namespace genetic_algorithm
168 } // namespace protocols
169