Skip to content

Instantly share code, notes, and snippets.

@dat-adi
Created December 16, 2022 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dat-adi/30a78cfbc8c3527210a1ba9e437b7304 to your computer and use it in GitHub Desktop.
Save dat-adi/30a78cfbc8c3527210a1ba9e437b7304 to your computer and use it in GitHub Desktop.
A Markov Chain Implementation to create a controller priority list
#!/usr/bin/python
"""
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)
print(stats)
res_controllers = [state[stats.index(x)] for x in sorted(stats)][::-1]
return res_controllers
if __name__ == "__main__":
controller_priority()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment