diff options
author | David Robillard <d@drobilla.net> | 2007-12-06 00:02:08 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2007-12-06 00:02:08 +0000 |
commit | e19c643153b32983cfeb319356dbbc2f7798c4b7 (patch) | |
tree | b12eef6afc36e079482fe5a72f22df149cbe5d2d /src/engine/Mutation.cpp | |
parent | a96b70b1c92b7f2622ebeff9d34c92ba089997f1 (diff) | |
download | machina-e19c643153b32983cfeb319356dbbc2f7798c4b7.tar.gz machina-e19c643153b32983cfeb319356dbbc2f7798c4b7.tar.bz2 machina-e19c643153b32983cfeb319356dbbc2f7798c4b7.zip |
Rename uselessley verbose MachineMutation.[ch]pp
git-svn-id: http://svn.drobilla.net/lad/machina@953 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine/Mutation.cpp')
-rw-r--r-- | src/engine/Mutation.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp new file mode 100644 index 0000000..c861810 --- /dev/null +++ b/src/engine/Mutation.cpp @@ -0,0 +1,120 @@ +/* This file is part of Machina. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * Machina is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * Machina is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include <iostream> +#include <cstdlib> +#include "machina/Edge.hpp" +#include "machina/Machine.hpp" +#include "machina/Mutation.hpp" +#include "machina/ActionFactory.hpp" + +using namespace std; + +namespace Machina { +namespace Mutation { + + +void +Compress::mutate(Machine& machine) +{ + // Trim disconnected nodes + for (Machine::Nodes::iterator i = machine.nodes().begin(); i != machine.nodes().end() ;) { + Machine::Nodes::iterator next = i; + ++next; + + if ((*i)->edges().empty()) + machine.remove_node(*i); + + i = next; + } +} + + +void +AddNode::mutate(Machine& machine) +{ + // Create random node + SharedPtr<Node> node(new Node(1.0)); + uint8_t note = rand() % 128; + 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 + SharedPtr<Node> tail = machine.random_node(); + if (tail && tail != node) + tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, node))); +} + + +void +RemoveNode::mutate(Machine& machine) +{ + SharedPtr<Node> node = machine.random_node(); + machine.remove_node(node); +} + + +void +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()); + if (enter_action && exit_action) { + const uint8_t note = rand() % 128; + enter_action->event()[1] = note; + exit_action->event()[1] = note; + } + node->set_changed(); + } +} + + +void +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)) + tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, head))); +} + + +void +RemoveEdge::mutate(Machine& machine) +{ + SharedPtr<Node> tail = machine.random_node(); + if (tail) + tail->remove_edge(tail->random_edge()); +} + + +void +AdjustEdge::mutate(Machine& machine) +{ + SharedPtr<Edge> edge = machine.random_edge(); + if (edge) + edge->set_probability(rand() / (float)RAND_MAX); +} + + +} // namespace Mutation +} // namespace Machina + |