Structural deformable models
StructTable.h
Go to the documentation of this file.
1 #ifndef _STRUCTABLE_H_
2 #define _STRUCTABLE_H_
3 
4 #include <iostream>
5 #include <string>
6 #include <map>
7 #include <set>
8 #include <list>
9 #include <vector>
10 #include "common.h"
11 #include "ParseFile.h"
12 #include "Model.h"
13 #include "ExpMap.h"
14 
15 #include "MStruct.h"
16 
17 class Brain;
18 class StructTable;
19 
20 //----------------------------------------------------------------------------
21 class StructPath : public std::map<std::string,dword>
22 {
23 public:
24  StructPath();
25  StructPath(const StructPath& rhs);
26  ~StructPath();
27  StructPath& operator=(const StructPath& rhs);
28  void clear();
29  Winner* getWinner(MStructure& struc);
30  const Winner* getWinner(const MStructure& struc) const;
31  float getGoodness() const { return m_Goodness; }
32  void protectWinners(StructTable& structs);
33  void unprotectWinners(StructTable& structs);
34  void blockOtherPaths(const std::set<std::string>& freepath,
35  const StructTable& structs);
36  std::ostream& print(std::ostream& os, const StructTable& structs) const;
37  float getRelGoodness() const
38  { return m_Maxness != 0.f ? m_Goodness/m_Maxness : 0.f; }
39  StructPath branch() const;
40  void merge(const StructPath& mp);
41 public:
42  float m_Goodness;
43  float m_Maxness;
44 protected:
46 };
47 
48 //----------------------------------------------------------------------------
49 class StructTable {
50 public:
51  typedef std::map<std::string, std::set<std::string> > Interpretations;
52  typedef std::map<dword, StructPath> NodePaths;
53 public:
54  StructTable(Brain& brain, const std::string& filename=std::string());
55  ~StructTable();
56  void clear();
57  void attachBrain(Brain& brain) { m_Brain = &brain; }
58  dataset_ptr getDataset();
59  SensorCollection* getSensors();
60  void reattachSensors();
61  void adaptDataScale();
62  bool load(const std::string& filename);
63  bool read(ParseFile& is);
64  void write(std::ostream &os) const;
66  { st.read(is); return is; }
67  friend std::ostream& operator<<(std::ostream& os, const StructTable& st)
68  { st.write(os); return os; }
69  const std::string& getFilename() const { return m_FileName; }
70  const std::string& getPath() const { return m_Path; }
71  std::map<std::string, MStructure>& getStructs() { return m_Structs; }
72  const std::map<std::string, MStructure>& getStructs() const
73  { return m_Structs; }
74  bool hasStructure(const std::string& sname) const;
75  const MStructure& getStructure(const std::string& sname) const;
76  MStructure& getNextStruct(int dir=1, bool wrap=true);
77  void evalRelations();
78  void rebuildExpMaps();
79  void draw() const;
80  void startSearch(bool dostart=true);
81  int searchDone() const {
82  if(m_State&ST_DONE) return 1;
83  if(m_State&ST_TIMEOUT) return -1; else return 0;
84  }
85  bool stepSearch(float dt);
86  bool triggerTest(int mx, int my, int what);
87  static DMatrixf& adjustByAvgDir(DMatrixf& mat);
88  float findBestConnection();
89  bool findBestConnection(StructPath& path,
90  const Winner& winner) const;
91  const StructPath& getBestPath() const { return m_BestPath; }
92  const std::string& getBestInterpretation() const { return m_BestIP; }
93  const std::map<std::string,StructPath>& getIPaths() const
94  { return m_IPaths; }
95  void setShownIP(const std::string& curip) const
96  { m_CurIP = curip; }
97  const std::string& getShownIP() const
98  { return m_CurIP.empty() ? m_BestIP : m_CurIP; }
99  bool showNextIP(int dir=1) const;
100 protected:
101  void connectSubSuper();
102  bool readInterpreations(ParseFile& is);
103 
104 protected:
105  enum TimeMarks { TM_UPDEM, TM_TIMEOUT, TM_LAST };
106  enum StateFlags { ST_RUN=1, ST_RESULT=2, ST_DONE=4, ST_TIMEOUT=16 };
107  std::map<std::string, MStructure> m_Structs;
108  std::string m_FileName;
109  std::string m_Path;
111  std::string m_CurStruct;
113  float m_Time;
115  std::vector<float> m_TMarks;
116  Interpretations m_Interpretations;
117  std::map<std::string,StructPath> m_IPaths;
118  std::string m_BestIP;
119  mutable std::string m_CurIP;
120  mutable std::map<std::string,NodePaths> m_NodePaths;
121 };
122 
123 #endif
bool read(ParseFile &is)
Definition: StructTable.cpp:78
std::shared_ptr< Dataset > dataset_ptr
Definition: types_fwd.h:19
const std::map< std::string, StructPath > & getIPaths() const
Definition: StructTable.h:93
void attachBrain(Brain &brain)
Definition: StructTable.h:57
std::string m_FileName
Definition: StructTable.h:108
void merge(const StructPath &mp)
const std::string & getPath() const
Definition: StructTable.h:70
friend std::ostream & operator<<(std::ostream &os, const StructTable &st)
Definition: StructTable.h:67
const std::string & getShownIP() const
Definition: StructTable.h:97
std::map< std::string, MStructure > & getStructs()
Definition: StructTable.h:71
Brain * m_Brain
Definition: StructTable.h:110
std::map< std::string, std::set< std::string > > Interpretations
Definition: StructTable.h:51
const StructPath & getBestPath() const
Definition: StructTable.h:91
Winner * getWinner(MStructure &struc)
Interpretations m_Interpretations
Definition: StructTable.h:116
StructPath m_BestPath
Definition: StructTable.h:112
std::map< std::string, MStructure > m_Structs
Definition: StructTable.h:107
float m_Goodness
goodness of path
Definition: StructTable.h:42
int searchDone() const
Definition: StructTable.h:81
void setShownIP(const std::string &curip) const
Definition: StructTable.h:95
float m_Time
Definition: StructTable.h:113
float getRelGoodness() const
Definition: StructTable.h:37
std::vector< float > m_TMarks
Definition: StructTable.h:115
std::string m_CurIP
Definition: StructTable.h:119
std::string m_Path
Definition: StructTable.h:109
bool m_WinnersProtected
Definition: StructTable.h:45
friend ParseFile & operator>>(ParseFile &is, StructTable &st)
Definition: StructTable.h:65
const std::string & getFilename() const
Definition: StructTable.h:69
std::map< dword, StructPath > NodePaths
Definition: StructTable.h:52
void blockOtherPaths(const std::set< std::string > &freepath, const StructTable &structs)
std::ostream & print(std::ostream &os, const StructTable &structs) const
const std::string & getBestInterpretation() const
Definition: StructTable.h:92
std::map< std::string, NodePaths > m_NodePaths
Definition: StructTable.h:120
Definition: Brain.h:27
StructPath & operator=(const StructPath &rhs)
Definition: ExpMap.h:10
std::string m_BestIP
Definition: StructTable.h:118
float getGoodness() const
Definition: StructTable.h:31
const std::map< std::string, MStructure > & getStructs() const
Definition: StructTable.h:72
unsigned long dword
Definition: simpletypes.h:6
void unprotectWinners(StructTable &structs)
void write(std::ostream &os) const
std::map< std::string, StructPath > m_IPaths
Definition: StructTable.h:117
StructPath branch() const
void clear()
float m_Maxness
maximum goodness
Definition: StructTable.h:43
std::string m_CurStruct
Definition: StructTable.h:111
void protectWinners(StructTable &structs)
dword m_State
Definition: StructTable.h:114