Beispiel #1
0
    def testDataFrame(self):
        ampl = self.ampl
        # Create first dataframe (for data indexed over NUTR)
        # Add data row by row
        df1 = DataFrame('NUTR', ('n_min', 'n_max'))
        df1.addRow(('A', 700, 20000))
        df1.addRow(('B1', 700, 20000))
        df1.addRow(('B2', 700, 20000))
        df1.addRow(('C', 700, 20000))
        df1.addRow(('CAL', 16000, 24000))
        df1.addRow(('NA', 0.0, 50000))

        # Create second dataframe (for data indexed over FOOD)
        # Add column by column
        df2 = DataFrame('FOOD')
        foods = ['BEEF', 'CHK', 'FISH', 'HAM', 'MCH', 'MTL', 'SPG', 'TUR']
        df2.setColumn('FOOD', foods)
        contents = [2] * 8
        df2.addColumn('f_min', contents)
        contents = [10] * 8
        df2.addColumn('f_max', contents)
        costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]
        df2.addColumn('cost', costs)

        print(df2.getColumn('FOOD'))
        for index in df2.getColumn('FOOD'):
            print(df2.getRow(index))

        # Create third dataframe, to assign data to the AMPL entity
        # param amt{NUTR, FOOD};
        df3 = DataFrame(('NUTR', 'FOOD'))
        # Populate the set columns
        nutrWithMultiplicity = [''] * 48
        foodWithMultiplicity = [''] * 48
        i = 0
        for n in range(6):
            for f in range(8):
                print(df1.getRowByIndex(n)[0])
                nutrWithMultiplicity[i] = df1.getRowByIndex(n)[0]
                foodWithMultiplicity[i] = foods[f]
                i += 1
        df3.setColumn('NUTR', nutrWithMultiplicity)
        df3.setColumn('FOOD', foodWithMultiplicity)

        # Populate with all these values
        values = [
            60, 8, 8, 40, 15, 70, 25, 60, 10, 20, 15, 35, 15, 15, 25, 15, 15,
            20, 10, 10, 15, 15, 15, 10, 20, 0, 10, 40, 35, 30, 50, 20, 295,
            770, 440, 430, 315, 400, 370, 450, 968, 2180, 945, 278, 1182, 896,
            1329, 1397
        ]
        df3.addColumn('amt', values)
Beispiel #2
0
    def testDataFrame(self):
        ampl = self.ampl
        # Create first dataframe (for data indexed over NUTR)
        # Add data row by row
        df1 = DataFrame("NUTR", ("n_min", "n_max"))
        df1.addRow(("A", 700, 20000))
        df1.addRow(("B1", 700, 20000))
        df1.addRow(("B2", 700, 20000))
        df1.addRow(("C", 700, 20000))
        df1.addRow(("CAL", 16000, 24000))
        df1.addRow(("NA", 0.0, 50000))

        # Create second dataframe (for data indexed over FOOD)
        # Add column by column
        df2 = DataFrame("FOOD")
        foods = ["BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR"]
        df2.setColumn("FOOD", foods)
        self.assertEqual(list(df2.getColumn("FOOD")), foods)
        contents = [2] * 8
        df2.addColumn("f_min", contents)
        self.assertEqual(list(df2.getColumn("f_min")), contents)
        contents = [10] * 8
        df2.addColumn("f_max", contents)
        self.assertEqual(list(df2.getColumn("f_max")), contents)
        costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]
        df2.addColumn("cost", costs)
        self.assertEqual(list(df2.getColumn("cost")), costs)
        labels = [random.choice(string.ascii_letters)] * 8
        df2.addColumn("labels", labels)
        self.assertEqual(list(df2.getColumn("labels")), labels)
        df2.addColumn("empty", [])
        self.assertEqual(list(df2.getColumn("empty")), [None] * 8)

        print(df2.getColumn("FOOD"))
        for index in df2.getColumn("FOOD"):
            print(df2.getRow(index))

        # Create third dataframe, to assign data to the AMPL entity
        # param amt{NUTR, FOOD};
        df3 = DataFrame(("NUTR", "FOOD"))
        # Populate the set columns
        nutrWithMultiplicity = [""] * 48
        foodWithMultiplicity = [""] * 48
        i = 0
        for n in range(6):
            for f in range(8):
                print(df1.getRowByIndex(n)[0])
                nutrWithMultiplicity[i] = df1.getRowByIndex(n)[0]
                foodWithMultiplicity[i] = foods[f]
                i += 1
        df3.setColumn("NUTR", nutrWithMultiplicity)
        df3.setColumn("FOOD", foodWithMultiplicity)

        # Populate with all these values
        values = [
            60,
            8,
            8,
            40,
            15,
            70,
            25,
            60,
            10,
            20,
            15,
            35,
            15,
            15,
            25,
            15,
            15,
            20,
            10,
            10,
            15,
            15,
            15,
            10,
            20,
            0,
            10,
            40,
            35,
            30,
            50,
            20,
            295,
            770,
            440,
            430,
            315,
            400,
            370,
            450,
            968,
            2180,
            945,
            278,
            1182,
            896,
            1329,
            1397,
        ]
        df3.addColumn("amt", values)
Beispiel #3
0
def clearing_algorithm(id, v_e, li, c, ref_entities):
    """Stress testing algorithm : Solve the clearing problem.


    type : number of infeasible solutions
         num of non maximal solutions
          recovery rate vector
         equity
          vector of error in the update function  

    Parameters
    ----------
    id : string 
    v_ea : 2d array  ( post externall assets; vector of assets after shock)
    li : 2d array (liability matrix, debt contract)   
    c : 3d array (cds contracts)
    ref_entities : list (set of reference entities)


    """

    id_file_nonmax = open('id_file_nonmax.txt', 'a')
    id_file_infeasible = open('id_file_infeasible.txt', 'a')

    nBanks = len(v_e[0])
    fixed_vec = list()
    alpha, beta = 0.6, 0.6
    externalAssets, liability, CDS_contract, reference = v_e[
        0], li, c, ref_entities

    ## call AMPL

    ampl = AMPL()

    # set default cost parameters

    ampl.read('models/clearing_optimization.mod')  #c2.mod
    ampl.readData(
        'data/clearing_optimization.dat'
    )  # change this to irrational.dat if you don't have default costs

    #Set ampl options

    ampl.setOption('display_precision', 0)
    ampl.setOption('solution_precision', 0)

    # we will use Knitro as the solver, other options: lgo, loqo, conopt, ...

    ampl.setOption('solver', 'knitro')
    ampl.setOption('presolve_eps', 1.0e-6)
    ampl.setOption('outlev', 0)
    ampl.setOption('show_stats', 0)

    # set knitro options

    ampl.setOption(
        'knitro_options',
        'par_msnumthreads =32 ms_maxsolves=1 outlev=0 ma_terminate = 2 feastol = 1.0e-9 infeastol= 1.0e-1 feastol_abs= 1.0e-9 ms_deterministic=0  ma_terminate = 1 ma_outsub =1 bar_refinement=1  bar_slackboundpush=1.0 delta=1.0e-1 gradopt=1 hessopt=1 honorbnds=0 linesearch=0 linsolver_ooc=0 ms_enable=1 tuner=0 soc=0 initpenalty=3.0e10 bar_penaltyrule=1'
    )  # derivcheck=3')

    # another set of options to use, the above options has been tested and compared to other optio sets,
    # it obtained the most accurate results
    # one can use his/her options : see

    # ampl.setOption('knitro_options', 'par_msnumthreads =16 ms_maxsolves=10  ma_terminate = 2 feastol = 1.0e-9 infeastol= 1.0e-1 feastol_abs= 1.0e-9 ms_deterministic=0  ma_terminate = 1 ma_outsub =1 bar_refinement=1  bar_slackboundpush=1.0 delta=1.0e-1 gradopt=1 hessopt=1 honorbnds=0 linesearch=0 linsolver_ooc=0 ms_enable=1 tuner=0 soc=0 initpenalty=3.0e10 bar_penaltyrule=1 derivcheck=3')#    out_csvinfo=1   restarts=10 ms_maxsolves=0  soc=1 tuner=1 #bar_relaxcons=2 #bar_watchdog=1

    solver_status_maximal = ""

    ### prepare ampl, and initialize it by setting the data

    ampl_alpha = ampl.getParameter('alpha')
    ampl_beta = ampl.getParameter('betta')

    ampl_alpha.setValues(alpha)
    ampl_beta.setValues(beta)

    banks = [i for i in xrange(nBanks)]  #vector of indices

    ampl.getSet('Banks').setValues(banks)
    ampl.getSet('reference').setValues(d)

    ampl_liab = array_to_ampl_dataframe(nBanks, liability)

    ampl.setData(ampl_liab)

    ampl_external_assets = DataFrame('Banks', 'externalAssets')
    ampl_external_assets.setColumn('Banks', banks)
    ampl_external_assets.setColumn('externalAssets', externalAssets)

    ampl.setData(ampl_external_assets)

    ampl_CDSs = DataFrame(('i', 'j', 'k'), 'CDS_contract')
    for i in xrange(nBanks):
        for j in xrange(nBanks):
            for k in d:
                ampl_CDSs.addRow(i, j, k, c[i][j][k])

    ampl.setData(ampl_CDSs)

    maximal_out = []
    infeasible_out = []
    total_recovery_rates = []
    total_equity = []
    f_vec = []
    """
    set the objective, named recov
    if we have this objective funtion
    then the problem become Clearing Feasibility Problem
    as the value of recovery_rate_no_debts is constant
"""
    ampl.eval('maximize recov : sum{i in Banks} recovery_rate_no_debts[i];')
    ''' 
        for each element of the post external asset we need to solve the clearing problem
        remark: post_externall_assets are defined in the cl_btie_main.py or cl_uni_main.py;
        they contain external assets after applying shocks
        since we need to run the clearing algorithm for various array of external assets (see shock_gen.py) 
        and we don't want to have AMPL's loading and initializing overhead at every time.
        we will just update the externall assets parameter at each round.'''

    for m in range(len(v_e)):

        # if external asset is zero then, obviously, we don't do the clearing:
        # shock on a bank without externall assets does not change the vector of externall assets

        if v_e[0][m] != 0:

            equity = [1000000000000 for i in range(nBanks)]

            # set value of previous equity to infinity as we want this constraint be redundant in solving
            # the Clearing Optimization Problem and checking if the problem is infeasible

            prev_eq = ampl.getParameter('preveq')  #.getValues()

            prev_eq.setValues(equity)
            # drop the Clearing Optimization objective
            ampl.obj['recov'].drop()
            # restore new objective which is constant: to use if for the Clearing Feasibility Problem, and checking maximality
            #Solve the clearing optimization problem,
            # we solve the model given our data, but this time the objective function is maximizing 'total_equity'
            # as defined in the clearing_optimization.mod .

            ampl.obj['Tot_recov'].restore()

            ea = ampl.getParameter('externalAssets')

            ea.setValues(v_e[m])

            # set ampl options again

            ## Clearing Optimization Problem, checking feasibility

            ampl.setOption(
                'knitro_options',
                'par_msnumthreads =32 ms_maxsolves=10 outlev=0 ma_terminate = 2 feastol = 1.0e-9 infeastol= 1.0e-1 feastol_abs= 1.0e-9 ms_deterministic=0  ma_terminate = 1 ma_outsub =1 bar_refinement=1  bar_slackboundpush=1.0 delta=1.0e-1 gradopt=1 hessopt=1 honorbnds=0 linesearch=0 linsolver_ooc=0 ms_enable=1 tuner=0 soc=0 initpenalty=3.0e10 bar_penaltyrule=1'
            )  # derivcheck=3')

            ampl.solve()

            tot_payment_1 = ampl.obj['Tot_recov']

            solver_status = tot_payment_1.result()

            tot_payment_1 = tot_payment_1.drop()

            ampl.obj['Tot_recov'].drop()

            ampl.obj['recov'].restore()

            recoveryRate = ampl.getVariable('result').getValues().toList()
            equity = (ampl.getVariable('equity').getValues()).toList()

            ## update recovery results by rounding those near to 1, to 1

            for x in xrange(len(recoveryRate)):

                if recoveryRate[x][1] > 1 or recoveryRate[x][1] > 0.99999999:
                    recoveryRate[x] = 1
                else:
                    recoveryRate[x] = (recoveryRate[x])[1]

            for x in range(len(equity)):
                equity[x] = equity[x][1]
            '''
            #retrieve alpha and beta
            
            alpha = ampl.getParameter('alpha').getValues()
            alpha = ((alpha.toList())[0][0])
            beta = ampl.getParameter('betta').getValues()
            beta = ((beta.toList())[0][0])
      

            '''

            CDSs = d

            # s is the result of update function, i.e., the difference of approximate and actual value
            s = abs(
                uf.update_f(alpha, beta, nBanks, CDSs, v_e[m], b, c,
                            recoveryRate))

            if solver_status == 'solved':

                ## CLearing Feasibility Problem (maximality check)
                maximality = 'maximal'
                prev_eq.setValues(equity)

                ampl.setOption(
                    'knitro_options',
                    'par_msnumthreads =32 ms_maxsolves=1 outlev=0 ma_terminate = 2 feastol = 1.0e-9 infeastol= 1.0e-1 feastol_abs= 1.0e-9 ms_deterministic=0  ma_terminate = 1 ma_outsub =1 bar_refinement=1  bar_slackboundpush=1.0 delta=1.0e-1 gradopt=1 hessopt=1 honorbnds=0 linesearch=0 linsolver_ooc=0 ms_enable=1 tuner=0 soc=0 initpenalty=3.0e10 bar_penaltyrule=1'
                )  # derivcheck=3')

                ## solve the clearing feasibility problem, this time to check if the previous solution is maximal
                # if it returns infeasible, then the solution of Clearing Optimization program is not maximal, other wise
                # we have found a maximal solution

                ampl.solve()
                tot_payment_2 = ampl.getObjective('recov')

                solver_status_maximal = tot_payment_2.result()

                tot_payment_2 = tot_payment_2.drop()

                if solver_status_maximal == 'solved':
                    maximality = 'non_maximal'
                else:
                    maximality = 'maximal'

            else:
                maximality = 'none'

            total_equity.append(sum(equity))
            total_recovery_rates.append(
                np.count_nonzero(np.array(recoveryRate) - 1))

            if solver_status != 'infeasible':
                f_vec.append(s)

            if maximality == 'non_maximal':

                maximal_out.append(m)
                status = 'nonmax'
                id_file_nonmax.write(id)
                generate_polynomials(status, id, v_e[m], li, c, ref_entities,
                                     alpha, beta)

            if solver_status == 'infeasible':

                infeasible_out.append(m)
                status = 'infeasible'
                id_file_infeasible.write(id)
                generate_polynomials(status, id, v_e[m], li, c, ref_entities,
                                     alpha, beta)

    ampl.reset()

    return f_vec, total_recovery_rates, total_equity, infeasible_out, maximal_out
def modelo():

    # Variáveis globais
    global otimo, m, n, s, capacidade_dep, capacidade_vei, SOC_max, ampl
    global velocidad_carga, demanda, servico, inicio, ultimo, custo
    global distancia, tempo, nodos, arcos
    global titulo, dados_1, graf, indices, indices_2

    # Chamar função cluster
    cluster()

    # Carregar parãmetros e variáveis
    ampl = AMPL()  # Carregar objeto AMPL
    ampl.reset()  # Reiniciar as variáveis e parâmetros
    ampl.read('modelo_PRFVE.mod')  # Carregar modelo
    m = ampl.getParameter('m')  # Número de nós
    n = ampl.getParameter('n')  # Número de veículos
    s = ampl.getParameter('s')  # Número de estações de carregamento
    M = ampl.getParameter('M')  # Minutos na estação de carregamento
    alpha = ampl.getParameter('alpha')  # Limite superior do estado de carga
    beta = ampl.getParameter('beta')  # Limite inferior do estado de carga
    consumo = ampl.getParameter(
        'consumo')  # Consumo de energia por quilômetro percorrido
    capacidade_dep = ampl.getParameter(
        'capacidade_dep')  # Capacidade do depósito
    capacidade_vei = ampl.getParameter('capacidade_vei')  # Capacidade veículos
    SOC_max = ampl.getParameter('SOC_max')  # SOC max bateria
    velocidad_carga = ampl.getParameter(
        'velocidad_carga')  # Velocidade de carga
    demanda = ampl.getParameter(
        'demanda')  # Demanda de mercadorias dos clientes
    servico = ampl.getParameter('servico')  # Tempo de serviço no cliente
    inicio = ampl.getParameter(
        'inicio')  # Inicio da janela de tempo de atendimento
    ultimo = ampl.getParameter(
        'ultimo')  # Fim da janela de tempo de atendimento
    custo = ampl.getParameter('custo')  # Custo de transporte do nó i ao nó j
    distancia = ampl.getParameter('distancia')  # Distancia do nó i ao nó j
    tempo = ampl.getParameter('tempo')  # Tempo de percorrer o nó i ao nó j
    nodos = ampl.getSet('NODOS')  # Conjunto de nós
    arcos = ampl.getSet('ARCOS')  # Conjunto de arcos
    #ar = ampl.getSet('AR')  # Conjunto de arcos

    # Atribuir valores
    m.setValues([int(e_0.get()) + int(e_3.get())])  # Valor do número de n
    n.setValues([int(e_2.get())])  # Valor de veículos
    s.setValues([int(e_3.get())])  # Valor de estações
    M.setValues([1246])  # Valor de minutos de carga na estação
    alpha.setValues([0.8])  # Valor do limite superior do estado de carga
    beta.setValues([0.2])  # Valor do limite inferior do estado de carga
    capacidade_dep.setValues([int(e_4.get())])  # Valor de capacidade

    # Adicionar as estações de carregamento
    for i in range(1, int(s.value()) + 1):
        titulo.loc[i, 'X_coord'] = round(centroides[i - 1, 0], 1)
        titulo.loc[i, 'Y_coord'] = round(centroides[i - 1, 1], 1)

    # Valores das capacidades de mercadorias dos veículos
    for i in range(1, capacidade_vei.numInstances() + 1):
        capacidade_vei[i] = int(e_5.get())

    # Valores do SOC máximo das baterias dos veículos
    for i in range(1, SOC_max.numInstances() + 1):
        SOC_max[i] = int(e_6.get())

    # Valores da velocidade da carga das baterias dos veículos
    for i in range(1, velocidad_carga.numInstances() + 1):
        velocidad_carga[i] = int(e_7.get())

    # Consumo de energia por quilômetro percorrido dos veículos
    for i in range(1, consumo.numInstances() + 1):
        consumo[i] = 1

    # Valores de demanda de mercadorias dos clientes
    for i in range(1, demanda.numInstances() + 1):
        if i >= demanda.numInstances() + 1 - s.value():
            demanda[i] = 0
        else:
            demanda[i] = int(titulo.iloc[i - 1, 3])

    # Valores de tempo de serviço nos clientes
    for i in range(1, servico.numInstances() + 1):
        if i >= servico.numInstances() + 1 - s.value():
            servico[i] = 0
        else:
            servico[i] = int(titulo.iloc[i - 1, 6])

    # Valores do inicio da janela de tempo de atendimento
    for i in range(1, inicio.numInstances() + 1):
        if i >= inicio.numInstances() + 1 - s.value():
            inicio[i] = 0
        else:
            inicio[i] = int(titulo.iloc[i - 1, 4])

    # Valores do fim da janela de tempo de atendimento
    for i in range(1, ultimo.numInstances() + 1):
        if i >= ultimo.numInstances() + 1 - s.value():
            ultimo[i] = int(titulo.iloc[0, 5])
        else:
            ultimo[i] = int(titulo.iloc[i - 1, 5])

    # Criar indices para os conjuntos de dados
    indices = DataFrame(index=('Index0', 'Index1'))
    for i in range(1, int(m.value()) + 1):
        for j in range(1, int(m.value()) + 1):
            if i != j:
                indices.addRow(i, j)

    # Valores dos arcos
    arcos.setValues(indices)
    #ar.setValues(indices_2)

    # Valores do custo, distância e tempo
    for i in range(1, int(m.value()) + 1):
        for j in range(1, int(m.value()) + 1):
            if i != j:
                x = float(titulo.iloc[i - 1, 1]) - float(titulo.iloc[j - 1, 1])
                y = float(titulo.iloc[i - 1, 2]) - float(titulo.iloc[j - 1, 2])
                dist = round(math.sqrt(pow(x, 2) + pow(y, 2)), 1)
                distancia[i, j] = dist
                custo[i, j] = dist * 3
                tempo[i, j] = dist
Beispiel #5
0
def main(argc, argv):
    from amplpy import AMPL, DataFrame
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        # Create first dataframe (for data indexed over NUTR)
        # Add data row by row
        df1 = DataFrame('NUTR', ('n_min', 'n_max'))
        df1.addRow('A', 700, 20000)
        df1.addRow('B1', 700, 20000)
        df1.addRow('B2', 700, 20000)
        df1.addRow('C', 700, 20000)
        df1.addRow('CAL', 16000, 24000)
        df1.addRow('NA', 0.0, 50000)

        # Create second dataframe (for data indexed over FOOD)
        # Add column by column
        df2 = DataFrame('FOOD')
        foods = ['BEEF', 'CHK', 'FISH', 'HAM', 'MCH', 'MTL', 'SPG', 'TUR']
        df2.setColumn('FOOD', foods)
        contents = [2] * 8
        df2.addColumn('f_min', contents)
        contents = [10] * 8
        df2.addColumn('f_max', contents)
        costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]
        df2.addColumn('cost', costs)

        # Create third dataframe, to assign data to the AMPL entity
        # param amt{NUTR, FOOD};
        df3 = DataFrame(index=('NUTR', 'FOOD'))
        # Populate the set columns
        nutrWithMultiplicity = [''] * 48
        foodWithMultiplicity = [''] * 48
        i = 0
        for n in range(6):
            for f in range(8):
                print(df1.getRowByIndex(n)[0])
                nutrWithMultiplicity[i] = df1.getRowByIndex(n)[0]
                foodWithMultiplicity[i] = foods[f]
                i += 1
        df3.setColumn('NUTR', nutrWithMultiplicity)
        df3.setColumn('FOOD', foodWithMultiplicity)

        # Populate with all these values
        values = [
            60, 8, 8, 40, 15, 70, 25, 60, 10, 20, 15, 35, 15, 15, 25, 15, 15,
            20, 10, 10, 15, 15, 15, 10, 20, 0, 10, 40, 35, 30, 50, 20, 295,
            770, 440, 430, 315, 400, 370, 450, 968, 2180, 945, 278, 1182, 896,
            1329, 1397
        ]
        df3.addColumn('amt', values)

        # Create AMPL object
        ampl = AMPL()

        if argc > 1:
            ampl.setOption('solver', argv[1])

        # Read the model file
        modelDirectory = argv[2] if argc == 3 else os.path.join('..', 'models')
        ampl.read(os.path.join(modelDirectory, 'diet/diet.mod'))

        # Assign data to NUTR, n_min and n_max
        ampl.setData(df1, 'NUTR')
        # Assign data to FOOD, f_min, f_max and cost
        ampl.setData(df2, 'FOOD')
        # Assign data to amt
        ampl.setData(df3)
        # Solve the model
        ampl.solve()

        # Print out the result
        print("Objective function value: {}".format(
            ampl.getObjective('total_cost').value()))

        # Get the values of the variable Buy in a dataframe
        results = ampl.getVariable('Buy').getValues()
        # Print
        print(results)
    except Exception as e:
        print(e)
        raise