Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
MATRIX_SIZE = 100

P = 1 # population density
L = 10


class DoubtLevel(Enum):
S1 = 1
Expand Down Expand Up @@ -88,7 +86,7 @@ def __init__(
self,
state,
position,
cool_down_episode_countdown: int = L):
cool_down_episode_countdown):
super().__init__(state=state.value, position=position)
self._probability_to_believe = PROBABILITY_TO_BELIEVE[state]
self._doubt_level = state
Expand Down Expand Up @@ -186,7 +184,9 @@ def __init__(
n_cols: int,
population_density: float,
persons_distribution: Dict[DoubtLevel, float],
cool_down_l: int
):
self.cool_down_l = cool_down_l
self.doubt_level_locations_dict = None
self.persons_location = None
self._n_rows = n_rows
Expand Down Expand Up @@ -267,7 +267,7 @@ def _init_matrix_cells(self, doubt_level_locations_dict: Dict[Tuple[int, int], D
self._matrix[x][y] = PersonCell(
state=doubt_level,
position=Location(x=x, y=y),
)
cool_down_episode_countdown=self.cool_down_l)
for row in range(self._n_rows):
for col in range(self._n_cols):
if self._matrix[row][col] is None:
Expand Down
48 changes: 48 additions & 0 deletions figure_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np
import matplotlib.pyplot as plt


def plot_experiment(growth, believers, times=None, size=None):
if size is None:
size = len(growth)
plt.plot(np.arange(0, size), growth, label="average growth", color='blue', marker=".", markersize=5)
# plt.plot(np.arange(0, size), believers, label="average believers", color='olive', marker=".", markersize=5)

plt.legend()
plt.title(f"repeated experiment :={times}", fontsize=10)
plt.suptitle("Rumors statistics graph", fontsize=20)
plt.show()


if __name__ == '__main__':
# TODO- growth is just a moq here.
average_growth = [193.33333333333337, 100.0, 79.24222320507151, 59.669774669774675, 46.970017230004544, 36.99652777777777, 32.82867310847102, 24.860085916992144, 22.81534676063843, 17.397486062081352, 17.903296309789695, 16.37275365511349, 14.575498594040559, 14.229832631131378, 13.864861961792778, 12.442980654101541, 11.61401134664488, 10.695049611295081, 10.993255817076077, 10.139359992140227, 9.678088656409814, 9.13223907374223, 8.843964121244094, 8.234181386854805, 7.806958718458997, 7.6010711912229185, 7.143834894222198, 6.488350853676337, 6.181228664919922, 5.538633638752102, 5.646309123757667, 5.458857949865897, 5.176192369404774, 4.849344557834169, 4.888544734178689, 4.651530571015955, 4.313374937785832, 4.075411434250982, 4.104679457964651, 4.083094010573979, 3.734984023960108, 3.23866410174052, 3.2558040355007765, 3.0098753122221003, 3.2181012410074428, 2.9256769734892156, 2.8769915831199135, 2.736147785161819, 2.8071389049394067, 2.678124345625458, 2.662740317059443, 2.907145425785121, 2.7636123429551325, 2.6764236709163107, 2.61919397319094, 2.468197600786283, 2.4995563050084058, 2.446386579981833, 2.1956674355768526, 2.003532666245132, 1.9085226484359037, 1.879645713528187, 1.7422203056475254, 1.8019004813549333, 1.762572468135476, 1.7394087242229042, 1.7366244360317193, 1.6441506073994117, 1.4110715801495795, 1.5103929035603745, 1.4629555708041255, 1.4212698666515677, 1.2241006313204332, 1.2258067881359038, 1.1201503086941527, 0.9389453293561542, 0.8432312591061075, 0.7085713859725408, 0.6438017145563447, 0.5944157908280138, 0.5443432758159367, 0.5777921281670665, 0.5383315024102504, 0.4517694832506945, 0.4488036891355387, 0.411116069702977, 0.45180971634783756, 0.43669754746713596, 0.44946367665472436, 0.42273460267714763, 0.38191647242119514, 0.3145598663946154, 0.23935893433239733, 0.23076393194186262, 0.21891568325636945, 0.2004315699861017, 0.1418870984088369, 0.07737074040434518, 0.0738354141495496, 0.0738354141495496]
# average_believers_per_turn
average_believers_per_turn = [0.00052, 0.0013399999999999998, 0.00268, 0.00455, 0.00704, 0.01003,
0.013519999999999999, 0.01736, 0.02201,
0.026970000000000004, 0.03252, 0.038470000000000004, 0.044649999999999995,
0.05136999999999999, 0.05887,
0.06677000000000001, 0.07504000000000001, 0.08354999999999999, 0.09246,
0.10127000000000001,
0.11062999999999998, 0.12023999999999999, 0.13036, 0.14139, 0.15233000000000002,
0.16376000000000002,
0.17584, 0.18788, 0.20019, 0.21336, 0.22606, 0.23893, 0.2519, 0.26515000000000005,
0.27843, 0.2913,
0.30483000000000005, 0.31804, 0.33131000000000005, 0.34450000000000003, 0.35773,
0.37124999999999997,
0.38476, 0.39785999999999994, 0.41176, 0.42595, 0.43986000000000003,
0.4529500000000001,
0.4663899999999999, 0.47969999999999996, 0.49367, 0.50809, 0.52227,
0.5368200000000001, 0.55128,
0.5659099999999999, 0.57995, 0.59457, 0.60962, 0.6247699999999999, 0.63929,
0.6537300000000001, 0.66783,
0.68256, 0.69709, 0.7115, 0.72567, 0.7389300000000001, 0.7516200000000001, 0.76493,
0.77789, 0.79091,
0.8038000000000001, 0.81587, 0.82774, 0.8390599999999999, 0.85025, 0.8609399999999999,
0.8703999999999998,
0.8796700000000002, 0.88918, 0.8979500000000001, 0.9065700000000001, 0.91519,
0.9228900000000001, 0.9301,
0.93743, 0.9446900000000001, 0.95137, 0.9575699999999999, 0.9638899999999999, 0.97008,
0.97589, 0.98079,
0.98486, 0.9887, 0.99153, 0.99375, 0.99526, 0.99656]
plot_experiment(growth=average_growth, believers=average_believers_per_turn, times=10)
104 changes: 104 additions & 0 deletions report_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from typing import Callable

from ex1 import EnvMap, MATRIX_SIZE, P, PERSONS_DISTRIBUTION
import matplotlib.pyplot as plt
import numpy as np


def calc_spread_rate(env_map: EnvMap):
count = 0
for i in range(env_map._n_rows):
for j in range(env_map._n_cols):
if env_map._matrix[i][j].did_hear_rumour_sometime():
count += 1
return count / len(env_map.persons_location)


def run_experiment_multiple_times(env_map_creator: Callable[...,EnvMap], times):
raw_stats = []
for t in range(times):
believers = []
env_map = env_map_creator()
for i in range(100):
print(f"turn {i}==================")
env_map.spread_rumor()
believers.append(env_map.calculate_percentage_of_believeres())
raw_stats.append(believers)
return raw_stats


def calc_growth(population):
growth = []
for pop in range(1, len(population)):
numbers = (population[pop] - population[pop - 1]) / population[pop - 1] * 100
growth.append(numbers)
return growth


def calc_average_per_turn(raw_stats):
total_avgs = []
for turn_number in range(len(raw_stats[0])):
sum_for_turn = 0
for population in raw_stats:
sum_for_turn = sum_for_turn + population[turn_number]
total_avgs.append(sum_for_turn / len(raw_stats))
return total_avgs


def raw_stats_to_growth(raw_stats):
growth = []
for population in raw_stats:
growth.append(calc_growth(population))
return growth




def plot_experiment(graph, label: str, times=None,cool_down=None):
size = len(graph)

plt.plot(np.arange(0, size), graph, label=label, color='blue', marker=".", markersize=5)

plt.legend()
plt.title(f"repeated experiment :={times} cool_down:={cool_down}", fontsize=10)
plt.suptitle("Rumors statistics graph", fontsize=20)
plt.show()



def main(env_map_creator: Callable[...,EnvMap],times=10) -> None:
raw_stats = run_experiment_multiple_times(env_map_creator, times)

for believers_percentage in raw_stats:
print(f"population belivers percentage:{believers_percentage}")
print()

avg_believers = calc_average_per_turn(raw_stats)
print(f"Average believers per turn:={avg_believers}")

growth_all = raw_stats_to_growth(raw_stats)
avg_growth = calc_average_per_turn(growth_all)

for cnt, growth in enumerate(growth_all):
print(f"turn {cnt} =============")
print(growth)
print()
print(f"Average growth per turn:={avg_growth}")

plot_experiment(avg_believers, label="average believers", times=times,cool_down=cool_down)
plot_experiment(avg_growth, label="average growth", times=times,cool_down=cool_down)

def create_env_map(cool_down):
return EnvMap(
n_rows=MATRIX_SIZE,
n_cols=MATRIX_SIZE,
population_density=P,
persons_distribution=PERSONS_DISTRIBUTION,
cool_down_l=cool_down,
)


if __name__ == "__main__":
for cool_down in [2,3,4,6,8,10,1]:
main(lambda: create_env_map(cool_down))