def buildLocationVector(raws_user_staypointcenter, mindist=80): ''' 建立分类算法时需要用到的用户总的位置向量列表,提取用户停留位置中心,删除重复的停留位置中心-v1.1--------------算法还存在漏洞,在最后写毕设的时候再来排查 :param raws_user_staypointcenter: dict{'user':list[Location]},原始用户的位置数据 :param mindist: 位置归类,最小距离为20m :return: 所有用户的停留位置中心向量列表final_vector类型为list[Location] ''' final_vector = [] for key_fisrt in raws_user_staypointcenter.keys(): content = raws_user_staypointcenter.get(key_fisrt) # 获得某个用户的所有中心位置点 skip = False for item in content: # 开始循环 for skip_item in final_vector: #查找某个位置是否已经添加至最终位置列表中 if skip_item.lat == item.lat and skip_item.longti == item.longti: #由于每次添加位置信息都是新生产一个Location类型实例,所以必须要使用数值相等判断是否相等而不能使用地址相等判断 # print('------skipping [%f,%f] ==> [%f,%f]------' % (skip_item.lat, skip_item.longti, item.lat, item.longti)) skip = True break if skip: skip = False continue nerbor = [] for key_second in raws_user_staypointcenter.keys( ): #从所有用户数据中依次寻找符合条件的同类型中心点 temp = raws_user_staypointcenter.get(key_second) for line in temp: if item == line: continue dist = calDistance(item, line) if dist <= mindist: #符合条件的中心点,依次添加至临时向量中,由于这里是直接添加的中心点的地址,因此在后续修改数值时,可以直接影响原始结果 nerbor.append(line) if len(nerbor) != 0: nerbor.append(item) location = Location() location.lat, location.longti = calAvgPoint(nerbor) isnew = False #用来判断是否产生新的中心点 for ci in nerbor: for cm in final_vector: #判断这个新的中心点是否是从已经计算过的中心点中再次产生的 if cm.lat == ci.lat and cm.longti == ci.longti: #表明新产生的中心点是从已经计算过的中心点中产生的,因此只需修改已计算过的中心点数值,然后将其他中心点的值改为最新值即可 isnew = True cm.lat, cm.longti = location.lat, location.longti break # print('******changing [%f,%f] ==> [%f,%f]******' % (ci.lat, ci.longti, location.lat, location.longti)) ci.lat, ci.longti = location.lat, location.longti if not isnew: #表明这是一个完成新产生的中心点 final_vector.append(location) else: final_vector.append(item) return final_vector
def generateRandomDistributedClan(_count, x, y, dist): _geesearray = [] for count in range(_count): _location = Location(x + random.uniform(-dist, dist) * 2, y + random.uniform(-dist, dist)) _geesearray.append(entity.generateRandomClanGoose(_location)) return _geesearray
def __init__(self, entity_id=-1, server=None): super(MissileEntity, self).__init__(entity_id) self.player_id = -1 self.server = server self.toward_vector = [0, 0, 0] self.init_location = Location()
def buildUserTFIDFVector(user_location_vector, user_final_location_vector): ''' 建立某个用户的TFIDF向量列表-v1.1 :param user_location_vector: 某用户的停留位置中心向量列表,类型为list[location] :param user_final_location_vector: 所有用户总的停留位置向量列表,类型为list[location] :return: 返回值为list[location] ''' result = [] for raws_locations in user_final_location_vector: location = Location() location.lat, location.longti = raws_locations.lat, raws_locations.longti location.tfidf = 0 for item in user_location_vector: if location.lat == item.lat and location.longti == item.longti: location.tfidf = item.tfidf break result.append(location) return result
def loadClassifyData(dir=r'saveresults', filename=r'clusterresult.pkl'): ''' 从二进制文件中加载停留位置数据信息,为分类算法提供输入数据-v1.1 :param dir: 保存结果目录名 :param filename: 保存数据文件名 :return: 返回所有用户的中心位置,类型为 dict{'user':list[location]} ''' data = {} for (thisDir, dirsHere, filesHere) in os.walk(dir): for files in filesHere: user = files.split('&')[0] content = loadStayPointPklResults(dirs=dir, fname=files) locations = [] for line in content: location = Location() location.lat, location.longti = line.centerlat, line.centerlon location.tfidf = 0 locations.append(location) data[user] = locations return data
def loadRawsData(dirs=r'rawsdata', fname=r'5477.txt'): ''' 从文本文件中加载原始地理位置数据信息,DBScan挖掘用户停留区域的数据源,包括纬度(浮点类型)、经度(浮点类型)、时间点(日期类型)-v1.1 :param dirs: 目录名 :param fname:是读取存储处理好的数据文件名 :return: list[Location] 用户所有地理位置数量number ''' fname = combileFileName(dirs, fname) result = [] number = 0 for line in open(fname): content = line.rstrip().split(',') location = Location() location.lat = float(content[0]) location.longti = float(content[1]) # location.timer = float(content[2]) location.timer = datetime.strptime(content[2], str_formate) result.append(location) number += 1 return result, number
def generateRandomClan(_count, x, y): _geesearray = [] _location = Location(x, y) for count in range(_count): _geesearray.append(entity.generateRandomClanGoose(_location)) return _geesearray