From e1a63732f796c8057751cb7f8ac98a595b91692e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Jan 2013 03:17:47 +0000 Subject: Bulk reformat. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4929 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Problem.cpp | 136 ++++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 57 deletions(-) (limited to 'src/engine/Problem.cpp') diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index 7607829..5c5992d 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -38,12 +38,13 @@ using namespace std; namespace Machina { -Problem::Problem(TimeUnit unit, const std::string& target_midi, SharedPtr seed) +Problem::Problem(TimeUnit unit, const std::string& target_midi, + SharedPtr seed) : _unit(unit) , _target(*this) , _seed(new Machine(*seed.get())) { - SMFReader smf; + SMFReader smf; const bool opened = smf.open(target_midi); assert(opened); @@ -60,7 +61,7 @@ Problem::Problem(TimeUnit unit, const std::string& target_midi, SharedPtr(const_machine); map::const_iterator cached = _fitness.find(&machine); - if (cached != _fitness.end()) + if (cached != _fitness.end()) { return cached->second; + } SharedPtr eval(new Evaluator(*this)); @@ -94,20 +96,21 @@ Problem::fitness(const Machine& const_machine) const // FIXME: timing stuff here isn't right at all... static const unsigned ppqn = MACHINA_PPQN; - Raul::TimeSlice time(ppqn, ppqn, 120.0); + Raul::TimeSlice time(ppqn, ppqn, 120.0); time.set_slice(TimeStamp(_unit, 0, 0), TimeDuration(_unit, 2 * ppqn)); machine.run(time); - if (eval->n_notes() == 0) + if (eval->n_notes() == 0) { return 0.0f; // bad dog - + } TimeStamp end(_unit, time.start_ticks().ticks() + 2 * ppqn); time.set_slice(end, TimeStamp(_unit, 0, 0)); while (eval->n_notes() < _target.n_notes()) { machine.run(time); - if (machine.is_finished()) + if (machine.is_finished()) { machine.reset(time.start_ticks()); + } time.set_slice(end, TimeStamp(end.unit(), 0, 0)); } @@ -117,11 +120,11 @@ Problem::fitness(const Machine& const_machine) const #if 0 float f = 0; - for (uint8_t i=0; i < 128; ++i) { + for (uint8_t i = 0; i < 128; ++i) { /*if (eval->_note_frequency[i] <= _target._note_frequency[i]) - f += eval->_note_frequency[i]; - else - f -= _target._note_frequency[i] - eval->_note_frequency[i];*/ + f += eval->_note_frequency[i]; + else + f -= _target._note_frequency[i] - eval->_note_frequency[i];*/ //f -= fabs(eval->_note_frequency[i] - _target._note_frequency[i]); } #endif @@ -132,24 +135,30 @@ Problem::fitness(const Machine& const_machine) const float f = 0.0; - for (Evaluator::Patterns::const_iterator i = eval->_patterns.begin(); i != eval->_patterns.end(); ++i) { + for (Evaluator::Patterns::const_iterator i = eval->_patterns.begin(); + i != eval->_patterns.end(); ++i) { // Reward for matching patterns if (_target._patterns.find(i->first) != _target._patterns.end()) { - Evaluator::Patterns::const_iterator c = _target._patterns.find(i->first); - const uint32_t cnt = (c == _target._patterns.end()) ? 1 : c->second; - f += min(i->second, cnt) * (i->first.length()); + Evaluator::Patterns::const_iterator c = _target._patterns.find( + i->first); + const uint32_t cnt + = (c == _target._patterns.end()) ? 1 : c->second; + f += min(i->second, cnt) * (i->first.length()); - // Punish for bad patterns } else { + // Punish for bad patterns const uint32_t invlen = (eval->_order - i->first.length() + 1); - f -= (i->second / (float)eval->_patterns.size() * (float)(invlen*invlen*invlen)) * 4; + f -= (i->second / (float)eval->_patterns.size() + * (float)(invlen * invlen * invlen)) * 4; } } // Punish for missing patterns - for (Evaluator::Patterns::const_iterator i = _target._patterns.begin(); i != _target._patterns.end(); ++i) { + for (Evaluator::Patterns::const_iterator i = _target._patterns.begin(); + i != _target._patterns.end(); ++i) { if (eval->_patterns.find(i->first) == _target._patterns.end()) { - f -= i->second / (float)_target.n_notes() * (float)(eval->_order - i->first.length() + 1); + f -= i->second / (float)_target.n_notes() + * (float)(eval->_order - i->first.length() + 1); } } @@ -169,28 +178,31 @@ Problem::Evaluator::write_event(Raul::TimeStamp time, const uint8_t note = ev[1]; - if (_first_note == 0) + if (_first_note == 0) { _first_note = note; + } /*++_note_frequency[note]; - ++n_notes();*/ + ++n_notes();*/ //_notes.push_back(note); if (_read.length() == 0) { _read = note; return; } - if (_read.length() == _order) + if (_read.length() == _order) { _read = _read.substr(1); + } _read = _read + (char)note; for (size_t i = 0; i < _read.length(); ++i) { - const string pattern = _read.substr(i); - Patterns::iterator i = _patterns.find(pattern); - if (i != _patterns.end()) + const string pattern = _read.substr(i); + Patterns::iterator i = _patterns.find(pattern); + if (i != _patterns.end()) { ++(i->second); - else + } else { _patterns[pattern] = 1; + } } ++_counts[note]; @@ -203,12 +215,12 @@ void Problem::Evaluator::compute() { /* - for (uint8_t i=0; i < 128; ++i) { - if (_note_frequency[i] > 0) { - _note_frequency[i] /= (float)n_notes(); - //cout << (int)i << ":\t" << _note_frequency[i] << endl; - } - }*/ + for (uint8_t i=0; i < 128; ++i) { + if (_note_frequency[i] > 0) { + _note_frequency[i] /= (float)n_notes(); + //cout << (int)i << ":\t" << _note_frequency[i] << endl; + } + }*/ } boost::shared_ptr @@ -219,10 +231,10 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const // FIXME: ignores _seed and builds based on MIDI // evolution of the visible machine would be nice.. SharedPtr base = SharedPtr(new Machine(_unit)); - for (uint8_t i=0; i < 128; ++i) { + for (uint8_t i = 0; i < 128; ++i) { if (_target._counts[i] > 0) { //cout << "Initial note: " << (int)i << endl; - SharedPtr node(new Node(TimeDuration(_unit, 1/2.0))); + SharedPtr node(new Node(TimeDuration(_unit, 1 / 2.0))); node->set_enter_action(ActionFactory::note_on(i)); node->set_exit_action(ActionFactory::note_off(i)); node->set_selector(true); @@ -235,10 +247,12 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const Machine m(*base.get()); set< SharedPtr > unreachable; - SharedPtr initial; + SharedPtr initial; - for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); ++i) { - if (PtrCast((*i)->enter_action())->event()[1] == _target.first_note()) { + for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); + ++i) { + if (PtrCast((*i)->enter_action())->event()[1] == + _target.first_note()) { (*i)->set_initial(true); initial = *i; } else { @@ -250,13 +264,14 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const unreachable.erase(cur); SharedPtr head; - while ( ! unreachable.empty()) { - if (rand() % 2) + while (!unreachable.empty()) { + if (rand() % 2) { head = m.random_node(); - else + } else { head = *unreachable.begin(); + } - if ( ! head->connected_to(head) ) { + if (!head->connected_to(head) ) { cur->add_edge(SharedPtr(new Edge(cur, head))); unreachable.erase(head); cur = head; @@ -266,7 +281,7 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const ret->push_back(m); /*cout << "initial # nodes: " << m.nodes().size(); - cout << "initial fitness: " << fitness(m) << endl;*/ + cout << "initial fitness: " << fitness(m) << endl;*/ } return ret; @@ -286,22 +301,27 @@ Problem::distance(const std::vector& source, const uint16_t n = source.size(); const uint16_t m = target.size(); - if (n == 0) + if (n == 0) { return m; - if (m == 0) + } + if (m == 0) { return n; + } _matrix.resize(n + 1); - for (uint16_t i = 0; i <= n; i++) - _matrix[i].resize (m + 1); + for (uint16_t i = 0; i <= n; i++) { + _matrix[i].resize(m + 1); + } // Step 2 - for (uint16_t i = 0; i <= n; i++) + for (uint16_t i = 0; i <= n; i++) { _matrix[i][0] = i; + } - for (uint16_t j = 0; j <= m; j++) + for (uint16_t j = 0; j <= m; j++) { _matrix[0][j] = j; + } // Step 3 @@ -327,9 +347,9 @@ Problem::distance(const std::vector& source, // Step 6 const uint16_t above = _matrix[i - 1][j]; - const uint16_t left = _matrix[i][j - 1]; - const uint16_t diag = _matrix[i - 1][j - 1]; - uint16_t cell = min (above + 1, min (left + 1, diag + cost)); + const uint16_t left = _matrix[i][j - 1]; + const uint16_t diag = _matrix[i - 1][j - 1]; + uint16_t cell = min(above + 1, min(left + 1, diag + cost)); // Step 6A: Cover transposition, in addition to deletion, // insertion and substitution. This step is taken from: @@ -337,14 +357,17 @@ Problem::distance(const std::vector& source, // Enhanced Dynamic Programming ASM Algorithm" // (http://www.acm.org/~hlb/publications/asm/asm.html) - if (i > 2 && j > 2) { + if (( i > 2) && ( j > 2) ) { uint16_t trans = _matrix[i - 2][j - 2] + 1; - if (source[i - 2] != t_j) + if (source[i - 2] != t_j) { trans++; - if (s_i != target[j - 2]) + } + if (s_i != target[j - 2]) { trans++; - if (cell > trans) + } + if (cell > trans) { cell = trans; + } } _matrix[i][j] = cell; @@ -357,4 +380,3 @@ Problem::distance(const std::vector& source, } } // namespace Machina - -- cgit v1.2.1