示例#1
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'))
示例#2
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]))
示例#3
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]))
示例#4
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"))
示例#5
0
文件: main.py 项目: AliHeggie/Upside
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',
示例#6
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)