-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_map.py
83 lines (59 loc) · 2.36 KB
/
logistic_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import matplotlib.pyplot as plt
from math import log
from random import random
# TODO - Color based on probability mass
# TODO - Show temperature/entropy as background color
# TODO - https://en.wikipedia.org/wiki/List_of_chaotic_maps
# entropy coloring stable 100-800 generations_to_plot
COLOR_MAP = {.04: 'violet', .13: 'red', .25: 'pink', .5: 'orange', 1:'blue'}
def COLOR(x):
for key, value in COLOR_MAP.items():
if x <= key:
return value
return 'black'
NORM_FACTOR = .001
def NORMALIZE(x):
if NORM_FACTOR == 0:
return x
return x // NORM_FACTOR * NORM_FACTOR
if __name__ == '__main__':
# Setup
RULE = lambda x: r * x * (1-x)
USE_ENTROPY = False # Measure entropy or temperature by color
MIN = 0 # Min r
MAX = 4 # Max r
GRANULARITY = 500 # Slices per unit r
GENERATIONS = 2000 # Times population is recalculated
GENERATIONS_TO_PLOT = int(.1 * GENERATIONS)
# Calculate
X = [] # r
Y = [] # population
Z = [] # entropy or temperature
for r in (MIN + (i / GRANULARITY) for i in range((MAX-MIN) * GRANULARITY)):
# Initialize
populations = [max(.1, random())] # set to ~.5 value with small generation cound
if GENERATIONS == GENERATIONS_TO_PLOT:
X.append(r)
Y.append(NORMALIZE(populations[-1]))
# Calculate Y
for g in range(len(populations), GENERATIONS):
population = RULE(populations[-1])
populations.append(population)
if g >= GENERATIONS - GENERATIONS_TO_PLOT:
X.append(r)
Y.append(NORMALIZE(population))
# Calculate Z
sample = Y[-GENERATIONS_TO_PLOT:]
probability_mass = {v: 1 / sample.count(v) for v in set(sample)}
temperature = len(set(sample))
entropy = - sum([x * log(x) for x in probability_mass.values()])
color = COLOR(1 / (1 + entropy) if USE_ENTROPY else 1 / temperature)
Z += [color] * GENERATIONS_TO_PLOT
# Plot
plt.scatter(X, Y, c=Z, s=.1)
plt.gcf().canvas.set_window_title('Bifurication Diagram')
plt.title('Logistic Map')
plt.xlabel('Replacement Rate')
plt.ylabel('Population / Carrying Capacity')
plt.legend(['Entropy' if USE_ENTROPY else 'Temperature'])
plt.show()