aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/Mutation.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-12-07 14:31:10 +0000
committerDavid Robillard <d@drobilla.net>2007-12-07 14:31:10 +0000
commit273517a83b9e04e15b07fa1f64c76b10af7c3091 (patch)
treee9af9b1c48d3db668c4e19822bc3f041d6d2aa8e /src/engine/Mutation.cpp
parent7e8186df9bdb8bcc0177cb572f39ae96fb046004 (diff)
downloadmachina-273517a83b9e04e15b07fa1f64c76b10af7c3091.tar.gz
machina-273517a83b9e04e15b07fa1f64c76b10af7c3091.tar.bz2
machina-273517a83b9e04e15b07fa1f64c76b10af7c3091.zip
Vast quantities of filthy, filthy evolution code.
Also, bugs and crashes. git-svn-id: http://svn.drobilla.net/lad/machina@961 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine/Mutation.cpp')
-rw-r--r--src/engine/Mutation.cpp55
1 files changed, 49 insertions, 6 deletions
diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp
index 95e31a3..d60ef34 100644
--- a/src/engine/Mutation.cpp
+++ b/src/engine/Mutation.cpp
@@ -53,15 +53,24 @@ AddNode::mutate(Machine& machine)
// Create random node
SharedPtr<Node> node(new Node(1.0));
- uint8_t note = rand() % 128;
+ node->set_selector(true);
+
+ SharedPtr<Node> note_node = machine.random_node();
+ uint8_t note = PtrCast<MidiAction>(note_node->enter_action())->event()[1];
+
node->set_enter_action(ActionFactory::note_on(note));
node->set_exit_action(ActionFactory::note_off(note));
machine.add_node(node);
- // Add as a successor to some other random node
+ // Insert after some node
SharedPtr<Node> tail = machine.random_node();
- if (tail && tail != node)
+ 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)*/)
+ node->add_edge(boost::shared_ptr<Edge>(new Edge(node, head)));
}
@@ -71,7 +80,8 @@ RemoveNode::mutate(Machine& machine)
//cout << "REMOVE NODE" << endl;
SharedPtr<Node> node = machine.random_node();
- machine.remove_node(node);
+ if (node && !node->is_initial())
+ machine.remove_node(node);
}
@@ -93,6 +103,34 @@ AdjustNode::mutate(Machine& machine)
}
}
+
+void
+SwapNodes::mutate(Machine& machine)
+{
+ //cout << "SWAP NODE" << endl;
+
+ if (machine.nodes().size() <= 1)
+ return;
+
+ SharedPtr<Node> a = machine.random_node();
+ SharedPtr<Node> b = machine.random_node();
+ 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());
+ SharedPtr<MidiAction> b_enter = PtrCast<MidiAction>(b->enter_action());
+ SharedPtr<MidiAction> b_exit = PtrCast<MidiAction>(b->exit_action());
+
+ uint8_t note_a = a_enter->event()[1];
+ uint8_t note_b = b_enter->event()[1];
+
+ a_enter->event()[1] = note_b;
+ a_exit->event()[1] = note_b;
+ b_enter->event()[1] = note_a;
+ b_exit->event()[1] = note_a;
+}
+
void
AddEdge::mutate(Machine& machine)
@@ -102,8 +140,11 @@ 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))
+ 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)));
+ }
}
@@ -124,8 +165,10 @@ AdjustEdge::mutate(Machine& machine)
//cout << "ADJUST EDGE" << endl;
SharedPtr<Edge> edge = machine.random_edge();
- if (edge)
+ if (edge) {
edge->set_probability(rand() / (float)RAND_MAX);
+ edge->tail().lock()->edges_changed();
+ }
}