aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/Mutation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/Mutation.cpp')
-rw-r--r--src/engine/Mutation.cpp49
1 files changed, 31 insertions, 18 deletions
diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp
index 73fc0e9..e25cdbb 100644
--- a/src/engine/Mutation.cpp
+++ b/src/engine/Mutation.cpp
@@ -36,12 +36,14 @@ Compress::mutate(Machine& machine)
//cout << "COMPRESS" << endl;
// Trim disconnected nodes
- for (Machine::Nodes::iterator i = machine.nodes().begin(); i != machine.nodes().end() ;) {
+ for (Machine::Nodes::iterator i = machine.nodes().begin();
+ i != machine.nodes().end(); ) {
Machine::Nodes::iterator next = i;
++next;
- if ((*i)->edges().empty())
+ if ((*i)->edges().empty()) {
machine.remove_node(*i);
+ }
i = next;
}
@@ -57,14 +59,17 @@ AddNode::mutate(Machine& machine)
node->set_selector(true);
SharedPtr<Node> note_node = machine.random_node();
- if (!note_node)
+ if (!note_node) {
return;
+ }
uint8_t note = rand() % 128;
- SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(note_node->enter_action());
- if (enter_action)
+ SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(
+ note_node->enter_action());
+ if (enter_action) {
note = enter_action->event()[1];
+ }
node->set_enter_action(ActionFactory::note_on(note));
node->set_exit_action(ActionFactory::note_off(note));
@@ -72,13 +77,15 @@ AddNode::mutate(Machine& machine)
// Insert after some node
SharedPtr<Node> tail = machine.random_node();
- if (tail && tail != node/* && !node->connected_to(tail)*/)
+ if (tail && (tail != node) /* && !node->connected_to(tail)*/) {
tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, node)));
+ }
// Insert before some other node
SharedPtr<Node> head = machine.random_node();
- if (head && head != node/* && !head->connected_to(node)*/)
+ if (head && (head != node) /* && !head->connected_to(node)*/) {
node->add_edge(boost::shared_ptr<Edge>(new Edge(node, head)));
+ }
}
void
@@ -87,8 +94,9 @@ RemoveNode::mutate(Machine& machine)
//cout << "REMOVE NODE" << endl;
SharedPtr<Node> node = machine.random_node();
- if (node && !node->is_initial())
+ if (node && !node->is_initial()) {
machine.remove_node(node);
+ }
}
void
@@ -98,12 +106,14 @@ AdjustNode::mutate(Machine& machine)
SharedPtr<Node> node = machine.random_node();
if (node) {
- SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(node->enter_action());
- SharedPtr<MidiAction> exit_action = PtrCast<MidiAction>(node->exit_action());
+ SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(
+ node->enter_action());
+ SharedPtr<MidiAction> exit_action = PtrCast<MidiAction>(
+ node->exit_action());
if (enter_action && exit_action) {
const uint8_t note = rand() % 128;
enter_action->event()[1] = note;
- exit_action->event()[1] = note;
+ exit_action->event()[1] = note;
}
node->set_changed();
}
@@ -114,13 +124,15 @@ SwapNodes::mutate(Machine& machine)
{
//cout << "SWAP NODE" << endl;
- if (machine.nodes().size() <= 1)
+ if (machine.nodes().size() <= 1) {
return;
+ }
SharedPtr<Node> a = machine.random_node();
SharedPtr<Node> b = machine.random_node();
- while (b == a)
+ while (b == a) {
b = machine.random_node();
+ }
SharedPtr<MidiAction> a_enter = PtrCast<MidiAction>(a->enter_action());
SharedPtr<MidiAction> a_exit = PtrCast<MidiAction>(a->exit_action());
@@ -131,9 +143,9 @@ SwapNodes::mutate(Machine& machine)
uint8_t note_b = b_enter->event()[1];
a_enter->event()[1] = note_b;
- a_exit->event()[1] = note_b;
+ a_exit->event()[1] = note_b;
b_enter->event()[1] = note_a;
- b_exit->event()[1] = note_a;
+ b_exit->event()[1] = note_a;
}
void
@@ -144,7 +156,8 @@ AddEdge::mutate(Machine& machine)
SharedPtr<Node> tail = machine.random_node();
SharedPtr<Node> head = machine.random_node();
- if (tail && head && tail != head/* && !tail->connected_to(head) && !head->connected_to(tail)*/) {
+ if (tail && head && tail != head) {
+ // && !tail->connected_to(head) && !head->connected_to(tail)
SharedPtr<Edge> edge(new Edge(tail, head));
edge->set_probability(rand() / (float)RAND_MAX);
tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, head)));
@@ -157,8 +170,9 @@ RemoveEdge::mutate(Machine& machine)
//cout << "REMOVE EDGE" << endl;
SharedPtr<Node> tail = machine.random_node();
- if (tail && !(tail->is_initial() && tail->edges().size() == 1))
+ if (tail && !(tail->is_initial() && tail->edges().size() == 1)) {
tail->remove_edge(tail->random_edge());
+ }
}
void
@@ -175,4 +189,3 @@ AdjustEdge::mutate(Machine& machine)
} // namespace Mutation
} // namespace Machina
-