Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Chunk.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 protocols/nonlocal/Chunk.cc
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 // Unit headers
15 
16 // External headers
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/accumulators/accumulators.hpp>
19 #include <boost/accumulators/statistics/mean.hpp>
20 #include <boost/accumulators/statistics/stats.hpp>
21 #include <boost/accumulators/statistics/variance.hpp>
22 #include <boost/math/distributions/normal.hpp>
23 
24 // Utility headers
25 #include <numeric/random/DistributionSampler.hh>
26 #include <utility/pointer/ReferenceCount.hh>
27 
28 // Project headers
29 #include <core/types.hh>
31 
32 // Package headers
34 
35 namespace protocols {
36 namespace nonlocal {
37 
38 // @brief Auto-generated virtual destructor
40 
41 /// @brief A small, positive value added to the standard deviation of each chunk.
42 /// The distribution requires its standard deviation to be strictly positive.
43 #define SALT 0.00000001
44 
45 /// @brief A multiplicative factor applied to control the width of the normal
46 /// distribution. Affects the width of the distribution.
47 #define SD_MULTIPLIER 1.0
48 
49 typedef boost::math::normal Normal;
50 using core::Size;
53 
54 // -- Construction and Assignment -- //
55 //
56 // Remember: scoped_ptr's aren't copyable. We need to allocate new memory for
57 // <sampler_> in the copy constructor and assignment operator.
58 
59 Chunk::Chunk(const RegionOP& region, const MoveMapOP& movable)
60  : ReferenceCount(), region_(region), movable_(movable) {
61  using namespace boost::accumulators;
62 
63  // compute normal distribution parameters
64  accumulator_set<Size, stats<tag::mean, tag::variance> > acc;
65  for (Size i = start(); i <= stop(); ++i)
66  acc(i);
67 
68  double mu = mean(acc);
69  double sigma = (std::sqrt(variance(acc)) * SD_MULTIPLIER) + SALT;
70  Normal dist(mu, sigma);
71  sampler_.reset(new numeric::random::DistributionSampler<Normal>(dist));
72 }
73 
74 Chunk::Chunk(const Chunk& other)
75  : ReferenceCount(), region_(other.region_), movable_(other.movable_) {
76  Normal dist = other.sampler_->distribution();
77  sampler_.reset(new numeric::random::DistributionSampler<Normal>(dist));
78 }
79 
80 Chunk& Chunk::operator=(const Chunk& other) {
81  // check for self-assignment
82  if (this == &other)
83  return *this;
84 
85  region_ = other.region_;
86  movable_ = other.movable_;
87  Normal dist = other.sampler_->distribution();
88  sampler_.reset(new numeric::random::DistributionSampler<Normal>(dist));
89  return *this;
90 }
91 
92 // -- Accessors -- //
93 
95  assert(valid());
96  while (1) {
97  Size insert_pos = static_cast<Size>(sampler_->sample());
98 
99  // restrict samples to the closed interval [start(), stop()]
100  if (insert_pos < start() || insert_pos > stop())
101  continue;
102 
103  if (movable_->get_bb(insert_pos))
104  return insert_pos;
105  }
106  return 0;
107 }
108 
110  return region_->start();
111 }
112 
113 Size Chunk::stop() const {
114  return region_->stop();
115 }
116 
118  return region_->length();
119 }
120 
121 // -- Utility Functions -- //
122 
123 bool Chunk::is_movable() const {
124  for (Size i = start(); i <= stop(); ++i) {
125  if (movable_->get_bb(i))
126  return true;
127  }
128 
129  return false;
130 }
131 
132 bool Chunk::valid() const {
133  // Region's are agnostic as to their direction: start => stop, stop <= start.
134  // For simplicity, our iteration methods assume a left-to-right orientation
135  if (start() >= stop())
136  return false;
137 
138  // In order to avoid an infinite loop in choose(), at least one position on
139  // the interval [start(), stop()] must be movable. Short-circuit evaluation.
140  return is_movable();
141 }
142 
143 } // namespace nonlocal
144 } // namespace protocols