コード例 #1
0
ファイル: dietmodel.py プロジェクト: ytakashina/amplpy
def main(argc, argv):
    from amplpy import AMPL, DataFrame
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        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'))

        foods = ['BEEF', 'CHK', 'FISH', 'HAM', 'MCH', 'MTL', 'SPG', 'TUR']
        costs = [3.59, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]

        fmin = [2, 2, 2, 2, 2, 2, 2, 2]
        fmax = [10, 10, 10, 10, 10, 10, 10, 10]

        df = DataFrame('FOOD')
        df.setColumn('FOOD', foods)
        df.addColumn('cost', costs)
        df.addColumn('f_min', fmin)
        df.addColumn('f_max', fmax)
        ampl.setData(df, 'FOOD')

        nutrients = ['A', 'C', 'B1', 'B2', 'NA', 'CAL']
        nmin = [700, 700, 700, 700, 0, 16000]
        nmax = [20000, 20000, 20000, 20000, 50000, 24000]

        df = DataFrame('NUTR')
        df.setColumn('NUTR', nutrients)
        df.addColumn('n_min', nmin)
        df.addColumn('n_max', nmax)
        ampl.setData(df, 'NUTR')

        amounts = [[60, 8, 8, 40, 15, 70, 25, 60],
                   [20, 0, 10, 40, 35, 30, 50, 20],
                   [10, 20, 15, 35, 15, 15, 25, 15],
                   [15, 20, 10, 10, 15, 15, 15, 10],
                   [928, 2180, 945, 278, 1182, 896, 1329, 1397],
                   [295, 770, 440, 430, 315, 400, 379, 450]]

        df = DataFrame(('NUTR', 'FOOD'), 'amt')
        df.setValues({(nutrient, food): amounts[i][j]
                      for i, nutrient in enumerate(nutrients)
                      for j, food in enumerate(foods)})
        ampl.setData(df)

        ampl.solve()

        print('Objective: {}'.format(ampl.getObjective('total_cost').value()))
    except Exception as e:
        print(e)
        raise
コード例 #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)
        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)
コード例 #3
0
def main(argc, argv):
    from amplpy import AMPL, DataFrame
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        # Create an AMPL instance
        ampl = AMPL()

        """
        # If the AMPL installation directory is not in the system search path:
        from amplpy import Environment
        ampl = AMPL(
            Environment('full path to the AMPL installation directory'))
        """

        ampl.eval('set CITIES; set LINKS within (CITIES cross CITIES);')
        ampl.eval('param cost {LINKS} >= 0; param capacity {LINKS} >= 0;')
        ampl.eval('data; set CITIES := PITT NE SE BOS EWR BWI ATL MCO;')

        cost = [2.5, 3.5, 1.7, 0.7, 1.3, 1.3, 0.8, 0.2, 2.1]
        capacity = [250, 250, 100, 100, 100, 100, 100, 100, 100]
        LinksFrom = ['PITT', 'PITT', 'NE', 'NE', 'NE', 'SE', 'SE', 'SE', 'SE']
        LinksTo = ['NE', 'SE', 'BOS', 'EWR', 'BWI', 'EWR', 'BWI', 'ATL', 'MCO']

        df = DataFrame(('LINKSFrom', 'LINKSTo'), ('cost', 'capacity'))
        df.setColumn('LINKSFrom', LinksFrom)
        df.setColumn('LINKSTo', LinksTo)
        df.setColumn('cost', cost)
        df.setColumn('capacity', capacity)
        print(df)

        ampl.setData(df, 'LINKS')
    except Exception as e:
        print(e)
        raise
コード例 #4
0
def main(argc, argv):
    from amplpy import AMPL, DataFrame
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        ampl = AMPL()
        ampl.eval('set CITIES; set LINKS within (CITIES cross CITIES);')
        ampl.eval('param cost {LINKS} >= 0; param capacity {LINKS} >= 0;')
        ampl.eval('data; set CITIES := PITT NE SE BOS EWR BWI ATL MCO;')

        cost = [2.5, 3.5, 1.7, 0.7, 1.3, 1.3, 0.8, 0.2, 2.1]
        capacity = [250, 250, 100, 100, 100, 100, 100, 100, 100]
        LinksFrom = ['PITT', 'PITT', 'NE', 'NE', 'NE', 'SE', 'SE', 'SE', 'SE']
        LinksTo = ['NE', 'SE', 'BOS', 'EWR', 'BWI', 'EWR', 'BWI', 'ATL', 'MCO']

        df = DataFrame(('LINKSFrom', 'LINKSTo'), ('cost', 'capacity'))
        df.setColumn('LINKSFrom', LinksFrom)
        df.setColumn('LINKSTo', LinksTo)
        df.setColumn('cost', cost)
        df.setColumn('capacity', capacity)
        print(df)

        ampl.setData(df, 'LINKS')
    except Exception as e:
        print(e)
        raise
コード例 #5
0
def array_to_ampl_dataframe(
        n_banks, liability):  #change the name of liability to 2d array? TODO
    ampl_liab = DataFrame(
        ('Banks', 'Banks2'),
        'liability')  #TODO : set 'liability' to parameter name, can get it?

    ampl_liab.setValues({(i, j): liability[i][j]
                         for i in xrange(n_banks) for j in xrange(n_banks)})

    return ampl_liab
コード例 #6
0
ファイル: test_dataframe.py プロジェクト: ampl/amplpy
 def testNoIndex(self):
     df = DataFrame([], ["x", "y"])
     x = [1, 2, 3]
     y = [4, 5, 6]
     df.setColumn("x", x)
     df.setColumn("y", y)
     with self.assertRaises(ValueError):
         df.toDict()
     pd_df = df.toPandas()
     self.assertEqual(list(pd_df["x"]), x)
     self.assertEqual(list(pd_df["y"]), y)
コード例 #7
0
ファイル: test_dataframe.py プロジェクト: ampl/amplpy
 def test_no_index(self):
     if pd is None:
         self.skipTest("pandas not available")
     df = DataFrame([], ["x", "y"])
     x = [1, 2, 3]
     y = [4, 5, 6]
     df.set_column("x", x)
     df.set_column("y", y)
     with self.assertRaises(ValueError):
         df.to_dict()
     pd_df = df.to_pandas()
     self.assertEqual(list(pd_df["x"]), x)
     self.assertEqual(list(pd_df["y"]), y)
コード例 #8
0
ファイル: test_dataframe.py プロジェクト: ampl/amplpy
 def testDict(self):
     dic = {"aa": "bb", "c": "a"}
     self.assertEqual(dic, DataFrame.fromDict(dic).toDict())
     dic = {1: 2}
     self.assertEqual(dic, DataFrame.fromDict(dic).toDict())
     dic = {1: 2, 3: 4}
     self.assertEqual(dic, DataFrame.fromDict(dic).toDict())
     dic = {2.0: ("a", "b"), 3: ("1", "2")}
     self.assertEqual(dic, DataFrame.fromDict(dic).toDict())
     dic = {(2.0, "c"): ("a", "b"), (3, "a"): ("1", "2")}
     self.assertEqual(dic, DataFrame.fromDict(dic).toDict())
     df = DataFrame("x", "y")
     dic = {1: 12, 2: 23}
     df.setValues(dic)
     self.assertEqual(dic, df.toDict())
     df = DataFrame("x", ["y", "z"])
     dic = {1: (12, 2), 2: (23, -1)}
     df.setValues(dic)
     self.assertEqual(dic, df.toDict())
     df = DataFrame("x", ["y", "z"])
     df.setValues({1: [1, 2]})
     self.assertEqual({1: (1, 2)}, df.toDict())
コード例 #9
0
def compute_defense(att_stg,
                    prod_dist,
                    num_of_hp=args.fix_honeypots,
                    rationality=args.fix_rationality):
    # production ports and attacker"s strategy
    df = DataFrame('P')
    ports = getRelPorts(att_stg, prod_dist, num=25)
    df.setColumn('P', list(ports))

    #ports = getAllPorts(att_stg, prod_dist)
    #print(('Considered ports are: ', ports))
    att = [att_stg.get(x, 0) for x in ports]
    prod = [prod_dist.get(x, 0) for x in ports]
    #print(('Attack ports: ', att, len(att)))
    #print(('Dist ports: ', prod, len(prod)))

    df.addColumn('s', prod)
    df.addColumn('p', att)

    ampl = AMPL(Environment(args.ampl))
    ampl.setOption('solver', args.solver)
    # ampl.setOption('verbosity', 'terse')
    # Read the model file
    ampl.read(args.model)

    # Assign data to s
    ampl.setData(df, 'P')
    ampl.eval('let L :=  {}; let rat := {};'.format(num_of_hp, rationality))

    #print(df)
    # Solve the model
    with suppress_stdout():
        ampl.solve()
    reward = ampl.getObjective("reward").value()

    hp_stg = ampl.getData("{j in P} h[j]")
    output = dict()
    stg_json = list()
    for k, v in hp_stg.toDict().items():
        stg_json.append({"port": int(k), "prob": v})

    output.update({"stg": stg_json})
    output.update({"reward": reward})
    output.update({"rationality": rationality})
    output.update({"num_of_hp": num_of_hp})
    output.update({"used_hps": ampl.getData("tot").toDict().popitem()[1]})

    ampl.close()
    return output
コード例 #10
0
def main(argc, argv):
    from amplpy import AMPL, DataFrame

    os.chdir(os.path.dirname(__file__) or os.curdir)

    # Create an AMPL instance
    ampl = AMPL()
    """
    # If the AMPL installation directory is not in the system search path:
    from amplpy import Environment
    ampl = AMPL(
        Environment('full path to the AMPL installation directory'))
    """

    ampl.eval("set CITIES; set LINKS within (CITIES cross CITIES);")
    ampl.eval("param cost {LINKS} >= 0; param capacity {LINKS} >= 0;")
    ampl.eval("data; set CITIES := PITT NE SE BOS EWR BWI ATL MCO;")

    cost = [2.5, 3.5, 1.7, 0.7, 1.3, 1.3, 0.8, 0.2, 2.1]
    capacity = [250, 250, 100, 100, 100, 100, 100, 100, 100]
    links_from = ["PITT", "PITT", "NE", "NE", "NE", "SE", "SE", "SE", "SE"]
    links_to = ["NE", "SE", "BOS", "EWR", "BWI", "EWR", "BWI", "ATL", "MCO"]

    # Using amplpy.DataFrame
    df = DataFrame(("LINKSFrom", "LINKSTo"), ("cost", "capacity"))
    df.set_column("LINKSFrom", links_from)
    df.set_column("LINKSTo", links_to)
    df.set_column("cost", cost)
    df.set_column("capacity", capacity)
    print(df)

    ampl.set_data(df, "LINKS")
    ampl.display("LINKS")

    # Using pandas.DataFrame (recommended)
    df = pd.DataFrame(
        list(zip(links_from, links_to, cost, capacity)),
        columns=["LINKSFrom", "LINKSTo", "cost", "capacity"],
    ).set_index(["LINKSFrom", "LINKSTo"])
    print(df)

    ampl.eval("reset data LINKS;")
    ampl.set_data(df, "LINKS")
    ampl.display("LINKS")
コード例 #11
0
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
コード例 #12
0
ファイル: test_dataframe.py プロジェクト: ampl/amplpy
    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)
コード例 #13
0
            'asked_cpu': 0,
            'asked_mem': 0,
            'asked_disk': 0,
            'frees_mem': leaves_mem[i],
            'frees_cpu': leaves_cpu[i],
            'frees_disk': leaves_disk[i],
            'frees_arrival': leaves_arrival[i]
        }

    # Set the ordered timestamps
    timestamps = times + leaves_time
    timestamps.sort()
    ampl.set['timestamps'] = timestamps

    # Set profits
    df = DataFrame(('timestamps'), 'profit_federate')
    df.setValues({t: events[t]['profit_federate'] for t in events.keys()})
    ampl.setData(df)
    df = DataFrame(('timestamps'), 'profit_local')
    df.setValues({t: events[t]['profit_local'] for t in events.keys()})
    ampl.setData(df)
    df = DataFrame(('timestamps'), 'profit_reject')
    df.setValues({t: events[t]['profit_reject'] for t in events.keys()})
    ampl.setData(df)

    # Set asked resources
    df = DataFrame(('timestamps'), 'asked_cpu')
    df.setValues({t: events[t]['asked_cpu'] for t in events.keys()})
    ampl.setData(df)
    df = DataFrame(('timestamps'), 'asked_mem')
    df.setValues({t: events[t]['asked_mem'] for t in events.keys()})
コード例 #14
0
def main(argc, argv):
    from amplpy import AMPL, DataFrame
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        ampl = AMPL()
        ampl.setOption('solver', 'cplexamp')
        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, 'Dr/pigskin_updated1.mod'))

        period0 = [0]

        df = DataFrame('ONLY0')
        df.setColumn('ONLY0', period0)
        ampl.setData(df, 'ONLY0')

        period0toends = [0, 1]

        df = DataFrame('PERIOD0_TO_END')
        df.setColumn('PERIOD0_TO_END', period0toends)
        ampl.setData(df, 'PERIOD0_TO_END')

        period1toends = [1]

        df = DataFrame('PERIOD1_TO_END')
        df.setColumn('PERIOD1_TO_END', period1toends)
        ampl.setData(df, 'PERIOD1_TO_END')

        products = ['1P', '2P']

        df = DataFrame('PRODUCT')
        df.setColumn('PRODUCT', products)
        ampl.setData(df, 'PRODUCT')

        resources = ['1R', '2R']

        df = DataFrame('RESOURCE')
        df.setColumn('RESOURCE', resources)
        ampl.setData(df, 'RESOURCE')

        scenarios = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
        ]

        df = DataFrame('SCENARIO')
        df.setColumn('SCENARIO', scenarios)
        ampl.setData(df, 'SCENARIO')

        inv0prod = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                    [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                    [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                    [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                    [0, 0], [0, 0]]
        df = DataFrame(('SCENARIO', 'PRODUCT'), 'Inv0prod')
        df.setValues({(scenario, product): inv0prod[s][i]
                      for s, scenario in enumerate(scenarios)
                      for i, product in enumerate(products)})
        ampl.setData(df)

        prodcost = [[12.5], [12.55]]

        df = DataFrame(('PRODUCT', 'PERIOD1_TO_END'), 'Prodcost')
        df.setValues({(product, period1toend): prodcost[i][t]
                      for i, product in enumerate(products)
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        Resource = [[16, 100], [24, 200]]

        df = DataFrame(('PRODUCT', 'RESOURCE'), 'Resource')
        df.setValues({(product, resource): Resource[i][r]
                      for i, product in enumerate(products)
                      for r, resource in enumerate(resources)})
        ampl.setData(df)

        avail = [[138516], [278847]]

        df = DataFrame(('RESOURCE', 'PERIOD1_TO_END'), 'avail')
        df.setValues({(resource, period1toend): avail[r][t]
                      for r, resource in enumerate(resources)
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        perhold = [[0.88], [0.88]]

        df = DataFrame(('PRODUCT', 'PERIOD1_TO_END'), 'perhold')
        df.setValues({(product, period1toend): perhold[i][t]
                      for i, product in enumerate(products)
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        prob = [
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333,
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333,
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333,
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333,
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333,
            0.03333333, 0.03333333, 0.03333333, 0.03333333, 0.03333333
        ]

        df = DataFrame(('SCENARIO'), 'prob')
        df.setValues({(scenario): prob[s]
                      for s, scenario in enumerate(scenarios)})
        ampl.setData(df)

        MPC = [800]

        df = DataFrame(('PERIOD1_TO_END'), 'MPC')
        df.setValues({(period1toend): MPC[t]
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        MS = [100]

        df = DataFrame(('PERIOD1_TO_END'), 'MS')
        df.setValues({(period1toend): MS[t]
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        Purchase_cost = [[10], [10]]

        df = DataFrame(('PRODUCT', 'PERIOD1_TO_END'), 'Purchase_cost')
        df.setValues({(product, period1toend): Purchase_cost[i][t]
                      for i, product in enumerate(products)
                      for t, period1toend in enumerate(period1toends)})
        ampl.setData(df)

        Demand = [[[216], [191]], [[290], [330]], [[224], [194]], [[283],
                                                                   [329]],
                  [[159], [164]], [[215], [349]], [[247], [201]], [[201],
                                                                   [328]],
                  [[343], [230]], [[326], [278]], [[319], [342]], [[223],
                                                                   [298]],
                  [[292], [191]], [[262], [284]], [[285], [200]], [[296],
                                                                   [346]],
                  [[210], [318]], [[200], [257]], [[314], [336]], [[205],
                                                                   [290]],
                  [[174], [184]], [[258], [290]], [[284], [158]], [[264],
                                                                   [312]],
                  [[228], [333]], [[239], [255]], [[190], [322]], [[344],
                                                                   [219]],
                  [[280], [241]], [[186], [254]]]

        df = DataFrame(('SCENARIO', 'PERIOD1_TO_END', 'PRODUCT'), 'Demand')
        df.setValues({(scenario, period1toend, product): Demand[s][t][i - 1]
                      for s, scenario in enumerate(scenarios)
                      for t, period1toend in enumerate(period1toends)
                      for i, product in enumerate(products)})
        ampl.setData(df)

        ampl.solve()

        print('Objective: {}'.format(ampl.getObjective('Total_cost').value()))

        produce = ampl.getVariable('Produce')
        df = produce.getValues()
        # Print them
        print(df)
        # Get the values of the variable in a dataframe object
        inventory = ampl.getVariable('Inventory')
        df = inventory.getValues()
        # Print them
        print(df)

    except Exception as e:
        print(e)
        raise
コード例 #15
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
コード例 #16
0
ファイル: dataframeexample.py プロジェクト: ytakashina/amplpy
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
コード例 #17
0
ファイル: test_dataframe.py プロジェクト: ampl/amplpy
 def testIter(self):
     df = DataFrame("x", "y")
     dic = {1: 12, 2: 23}
     df.setValues(dic)
     for row in df:
         self.assertTrue(tuple(row) in [(1, 12), (2, 23)])
コード例 #18
0
ファイル: dataframeexample.py プロジェクト: ampl/amplpy
def main(argc, argv):
    # You can install amplpy with "python -m pip install amplpy"
    from amplpy import AMPL, DataFrame

    os.chdir(os.path.dirname(__file__) or os.curdir)

    # Note: If you want to perform data transformations use pandas dataframes.
    # amplpy dataframes are simple dataframes for data communication only.

    # Create first dataframe (for data indexed over NUTR)
    # Add data row by row
    df1 = DataFrame("NUTR", ("n_min", "n_max"))
    df1.add_row("A", 700, 20000)
    df1.add_row("B1", 700, 20000)
    df1.add_row("B2", 700, 20000)
    df1.add_row("C", 700, 20000)
    df1.add_row("CAL", 16000, 24000)
    df1.add_row("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.set_column("FOOD", foods)
    contents = [2] * 8
    df2.add_column("f_min", contents)
    contents = [10] * 8
    df2.add_column("f_max", contents)
    costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]
    df2.add_column("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
    nutr_with_multiplicity = [""] * 48
    food_with_multiplicity = [""] * 48
    i = 0
    for n in range(6):
        for f in range(8):
            nutr_with_multiplicity[i] = df1.get_row_by_index(n)[0]
            food_with_multiplicity[i] = foods[f]
            i += 1
    df3.set_column("NUTR", nutr_with_multiplicity)
    df3.set_column("FOOD", food_with_multiplicity)

    # 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.add_column("amt", values)

    # Create an AMPL instance
    ampl = AMPL()

    if argc > 1:
        ampl.set_option("solver", argv[1])

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

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

    # Print out the result
    print(
        "Objective function value: {}".format(ampl.get_objective("Total_Cost").value())
    )

    # Get the values of the variable Buy in a dataframe
    results = ampl.get_variable("Buy").get_values()
    # Print
    print(results)
コード例 #19
0
ファイル: main.py プロジェクト: AliHeggie/Upside
def assign_set_data(name,data):
    """Method to assign set data taking set name and a list as arguments"""
    df = DataFrame(name)
    df.setColumn(name,data)
    ampl.setData(df,name)
コード例 #20
0
    while nnodes > 1:
        nnodes = nnodes / 10
        i += 1
    formatString = f"{{:0{i}d}}"
    nodes = {
        formatString.format(value): p.node_coords[index + 1]
        for index, value in enumerate(p.get_nodes())
    }
    return nodes


# Set problem data from tsp file
nodes = getDictFromTspFile(tspFile)

# Pass them to AMPL using a dataframe
df = DataFrame(index=[('NODES')], columns=['hpos', 'vpos'])
df.setValues(nodes)
ampl.setData(df, "NODES")

# Set some globals that never change during the execution of the problem
NODES = set(nodes.keys())
CPOINTS = {
    node: complex(coordinate[0], coordinate[1])
    for (node, coordinate) in nodes.items()
}


# Plot helpers
def plotTours(tours: list, points_coordinate: dict):
    # Plot all the tours in the list each with a different color
    colors = ['b', 'g', 'c', 'm', 'y', 'k']