|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Wed Jul 31 17:15:19 2024 |
| 4 | +
|
| 5 | +@author: ektop |
| 6 | +""" |
| 7 | + |
| 8 | +import networkx as nx |
| 9 | +import numpy as np |
| 10 | +from random import uniform |
| 11 | +from math import pi |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +from mpl_toolkits.mplot3d import Axes3D |
| 14 | + |
| 15 | +alpha = 1 # coupling strength |
| 16 | +Dt = 0.01 # Delta t |
| 17 | +m = 1 # Assume mass as 1 for simplicity |
| 18 | + |
| 19 | +def initialize(): |
| 20 | + global g, nextg |
| 21 | + g = nx.karate_club_graph() |
| 22 | + for i in list(g.nodes()): |
| 23 | + g.node[i]['theta'] = 2 * pi * np.random.random() |
| 24 | + g.node[i]['omega'] = 1. + uniform(-0.05, 0.05) |
| 25 | + nextg = g.copy() |
| 26 | + |
| 27 | +def observe(): |
| 28 | + global g |
| 29 | + plt.clf() |
| 30 | + nx.draw(g, cmap=plt.cm.hsv, vmin=-1, vmax=1, |
| 31 | + node_color=[np.sin(g.node[i]['theta']) for i in list(g.nodes())], |
| 32 | + pos=nx.spring_layout(g)) |
| 33 | + plt.title('Network Visualization') |
| 34 | + plt.show() |
| 35 | + |
| 36 | +def gauss_mouse_map(phase): |
| 37 | + return np.sin(phase) |
| 38 | + |
| 39 | +def update(): |
| 40 | + global g, nextg, chaotic_numbers_data, timestamps, frequency_shifts, action_derivative_values |
| 41 | + chaotic_numbers = [] |
| 42 | + angular_accelerations = np.zeros(len(g.nodes())) |
| 43 | + action_derivative = 0 # Initialize action derivative for this timestep |
| 44 | + previous_angular_velocities = np.array([g.node[i]['omega'] for i in g.nodes()]) |
| 45 | + |
| 46 | + for i in list(g.nodes()): |
| 47 | + theta_i = g.node[i]['theta'] |
| 48 | + omega_i = g.node[i]['omega'] |
| 49 | + |
| 50 | + # Update angular position using Euler's method |
| 51 | + nextg.node[i]['theta'] = theta_i + omega_i * Dt + (alpha * ( |
| 52 | + np.sum(np.sin(g.node[j]['theta'] - theta_i) for j in g.neighbors(i)) |
| 53 | + / g.degree(i))) * Dt |
| 54 | + |
| 55 | + angular_accelerations[i] = (nextg.node[i]['theta'] - theta_i) / Dt |
| 56 | + |
| 57 | + chaotic_number = gauss_mouse_map(g.node[i]['theta']) |
| 58 | + chaotic_numbers.append(chaotic_number) |
| 59 | + |
| 60 | + # Compute derivative of action |
| 61 | + for i in range(len(g.nodes())): |
| 62 | + action_derivative += 0.5 * m * previous_angular_velocities[i] * angular_accelerations[i] |
| 63 | + |
| 64 | + action_derivative_values.append(action_derivative) # Store action derivative over time |
| 65 | + # Calculate frequency shifts |
| 66 | + if len(chaotic_numbers_data) > 0: |
| 67 | + previous_chaotic_numbers = chaotic_numbers_data[-1] |
| 68 | + frequency_shift = [chaotic_numbers[j] - previous_chaotic_numbers[j] for j in range(len(chaotic_numbers))] |
| 69 | + frequency_shifts.append(np.mean(frequency_shift)) # Store the average frequency shift over time |
| 70 | + else: |
| 71 | + frequency_shifts.append(0) # No shift initially |
| 72 | + |
| 73 | + |
| 74 | + # Update the states |
| 75 | + g, nextg = nextg, g |
| 76 | + chaotic_numbers_data.append(chaotic_numbers) |
| 77 | + timestamps.append(len(chaotic_numbers_data)) |
| 78 | + |
| 79 | +def initialize_and_update(): |
| 80 | + initialize() |
| 81 | + update() |
| 82 | + |
| 83 | +import pycxsimulator |
| 84 | + |
| 85 | +# Initialize lists to store data |
| 86 | +chaotic_numbers_data = [] |
| 87 | +frequency_shifts = [] |
| 88 | +action_derivative_values = [] # List to store action derivatives over time |
| 89 | +timestamps = [] # Initialize timestamps |
| 90 | + |
| 91 | +# Run the simulation |
| 92 | +pycxsimulator.GUI().start(func=[initialize, observe, update]) |
| 93 | + |
| 94 | + |
| 95 | +# Create scatter plot of chaotic number values vs timestamps |
| 96 | +plt.figure(figsize=(12, 5)) |
| 97 | +for i, chaotic_numbers in enumerate(chaotic_numbers_data): |
| 98 | + colors = ['r' if num >= 0 else 'b' for num in chaotic_numbers] |
| 99 | + plt.scatter([timestamps[i]] * len(chaotic_numbers), chaotic_numbers, color=colors, alpha=0.5) |
| 100 | + |
| 101 | +plt.xlabel('Timestamp') |
| 102 | +plt.ylabel('Chaotic Number Value') |
| 103 | +plt.title('Scatter Plot of Chaotic Number Values vs Timestamp') |
| 104 | +plt.show() |
| 105 | + |
| 106 | + |
| 107 | +# Create scatter plot of frequency shifts |
| 108 | +plt.figure(figsize=(12, 5)) |
| 109 | +for i, shift in enumerate(frequency_shifts): |
| 110 | + plt.scatter(timestamps[i], shift, color='g', alpha=0.5) |
| 111 | + |
| 112 | +plt.xlabel('Timestamp') |
| 113 | +plt.ylabel('Average Frequency Shift') |
| 114 | +plt.title('Scatter Plot of Frequency Shifts vs Timestamp') |
| 115 | +plt.show() |
| 116 | + |
| 117 | + |
| 118 | +# New: Plot action derivatives over time |
| 119 | +plt.figure(figsize=(12, 5)) |
| 120 | +plt.plot(timestamps, action_derivative_values, marker='o', linestyle='-') |
| 121 | +plt.title('Action Derivative over Time') |
| 122 | +plt.xlabel('Timestamp') |
| 123 | +plt.ylabel('Action Derivative') |
| 124 | +plt.grid() |
| 125 | +plt.show() |
| 126 | + |
| 127 | +# New: Create scatter plot of action derivative vs chaotic numbers |
| 128 | +plt.figure(figsize=(12, 5)) |
| 129 | +for i, chaotic_numbers in enumerate(chaotic_numbers_data): |
| 130 | + for j in range(len(chaotic_numbers)): |
| 131 | + plt.scatter(action_derivative_values[i], chaotic_numbers[j], color='red', alpha=0.5) |
| 132 | + |
| 133 | +plt.title('Chaotic Numbers vs Action Derivative') |
| 134 | +plt.xlabel('Action Derivative') |
| 135 | +plt.ylabel('Chaotic Number Value') |
| 136 | +plt.grid() |
| 137 | +plt.show() |
| 138 | + |
| 139 | + |
| 140 | +# New: Plot frequency shifts vs actions |
| 141 | +plt.figure(figsize=(12, 5)) |
| 142 | +plt.scatter(action_derivative_values, frequency_shifts, color='orange', alpha=0.5) |
| 143 | +plt.title('Frequency Shifts vs Action Derivative') |
| 144 | +plt.xlabel('Action Derivative') |
| 145 | +plt.ylabel('Average Frequency Shift') |
| 146 | +plt.grid() |
| 147 | +plt.show() |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +# Assuming you have the following variables defined |
| 154 | +# timestamps, action_derivative_values, frequency_shifts |
| 155 | + |
| 156 | +# Create a figure |
| 157 | +fig = plt.figure(figsize=(12, 8)) |
| 158 | + |
| 159 | +# Create a 3D scatter plot |
| 160 | +ax = fig.add_subplot(111, projection='3d') |
| 161 | + |
| 162 | +# Create a scatter plot, using timestamps, action derivatives, and chaotic numbers |
| 163 | +scatter = ax.scatter(timestamps, action_derivative_values, frequency_shifts, |
| 164 | + c=action_derivative_values, cmap='viridis', alpha=0.5) |
| 165 | + |
| 166 | +# Add titles and labels |
| 167 | +ax.set_title('3D Visualization of Action Derivative, Frequency Shift, va Timestamps') |
| 168 | +ax.set_xlabel('Timestamp') |
| 169 | +ax.set_ylabel('Action Derivative') |
| 170 | +ax.set_zlabel('Frequency Shift Value') |
| 171 | + |
| 172 | +# Show color bar for reference |
| 173 | +plt.colorbar(scatter, label='Action Derivative') |
| 174 | + |
| 175 | +# Show the plot |
| 176 | +plt.show() |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
0 commit comments