PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: RealMapping.h 9146 2008-06-18 20:46:01Z nouiz $ 00038 * This file is part of the PLearn library. 00039 ******************************************************* */ 00040 00041 #ifndef RealMapping_INC 00042 #define RealMapping_INC 00043 00044 //#include "general.h" 00045 #include "Object.h" 00046 #include <plearn/math/TMat.h> 00047 #include <map> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00053 class RealRange 00054 { 00055 public: 00056 real low; 00057 real high; 00058 char leftbracket; // either '[' (inclusive left) or ']' (exclusive left) 00059 char rightbracket; // // either '[' (exclusive right) or ']' (inclusive right) 00060 00061 public: 00062 RealRange(): // default construvtor 00063 low(0), high(0), leftbracket(']'), rightbracket('[') 00064 {} 00065 00066 00067 RealRange(char leftbracket_, real low_, real high_, char rightbracket_): 00068 low(low_), high(high_), leftbracket(leftbracket_), rightbracket(rightbracket_) 00069 { checkbrackets(); } 00070 00071 real span(){return abs(high-low);} 00072 00073 void checkbrackets() const 00074 { 00075 if( (leftbracket!='[' && leftbracket!=']') || (rightbracket!='[' && rightbracket!=']') ) 00076 PLERROR("In RealRange: Brackets must be either '[' or ']'"); 00077 } 00078 00079 void print(ostream& out) const 00080 { out << leftbracket << low << ' ' << high << rightbracket; } 00081 00082 void write(ostream& out) const 00083 { out << leftbracket << low << ' ' << high << rightbracket; } 00084 00085 void read(PStream& in) 00086 { in >> leftbracket >> low >> high >> rightbracket; checkbrackets(); } 00087 00088 00089 string getString() const; 00090 00096 bool contains(real val) const; 00097 bool operator<(real x) const; 00098 bool operator>(real x) const; 00099 00100 /* inline bool operator<(real x) const 00101 { return low < x || high == x && rightbracket == '['; } 00102 00103 inline bool operator>(real x) const 00104 { return low > x || low == x && leftbracket == ']'; } 00105 00106 */ 00111 bool operator<(const RealRange& x) const; 00112 bool operator>(const RealRange& x) const; 00113 bool operator==(const RealRange& rr) const; 00114 }; 00115 00116 inline void write(ostream& out, const RealRange& range) { range.write(out); } 00117 inline ostream& operator<<(ostream& out, const RealRange& range) { range.print(out); return out; } 00118 inline void read(PStream& in, RealRange& range) { range.read(in); } 00119 00120 PStream& operator<<(PStream& out, const RealRange& x); 00121 PStream& operator>>(PStream& in, RealRange &x); 00122 00123 00136 class RealMapping: public Object 00137 { 00138 typedef Object inherited; 00139 00140 public: 00141 typedef pair<RealRange, real> single_mapping_t; 00142 typedef TVec< single_mapping_t > ordered_mapping_t; 00143 typedef std::map<RealRange, real> mapping_t; 00144 typedef mapping_t::iterator iterator; 00145 typedef mapping_t::const_iterator const_iterator; 00146 00148 mapping_t mapping; 00149 00151 real missing_mapsto; 00152 00155 bool keep_other_as_is; 00156 00159 real other_mapsto; 00160 00166 ordered_mapping_t o_mapping; 00167 00168 public: 00169 PLEARN_DECLARE_OBJECT(RealMapping); 00170 00171 RealMapping() 00172 :missing_mapsto(MISSING_VALUE), 00173 keep_other_as_is(true), 00174 other_mapsto(MISSING_VALUE) 00175 {} 00176 00177 int size() const { return (int)mapping.size(); } 00178 int length() const { return (int)mapping.size(); } 00179 00181 inline void clear() { mapping.clear(); } 00182 00183 void buildOrderedMapping(); 00184 00185 bool checkConsistency(); 00186 00187 void removeMapping(const RealRange& range) 00188 { 00189 mapping_t::iterator it= mapping.find(range); 00190 if(it != mapping.end()) 00191 mapping.erase(it); 00192 else 00193 PLWARNING("In RealMapping::removeMapping mapping not removed: does not exist."); 00194 } 00195 00196 void removeMapping(real x) //remove range where x falls 00197 { 00198 mapping_t::iterator it= mapping.lower_bound(RealRange('[',x,x,']')); 00199 if(it != mapping.end() && it->first.contains(x)) 00200 mapping.erase(it); 00201 else 00202 PLWARNING("In RealMapping::removeMapping mapping not removed: does not exist."); 00203 } 00204 00205 void addMapping(const RealRange& range, real val); 00206 00208 void setMappingForMissing(real what_missing_mapsto) 00209 { missing_mapsto = what_missing_mapsto; } 00210 00212 void setMappingForOther(real what_other_mapsto) 00213 { keep_other_as_is=false; other_mapsto = what_other_mapsto; } 00214 00215 void keepOtherAsIs() 00216 { keep_other_as_is=true; } 00217 00218 // returns the mapped value corresponding to val 00219 real map(real val) const; 00220 00221 // returns the number of the bin in which 'val' falls 00222 int binnumber(real val) const; 00223 00224 // transforms v by applying the mapping on all its elements 00225 void transform(const Vec& v) const; 00226 00227 pair<RealRange, real> lastMapping() 00228 { return *(mapping.rbegin()); } 00229 // { return mapping.lastElement(); } 00230 00231 /*** 00232 * map methods "forwarded" 00233 */ 00234 00235 iterator begin() 00236 { return mapping.begin(); } 00237 const_iterator begin() const 00238 { return mapping.begin(); } 00239 iterator end() 00240 { return mapping.end(); } 00241 const_iterator end() const 00242 { return mapping.end(); } 00243 void erase(iterator it) 00244 { mapping.erase(it); } 00245 00246 bool operator==(const RealMapping& rm) const; 00247 00249 virtual void newwrite(PStream& out) const; 00250 00251 virtual void print(ostream& out) const; 00252 virtual void write(ostream& out) const; 00253 virtual void read(PStream& in); 00254 00255 real maxMappedToValue(); 00256 00259 Vec getCutPoints() const; 00260 00261 protected: 00262 static void declareOptions(OptionList& ol); 00263 }; 00264 00265 DECLARE_OBJECT_PTR(RealMapping); 00266 00267 } // end of namespace PLearn 00268 00269 #endif 00270 00271 00272 /* 00273 Local Variables: 00274 mode:c++ 00275 c-basic-offset:4 00276 c-file-style:"stroustrup" 00277 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00278 indent-tabs-mode:nil 00279 fill-column:79 00280 End: 00281 */ 00282 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :