def get_state(boat, other_boat): # State minimalist a etoffer pour une generalisation rot_angle = angle_between(np.array([1, 0]), boat.target) cor_other_pos = other_boat.pos - boat.pos rot_other_pos = [ cor_other_pos[0] * np.cos(rot_angle) + cor_other_pos[1] * np.sin(rot_angle), -cor_other_pos[0] * np.sin(rot_angle) + cor_other_pos[1] * np.cos(rot_angle) ] rot_other_speed = [ other_boat.speed[0] * np.cos(rot_angle) + other_boat.speed[1] * np.sin(rot_angle), -other_boat.speed[0] * np.sin(rot_angle) + other_boat.speed[1] * np.cos(rot_angle) ] rot_speed = [ boat.speed[0] * np.cos(rot_angle) + boat.speed[1] * np.sin(rot_angle), -boat.speed[0] * np.sin(rot_angle) + boat.speed[1] * np.cos(rot_angle) ] cur_state = [] cur_state.append(distance(boat.pos, boat.target)) cur_state.append(angle_between(boat.pos - boat.target, boat.dir)) cur_state.append(rot_speed[0]) cur_state.append(rot_speed[1]) cur_state.append(rot_other_pos[0]) cur_state.append(rot_other_pos[1]) cur_state.append(rot_other_speed[0]) cur_state.append(rot_other_speed[1]) cur_state.append(distance(boat.pos, other_boat.pos)) return np.array(cur_state).reshape((1, 9))
def geocast_knn(data, t): # find all workers in MTD # find all workers in the query MTD_RECT = np.array([[t[0] - Params.ONE_KM * Params.MTD, t[1] - Params.ONE_KM * Params.MTD], [t[0] + Params.ONE_KM * Params.MTD, t[1] + Params.ONE_KM * Params.MTD]]) locs = rect_query_points(data, MTD_RECT).transpose() locs = sorted(locs, key=lambda loc: distance(loc[0], loc[1], t[0], t[1])) u, dist, found = 0, 0, False workers = np.zeros(shape=(2, 0)) for loc in locs: workers = np.concatenate([workers, np.array([[loc[0]], [loc[1]]])], axis=1) _dist = distance(loc[0], loc[1], t[0], t[1]) u_c = acc_rate(Params.MTD, _dist) u = 1 - (1 - u) * (1 - u_c) if is_performed(u_c): if not found: found = True dist = _dist if u >= Params.U: break # simulation isPerformed, worker, dist_fcfs = performed_tasks(workers, Params.MTD, t, True) hops_count, coverage, hops_count2 = hops_expansion(t, workers.transpose(), Params.NETWORK_DIAMETER) if isPerformed: # the task is performed return workers.shape[1], True, dist, dist_fcfs, hops_count, coverage, hops_count2 return workers.shape[1], False, None, None, hops_count, coverage, hops_count2
def __init__(self, data, param): Grid_adaptive.__init__(self, data, param) # compute the best grid size at level 1 # grid size d1 = distance(Params.LOW[0], Params.LOW[1], Params.LOW[0], Params.HIGH[1]) d2 = distance(Params.LOW[0], Params.LOW[1], Params.HIGH[0], Params.LOW[1]) mind = min(d1, d2) self.m = int(math.floor(mind / (2 * Params.MTD))) logging.debug("Grid_adaptive_localness: Level 1 size: %d" % self.m)
def getNegRatio(self, curr): """ return true count """ ratio = None if curr.n_data is not None: neg_tweets = curr.n_data[2, :].transpose().tolist().count(-1) pos_tweets = curr.n_data[2, :].transpose().tolist().count(1) # zero_tweets = curr.n_data[2, :].transpose().tolist().count(0) total_tweets = curr.n_data.shape[1] if neg_tweets > 15: ratio = (neg_tweets + 0.0) / (total_tweets) center = curr.center() print distance(center[0], center[1], self.param.epicenter[0], self.param.epicenter[1]), '\t', ratio return ratio
def update(self): other_boat = None for b in self.boatList: if not b is self: other_boat = b break self.state = get_state(self, other_boat) super().update() if not self.done: reward = -0.001 if len(self.partial_rewards) > 0 and distance( self.pos, self.target) < self.partial_rewards[0]: self.partial_rewards.pop(0) reward += self.reward_level #self.reward_level += 1 if self.isColliding: self.done = True reward -= 5 elif self.target_reached: self.done = True reward += 5 new_state = get_state(self, other_boat) self.agent.remember(self.state, self.last_action_id, reward, new_state, self.done) self.total_reward += reward
def selection_WST(data, t, tree=None): # find all workers in MTD MTD_RECT = np.array([[ t[0] - Params.ONE_KM * Params.MTD, t[1] - Params.ONE_KM * Params.MTD ], [t[0] + Params.ONE_KM * Params.MTD, t[1] + Params.ONE_KM * Params.MTD]]) locs = rect_query_points(data, MTD_RECT).transpose() #locs = sorted(locs, key=lambda loc: distance(loc[0], loc[1], t[0], t[1])) #u = 0 workers, dists = np.zeros(shape=(2, 0)), [] # find workers who would perform the task for loc in locs: dist = distance(loc[0], loc[1], t[0], t[1]) u_c = acc_rate(Params.MTD, dist) #u = 1 - (1 - u) * (1 - u_c) if is_performed(u_c): workers = np.concatenate( [workers, np.array([[loc[0]], [loc[1]]])], axis=1) dists.append(dist) # simulation if workers.shape[1] == 0: # no workers return 0, False, None, None, None, None, None return len(locs), True, 0, random.choice(dists), 0, 0, 0
def __init__(self, param, initState, targets): super().__init__(param, initState, targets) self.agent = None self.last_action_id = 0 self.done = False self.init_dist = distance(self.pos, self.target) self.reward_level = 1 self.state = self.get_state() self.total_reward = 0
def gen_vect(pos, boat): vect = np.array([0, 0]) d = distance(pos, boat.pos) V = boat.pos - pos N = np.array([-boat.dir[1], boat.dir[0]]) vect = 30 * (V / d - 0.2 * N) / d if (V[0]) * boat.dir[0] + (V[1]) * boat.dir[1] < 0: vect = vect return vect
def select(self, coords): dists = [sum([distance(a, b) for b in self.archive]) / len(self.archive) for a in coords] ids = list(range(len(coords))) ids.sort(key=lambda i: dists[i], reverse=True) i = 0 while i < len(ids) and dists[ids[i]] > self.MIN_ARCHIVE_ADD_DISTANCE: self.archive.add(coords[ids[i]]) i += 1 print("archive len: ", len(self.archive)) return dists
def __init__(self, param, initState, targets): super().__init__(param, initState, targets) self.agent = None self.state = None self.last_action_id = 0 self.done = False self.init_dist = distance(self.pos, self.target) #self.partial_rewards = [init_dist/2,init_dist/4,init_dist/8, init_dist/16] self.partial_rewards = [200] self.reward_level = 1 self.total_reward = 0
def prev_collision(boat1, boat2, step=0, nb_test=5): next_pos1 = boat1.phantom_boat.pastTrajectory.to_list() next_pos2 = boat2.phantom_boat.pastTrajectory.to_list() nb_pos = min(len(next_pos1), len(next_pos2)) - 1 if step == -1: pass elif step == 0: for i in range(1, nb_test + 1): j = i * (nb_pos // (nb_test + 1)) if distance(next_pos1[j], next_pos2[j]) < boat1.colRadius + boat2.colRadius: return True, j else: j = 0 for i in range(nb_pos, 1, -step): if distance(next_pos1[i], next_pos2[i]) < boat1.colRadius + boat2.colRadius: return True, j j += step return distance(next_pos1[0], next_pos2[0]) < boat1.colRadius + boat2.colRadius, nb_pos
def evalDivGeoI(p, D_actual): exp_name = sys._getframe().f_code.co_name logging.info(exp_name) res_cube = np.zeros((len(eps_list), len(seed_list), len(divMetricList))) sensitivity = p.M # diversitySensitivity(p.M) differ = Differential(p.seed) u = distance(p.x_min, p.y_min, p.x_max, p.y_min) * 1000.0 / Params.GEOI_GRID_SIZE v = distance(p.x_min, p.y_min, p.x_min, p.y_max) * 1000.0 / Params.GEOI_GRID_SIZE rad = euclideanToRadian((u, v)) cell_size = np.array([rad[0], rad[1]]) for j in range(len(seed_list)): for i in range(len(eps_list)): p.seed = seed_list[j] p.eps = eps_list[i] D_noisy = defaultdict(Counter) for lid, loc in p.locDict.iteritems(): eps = p.eps/sensitivity noisyLoc = differ.addPolarNoise(eps, loc, p.radius) # perturbed noisy location # rounded to grid roundedPoint = round2Grid(noisyLoc, cell_size, p.x_min, p.y_min) cellId = coord2CellId(roundedPoint, p) # obtain cell id from noisy location D_noisy[cellId].update(p.locs[lid]) # update count(userid/freq) actual, noisy = [], [] for cellId, d in D_actual.iteritems(): actual.append(d) noisy.append(normalizeDiversity(randomEntropy(len(D_noisy.get(cellId, Counter([])))))) # default entropy = 0 for k in range(len(divMetricList)): res_cube[i, j, k] = divMetricList[k](actual, noisy) res_summary = np.average(res_cube, axis=1) np.savetxt(p.resdir + Params.DATASET + "_" + exp_name + '_M' + str(p.M) + '_C' + str(p.C), res_summary, header="\t".join([f.__name__ for f in divMetricList]), fmt='%.4f\t')
def probabilities(self, ant): probabs = [0.0 for _ in range(0, self.__problParams["noNodes"])] #sum of pheromones on edges for possible destinations i = ant.trail[ant.trailLength - 1] pheromones = 0 for pos in range(0, self.__problParams["noNodes"]): if not ant.isVisited(pos) and distance(i, pos, self.__problParams["matrix"]) != 0: pheromones += computeNumerator(self.__trails[i][pos], 1.0 / distance(i, pos, self.__problParams["matrix"]), self.__params["pheromoneImportance"], self.__params["distancePriority"]) for pos in range(0, self.__problParams["noNodes"]): if ant.isVisited(pos) and distance(i, pos, self.__problParams["matrix"]) != 0: probabs[pos] = 0.0 elif distance(i, pos, self.__problParams["matrix"]) != 0: probabs[pos] = probability(self.__trails[i][pos], 1.0 / distance(i, pos, self.__problParams["matrix"]), self.__params["pheromoneImportance"], self.__params["distancePriority"], pheromones) return probabs
def evalCountGeoI(p, C_actual): exp_name = sys._getframe().f_code.co_name logging.info(exp_name) res_cube = np.zeros((len(eps_list), len(seed_list), len(freqMetricList))) differ = Differential(p.seed) u = distance(p.x_min, p.y_min, p.x_max, p.y_min) * 1000.0 / Params.GEOI_GRID_SIZE v = distance(p.x_min, p.y_min, p.x_min, p.y_max) * 1000.0 / Params.GEOI_GRID_SIZE rad = euclideanToRadian((u, v)) cell_size = np.array([rad[0], rad[1]]) for j in range(len(seed_list)): for i in range(len(eps_list)): p.seed = seed_list[j] p.eps = eps_list[i] C_noisy = defaultdict() for lid, loc in p.locDict.iteritems(): noisyLoc = differ.addPolarNoise(p.eps, loc, p.radius) # perturbed noisy location # rounded to grid roundedPoint = round2Grid(noisyLoc, cell_size, p.x_min, p.y_min) cellId = coord2CellId(roundedPoint, p) # obtain cell id from noisy location C_noisy[cellId] = C_noisy.get(cellId, 0) + 1 actual, noisy = [], [] for cellId, c in C_actual.iteritems(): if c > 0: actual.append(c) noisy.append(C_noisy.get(cellId, Params.DEFAULT_ENTROPY)) # default entropy = 0 for k in range(len(freqMetricList)): res_cube[i, j, k] = freqMetricList[k](actual, noisy) res_summary = np.average(res_cube, axis=1) np.savetxt(p.resdir + Params.DATASET + "_" + exp_name + "_m" + str(p.m) + '_r' + str(p.radius), res_summary, header="\t".join([f.__name__ for f in divMetricList]), fmt='%.4f\t')
def update(self): reward = np.dot(unit_vector(self.dir), unit_vector(self.target-self.pos)) * \ (self.init_dist-distance(self.pos, self.target))/(self.init_dist*2000) previous_state = self.state super().update() if not self.done: if self.isColliding: self.done = True reward -= 3 elif self.target_reached: self.done = True reward += 2 self.state = self.get_state() self.agent.remember(previous_state, self.last_action_id, reward, self.state, self.done) self.total_reward += reward
def get_state(self): # State minimalist a etoffer pour une generalisation rot_angle = angle_between(np.array([1, 0]), self.target) speed = [ self.speed[0] * np.cos(rot_angle) + self.speed[1] * np.sin(rot_angle), -self.speed[0] * np.sin(rot_angle) + self.speed[1] * np.cos(rot_angle) ] cur_state = [] cur_state.append(distance(self.pos, self.target) / 2000) cur_state.append(angle_between(self.pos - self.target, self.dir)) cur_state.append(self.angular_speed) cur_state.append(speed[0]) cur_state.append(speed[1]) return np.array(cur_state).reshape((1, 5))
def extract_tweets(tweet_input, tweet_output, tweet_index=0, type='tweet_only'): print 'extracting...', tweet_input if type == 'tweet_only': columns = 5 elif type == 'with_sentiment': basepath, filename = os.path.split(file) label_file = Params.label_folder + filename if not os.path.isfile(label_file): return labels = np.loadtxt(label_file) j = 0 columns = 5 elif type == 'with_informative': columns = 6 elif type == 'without_tweet': columns = 6 with open(tweet_input, 'rU') as f: with open(tweet_output, "w") as f2: for a in f: b = [] if len(a.split(',')) >= columns: a = a.split(',') tweet = ','.join([a[i] for i in xrange(0, len(a) - columns + 1)]) b.append(tweet) b += [a[i] for i in xrange(len(a) - columns + 1, len(a))] elif len(b) < columns: print len(b), b j = j + 1 continue if type == 'tweet_only': s = re.sub('[\s]+|&', ' ', b[tweet_index]) # Remove additional white spaces s = re.sub(r'https?:\/\/.*\/[a-zA-Z0-9]*', '', s) # Remove hyperlinks f2.write(s + "\n") elif type == 'with_sentiment': line = str(b[0]) + ',' + str(b[1]) + ',' + str(int(b[2])) + ',' + str(float(b[3])) + ',' + str(float(b[4].rstrip('\n'))) + ',' + str(int(labels[j])) + '\n' f2.write(line) j = j + 1 elif type == 'with_informative': line = str(b[0]) + ',' + str(b[1]) + ',' + str(int(b[2])) + ',' + str(float(b[3])) + ',' + str( float(b[4].rstrip('\n'))) + ',' + str(int(b[5])) + '\n' f2.write(line) elif type == 'without_tweet': t = calendar.timegm(datetime.datetime.strptime(b[1].strip(), "%Y-%m-%d %H:%M:%S").timetuple()) d = distance(float(b[3]), float(b[4]), 38.2414392,-122.3128157) f2.write(str(b[1])+ '\t' + str(t) + '\t' + str(int(b[2])) + '\t' + str(float(b[3])) + '\t' + str(float(b[4].rstrip('\n'))) + '\t' + str(int(b[5])) + '\t' + str(d) + '\n')
def update(self): if not self.target_reached: self.do_action() if distance(self.target, self.pos) < self.colRadius: if len(self.next_targets) == 0: self.target_reached = True self.cape = 0 self.gear = 0 else: self.target = np.array(self.next_targets.pop(), dtype=int) else: self.gear = 0 self.update_angle() self.update_speed() self.collision() if self.total_step % self.save_delay == 0: self.save_pos() self.total_step += 1
def selection_WST(data, t, tree=None): # find all workers in MTD MTD_RECT = np.array([[t[0] - Params.ONE_KM * Params.MTD, t[1] - Params.ONE_KM * Params.MTD], [t[0] + Params.ONE_KM * Params.MTD, t[1] + Params.ONE_KM * Params.MTD]]) locs = rect_query_points(data, MTD_RECT).transpose() #locs = sorted(locs, key=lambda loc: distance(loc[0], loc[1], t[0], t[1])) #u = 0 workers, dists = np.zeros(shape=(2, 0)), [] # find workers who would perform the task for loc in locs: dist = distance(loc[0], loc[1], t[0], t[1]) u_c = acc_rate(Params.MTD, dist) #u = 1 - (1 - u) * (1 - u_c) if is_performed(u_c): workers = np.concatenate([workers, np.array([[loc[0]], [loc[1]]])], axis=1) dists.append(dist) # simulation if workers.shape[1] == 0: # no workers return 0, False, None, None, None, None, None return len(locs), True, 0, random.choice(dists), 0, 0, 0
def area(self): return distance(self.n_box[0,0],self.n_box[0,1],self.n_box[0,0],self.n_box[1,1]) * distance(self.n_box[0,0],self.n_box[0,1],self.n_box[1,0],self.n_box[0,1]);
def collide(boat1, boat2): return distance(boat1.pos, boat2.pos) < boat1.colRadius + boat2.colRadius
trueR = np.array([[float(ss[0]), float(ss[1]), float(ss[2])], [float(ss[4]), float(ss[5]), float(ss[6])], [float(ss[8]), float(ss[9]), float(ss[10])]]) trueRx, trueRy, trueRz = rotationMatrixToEulerAngles(trueR) trueRvector = np.array([[trueRx], [trueRy], [trueRz]]) # buildObj("dump/groundtruth/", [[20*float(ss[1])], [20*float(ss[2])], [20*float(ss[3])]]) cv.circle(trajectory, (x, y), 1, (0, 0, 255), 2) cv.circle(trajectory, (trueX, trueY), 1, (0, 255, 0)) error = distance((x, y, 0), (trueX, trueY, 0)) rotationError = np.linalg.norm(computedR - trueRvector) localErrors = np.append(localErrors, error) localRotationErrors = np.append(localRotationErrors, rotationError) draw_str(trajectory, (20, 20), "Local error %f" % (localErrors.mean())) draw_str(trajectory, (750, 20), "Local angular error %f" % (localRotationErrors.mean())) if localErrors.size >= Parameters.localPathThresh: localErrors = np.array([]) localRotationErrors = np.array([]) cv.imshow("frame", frame) cv.imshow("Trajectory", trajectory) ch = cv.waitKey(1)