Пример #1
0
def run_net():
    t = 0
    graph.time_update()
    graph.input_new_point(t)
    l_num = len(graph.left)
    r_num = len(graph.right)
    global all_reward
    reward = 0
    l, t = 1, 1
    l = lmin
    while t < H:
        graph.time_update()
        graph.input_new_point(t)
        state = np.array([
            l_num, r_num,
            match.fake_matching(graph.left, graph.right, graph.edge), l
        ])
        action = RL.choose_action(state)
        if action == 0:
            l_num = len(graph.left)
            r_num = len(graph.right)
            reward = 0
            l = l + 1
            state_ = np.array([
                l_num, r_num,
                match.fake_matching(graph.left, graph.right, graph.edge), l
            ])
            RL.store_transition(state, action, reward, state_)
            t = t + 1
        else:
            reward = match.matching(graph.left, graph.right, graph.edge)
            print("r", reward)
            l_num = len(graph.left)
            r_num = len(graph.right)
            l = 0
            state_ = np.array([
                l_num, r_num,
                match.fake_matching(graph.left, graph.right, graph.edge), l
            ])
            RL.store_transition(state, action, reward, state_)
            t = t + 1
        if (t > 200) and (t % 5 == 0):
            print("sss")
            RL.learn()
        all_reward = reward + all_reward
        print("得分", all_reward, "轮数", t)
Пример #2
0
module_path = os.path.dirname(__file__)
filename = "16_15_1.5.txt"
time_number = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
logfile = str(module_path + "/greedylog/" + "greedylog" + filename)

file_handle = open(logfile, mode="w")
graph = Graph(filename)
u = 0
i = 0
all_reward = 0
record = 0
record_old = 0
while True:

    graph.time_update()
    graph.input_new_point(i)
    l = graph.existing_time()

    #if i%3==0:
    u = matching(graph.left, graph.right, graph.edge)
    all_reward = u + all_reward
    #print_state(graph.left,graph.right,graph.edge,i)
    if i % 1000 == 0:
        record = all_reward
        file_handle.write("得分" + str(all_reward) + " " + "轮数" + str(i) + "\n")
        file_handle.write("比上一千轮增加:" + str(record - record_old) + "\n")
        file_handle.write("\n")
        record_old = record
    print("得分", all_reward, "轮数", i)
    i = i + 1
Пример #3
0
                    help="Path to the file that you want to verify.")
parser.add_argument("--temp_dir", type=str, default="./feature/",
                    help="Path to the directory containing templates.")
parser.add_argument("--thres", type=float, default=0.38,
                    help="Threshold for matching.")
args = parser.parse_args()

if __name__ == '__main__':
    # Extract feature
    start = time()
    print('>>> Start verifying {}\n'.format(args.file))

    # Matching
    ft_load = loadmat(args.file)
    template_extr, mask_extr = ft_load['template'], ft_load['mask']
    result = matching(template_extr, mask_extr, args.temp_dir, args.thres)

    if result == -1:
        print('>>> No registered sample.')

    elif result == 0:
        print('>>> No sample matched.')

    else:
        print('>>> {} samples matched (descending reliability):'.format(len(result)))
        for res in result:
            print("\t", res)

    # Time measure
    end = time()
    print('\n>>> Verification time: {} [s]\n'.format(end - start))
Пример #4
0
def trade(numTxCentral, numTxNodes, numTxInitiate, price, battery,
          availableFlex, deviation, numHouses):
    # This is the main function that does the trading.

    wholesalePrice = 470
    flexFlag = -1
    transactions = [[] for y in range(3)]
    upPrice = [[0 for x in range(numHouses)] for y in range(2)]
    upPrice[0] = [x for x in range(numHouses)]
    upPrice[1] = price[0]
    downPrice = [[0 for x in range(numHouses)] for y in range(2)]
    downPrice[0] = [x for x in range(numHouses)]
    downPrice[1] = price[1]
    upAvailableFlex = [[0 for x in range(numHouses)] for y in range(2)]
    upAvailableFlex[0] = [x for x in range(numHouses)]
    upAvailableFlex[1] = availableFlex[
        0]  # This is used if the system needs less consumption
    downAvailableFlex = [[0 for x in range(numHouses)] for y in range(2)]
    downAvailableFlex[0] = [x for x in range(numHouses)]
    downAvailableFlex[1] = availableFlex[
        1]  # This is used if system needs more consumption
    demand = [[] for y in range(2)]
    supply = [[] for y in range(2)]
    updateCost = []
    transferCost = ''
    nodeCost = 0
    centralCost = 0

    # Some houses have flexibility, and no deviation. Other houses have deviation, but no flex. Lets do 50/50
    # The for loop under is information gathering to the blockchain
    for i in range(0, numHouses):

        if (i % 2 == 0):
            updateCost.append(RealTime.transact().setRealTimeNodePrice(
                i, upPrice[1][i], downPrice[1][i]))
            updateCost.append(RealTime.transact().setRealTimeNodeBattery(
                i, upAvailableFlex[1][i], downAvailableFlex[1][i], 0))
            numTxNodes = numTxNodes + 2
        else:
            updateCost.append(RealTime.transact().setRealTimeNodePrice(
                i, 0, 0))
            updateCost.append(RealTime.transact().setRealTimeNodeBattery(
                i, 0, 0, deviation[i]))
            numTxNodes = numTxNodes + 2

    # The for loop below is the central node fetching all information from the blockchain
    for i in range(0, numHouses):
        h = RealTime.call().getRealTimeNode(i)
        if (h[5] < 0):
            demand[0].append(i)
            demand[1].append(-h[5])
        elif (h[5] > 0):
            supply[0].append(i)
            supply[1].append(h[5])

    # Here, the match.py script is used to match supply and demand
    flexFlag, transactions, restEnergy = match.matching(
        flexFlag, transactions, demand, supply)

    numTransactionsFirstRound = len(transactions[0])
    if (
            sum(restEnergy[1]) == 0
    ):  # This means that supply and demand cancel each other, and no more trading is necessary
        flexFlag = 2
        marketPrice = wholesalePrice

    sortedPrice = [[0 for x in range(numHouses)] for y in range(2)]
    copySortedPrice = [[0 for x in range(numHouses)] for y in range(2)]

    # If supply and demand do not cancel each other, we must utilise the batteries to cancel the deviations
    i = 0
    if (flexFlag == 0):
        demandFlex = copy.deepcopy(upAvailableFlex)
        supplyFlex = restEnergy
        sortedPrice = copy.deepcopy(upPrice)
        copySortedPrice = copy.deepcopy(upPrice)
        sortedPrice[1] = sorted(sortedPrice[1])
        for i in range(len(upPrice[1])):
            sortedPrice[0][i] = copySortedPrice[0][copySortedPrice[1].index(
                sortedPrice[1][i])]
            demandFlex[1][i] = upAvailableFlex[1][copySortedPrice[1].index(
                sortedPrice[1][i])]
            demandFlex[0][i] = sortedPrice[0][i]
            copySortedPrice[1][copySortedPrice[1].index(
                sortedPrice[1][i])] = -1
    else:
        demandFlex = restEnergy
        supplyFlex = copy.deepcopy(downAvailableFlex)
        sortedPrice = copy.deepcopy(downPrice)
        copySortedPrice = copy.deepcopy(downPrice)
        sortedPrice[1] = sorted(sortedPrice[1], reverse=True)
        for i in range(len(downPrice[1])):
            sortedPrice[0][i] = copySortedPrice[0][copySortedPrice[1].index(
                sortedPrice[1][i])]
            supplyFlex[1][i] = downAvailableFlex[1][copySortedPrice[1].index(
                sortedPrice[1][i])]
            supplyFlex[0][i] = sortedPrice[0][i]
            copySortedPrice[1][copySortedPrice[1].index(
                sortedPrice[1][i])] = -1

    firstFlexFlag = copy.deepcopy(flexFlag)

    # The line below matches the batteries with the rest energy
    flexFlag, transactions, restEnergy = match.matching(
        flexFlag, transactions, demandFlex, supplyFlex)

    # The part below finds the market price
    if (firstFlexFlag == 0):
        if (flexFlag == 0):
            wholesale = sum(restEnergy[1])
            marketPrice = wholesalePrice
            lastBattery = -1
            print("The system are still ", wholesale,
                  " kWh over the bid amount")
        else:
            lastBattery = transactions[1][len(transactions[0]) - 1]
            marketPrice = sortedPrice[1][sortedPrice[0].index(lastBattery)]
            print('The systems batteries can cover the deviations')
    elif (firstFlexFlag == 1):
        if (flexFlag == 0):
            lastBattery = transactions[0][len(transactions[0]) - 1]
            marketPrice = sortedPrice[1][sortedPrice[0].index(lastBattery)]
            print('The systems batteries can cover the deviations')
        else:
            wholesale = sum(restEnergy[1])
            marketPrice = wholesalePrice
            lastBattery = -1
            print("The system are still ", wholesale,
                  " kWh under the bid amount")

    ################################################################################
    ### The calculation is done, and the transactions are performed in blockchain ##
    ################################################################################
    numTxCentral = len(transactions[0])
    print(len(transactions[0]), len(transactions[1]), len(transactions[2]))
    transferCost = RealTime.transact().checkAndTransactList(
        firstFlexFlag, sortedPrice[0], sortedPrice[1], transactions[0],
        transactions[1], transactions[2], marketPrice, FlexCoin.address)
    centralCost = web3.eth.getTransactionReceipt(transferCost).gasUsed

    if (firstFlexFlag == 0):
        for i in range(numTransactionsFirstRound, len(transactions[0])):
            battery[transactions[1]
                    [i]] = battery[transactions[1][i]] - transactions[2][i]
    if (firstFlexFlag == 1):
        for i in range(numTransactionsFirstRound, len(transactions[0])):
            battery[transactions[0]
                    [i]] = battery[transactions[0][i]] + transactions[2][i]
    for i in range(0, len(updateCost)):
        nodeCost = web3.eth.getTransactionReceipt(
            updateCost[i]).gasUsed + nodeCost
    #for i in range(0, len(createNodeCost)):
    #    nodeCost = web3.eth.getTransactionReceipt(createNodeCost[i]).gasUsed + nodeCost

    return (numTxCentral, numTxNodes, numTxInitiate, battery, marketPrice,
            nodeCost, centralCost)