From 273517a83b9e04e15b07fa1f64c76b10af7c3091 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 7 Dec 2007 14:31:10 +0000 Subject: 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 --- src/engine/machina/Evolver.hpp | 2 +- src/engine/machina/Mutation.hpp | 1 + src/engine/machina/Node.hpp | 6 ++++- src/engine/machina/Problem.hpp | 60 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 4 deletions(-) (limited to 'src/engine/machina') diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp index 051513b..6025210 100644 --- a/src/engine/machina/Evolver.hpp +++ b/src/engine/machina/Evolver.hpp @@ -47,7 +47,7 @@ private: SharedPtr _ga; SharedPtr _problem; - float _active_fitness; + float _seed_fitness; Schrodinbit _improvement; }; diff --git a/src/engine/machina/Mutation.hpp b/src/engine/machina/Mutation.hpp index 52c02bc..18bc621 100644 --- a/src/engine/machina/Mutation.hpp +++ b/src/engine/machina/Mutation.hpp @@ -39,6 +39,7 @@ 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 SwapNodes 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); }; diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp index 1f0face..87e14af 100644 --- a/src/engine/machina/Node.hpp +++ b/src/engine/machina/Node.hpp @@ -44,7 +44,7 @@ class Node : public Raul::Stateful { public: typedef std::string ID; - Node(BeatCount duration=0, bool initial=false); + Node(BeatCount duration=1.0, bool initial=false); Node(const Node& copy); void set_enter_action(SharedPtr action); @@ -56,6 +56,8 @@ public: void enter(SharedPtr driver, BeatTime time); void exit(SharedPtr driver, BeatTime time); + void edges_changed(); + void add_edge(SharedPtr edge); void remove_edge(SharedPtr edge); void remove_edges_to(SharedPtr node); @@ -82,6 +84,8 @@ public: SharedPtr random_edge(); private: + Node& operator=(const Node& other); // undefined + Schrodinbit _changed; bool _is_initial; bool _is_selector; diff --git a/src/engine/machina/Problem.hpp b/src/engine/machina/Problem.hpp index 96ecb39..7605e88 100644 --- a/src/engine/machina/Problem.hpp +++ b/src/engine/machina/Problem.hpp @@ -18,6 +18,7 @@ #ifndef MACHINA_PROBLEM_HPP #define MACHINA_PROBLEM_HPP +#include #include #include #include @@ -34,13 +35,20 @@ public: float fitness(const Machine& machine) const; bool fitness_less_than(float a, float b) const { return a < b; } + + void clear_fitness_cache() { _fitness.clear(); } boost::shared_ptr initial_population(size_t gene_size, size_t pop_size) const; private: - struct Evaluator : public Raul::MIDISink { - Evaluator(const Problem& problem) : _problem(problem), _n_notes(0), _length(0) { + size_t distance(const std::vector& source, + const std::vector& target) const; + + // count + /*struct FreqEvaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) + : _problem(problem), _n_notes(0), _length(0) { for (uint8_t i=0; i < 128; ++i) _note_frequency[i] = 0; } @@ -49,14 +57,62 @@ private: const uint8_t* ev) throw (std::logic_error); void compute(); const Problem& _problem; + + size_t n_notes() const { return _n_notes; } float _note_frequency[128]; size_t _n_notes; double _length; + };*/ + + // distance + /*struct Evaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) : _problem(problem) {} + void write_event(Raul::BeatTime time, + size_t ev_size, + const uint8_t* ev) throw (std::logic_error); + void compute(); + const Problem& _problem; + + size_t n_notes() const { return _notes.size(); } + + std::vector _notes; + float _counts[128]; + };*/ + + struct Evaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) : _problem(problem), _order(8), _n_notes(0), _first_note(0) { + for (uint8_t i=0; i < 128; ++i) + _counts[i] = 0; + } + void write_event(Raul::BeatTime time, + size_t ev_size, + const uint8_t* ev) throw (std::logic_error); + void compute(); + const Problem& _problem; + + size_t n_notes() const { return _n_notes; } + uint8_t first_note() const { return _first_note; } + + const uint32_t _order; + + std::string _read; + + typedef std::map Patterns; + Patterns _patterns; + uint32_t _counts[128]; + size_t _n_notes; + uint8_t _first_note; }; Evaluator _target; SharedPtr _seed; + + /// for levenshtein distance + mutable std::vector< std::vector > _matrix; + + /// memoization + mutable std::map _fitness; }; -- cgit v1.2.1