The challenge is the following: Doomsday Fuel
Making fuel for the LAMBCHOP's reactor core is a tricky process because of the exotic matter involved. It starts as raw ore, then during processing, begins randomly changing between forms, eventually reaching a stable form. There may be multiple stable forms that a sample could ultimately reach, not all of which are useful as fuel.
Commander Lambda has tasked you to help the scientists increase fuel creation efficiency by predicting the end state of a given ore sample. You have carefully studied the different structures that the ore can take and which transitions it undergoes. It appears that, while random, the probability of each structure transforming is fixed. That is, each time the ore is in 1 state, it has the same probabilities of entering the next state (which might be the same state). You have recorded the observed transitions in a matrix. The others in the lab have hypothesized more exotic forms that the ore can become, but you haven't seen all of them.
Write a function solution(m) that takes an array of array of nonnegative ints representing how many times that state has gone to the next state and return an array of ints for each terminal state giving the exact probabilities of each terminal state, represented as the numerator for each state, then the denominator for all of them at the end and in simplest form. The matrix is at most 10 by 10. It is guaranteed that no matter which state the ore is in, there is a path from that state to a terminal state. That is, the processing will always eventually end in a stable state. The ore starts in state 0. The denominator will fit within a signed 32-bit integer during the calculation, as long as the fraction is simplified regularly.
For example, consider the matrix m:
[
[0,1,0,0,0,1], # s0, the initial state, goes to s1 and s5 with equal probability
[4,0,0,3,2,0], # s1 can become s0, s3, or s4, but with different probabilities
[0,0,0,0,0,0], # s2 is terminal, and unreachable (never observed in practice)
[0,0,0,0,0,0], # s3 is terminal
[0,0,0,0,0,0], # s4 is terminal
[0,0,0,0,0,0], # s5 is terminal
]
So, we can consider different paths to terminal states, such as:
s0 -> s1 -> s3
s0 -> s1 -> s0 -> s1 -> s0 -> s1 -> s4
s0 -> s1 -> s0 -> s5
Tracing the probabilities of each, we find that
s2 has probability 0
s3 has probability 3/14
s4 has probability 1/7
s5 has probability 9/14
So, putting that together, and making a common denominator, gives an answer in the form of
[s2.numerator, s3.numerator, s4.numerator, s5.numerator, denominator] which is
[0, 3, 2, 9, 14].
I am passing 7 of the 10 test cases. I've tried swapping the absorbing rows to see if that 'ruins' the answer but that's not the case. My code is the following:
import numpy as np
from fractions import Fraction
def trans(m):
trans_index = 0
for i in range(len(m)):
if sum(m[i]) == 0:
trans_index = i
break
if trans_index == 0:
raise Exception("No Trans Row")
else:
return trans_index
def decompose(m):
Trans = trans(m)
if Trans==0:
raise Exception("No transient states - Not an Absorbing Markov Chain")
Q = []
for i in range(Trans):
row = []
for j in range(Trans):
row.append(m[i][j])
Q.append(row)
R = []
for i in range(Trans):
row = []
for j in range (Trans, len(m[i])):
row.append(m[i][j])
R.append(row)
return Q, R
def identity(t):
I = np.zeros(shape=(t,t))
for i in range(t):
for j in range(t):
if i == j:
I[i][j] = 1
return I
def swap_rows(m, r1, r2):
if r1==r2:
return m
temp = m[r1].copy()
m[r1] = m[r2]
m[r2] = temp
return m
def sort(m):
size = len(m)
zero_row =-1
n = m
for i in range(size):
Sum = sum(m[i])
if Sum == 0.0:
zero_row = i
continue
if Sum != 0 and zero_row > -1:
n = swap_rows(m,i, zero_row)
return sort(n)
return n
def normalize(m):
for i in range(len(m)):
Sum = sum(m[i])
if Sum == 0:
continue
else:
m[i] = m[i]/Sum
return m
def probability(Q,R,I):
prob = np.subtract(I, Q)
prob = np.linalg.inv(prob)
prob = np.matmul(prob,R)
return prob
def to_fraction(m, denom = 10000):
fract = []
for i in range(len(m[0])):
fract.append(Fraction(m[0][i]).limit_denominator(denom))
return fract
def find_lcm(p):
lcm = p[0].denominator
for i in range(len(p)-1):
if np.lcm(p[i].denominator,p[i+1].denominator) > lcm:
lcm = np.lcm(p[i].denominator,p[i+1].denominator)
return lcm
def solution(m):
mk_chain = np.array(m)
mk_chain = mk_chain.astype(np.float32)
mk_chain = normalize(mk_chain)
mk_chain = sort(mk_chain)
Q, R = decompose(mk_chain)
I = identity(trans(mk_chain))
non_trans = len(mk_chain)-trans(mk_chain)
O = np.zeros(shape = (non_trans, non_trans))
prob = probability(Q,R,I)
prob = to_fraction(prob)
lcm = find_lcm(prob)
final = []
for i in range(len(prob)):
if prob[i].denominator != lcm:
numerator = prob[i].numerator*(lcm/prob[i].denominator)
denominator = prob[i].denominator*(lcm/prob[i].denominator)
final.append(numerator)
else:
final.append(prob[i].numerator)
final.append(lcm)
return final
I am not at all sure why I am not passing the (hidden) test cases. Are there exception I should raise? What are some possible cases that could fail and how do I fix it?