Exemple #1
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
Exemple #2
0
    def testPandasNamedColumns(self):
        ampl = self.ampl
        try:
            import pandas as pd
        except ImportError:
            return
        df_unindexed = pd.DataFrame(
            [['Apple', 'Red', 3, 1.29], ['Apple', 'Green', 9, 0.99],
             ['Pear', 'Red', 25, 2.59], ['Pear', 'Green', 26, 2.79],
             ['Lime', 'Green', 99, 0.39]],
            columns=['Fruit', 'Color', 'Count', 'Price'])
        # RangeIndex
        self.assertEqual(
            DataFrame.fromPandas(df_unindexed).getHeaders(),
            ('index0', 'Fruit', 'Color', 'Count', 'Price'))

        # MultiIndex
        df_indexed = df_unindexed.set_index(['Fruit', 'Color'])
        self.assertEqual(
            DataFrame.fromPandas(df_indexed,
                                 index_names=['Fruit', 'Color']).getHeaders(),
            ('Fruit', 'Color', 'Count', 'Price'))

        # Index without name
        df = pd.DataFrame([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]],
                          index=['First', 'Second'],
                          columns=[1, 2, 3, 4, 5])
        self.assertEqual(
            DataFrame.fromPandas(df.stack()).getHeaders(),
            ('index0', 'index1', '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
Exemple #4
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
Exemple #5
0
 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())
Exemple #6
0
    def testPandas(self):
        ampl = self.ampl
        try:
            import pandas as pd
        except ImportError:
            return
        df = pd.DataFrame({"a": [1, 2], "b": [3.5, 4]}, index=["x", "y"])
        ampl.eval("""
            set S;
            param a{S};
            param b{S};
        """)
        ampl.setData(df, "S")
        self.assertEqual(list(ampl.set["S"].members()), ["x", "y"])
        self.assertEqual(ampl.param["a"]["x"], 1)
        self.assertEqual(ampl.param["b"]["y"], 4)

        df2 = pd.DataFrame(
            {
                "a": [10, 20, 30],
            },
            index=["x", "y", "z"],
        )
        df3 = pd.DataFrame({}, index=["xx", "yy"])
        df = DataFrame.fromPandas(df)
        df2 = DataFrame.fromPandas(df2)
        df3 = DataFrame.fromPandas(df3)
        self.assertTrue(isinstance(df.toDict(), dict))
        self.assertTrue(isinstance(df.toList(), list))
        self.assertTrue(isinstance(df.toPandas(), pd.DataFrame))

        self.assertEqual(df.toList()[0][1:], (1, 3.5))
        self.assertEqual(df2.toList()[0], ("x", 10))
        self.assertEqual(df3.toList()[0], "xx")

        self.assertEqual(set(df.toDict().keys()), set(["x", "y"]))
        self.assertEqual(set(df2.toDict().keys()), set(["x", "y", "z"]))
        self.assertEqual(set(df3.toDict().keys()), set(["xx", "yy"]))

        self.assertEqual(df.toDict()["x"], (1, 3.5))
        self.assertEqual(df2.toDict()["x"], 10)
        self.assertEqual(df3.toDict()["xx"], None)

        csv_file = os.path.join(os.path.dirname(__file__), "data.csv")
        p_df = pd.read_csv(csv_file, sep=";", index_col=0)
        df = DataFrame.fromPandas(p_df)
        self.assertTrue(isinstance(df.toDict(), dict))
        self.assertEqual(set(df.toDict().keys()), set([1.0, 2.0, 3.0]))
        self.assertEqual(set(df.toList()[0]), set([1.0, 0.01]))
        self.assertEqual(set(df.toList()[1]), set([2.0, 0.02]))
        self.assertEqual(set(df.toList()[2]), set([3.0, 0.03]))
Exemple #7
0
    def testPandas(self):
        ampl = self.ampl
        try:
            import pandas as pd
        except ImportError:
            return
        df = pd.DataFrame({'a': [1, 2], 'b': [3.5, 4]}, index=['x', 'y'])
        ampl.eval('''
            set S;
            param a{S};
            param b{S};
        ''')
        ampl.setData(df, 'S')
        self.assertEqual(list(ampl.set['S'].members()), ['x', 'y'])
        self.assertEqual(ampl.param['a']['x'], 1)
        self.assertEqual(ampl.param['a']['y'], 2)
        df2 = pd.DataFrame({
            'a': [10, 20, 30],
        }, index=['x', 'y', 'z'])
        df3 = pd.DataFrame({}, index=['xx', 'yy'])
        df = DataFrame.fromPandas(df)
        df2 = DataFrame.fromPandas(df2)
        df3 = DataFrame.fromPandas(df3)
        self.assertTrue(isinstance(df.toDict(), dict))
        self.assertTrue(isinstance(df.toList(), list))
        self.assertTrue(isinstance(df.toPandas(), pd.DataFrame))

        self.assertEqual(df.toList()[0][1:], (1, 3.5))
        self.assertEqual(df2.toList()[0], ('x', 10))
        self.assertEqual(df3.toList()[0], 'xx')

        self.assertEqual(set(df.toDict().keys()), set(['x', 'y']))
        self.assertEqual(set(df2.toDict().keys()), set(['x', 'y', 'z']))
        self.assertEqual(set(df3.toDict().keys()), set(['xx', 'yy']))

        self.assertEqual(df.toDict()['x'], (1, 3.5))
        self.assertEqual(df2.toDict()['x'], 10)
        self.assertEqual(df3.toDict()['xx'], None)

        csv_file = os.path.join(os.path.dirname(__file__), 'data.csv')
        p_df = pd.read_table(csv_file, sep=';', index_col=0)
        df = DataFrame.fromPandas(p_df)
        self.assertTrue(isinstance(df.toDict(), dict))
        self.assertEqual(set(df.toDict().keys()), set([1.0, 2.0, 3.0]))
        self.assertEqual(set(df.toList()[0]), set([1.0, 0.01]))
        self.assertEqual(set(df.toList()[1]), set([2.0, 0.02]))
        self.assertEqual(set(df.toList()[2]), set([3.0, 0.03]))
Exemple #8
0
 def test_numpy(self):
     ampl = self.ampl
     if np is None:
         self.skipTest("numpy not available")
     ampl.eval("set X;")
     arr = np.array([1, 2, 3])
     ampl.set["X"] = arr
     self.assertEqual(list(ampl.set["X"].members()), [1, 2, 3])
     ampl.eval("param p{1..3};")
     ampl.param["p"] = arr
     self.assertEqual(ampl.param["p"][2], 2)
     self.assertEqual(DataFrame.from_numpy(arr).to_list(), [1, 2, 3])
     mat = np.array([[1, 2], [3, 4], [5, 6]])
     self.assertEqual(
         DataFrame.from_numpy(mat).to_list(), [(1, 2), (3, 4), (5, 6)])
     self.assertEqual(DataFrame.from_numpy(mat[:, 0]).to_list(), [1, 3, 5])
     self.assertEqual(DataFrame.from_numpy(mat[:, 1]).to_list(), [2, 4, 6])
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")
Exemple #10
0
 def testNumpy(self):
     ampl = self.ampl
     try:
         import numpy as np
     except ImportError:
         return
     ampl.eval("set X;")
     arr = np.array([1, 2, 3])
     ampl.set["X"] = arr
     self.assertEqual(list(ampl.set["X"].members()), [1, 2, 3])
     ampl.eval("param p{1..3};")
     ampl.param["p"] = arr
     self.assertEqual(ampl.param["p"][2], 2)
     self.assertEqual(DataFrame.fromNumpy(arr).toList(), [1, 2, 3])
     mat = np.array([[1, 2], [3, 4], [5, 6]])
     self.assertEqual(
         DataFrame.fromNumpy(mat).toList(), [(1, 2), (3, 4), (5, 6)])
     self.assertEqual(DataFrame.fromNumpy(mat[:, 0]).toList(), [1, 3, 5])
     self.assertEqual(DataFrame.fromNumpy(mat[:, 1]).toList(), [2, 4, 6])
Exemple #11
0
    def testPandasNamedColumns(self):
        ampl = self.ampl
        try:
            import pandas as pd
        except ImportError:
            return
        df_unindexed = pd.DataFrame(
            [
                ["Apple", "Red", 3, 1.29],
                ["Apple", "Green", 9, 0.99],
                ["Pear", "Red", 25, 2.59],
                ["Pear", "Green", 26, 2.79],
                ["Lime", "Green", 99, 0.39],
            ],
            columns=["Fruit", "Color", "Count", "Price"],
        )
        # RangeIndex
        self.assertEqual(
            DataFrame.fromPandas(df_unindexed).getHeaders(),
            ("index0", "Fruit", "Color", "Count", "Price"),
        )

        # MultiIndex
        df_indexed = df_unindexed.set_index(["Fruit", "Color"])
        self.assertEqual(
            DataFrame.fromPandas(df_indexed,
                                 index_names=["Fruit", "Color"]).getHeaders(),
            ("Fruit", "Color", "Count", "Price"),
        )

        # Index without name
        df = pd.DataFrame(
            [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]],
            index=["First", "Second"],
            columns=[1, 2, 3, 4, 5],
        )
        self.assertEqual(
            DataFrame.fromPandas(df.stack()).getHeaders(),
            ("index0", "index1", "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
Exemple #13
0
 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)
Exemple #14
0
    def test_pandas_named_columns(self):
        if pd is None:
            self.skipTest("pandas not available")
        df_unindexed = pd.DataFrame(
            [
                ["Apple", "Red", 3, 1.29],
                ["Apple", "Green", 9, 0.99],
                ["Pear", "Red", 25, 2.59],
                ["Pear", "Green", 26, 2.79],
                ["Lime", "Green", 99, 0.39],
            ],
            columns=["Fruit", "Color", "Count", "Price"],
        )
        # RangeIndex
        self.assertEqual(
            DataFrame.from_pandas(df_unindexed).get_headers(),
            ("index0", "Fruit", "Color", "Count", "Price"),
        )

        # MultiIndex
        df_indexed = df_unindexed.set_index(["Fruit", "Color"])
        self.assertEqual(
            DataFrame.from_pandas(df_indexed,
                                  index_names=["Fruit",
                                               "Color"]).get_headers(),
            ("Fruit", "Color", "Count", "Price"),
        )

        # Index without name
        df = pd.DataFrame(
            [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]],
            index=["First", "Second"],
            columns=[1, 2, 3, 4, 5],
        )
        self.assertEqual(
            DataFrame.from_pandas(df.stack()).get_headers(),
            ("index0", "index1", "0"))
Exemple #15
0
 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)
Exemple #16
0
assign_set_data('DFFR_PRICE', dffr.DFFR_PRICE.values)
assign_set_data('DA_PRICE',  da.DA_PRICE.unique())

## Assign parameter data ##
ampl.getParameter('Cost').set(0)
ampl.getParameter('Ramp').set(999999)
ampl.getParameter('Ramp_DFFR').set(9999999)
ampl.getParameter('P_MAX').set(2)

n_DFFR_PRICE = len(dffr.DFFR_PRICE.unique())
n_DA_PRICE   = len(da.DA_PRICE.unique())
n_INTERVAL   = len(intervals)

df = pd.DataFrame([1/n_DFFR_PRICE] * n_DFFR_PRICE,
                  columns=['p_R'], index=dffr.DFFR_PRICE.values)
ampl.setData(DataFrame.fromPandas(df))

df = pd.DataFrame([1/n_DA_PRICE] * n_DA_PRICE,
                  columns=['p_DA'], index=da.DA_PRICE.unique())
ampl.setData(DataFrame.fromPandas(df))

df = dffr.set_index('DFFR_PRICE')
ampl.setData(DataFrame.fromPandas(df))

df = da.set_index(['DA_PRICE','INTERVALS'])
ampl.setData(DataFrame.fromPandas(df))

## Solve the model ##
# Set ampl options
settings = {
    'solver'         : 'cplexamp',
Exemple #17
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)
            '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()})
Exemple #19
0
def prodalloc(RID, SID, shared_ns=None, f_out=None, f_log=None):
    from amplpy import AMPL, DataFrame

    if shared_ns == None:
        (df_demand, wup_12mavg, ppp_sum12, df_scenario, sw_avail, df_penfunc,
         df_relcost) = pull_data(RID, SID)

    # =======================================
    # instantiate AMPL class and set AMPL options
    ampl = AMPL()

    # set options
    ampl.setOption('presolve', False)
    ampl.setOption('solver', 'gurobi_ampl')
    # ampl.setOption('solver', 'cbc')
    ampl.setOption('gurobi_options',
                   'iisfind=1 iismethod=1 lpmethod=4 mipgap=1e-6 warmstart=1')
    ampl.setOption('reset_initial_guesses', True)
    ampl.setOption('solver_msg', True)

    # real model from model file
    d_cur = os.getcwd()
    f_model = os.path.join(d_cur, 'model.amp')
    ampl.read(f_model)

    if shared_ns != None:
        df_demand = shared_ns.df_demand.query(
            f'RealizationID == {RID}').loc[:, ['wpda', 'dates', 'Demand']]
    dates = df_demand.loc[:, ['dates']].values
    df_demand.loc[:, 'dates'] = [
        pd.to_datetime(d).strftime('%Y-%b') for d in dates
    ]
    df_demand.set_index(keys=['wpda', 'dates'], inplace=True)
    wpda = sorted(list(set([w for (w, m) in df_demand.index])))
    monyr = df_demand.loc[('COT', ), ].index.values
    nyears = int(len(monyr) / 12.0)

    # lambda for determine number of days in a month
    dates = dates[0:(12 * nyears)]
    f_ndays_mo = lambda aday: (aday + dt.timedelta(days=32)).replace(day=1
                                                                     ) - aday
    ndays_mo = map(f_ndays_mo, [pd.to_datetime(d[0]) for d in dates])
    ndays_mo = [i.days for i in ndays_mo]

    # add data to sets -- Demand
    ampl.getParameter('nyears').value = nyears
    ampl.getSet('monyr').setValues(monyr)
    ampl.getSet('wpda').setValues(wpda)
    ampl.getParameter('demand').setValues(DataFrame.fromPandas(df_demand))
    ampl.getParameter('ndays_mo').setValues(ndays_mo)

    # index to year number
    yearno = [(i // 12) + 1 for i in range(len(dates))]
    ampl.getParameter('yearno').setValues(np.asarray(yearno))
    monthno = [pd.to_datetime(d[0]).month for d in dates]
    ampl.getParameter('monthno').setValues(np.asarray(monthno))

    # ---------------------------------------
    # WUP and preferred ranges
    if shared_ns != None:
        wup_12mavg = shared_ns.wup_12mavg
    ampl.getParameter('wup_12mavg').setValues(
        DataFrame.fromPandas(wup_12mavg.loc[:, ['wup_12mavg']]))
    ampl.getParameter('prod_range_lo').setValues(
        DataFrame.fromPandas(wup_12mavg.loc[:, ['prod_range_lo']]))
    ampl.getParameter('prod_range_hi').setValues(
        DataFrame.fromPandas(wup_12mavg.loc[:, ['prod_range_hi']]))

    # add ppp_sum12
    if shared_ns != None:
        ppp_sum12 = shared_ns.ppp_sum12
    ppp_sum12.loc[:, 'monyr'] = [i for i in range(1, 12)] * 3
    ppp_sum12.set_index(keys=['WF', 'monyr'], inplace=True)
    ampl.getParameter('ppp_sum12').setValues(DataFrame.fromPandas(ppp_sum12))

    # Relative cost of water per a million gallon
    if shared_ns != None:
        df_relcost = shared_ns.df_relcost
    ampl.getParameter('relcost').setValues(DataFrame.fromPandas(df_relcost))

    # ---------------------------------------
    # Scenario's data
    if shared_ns != None:
        df_scenario = shared_ns.df_scenario.query(
            f'ScenarioID=={SID}').loc[:, ['ParameterName', 'MonthNo', 'Value']]
    AVAIL_PCTILE = df_scenario.query(f"ParameterName == 'AVAIL_PCTILE'")
    AVAIL_PCTILE = AVAIL_PCTILE.loc[AVAIL_PCTILE.index, 'Value'].values[0]
    RES_INIT = df_scenario.query(f"ParameterName == 'RES_INIT'")
    RES_INIT = RES_INIT.loc[RES_INIT.index, 'Value'].values[0]
    # Surface Water Availability Data by month repeated for nyears
    ampl.getParameter('avail_pctile').value = AVAIL_PCTILE
    if shared_ns != None:
        sw_avail = shared_ns.sw_avail.query(
            f'Percentile == {AVAIL_PCTILE}'
        ).loc[:, ['source', 'monthno', 'value']]
    # sw_avail = temp.copy()
    # if nyears > 1:
    #     for i in range(1, nyears):
    #         sw_avail = sw_avail.append(temp)
    # srcs = sw_avail.loc[:, 'source'].unique()
    # for j in srcs:
    #     sw_avail.loc[sw_avail['source']==j,'monthno'] = [i+1 for i in range(len(monyr))]
    # sw_avail.set_index(keys=['source', 'monthno'], inplace=True)
    # ampl.getParameter('ngw_avail').setValues(DataFrame.fromPandas(sw_avail))
    sw_avail.set_index(keys=['source', 'monthno'], inplace=True)
    ampl.getParameter('ngw_avail').setValues(DataFrame.fromPandas(sw_avail))

    # ---------------------------------------
    # Penalty functions for under utilization
    if shared_ns != None:
        df_penfunc = shared_ns.df_penfunc
    ampl.getParameter('penfunc_x').setValues(
        DataFrame.fromPandas(df_penfunc.loc[:, ['under_limit']]))
    ampl.getParameter('penfunc_r').setValues(
        DataFrame.fromPandas(df_penfunc.loc[:, ['penalty_rate']]))
    '''
    # =======================================
    # Read fixed allocation from spreadsheet
    # f_excel = os.path.join(
    #     d_cur, 'WY 2019 monthly delivery and supply for budget InitialDraft.xlsx')
    sheet_names = ['WY 2019','WY 2020','WY 2021','WY 2022','WY 2023','WY 2024']
    # ch_poc: Central Hills delivery (row 16)
    # reg_lithia: Regional to Lithia (row 20)
    # reg_cot: Regional to City of Tampa (row 26)
    # reg_thic: THIC intertie purchase (row 37)
    # crw_prod: Carrollwood WF production (row 40)
    # eag_prod: Production for Eagle Well (row 41)

    # row index is zero based, minus one header row = -2
    row_offset = -2
    ch_poc, reg_lithia, reg_cot, reg_thic, crw_prod, eag_prod = [], [], [], [], [], []
    # These DV is fixed or receives values from other optimizer
    bud_fix, ds_fix, swtp_fix=[], [], []
    for i in range(nyears):
        df_excel = pd.read_excel(f_excel, sheet_names[i], usecols='C:N', nrows=41)
        ch_poc    .extend(list(df_excel.loc[16 + row_offset, :].values))
        reg_lithia.extend(list(df_excel.loc[20 + row_offset, :].values))
        reg_cot   .extend(list(df_excel.loc[26 + row_offset, :].values))
        reg_thic  .extend(list(df_excel.loc[37 + row_offset, :].values))
        crw_prod  .extend(list(df_excel.loc[40 + row_offset, :].values))
        eag_prod  .extend(list(df_excel.loc[41 + row_offset, :].values))
        
        bud_fix .extend(list(df_excel.loc[22 + row_offset, :].values))
        ds_fix  .extend(list(df_excel.loc[36 + row_offset, :].values))
        swtp_fix.extend(list(df_excel.loc[38 + row_offset, :].values))
    '''
    ch_poc = df_scenario[df_scenario.ParameterName == 'ch_poc'].Value
    reg_lithia = df_scenario[df_scenario.ParameterName == 'reg_lithia'].Value
    reg_cot = df_scenario[df_scenario.ParameterName == 'reg_cot'].Value
    reg_thic = df_scenario[df_scenario.ParameterName == 'reg_thic'].Value
    crw_prod = df_scenario[df_scenario.ParameterName == 'crw_prod'].Value
    eag_prod = df_scenario[df_scenario.ParameterName == 'eag_prod'].Value

    ampl.getParameter('ch_poc').setValues(np.asarray(ch_poc, dtype=np.float32))
    ampl.getParameter('reg_lithia').setValues(
        np.asarray(reg_lithia, dtype=np.float32))
    ampl.getParameter('reg_cot').setValues(
        np.asarray(reg_cot, dtype=np.float32))
    ampl.getParameter('reg_thic').setValues(
        np.asarray(reg_thic, dtype=np.float32))
    ampl.getParameter('crw_prod').setValues(
        np.asarray(crw_prod, dtype=np.float32))
    ampl.getParameter('eag_prod').setValues(
        np.asarray(eag_prod, dtype=np.float32))

    # overloaded function 'VariableInstance_fix' need float64
    bud_fix = np.asarray(
        df_scenario[df_scenario.ParameterName == 'bud_fix'].Value,
        dtype=np.float64)
    ds_fix = np.asarray(
        df_scenario[df_scenario.ParameterName == 'ds_fix'].Value,
        dtype=np.float64)
    swtp_fix = np.asarray(
        df_scenario[df_scenario.ParameterName == 'swtp_fix'].Value,
        dtype=np.float64)
    ampl.getParameter('bud_fix').setValues(
        np.asarray(bud_fix, dtype=np.float32))
    ampl.getParameter('ds_fix').setValues(np.asarray(ds_fix, dtype=np.float32))
    ampl.getParameter('swtp_fix').setValues(
        np.asarray(swtp_fix, dtype=np.float32))

    # ---------------------------------------
    # initialize/fix variable values
    ampl.getParameter('res_init').value = RES_INIT
    res_vol = ampl.getVariable('res_vol')
    res_vol[0].fix(RES_INIT)

    gw_prod = ampl.getVariable('gw_prod')
    bud_prod = [
        gw_prod[j, i] for ((j, i), k) in gw_prod.instances() if j == 'BUD'
    ]
    for i in range(len(bud_prod)):
        bud_prod[i].fix(bud_fix[i])

    ds_prod = ampl.getVariable('ds_prod')
    for i in range(ds_prod.numInstances()):
        ds_prod[i + 1].fix(ds_fix[i])

    swtp_prod = ampl.getVariable('swtp_prod')
    for i in range(swtp_prod.numInstances()):
        swtp_prod[i + 1].fix(swtp_fix[i])

    # ---------------------------------------
    # dump data
    with open('dump.dat', 'w') as f:
        with stdout_redirected(f):
            ampl.display('wpda,monyr')
            ampl.display('demand')
            ampl.display('nyears')
            ampl.display('ndays_mo,yearno,monthno')
            # ampl.display('years')
            # ampl.display('dem_total')
            ampl.display(
                'ch_poc,reg_lithia,reg_cot,reg_thic,crw_prod,eag_prod,bud_fix,ds_fix'
            )
            ampl.display('wup_12mavg')
            ampl.display('ppp_sum12')
            ampl.display('ngw_avail')
            ampl.display('prod_range_lo,prod_range_hi')
            ampl.display('relcost')
            ampl.display('penfunc_x,penfunc_r')

    # =======================================
    # silence solver
    with open('nul', 'w') as f:
        with stdout_redirected(f):
            ampl.solve()

    if f_out != None:
        with open(f_out, 'w') as f:
            print(r'# *** SOURCE ALLOCATION MODEL ****', file=f)
            print(r'# Monthly Delivery and Supply for Budgeting', file=f)
            print('\n# Objective: {}'.format(
                ampl.getObjective('mip_obj').value()),
                  file=f)

    if (f_log != None) & (ampl.getObjective('mip_obj').result() == 'solved'):
        with open(f_log, "w") as f:
            print('\n\nDump Variable and Constraint Values', file=f)
            with stdout_redirected(f):
                write_log(ampl)

    if ampl.getObjective('mip_obj').result() == 'infeasible':
        if f_log != None:
            with open(f_log, "w") as f:
                with stdout_redirected(f):
                    write_iis(ampl)
        else:
            write_iis(ampl)

    # =======================================
    # print output
    # groundwater production
    temp = ampl.getVariable('gw_prod').getValues().toPandas().join(
        ampl.getVariable('gw_under').getValues().toPandas()).join(
            ampl.getVariable('gw_over').getValues().toPandas())
    temp.columns = [i.replace('.val', '') for i in temp.columns]

    # pivoting df by source
    cwup_prod = temp.loc[[i for i in temp.index if i[0] == 'CWUP'], :].assign(
        index=monyr).set_index('index').rename(columns={
            'gw_prod': 'cwup_prod',
            'gw_under': 'cwup_under',
            'gw_over': 'cwup_over'
        })
    bud_prod = temp.loc[[i for i in temp.index if i[0] == 'BUD'], :].assign(
        index=monyr).set_index('index').rename(columns={
            'gw_prod': 'bud_prod',
            'gw_under': 'bud_under',
            'gw_over': 'bud_over'
        })
    sch_prod = temp.loc[[i for i in temp.index if i[0] == 'SCH'], :].assign(
        index=monyr).set_index('index').rename(columns={
            'gw_prod': 'sch_prod',
            'gw_under': 'sch_under',
            'gw_over': 'sch_over'
        })
    temp = cwup_prod.join(sch_prod.join(bud_prod))
    gw_results = temp

    temp_avg = temp.groupby(by=yearno).mean().reset_index()
    temp_avg = temp_avg.loc[:,
                            [i for i in temp_avg.columns[1:temp_avg.shape[1]]]]

    if f_out != None:
        with open(f_out, 'a') as f:
            # print heading
            print('\n\n# Monthly Groudwater Production', file=f)
            for l in range(len(temp)):
                if (l % 12) == 0:
                    print(('\n%10s' % 'Yr-Month') +
                          ('%11s' * len(temp.columns) % tuple(temp.columns)),
                          file=f)
                print(('%10s' % monyr[l]) + ('%11.3f' * len(temp.columns) %
                                             tuple(temp.iloc[l, :].values)),
                      file=f)
                if (l + 1) % 12 == 0:
                    print(('%10s' % 'Average') +
                          ('%11.3f' * len(temp_avg.columns) %
                           tuple(temp_avg.iloc[l // 12, :].values)),
                          file=f)
            print(
                ('\n%10s' % 'Total Avg') + ('%11.3f' * len(temp_avg.columns) %
                                            tuple(temp_avg.mean().values)),
                file=f)

    # ---------------------------------------
    # SWTP Production
    to_swtp = ampl.getVariable('to_swtp').getValues().toPandas()
    to_res = ampl.getVariable('to_res').getValues().toPandas()
    idx = to_swtp.index
    temp = ampl.getVariable('swtp_prod').getValues().toPandas()
    temp = temp.assign(tbc_swtp=to_swtp.loc[[i for i in idx
                                             if i[0] == 'TBC']].values)
    temp = temp.assign(
        alf_swtp=to_swtp.loc[[i for i in idx if i[0] == 'Alafia']].values)
    temp = temp.join(ampl.getVariable('res_eff').getValues().toPandas())
    temp = temp.assign(tbc_res=to_res.loc[[i for i in idx
                                           if i[0] == 'TBC']].values)
    temp = temp.assign(alf_res=to_res.loc[[i for i in idx
                                           if i[0] == 'Alafia']].values)
    temp = temp.join(ampl.getVariable('res_inf').getValues().toPandas()).join(
        ampl.getVariable('res_vol').getValues().toPandas())

    # Add availability columns
    temp = temp.assign(tbc_avail=sw_avail.loc[[('TBC', i) for i in monthno],
                                              ['value']].values)
    temp = temp.assign(alf_avail=sw_avail.loc[[('Alafia', i) for i in monthno],
                                              ['value']].values)

    # Add SW withdraws
    df1 = ampl.getVariable('sw_withdraw').getValues().toPandas()
    idx = temp.index
    temp = temp.assign(tbc_wthdr=df1.loc[[('TBC', i) for i in idx], :].values)
    temp = temp.assign(alf_wthdr=df1.loc[[('Alafia', i)
                                          for i in idx], :].values)
    temp.columns = [i.replace('.val', '') for i in temp.columns]
    sw_results = temp

    # Compute annual average
    temp_avg = temp.groupby(by=yearno).mean().reset_index()
    temp_avg = temp_avg.loc[:,
                            [i for i in temp_avg.columns[1:temp_avg.shape[1]]]]

    if f_out != None:
        with open(f_out, 'a') as f:
            # print heading
            print('\n\n# Monthly Surface Water Production', file=f)
            for l in range(len(temp)):
                if (l % 12) == 0:
                    print(('\n%10s' % 'Yr-Month') +
                          ('%10s' * len(temp.columns) % tuple(temp.columns)),
                          file=f)
                print(('%10s' % monyr[l]) + ('%10.3f' * len(temp.columns) %
                                             tuple(temp.loc[float(l + 1), :])),
                      file=f)
                if ((l + 1) % 12) == 0:
                    print(('%10s' % 'Average') +
                          ('%10.3f' * len(temp_avg.columns) %
                           tuple(temp_avg.loc[l // 12, :])),
                          file=f)
            print(
                ('\n%10s' % 'Total Avg') + ('%10.3f' * len(temp_avg.columns) %
                                            tuple(temp_avg.mean().values)),
                file=f)

    # ---------------------------------------
    # print multi objective values
    temp = ampl.getVariable('prodcost_avg').getValues().toPandas()
    idx = [i for i in temp.index]
    temp = temp.assign(
        prod_avg=temp.loc[:, 'prodcost_avg.val'] * 1e-3 /
        np.asarray([df_relcost.loc[i[0], 'relcost'] for i in idx]))

    temp = temp.join(
        ampl.getVariable('uu_penalty').getValues().toPandas()).join(
            ampl.getVariable('uu_avg').getValues().toPandas())
    temp.columns = [i.replace('.val', '') for i in temp.columns]
    temp_avg = temp.groupby([i[0] for i in temp.index]).mean().reset_index()

    if f_out != None:
        with open(f_out, 'a') as f:
            print(
                '\n\n# Annual Production and Under Utilization (Opportunity) Costs',
                file=f)
            for l in range(len(temp)):
                if (l % nyears) == 0:
                    print(('\n%10s%10s' % ('YearNo', 'Source')) +
                          ('%15s' * len(temp.columns) % tuple(temp.columns)),
                          file=f)
                print(('%10d%10s' % (idx[l][1], idx[l][0])) +
                      ('%15.3f' * len(temp.columns) %
                       tuple(temp.loc[[idx[l]], :].values[0])),
                      file=f)
                if ((l + 1) % nyears) == 0:
                    print(('%10s' % 'Average') +
                          (('%10s' + '%15.3f' * len(temp.columns)) %
                           tuple(temp_avg.iloc[l // nyears, :])),
                          file=f)

    ampl.close()

    # prepare data for ploting
    if f_out != None:
        df_plotdata = df_demand.groupby(level=1).sum().join(df_demand.loc[(
            'COT',
        ), ['Demand']].rename(columns={'Demand': 'COT'})).assign(
            TBW_Demand=lambda x: x.Demand - x.COT).loc[:, ['TBW_Demand']].join(
                gw_results.join(sw_results.set_index(gw_results.index)))
        monyr = [pd.Timestamp(i + '-01') for i in df_plotdata.index]
        df_plotdata = df_plotdata.assign(
            Dates=monyr).set_index('Dates').sort_index()
        df_plotdata = df_plotdata.assign(ndays_mo=ndays_mo)

        plot_results(SID, AVAIL_PCTILE, df_plotdata, f_out)
Exemple #20
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'))
        """

        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
Exemple #21
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
Exemple #22
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
Exemple #23
0
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)
Exemple #24
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)
Exemple #25
0
 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())
Exemple #26
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
Exemple #27
0
 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)])
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
Exemple #29
0
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)
Exemple #30
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']