Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The connection check for the ensemble model #698

Merged
merged 4 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/Models/EnsembleModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def initialize(self,runInfo,inputs,initDict=None):
# e -> d -> f
if not self.ensembleModelGraph.isConnectedNet():
isolatedModels = self.ensembleModelGraph.findIsolatedVertices()
#self.raiseAnError(IOError, "Some models are not connected. Possible candidates are: "+' '.join(isolatedModels))
self.raiseAnError(IOError, "Some models are not connected. Possible candidates are: "+' '.join(isolatedModels))
# get all paths
allPath = self.ensembleModelGraph.findAllUniquePaths(self.initialStartModels)
###################################################
Expand Down
4 changes: 2 additions & 2 deletions framework/OutStreams/OutStreamPrint.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def localGetInitParams(self):
for index in range(len(self.sourceName)):
paramDict['Source Name ' + str(index) + ' :'] = self.sourceName[index]
if self.what:
for index in range(len(self.what)):
paramDict['Variable Name ' + str(index) + ' :'] = self.what[index]
for index, var in enumerate(self.what.split(',')):
paramDict['Variable Name ' + str(index) + ' :'] = var
return paramDict

def initialize(self, inDict):
Expand Down
44 changes: 25 additions & 19 deletions framework/utils/graphStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#External Modules------------------------------------------------------------------------------------
import sys
import itertools
import copy
#External Modules End--------------------------------------------------------------------------------
#Internal Modules------------------------------------------------------------------------------------
from utils import utils
Expand Down Expand Up @@ -127,7 +128,7 @@ def findIsolatedVertices(self):
@ In, None
@ Out, isolated, list, list of isolated nodes (verteces)
"""
graph = self.__graphDict
graph = self.__extendDictForGraphTheory()
isolated = []
for vertex in graph:
if not graph[vertex]:
Expand Down Expand Up @@ -202,25 +203,25 @@ def findAllPaths(self, startVertex, endVertex, path=[]):
paths.append(p)
return paths

def isConnected(self, verticesEncountered = None, startVertex=None):
def isConnected(self, graphDict, verticesEncountered = None, startVertex=None):
"""
Method that determines if the graph is connected (graph theory connectivity)
@ In, verticesEncountered, set, the encountered vertices (generally it is None when called from outside)
@ In, graphDict, dict, the graph dictionary
@ In, startVertex, string, the starting vertex
@ Out, isConnected, bool, is the graph fully connected?
"""
if verticesEncountered is None:
verticesEncountered = set()
gdict = self.__graphDict
vertices = list(gdict.keys())
vertices = list(graphDict.keys())
if not startVertex:
# chosse a vertex from graph as a starting point
startVertex = vertices[0]
verticesEncountered.add(startVertex)
if len(verticesEncountered) != len(vertices):
for vertex in gdict[startVertex]:
for vertex in graphDict[startVertex]:
if vertex not in verticesEncountered:
if self.isConnected(verticesEncountered, vertex):
if self.isConnected(graphDict, verticesEncountered, vertex):
return True
else:
return True
Expand All @@ -233,21 +234,26 @@ def isConnectedNet(self, startVertex=None):
@ In, startVertex, string, the starting vertex
@ Out, graphNetConnected, bool, is the graph net fully connected?
"""
graphNetConnected = self.isConnected()
if graphNetConnected:
return graphNetConnected
# the graph is not connected (Graph theory)
uniquePaths = self.findAllUniquePaths()
# now check if there is at list a node that is common in all the paths
if len(uniquePaths) > 0:
graphNetConnected = True
startPath = set(uniquePaths.pop())
for otherPath in uniquePaths:
if startPath.isdisjoint(otherPath):
graphNetConnected = False
break
graphDict = self.__extendDictForGraphTheory()
graphNetConnected = self.isConnected(graphDict)
return graphNetConnected

def __extendDictForGraphTheory(self):
"""
Method to extend the graph dictionary in order to be accepted by the graph theory
@ In, None
@ Out, graphDict, dict, the extended graph dictionary, used for isConnectedNet and findIsolatedVertices
"""
graphDict = copy.deepcopy(self.__graphDict)
# Extend graph dictionary generated by ensemble model to graph theory acceptable dictionary
# Basicly, if a -> b (a is connected to b), the self.__graphDict = {'a':['b'], 'b':[]}
# Since a is connected to b, from the graph theory, the dictionary should become {'a':['b'], 'b':['a']}
for inModel, outModelList in self.__graphDict.items():
for outModel in outModelList:
if inModel not in graphDict[outModel]:
graphDict[outModel].append(inModel)
return graphDict

def findAllUniquePaths(self, startVertices=None):
"""
This method finds all the unique paths in the graph
Expand Down
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/test1/ET_SG.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def run(self,Input):
self.outcome_SG = 1 # SD

self.p_SG_ET = self.p_SG
self.t_SG_ET = self.t_SG
self.t_SG_ET = self.t_SG
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/test1/ET_V2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def run(self,Input):
self.outcome_V2 = 1 # SD

self.p_V2_ET = self.p_V2
self.t_V2_ET = self.t_V2
self.t_V2_ET = self.t_V2
4 changes: 2 additions & 2 deletions plugins/PRAplugin/tests/CRAFT/test1/RUL_P1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def RULmodel(a,b):
# a = 2.31
# b = 0.267
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html

rvs = beta.rvs(a, b)
time = 7300 * rvs
return time


def run(self,Input):
# lambda(t) = a + t*b
Expand Down
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/test1/RUL_SG.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def RULmodel(mu,sigma,time):
# b = 0.267
prob = norm.cdf(time, mu, sigma)
return prob


def run(self,Input):
# intput: alpha, beta
Expand All @@ -19,6 +19,6 @@ def run(self,Input):
self.t_SG = random.random()*Input['T']
self.p_SG = float(RULmodel(Input['alpha_SG'], Input['beta_SG'], self.t_SG))
else:
self.t_SG = Input['T']+ 1.0
self.t_SG = Input['T']+ 1.0
self.p_SG = 1.0 - float(RULmodel(Input['alpha_SG'], Input['beta_SG'], self.t_SG))

8 changes: 4 additions & 4 deletions plugins/PRAplugin/tests/CRAFT/test1/cost_P1P2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_P1P2 == 0:
self.cost_P1P2 = 0.
Expand All @@ -16,11 +16,11 @@ def run(self,Input):
costSD = numberDaysSD * costPerDaySD

costPerDayReg = 0.2 + 0.1 * random.random()
costReg = numberDaysSD * costPerDayReg
costReg = numberDaysSD * costPerDayReg

self.cost_P1P2 = costSD + costReg
else:
print('error costP1P2')
print('error costP1P2')

self.p_P1P2_cost = self.p_P1P2_ET
self.t_P1P2_cost = self.t_P1P2_ET
4 changes: 2 additions & 2 deletions plugins/PRAplugin/tests/CRAFT/test1/cost_SG.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_SG == 0:
self.cost_SG = 0.
Expand Down
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/test1/cost_V1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_V1 == 0:
self.cost_V1 = 0.
Expand All @@ -12,4 +12,4 @@ def run(self,Input):
self.cost_V1 = numberDaysSD * costPerDay

self.p_V1_cost = self.p_V1_ET
self.t_V1_cost = self.t_V1_ET
self.t_V1_cost = self.t_V1_ET
8 changes: 4 additions & 4 deletions plugins/PRAplugin/tests/CRAFT/test1/cost_V2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_V2 == 0:
self.cost_V2 = 0.
else:
numberDaysSD = float(random.randint(10,30))
costPerDay = 0.8 + 0.4 * random.random()
self.cost_V2 = numberDaysSD * costPerDay
self.cost_V2 = numberDaysSD * costPerDay

self.p_V2_cost = self.p_V2_ET
self.t_V2_cost = self.t_V2_ET
self.t_V2_cost = self.t_V2_ET
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/test1/lambdaV1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def timeDepLambda(t,a,b):

def pdfFailure(t,a,b):
first = timeDepLambda(t,a,b)
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
return first*second

def run(self,Input):
Expand Down
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/test1/lambdaV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def timeDepLambda(t,a,b):

def pdfFailure(t,a,b):
first = timeDepLambda(t,a,b)
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
return first*second

def run(self,Input):
Expand Down
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/test1/testIntegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def timeDepLambda(t,a,b):

def pdfFailure(t,a,b):
first = timeDepLambda(t,a,b)
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
second = math.exp(-quad(timeDepLambda, 0, t, args=(a,b))[0])
return first*second

t = 3700.
Expand All @@ -20,5 +20,5 @@ def pdfFailure(t,a,b):
print(probability1, time.clock() - start_time)

start_time = time.clock()
probability2 = 1. - math.exp(-quad(timeDepLambda, 0, t, args=(a_V2,b_V2))[0])
print(probability2, time.clock() - start_time)
probability2 = 1. - math.exp(-quad(timeDepLambda, 0, t, args=(a_V2,b_V2))[0])
print(probability2, time.clock() - start_time)
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/test1/unav_P2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def run(self,Input):
self.p_P2 = float(unavailability(rho,compLambda,Input['delta_P2'],Input['T']))
else:
self.t_P2 = float(Input['T'] + 1.0)
self.p_P2 = 1.0 - float(unavailability(rho,compLambda,Input['delta_P2'],Input['T']))
self.p_P2 = 1.0 - float(unavailability(rho,compLambda,Input['delta_P2'],Input['T']))
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/testMC/ET_SG.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def run(self,Input):
self.outcome_SG = 1 # SD

self.p_SG_ET = self.p_SG
self.t_SG_ET = self.t_SG
self.t_SG_ET = self.t_SG
2 changes: 1 addition & 1 deletion plugins/PRAplugin/tests/CRAFT/testMC/ET_V2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def run(self,Input):
self.outcome_V2 = 1 # SD

self.p_V2_ET = self.p_V2
self.t_V2_ET = self.t_V2
self.t_V2_ET = self.t_V2
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/testMC/PRAmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@


def run(self,Input):
# intput:
# output:
# intput:
# output:

IEfreq = 1.E-3
numberDiscretizations = 100

self.time = np.linspace(0,Input['T'],numberDiscretizations)
self.time = np.linspace(0,Input['T'],numberDiscretizations)
4 changes: 2 additions & 2 deletions plugins/PRAplugin/tests/CRAFT/testMC/RUL_P1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def RULmodel(a,b):
# a = 2.31
# b = 0.267
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html

rvs = beta.rvs(a, b)
time = 7300 * rvs
return time


def run(self,Input):
# lambda(t) = a + t*b
Expand Down
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/testMC/RUL_SG.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def RULmodel(mu,sigma,time):
# b = 0.267
prob = norm.cdf(time, mu, sigma)
return prob


def run(self,Input):
# intput: alpha, beta
Expand All @@ -19,6 +19,6 @@ def run(self,Input):
self.t_SG = random.random()*Input['T']
self.p_SG = float(RULmodel(Input['alpha_SG'], Input['beta_SG'], self.t_SG))
else:
self.t_SG = Input['T']+ 1.0
self.t_SG = Input['T']+ 1.0
self.p_SG = 1.0 - float(RULmodel(Input['alpha_SG'], Input['beta_SG'], self.t_SG))

8 changes: 4 additions & 4 deletions plugins/PRAplugin/tests/CRAFT/testMC/cost_P1P2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_P1P2 == 0:
self.cost_P1P2 = 0.
Expand All @@ -16,11 +16,11 @@ def run(self,Input):
costSD = numberDaysSD * costPerDaySD

costPerDayReg = 0.2 + 0.1 * random.random()
costReg = numberDaysSD * costPerDayReg
costReg = numberDaysSD * costPerDayReg

self.cost_P1P2 = costSD + costReg
else:
print('error costP1P2')
print('error costP1P2')

self.p_P1P2_cost = self.p_P1P2_ET
self.t_P1P2_cost = self.t_P1P2_ET
4 changes: 2 additions & 2 deletions plugins/PRAplugin/tests/CRAFT/testMC/cost_SG.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_SG == 0:
self.cost_SG = 0.
Expand Down
6 changes: 3 additions & 3 deletions plugins/PRAplugin/tests/CRAFT/testMC/cost_V1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random

def run(self,Input):
# intput:
# output:
# intput:
# output:

if self.outcome_V1 == 0:
self.cost_V1 = 0.
Expand All @@ -12,4 +12,4 @@ def run(self,Input):
self.cost_V1 = numberDaysSD * costPerDay

self.p_V1_cost = self.p_V1_ET
self.t_V1_cost = self.t_V1_ET
self.t_V1_cost = self.t_V1_ET
Loading