aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/Problem.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2013-01-12 03:17:47 +0000
committerDavid Robillard <d@drobilla.net>2013-01-12 03:17:47 +0000
commite1a63732f796c8057751cb7f8ac98a595b91692e (patch)
tree764392d055901118ee285dd20f233ff33e0cd04a /src/engine/Problem.cpp
parentbb62bf2c23ba84f547e37b70b3ef31b51ba987ab (diff)
downloadmachina-e1a63732f796c8057751cb7f8ac98a595b91692e.tar.gz
machina-e1a63732f796c8057751cb7f8ac98a595b91692e.tar.bz2
machina-e1a63732f796c8057751cb7f8ac98a595b91692e.zip
Bulk reformat.
git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4929 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine/Problem.cpp')
-rw-r--r--src/engine/Problem.cpp136
1 files changed, 79 insertions, 57 deletions
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<Machine> seed)
+Problem::Problem(TimeUnit unit, const std::string& target_midi,
+ SharedPtr<Machine> 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<Machin
if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
const uint8_t note = buf[1];
/*++_target._note_frequency[note];
- ++_target.n_notes();*/
+ ++_target.n_notes();*/
++_target._counts[note];
_target._notes.push_back(note);
}
@@ -81,8 +82,9 @@ Problem::fitness(const Machine& const_machine) const
Machine& machine = const_cast<Machine&>(const_machine);
map<Machine*, float>::const_iterator cached = _fitness.find(&machine);
- if (cached != _fitness.end())
+ if (cached != _fitness.end()) {
return cached->second;
+ }
SharedPtr<Evaluator> 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<Problem::Population>
@@ -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<Machine> base = SharedPtr<Machine>(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> node(new Node(TimeDuration(_unit, 1/2.0)));
+ SharedPtr<Node> 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<Node> > unreachable;
- SharedPtr<Node> initial;
+ SharedPtr<Node> initial;
- for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); ++i) {
- if (PtrCast<MidiAction>((*i)->enter_action())->event()[1] == _target.first_note()) {
+ for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end();
+ ++i) {
+ if (PtrCast<MidiAction>((*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<Node> 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<Edge>(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<uint8_t>& 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<uint8_t>& 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<uint8_t>& 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<uint8_t>& source,
}
} // namespace Machina
-