diff --git a/app.py b/app.py index b24051d4d207e7031c4b3cc4e8f35ee43b807674..952de6ec9747514c35249d3e6df09bb6ac7d532a 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ from src.population import Population app = Flask(__name__) world = None +population = None simulation_frames = {} cone_number = 30 @@ -22,6 +23,7 @@ topological_checked = "checked" @app.route('/', methods=["GET", "POST"]) def main(): global world + global population global iterations global population_size global mutation_step_size @@ -30,14 +32,17 @@ def main(): global situational_checked global normative_checked global topological_checked + global simulation_frames """Entry point; the view for the main page""" if 'new_world' in request.form: cone_number = int(request.form['cone_number']) world = World(cone_number) + simulation_frames = {} if "run_simulation" in request.form: + simulation_frames = {} no_cultural_k = False normative_k = False topological_k = False @@ -81,6 +86,7 @@ def main(): @app.route('/main.png') def main_plot(): global world + print("world: " + str(world)) """The view for rendering the scatter chart""" if not world: world = World(3) @@ -96,5 +102,15 @@ def time_plot(frame_id): return base64.b64encode(simulation_frames[int(frame_id)].getvalue()) +@app.route('/fitness.png') +def fitness_plot(): + print("fitness plot :" + str(population)) + if population: + img = population.plot_fitness() + return send_file(img, mimetype='image/png', cache_timeout=0) + else: + return '', 204 + + if __name__ == '__main__': app.run() diff --git a/src/__pycache__/population.cpython-37.pyc b/src/__pycache__/population.cpython-37.pyc index 7f61b2c8384765b7d859da5788da03b21bb24fb0..54a0d7d7e8f246da6741429b795e76d65e055f1e 100644 Binary files a/src/__pycache__/population.cpython-37.pyc and b/src/__pycache__/population.cpython-37.pyc differ diff --git a/src/__pycache__/world.cpython-37.pyc b/src/__pycache__/world.cpython-37.pyc index acac66c4f302c1cd111f5e3cf7ff16c6567b94be..5fc9c75c084a66d45a51b86a3c454e8f6cc34ccd 100644 Binary files a/src/__pycache__/world.cpython-37.pyc and b/src/__pycache__/world.cpython-37.pyc differ diff --git a/src/control.py b/src/control.py index 687d9b025f823abe388981d13484dce1928fd928..78e528daa1cd7c94b0a2dc4b140ed973e1d70cbc 100644 --- a/src/control.py +++ b/src/control.py @@ -10,6 +10,7 @@ def main(): print("iteration: " + str(i)) population.plot(True) population.new_generation() + population.plot_fitness(True) if __name__ == '__main__': diff --git a/src/population.py b/src/population.py index 571e357b6fc68038e0010372dacdadd326915a04..3a6ff303389d62fd37fb821473352566d66c3a10 100644 --- a/src/population.py +++ b/src/population.py @@ -24,6 +24,7 @@ class Population: self.x[i] = (random() * world.size_x) self.y[i] = (random() * world.size_y) self.fitness[i] = (world.height(self.x[i], self.y[i])) + self.population_fitness = [] # cultural components self.situational = None @@ -111,6 +112,7 @@ class Population: self.normative.influence(self) if self.topological: self.topological.influence(self) + self.population_fitness.append(np.sum(self.fitness) / self.individuals_n) def evolve(self): """Generates the next generation of individuals""" @@ -191,7 +193,6 @@ class Population: def_y = self.y[self.knowledge == 0] print("numbers sit:" + str(len(sit_x)) + " norm: " + str(len(norm_x)) + " top: " + str(len(top_x)) + " def: " + str(len(def_x))) print("total " + str(len(sit_x)+len(norm_x)+len(top_x)+len(def_x))) - plt.clf() fig, ax = plt.subplots() ax.set_xlim(0, self.world.size_x) ax.set_ylim(0, self.world.size_y) @@ -205,9 +206,30 @@ class Population: if plot_vs_return: plt.show() + plt.clf() + plt.cla() + plt.close() else: img = BytesIO() plt.savefig(img, format='png') - img.seek(0) + plt.clf() + plt.cla() plt.close(fig) + img.seek(0) + return img + + def plot_fitness(self, plot_vs_return=False): + fit_fig, fit_ax = plt.subplots() + fit_ax.plot(range(len(self.population_fitness)), self.population_fitness) + plt.xlabel("iteration") + plt.ylabel("average fitness") + + if plot_vs_return: + plt.show() + plt.close(fit_fig) + else: + img = BytesIO() + fit_fig.savefig(img, format='png') + plt.close(fit_fig) + img.seek(0) return img diff --git a/src/world.py b/src/world.py index 343d7aefe878503e6fc17ce919a239a6d79ee0fd..c9d0399baa399d972d5cd7d3665a02ffbc65ee55 100644 --- a/src/world.py +++ b/src/world.py @@ -38,22 +38,23 @@ class World: def get_plot(self, plot_vs_return): - plt.clf() - fig = plt.figure() - ax = Axes3D(fig) + world_fig = plt.figure() + world_ax = Axes3D(world_fig) x = np.linspace(0, self.size_x, 100) y = np.linspace(0, self.size_y, 100) x_grid, y_grid = np.meshgrid(x, y) z = np.reshape(np.array(list(map(self.height, np.ravel(x_grid), np.ravel(y_grid)))), (100, 100)) - # Plot a basic wireframe. - ax.plot_surface(x_grid, y_grid, z) + # Plot a basic wire frame. + world_ax.plot_surface(x_grid, y_grid, z) if plot_vs_return: plt.show() + plt.close(world_fig) else: img = BytesIO() plt.savefig(img) + plt.close(world_fig) img.seek(0) return img diff --git a/templates/main.html b/templates/main.html index 3c668382ee769e07da126d9410801a1a5bb0d282..7bec8877170da2e4273c1cb1a9d9fb7fad8f5972 100644 --- a/templates/main.html +++ b/templates/main.html @@ -10,6 +10,15 @@ <script>window.jQuery || document.write('<script src="{{ url_for('static', filename='jquery.js') }}">\x3C/script>') $SCRIPT_ROOT = {{ request.script_root|tojson|safe }}; + $.ajax({ + dataType: "text", + url: $SCRIPT_ROOT + '/img/0', + data: "null", + success: function(data) { + console.log("fuck"); + $("#time_plot").attr("src", "data:image/png;base64," + data); + } + }); </script> <script type=text/javascript> $(function() { @@ -36,7 +45,8 @@ }); </script> <div id="element1"> - <img src="{{ url_for('main_plot') }}" alt="Image"> + <p>Problem domain:</p><br> + <img src="{{ url_for('main_plot') }}" alt="cone world"> </div> <div id="element2"> <form method="post"> @@ -53,10 +63,14 @@ </form> </div> <div class="slidecontainer"> - <img class="image" alt="Image" id="time_plot" src="?"><br> + <img class="image" alt="Population per year" id="time_plot" src="?"><br> <input type="range" min="0" max="{{ iterations }}" value="0" class="slider" id="myRange"><br> <p>Year: <span id="current_frame"></span></p> </div> + <div id="element3"> + <p>Average fitness:</p><br> + <img src="{{ url_for('fitness_plot') }}" alt="Run simulation to show the average fitness"> + </div> </body> </html> \ No newline at end of file