00001
00002
00003
00004
00005
00006
00007 #ifndef IMPDOMINO_SIMPLE_DISCRETE_SAMPLER_H
00008 #define IMPDOMINO_SIMPLE_DISCRETE_SAMPLER_H
00009
00010 #include "domino_config.h"
00011 #include "DiscreteSampler.h"
00012 #include "SimpleDiscreteSpace.h"
00013 #include <IMP/Particle.h>
00014 #include <map>
00015 #include <sstream>
00016
00017
00018 IMPDOMINO_BEGIN_NAMESPACE
00019
00020 class SimpleDiscreteSampler : public DiscreteSampler
00021 {
00022 public:
00023 SimpleDiscreteSampler(){}
00024
00025 void move2state(const CombState *cs) {
00026 Particle *p;
00027 SimpleDiscreteSpace *ds;
00028 const std::vector<FloatKey> *atts;
00029 for (std::map<Particle *,unsigned int>::const_iterator
00030 it = cs->get_data()->begin();it != cs->get_data()->end(); it++) {
00031 p = it->first;
00032 ds = data_.find(p)->second;
00033 atts = ds->get_att_keys();
00034 for (std::vector<FloatKey>::const_iterator k_iter = atts->begin();
00035 k_iter != atts->end(); k_iter++) {
00036 IMP_LOG(VERBOSE,"particle : " << p->get_value(node_name_key())
00037 << "setting value for: " << *k_iter <<" to: "
00038 << it->second <<std::endl);
00039 p->set_value(*k_iter,ds->get_state_val(it->second, *k_iter));
00040 }
00041 }
00042 }
00043 void populate_states_of_particles(Particles *particles,
00044 std::map<std::string, CombState *> *states) const
00045 {
00046 Int num_states = 1;
00047 for (Particles::const_iterator it = particles->begin();
00048 it != particles->end(); it++) {
00049 num_states *= data_.find(*it)->second->get_number_of_states();
00050 }
00051 Int global_iterator, global_index;
00052
00053 Particle* p;
00054 Int sample_size;
00055 for (Int state_index = 0;state_index < num_states; state_index++) {
00056 CombState *calc_state = new CombState();
00057 global_iterator = num_states;
00058 global_index = state_index;
00059 for (Particles::const_iterator it = particles->begin();
00060 it != particles->end(); it++) {
00061 p = *it;
00062 sample_size = data_.find(p)->second->get_number_of_states();
00063 global_iterator /= sample_size;
00064 calc_state->add_data_item(p, global_index / global_iterator);
00065 global_index -= (global_index / global_iterator) * global_iterator;
00066 }
00067 (*states)[calc_state->partial_key(particles)] = calc_state;
00068 }
00069 }
00070
00071 void show_space(Particle *p,
00072 std::ostream& out = std::cout) const{}
00073
00074 void show(std::ostream& out=std::cout) const {
00075 out << "================ show sampling spaces ============== " << std::endl;
00076 for (std::map<const Particle *,SimpleDiscreteSpace *>::const_iterator it
00077 = data_.begin(); it != data_.end(); it++) {
00078 out << " space for particle with index: "
00079 << it->first->get_value(node_name_key()) << " is : ";
00080 it->second->show(out);
00081 out << std::endl;
00082 }
00083 out << "=================================================== " << std::endl;
00084 }
00085
00086 Float get_state_val(Particle *p, unsigned int i, FloatKey key) const {
00087 return data_.find(p)->second->get_state_val(i, key);
00088 }
00089
00090 unsigned int get_space_size(Particle *p) const {
00091 return data_.find(p)->second->get_number_of_states();
00092 }
00093
00094 FloatKey get_attribute_key(Particle *p, unsigned int att_index) const {
00095 return (*(data_.find(p)->second->get_att_keys()))[att_index];
00096 }
00097
00098 unsigned int get_number_of_attributes(Particle *p) const {
00099 return data_.find(p)->second->get_number_of_attributes();
00100 }
00101
00102 void add_space(const Particle &p, SimpleDiscreteSpace &sds) {
00103 data_[&p] = &sds;
00104 }
00105 DiscreteSet* get_space(Particle *p) const {
00106 IMP_INTERNAL_CHECK(data_.find(p) != data_.end(),
00107 "Particle "<<p->get_name() <<
00108 "was not found in SimpleDiscreteSampler" <<std::endl);
00109 return data_.find(p)->second;
00110 }
00111 protected:
00112 std::map<const Particle *, SimpleDiscreteSpace *> data_;
00113 };
00114
00115 IMPDOMINO_END_NAMESPACE
00116
00117 #endif