import mat4py
from utils import ora
from eq_db.distributed_bids import make_distributed_bids
from decimal import *
from operator import itemgetter
getcontext().prec = 28
getcontext().rounding = ROUND_HALF_UP


def f(x):
    return Decimal(x).quantize(Decimal('0.0000001')).quantize(
        Decimal('0.000001'))


con = ora.OracleConnection()

demands_query = '''select hour_num, id, interval_num, node_id, volume, price, is_accepted
                   from tsdb2.wh_eq_db_demands partition (&tsid)
                   order by node_id, hour_num, id, interval_num'''

supplies_query = '''select hour_num, node_id, p_max, p_min, cost, gen_id, interval_num, integral_constr_id, tariff, forced_sm
                    from tsdb2.wh_eq_db_supplies partition (&tsid)
                    order by node_id, hour_num, gen_id, interval_num'''

impexbids_query = '''select HOUR_NUM, DIRECTION, INTERVAL_NUM, VOLUME, PRICE, SECTION_NUMBER, IS_ACCEPTING
                     from table(tsdb2.wh_view.eq_db_impexbids(replace('&tsid','TS_','')))
                     order by section_number, direction, hour_num'''

dates = con.exec_script('''select trade_session_id, target_date--, note
                           from tsdb2.trade_Session
                           where trunc(target_date) = to_date('20062015', 'ddmmyyyy')
def make_dpgs(tsid, tdate=''):
    # tsid = 221348901
    # tdate = '01.01.1970'

    if tdate:
        print('preparing dpgs for', tdate)
    start_time = time.time()

    cs = sql_scripts.ConsumersScript()
    gs = sql_scripts.GeneratorsScript()
    bs = sql_scripts.BidsScript()
    ks = sql_scripts.KcNodeScript()
    kr = sql_scripts.KgRgeScript()
    rgs = sql_scripts.RastrGenScript()
    rs = sql_scripts.RastrConsumerScript()
    ls = sql_scripts.RastrLoadScript()

    con = ora.OracleConnection()

    DpgDemand.set_max_bid_prices(
        con.exec_script(sql_scripts.MaxBidPriceScript().get_query(),
                        {'tsid': tsid}))
    DpgSupply.set_wsum_data(
        con.exec_script(sql_scripts.WsumgenScript().get_query(),
                        {'tsid': tsid}))

    dpgs = []
    dpgs_index = {}
    consumers_index = {}

    @ora.process_cursor(con, cs, {'tsid': tsid})
    def process_consumers(new_row, dpg_list, dpg_list_index,
                          consumer_list_index):
        dpg_list_index[new_row[cs['dpg_id']]] = len(dpg_list)
        consumer_list_index[new_row[cs['consumer_code']]] = len(dpg_list)
        dpg_list.append(DpgDemand(new_row))

    @ora.process_cursor(con, gs, {'tsid': tsid})
    def process_generators(new_row, dpg_list, dpg_list_index):
        dpg_list_index[new_row[gs['gtp_id']]] = len(dpg_list)
        dpg_list.append(DpgSupply(new_row))

    @ora.process_cursor(con, rgs, {'tsid': tsid})
    def process_generator_data(new_row, dpg_list, dpg_list_index):
        dpg_id = new_row[rgs['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_generator_data(new_row)

    @ora.process_cursor(con, bs, {'tsid': tsid})
    def process_bids(new_row, dpg_list, dpg_list_index):
        dpg_id = new_row[bs['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_bid_data(new_row)

    @ora.process_cursor(con, ks, {'tsid': tsid})
    def process_k_distr(new_row, dpg_list, dpg_list_index):
        dpg_id = new_row[ks['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_k_distr_data(new_row)
        else:
            print('!!consumer dpg %i not in list!!' % dpg_id)

    @ora.process_cursor(con, kr, {'tsid': tsid})
    def process_k_rge_distr(new_row, dpg_list, dpg_list_index):
        dpg_id = new_row[kr['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_k_distr_data(new_row)
        # else:
        #     print('!!supplier dpg %i not in list!!' % dpg_id)

    @ora.process_cursor(con, rs, {'tsid': tsid})
    def process_rastr_consumer(new_row, dpg_list, consumer_list_index):
        consumer_code = new_row[rs['consumer_code']]
        if consumer_code in consumer_list_index.keys():
            dpg_list[consumer_list_index[consumer_code]].add_consumer_data(
                new_row)

    @ora.process_cursor(con, ls, {'tsid': tsid})
    def process_rastr_load(new_row, dpg_list, consumer_list_index):
        consumer_code = new_row[ls['consumer_code']]
        if consumer_code in consumer_list_index.keys():
            dpg_list[consumer_list_index[consumer_code]].add_load_data(new_row)

    print('getting consumer DPGs')
    process_consumers(dpgs, dpgs_index, consumers_index)
    process_rastr_consumer(dpgs, consumers_index)
    process_rastr_load(dpgs, consumers_index)

    print('getting supplier DPGs')
    process_generators(dpgs, dpgs_index)

    # R = random.randint(0, len(dpgs))
    # DPGCODE = dpgs[R].code
    # DPGID = dpgs[R].id

    print('getting generator information')
    process_generator_data(dpgs, dpgs_index)

    nodes, nodes_index = make_eq_db_nodes(tsid, tdate)

    print('getting bid information')
    process_bids(dpgs, dpgs_index)
    #
    print('getting k_distr information')
    # process_k_distr(dpgs, dpgs_index)
    process_k_rge_distr(dpgs, dpgs_index)

    print("distributing consumer's bids")
    for i, d in enumerate(dpgs):
        d.finalize_data()
        d.distribute_bid()
        d.prepare_generator_data()
        if isinstance(d, DpgSupply):
            d.prepare_fixedgen_data(nodes, nodes_index)
        update_progress((i + 1) / len(dpgs))
    print('done!')
    # [print(d, i) for d, i in zip(dpgs, dpgs_index)]

    print('---------- %s seconds -----------' % (time.time() - start_time))

    # print(R, DPGCODE, dpgs[R].consumer_code, len(dpgs[dpgs_index[DPGID]].get_distributed_bid()))
    # [print(d) for d in dpgs[dpgs_index[907]].get_distributed_bid()]
    return dpgs, dpgs_index
示例#3
0
def make_dpgs(tsid, tdate=''):
    if tdate:
        print('making demands for', tdate)
    start_time = time.time()

    cs = sql_scripts.ConsumersScript()
    bs = sql_scripts.BidsScript()
    ks = sql_scripts.KcNodeScript()

    con = ora.OracleConnection()

    DpgDemand.set_max_bid_prices(
        con.exec_script(sql_scripts.MaxBidPriceScript().get_query(),
                        {'tsid': tsid}))

    dpgs = []
    dpgs_index = {}

    @ora.process_cursor(con, cs, {'tsid': tsid})
    def process_consumers(dpg_list, dpg_list_index, new_row):
        dpg_list.append(DpgDemand(new_row))
        dpg_list_index[new_row[cs['dpg_id']]] = len(dpg_list) - 1

    print('getting consumer DPGs')
    process_consumers(dpgs, dpgs_index)

    # R = random.randint(0, len(dpgs))
    # DPGCODE = dpgs[R].code
    # DPGID = dpgs[R].id

    @ora.process_cursor(con, bs, {'tsid': tsid})
    def process_bids(dpg_list, dpg_list_index, new_row):
        dpg_id = new_row[bs['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_bid_data(new_row)

    print('getting bid information')
    process_bids(dpgs, dpgs_index)

    @ora.process_cursor(con, ks, {'tsid': tsid})
    def process_k_distr(dpg_list, dpg_list_index, new_row):
        dpg_id = new_row[ks['dpg_id']]
        if dpg_id in dpg_list_index.keys():
            dpg_list[dpg_list_index[dpg_id]].add_k_distr_data(new_row)

    print('getting k_distr information')
    process_k_distr(dpgs, dpgs_index)

    print("distributing consumer's bids")
    for i, d in enumerate(dpgs):
        d.finalize_data()
        d.distribute_bid()
        update_progress((i + 1) / len(dpgs))
    print('done!')
    # [print(d, i) for d, i in zip(dpgs, dpgs_index)]

    print('---------- %s seconds -----------' % (time.time() - start_time))

    # print(R, DPGCODE, dpgs[R].consumer_code, len(dpgs[dpgs_index[DPGID]].get_distributed_bid()))
    # [print(d) for d in dpgs[dpgs_index[DPGID]].get_distributed_bid()]
    return dpgs, dpgs_index
示例#4
0
def make_eq_db_supplies(tsid, tdate=''):
    if tdate:
        print('making eq_db_supplies for', tdate)

    start_time = time.time()

    PMINTECHPRICE = 0
    PMINTECHINTERVAL = -20
    PMINPRICE = 0.01
    PMININTERVAL = -18
    TARIFF = 9999
    FORCEDSMOOTH = 0
    PRICEACC = 0.8
    GESSTATIONTYPE = 2
    GESINTERVAL = 0

    con = ora.OracleConnection()

    gs = GeneratorsScript()
    bs = BidsScript()
    ks = KgRgeScript()
    rgs = RastrGenScript()
    ws = WsumgenScript()

    gtps = con.exec_script(gs.get_query(), {'tsid': tsid})
    # rges = con.exec_script(rgs.get_query())
    wsumgen = con.exec_script(ws.get_query(), {'tsid': tsid})

    def index_wsumgen(row):
        return row[ws['rge_code']]

    ws_index = list(map(index_wsumgen, wsumgen))

    # def index_rges(rges_row):
    #     return rges_row[rgs['hour']], rges_row[rgs['rge_code']]
    #
    # rges_index = list(map(index_rges, rges))

    # cntr = 0
    data = []
    for g in gtps:
        # print(g[gs['dpg_code']])
        dpg_bids = []
        if g[gs['station_type']] != GESSTATIONTYPE:
            dpg_bids = sorted(con.exec_script(bs.get_query(), {
                'dpg_code': g[gs['dpg_code']],
                'tsid': tsid
            }),
                              key=itemgetter(bs['hour'],
                                             bs['interval_number']))
        # dpg_bids = sorted(
        #     [bid for bid in bids if bid[bs['dpg_code']] == d[gs['dpg_code']]]
        #     , key=itemgetter(bs['hour'], bs['interval_number']))
        rge_kg = sorted(con.exec_script(ks.get_query(), {
            'dpg_id': g[gs['gtp_id']],
            'tsid': tsid
        }),
                        key=itemgetter(ks['hour']))

        for k in rge_kg:
            # rge_data = rges[rges_index.index((k[ks['hour']], k[ks['rge_code']]))]
            pmax = k[ks['pmax']]

            # if rge_data:
            #     prev_volume = rge_data[rgs['pmin']]
            #     pmax = rge_data[rgs['pmax']]
            #     if rge_data[rgs['pmin_tech']]:
            #         cur_row = (k[ks['hour']], k[ks['node']], rge_data[rgs['pmin_tech']], rge_data[rgs['pmin_tech']],
            #                    PMINTECHPRICE, k[ks['rge_code']], PMINTECHINTERVAL, 0, TARIFF, FORCEDSMOOTH)
            #         data.append(cur_row)
            #     volume = rge_data[rgs['pmin']] - rge_data[rgs['pmin_tech']]
            #     if volume:
            #         cur_row = (k[ks['hour']], k[ks['node']], volume, volume,
            #                    PMINPRICE, k[ks['rge_code']], PMININTERVAL, 0, TARIFF, FORCEDSMOOTH)
            #         data.append(cur_row)

            prev_volume = k[ks['pmin']]
            if k[ks['rge_code']] in ws_index:
                volume = k[ks['pmin']]
            else:
                volume = max(
                    k[ks['pminagg']], k[ks['dpminso']], k[ks['pmin']]
                    if g[gs['station_type']] == GESSTATIONTYPE else 0)
            if check_number(volume):
                cur_row = (k[ks['hour']], k[ks['node']], volume, volume,
                           PMINTECHPRICE, k[ks['rge_code']], PMINTECHINTERVAL,
                           0, TARIFF, FORCEDSMOOTH)
                data.append(cur_row)

            volume = k[ks['pmin']] - volume
            if check_number(volume):
                cur_row = (k[ks['hour']], k[ks['node']], volume, volume,
                           PMINPRICE, k[ks['rge_code']], PMININTERVAL, 0,
                           TARIFF, FORCEDSMOOTH)
                data.append(cur_row)

            if k[ks['rge_code']] in ws_index:
                if [w for w in wsumgen if w[ws['rge_code']] == k[ks['rge_code']]][0][ws['hour_start']]\
                   <= k[ks['hour']] <= [w for w in wsumgen if w[ws['rge_code']] == k[ks['rge_code']]][0][ws['hour_end']]:
                    w = wsumgen[ws_index.index(k[ks['rge_code']])]
                    integral_id = w[ws['integral_id']] if w[
                        ws['integral_id']] else 0
                    volume = pmax - prev_volume
                    if check_number(volume):
                        cur_row = (k[ks['hour']], k[ks['node']], volume, 0,
                                   w[ws['price']], k[ks['rge_code']],
                                   GESINTERVAL, w[ws['integral_id']], TARIFF,
                                   FORCEDSMOOTH)

                        data.append(cur_row)
            elif g[gs['station_type']] == GESSTATIONTYPE:
                volume = min(k[ks['p']] - prev_volume, pmax - prev_volume)
                if check_number(volume):
                    min_volume = volume
                    price = PMINPRICE
                    cur_row = (k[ks['hour']], k[ks['node']], volume,
                               min_volume, price, k[ks['rge_code']],
                               GESINTERVAL, 0, TARIFF, FORCEDSMOOTH)

                    data.append(cur_row)
            else:
                for bid in dpg_bids:
                    if bid[bs['hour']] == k[ks['hour']]:
                        if k[ks['pmax_dpg']] > k[ks['pmin_dpg']]:
                            if min(bid[bs['volume']],
                                   k[ks['pmax_dpg']]) <= k[ks['pmin_dpg']]:
                                k_distr = k[ks['kg_min']]
                            else:
                                k_distr = k[ks['kg_reg']]
                        else:
                            k_distr = k[ks['kg']]

                        bid_rge = (bid[bs['volume']] -
                                   k[ks['pmin_dpg']]) * k_distr + k[ks['pmin']]
                        volume = min(bid_rge - prev_volume, pmax - prev_volume)

                        if check_number(volume):
                            prev_volume = bid_rge
                            min_volume = volume if bid[
                                bs['interval_number']] < 0 else 0
                            price_acc = PMINPRICE if g[
                                gs['is_pintsch_gas']] else PRICEACC
                            price = bid[bs['price']] if bid[
                                bs['price']] > 0 else price_acc
                            cur_row = (bid[bs['hour']], k[ks['node']], volume,
                                       min_volume, price, k[ks['rge_code']],
                                       bid[bs['interval_number']], 0, TARIFF,
                                       FORCEDSMOOTH)

                            data.append(cur_row)

    data = sorted(data, key=itemgetter(1, 0, 5, 6))
    mat4py.savemat('common.mat', {'eq_db_supplies': data})

    print('---------- %s seconds -----------' % (time.time() - start_time))
def make_dpgs(tsid, tdate='', nodes=None, lines=None, sections=None):
    # tsid = 221348901
    # tdate = '01.01.1970'

    if tdate:
        print('preparing dpgs for', tdate)
    start_time = time.time()

    if not nodes:
        nodes = make_nodes(tsid, tdate)

    if not lines:
        lines = make_lines(tsid, tdate, nodes)

    if not sections:
        sections = make_sections(tsid, tdate, lines)

    cs = sql_scripts.ConsumersScript()
    gs = sql_scripts.GeneratorsScript()
    imp_s = sql_scripts.ImpexDpgsScript()
    bs = sql_scripts.BidsScript()
    ks = sql_scripts.KcNodeScript()
    kr = sql_scripts.KgRgeScript()
    rgs = sql_scripts.RastrGenScript()
    rs = sql_scripts.RastrConsumerScript()
    ls = sql_scripts.RastrLoadScript()
    ra = sql_scripts.RastrAreaScript()

    con = ora.OracleConnection()

    DpgDemand.set_max_bid_prices(
        con.exec_script(sql_scripts.MaxBidPriceScript().get_query(),
                        {'tsid': tsid}))
    DpgDemand.add_disqualified_data(
        con.exec_script(sql_scripts.DisqualifiedDataScript().get_query(),
                        {'tsid': tsid}))
    DpgSupply.set_wsum_data(
        con.exec_script(sql_scripts.WsumgenScript().get_query(),
                        {'tsid': tsid}))

    # dpgs = []
    dpgs = DpgList()
    # dpgs_index = {}
    consumers_index = {}
    dpg_areas_index = {}

    @ora.process_cursor(con, cs, {'tsid': tsid})
    def process_consumers(new_row, dpg_list):
        dpg_list.add_consumer(new_row)

    @ora.process_cursor(con, gs, {'tsid': tsid})
    def process_generators(new_row, dpg_list):
        dpg_list.add_generator(new_row)

    @ora.process_cursor(con, imp_s, {'tsid': tsid})
    def process_impex_dpgs(new_row, dpg_list):
        dpg_list.add_impex(new_row)

    @ora.process_cursor(con, rgs, {'tsid': tsid})
    def process_generator_data(new_row, dpg_list):
        dpg_id = new_row[rgs['dpg_id']]
        if dpg_list[dpg_id]:
            dpg_list[dpg_id].add_generator_data(new_row)

    @ora.process_cursor(con, bs, {'tsid': tsid})
    def process_bids(new_row, dpg_list):
        dpg_id = new_row[bs['dpg_id']]
        if dpg_list[dpg_id]:
            dpg_list[dpg_id].add_bid_data(new_row)

    @ora.process_cursor(con, ks, {'tsid': tsid})
    def process_k_distr(new_row, dpg_list):
        dpg_id = new_row[ks['dpg_id']]
        if dpg_list[dpg_id]:
            dpg_list[dpg_id].add_k_distr_data(new_row)

    @ora.process_cursor(con, kr, {'tsid': tsid})
    def process_k_rge_distr(new_row, dpg_list):
        dpg_id = new_row[kr['dpg_id']]
        if dpg_list[dpg_id]:
            dpg_list[dpg_id].add_k_distr_data(new_row)

    @ora.process_cursor(con, rs, {'tsid': tsid})
    def process_rastr_consumer(new_row, dpg_list):
        consumer_code = new_row[rs['consumer_code']]
        if dpg_list.get_consumer_by_code(consumer_code):
            dpg_list.get_consumer_by_code(consumer_code).add_consumer_data(
                new_row)

    @ora.process_cursor(con, ls, {'tsid': tsid})
    def process_rastr_load(new_row, dpg_list):
        consumer_code = new_row[ls['consumer_code']]
        if dpg_list.get_consumer_by_code(consumer_code):
            dpg_list.get_consumer_by_code(consumer_code).add_load_data(new_row)

    @ora.process_cursor(con, ra, {'tsid': tsid})
    def process_rastr_area(new_row, dpg_list):
        area = new_row[ra['area']]
        if dpg_list.get_consumer_by_area(area):
            dpg_list.get_consumer_by_area(area).add_area_data(new_row)

    # print('getting consumer DPGs')
    # process_consumers(dpgs)
    # process_rastr_consumer(dpgs)
    # process_rastr_load(dpgs)
    # process_rastr_area(dpgs)
    # for dpg in dpgs:
    #     dpg.attach_nodes(nodes)
    #
    # print('getting supplier DPGs')
    # process_generators(dpgs)

    print('getting impex DPGs')
    process_impex_dpgs(dpgs)
    for i, d in enumerate(dpgs):
        d.attach_sections(sections)
        update_progress((i + 1) / len(dpgs))

    # R = random.randint(0, len(dpgs))
    # DPGCODE = dpgs[R].code
    # DPGID = dpgs[R].id

    # print('getting generator information')
    # process_generator_data(dpgs)

    # print('getting bid information')
    # process_bids(dpgs)
    # #
    # # print('getting k_distr information')
    # # process_k_distr(dpgs)
    # # process_k_rge_distr(dpgs)
    #
    # print("distributing consumer's bids")
    # for i, d in enumerate(dpgs):
    #     d.finalize_data()
    #     d.distribute_bid()
    #     d.prepare_generator_data()
    #     d.prepare_fixedgen_data(nodes)
    #     d.attach_to_fed_station(dpgs)
    #     update_progress((i + 1) / len(dpgs))
    #
    # for i, d in enumerate(dpgs):
    #     d.prepare_fixedcon_data()
    #     update_progress((i + 1) / len(dpgs))
    # print('done!')
    # [print(d, i) for d, i in zip(dpgs, dpgs_index)]

    print('---------- %s seconds -----------' % (time.time() - start_time))

    # print(R, DPGCODE, dpgs[R].consumer_code, len(dpgs[dpgs_index[DPGID]].get_distributed_bid()))
    # [print(d) for d in dpgs[dpgs_index[907]].get_distributed_bid()]
    return dpgs, sections