diff --git a/app.py b/app.py index 7aec1b1255c2fd651b74cc7449ff6ce1bb5d6e47..b24051d4d207e7031c4b3cc4e8f35ee43b807674 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 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 a79a8db1e351088cb712108ea33b5180b3e50229..acac66c4f302c1cd111f5e3cf7ff16c6567b94be 100644 Binary files a/src/__pycache__/world.cpython-37.pyc and b/src/__pycache__/world.cpython-37.pyc differ diff --git a/src/population.py b/src/population.py index b8ba82d8d03aaf6cc68ce1dbf8de81833d674498..571e357b6fc68038e0010372dacdadd326915a04 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 f954e0ce73c8f290dbf8f9eea588234e5eada4e8..53e80adcef26bc00c4d766edc4bdb750d2b51cf0 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 7dc47c1c8db75f166a07bec6f25302c840444764..343d7aefe878503e6fc17ce919a239a6d79ee0fd 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 22309f1c1a31a12acbc4560df3f9279abf1fc157..3c668382ee769e07da126d9410801a1a5bb0d282 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>