comparison src/AgentFeederMono.h @ 189:3de7c871d9c8 noteagent

Fixes to mono feeder; use it for monophonic instruments
author Chris Cannam
date Thu, 29 May 2014 10:30:08 +0100
parents 1697457458b7
children
comparison
equal deleted inserted replaced
188:462b165c8c0f 189:3de7c871d9c8
29 * 29 *
30 * One satisfied hypothesis is considered to be "accepted" at any 30 * One satisfied hypothesis is considered to be "accepted" at any
31 * moment (that is, the earliest contemporary hypothesis to have 31 * moment (that is, the earliest contemporary hypothesis to have
32 * become satisfied). The series of accepted and completed hypotheses 32 * become satisfied). The series of accepted and completed hypotheses
33 * from construction to the present time can be queried through 33 * from construction to the present time can be queried through
34 * getAcceptedHypotheses(). 34 * retrieveAcceptedHypotheses(), which detaches them from the current
35 * feeder (i.e. hypotheses returned from one call to this function
36 * will not be returned by any subsequent call).
35 * 37 *
36 * Call feed() to provide a new observation. Call finish() when all 38 * Call feed() to provide a new observation. Call finish() when all
37 * observations have been provided. The set of hypotheses returned by 39 * observations have been provided. The set of hypotheses returned by
38 * getAcceptedHypotheses() will not be complete unless finish() has 40 * getAcceptedHypotheses() will not be complete unless finish() has
39 * been called. 41 * been called.
40 */ 42 */
41 template <typename Hypothesis> 43 template <typename Hypothesis>
42 class AgentFeederMono : public AgentFeeder 44 class AgentFeederMono : public AgentFeeder
43 { 45 {
46 private:
47 typedef std::vector<Hypothesis> Hypotheses;
48
44 public: 49 public:
45 AgentFeederMono() { } 50 AgentFeederMono() : m_haveCurrent(false) { }
46
47 typedef std::set<Hypothesis> Hypotheses;
48 51
49 virtual void feed(AgentHypothesis::Observation o) { 52 virtual void feed(AgentHypothesis::Observation o) {
50 53
51 #ifdef DEBUG_FEEDER 54 #ifdef DEBUG_FEEDER
52 std::cerr << "feed: have observation [value = " << o.value << ", time = " << o.time << "]" << std::endl; 55 std::cerr << "feed: have observation [value = " << o.value << ", time = " << o.time << "]" << std::endl;
53 #endif 56 #endif
54
55 if (!m_current.accept(o)) {
56 57
58 if (m_haveCurrent) {
59 if (m_current.accept(o)) {
60 return;
61 }
57 if (m_current.getState() == Hypothesis::Expired) { 62 if (m_current.getState() == Hypothesis::Expired) {
58 m_accepted.insert(m_current); 63 m_accepted.push_back(m_current);
59 #ifdef DEBUG_FEEDER 64 #ifdef DEBUG_FEEDER
60 std::cerr << "current has expired, pushing to accepted" << std::endl; 65 std::cerr << "current has expired, pushing to accepted" << std::endl;
61 #endif 66 #endif
67 m_haveCurrent = false;
62 } 68 }
69 }
63 70
64 bool swallowed = false; 71 bool swallowed = false;
65 72
66 #ifdef DEBUG_FEEDER 73 #ifdef DEBUG_FEEDER
67 std::cerr << "not swallowed by current" << std::endl; 74 std::cerr << "not swallowed by current" << std::endl;
68 #endif 75 #endif
69 76
70 Hypotheses newCandidates; 77 Hypotheses newCandidates;
71 78
72 for (typename Hypotheses::iterator i = m_candidates.begin(); 79 for (typename Hypotheses::iterator i = m_candidates.begin();
73 i != m_candidates.end(); ++i) { 80 i != m_candidates.end(); ++i) {
74 81
75 Hypothesis h = *i; 82 Hypothesis h = *i;
76 83
77 if (swallowed) { 84 if (swallowed) {
78 85
79 // don't offer: each observation can only belong to one 86 // don't offer: each observation can only belong to one
80 // satisfied hypothesis 87 // satisfied hypothesis
81 newCandidates.insert(h); 88 newCandidates.push_back(h);
82 89
83 } else { 90 } else {
84 91
85 if (h.accept(o)) { 92 if (h.accept(o)) {
86 #ifdef DEBUG_FEEDER 93 #ifdef DEBUG_FEEDER
87 std::cerr << "accepted, state is " << h.getState() << std::endl; 94 std::cerr << "accepted, state is " << h.getState() << std::endl;
88 #endif 95 #endif
89 if (h.getState() == Hypothesis::Satisfied) { 96 if (h.getState() == Hypothesis::Satisfied) {
90 97
91 swallowed = true; 98 swallowed = true;
92 99
93 if (m_current.getState() == Hypothesis::Expired || 100 if (!m_haveCurrent ||
94 m_current.getState() == Hypothesis::Rejected) { 101 m_current.getState() == Hypothesis::Expired ||
102 m_current.getState() == Hypothesis::Rejected) {
95 #ifdef DEBUG_FEEDER 103 #ifdef DEBUG_FEEDER
96 std::cerr << "current has ended, updating from candidate" << std::endl; 104 std::cerr << "current has ended, updating from candidate" << std::endl;
97 #endif 105 #endif
98 m_current = h; 106 m_current = h;
99 } else { 107 m_haveCurrent = true;
100 newCandidates.insert(h);
101 }
102
103 } else { 108 } else {
104 newCandidates.insert(h); 109 newCandidates.push_back(h);
105 } 110 }
111
112 } else {
113 newCandidates.push_back(h);
106 } 114 }
107 } 115 }
108 } 116 }
117 }
109 118
110 if (!swallowed) { 119 if (!swallowed) {
111 Hypothesis h; 120 Hypothesis h;
112 h.accept(o); // must succeed, as h is new 121 h.accept(o); // must succeed, as h is new
113 newCandidates.insert(h); 122 newCandidates.push_back(h);
114 #ifdef DEBUG_FEEDER 123 #ifdef DEBUG_FEEDER
115 std::cerr << "not swallowed, creating new hypothesis" << std::endl; 124 std::cerr << "not swallowed, creating new hypothesis" << std::endl;
116 #endif 125 #endif
117 } 126 }
118 127
119 // reap rejected/expired hypotheses from candidates list, 128 // reap rejected/expired hypotheses from candidates list,
120 // and assign back to m_candidates 129 // and assign back to m_candidates
130
131 m_candidates.clear();
121 132
122 m_candidates.clear(); 133 for (typename Hypotheses::const_iterator i = newCandidates.begin();
123 134 i != newCandidates.end(); ++i) {
124 for (typename Hypotheses::const_iterator i = newCandidates.begin(); 135 Hypothesis h = *i;
125 i != newCandidates.end(); ++i) { 136 if (h.getState() != Hypothesis::Rejected &&
126 Hypothesis h = *i; 137 h.getState() != Hypothesis::Expired) {
127 if (h.getState() != Hypothesis::Rejected && 138 m_candidates.push_back(h);
128 h.getState() != Hypothesis::Expired) { 139 } else {
129 m_candidates.insert(h);
130 } else {
131 #ifdef DEBUG_FEEDER 140 #ifdef DEBUG_FEEDER
132 std::cerr << "reaping a candidate" << std::endl; 141 std::cerr << "reaping a candidate" << std::endl;
133 #endif 142 #endif
134 }
135 } 143 }
136 } 144 }
137 #ifdef DEBUG_FEEDER 145 #ifdef DEBUG_FEEDER
138 std::cerr << "have " << m_candidates.size() << " candidates" << std::endl; 146 std::cerr << "have " << m_candidates.size() << " candidates" << std::endl;
139 #endif 147 #endif
140 } 148 }
141 149
142 virtual void finish() { 150 virtual void finish() {
143 if (m_current.getState() == Hypothesis::Satisfied) { 151 if (m_haveCurrent &&
152 (m_current.getState() == Hypothesis::Satisfied)) {
144 #ifdef DEBUG_FEEDER 153 #ifdef DEBUG_FEEDER
145 std::cerr << "finish: current is satisfied, pushing to accepted" << std::endl; 154 std::cerr << "finish: current is satisfied, pushing to accepted" << std::endl;
146 #endif 155 #endif
147 m_accepted.insert(m_current); 156 m_accepted.push_back(m_current);
148 } 157 }
149 } 158 }
150 159
151 Hypotheses retrieveAcceptedHypotheses() { 160 std::set<Hypothesis> retrieveAcceptedHypotheses() {
152 Hypotheses aa = m_accepted; 161 std::set<Hypothesis> hs;
162 #ifdef DEBUG_FEEDER
163 std::cerr << "retrieveAcceptedHypotheses: returning " << m_accepted.size() << " accepted" << std::endl;
164 #endif
165 for (typename Hypotheses::const_iterator i = m_accepted.begin();
166 i != m_accepted.end(); ++i) {
167 hs.insert(*i);
168 }
153 m_accepted.clear(); 169 m_accepted.clear();
154 return aa; 170 return hs;
155 } 171 }
156 172
157 private: 173 private:
158 Hypotheses m_candidates; 174 Hypotheses m_candidates;
159 Hypothesis m_current; 175 Hypothesis m_current;
176 bool m_haveCurrent;
160 Hypotheses m_accepted; 177 Hypotheses m_accepted;
161 }; 178 };
162 179
163 #endif 180 #endif