def main(wScaleBound, setUpCost, crBound, ranSeed ): nComponents = 3 #input: number of components nStages = 6 #input: planning horizon: t = 1, 2, ..., T T = nStages; #remaining time horizon R = T + 1; #number of individuals nScenarios = 910; #input: number of scenarios cS = setUpCost #setup cost intvl = 1; #input: inspection interval sysInfo = class_info.system_info(nComponents, T, intvl, cS, nScenarios, ranSeed); kesi = [0]*nComponents; age = [0]*nComponents; cPR = [0]*nComponents; cCR = [0]*nComponents; w_shape = [0]*nComponents; w_scale = [0]*nComponents; for i in range(nComponents): #input: initial failure state kesi if i==0: kesi[i] = 1; #input: initial age age[i] = 2; #input: preventive replacement cost cPR[i] = 1; #cCR random.seed(i*30); temp = random.uniform(crBound[0], crBound[1]); cCR[i] = round(temp,1); #input: shape parameter random.seed(i*20) temp = random.uniform(4,7) w_shape[i] = round(temp,1); #scale random.seed(i*10) temp = random.uniform(wScaleBound[0], wScaleBound[1])#(4,11) w_scale[i] = round(temp,1); comInfo = class_info.component_info(i, w_shape[i], w_scale[i], age[i], kesi[i], \ intvl, cCR[i], cPR[i], cS, nScenarios,R); # LT is initialized to be empty. sysInfo.add_com(comInfo); sysInfo.comInfoAll[i].create_LifeTime(sysInfo.ranSeed); #print (sysInfo.comInfoAll[i].LT); retCost = 0; ## solve current stage problem PH_alg(sysInfo); return sysInfo.objValue
import class_info import numpy as np import main import time ##################### #system information ##################### sysInfo = class_info.system_info() sysInfo.T = 3 #planning horizon sysInfo.numAction = 3 #size of action space sysInfo.numState = 5 #size of state space sysInfo.theta = 3 #wasserstein radius sysInfo.normp = 2 #distance metric # support normp = 1, 2. General case normp > 1 is implemented, but # not 100% sure is correct. sysInfo.numData = 3 #number of data sysInfo.v.append([-1, -3, -5, -7, -80]) #tmp = np.matrix(sysInfo.vTList); #sysInfo.vTMatrix = tmp.transpose(); #make it a column vector' sysInfo.qList = [] #The mx effects list. For each from_state, it's a #2-dimensional list: action * to_state.
########################## #start from here ########################## # some fixed parameters nComponents = 3 #input: component numbers high = 100 low = 5 cS = high #input: setup cost intvl = 1 #input: inspection interval. nStages = 6 #input: planning horizon, 1,2,...,nStages sysInfo = class_info.system_info(nComponents, nStages, intvl, cS) kesi = [0] * nComponents age = [0] * nComponents cPR = [0] * nComponents cCR = [0] * nComponents w_shape = [0] * nComponents w_scale = [0] * nComponents for i in range(nComponents): #input: initial failure state kesi if i == 0: kesi[i] = 0 #input: initial age age[i] = 0 #input: initial PR cost
#alpha #gam_a = [] #now it a rate parameter, following gamma(kappa, lambda), lambda is a rate as well gam_b = [8.556, 7.654] #shape & rate S = [2] * nComponents #failure threshold, 10mm nRep = 102 c0All = [] c1All = [] t0All = [] t1All = [] errAll = [] for rep in range(nRep - 1, nRep): #control replicates, random seed #init system parameter sysInfo = class_info.system_info(nComponents, nStages, inspInterval, cS, cInsp) initState = [] cCM = [] cPM = [] initAge = [] for i in range(nComponents): baseSeedState = 10000 + nComponents * 100 baseSeedCM = baseSeedState * 10 baseSeedPM = baseSeedState * 100 baseSeedT = baseSeedState * 1000 #for initial state ranSeed = (i + 1) * baseSeedState + rep + nComponents #random seeds random.seed(ranSeed) tmp = random.randint(0, nStates - 1)
def main(wShapeBound, wScaleBound, setUpCost, crBound, ranSeed ): nComponents = 8 #input: number of components nStages = 20 #input: planning horizon: t = 1, 2, ..., T T = nStages; #remaining time horizon R = T + 20; #number of individuals nScenarios = 250; #input: number of scenarios cS = setUpCost #input:setup cost intvl = 1; #input: inspection interval sysInfo = class_info.system_info(nComponents, T, intvl, cS, nScenarios, ranSeed); kesi = [0]*nComponents; age = [0]*nComponents; cPR = [0]*nComponents; cCR = [0]*nComponents; w_shape = [0]*nComponents; w_scale = [0]*nComponents; for i in range(nComponents): #input: initial failure state kesi if i==0: kesi[i] = 0; #input: initial age age[i] = 0; #input: PR cost cPR[i] = 1; #input: CR cost random.seed(i*30); temp = random.uniform(crBound[0], crBound[1]); cCR[i] = round(temp,1); #input: Weilbull shape parameter random.seed(i*20) temp = random.uniform(wShapeBound[0],wShapeBound[1]) w_shape[i] = round(temp,1); #input: Weibull scale parameter random.seed(i*10) temp = random.uniform(wScaleBound[0], wScaleBound[1])#(4,11) w_scale[i] = round(temp,1); comInfo = class_info.component_info(i, w_shape[i], w_scale[i], age[i], kesi[i], \ intvl, cCR[i], cPR[i], cS, nScenarios,R); # LT is initialized to be empty. sysInfo.add_com(comInfo); sysInfo.comInfoAll[i].create_LifeTime(sysInfo.ranSeed); #print (sysInfo.comInfoAll[i].LT); retCost = 0; for t in range(nStages): print ("t=",t); #sTime = time.clock(); if t == nStages - 1: tmp = 0; for i in range(sysInfo.nComponents): if sysInfo.comInfoAll[i].initFail == 1: tmp += sysInfo.comInfoAll[i].cCR print ("fail",i); if tmp > 0: tmp += sysInfo.cS; retCost += tmp; else: ## solve current stage problem #flag = False; #if t == 6: #flag = True; #print("LT[0]", sysInfo.comInfoAll[0].LT); #print("LT[1]", sysInfo.comInfoAll[1].LT); x = PH_alg(sysInfo); #print ("x=",x); res = copy.deepcopy(x); for i in range(sysInfo.nComponents): if sysInfo.comInfoAll[i].initFail == 1: res[i] = 2; print ("res=",res); x = [round(x[i]) for i in range(len(x))]; #calculate cost at current stage tmp = 0; for i in range(sysInfo.nComponents): tmp += sysInfo.comInfoAll[i].cPR*x[i]; tmp += (sysInfo.comInfoAll[i].cCR - sysInfo.comInfoAll[i].cPR)*\ sysInfo.comInfoAll[i].initFail; #if (x[i] == 1): #print (sysInfo.comInfoAll[i].initFail); if tmp > 0: tmp += sysInfo.cS; retCost += tmp; ## prepare next stage: #generate the failure ageAfterMx = [sysInfo.comInfoAll[i].initAge*(1-x[i]) \ for i in range(sysInfo.nComponents)]; failProb = [sysInfo.comInfoAll[i].cond_fail_prob(ageAfterMx[i],ageAfterMx[i]+1) \ for i in range(sysInfo.nComponents)]; randProb = []; for i in range(sysInfo.nComponents): random.seed(ranSeed + t*100 + i); randProb.append(random.uniform(0,1)); failState = [int(randProb[i]<failProb[i]) for i in range(sysInfo.nComponents)]; ageAfterMx = [ageAfterMx[i]+1 for i in range(sysInfo.nComponents)]; #sysInfo.nStages += -1; sysInfo.nStages = max(sysInfo.nStages, 10) #R = sysInfo.nStages + 1; for i in range(sysInfo.nComponents): sysInfo.comInfoAll[i].initAge = ageAfterMx[i]; sysInfo.comInfoAll[i].initFail = failState[i]; #sysInfo.comInfoAll[i].nIndividuals = R; sysInfo.comInfoAll[i].create_LifeTime(sysInfo.ranSeed + t*100); #print("failProb", failProb); #print("randProb", randProb); #print("failState,", failState); #print("LT[0]", sysInfo.comInfoAll[0].LT); #print("LT[1]", sysInfo.comInfoAll[1].LT); #eTime = time.clock(); #print ("time=", eTime-sTime); return retCost