Exemple #1
0
def main(argc, argv):
    ampl = AMPL(Environment(r'C:\Users\etjones\Desktop\AI OPS\AMPL\amplide.mswin64'))
    os.chdir(os.path.dirname(__file__) or os.curdir)
    try:
        if argc > 1:
            ampl.setOption('solver', argv[1])

        # Read the model and data files.
        #modelDirectory = argv[2] if argc == 3 else os.path.join('..', 'models')
        #ampl.read(os.path.join(modelDirectory, 'diet/diet.mod'))
        #ampl.readData(os.path.join(modelDirectory, 'diet/diet.dat'))
        ampl.read(r'C:\Users\etjones\Desktop\AI OPS\AMPL\custom_models\transp.mod')
        ampl.readData(r'C:\Users\etjones\Desktop\AI OPS\AMPL\custom_models\transp.dat')

        # Solve
        print('\n' + "AMPL MODEL:" + '\n')
        ampl.solve()
        totalcost = ampl.getObjective('Total_Cost')
        print("Minimum Cost:", totalcost.value())
        print('\n' + "Optimal Flow:")
        ampl.display('Trans')
        print('\n' + "Compare pdf flow to the above table to confirm optmial flows")
    except Exception as e:
        print(e)
        raise
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 #3
0
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 11 17:20:23 2018

@author: donja
"""

from amplpy import AMPL, Environment

ampl = AMPL(Environment('D:\\amplide.mswin64\\amplide.mswin64'))
ampl.option['solver'] = 'cplexamp'
ampl.read('example.mod')  #read the model
ampl.eval('objective z; solve;')
ampl.display('z', 'roth', 'traditional', 'surplus')

pstring = """
objective function = {z}
bi-weekly roth contribution = ${roth}
bi-weekly traditional contribution = ${traditional}
surplus (amount left after optimal allocation) = ${surplus}
total (without surplus)  = ${total}
total (with surplus)  = ${totalS}
"""
z = ampl.getValue('z')
roth = ampl.getValue('roth')
traditional = ampl.getValue('traditional')
surplus = ampl.getValue('surplus')

annual_roth_amt = roth * 12 * 2
annual_traditional_amt = traditional * 12 * 2
Exemple #4
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 #5
0
def main(argc, argv):
    # You can install amplpy with "python -m pip install amplpy"
    from amplpy import AMPL

    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'))
    """

    if argc > 1:
        # ampl.set_option('solver', argv[1])
        pass

    # Must be solved with a solver supporting the suffix dunbdd
    ampl.set_option("solver", "cplex")
    ampl.set_option("presolve", False)
    ampl.set_option("omit_zero_rows", False)

    model_directory = os.path.join(
        argv[2] if argc == 3 else os.path.join("..", "models"), "locationtransportation"
    )

    # Load the AMPL model from file
    ampl.read(os.path.join(model_directory, "trnloc2.mod"))
    # Read data
    ampl.read_data(os.path.join(model_directory, "trnloc.dat"))

    # Get references to AMPL's model entities for easy access.
    ship_cost = ampl.get_objective("Ship_Cost")
    max_ship_cost = ampl.get_variable("Max_Ship_Cost")
    build_var = ampl.get_variable("Build")
    supply = ampl.get_constraint("Supply")
    demand = ampl.get_constraint("Demand")
    num_cut_param = ampl.get_parameter("nCUT")
    cut_type = ampl.get_parameter("cut_type")
    build_param = ampl.get_parameter("build")
    supply_price = ampl.get_parameter("supply_price")
    demand_price = ampl.get_parameter("demand_price")

    num_cut_param.set(0)
    max_ship_cost.set_value(0)
    build_param.set_values([1] * ampl.get_set("ORIG").size())

    num_cuts = 0
    while True:
        num_cuts += 1
        print("Iteration {}".format(num_cuts))
        ampl.display("build")
        # Solve the subproblem.
        ampl.eval("solve Sub;")
        result = ship_cost.result()
        if result == "infeasible":
            # Add a feasibility cut.
            num_cut_param.set(num_cuts)
            cut_type.set(num_cuts, "ray")
            for index, value in supply.get_values(["dunbdd"]):
                supply_price[index, num_cuts] = value
            for index, value in demand.get_values(["dunbdd"]):
                demand_price[index, num_cuts] = value
        elif ship_cost.value() > max_ship_cost.value() + 0.00001:
            # Add an optimality cut.
            num_cut_param.set(num_cuts)
            cut_type.set(num_cuts, "point")
            ampl.set_option("display_1col", 0)
            ampl.display("Ship")
            for index, value in supply.get_values():
                supply_price[index, num_cuts] = value
            for index, value in demand.get_values():
                demand_price[index, num_cuts] = value
        else:
            break
        # Re-solve the master problem.
        print("RE-SOLVING MASTER PROBLEM")
        ampl.eval("solve Master;")
        solve_result = ampl.get_value("solve_result")
        if solve_result != "solved":
            raise Exception("Failed to solve (solve_result: {})".format(solve_result))
        # Copy the data from the Build variable used in the master problem
        # to the build parameter used in the subproblem.
        build_param.set_values(build_var.get_values())
    print("\nProcedure completed in {} iterations\n".format(num_cuts))
    ampl.display("Ship")
Exemple #6
0
                        ampl.param['T'][a] = param_t['t'][d_type['date_type'][w,d],a]  # Changes in parameter T[a](appliance time to operate) based on day d
                        ampl.param['W'][a] = param_w['w'][d_type['date_type'][w,d],a]  # Changes in parameter W[a](priority of appliance) based on day d
                  
                    ampl.param['W'][1] = priority_Washer   # weekly required operation : update priority of washer
                    ampl.param['W'][2] = priority_Dryer   # weekly required operation : update priority of dryer
                    ampl.param['T'][1] = opt_time_Washer  # weekly required operation : update operation time of washer
                    ampl.param['T'][2] = opt_time_Dryer  # weekly required operation : update operation time of dryer
                          
                    print("")                
                    print("Type of day {d} of week {w} : ".format(d=d, w=w), ampl.getValue('DATE[{w},{d}]'.format(w=w, d=d)))  
                    print("PV size :", ampl_rpv*ampl_apv, "kW", "/", "Battery capacity :", x_bat, "kWh")                            
                    print("SISC for day {d} of week {w} : ".format(d=d, w=w), ampl.getValue('SISC'))                                                 
   
                    ampl.solve()  # solving the model : obtain pre-schedules of applainces operation based on the forecasted solar irradiance data

                    ampl.display('sbat[24]*CBAT;') # pre-schedules of applainces operation

                    daily_energy_generated_forecasted [w,d] = ampl.getValue ('sum{t in Time} EPV[t];')
                    yearly_energy_generated_forecasted = yearly_energy_generated_forecasted  + daily_energy_generated_forecasted [w,d]  # yearly energy generated by pv system from AMPL model.

                    daily_energy_demand_forecasted [w,d] = ampl.getValue ('sum{a in Appliance} EAPP[a] * T[a] + sum{t in Time} EM[t];') # yearly energy demand -> since it doesn't consider operation of weekly appliances, it might be greater than actual.      
                    yearly_energy_demand_forecasted  = yearly_energy_demand_forecasted  + daily_energy_demand_forecasted [w,d] # yearly energy demand -> since it doesn't consider operation of weekly appliances, it might be greater than actual.      
 
                    daily_energy_appliance_total_forecasted [w,d] = ampl.getValue ('sum{t in Time, a in Appliance} EAPP[a] * xapp_state[a,t] + sum{t in Time} EM[t];')
                    yearly_energy_appliance_total_forecasted  = yearly_energy_appliance_total_forecasted  + daily_energy_appliance_total_forecasted [w,d] # yearly energy used by appliances -> since it doesn't consider operation of weekly appliances, it might be greater than actual.      

                    daily_energy_appliance_pv_forecasted [w,d] = ampl.getValue('sum{t in Time} eapp_pv[t];')
                    yearly_energy_appliance_pv_forecasted  = yearly_energy_appliance_pv_forecasted  + daily_energy_appliance_pv_forecasted [w,d]  # yearly energy used by appliance from pv                

                    daily_energy_appliance_battery_forecasted [w,d] = ampl.getValue ('sum{t in Time} eapp_bat[t];')  
                    yearly_energy_appliance_battery_forecasted  = yearly_energy_appliance_battery_forecasted  + daily_energy_appliance_battery_forecasted [w,d]  # yearly energy used by appliance from battery