def get_samples(self, validation, sampling_type): print 'Generating samples...' rewards = np.zeros((self.no_days * self.time_int)) exp_rewards = np.zeros((self.no_days * self.time_int)) act_rewards = np.zeros((self.no_days * self.time_int)) cl_ids = np.zeros((self.no_days * self.time_int)) task_executed = np.zeros((self.no_days * self.time_int)) if validation == False: cl_len = len(self.clusters) total_prob = self.prob for i in range(self.no_days - 1): total_prob = np.vtack(total_prob, self.prob) column_indices = [i for i in range(cl_len)] for i in range(self.time_int * self.no_days): ###### Sampling according to distribution if sampling_type == 'prob': index = np.random.choice(column_indices, p=total_prob[i]) ###### Sampling the highest value available elif sampling_type == 'max': index = list(total_prob[i]).index(np.max(total_prob[i])) rewards[i] = self.clusters[ index] ## In this case actual rewards = rewards (matched) cl_ids[i] = index elif validation == True: print 'Days available: ', self.no_days for i in range(len(rewards)): day_i = i / self.time_int interval = i % self.time_int if interval == 0: start_int = datetime.combine(self.test_days[day_i], time(0, 0)) else: start_int = end_int end_int = start_int + timedelta(minutes=self.int_duration) act_rewards[i] = self.test_tasks[ (self.test_tasks['start_time'] < end_int) & (self.test_tasks['end_time'] > start_int )]['priority'].sum() rewards[i], cl_ids[i] = self.closest_cluster(act_rewards[i]) task_executed[i] = np.random.choice(np.array([0, 1]), p=[0.1, 0.9]) exp_rewards[i] = sum(self.prob[interval] * self.clusters) return rewards, cl_ids, act_rewards, task_executed, exp_rewards
def get_samples(self, validation, sampling_type): print 'Generating samples...' rewards = np.zeros((self.no_days * self.time_int)) # exp_rewards = np.zeros((self.no_days*self.time_int)) act_rewards = np.zeros((self.no_days * self.time_int)) cl_ids = np.zeros((self.no_days * self.time_int)) #####expected # rewards = len(self.test_rewards)*[0] # exp_rewards = len(self.test_rewards)*[0] # act_rewards = len(self.test_rewards)*[0] # cl_ids = len(self.test_rewards)*[0] if validation == False: cl_len = len(self.clusters) total_prob = self.prob for i in range(self.no_days - 1): total_prob = np.vtack(total_prob, self.prob) column_indices = [i for i in range(cl_len)] for i in range(self.time_int * self.no_days): ###### Sampling according to distribution if sampling_type == 'prob': index = np.random.choice(column_indices, p=total_prob[i]) ###### Sampling the highest value available elif sampling_type == 'max': index = list(total_prob[i]).index(np.max(total_prob[i])) rewards[i] = self.clusters[ index] ## In this case actual rewards = rewards (matched) cl_ids[i] = index elif validation == True: print 'Days available: ', self.no_days for i in range(len(rewards)): day_i = i / self.time_int interval = i % self.time_int if interval == 0: start_int = datetime.combine(self.test_days[day_i], time(0, 0)) else: start_int = end_int end_int = start_int + timedelta(minutes=self.int_duration) act_rewards[i] = self.test_tasks[ (self.test_tasks['start_time'] < end_int) & (self.test_tasks['end_time'] > start_int )]['priority'].sum() rewards[i], cl_ids[i] = self.closest_cluster(act_rewards[i]) ####exp rew # cl_no = 0 # for i in range(len(self.test_rewards)): # if i == 48 or i==96: # cl_no = 0 # cl_reward, index = self.closest_cluster(self.test_rewards[i], self.clusters[i%self.time_int]) # for k in range(len(self.clusters[i%self.time_int])): # exp_rewards[i] = exp_rewards[i] + self.clusters[i%self.time_int][k]*self.prob[i%self.time_int][k] # act_rewards[i] = self.test_rewards[i] # rewards[i] = cl_reward # cl_ids[i] = cl_no +index # cl_no = cl_no+len(self.clusters[i%self.time_int]) return rewards, cl_ids, act_rewards #, exp_rewards
def validate_interval_data(self, from_to, input_values, collocation_distance=1e-4): """ Compare new and current depth values, append new vertices if necessary and return an augmented values vector that matches the vertices indexing. """ if isinstance(from_to, list): from_to = np.vtack(from_to) assert from_to.shape[0] == len(input_values), ( f"Mismatch between input 'from_to' shape{from_to.shape} " + f"and 'values' shape{input_values.shape}" ) assert from_to.shape[1] == 2, "The `from-to` values must have shape(*, 2)" if (self._from is None) and (self._to is None): uni_depth, inv_map = np.unique(from_to, return_inverse=True) self.cells = self.add_vertices(self.desurvey(uni_depth))[inv_map].reshape( (-1, 2) ) self.workspace.create_entity( Data, entity={ "parent": self, "association": "CELL", "name": "FROM", "values": from_to[:, 0], }, entity_type={"primitive_type": "FLOAT"}, ) self.workspace.create_entity( Data, entity={ "parent": self, "association": "CELL", "name": "TO", "values": from_to[:, 1], }, entity_type={"primitive_type": "FLOAT"}, ) else: from_ind = match_values( self._from.values, from_to[:, 0], collocation_distance=collocation_distance, ) to_ind = match_values( self._to.values, from_to[:, 1], collocation_distance=collocation_distance, ) # Find matching cells in_match = np.ones((self._from.values.shape[0], 2)) * np.nan in_match[from_ind[:, 0], 0] = from_ind[:, 1] in_match[to_ind[:, 0], 1] = to_ind[:, 1] out_match = np.ones_like(from_to) * np.nan out_match[from_ind[:, 1], 0] = from_ind[:, 0] out_match[to_ind[:, 1], 1] = to_ind[:, 0] cell_map = np.c_[ np.where(in_match[:, 0] == in_match[:, 1])[0], np.where(out_match[:, 0] == out_match[:, 1])[0], ] # Add vertices vert_new = np.ones_like(from_to, dtype="bool") vert_new[from_ind[:, 1], 0] = False vert_new[to_ind[:, 1], 1] = False ind_new = np.where(vert_new.flatten())[0] uni_new, inv_map = np.unique( from_to.flatten()[ind_new], return_inverse=True ) # Add cells new_cells = np.ones_like(from_to.flatten()) * np.nan new_cells[ind_new] = self.add_vertices(self.desurvey(uni_new))[inv_map] new_cells = new_cells.reshape((-1, 2)) new_cells[from_ind[:, 1], 0] = self.cells[from_ind[:, 0], 0] new_cells[to_ind[:, 1], 1] = self.cells[to_ind[:, 0], 1] new_cells = np.delete(new_cells, cell_map[:, 1], 0) # Append values input_values = merge_arrays( np.ones(self.n_cells) * np.nan, np.r_[input_values], replace="B->A", mapping=cell_map, ) self._from.values = merge_arrays( self._from.values, from_to[:, 0], mapping=cell_map ) self._to.values = merge_arrays( self._to.values, from_to[:, 1], mapping=cell_map ) self.cells = np.r_[self.cells, new_cells.astype("uint32")] return input_values