From 53553c8806ec57f3e666c46c5dbece1ed14a691d Mon Sep 17 00:00:00 2001 From: Philipp Zander <p.raubfisch@gmx.de> Date: Mon, 2 Dec 2019 20:43:46 -0300 Subject: [PATCH] interface improvements, topological knowledge --- app.py | 51 +++++++++++++++++++--- src/__pycache__/population.cpython-37.pyc | Bin 5834 -> 5938 bytes src/__pycache__/world.cpython-37.pyc | Bin 2076 -> 2121 bytes src/population.py | 6 ++- src/topologicalKnowledge.py | 25 ++++++++++- src/world.py | 4 +- templates/main.html | 16 +++---- 7 files changed, 82 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index 7aec1b1..b24051d 100644 --- a/app.py +++ b/app.py @@ -9,14 +9,34 @@ app = Flask(__name__) world = None simulation_frames = {} +cone_number = 30 +iterations = 50 +population_size = 100 +mutation_step_size = 0.1 +no_cultural_checked = "" +situational_checked = "checked" +normative_checked = "" +topological_checked = "checked" + @app.route('/', methods=["GET", "POST"]) def main(): global world - iterations = 0 + global iterations + global population_size + global mutation_step_size + global cone_number + global no_cultural_checked + global situational_checked + global normative_checked + global topological_checked + """Entry point; the view for the main page""" + if 'new_world' in request.form: - world = World(int(request.form['cone_number'])) + cone_number = int(request.form['cone_number']) + world = World(cone_number) + if "run_simulation" in request.form: no_cultural_k = False normative_k = False @@ -24,21 +44,38 @@ def main(): situational_k = False if 'normative_k' in request.form: normative_k = True + normative_checked = "checked" + else: + normative_checked = "" if 'topological_k' in request.form: topological_k = True + topological_checked = "checked" + else: + topological_checked = "" if 'situational_k' in request.form: situational_k = True + situational_checked = "checked" + else: + situational_checked = "" if 'no_cultural_k' in request.form: no_cultural_k = True - population = Population(int(request.form['population_size']), world, default=no_cultural_k, + no_cultural_checked = "checked" + else: + no_cultural_checked = "" + population_size = int(request.form['population_size']) + mutation_step_size = float(request.form['mutation_step_size']) + iterations = int(request.form['iterations']) + population = Population(population_size, world, default=no_cultural_k, situational=situational_k, normative=normative_k, topological=topological_k, - mutation_step_size=float(request.form['mutation_step_size'])) - for i in range(int(request.form['iterations'])): + mutation_step_size=mutation_step_size) + for i in range(iterations + 1): simulation_frames[i] = population.plot(False) population.new_generation() - iterations = int(request.form['iterations']) - 1 - return render_template('main.html', iterations=iterations) + return render_template('main.html', iterations=iterations, population_size=population_size, + mutation_step_size=mutation_step_size, cone_number=cone_number, + no_cultural_checked=no_cultural_checked, situational_checked=situational_checked, + normative_checked=normative_checked, topological_checked=topological_checked) @app.route('/main.png') diff --git a/src/__pycache__/population.cpython-37.pyc b/src/__pycache__/population.cpython-37.pyc index edf73248a8e10d6aa913f3b538756b077d4315db..7f61b2c8384765b7d859da5788da03b21bb24fb0 100644 GIT binary patch delta 325 zcmX@5yGf7FiI<m)fq{Wx#{8$TCJGz*e(*CgZe|yFz{q%M^9#WY#(M57z8Z!s{wx77 zDVV~aB9J23%fiS|!nQytg>NAvm?fMd1Y*@NPGBrrRKm7EWFZ41L!M5FXfs1CPYGL= zSPf5#aEeGT6C*<n&jRrj7Le)^wgnP3Tp(5rPcTCYLokD;s9zDtIYp`r3=BnT3=9mD zR}1@UGiu#pP0C43&emiqas|mafe1kc28LVA1$pV3jJMd*@{4j4OEe`XbBM&b6^S!2 zFhsE>r52aOS3v2?TWl3MiAkwBx7aEnG)r+tet8iS$Q1VC#In@1%=B9<#i^;;w^);N t@{3a^w~JIVT1;jX)!<^3V&r1vVq#<DnyfF{$QUzuqi7wY&twC!Spc;YS1|wp delta 189 zcmdm_cS@JfiI<m)fq{Wxz2%eG1M(aBe(*CgY-SgDz{t36^9#WYMsLOmj74=NYzz1o zGB7ffuq_a%;abSZ$dIQ~BG}AO%TvOZB~-(c!k;40%f!e~!?Qp*g#{#!P+7wh%#gwm z%%CYadB1S10CPcJx+dc-wzT}B+{6-1k;(2NalE%!iZk-dZ?Pul<QJz-z91qp`L#$T iqsin{Q4KaGHbxFcp2^ci8yO=eON!ModQF}oHVXiPUo~_9 diff --git a/src/__pycache__/world.cpython-37.pyc b/src/__pycache__/world.cpython-37.pyc index a79a8db1e351088c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)YNQEt|Aezl?6HZCGlm&@kOa6rA2vBllQTzOM<Ng6JUEe(o;*~ zLF!Z||6>hP21#f#g0*9~uqeMIu_V=Maygrm1;hy;cL;+BArK(}BBVit9*EEf5g_xB z4VwIsO^%UcvH*LCCPE7A5U!Hc;*$9C{Gyx`J5V67fC7P$gR#hTaxc57qC6;&1alKh uKyJ;+Owuc;1hH<hLFgjc$&cApq)b6}8h{9d>0p-lWJL~TR$~rU4n_cpPJY+` diff --git a/src/population.py b/src/population.py index b8ba82d..571e357 100644 --- a/src/population.py +++ b/src/population.py @@ -172,7 +172,7 @@ class Population: roulette = random() r_sum = probability[0] choice = 0 - while r_sum < roulette: + while r_sum <= roulette: choice += 1 r_sum += probability[choice] result[x] = choice @@ -199,10 +199,12 @@ class Population: ax.scatter(def_x, def_y, marker='v') ax.scatter(sit_x, sit_y, marker='+') ax.scatter(top_x, top_y, marker='x') + ax.scatter(self.situational.best_x, self.situational.best_y, marker="*", c='black') + plt.xlabel("x") + plt.ylabel("y") if plot_vs_return: plt.show() - plt.close(fig) else: img = BytesIO() plt.savefig(img, format='png') diff --git a/src/topologicalKnowledge.py b/src/topologicalKnowledge.py index f954e0c..53e80ad 100644 --- a/src/topologicalKnowledge.py +++ b/src/topologicalKnowledge.py @@ -1,12 +1,33 @@ +import sys +from random import random + +import numpy as np + + class TopologicalKnowledge: # 3 def __init__(self): self.weight = 0 + self.nx = 10 + self.ny = 10 + self.topology = np.zeros((self.nx, self.ny)) def accept(self, population): - pass + self.topology = np.zeros((self.nx, self.ny)) + for i in range(population.individuals_n): + self.topology[min(int(population.x[i]), 9)][min(int(population.y[i]), 9)] += 1 def influence(self, population): - pass \ No newline at end of file + sparse = np.where( self.topology == np.min(self.topology)) + for i in range(population.individuals_n): + if population.knowledge[i] == 3: + if random() < 0.5: + index = np.random.choice(len(sparse[0])) + population.x[i] = sparse[0][index] + random() * 10 / self.nx + population.y[i] = sparse[1][index] + random() * 10 / self.ny + + # print(self.topology) + # print(np.sum(self.topology)) + # print(sparse) diff --git a/src/world.py b/src/world.py index 7dc47c1..343d7ae 100644 --- a/src/world.py +++ b/src/world.py @@ -19,7 +19,7 @@ class World: # list of tuples (peak_x, peak_y, height, gradient) self.cones = [] if cones_n is None: - self.cones.append((5, 5, 10, 1)) + self.cones.append((8, 8, 10, 1)) else: for cone in range(cones_n): self.cones.append((random() * self.size_x, random() * self.size_y, @@ -28,6 +28,8 @@ class World: def height(self, x_position, y_position): height_max = 0.0 + if x_position >= 10 or x_position < 0 or y_position >= 10 or y_position < 0: + return height_max for cone in self.cones: height_current = cone[2] - sqrt(pow(x_position - cone[0], 2) + pow(y_position - cone[1], 2)) * cone[3] if height_current > height_max: diff --git a/templates/main.html b/templates/main.html index 22309f1..3c66838 100644 --- a/templates/main.html +++ b/templates/main.html @@ -41,14 +41,14 @@ <div id="element2"> <form method="post"> <input type="submit" name="new_world" value="Create new world"> - Cones: <input type="text" name="cone_number" value="5"><br> - Population size: <input type="text" name="population_size" value="200"><br> - Iterations: <input type="text" name="iterations" value="10"><br> - Maximum mutation step: <input type="text" name="mutation_step_size" value="0.1"><br> - <input type="checkbox" name="no_cultural_k" value="no cultural knowledge" checked> no cultural knowledge (v)<br> - <input type="checkbox" name="situational_k" value="situational knowledge" checked> situational knowledge (+)<br> - <input type="checkbox" name="normative_k" value="normative knowledge" checked> normative knowledge (o)<br> - <input type="checkbox" name="topological_k" value="topological knowledge" checked> topological knowledge (x)<br> + Cones: <input type="text" name="cone_number" value="{{ cone_number }}"><br> + Population size: <input type="text" name="population_size" value="{{ population_size }}"><br> + Iterations: <input type="text" name="iterations" value="{{ iterations }}"><br> + Maximum mutation step: <input type="text" name="mutation_step_size" value="{{ mutation_step_size }}"><br> + <input type="checkbox" name="no_cultural_k" value="no cultural knowledge" {{ no_cultural_checked }}> no cultural knowledge (v)<br> + <input type="checkbox" name="situational_k" value="situational knowledge" {{ situational_checked }}> situational knowledge (+)<br> + <input type="checkbox" name="normative_k" value="normative knowledge" {{ normative_checked }}> normative knowledge (o)<br> + <input type="checkbox" name="topological_k" value="topological knowledge" {{ topological_checked }}> topological knowledge (x)<br> <input type="submit" name="run_simulation" value="Run simulation"><br> </form> </div> -- GitLab