示例#1
0
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 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)
示例#3
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)
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
示例#5
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
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
示例#7
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)
示例#8
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)
示例#9
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
示例#10
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
示例#11
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