Skip to content
Snippets Groups Projects
Commit fc5c0f18 authored by Torben Nattermann's avatar Torben Nattermann
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
index.py 0 → 100644
import numpy as np
import random
import matplotlib.pyplot as plt
class HysteresisAgent:
def __init__(self, initial_state, n_iterations, n_grid, m_grid, p_global_factor, p_local_factor=0.125, verbose=False):
self.initial_state = initial_state
self.reverse_state = 1 if self.initial_state == 0 else 0
self.n_iterations = n_iterations
self.n_grid = n_grid
self.m_grid = m_grid
self.p_global_factor = p_global_factor
self.p_local_factor = p_local_factor
self.grid = np.full((n_grid, m_grid), initial_state)
self.entropies_1 = []
self.entropies_2 = []
self.verbose = verbose
def run(self):
terminated = self.step_iterator(self.entropies_1)
if terminated: # reverse states and repeat
self.initial_state = self.reverse_state
self.reverse_state = 1 if self.initial_state == 0 else 0
self.step_iterator(self.entropies_2)
self.plotter()
if self.verbose:
print('\n-------------\nExperiment terminated')
print(f'Iterations first run: {len(self.entropies_1)}')
print(f'Iterations second run: {len(self.entropies_2)}')
print(f'Entropy Scores first run: {self.entropies_1}')
print(f'Entropy Scores first run: {self.entropies_2}')
def step_iterator(self, storage):
for iteration in range(self.n_iterations):
if iteration != 0 and iteration % 10 == 0: # print every 10th state of the grid
if self.verbose:
print(self.grid)
entropy = self.step(iteration)
storage.append(entropy)
if entropy == 1: # terminate early if system is fully flipped
return True
return False
def step(self, iteration):
for n in range(self.n_grid):
for m in range(self.m_grid):
if self.grid[n][m] == self.initial_state: # exclude flipped units from consideration
p_local = self.compute_p_local(n,m)
flip_probability = (iteration+1)*self.p_global_factor + p_local
self.grid[n][m] = self.reverse_state if random.random() < flip_probability else self.initial_state
return (self.grid == self.reverse_state).sum() / (self.n_grid*self.m_grid) # entropy measure
def compute_p_local(self, n, m):
flipped_neighbours = 0
if n+1 <= self.n_grid-1:
if self.grid[n+1][m] == self.reverse_state:
flipped_neighbours +=1
if n-1 >= 0:
if self.grid[n-1][m] == self.reverse_state:
flipped_neighbours +=1
if m+1 <= self.m_grid-1:
if self.grid[n][m+1] == self.reverse_state:
flipped_neighbours +=1
if m-1 >= 0:
if self.grid[n][m-1] == self.reverse_state:
flipped_neighbours +=1
return flipped_neighbours*self.p_local_factor
def plotter(self):
x1 = range(len(self.entropies_1))
x2 = range(len(self.entropies_2))
flipped = np.flip(self.entropies_2) # flip array for backpass
reverse = 1-flipped
plt.plot(x1, self.entropies_1, label='First Run')
plt.plot(x2, reverse, label='Reverse Run')
plt.xlabel('Iterations')
plt.ylabel('Entropy')
plt.title(f'Hysteresis Curves (p_local:{self.p_local_factor}, p_global:{self.p_global_factor}, grid:{self.n_grid}X{self.m_grid})')
plt.legend()
plt.savefig(f'plots/Hysteresis_{self.n_grid}X{self.m_grid}_loc{self.p_local_factor}_glob{self.p_global_factor}.png')
if __name__ == "__main__":
agent = HysteresisAgent(initial_state = 1, n_iterations = 1000, n_grid = 150, m_grid = 150, p_global_factor = 0.005, p_local_factor = 0.005, verbose=True)
agent.run()
plots/Hysteresis_100X100_loc0.0125_glob0.001.png

34.3 KiB

plots/Hysteresis_100X100_loc0.125_glob0.001.png

34 KiB

plots/Hysteresis_100X100_loc0.125_glob0.01.png

34.4 KiB

plots/Hysteresis_150X150_loc0.001_glob0.001.png

34.9 KiB

plots/Hysteresis_150X150_loc0.005_glob0.005.png

34.3 KiB

plots/Hysteresis_40X40_loc0.125_glob0.01.png

35.1 KiB

plots/Hysteresis_500X500_loc0.001_glob0.001.png

34.7 KiB

plots/Hysteresis_50X50_loc0.001_glob0.001.png

35 KiB

numpy
matplotlib
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment