diff options
Diffstat (limited to 'src/engine/machina')
-rw-r--r-- | src/engine/machina/Engine.hpp | 5 | ||||
-rw-r--r-- | src/engine/machina/Evolver.hpp | 57 | ||||
-rw-r--r-- | src/engine/machina/Machine.hpp | 16 | ||||
-rw-r--r-- | src/engine/machina/Makefile.am | 1 | ||||
-rw-r--r-- | src/engine/machina/Mutation.hpp | 23 | ||||
-rw-r--r-- | src/engine/machina/Problem.hpp | 24 |
6 files changed, 104 insertions, 22 deletions
diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index d8d16d9..effa4be 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -35,7 +35,8 @@ public: : _driver(driver) , _rdf_world(rdf_world) , _loader(_rdf_world) - { } + { + } Redland::World& rdf_world() { return _rdf_world; } @@ -51,7 +52,7 @@ public: private: SharedPtr<Driver> _driver; - Redland::World& _rdf_world; + Redland::World& _rdf_world; Loader _loader; }; diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp new file mode 100644 index 0000000..051513b --- /dev/null +++ b/src/engine/machina/Evolver.hpp @@ -0,0 +1,57 @@ +/* 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 + */ + +#ifndef MACHINA_EVOLVER_HPP +#define MACHINA_EVOLVER_HPP + +#include <raul/SharedPtr.hpp> +#include <raul/Thread.hpp> +#include <eugene/core/GAImpl.hpp> +#include "Schrodinbit.hpp" + +namespace Eugene { template <typename G> class HybridMutation; } + +namespace Machina { + +class Machine; +class Problem; + + +class Evolver : public Raul::Thread { +public: + Evolver(const string& target_midi, SharedPtr<Machine> seed); + + void seed(SharedPtr<Machine> parent); + bool improvement() { return _improvement; } + + SharedPtr<const Machine> best() { return _ga->best(); } + + typedef Eugene::GAImpl<Machine> MachinaGA; + +private: + void _run(); + + SharedPtr<MachinaGA> _ga; + SharedPtr<Problem> _problem; + float _active_fitness; + Schrodinbit _improvement; +}; + + +} // namespace Machina + +#endif // MACHINA_EVOLVER_HPP diff --git a/src/engine/machina/Machine.hpp b/src/engine/machina/Machine.hpp index 4544090..5c4df36 100644 --- a/src/engine/machina/Machine.hpp +++ b/src/engine/machina/Machine.hpp @@ -18,6 +18,7 @@ #ifndef MACHINA_MACHINE_HPP #define MACHINA_MACHINE_HPP +#include <vector> #include <boost/utility.hpp> #include <raul/SharedPtr.hpp> #include <raul/WeakPtr.hpp> @@ -37,7 +38,11 @@ class Machine : public Raul::Stateful { public: Machine(); Machine(const Machine& copy); - ~Machine(); + + Machine& operator=(const Machine& other); + + // Kluge to appease Eugene + bool operator==(const Machine& other) { return false; } // Main context void activate() { _is_activated = true; } @@ -64,7 +69,8 @@ public: void clear_pending_learn() { _pending_learn.reset(); } typedef Raul::List< SharedPtr<Node> > Nodes; - Nodes& nodes() { return _nodes; } + Nodes& nodes() { return _nodes; } + const Nodes& nodes() const { return _nodes; } SharedPtr<Node> random_node(); SharedPtr<Edge> random_edge(); @@ -75,11 +81,11 @@ private: // Audio context SharedPtr<Node> earliest_node() const; - bool enter_node(const SharedPtr<Raul::MIDISink> sink, const SharedPtr<Node> node); - void exit_node(const SharedPtr<Raul::MIDISink> sink, const SharedPtr<Node>); + bool enter_node(SharedPtr<Raul::MIDISink> sink, SharedPtr<Node> node); + void exit_node(SharedPtr<Raul::MIDISink> sink, SharedPtr<Node>); static const size_t MAX_ACTIVE_NODES = 128; - SharedPtr<Node> _active_nodes[MAX_ACTIVE_NODES]; + std::vector< SharedPtr<Node> > _active_nodes; bool _is_activated; bool _is_finished; diff --git a/src/engine/machina/Makefile.am b/src/engine/machina/Makefile.am index f37974c..e4f4bf0 100644 --- a/src/engine/machina/Makefile.am +++ b/src/engine/machina/Makefile.am @@ -6,6 +6,7 @@ libmachinainclude_HEADERS = \ Driver.hpp \ Edge.hpp \ Engine.hpp \ + Evolver.hpp \ JackDriver.hpp \ LearnRequest.hpp \ Loader.hpp \ diff --git a/src/engine/machina/Mutation.hpp b/src/engine/machina/Mutation.hpp index 0a9731c..52c02bc 100644 --- a/src/engine/machina/Mutation.hpp +++ b/src/engine/machina/Mutation.hpp @@ -18,6 +18,15 @@ #ifndef MACHINA_MACHINE_MUTATION_HPP #define MACHINA_MACHINE_MUTATION_HPP +#include CONFIG_H_PATH + +#if HAVE_EUGENE + #include <eugene/core/Mutation.hpp> + #define SUPER : public Eugene::Mutation<Machine> +#else + #define SUPER +#endif + namespace Machina { class Machine; @@ -26,13 +35,13 @@ namespace Mutation { struct Mutation { virtual void mutate(Machine& machine) = 0; }; -struct Compress { static void mutate(Machine& machine); }; -struct AddNode { static void mutate(Machine& machine); }; -struct RemoveNode { static void mutate(Machine& machine); }; -struct AdjustNode { static void mutate(Machine& machine); }; -struct AddEdge { static void mutate(Machine& machine); }; -struct RemoveEdge { static void mutate(Machine& machine); }; -struct AdjustEdge { static void mutate(Machine& machine); }; +struct Compress SUPER { void mutate(Machine& machine); }; +struct AddNode SUPER { void mutate(Machine& machine); }; +struct RemoveNode SUPER { void mutate(Machine& machine); }; +struct AdjustNode SUPER { void mutate(Machine& machine); }; +struct AddEdge SUPER { void mutate(Machine& machine); }; +struct RemoveEdge SUPER { void mutate(Machine& machine); }; +struct AdjustEdge SUPER { void mutate(Machine& machine); }; } // namespace Mutation diff --git a/src/engine/machina/Problem.hpp b/src/engine/machina/Problem.hpp index d221e5c..abfc534 100644 --- a/src/engine/machina/Problem.hpp +++ b/src/engine/machina/Problem.hpp @@ -19,21 +19,28 @@ #define MACHINA_PROBLEM_HPP #include <raul/MIDISink.hpp> +#include <eugene/core/Problem.hpp> +#include <machina/Machine.hpp> namespace Machina { -class Machine; - -class Problem { +class Problem : public Eugene::Problem<Machine> { public: - Problem(const std::string& target_midi); + Problem(const std::string& target_midi, SharedPtr<Machine> seed = SharedPtr<Machine>()); + + void seed(SharedPtr<Machine> parent) { _seed = parent; } - float fitness(Machine& machine); + float fitness(const Machine& machine) const; + + bool fitness_less_than(float a, float b) const { return a < b; } + + boost::shared_ptr<Population> + initial_population(size_t gene_size, size_t pop_size) const; private: struct Evaluator : public Raul::MIDISink { - Evaluator(Problem& problem) : _problem(problem), _n_notes(0) { + Evaluator(const Problem& problem) : _problem(problem), _n_notes(0) { for (uint8_t i=0; i < 128; ++i) _note_frequency[i] = 0; } @@ -41,13 +48,14 @@ private: size_t ev_size, const uint8_t* ev) throw (std::logic_error); void compute(); - Problem& _problem; + const Problem& _problem; float _note_frequency[128]; size_t _n_notes; }; - Evaluator _target; + Evaluator _target; + SharedPtr<Machine> _seed; }; |