Ejemplo n.º 1
0
    def reduce_latency(self, locations_by_uid):

        for i in range(INTERDEPENDENCY_ITERATIONS):
            uuid_to_interdependencies = []
            for uid, location in locations_by_uid.items():
                # Find a count for unique uuids a uuid is interdependent with, and how many requests for each interdependent request are made
                uid_count = 0
                visited = []
                req_count = 0
                for req in self.amap:
                    if uid == req['dep']:
                        if uid not in visited:
                            uid_count = uid_count + 1
                            visited.append(uid)
                    req_count = req_count + 1
                uuid_to_interdependencies.append((uid, req_count))
                #self.log_manager.get_interdependency_grouped_by_uuid(uuid)

            for tuple in uuid_to_interdependencies:
                other_item_uuid = tuple[0]
                other_item_location = locations_by_uid[other_item_uuid]
                request_count = tuple[1]
                distance = util.get_distance(location, other_item_location)
                weight = 1 / (1 + (KAPPA * distance * request_count))
                location = self.interp(weight, location, other_item_location)

            locations_by_uid[uid] = location

        return locations_by_uid
Ejemplo n.º 2
0
def retrieve_data(filename):
    total = []
    error_count  = 0

    for line in open(filename, 'r'):
        line = line.strip().split(',')
        plong,plat,dlong,dlat=line[-4:]
        try:
            plong = float(plong)
            plat = float(plat)
            dlong = float(dlong)
            dlat = float(dlat)

            # part b
            distance = util.get_distance(plat,plong,dlat,dlong)

            ptime = float(util.change_time(line[5]))
            ttime = float(line[8])
            tdist = float(line[9])

            total.append([ttime, tdist, ptime, distance, plong, plat, dlong, dlat])
        except:
            error_count += 1

    data = np.asarray(total)
    print "number of distances","maximum distance","minimum distance"
    print len(data[:,3]),max(data[:,3]),min(data[:,3]) # distance is measured in miles
    return data
Ejemplo n.º 3
0
    def evaluate(self, read_logs=None, moving_logs=None):
        # collect server logs
        if read_logs is None:
            read_logs = self.aggregator.get_read_log_entries(
                self.start_time, self.end_time)
        if moving_logs is None:
            moving_logs = self.aggregator.get_moving_log_entries(
                self.start_time, self.end_time)
        # calculate the average latency
        latency_sum = 0
        request_count = 0
        ip_cache = ip_location_cache()
        for log in read_logs:
            timestamp, uuid, source, source_uuid, dest, req_type, status, response_size = log
            client_loc = ip_cache.get_lat_lon_from_ip(source)
            server_loc = ip_cache.get_lat_lon_from_ip(dest)
            distance = util.get_distance(client_loc, server_loc)
            unit = 1000.0
            latency = distance / unit
            request_importance = 1
            latency_sum += latency * request_importance
            request_count += request_importance
        average_latency = latency_sum / request_count

        inter_datacenter_traffic = 0
        for log in moving_logs:
            timestamp, uuid, source, source_uuid, dest, req_type, status, response_size = log
            # treat all files as uniform size
            inter_datacenter_traffic += 1
        # display latency, cost, etc
        return average_latency, inter_datacenter_traffic
Ejemplo n.º 4
0
 def get_dict(self, sort_type, lat=None, long=None):
     dict = {}
     dict['name'] = self.name
     dict['location'] = self.location
     dict['building_name'] = self.building_name
     dict['atResidence'] = self.atResidence
     dict['latitude'] = self.latitude
     dict['longitude'] = self.longitude
     dict['error_msg'] = self.error_msg
     dict['status'] = self.status
     if sort_type == 'name':
         dict['section_header'] = dict['name'][0]
         dict['distance'] = 0.0
     elif sort_type == 'building':
         dict['section_header'] = dict['building_name']
         dict['distance'] = 0.0
     elif sort_type == 'distance':
         if lat == None or long == None:
             dict['section_header'] = ""
         else:
             dist = util.get_distance(lat, long, self.latitude / 1000000.0, self.longitude / 1000000.0)
             dict['distance'] = dist
             if dist < 0.1:
                 dict['section_header'] = "< 0.1 miles"
             elif dist >= 0.1 and dist < 0.3:
                 dict['section_header'] = "0.1 - 0.3 miles"
             elif dist >= 0.3 and dist < 0.5:
                 dict['section_header'] = "0.3 - 0.5 miles"
             elif dist >=0.5 and dist < 1.0:
                 dict['section_header'] = "0.5 - 1.0 miles"
             else:
                 dict['section_header'] = "> 1.0 miles"
     else:
         dict['section_header'] = ""
     return dict
Ejemplo n.º 5
0
def filter_places(api, places, start, min_rating):
    highest_rated = {}
    closest = {}
    closest_dist = 5000
    result_term = 'results' if api == 'google' else 'businesses'
    review_count_term = 'user_ratings_total' if api == 'google' else 'review_count'

    if not places.get(result_term, False):
        print('No places of place type for {} - places = {}'.format(
            start, places))
        return None

    for idx, place in enumerate(places[result_term]):
        #print ('place {}'.format(idx))
        if api == 'google':
            if place['business_status'] == 'CLOSED_PERMANENTLY':
                #print('It"s closed bitches - {}'.format(place['business_status']))
                continue
        else:
            if place['is_closed'] == 'true':
                #print('It"s closed bitches - {}'.format(place['is_closed']))
                continue
        if place['rating'] < min_rating and place[review_count_term] >= 10:
            #print('It"s shit - Rating {} from {} reviews'.format(place['rating'],place[review_count_term]))
            continue

        if place['rating'] > highest_rated.get('rating', 0):
            highest_rated = place

        if api == 'google':
            place_geo = (place['geometry']['location']['lat'],
                         place['geometry']['location']['lng'])
        else:
            place_geo = (place['coordinates']['latitude'],
                         place['coordinates']['longitude'])
        place_dist = get_distance(place_geo, start)
        if place_dist < closest_dist:
            closest = place
            closest_dist = place_dist
    #print('Highest Rated ({}):'.format(highest_rated.get('rating',0)))
    #print(json.dumps(highest_rated, indent=2))
    #print('Closest ({}mi):'.format(closest_dist))
    #print(json.dumps(closest, indent=2))
    return {
        'highest_rated': {
            'place': highest_rated,
            'value': highest_rated.get('rating', 0)
        },
        'closest': {
            'place': closest,
            'value': closest_dist
        }
    }
Ejemplo n.º 6
0
    def reduce_latency(self, locations_by_uuid):

        for i in range(INTERDEPENDENCY_ITERATIONS):
            for uuid, location in locations_by_uuid.iteritems():
                uuid_to_interdependencies = self.log_manager.get_interdependency_grouped_by_uuid(
                    uuid)

                for tuple in uuid_to_interdependencies:
                    other_item_uuid = tuple[0]
                    other_item_location = locations_by_uuid[other_item_uuid]
                    request_count = tuple[1]
                    distance = util.get_distance(location, other_item_location)
                    weight = 1 / (1 + (KAPPA * distance * request_count))
                    location = self.interp(weight, location,
                                           other_item_location)

                locations_by_uuid[uuid] = location

        return locations_by_uuid
Ejemplo n.º 7
0
    def get_reward(self, data):
        global name
        global last_data
        score = data['tanks'][config.TANK_ID[name]]['score']
        last_score = last_data['tanks'][config.TANK_ID[name]]['score'] if last_data else score

        last_pos = (last_data['tanks'][config.TANK_ID[name]]['position'][0],
                    last_data['tanks'][config.TANK_ID[name]]['position'][1],
                    last_data['tanks'][config.TANK_ID[name]]['direction']) if last_data else (0, 0, 0)

        distance = util.get_distance(self.me, last_pos)

        reward = score-last_score
        if reward == 0:
            if distance < 0.15:
                reward = -5
        elif reward < 0:
            reward = -10

        print("reward is {}.".format(reward))
        return reward
def compute_min_dis(points_1, points_2):
    min_dis = 100
    for i in itertools.product(points_1[0], points_2[0]):
        distance = calculate_distance(i[0], i[1])
        if distance < min_dis:
            min_dis = distance
    central_dis = calculate_distance(np.mean(points_1[0], axis=0),
                                     np.mean(points_2[0], axis=0))
    if central_dis < min_dis:
        min_dis = central_dis
    # max_xyz_1 = np.max(np.array(points_1), axis=0)
    # min_xyz_1 = np.min(np.array(points_1), axis=0)
    # max_xyz_2 = np.max(np.array(points_2), axis=0)
    # min_xyz_2 = np.min(np.array(points_2), axis=0)
    # four_1 = [(max_xyz_1[0], max_xyz_1[1]), (min_xyz_1[0], min_xyz_1[1]),
    #           (min_xyz_1[0], max_xyz_1[1]), (max_xyz_1[0], min_xyz_1[1])]
    # four_2 = [(max_xyz_2[0], max_xyz_2[1]), (min_xyz_2[0], min_xyz_2[1]),
    #           (min_xyz_2[0], max_xyz_2[1]), (max_xyz_2[0], min_xyz_2[1])]

    # dis = util.get_distance_simple(four_1, four_2)
    dis = util.get_distance(points_1[1], points_2[1])
    if dis < min_dis:
        min_dis = dis
    return min_dis
Ejemplo n.º 9
0
    def get_observation(self, data):
        # observation = []
        tank_observation = []
        bullet_observation = []
        tanks = data['tanks']
        bullets = data['bullets']
        global name

        # --------------------------me---------------------------------

        my_tank = tanks[self.my_tank_name]

        self.me = [my_tank['position'][0], my_tank['position'][1], my_tank['direction'],
                   my_tank['score'], my_tank['fireCd'], my_tank['shieldCd'],
                   my_tank['rebornCd'] if my_tank['rebornCd'] else 0]

        # ------------------------enemy--------------------------------
        self.enemy_list = []
        for (tank_name, value) in tanks.items():
            if tank_name == self.my_tank_name:
                continue
            # -----------tank feature------------
            enemy = [value['position'][0], value['position'][1], value['direction'],
                     value['score'], value['fireCd'], value['shieldCd'], value['rebornCd'] if value['rebornCd'] else 0]
            self.enemy_list.append(enemy)

        # sorted by score from minimum to maximum
        self.enemy_list = sorted(self.enemy_list, key=lambda x:x[3], reverse=False)

        # while len(self.enemy_list) < MAX_TANK:
        #     self.enemy_list.append([0]*TANK_FEATURES)

        for enemy in self.enemy_list[:MAX_TANK]:
            tank_observation += [e for e in enemy[:TANK_FEATURES]]

        tank_observation = np.pad(tank_observation,
                                  (0, MAX_TANK*TANK_FEATURES-len(tank_observation)), 'constant')

        print("The length of tank_observation is {}".format(len(tank_observation)))

        # ------------------------bullet-------------------------------
        self.bullet_list = []
        for bullet in bullets:
            # ------------bullet feature-------------
            distance = util.get_distance(self.me, [bullet['position'][0], bullet['position'][1]])
            self.bullet_list.append([
                bullet['position'][0], bullet['position'][1], bullet['direction'], distance])

        # sorted by distance from maximum to minimum
        self.bullet_list = sorted(self.bullet_list, key=lambda x:x[3], reverse=True)

        # while len(self.bullet_list) < MAX_BULLET:
        #     self.bullet_list.append([0]*BULLET_FEATURES)

        for bullet in self.bullet_list[:MAX_BULLET]:
            bullet_observation += [b for b in bullet[:BULLET_FEATURES]]

        bullet_observation = np.pad(bullet_observation,
                                    (0, MAX_BULLET*BULLET_FEATURES-len(bullet_observation)), 'constant')

        print("The length of bullet_observation is {}".format(len(bullet_observation)))

        return {
            "tank": np.reshape(np.array(tank_observation), (-1, TANK_FEATURES)),
            "bullet": np.reshape(np.array(bullet_observation), (-1, BULLET_FEATURES))
        }