faif
Point.hpp
1 #ifndef FAIF_POINT_HPP_
2 #define FAIF_POINT_HPP_
3 
4 #include <ostream>
5 #include <vector>
6 #include <list>
7 #include <algorithm>
8 #include <utility>
9 
10 #include <boost/bind.hpp>
11 
12 #include <boost/serialization/serialization.hpp>
13 #include <boost/serialization/list.hpp>
14 #include <boost/serialization/nvp.hpp>
15 #include <boost/serialization/vector.hpp>
16 
17 #include "Value.hpp"
18 
19 namespace faif {
20 
21  /** \brief Point in n-space, each component of the same type */
22  template<typename Val> class Point : public std::vector<typename Val::DomainType::ValueId> {
23  BOOST_CONCEPT_ASSERT((ValueConcept<Val>));
24 
25  public:
26  /** \brief serialization using boost::serialization */
27  template<class Archive>
28  void serialize(Archive & ar, const unsigned int /* file_version */){
29  ar & boost::serialization::make_nvp("PointVect",
30  boost::serialization::base_object< std::vector<typename Val::DomainType::ValueId> >(*this) );
31  }
32  };
33 
34  /** stream operator - calls the write method */
35  template<typename Val>
36  std::ostream& operator<<(std::ostream& os, const Point<Val>& p) {
37  for(typename std::vector<typename Val::DomainType::ValueId>::const_iterator i = p.begin(); i != p.end(); ++i) {
38  os << **i << ' ';
39  }
40  return os;
41  }
42 
43  /**
44  \brief feature init policy - use default constructor
45  */
46  template<typename Feature> struct FeatureInitDefault {
47  static Feature init() {
48  return Feature();
49  }
50  };
51 
52  /** \brief point and some feature
53 
54  examples: train example or point with fitness
55  */
56  template<typename Value, typename Feature,
57  template <typename> class FeatureInit = FeatureInitDefault >
58  class PointAndFeature : public Point<Value> {
59  BOOST_CONCEPT_ASSERT((ValueConcept<Value>));
60  public:
61  PointAndFeature() : Point<Value>(), feature_(FeatureInit<Feature>::init()) { }
62 
63  PointAndFeature(const Point<Value>& p, Feature f) : Point<Value>(p), feature_(f) { }
64 
65  PointAndFeature(const PointAndFeature<Value, Feature, FeatureInit>& pf) : Point<Value>(pf), feature_(pf.feature_) { }
68  feature_ = right.feature_;
69  return *this;
70  }
71  Feature getFeature() const { return feature_; }
72 
73  /** \brief serialization using boost::serialization */
74  template<class Archive>
75  void serialize(Archive & ar, const unsigned int /* file_version */){
76  ar & boost::serialization::make_nvp("Point", boost::serialization::base_object< Point<Value> >(*this) );
77  ar & boost::serialization::make_nvp("Feature", feature_ );
78  }
79  private:
80  Feature feature_;
81  };
82 
83 
84  /** ostream operator - for debugging */
85  template <typename Value, typename Feature, template <typename> class FeatureInit >
86  std::ostream& operator<<(std::ostream& os, const PointAndFeature<Value, Feature, FeatureInit>& pf) {
87  os << static_cast<const Point<Value>&>(pf) << ": " << pf.getFeature();
88  return os;
89  }
90 
91  /** \brief Space n-dimensional, each domain of the same type */
92  template<typename Domain> class Space : public std::list<Domain> {
93  BOOST_CONCEPT_ASSERT((DomainConcept<Domain>));
94 
95  typedef typename Domain::Value Value;
96  public:
97  Space() {}
98 
99  /** \brief create the test example from iterator range or C-like table of values */
100  template<typename It>
101  Point<Value> createPoint(It begin_range, It end_range) const {
102  Point<Value> point;
103  typename Space::const_iterator j = this->begin();
104  for(It i = begin_range; i != end_range && j != this->end(); ++i, ++j) {
105  point.push_back( j->find(*i) );
106  }
107  return point;
108  }
109 
110  /** \brief create the test example from collection of pairs: attribute(domain) identifier and attribute value */
111  Point<Value> createPoint(const std::vector<std::pair<std::string, typename Value::Value> >& collection) const {
112  typedef std::pair<std::string, typename Value::Value> AttrValPair;
113  typedef std::vector<AttrValPair> VecAttrValPair;
114 
115  Point<Value> point;
116  for(typename Space::const_iterator j = this->begin(); j != this->end(); ++j ) {
117  const typename Value::DomainType& d = *j;
118  typename VecAttrValPair::const_iterator k = std::find_if(collection.begin(), collection.end(),
119  boost::bind(&AttrValPair::first, _1) == boost::bind(&Domain::getId, d) );
120 
121  typename Domain::ValueId id = Domain::getUnknownId(); //if not found given domain id the 'unknown' is inserted
122  if( k != collection.end() ) {
123  try {
124  id = d.find( k->second );
125  } catch(NotFoundException&) //if not found value the 'unknown' is inserted
126  { }
127  }
128  point.push_back(id);
129  }
130  return point;
131  }
132 
133  /** \brief create the test example from collection of pairs: attribute(domain) identifier and attribute value
134  \throws NotFoundException if domain id is not correct or atribute value is not correct */
135  Point<Value> createPointStrict(const std::vector<std::pair<std::string, typename Value::Value> >& collection) const {
136  typedef std::pair<std::string, typename Value::Value> AttrValPair;
137  typedef std::vector<AttrValPair> VecAttrValPair;
138 
139  Point<Value> point;
140  for(typename Space::const_iterator j = this->begin(); j != this->end(); ++j ) {
141  const typename Value::DomainType& d = *j;
142  typename VecAttrValPair::const_iterator k = std::find_if(collection.begin(), collection.end(),
143  boost::bind(&AttrValPair::first, _1) == boost::bind(&Domain::getId, d) );
144  if( k == collection.end() )
145  throw NotFoundException( d.getId().c_str() );
146  point.push_back( d.find( k->second ) ); //find throws NotFoundException if fails finding the value
147  }
148  return point;
149  }
150 
151  };
152 
153 
154 } //namespace faif
155 
156 #endif //FAIF_POINT_HPP_
Definition: Chain.h:17
the domain concept
Definition: Value.hpp:140
point and some feature
Definition: Point.hpp:58
feature init policy - use default constructor
Definition: Point.hpp:46
Space n-dimensional, each domain of the same type.
Definition: Point.hpp:92
Point< Value > createPoint(const std::vector< std::pair< std::string, typename Value::Value > > &collection) const
create the test example from collection of pairs: attribute(domain) identifier and attribute value ...
Definition: Point.hpp:111
Point in n-space, each component of the same type.
Definition: Point.hpp:22
void serialize(Archive &ar, const unsigned int)
serialization using boost::serialization
Definition: Point.hpp:28
the value concept
Definition: Value.hpp:41
Point< Value > createPoint(It begin_range, It end_range) const
create the test example from iterator range or C-like table of values
Definition: Point.hpp:101
Point< Value > createPointStrict(const std::vector< std::pair< std::string, typename Value::Value > > &collection) const
create the test example from collection of pairs: attribute(domain) identifier and attribute value ...
Definition: Point.hpp:135
void serialize(Archive &ar, const unsigned int)
serialization using boost::serialization
Definition: Point.hpp:75
the exception thrown when the value for given attribute is not found
Definition: ExceptionsFaif.hpp:32