Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2022 10:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A Markov Chain Implementation to create a controller priority list
This is a program that implements the Markov Chain algorithm
to calculate the next best possible controller to contact for
executing the source and destination flow modification.
import scipy.linalg
import numpy as np
# Random Walk
def random_walk(state, A):
n = 10
start_state = 0
prev_state = start_state
while n>1:
curr_state = np.random.choice([0, 1, 2], p=A[prev_state])
prev_state = curr_state
n -= 1
values, left = scipy.linalg.eig(A, right=False, left=True)
# Normalized values of the eigen vectors
pi = left[:,0]
pi_normalized = [(x/np.sum(pi)).real for x in pi]
return pi_normalized
def controller_priority():
state = {
0: "ODL",
1: "Ryu",
2: "FL"
A = np.array([[0.2, 0.15, 0.65], [0.7, 0.2, 0.1], [0.9, 0.05, 0.05]])
stats = random_walk(state, A)
res_controllers = [state[stats.index(x)] for x in sorted(stats)][::-1]
return res_controllers
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment