comparison src/main.cpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 // Copyright 2011, Ian Hobson.
2 //
3 // This file is part of gpsynth.
4 //
5 // gpsynth is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // gpsynth is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with gpsynth in the file COPYING.
17 // If not, see http://www.gnu.org/licenses/.
18
19 #include "file_comparer.hpp"
20 #include "graph_helpers.hpp"
21 #include "logger.hpp"
22 #include "population.hpp"
23 #include "program_options.hpp"
24 #include "sc_converter.hpp"
25 #include "sc_default_grammar.hpp"
26 #include "sc_evaluator.hpp"
27 #include "sc_grammar.hpp"
28
29 #include "boost/filesystem.hpp"
30 #include "boost/shared_array.hpp"
31
32 #include <algorithm>
33 #include <ctime>
34 #include <fstream>
35 #include <iostream>
36 #include <sstream>
37
38 const std::string g_test_grammar_path = "/tmp/sc.json";
39
40 template<typename T>
41 void SaveToFile(const T& value, const std::string& file_path) {
42 std::ofstream file(file_path.c_str());
43 file << value;
44 }
45
46 void TestCrossover() {
47 sc::Grammar grammar(g_test_grammar_path);
48 sc::Converter converter(grammar);
49 sg::Graph parent_1;
50 sg::Graph parent_2;
51 grammar.SetMaximumDepth(2);
52 grammar.RandomGraph(parent_1);
53 grammar.RandomGraph(parent_2);
54 sg::Vertex crossover_1;
55 sg::Vertex crossover_2;
56 grammar.PickCrossoverNodes(parent_1, parent_2, &crossover_1, &crossover_2);
57 // swap the subtrees
58 sg::Graph child_1;
59 sg::Graph child_2;
60 SwapSubTrees(crossover_1, crossover_2, parent_1, parent_2, child_1, child_2);
61 SaveToFile(converter.ToDOT(parent_1), "/tmp/parent1.dot");
62 SaveToFile(converter.ToDOT(parent_2), "/tmp/parent2.dot");
63 SaveToFile(converter.ToDOT(child_1), "/tmp/child1.dot");
64 SaveToFile(converter.ToDOT(child_2), "/tmp/child2.dot");
65 }
66
67 void TestMutationReplaceSubtree() {
68 sc::Grammar grammar(g_test_grammar_path);
69 sc::Converter converter(grammar);
70 sg::Graph graph;
71 grammar.RandomGraph(graph);
72 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_before.dot");
73 grammar.MutationReplaceSubTree(graph);
74 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_after.dot");
75 }
76
77 void TestMutationReplaceCommand() {
78 sc::Grammar grammar(g_test_grammar_path);
79 sc::Converter converter(grammar);
80 sg::Graph graph;
81 grammar.SetMaximumDepth(3);
82 grammar.RandomGraph(graph);
83 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_before.dot");
84 SaveToFile(converter.ToSynthDef(graph, "before"),
85 "/tmp/mutation_str_before.sc");
86 grammar.MutationReplaceCommand(graph);
87 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_after.dot");
88 SaveToFile(converter.ToSynthDef(graph, "after"),
89 "/tmp/mutation_str_after.sc");
90 }
91
92 void TestMutationInsertCommand() {
93 sc::Grammar grammar(g_test_grammar_path);
94 sc::Converter converter(grammar);
95 sg::Graph graph;
96 grammar.SetMaximumDepth(4);
97 grammar.RandomGraph(graph);
98 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_before.dot");
99 SaveToFile(converter.ToSynthDef(graph, "before"),
100 "/tmp/mutation_str_before.sc");
101 grammar.MutationInsertCommand(graph);
102 SaveToFile(converter.ToDOT(graph), "/tmp/mutation_str_after.dot");
103 SaveToFile(converter.ToSynthDef(graph, "after"),
104 "/tmp/mutation_str_after.sc");
105 }
106
107 void DoEvolution(const ProgramOptions& settings) {
108 // prepare the file comparer
109 dsp::FileComparer target(settings.fitness_features_,
110 static_cast<int>(settings.analysis_window_size_),
111 static_cast<int>(settings.analysis_hop_size_));
112 target.SetTargetFile(settings.target_path_);
113 // prepare the supercollider objects
114 std::string grammar_json;
115 if (settings.grammar_path_.empty()) {
116 grammar_json.assign(sc::g_default_grammar);
117 } else {
118 grammar_json = stdx::LoadFile(settings.grammar_path_);
119 }
120 sc::Grammar grammar(grammar_json);
121 grammar.SetMaximumDepth(static_cast<int>(settings.maximum_tree_depth_));
122 sc::Evaluator evaluator(grammar,
123 settings.sc_app_path_,
124 target,
125 static_cast<int>(settings.core_limit_));
126 sc::Converter converter(grammar);
127 // set up the population
128 Population population(&grammar, &converter, &evaluator);
129 population.SetWorkFolder(settings.work_folder_);
130 population.SetFitnessThreshold(settings.fitness_threshold_);
131 population.SetCrossoverRate(settings.crossover_rate_);
132 population.SetMutationRate(settings.mutation_rate_);
133 population.SetTournamentSize(static_cast<int>(settings.tournament_size_));
134 population.SetReproduceBest(settings.reproduce_best_individual_);
135 population.InitializePopulation(settings.population_size_);
136 population.SetKeepTempFolders(settings.keep_temp_folders_);
137 // run the evolution loop
138 population.Evolve(static_cast<int>(settings.generations_));
139 }
140
141 int main(int argument_count, const char* arguments[])
142 {
143 srand(static_cast<unsigned int>(time(NULL)));
144 try {
145 // parse the command line options
146 ProgramOptions settings;
147 ParseCommandLine(argument_count, arguments, &settings);
148 DoEvolution(settings);
149
150 //TestCrossover();
151 //TestMutationReplaceSubtree();
152 //TestMutationInsertCommand();
153 //TestMutationReplaceCommand();
154 } catch (const std::exception& e) {
155 std::stringstream message;
156 message << "Exception: " << e.what() << '\n';
157 logger::Log(message.str());
158 logger::Flush();
159 return -1;
160 }
161 logger::Flush();
162 }