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: 8 additions & 0 deletions DoubtLevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from enum import Enum


class DoubtLevel(Enum):
S1 = 1
S2 = 2
S3 = 3
S4 = 4
60 changes: 40 additions & 20 deletions ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
from typing import Dict, Tuple, List
import typing

from DoubtLevel import DoubtLevel
from persons_location_generator import PersonsLocationGenerator

Location = typing.NamedTuple("Location", [("x", int), ("y", int)])

MATRIX_SIZE = 100

P = 1 # population density

class DoubtLevel(Enum):
S1 = 1
S2 = 2
S3 = 3
S4 = 4

P = 0.64 # population density

MIN_DOUBT_LEVEL = 1

Expand Down Expand Up @@ -184,8 +180,12 @@ def __init__(
n_cols: int,
population_density: float,
persons_distribution: Dict[DoubtLevel, float],
cool_down_l: int
cool_down_l: int,
location_shape:str,
distribution_rule:str,
location_generator = PersonsLocationGenerator(),
):
self.location_generator = location_generator
self.cool_down_l = cool_down_l
self.doubt_level_locations_dict = None
self.persons_location = None
Expand All @@ -203,7 +203,7 @@ def __init__(
self._persons_distribution = persons_distribution
self._matrix: List[List[Cell]] = self._create_matrix(n_rows=n_rows, n_cols=n_cols)
self._num_dimensions = 2
self.init_matrix()
self.init_matrix(location_shape=location_shape,distribution_rule=distribution_rule)

@staticmethod
def _create_matrix(n_rows: int, n_cols: int) -> typing.List[typing.List[typing.Any]]:
Expand Down Expand Up @@ -243,19 +243,37 @@ def _sample_for_each_doubt_level(persons_location, persons_distribution):
)
return doubt_level_locations_dict

def init_matrix(self):
def init_matrix(self, location_shape, distribution_rule):
# init matrix with cells
n_person_cells = int(self._n_cols * self._n_rows * self._population_density)

self.persons_location = random.sample(
population=list(product(range(self._n_rows), range(self._n_cols))),
k=n_person_cells,
)

self.doubt_level_locations_dict = self._sample_for_each_doubt_level(
persons_location=self.persons_location,
persons_distribution=self._persons_distribution,
)
if location_shape == 'random':
self.persons_location = self.location_generator.random_locations(n_person_cells=n_person_cells,
n_cols=self._n_cols,
n_rows=self._n_rows)
elif location_shape == 'line':
self.persons_location = self.location_generator.lines_location(n_person_cells=n_person_cells,
n_cols=self._n_cols,
n_rows=self._n_rows)
elif location_shape == 'square':
self.persons_location = self.location_generator.square_location(n_person_cells=n_person_cells,
n_cols=self._n_cols,
n_rows=self._n_rows)

if distribution_rule == 'space':
self.doubt_level_locations_dict = self.location_generator.doubt_sample_easy_believer_next_to_not(persons_location=self.persons_location)
elif distribution_rule == 'k_space':
self.doubt_level_locations_dict = self.location_generator.doubt_sample_easy_believer_next_to_k_hard_believers(
persons_location=self.persons_location)
elif distribution_rule == 'line_space':
self.doubt_level_locations_dict =self.location_generator.doubt_sample_line_between_easy_believer_hard_believers(
persons_location=self.persons_location,easy_doubt=[DoubtLevel.S1],hard_doubt=[DoubtLevel.S4])
else:
# default
self.doubt_level_locations_dict = self._sample_for_each_doubt_level(
persons_location=self.persons_location,
persons_distribution=self._persons_distribution,
)
self._init_matrix_cells(
doubt_level_locations_dict=self.doubt_level_locations_dict
)
Expand Down Expand Up @@ -392,6 +410,8 @@ def calculate_percentage_of_believeres(self):
n_cols=MATRIX_SIZE,
population_density=P,
persons_distribution=PERSONS_DISTRIBUTION,
cool_down_l=4,
location_shape='square',distribution_rule='space'
)
for i in range(100):
print(f"turn {i}==================")
Expand Down
93 changes: 93 additions & 0 deletions persons_location_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import math
import random
from itertools import product
from typing import List

from DoubtLevel import DoubtLevel

class PersonsLocationGenerator:
@staticmethod
def random_locations(n_person_cells=None, n_cols=None, n_rows=None):
persons_location = random.sample(
population=list(product(range(n_rows), range(n_cols))),
k=n_person_cells,
)
return persons_location

@staticmethod
def lines_location(n_person_cells=None, n_cols=None, n_rows=None):
counter = 0
locations = []
for i in range(n_rows):
for j in range(n_cols):
if counter >= n_person_cells:
return locations
locations.append((i,j))
counter += 1
return locations

@staticmethod
def square_location(n_person_cells=None, n_cols=None, n_rows=None):
locations = []
root = int(math.floor(math.sqrt(n_person_cells)))
assert math.pow(root,2) == n_person_cells, "number of persons cell when square shape used should be n^2 for natural n"
margin_row = int((n_cols-root)/2)
margin_col = int((n_rows-root)/2)
for i in range(margin_row, margin_row+root):
for j in range(margin_col, margin_col+root):
locations.append((i, j))
return locations

@staticmethod
def doubt_sample_easy_believer_next_to_not(persons_location):
doubt_level_locations_dict = {}
for i, location in enumerate(persons_location):
if i % 2 == 0:
doubt_level_locations_dict[location] = DoubtLevel.S1
else:
doubt_level_locations_dict[location] = DoubtLevel.S4
return doubt_level_locations_dict

@staticmethod
def doubt_sample_easy_believer_next_to_k_hard_believers(persons_location,k=3):
doubt_level_locations_dict = {}
for i, location in enumerate(persons_location):
if i % k == 0:
doubt_level_locations_dict[location] = DoubtLevel.S1
else:
doubt_level_locations_dict[location] = DoubtLevel.S3
return doubt_level_locations_dict

@staticmethod
def doubt_sample_line_between_easy_believer_hard_believers(persons_location, easy_doubt: List, hard_doubt: List):
doubt_level_locations_dict = {}
for i, location in enumerate(persons_location):
if location[0] % 4 == 0:
doubt_level_locations_dict[location] = random.choice(easy_doubt)
else:
doubt_level_locations_dict[location] = random.choice(hard_doubt)
return doubt_level_locations_dict

@staticmethod
def merge_doubt_dict(first, second):
di = dict(second)
for k, v in first.items():
di[k] = v
return di
def _sample_for_each_doubt_level(persons_location, persons_distribution):
n_persons = len(persons_location)
n_doubt_level_dict = EnvMap._get_n_doubt_level_dict(
number_of_persons=n_persons, persons_distribution=persons_distribution
)
doubt_level_locations_dict = {}
for doubt_level in DoubtLevel:
n_doubt_level = n_doubt_level_dict[doubt_level]
n_doubt_level_randomized_locations = random.sample(
persons_location, k=n_doubt_level
)
for loca in n_doubt_level_randomized_locations:
doubt_level_locations_dict[loca] = doubt_level
persons_location = list(
set(persons_location) - set(n_doubt_level_randomized_locations)
)
return doubt_level_locations_dict
5 changes: 4 additions & 1 deletion pygame_frontend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pygame

from ex1 import EnvMap
from ex1 import L,P,PERSONS_DISTRIBUTION,MATRIX_SIZE
from ex1 import P,PERSONS_DISTRIBUTION,MATRIX_SIZE

class Board:
def __init__(self, board_size, tile_size, env_map: EnvMap):
Expand Down Expand Up @@ -108,6 +108,9 @@ def run(self):
n_cols=MATRIX_SIZE,
population_density=P,
persons_distribution=PERSONS_DISTRIBUTION,
cool_down_l=4,
location_shape='square', distribution_rule='space'

)

# Create a new Board instance with a board size of MATRIX_SIZE and a tile size of 50
Expand Down
12 changes: 6 additions & 6 deletions report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ def raw_stats_to_growth(raw_stats):



def plot_experiment(graph, label: str, times=None,cool_down=None):
def plot_experiment(graph, label: str, times=None,cool_down=None,shape=None,dist=None,p=P):
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.title(f"repeated experiment :={times} P:={p} cool_down:={cool_down} shape:={shape} dist={dist}", fontsize=10)
plt.suptitle("Rumors statistics graph", fontsize=20)
plt.show()

Expand All @@ -85,8 +85,8 @@ def main(env_map_creator: Callable[...,EnvMap],times=10) -> None:
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)
plot_experiment(avg_believers, label="average believers", times=times,cool_down=4, shape='square', dist='3 lines space',p=P)
plot_experiment(avg_growth, label="average growth", times=times,cool_down=4,shape='square', dist='3 lines space',p=P)

def create_env_map(cool_down):
return EnvMap(
Expand All @@ -95,10 +95,10 @@ def create_env_map(cool_down):
population_density=P,
persons_distribution=PERSONS_DISTRIBUTION,
cool_down_l=cool_down,
location_shape='square', distribution_rule='line_space'
)


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