00001 #ifndef __INPUTGENERATORDISCRETE_H 00002 #define __INPUTGENERATORDISCRETE_H 00003 00004 #include "IInputGenerator.hpp" 00005 00006 #include <fstream> 00007 #include <iostream> 00008 #include <Vector.hpp> 00009 00014 class RawReader 00015 { 00016 public: 00017 00018 //-------- 00019 class Point 00020 { 00021 public: 00022 Vector m_pos; 00023 Vector m_class; 00024 }; 00025 00026 typedef std::vector<Point> Points; 00027 00028 //-------- 00029 RawReader(std::string const & filename, int width, int height) 00030 { 00031 m_fileName = filename; 00032 m_width = width; 00033 m_height = height; 00034 } 00035 00036 //-------- 00037 Points * Convert() 00038 { 00039 std::ifstream * file = new std::ifstream(m_fileName.c_str()); 00040 00041 int numClasses = 0; 00042 while(! file->eof() ) 00043 { 00044 int data = file->get(); 00045 if(data > numClasses) 00046 numClasses = data; 00047 } 00048 00049 file->close(); 00050 delete file; 00051 file = 0; 00052 00053 file = new std::ifstream(m_fileName.c_str()); 00054 00055 Points * points = new Points; 00056 unsigned int index = 0; 00057 while(! file->eof() ) 00058 { 00059 int data = file->get(); 00060 00061 if(data > 0) 00062 { 00063 double x = ((double)(index % m_width) / (double)m_width) * 2.0 - 1.0; 00064 double y = ((double)((int)(index / m_height)) / (double)m_height) * 2.0 - 1.0; 00065 00066 Point point; 00067 point.m_pos.push_back( x ); 00068 point.m_pos.push_back( y ); 00069 00070 // create the output vector 00071 for(int i=0; i<numClasses; i++) 00072 point.m_class.push_back(0.0); 00073 00074 point.m_class[data-1]= 1.0; 00075 00076 points->push_back(point); 00077 } 00078 index++; 00079 } 00080 00081 std::cerr << "Number of Discrete points:" << points->size() << std::endl; 00082 return points; 00083 } 00084 00085 protected: 00086 int m_width; 00087 int m_height; 00088 std::string m_fileName; 00089 }; 00090 00091 00092 00093 class InputGeneratorDiscrete : public IInputGenerator 00094 { 00095 public: 00096 InputGeneratorDiscrete(RawReader::Points * points) 00097 { 00098 m_points = points; 00099 m_dimension = 2; 00100 m_min = 0.0; 00101 m_max = 1.0; 00102 00103 for(unsigned int i=0; i<(*points)[0].m_pos.size(); i++) 00104 m_input.push_back(0.0); 00105 00106 for(unsigned int i=0; i<(*points)[0].m_class.size(); i++) 00107 m_output.push_back(0.0); 00108 } 00109 00113 virtual Vector GetInput() 00114 { 00115 unsigned int index = (unsigned int)(Random::RandomizePositive() * m_points->size()); 00116 if(index == m_points->size()) 00117 index--; 00118 00119 m_input = (*m_points)[index].m_pos; 00120 m_output = (*m_points)[index].m_class; 00121 00122 return m_input; 00123 } 00124 00128 virtual Vector GetOutput() 00129 { 00130 return m_output; 00131 } 00132 00136 virtual Vector const & GetLastInput() const { return m_input; } 00137 00141 virtual unsigned int GetDimension() const { return m_dimension; } 00142 00143 virtual RawReader::Points const & GetPoints() { return *m_points; } 00144 00148 virtual GeneratorType GetGeneratorType() const { return ClassesGenerator; } 00149 00150 protected: 00151 unsigned int m_dimension; 00152 double m_min; 00153 double m_max; 00154 Vector m_input; 00155 RawReader::Points * m_points; 00156 }; 00157 00158 00162 class InputRepresentationDiscrete : public IGLInputRepresentation 00163 { 00164 public: 00165 InputRepresentationDiscrete(InputGeneratorDiscrete * i) : m_inputGenerator(*i) {} 00166 ~InputRepresentationDiscrete() {} 00167 00168 virtual void GLOutputDistribution() const 00169 { 00170 RawReader::Points const & p = m_inputGenerator.GetPoints(); 00171 glBegin(GL_POINTS); 00172 { 00173 for(unsigned int i=0; i<p.size(); i++) 00174 { 00175 unsigned int classIndex=0; 00176 for(; classIndex<p[0].m_class.size(); classIndex++) 00177 if(p[i].m_class[classIndex] == 1.0) 00178 break; 00179 00180 glColor4f(0.5 + ((double)classIndex)*0.2, 0.5 + ((double)classIndex)*0.2, 0.4, 1.0); 00181 00182 double x = p[i].m_pos[0]; 00183 double y = p[i].m_pos[1]; 00184 00185 glVertex2d(x, y); 00186 } 00187 } 00188 glEnd(); 00189 } 00190 00191 virtual void GLOutputSignal() const 00192 { 00193 Vector const & v = m_inputGenerator.GetLastInput(); 00194 glBegin(GL_POINTS); 00195 { 00196 glVertex2d(v[0], v[1]); 00197 } 00198 glEnd(); 00199 } 00200 00201 private: 00202 InputGeneratorDiscrete & m_inputGenerator; 00203 }; 00204 00205 #endif