Exemplo n.º 1
0
 def __init__(self, iterate, **kwargs):
     self.iterate = iterate
     self.startState = file.read()
     self.neighbor = Neighbor(self.startState)
     self.update_states = None
     if "update_states" in kwargs:
         self.update_states = True
Exemplo n.º 2
0
    def nearest_text(self, *args):
        """
            arg[0] = type (user, photo, poi)
            arg[1] = id
            arg[2] = model (tf, df, tf-idf)
            arg[3] = k
        """
        
        if not self.database:
            print("[ERROR] The database must be loaded for this action.")
            return

        if not len(args) is 4:
            print("Nearest Text expected 4 arguments but got " + str(len(args)) + ".")
            print("\targs = " + str(args))
            return

        # Get the first argument
        try:
            k = int(args[3])
        except:
            print("[ERROR] K Value provided is invalid.")
            print("\tk = " + str(args[0]))
            return
        
        # Get the type of item we are considering.
        itype = args[0]
        if not itype in self.valid_types:
            print("[ERROR] Item Type value provided was invalid.")
            print("\tItem Type = " + str(args[1]))
            return
        
        # Get the model to use. We do this before the item as it is easier
        #   to differentiate valid from invalid
        model = args[2]
        model = model.lower()
        if not model in self.valid_txt_models:
            print("[ERROR] Model Type value provided was invalid.")
            print("\tModel Type = " + str(args[2]))
            return

        try:
            # get vector representing item associated w/ id
            an_id = args[1]
            this_vector = Vectorizer.text_vector(an_id, self.database, model, itype)
        except:
            print("[ERROR] The ID specified was not found in the dataset.")
            print("\tID = " + an_id + "; Type = " + itype + "; Model = " + model)
            return
        
        nearest = Neighbor.knn_textual(k, an_id, model, itype, self.database, this_vector=this_vector)
        contribs = Neighbor.similarity_by_id(this_vector, nearest.keys(), 
                                            self.database, model, itype)

        print(str(k) + " Nearest Neighbors:")
        for i, (an_id, distance) in enumerate(nearest.items()):
            print('\t' + str(i) + ". " + str(an_id) + "; Distance = " + str(distance))
        print('Top 3 Features:')
        for i, item in enumerate(contribs):
            print('\t' + str(i) + '. ' + str(item))
Exemplo n.º 3
0
    def hill(self):
        currentState = self.startState

        nextEval = Heuristic(currentState).attacks()

        i = 0
        while i < self.iterate and nextEval != 0:
            newState = self.neighbor.generateState()
            currentEval = Heuristic(newState).attacks()
            if self.update_states:
                print Heuristic(currentState).queensPosition(
                ), " -> ", Heuristic(newState).queensPosition()
            if currentEval <= nextEval:
                currentState = newState
                nextEval = Heuristic(currentState).attacks()

            i += 1
            self.neighbor = Neighbor(currentState)

        file.write(Heuristic(currentState).queensPosition(),
                   self.neighbor.createBoard(),
                   url="./resource/newBoard.txt")

        print "Hill Comum > Iteracao  : ", i
        print "Posicao Inicial das ", len(
            self.startState), " rainhas : ", Heuristic(
                self.startState).queensPosition()
        print "Posicao Final das ", len(
            self.startState), " rainhas : ", Heuristic(
                currentState).queensPosition()
        print "\tNumero de rainhas atacando : ", Heuristic(
            currentState).attacks()
        self.startState = currentState
        return Heuristic(currentState).attacks()
    def _calculate_cost_of_swapping_items(self, first, second):
        i = min(first, second)
        j = max(first, second)

        # set up weights
        weight_diff = self.trip[i][utils.WEIGHT] - self.trip[j][utils.WEIGHT]
        cum_weight_before_i = np.sum(
            self.trip[i:][:, utils.WEIGHT]) + utils.SLEIGH_WEIGHT
        cum_weight_before_j = np.sum(
            self.trip[j:][:, utils.WEIGHT]) + utils.SLEIGH_WEIGHT
        weight_i = self.trip[i][utils.WEIGHT]
        weight_j = self.trip[j][utils.WEIGHT]

        # set up locations
        before_i = tuple(self.trip[i - 1][[utils.LAT, utils.LON
                                           ]]) if i > 0 else utils.NORTH_POLE
        before_j = tuple(self.trip[j - 1][[utils.LAT, utils.LON
                                           ]]) if j > 0 else utils.NORTH_POLE
        at_i = tuple(self.trip[i][[utils.LAT, utils.LON]])
        at_j = tuple(self.trip[j][[utils.LAT, utils.LON]])
        after_i = tuple(self.trip[i + 1][[
            utils.LAT, utils.LON
        ]]) if i < len(self.trip) - 1 else utils.NORTH_POLE
        after_j = tuple(self.trip[j + 1][[
            utils.LAT, utils.LON
        ]]) if j < len(self.trip) - 1 else utils.NORTH_POLE

        if i + 1 == j:
            # swap adjacent locations is simplified
            improvement = self._get_cost_of_swapping_adjacent(
                before_i, at_i, at_j, after_j, cum_weight_before_i, weight_i,
                weight_j)
        else:
            # cost of the old segments around i/j
            old_i = Neighbor.get_cost_of_tour_of_three(before_i, at_i, after_i,
                                                       cum_weight_before_i,
                                                       weight_i)
            old_j = Neighbor.get_cost_of_tour_of_three(before_j, at_j, after_j,
                                                       cum_weight_before_j,
                                                       weight_j)

            # cost of the new segments around i/j
            new_j = Neighbor.get_cost_of_tour_of_three(before_i, at_j, after_i,
                                                       cum_weight_before_i,
                                                       weight_j)
            new_i = Neighbor.get_cost_of_tour_of_three(
                before_j, at_i, after_j, cum_weight_before_j + weight_diff,
                weight_i)

            # cost difference from weight between i and j (sub-self.trip between i+1..j-1)
            distance = 0
            for k in range(i + 1, j - 1):
                distance += utils.distance(
                    tuple(self.trip[k][[utils.LAT, utils.LON]]),
                    tuple(self.trip[k + 1][[utils.LAT, utils.LON]]))
            diff = distance * weight_diff
            improvement = new_j + new_i - old_j - old_i + diff

        return improvement
Exemplo n.º 5
0
    def task3(self, *args):
        """
        Command:\ttask3 <vis model> <k> <method> <j> <id>
        Description:\tIdentifies k latent semantics using model and methodology. Given an \
        id, finds the top j most related items to that id.
        Arguments:
        \tVisual Space - What visual description model to pull. ('CM', 'CM3x3', 'CN', \
        'CN3x3', 'CSD', 'GLRLM', 'GLRLM3x3', 'HOG', 'LBP', 'LBP3x3')
        \tK - Number of latent semantics to return.
        \tMethod - The decomposition method to use. (PCA, SVD, LDA).
        \tJ - Number of nearest terms to find.
        \tID - The id of the image to find nearest neighbors to using the latent semantics.
        """
        if len(args) < 5:
            print("[ERROR] Not enough args were provided. Expected 5 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if len(args) > 5:
            print("[ERROR] Too many arguments were provided. Expected 5 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if not self.__database__:
            print("[ERROR] The Database must be loaded before this can be run.")
            print("\tCommand: load <filepath>")
            return

        try:
            vis_model = args[0]
            k = int(args[1])
            method = args[2]
            j = int(args[3])
            anid = int(args[4])
        except:
            print("[ERROR] One or more arguments could not be parsed: " + str(args))

        # Get latent semantics.
        matrix = Decompose.decompose_vis(vis_model, k, method, self.__database__)

        # Get nearest images from latent semantics.
        vector = matrix.loc[anid]
        vector1 = []
        for i in range(k):
            vector1.append(vector[i])
        nearest_img = "5 nearest images to " + str(args[3]) + " are:\n"
        nearest = Neighbor.knn_dot(5, vector1, matrix)
        nearest_img += "IMAGE ID\t\tSCORE\n"
        for values in nearest:
            nearest_img += str(values[1]) + "\t\t" + str(values[0]) + '\n'
        print(nearest_img)

        # Get nearest locations from latent semantics.
        nearest_loc = "5 nearest locations to " + str(args[3]) + " are:"
        nearest = Neighbor.knn_loc(5, vector1, vis_model, k, method, self.__database__)
        nearest_loc += "LOCATION ID\t\tSCORE"
        for values in nearest:
            nearest_loc += str(values[1]) + "\t\t\t" + str(values[0]) + "\n"
        print(nearest_loc)
        return nearest_img + '\n\n' + nearest_loc
Exemplo n.º 6
0
    def simulate(self):
        i = 0
        success = sys.maxsize
        currentState = self.startState
        t = self.startTemp

        solutions = []

        while not (success == 0) and i < self.iterate:
            j = 0
            success = 0
            while success <= self.maxSuc and j < self.maxDis:
                f1 = Heuristic(currentState).attacks()
                newState = self.neighbor.generateState()

                if Heuristic(newState).attacks() == 0:
                    if not Heuristic(newState).queensPosition() in solutions:
                        solutions.append(Heuristic(newState).queensPosition())

                if self.state_update:
                    print Heuristic(currentState).queensPosition(
                    ), " -> ", Heuristic(newState).queensPosition()

                f2 = Heuristic(newState).attacks()
                deltaF = f2 - f1
                if not t == 0.0:
                    if (deltaF <= 0) or (exp(-deltaF / t) > random.random()):
                        currentState = newState
                        success += 1

                j += 1
                self.neighbor = Neighbor(currentState)

            t = self.alpha * t
            i += 1

        file.write(Heuristic(currentState).queensPosition(),
                   self.neighbor.createBoard(),
                   url='./resource/newBoard.txt')
        print "Contagem final de sucessos : ", success
        print "Temperatura final : ", t
        print "Numero de iteracoes : ", i
        print "Posicao Inicial das ", len(
            self.startState), " rainhas : ", Heuristic(
                self.startState).queensPosition()
        print "Posicao Final das ", len(
            self.startState), " rainhas : ", Heuristic(
                currentState).queensPosition()
        print "\tNumero de rainhas atacando : ", Heuristic(
            currentState).attacks()

        print "Solucoes encontradas: "

        for solution in solutions:
            print solution

        return Heuristic(currentState).attacks()
Exemplo n.º 7
0
    def nearest_visual(self, *args):
        """
            arg[0] = locationid
            arg[1] = model (CM, CM3x3, CN, CN3x3, CSD, GLRLM, GLRLM3x3, HOG, LBP, LBP3x3, ALL)
            arg[2] = k
        """
        
        if not self.database:
            print("[ERROR] The database must be loaded for this action.")
            return
        
        if not len(args) == 3:
            print("[ERROR] Expected three arguments but got " + str(len(args)) + ".")
            print("\targ = " + str(args))
        
        # Get the first argument
        try:
            k = int(args[2])
        except:
            print("[ERROR] K Value provided is invalid.")
            print("\tk = " + str(args[2]))
            return

        # Get the model to use. We do this before the item as it is easier
        #   to differentiate valid from invalid
        model = args[1]
        if not model in self.valid_vis_models:
            print("[ERROR] Model Type value provided was invalid.")
            print("\tModel Type = " + str(args[1]))
            return

        try:
            locationid = int(args[0])
        except:
            print("[ERROR] The ID specified was not valid")
            print("\tID = " + str(locationid) + "; Model = " + model)
            return

        start = time()
        if model == 'ALL':
            this_vector = Vectorizer.visual_vector_multimodel(locationid, self.database, self.valid_vis_models)
            nearest = Neighbor.knn_visual_all(k, locationid, self.valid_vis_models, self.database, this_vector = this_vector)
        else:
            # get vector representing item associated w/ id
            this_vector = Vectorizer.visual_vector(locationid, self.database, model)
            nearest = Neighbor.knn_visual(k, locationid, model, self.database, this_vector=this_vector)
        
        contribs = Neighbor.visual_sim_contribution(this_vector, nearest.keys(), self.database,
                                                    model, k=3)
        print("The Visual Descriptor Nearest Neighbor took " + str(time() - start))

        print(str(k) + " Nearest Neighbors:")
        for i, (an_id, distance) in enumerate(nearest.items()):
            print('\t' + str(i) + ". " + str(an_id) + "; Distance = " + str(distance))
        print('Top 3 Image Pairs:')
        for i, item in enumerate(contribs):
            print('\t' + str(i) + '. ' + str(item))
Exemplo n.º 8
0
    def cost_delta(self):
        if self.cost is not None:
            return self.cost

        # find second trip to exchange gifts with and valid gifts to swap
        first_gifts_to_try = np.random.permutation(
            len(self.trips[self.first_trip]))
        for fg in first_gifts_to_try:
            self.second_trip = np.random.randint(len(self.trips))
            while len(self.trips[self.second_trip]
                      ) < 3 or self.first_trip == self.second_trip:
                self.second_trip = np.random.randint(len(self.trips))
            self.first_gift = fg
            self.second_gift = self._get_valid_swapee()
            if self.second_gift is not None:
                break

        # find insertion indexes with minimum cost
        self.first_trip_insertion_index, cost_to_insert_first = Neighbor.find_best_insertion_index(
            self.trips[self.first_trip],
            self.trips[self.second_trip][self.second_gift],
            index_to_be_removed=self.first_gift)
        self.second_trip_insertion_index, cost_to_insert_second = Neighbor.find_best_insertion_index(
            self.trips[self.second_trip],
            self.trips[self.first_trip][self.first_gift],
            index_to_be_removed=self.second_gift)

        # update temporary trips with new insertion to accurately calculate the cost of deletion
        temporary_first_trip = self.trips[self.first_trip]
        temporary_first_trip = np.insert(
            temporary_first_trip,
            self.first_trip_insertion_index,
            self.trips[self.second_trip][self.second_gift],
            axis=0)
        temporary_second_trip = self.trips[self.second_trip]
        temporary_second_trip = np.insert(
            temporary_second_trip,
            self.second_trip_insertion_index,
            self.trips[self.first_trip][self.first_gift],
            axis=0)

        # calculate (negative) cost of deletion
        cost_to_remove_first = self._cost_to_remove_gift(
            temporary_first_trip, self.first_gift
            if self.first_gift < self.first_trip_insertion_index else
            self.first_gift + 1)
        cost_to_remove_second = self._cost_to_remove_gift(
            temporary_second_trip, self.second_gift
            if self.second_gift < self.second_trip_insertion_index else
            self.second_gift + 1)

        self.cost = cost_to_insert_first + cost_to_insert_second + cost_to_remove_first + cost_to_remove_second

        return self.cost
Exemplo n.º 9
0
 def __init__(self, iterate, maxDis, maxSuc, alpha, startTemp, **kwargs):
     self.iterate = iterate
     self.maxDis = maxDis
     self.maxSuc = maxSuc
     self.alpha = alpha
     self.startState = file.read()
     self.neighbor = Neighbor(self.startState)
     self.startTemp = startTemp
     self.state_update = None
     if 'state_update' in kwargs:
         self.state_update = True
Exemplo n.º 10
0
def buildFourChildTreeTopology():
    print("\n\nBuilding four-child-tree topology")
    nodeA = Node("A")
    nodeB = Node("B")
    nodeC = Node("C")
    nodeD = Node("D")
    nodeE = Node("E", router=True)

    # Add connection to parent
    nodeA.addNeighbor(Neighbor(nodeE))
    nodeB.addNeighbor(Neighbor(nodeE))
    nodeC.addNeighbor(Neighbor(nodeE))
    nodeD.addNeighbor(Neighbor(nodeE))

    # Add reverse connection
    nodeE.addNeighbor(Neighbor(nodeA))
    nodeE.addNeighbor(Neighbor(nodeB))
    nodeE.addNeighbor(Neighbor(nodeC))
    nodeE.addNeighbor(Neighbor(nodeD))

    topologyName = "four-child-tree"
    nodes = [nodeA, nodeB, nodeC, nodeD, nodeE]
    printTopology(nodes)
    NlsrBuilder(topologyName).buildNlsrFiles(nodes)
    ComposeBuilder(topologyName, nodes).buildComposeFile()
Exemplo n.º 11
0
def buildLinearTwoTopology():
    print("\n\nBuilding linear-two topology")
    # A <--10--> B
    nodeA = Node("A")
    nodeB = Node("B")

    nodeA.addNeighbor(Neighbor(nodeB))

    nodeB.addNeighbor(Neighbor(nodeA))

    topologyName = "linear"
    nodes = [nodeA, nodeB]
    printTopology(nodes)
    NlsrBuilder(topologyName).buildNlsrFiles(nodes)
    ComposeBuilder(topologyName, nodes).buildComposeFile()
Exemplo n.º 12
0
def main():
    with open('config.json') as data:
        config = json.load(data)

    neighbors = []
    for item in config['neighbor_list']:
        neighbors.append(Neighbor(str(item)))
        ip, p2p_port = neighbors[0].getP2PConfig()

    diff = config['target']
    beneficiary = config['beneficiary']
    delay = config['delay']
    is_miner = config['mining']
    public_key = config['wallet']['public_key']
    private_key = config['wallet']['private_key']
    fee = config['fee']

    chain = minichain(diff)
    user_wallet = wallet(public_key, private_key)
    node1 = node(config['p2p_port'], config['user_port'], neighbors, chain,
                 beneficiary, user_wallet, fee, delay, is_miner)
    try:
        node1.start_node()
    except KeyboardInterrupt:
        sys.exit(1)
    except:
        print("[ERROR] UNKNOWN ERROR")
        sys.exit(1)
Exemplo n.º 13
0
    def get_optimally_sorted_trips(self, trips):
        self.log.warning("THIS DOESN'T WORK")
        self.log.info("Putting trips into optimal order")
        total_trip_count = len(trips)
        all_trips = []
        for i, trip in enumerate(trips.values()):
            self.log.debug("Processing trip {}/{} with length {}".format(
                i + 1, total_trip_count, len(trip[1])))
            sorted_trip = sorted(trip[1],
                                 key=lambda tup: tup.Weight,
                                 reverse=True)
            this_trip = []
            for gift in sorted_trip:
                if len(this_trip) == 0:
                    this_trip.append(gift)
                    continue
                gift[utils.TRIP] = len(all_trips)
                best_index, _ = Neighbor.find_best_insertion_index(
                    np.asarray(this_trip),
                    gift.values,
                    lat_index=1,
                    lon_index=2,
                    weight_index=3)
                this_trip.insert(best_index, gift)
            if this_trip:
                all_trips.append(this_trip)

        # extract gift/trip mapping
        combined_trips = np.concatenate(all_trips)[:, [utils.GIFT, utils.TRIP]]
        return pd.DataFrame(combined_trips, columns=["GiftId", "TripId"])
Exemplo n.º 14
0
    def cost_delta(self):
        if self.cost is not None:
            return self.cost

        if self.trip is None:
            self.trip = np.random.randint(len(self.trips))
            while len(self.trips[self.trip]) < 2:
                self.trip = np.random.randint(len(self.trips))

        source = self.trips[self.trip]

        if self.gift_to_move is not None or self.destination_trip is not None or self.destination_insertion_index is not None or self.cost_to_insert_in_destination is not None:
            # we should have *all* of these set
            return self.cost_to_insert_in_destination + self._cost_to_remove_gift(
                source, self.gift_to_move)

        self.gift_to_move = np.random.randint(len(source))
        self.destination_trip = self._get_valid_target_trip()

        gift = source[self.gift_to_move]

        self.destination_insertion_index, cost_to_insert = Neighbor.find_best_insertion_index(
            self.trips[self.destination_trip], gift)

        cost_to_remove = self._cost_to_remove_gift(source, self.gift_to_move)

        self.cost = cost_to_insert + cost_to_remove
        return self.cost
Exemplo n.º 15
0
 def hill_random(self):
     colision = sys.maxsize
     count = 0
     stagnate = sys.maxsize
     old_col = -1
     while colision != 0 and not stagnate == 100:
         print "Hill Random > Iteracao: ", count + 1
         print "------------------------------------------\n"
         start = time.time()
         colision = self.hill()
         end = time.time() - start
         print "\tTempo de execucao : ", end, " segundos"
         self.neighbor = Neighbor(self.startState)
         count += 1
         print "\n------------------------------------------\n"
         if not old_col == colision:
             old_col = colision
             stagnate = 0
         else:
             stagnate += 1
Exemplo n.º 16
0
def buildLinearThreeTopology():
    print("\n\nBuilding linear-three topology")
    """
     A <--10--> B <--20--> C
                |
                10
                X
    """

    nodeA = Node("A")
    nodeB = Node("B")
    nodeC = Node("C")

    # Adding my local machine to the mix
    nodeX = Node("X")
    nodeX.addNeighbor(Neighbor(nodeB))

    nodeA.addNeighbor(Neighbor(nodeB))

    nodeB.addNeighbor(Neighbor(nodeA))
    nodeB.addNeighbor(Neighbor(nodeC))
    nodeB.addNeighbor(Neighbor(nodeX))

    nodeC.addNeighbor(Neighbor(nodeB))

    topologyName = "linear-three"
    nodes = [nodeA, nodeB, nodeC, nodeX]
    printTopology(nodes)
    NlsrBuilder(topologyName).buildNlsrFiles(nodes)
    ComposeBuilder(topologyName, nodes).buildComposeFile()
Exemplo n.º 17
0
def buildDumbbellTopology():
    print("\n\nBuilding dumbbell topology")

    # Left side
    nodeA = Node("A")
    nodeB = Node("B")

    # Right side
    nodeC = Node("C")
    nodeD = Node("D")

    # Left router
    nodeE = Node("E", router=True)

    # Right router
    nodeF = Node("F", router=True)

    # Each of left nodes are connected to E
    nodeA.addNeighbor(Neighbor(nodeE))
    nodeB.addNeighbor(Neighbor(nodeE))

    # Each of right nodes are connected to F
    nodeC.addNeighbor(Neighbor(nodeF))
    nodeD.addNeighbor(Neighbor(nodeF))

    # E is connected to all left nodes and F
    nodeE.addNeighbor(Neighbor(nodeA))
    nodeE.addNeighbor(Neighbor(nodeB))
    nodeE.addNeighbor(Neighbor(nodeF))

    # F is connected to all right nodes and E
    nodeF.addNeighbor(Neighbor(nodeC))
    nodeF.addNeighbor(Neighbor(nodeD))
    nodeF.addNeighbor(Neighbor(nodeE))

    topologyName = "dumbbell"
    nodes = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF]
    printTopology(nodes)
    NlsrBuilder(topologyName).buildNlsrFiles(nodes)
    ComposeBuilder(topologyName, nodes).buildComposeFile()
Exemplo n.º 18
0
    def make_bldg_neighbor(self, name):
        bldg_powers = self.building_powers[name]

        # Create neighbor
        bldg = Neighbor()
        bldg.name = name
        bldg.maximumPower = bldg_powers[
            0]  # Remember loads have negative power [avg.kW]
        bldg.minimumPower = bldg_powers[1]  # [avg.kW]
        _log.debug("{} has minPower of {} and maxPower of {}".format(
            bldg.name, bldg.minimumPower, bldg.maximumPower))

        # Create neighbor model
        bldg_model = NeighborModel()
        bldg_model.name = name + '_Model'
        bldg_model.location = self.name
        bldg_model.convergenceThreshold = 0.02
        bldg_model.friend = True
        bldg_model.transactive = True
        bldg_model.costParameters = [0, 0, 0]

        # This is different building to building
        bldg_model.defaultPower = bldg.minimumPower / 2  # bldg_powers[2]  # [avg.kW]
        bldg_model.defaultVertices = [
            Vertex(float("inf"), 0, bldg_model.defaultPower, True)
        ]

        # Cross reference object & model
        bldg.model = bldg_model
        bldg_model.object = bldg

        return bldg
Exemplo n.º 19
0
    def cost_delta(self):
        if self.cost is not None:
            return self.cost

        trip = self.trips[self.trip]
        gift = trip[self.gift_index]
        cost_to_remove = self._cost_to_remove_gift(trip, self.gift_index)

        trip_without_gift = np.delete(trip, self.gift_index, axis=0)
        self.new_index, cost_to_insert = Neighbor.find_best_insertion_index(
            trip_without_gift, gift, index_to_be_removed=self.gift_index + 1)

        self.cost = cost_to_insert + cost_to_remove
        return self.cost
Exemplo n.º 20
0
    def _parse_config_file(self, config_file):
        ''' Config file has general configuration, such as href prefix.
            Config file has information for Switches
             - Name
             - Connection info 
             - List of reserved bridge numbers
             - Neighbors
        '''
        with open(config_file) as data_file:
            data = json.load(data_file)

        self.base_url = data['adaptor-href-base']

        for switch in data['switches']:
            switch_name = switch['name']
            connection_info = switch['connection-info']
            reserved_bridges = switch['reserved-bridges']
            neighbors = switch['neighbors']

            # Determine the correct href for the switch we're creating
            switch_href = self.base_url + "switches/" + switch_name

            # Create Switch object
            switch = Switch(switch_name, switch_href, self.no_switch)

            # Create the ConnectionInfo object and add it to the switch
            cxn_info_object = ConnectionInfo(connection_info['address'],
                                             connection_info['rest_key'])
            switch.set_connection_info(cxn_info_object)

            # Add list of reserved bridges
            switch.set_reserved_bridges(reserved_bridges)

            # Create all the neighbors and add them to the switch object
            for neighbor in neighbors:
                neighbor_name = neighbor['name']
                neighbor_type = neighbor['type']
                neighbor_physport = neighbor['port']
                neighbor_vlans = neighbor['vlans']
                neighbor_href = switch_href + "/neighbors/" + neighbor_name
                neighbor_object = Neighbor(neighbor_name, neighbor_href,
                                           neighbor_vlans, neighbor_physport,
                                           neighbor_type)

                switch.add_neighbor(neighbor_object)
            # Save off switch
            self.switches[switch_name] = switch
Exemplo n.º 21
0
    def task2(self, *args):
        """
        Command:\ttask2 <term space> <k> <method> <j> <id>
        Description:\tIdentifies the top k latent semantics uses an id in the term \
        space to identify the most related j ids.
        Arguments:
        \tTerm Space - What text description type to pull. (user, photo, poi).
        \tK - Number of latent semantics to return.
        \tMethod - The decomposition method to use. (PCA, SVD, LDA).
        \tJ - Number of nearest terms to find.
        \tID - The id to find nearest neighbors to using the latent semantics.
        """
        if len(args) < 5:
            print("[ERROR] Not enough args were provided. Expected 5 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if len(args) > 5:
            print("[ERROR] Too many arguments were provided. Expected 5 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if not self.__database__:
            print("[ERROR] The Database must be loaded before this can be run.")
            print("\tCommand: load <filepath>")
            return

        try:
            term_space = args[0]
            k = int(args[1])
            method = args[2]
            j = int(args[3])
            anid = args[4]
            if term_space == 'photo' or term_space == 'poi':
                anid = int(anid)
        except:
            print("[ERROR] One or more arguments could not be parsed: " + str(args))

        reduced_table, latent_semantics = Decompose.decompose_text(term_space, k, method.lower(), self.__database__)
        ls_str = self.__latent_semantic_string__(latent_semantics)
        print(ls_str)
        vector = reduced_table.loc[anid]
        neighbors = Neighbor.knn(j, vector, reduced_table)
        neighbors_str = ""
        neighbors_str += "NEIGHBORS to " + str(anid)
        for i, neighbor in enumerate(neighbors):
            neighbors_str += f"{i}: ID = {neighbor.id}, DIST = {neighbor.dist}"
        print(neighbors_str)
        return ls_str + '\n' + neighbors_str
Exemplo n.º 22
0
    def compute(self, system: System, binning: Binning,
                neighbor: Neighbor) -> None:
        neigh_list: NeighList2D = neighbor.get_neigh_list()
        self.num_neighs_view: pk.View1D = neigh_list.num_neighs
        self.neighs_view: pk.View2D = neigh_list.neighs

        self.N_local = system.N_local
        self.x = system.x
        self.f = system.f
        self.f_a = system.f
        self.type = system.type
        self.id = system.id

        self.parallel_for = True
        pk.execute(pk.ExecutionSpace.Default, self)

        self.step += 1
Exemplo n.º 23
0
    def task4(self, *args):
        """
        Command:\ttask4 <locationid> <vis model> <k> <method>
        Description:\tIdentifies the top k latent semantics for the given location \
        and lists the five most related locations.
        Arguments:
        \tLocation Id - A valid location id. (1 - 30)
        \tVisual Space - What visual description model to pull. ('CM', 'CM3x3', 'CN', \
        'CN3x3', 'CSD', 'GLRLM', 'GLRLM3x3', 'HOG', 'LBP', 'LBP3x3').
        \tK - Number of latent semantics to identify.
        \tMethod - The decomposition method to use. (PCA, SVD, LDA).
        """
        if len(args) < 4:
            print("[ERROR] Not enough args were provided. Expected 4 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if len(args) > 4:
            print("[ERROR] Too many arguments were provided. Expected 4 but got " + str(len(args)))
            print("\targs = " + str(args))
            return
        if not self.__database__:
            print("[ERROR] The Database must be loaded before this can be run.")
            print("\tCommand: load <filepath>")
            return

        try:
            locationid = int(args[0])
            vis_model = args[1]
            k = int(args[2])
            method = args[3]
        except:
            print("[ERROR] One or more arguments could not be parsed: " + str(args))

        # Get latent semantics.
        matrix = Decompose.decompose_loc_vis(vis_model, k, method, locationid, self.__database__)

        # Get nearest 5 locations from latent semantics. Neighbor.knn may be useful for you.

        nearest = Neighbor.knn_vd(5, matrix, vis_model, k, method, locationid, self.__database__)
        nearest_loc = "5 nearest locations to " + str(args[0]) + " are:\n"
        nearest_loc += "LOCATION ID\t\tSCORE\n"
        for values in nearest:
            nearest_loc += str(values[1]) + "\t\t\t" + str(values[0]) + '\n'
        print(nearest_loc)
        return nearest_loc
Exemplo n.º 24
0
  def cost_delta(self):
    if self.cost is not None:
      return self.cost

    self.trip_to_merge, self.trip_assignments_for_gifts = self._find_trip_to_merge()

    if self.trip_to_merge is None:
      return 0

    trip = self.trips[self.trip_to_merge]
    self.gift_insertions = []
    cost_of_insertions = 0

    for trip_index, gift in self.trip_assignments_for_gifts.items():
      index_in_trip, cost = Neighbor.find_best_insertion_index(self.trips[trip_index], gift)
      self.gift_insertions.append((gift, trip_index, index_in_trip))
      cost_of_insertions += cost

    self.cost = cost_of_insertions - utils.weighted_trip_length(trip[:, utils.LOCATION], trip[:, utils.WEIGHT])
    return self.cost
Exemplo n.º 25
0
    def main(self, L=2, k=3, imageId=5175916261, vectors=[], t=5, database=()):
        # imageId = imageId[0]
        # imageId = int(imageId[1:-1])
        image_dict = pd.DataFrame(database.get_vis_table())
        image_dict = image_dict.T
        image_dict = image_dict.to_dict('list')

        index_structure, vectors = self.get_index_structure(L, k, image_dict)
        # print(index_structure['h(0, 0)'])

        # get all the imageIds which are in the same bucket as the given imageId
        imageId_set = set(image_dict.keys())
        # print(imageId_set)
        num_total_images = 0
        for i in range(L):
            for j in range(k):
                temp_imageId = set()
                for bucket in index_structure['h(' + str(i) + ', ' + str(j) +
                                              ')']:
                    if imageId in index_structure['h(' + str(i) + ', ' +
                                                  str(j) + ')'][str(bucket)]:
                        temp_imageId = temp_imageId.intersection(
                            set(index_structure['h(' + str(i) + ', ' + str(j) +
                                                ')'][str(bucket)]))
                    num_total_images = num_total_images + len(temp_imageId)
                imageId_set = imageId_set.union(temp_imageId)
        # print(imageId_set)

        # calculate similarity and get t nearest images

        nearest, num_comparisons = Neighbor.knn_visual_LSH(
            t, imageId, database, list(imageId_set))
        print("Number of non-unique images considered\t: " +
              str(num_total_images))
        print("Number of unique images compared\t: " + str(num_comparisons))
        for image in nearest:
            print(image)

        return [int(n.id) for n in nearest]
    def particleCalculations(self):
        for i in range(len(self.particles)):
            # Density calculations
            densityFar = densityNear = 0
            targetParticle = self.particles[i]
            targetParticle.rho = targetParticle.rhoNear = 0
            for j in range(i + 1, len(self.particles)):
                neighborParticle = self.particles[j]
                distanceVector = neighborParticle.pos - targetParticle.pos
                distance = np.linalg.norm(distanceVector)
                if distance**2 < self.supportRadius**2:
                    weightedDistance = 1 - distance / self.supportRadius
                    densityFar += weightedDistance**2
                    densityNear += weightedDistance**3
                    neighborParticle.rho += weightedDistance**2
                    neighborParticle.rhoNear += weightedDistance**3
                    newNeighbor = Neighbor(neighborParticle, weightedDistance)
                    targetParticle.neighbors.append(newNeighbor)
            targetParticle.rho += densityFar
            targetParticle.rhoNear += densityNear

            # Pressure calculations
            targetParticle.press = self.k * (targetParticle.rho -
                                             self.restDensity)
            targetParticle.pressNear = self.kNear * targetParticle.rhoNear
            dX = np.array([0, 0])
            for neighborEntry in targetParticle.neighbors:
                neighborParticle = neighborEntry.particle
                distanceVector = neighborParticle.pos - targetParticle.pos
                dm = (targetParticle.pressure + neighborParticle.pressure
                      ) * neighborEntry.weightedDistance + (
                          targetParticle.pressNear + neighborParticle.
                          pressureNear) * (neighborEntry.weightedDistance**2)
                directionOfForce = (distanceVector /
                                    np.linalg.norm(distanceVector)) * dm
                dX = dX + directionOfForce
                neighborParticle.force = neighborParticle.force + directionOfForce
            targetParticle.force = targetParticle.force - dX
Exemplo n.º 27
0
    def make_campus(self):
        # Campus object
        campus = Neighbor()
        campus.name = 'PNNL_Campus'
        campus.description = 'PNNL_Campus'
        campus.maximumPower = 0.0  # Remember loads have negative power [avg.kW]
        campus.minimumPower = -20000  # [avg.kW]

        # Campus model
        campus_model = NeighborModel()
        campus_model.name = 'PNNL_Campus_Model'
        campus_model.location = self.name
        campus_model.defaultPower = -10000  # [avg.kW]
        campus_model.defaultVertices = [Vertex(0.045, 0.0, -10000.0)]
        #campus_model.demandThreshold = 0.8 * campus.maximumPower
        campus_model.transactive = True

        # Cross-reference object & model
        campus_model.object = campus
        campus.model = campus_model

        return campus
Exemplo n.º 28
0
    def make_supplier(self):
        # Add supplier
        supplier = Neighbor()
        supplier.name = 'BPA'
        supplier.description = 'The Bonneville Power Administration as electricity supplier to the City of Richland, WA'
        supplier.lossFactor = self.supplier_loss_factor
        supplier.maximumPower = 200800  # [avg.kW, twice the average COR load]
        supplier.minimumPower = 0.0  # [avg.kW, will not export]

        # Add supplier model
        supplierModel = BulkSupplier_dc()
        supplierModel.name = 'BPAModel'
        #supplierModel.demandThreshold = 0.75 * supplier.maximumPower
        supplierModel.converged = False  # Dynamically assigned
        supplierModel.convergenceThreshold = 0  # Not yet implemented
        supplierModel.effectiveImpedance = 0.0  # Not yet implemented
        supplierModel.friend = False  # Separate business entity from COR
        supplierModel.transactive = False  # Not a transactive neighbor

        # Add vertices
        # The first default vertex is, for now, based on the flat COR rate to
        # PNNL. The second vertex includes 2# losses at a maximum power that
        # is twice the average electric load for COR. This is helpful to
        # ensure that a unique price, power point will be found. In this
        # model the recipient pays the cost of energy losses.
        # The first vertex is based on BPA Jan HLH rate at zero power
        # importation.
        d1 = Vertex(0, 0, 0)  # create first default vertex
        d1.marginalPrice = 0.04196  # HLH BPA rate Jan 2018 [$/kWh]
        d1.cost = 2000.0  # Const. price shift to COR customer rate [$/h]
        d1.power = 0.0  # [avg.kW]
        # The second default vertex represents imported and lost power at a power
        # value presumed to be the maximum deliverable power from BPA to COR.
        d2 = Vertex(0, 0, 0)  # create second default vertex
        # COR pays for all sent power but receives an amount reduced by
        # losses. This creates a quadratic term in the production cost and
        # a slope to the marginal price curve.
        d2.marginalPrice = d1.marginalPrice / (1 - supplier.lossFactor
                                               )  # [$/kWh]
        # From the perspective of COR, it receives the power sent by BPA,
        # less losses.
        d2.power = (1 -
                    supplier.lossFactor) * supplier.maximumPower  # [avg.kW]
        # The production costs can be estimated by integrating the
        # marginal-price curve.
        d2.cost = d1.cost + d2.power * (d1.marginalPrice + 0.5 *
                                        (d2.marginalPrice - d1.marginalPrice)
                                        )  # [$/h]
        supplierModel.defaultVertices = [d1, d2]

        #   COST PARAMTERS
        #     A constant cost parameter is being used here to account for the
        #     difference between wholesale BPA rates to COR and COR distribution
        #     rates to customers like PNNL. A constant of $2,000/h steps the rates
        #     from about 0.04 $/kWh to about 0.06 $/kWh. This may be refined later.
        #     IMPORTANT: This shift has no affect on marginal pricing.
        supplierModel.costParameters[0] = 2000.0  # [$/h]

        # Cross-reference object & model
        supplierModel.object = supplier
        supplier.model = supplierModel

        # Meter
        bpaElectricityMeter = MeterPoint()  # Instantiate an electricity meter
        bpaElectricityMeter.name = 'BpaElectricityMeter'
        bpaElectricityMeter.description = 'BPA electricity to COR'
        bpaElectricityMeter.measurementType = MeasurementType.PowerReal
        bpaElectricityMeter.measurementUnit = MeasurementUnit.kWh
        supplierModel.meterPoints = [bpaElectricityMeter]

        return supplier
Exemplo n.º 29
0
class SimulatedAnnealing(object):
    # maxDisc -> Numero maximo de perturbacoes na temperatura
    # maxSuc -> Numero maximo de sucessos por iteracao
    # alpha -> fator de reducao da temperatura
    def __init__(self, iterate, maxDis, maxSuc, alpha, startTemp, **kwargs):
        self.iterate = iterate
        self.maxDis = maxDis
        self.maxSuc = maxSuc
        self.alpha = alpha
        self.startState = file.read()
        self.neighbor = Neighbor(self.startState)
        self.startTemp = startTemp
        self.state_update = None
        if 'state_update' in kwargs:
            self.state_update = True

    def simulate(self):
        i = 0
        success = sys.maxsize
        currentState = self.startState
        t = self.startTemp

        solutions = []

        while not (success == 0) and i < self.iterate:
            j = 0
            success = 0
            while success <= self.maxSuc and j < self.maxDis:
                f1 = Heuristic(currentState).attacks()
                newState = self.neighbor.generateState()

                if Heuristic(newState).attacks() == 0:
                    if not Heuristic(newState).queensPosition() in solutions:
                        solutions.append(Heuristic(newState).queensPosition())

                if self.state_update:
                    print Heuristic(currentState).queensPosition(
                    ), " -> ", Heuristic(newState).queensPosition()

                f2 = Heuristic(newState).attacks()
                deltaF = f2 - f1
                if not t == 0.0:
                    if (deltaF <= 0) or (exp(-deltaF / t) > random.random()):
                        currentState = newState
                        success += 1

                j += 1
                self.neighbor = Neighbor(currentState)

            t = self.alpha * t
            i += 1

        file.write(Heuristic(currentState).queensPosition(),
                   self.neighbor.createBoard(),
                   url='./resource/newBoard.txt')
        print "Contagem final de sucessos : ", success
        print "Temperatura final : ", t
        print "Numero de iteracoes : ", i
        print "Posicao Inicial das ", len(
            self.startState), " rainhas : ", Heuristic(
                self.startState).queensPosition()
        print "Posicao Final das ", len(
            self.startState), " rainhas : ", Heuristic(
                currentState).queensPosition()
        print "\tNumero de rainhas atacando : ", Heuristic(
            currentState).attacks()

        print "Solucoes encontradas: "

        for solution in solutions:
            print solution

        return Heuristic(currentState).attacks()
Exemplo n.º 30
0
    def init_objects(self):
        # Add meter
        # meter = MeterPoint()
        # meter.name = 'BuildingElectricMeter'
        # meter.measurementType = MeasurementType.PowerReal
        # meter.measurementUnit = MeasurementUnit.kWh
        # self.meterPoints.append(meter)

        # Add weather forecast service
        weather_service = TemperatureForecastModel(self.config_path, self)
        self.informationServiceModels.append(weather_service)

        # # Add inelastive asset
        # inelastive_load = LocalAsset()
        # inelastive_load.name = 'InelasticBldgLoad'
        # inelastive_load.maximumPower = 0  # Remember that a load is a negative power [kW]
        # inelastive_load.minimumPower = -200
        #
        # # Add inelastive asset model
        # inelastive_load_model = LocalAssetModel()
        # inelastive_load_model.name = 'InelasticBuildingModel'
        # inelastive_load_model.defaultPower = -100  # [kW]
        # inelastive_load_model.defaultVertices = [Vertex(float("inf"), 0, -100, True)]
        #
        # # Cross-reference asset & asset model
        # inelastive_load_model.object = inelastive_load
        # inelastive_load.model = inelastive_load_model

        # Add elastive asset
        elastive_load = LocalAsset()
        elastive_load.name = 'TccLoad'
        elastive_load.maximumPower = 0  # Remember that a load is a negative power [kW]
        elastive_load.minimumPower = -self.max_deliver_capacity

        # Add inelastive asset model
        # self.elastive_load_model = LocalAssetModel()
        self.elastive_load_model = TccModel()
        self.elastive_load_model.name = 'TccModel'
        self.elastive_load_model.defaultPower = -0.5*self.max_deliver_capacity  # [kW]
        self.elastive_load_model.defaultVertices = [Vertex(0.055, 0, -self.elastive_load_model.defaultPower, True),
                                                    Vertex(0.06, 0, -self.elastive_load_model.defaultPower/2, True)]

        # Cross-reference asset & asset model
        self.elastive_load_model.object = elastive_load
        elastive_load.model = self.elastive_load_model

        # Add inelastive and elastive loads as building' assets
        self.localAssets.extend([elastive_load])

        # Add Market
        market = Market()
        market.name = 'dayAhead'
        market.commitment = False
        market.converged = False
        market.defaultPrice = 0.0428  # [$/kWh]
        market.dualityGapThreshold = self.duality_gap_threshold  # [0.02 = 2#]
        market.initialMarketState = MarketState.Inactive
        market.marketOrder = 1  # This is first and only market
        market.intervalsToClear = 1  # Only one interval at a time
        market.futureHorizon = timedelta(hours=24)  # Projects 24 hourly future intervals
        market.intervalDuration = timedelta(hours=1)  # [h] Intervals are 1 h long
        market.marketClearingInterval = timedelta(hours=1)  # [h]
        market.marketClearingTime = Timer.get_cur_time().replace(hour=0,
                                                                 minute=0,
                                                                 second=0,
                                                                 microsecond=0)  # Aligns with top of hour
        market.nextMarketClearingTime = market.marketClearingTime + timedelta(hours=1)
        self.markets.append(market)

        # Campus object
        campus = Neighbor()
        campus.name = 'PNNL_Campus'
        campus.description = 'PNNL_Campus'
        campus.maximumPower = self.max_deliver_capacity
        campus.minimumPower = 0.  # [avg.kW]
        campus.lossFactor = self.campus_loss_factor

        # Campus model
        campus_model = NeighborModel()
        campus_model.name = 'PNNL_Campus_Model'
        campus_model.location = self.name
        campus_model.defaultVertices = [Vertex(0.045, 25, 0, True), Vertex(0.048, 0, self.max_deliver_capacity, True)]
        campus_model.demand_threshold_coef = self.demand_threshold_coef
        # campus_model.demandThreshold = self.demand_threshold_coef * self.monthly_peak_power
        campus_model.demandThreshold = self.monthly_peak_power
        campus_model.transactive = True
        campus_model.inject(self, system_loss_topic=self.system_loss_topic)

        # Avg building meter
        building_meter = MeterPoint()
        building_meter.name = self.name + ' ElectricMeter'
        building_meter.measurementType = MeasurementType.AverageDemandkW
        building_meter.measurementUnit = MeasurementUnit.kWh
        campus_model.meterPoints.append(building_meter)

        # Cross-reference object & model
        campus_model.object = campus
        campus.model = campus_model
        self.campus = campus

        # Add campus as building's neighbor
        self.neighbors.append(campus)