def dayrun(start, cluster): ''' Simulation of a single day according to start state 'start' and the stochastics associated with 'cluster'. ''' # script ########################################################## # we set the default daycheck at False for the first run and loop # creating days while False, meaning while the simulated day does # not correspond to the agreed-on rules in check(). # ----->>> The check is currently not effectively implemented. daycheck = False end = datetime.datetime.utcnow() + datetime.timedelta(seconds=10) # define the corresponding MCSA object from stats.py depicting a # Monte Carlo Survival Analysis object. SA = stats.MCSA(cluster) # and then keep simulating a day until True while daycheck == False: # set start state conditions tbin = 0 occs = np.zeros(144, dtype=int) occs[0] = start # occupancy data from survey are given per 30 min, so we need to know in which of 48 bins to look for data: t48 = np.array(sorted(list(range(1, 49)) * 3)) dt = SA.duration( start, t48[0] ) # get duration of current state at start time (4am) # and loop sequentially transition and duration functions while tbin < 143: tbin += 1 if dt == 0: # previous state duration has ended occs[tbin] = SA.transition( occs[tbin - 1], t48[tbin]) # find most probable next state dt = SA.duration( occs[tbin], t48[tbin] ) - 1 # restart duration counter for new state # -1 is necessary, as the occupancy state already started else: occs[tbin] = occs[tbin - 1] # maintain current state dt += -1 # count down duration of current state # whereafer we control if this day is ok daycheck = check( occs ) # ----->>> The check is currently not effectively implemented, always TRUE! # and we include a break if the while-loop takes to long until # check()-conditions are fulfilled. if datetime.datetime.utcnow() > end: break # ouput ########################################################### # return occupants array if daycheck is ok return occs
def dayrun(start, cluster): ''' Simulation of a single day according to start state 'start' and the stochastics stored in cluster 'BxDict'and daytype 'Bx'. ''' # script ########################################################## # we set the default daycheck at False for the first run and loop # creating days while False, meaning while the simulated day does # not correspond to the agreed-on rules in check(). daycheck = False end = datetime.datetime.utcnow() + datetime.timedelta(seconds=10) # defin the corresponding MCSA object from stats.py depicting a # Monte Carlo Survival Analysis object. SA = stats.MCSA(cluster) # and then keep simulating a day until True while daycheck == False: # set start state conditions tbin = 0 occs = np.zeros(144, dtype=int) occs[0] = start t48 = np.array(sorted(list(range(1, 49)) * 3)) dt = SA.duration(start, t48[0]) # and loop sequentially transition and duration functions while tbin < 143: tbin += 1 if dt == 0: occs[tbin] = SA.transition(occs[tbin - 1], t48[tbin]) dt = SA.duration(occs[tbin], t48[tbin]) - 1 # -1 is necessary, as the occupancy state already started else: occs[tbin] = occs[tbin - 1] dt += -1 # whereafer we control if this day is ok daycheck = check(occs) # and we include a break if the while-loop takes to long until # check()-conditions are fulfilled. if datetime.datetime.utcnow() > end: break # ouput ########################################################### # return occupants array if daycheck is ok according to Bx return occs
def __occupancy__(self, min_form=True, min_time=False): ''' Simulation of occupancy profiles for each occupant based on their clusters. - A weekday, saturday and sunday are simulated, after which a typical week is created and repeated for the entire simulation period. - The starting day of the year is taken into account. - The occupancy profiles start at 4:00 AM, in accordance with the used survey data. (later shifted, see roundUp()) ''' def check(occday, min_form=True, min_time=False): # -->> this check is not effective !!! ''' We set a check which becomes True if the simulated day behaves according to the cluster, as a safety measure for impossible solutions. ---->>> This check is not effectively implemented for the moment !!! ''' # script 1 ######################################################## # First we check if the simulated occ-chain has the same shape shape = True # --->>> Check not effective, shape=True always !!!!!!!!!!! if min_form: location = np.zeros(1, dtype=int) reduction = occday[0] * np.ones(1, dtype=int) for i in range(len(occday) - 1): if occday[i + 1] != occday[i]: location = np.append(location, i + 1) reduction = np.append(reduction, occday[i + 1]) # shape = np.array_equal(reduction, RED) --->>> RED not available any more!! # script 2 ######################################################## # And second we see if the chain has no sub-30 min differences length = True # --->>> Check not effective, length=True always !!!!!!!!!!! if min_time: # ------>>> min_time=False by default !! minlength = 99 for i in location: j = 0 while occday[i + j] == occday[i] and i + j < len(occday) - 1: j = j + 1 if j < minlength: minlength = j # and we neglect the very short presences of 20 min or less length = not minlength < 3 # output ########################################################## # both have to be true to allow continuation, and we return boolean return shape and length # --->>> Check not effective, always True !!!!!!!!!!! def dayrun(start, cluster): ''' Simulation of a single day according to start state 'start' and the stochastics associated with 'cluster'. ''' # script ########################################################## # we set the default daycheck at False for the first run and loop # creating days while False, meaning while the simulated day does # not correspond to the agreed-on rules in check(). # ----->>> The check is currently not effectively implemented. daycheck = False end = datetime.datetime.utcnow() + datetime.timedelta(seconds=10) # define the corresponding MCSA object from stats.py depicting a # Monte Carlo Survival Analysis object. SA = stats.MCSA(cluster) # and then keep simulating a day until True while daycheck == False: # set start state conditions tbin = 0 occs = np.zeros(144, dtype=int) occs[0] = start # occupancy data from survey are given per 30 min, so we need to know in which of 48 bins to look for data: t48 = np.array(sorted(list(range(1, 49)) * 3)) dt = SA.duration( start, t48[0] ) # get duration of current state at start time (4am) # and loop sequentially transition and duration functions while tbin < 143: tbin += 1 if dt == 0: # previous state duration has ended occs[tbin] = SA.transition( occs[tbin - 1], t48[tbin]) # find most probable next state dt = SA.duration( occs[tbin], t48[tbin] ) - 1 # restart duration counter for new state # -1 is necessary, as the occupancy state already started else: occs[tbin] = occs[tbin - 1] # maintain current state dt += -1 # count down duration of current state # whereafer we control if this day is ok daycheck = check( occs ) # ----->>> The check is currently not effectively implemented, always TRUE! # and we include a break if the while-loop takes to long until # check()-conditions are fulfilled. if datetime.datetime.utcnow() > end: break # ouput ########################################################### # return occupants array if daycheck is ok return occs def merge(occ): ''' Merge the occupancy profiles of all household members to a single profile denoting the most active state of all members. ''' # scirpt ########################################################## # We start defining an array of correct length filled with the # least active state and loop to see if more-active people are # present at the depicted moment. occs = int(3) * np.ones(len(occ[0])) for member in occ: for to in range(len(member)): if member[to] < occs[to]: occs[to] = member[to] # ouput ########################################################### # return the merge occupancy states return occs # script ############################################################## # We change the directory to to location where the data is stored, # and create a typical week. cdir = os.getcwd() occ_week = [] #typical week for member in self.clustersList: week = [] # initiate empty week SA = stats.MCSA(member['wkdy']) startstate = SA.startstate( ) # random starting state of week, depending on cluster for i in range(5): week = np.append(week, dayrun(startstate, member['wkdy'])) startstate = week[-1] week = np.append(week, dayrun(week[-1], member['sat'])) week = np.append(week, dayrun(week[-1], member['son'])) occ_week.append(week) # A merge occupancy is created depicted the most active state of all # household members, later-on used for set-point temperatures and hot water tappings. occ_merg = merge(occ_week) # and combine the weekly occupancy states for the entire year by # repeating them every week and correcting for the first day of year and stop time, # including for the merged occupancy. bins = 144 # number of datapoints in one day tstart = bins * (self.dow[0]) # need to sart on correct day of week tstop = tstart + bins * ( self.nday ) + 1 # need nday in total, plus one step (for IDEAS simulations) occ_year = [] for line in range(len(occ_week)): # per separate member #repeat week more than enough times (54), take days needed to have correct nday and start day occ_year.append(np.tile(occ_week, 54)[line][tstart:tstop]) occ_merged = [] occ_merged.append(np.tile(occ_merg, 54)[tstart:tstop]) # output ############################################################## # chdir back to original and return the occupancy states to the class # object. os.chdir(cdir) self.occ = occ_year self.occ_m = occ_merged # and print statements presence = [to for to in self.occ_m[0] if to < 2] hours = len(presence) / 6. print(' - Total presence time is {0:.1f} out of {1} hours'.format( hours, self.nday * 24)) print('\tbeing {:.1f} percent)'.format(hours * 100 / (self.nday * 24))) return None