Repository navigation
Expand file tree
/
Copy pathprobability.py
More file actions
94 lines (71 loc) · 2.52 KB
/
Copy pathprobability.py
File metadata and controls
94 lines (71 loc) · 2.52 KB
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
84
85
86
87
88
89
90
91
92
93
94
from algorithm import DC_Checker
from stn import STN, loadSTNfromJSONfile
from relax import relaxSearch
from scipy.stats import norm
from math import sqrt, log, exp
from typing import List
##
# \file probability.py
# \brief Computing some probabilities for degree of dynamic controllability
##
# \fn prob_small_sum(lengths, S)
# \brief
#
# @param lengths An array of the lengths l_i
# @param S A sum the (a_i)s should be less than
#
# @return The probability that a_1 + ... + a_n <= S given
# that a_i ~ U(0, l_i) if not gauss, or a_i ~ N(l_i/2, l_i/4)
# otherwise
def prob_small_sum(lengths: list, S: float, gauss) -> float:
mean = 0.0
variance = 0.0
N = len(lengths) # TODO: what is this used for?
for l in lengths:
mean += l
if gauss:
variance += (l * l)/16
else:
variance += (l * l)/12
mean = mean / 2
variance = variance
z_score = (S - mean) / sqrt(variance)
return norm.cdf(z_score)
##
# \fn special_prob()
# \brief Returns the closed form answer that should only work in a special case.
def special_prob(lengths: list, S: float) -> float:
n = len(lengths)
# Get the logarithm of the relevant expression
numerator = n * log(sum(lengths) - S)
log_lengths = [log(l) for l in lengths]
log_factorial = [log(m) for m in range(1, n + 1)]
denominator = sum(log_lengths) + sum(log_factorial)
log_prob = numerator - denominator
true_prob = exp(log_prob)
return true_prob
##
# \fn prob_of_DC_file()
def prob_of_DC_file(file_name: str, gauss) -> float:
network = loadSTNfromJSONfile(file_name)
return prob_of_DC(network, gauss)
def prob_of_multiple_conflicts(lengths_list: List[list], weights: List[float], gauss):
probability = 1.0
m = len(lengths_list)
assert len(weights) == m, "The input lists have different lengths!"
for j in range(m):
probability *= prob_small_sum(lengths_list[j], weights[j], gauss)
return probability
##
# \fn prob_of_DC()
def prob_of_DC(network: STN, gauss) -> float:
_, num_conflicts, cycles, neg_weights = relaxSearch(network)
lengths_list = [[] for j in range(num_conflicts)]
weights_list = []
for j in range(num_conflicts):
edges = cycles[j]
for edge in edges:
lengths_list[j].append(edge.getWeightMax() - edge.getWeightMin())
S = sum(lengths_list[j]) + neg_weights[j]
weights_list.append(S)
return prob_of_multiple_conflicts(lengths_list, weights_list, gauss)