Beispiel #1
0
def main():
    alfalfa = AlfalfaClient(url='http://localhost')

    # Winter
    start_time = datetime.datetime(2019, 2, 6, 9, 00, 0)
    simu_length = datetime.timedelta(hours=10)
    end_time = start_time + simu_length
    simu_steps = int(simu_length.total_seconds() / 60.0)  # number of time steps, of which each timestep is 1 minute
    simu_steps = 30
    print(
        f'start {start_time.strftime("%m/%d/%Y %H:%M:%S")}, end {end_time.strftime("%m/%d/%Y %H:%M:%S")} with {simu_steps} total steps'  # noqa
    )

    # Submit only one file
    files = [os.path.join(os.path.dirname(__file__), 'openstudio_model', 'SmallOffice_Unitary_1.osm')]
    # files = [os.path.join(os.path.dirname(__file__), 'openstudio_model', 'SmallOffice_VAV_1.osm')]
    siteids = alfalfa.submit_many(files)
    alfalfa.start_many(siteids, external_clock=True, start_datetime=start_time, end_datetime=end_time)

    history = {
        'timestamp': [],
        'T1': [],
        'T2': [],
        'T3': [],
        'T4': [],
        'T5': [],
        'RadTemp1': [],
        'RadTemp2': [],
        'RadTemp3': [],
        'RadTemp4': [],
        'RadTemp5': [],
        'u1': [],
        'u2': [],
        'u3': [],
        'u4': [],
        'u5': [],
        'ZN1_Cooling_Rate': [],
        'ZN2_Cooling_Rate': [],
        'ZN3_Cooling_Rate': [],
        'ZN4_Cooling_Rate': [],
        'ZN5_Cooling_Rate': [],
        'ZN1_Heating_Rate': [],
        'ZN2_Heating_Rate': [],
        'ZN3_Heating_Rate': [],
        'ZN4_Heating_Rate': [],
        'ZN5_Heating_Rate': [],
        'vdot1': [],
        'vdot2': [],
        'vdot3': [],
        'vdot4': [],
        'vdot5': [],
        'Tsetpoint_cooling': [],
        'Tsetpoint_heating': [],
    }

    # Initialize the flow control to random values
    flow = [1, 1, 1, 1, 1]
    # dual band thermostat
    heating_setpoint = 21
    cooling_setpoint = 25

    for i in range(simu_steps):
        print(f"Simulation step: {i}")
        alfalfa.advance(siteids)
        new_inputs = {}
        state_vars = []
        for siteid in siteids:
            model_outputs = alfalfa.outputs(siteid)

            current_time = start_time + datetime.timedelta(minutes=i)
            history['timestamp'].append(current_time.strftime('%m/%d/%Y %H:%M:%S'))

            # get zone mean air temperature
            temp_core = model_outputs["Core_ZN ZN_Zone Mean Air Temperature"]
            temp_p1 = model_outputs["Perimeter_ZN_1 ZN_Zone Mean Air Temperature"]
            temp_p2 = model_outputs["Perimeter_ZN_2 ZN_Zone Mean Air Temperature"]
            temp_p3 = model_outputs["Perimeter_ZN_3 ZN_Zone Mean Air Temperature"]
            temp_p4 = model_outputs["Perimeter_ZN_4 ZN_Zone Mean Air Temperature"]
            history['T1'].append(temp_core)
            history['T2'].append(temp_p1)
            history['T3'].append(temp_p2)
            history['T4'].append(temp_p3)
            history['T5'].append(temp_p4)
            history['RadTemp1'].append(model_outputs["Core_ZN ZN_Zone Mean Radiant Temperature"])
            history['RadTemp2'].append(model_outputs["Perimeter_ZN_1 ZN_Zone Mean Radiant Temperature"])
            history['RadTemp3'].append(model_outputs["Perimeter_ZN_2 ZN_Zone Mean Radiant Temperature"])
            history['RadTemp4'].append(model_outputs["Perimeter_ZN_3 ZN_Zone Mean Radiant Temperature"])
            history['RadTemp5'].append(model_outputs["Perimeter_ZN_4 ZN_Zone Mean Radiant Temperature"])
            state_vars.append(temp_core)
            state_vars.append(temp_p1)
            state_vars.append(temp_p2)
            state_vars.append(temp_p3)
            state_vars.append(temp_p4)

            print(f"statevars: core/p1/p2/p3/p4: {temp_core}/{temp_p1}/{temp_p2}/{temp_p3}/{temp_p4}")

            # get zone cooling/heating rate
            cooling_core = model_outputs["Core_ZN ZN_Zone Air System Sensible Cooling Rate"]
            heating_core = model_outputs["Core_ZN ZN_Zone Air System Sensible Heating Rate"]
            cooling_p1 = model_outputs["Perimeter_ZN_1 ZN_Zone Air System Sensible Cooling Rate"]
            heating_p1 = model_outputs["Perimeter_ZN_1 ZN_Zone Air System Sensible Heating Rate"]
            cooling_p2 = model_outputs["Perimeter_ZN_2 ZN_Zone Air System Sensible Cooling Rate"]
            heating_p2 = model_outputs["Perimeter_ZN_2 ZN_Zone Air System Sensible Heating Rate"]
            cooling_p3 = model_outputs["Perimeter_ZN_3 ZN_Zone Air System Sensible Cooling Rate"]
            heating_p3 = model_outputs["Perimeter_ZN_3 ZN_Zone Air System Sensible Heating Rate"]
            cooling_p4 = model_outputs["Perimeter_ZN_4 ZN_Zone Air System Sensible Cooling Rate"]
            heating_p4 = model_outputs["Perimeter_ZN_4 ZN_Zone Air System Sensible Heating Rate"]
            history['ZN1_Cooling_Rate'].append(cooling_core)
            history['ZN2_Cooling_Rate'].append(cooling_p1)
            history['ZN3_Cooling_Rate'].append(cooling_p2)
            history['ZN4_Cooling_Rate'].append(cooling_p3)
            history['ZN5_Cooling_Rate'].append(cooling_p4)
            history['ZN1_Heating_Rate'].append(heating_core)
            history['ZN2_Heating_Rate'].append(heating_p1)
            history['ZN3_Heating_Rate'].append(heating_p2)
            history['ZN4_Heating_Rate'].append(heating_p3)
            history['ZN5_Heating_Rate'].append(heating_p4)

            print(f"cooling rate: core/p1/p2/p3/p4: {cooling_core}/{cooling_p1}/{cooling_p2}/{cooling_p3}/{cooling_p4}")
            print(f"heating rate: core/p1/p2/p3/p4: {heating_core}/{heating_p1}/{heating_p2}/{heating_p3}/{heating_p4}")

            temps = [temp_core, temp_p1, temp_p2, temp_p3, temp_p4]
            # manually implement seasonal reset
            run_cooling_data_start = datetime.datetime(2019, 4, 15, 0, 00, 0)
            run_cooling_data_end = datetime.datetime(2019, 12, 30, 0, 00, 0)

            if current_time > run_cooling_data_start and current_time < run_cooling_data_end:
                flow = pid_control(temps, flow, cooling_setpoint)
            else:
                flow = pid_control(temps, flow, heating_setpoint)

            print(f"new control inputs: core/p1/p2/p3/p4: {flow[0]}/{flow[1]}/{flow[2]}/{flow[3]}/{flow[4]}")
            history['u1'].append(flow[0])
            history['u2'].append(flow[1])
            history['u3'].append(flow[2])
            history['u4'].append(flow[3])
            history['u5'].append(flow[4])

            history['vdot1'].append(flow[0])
            history['vdot2'].append(flow[1])
            history['vdot3'].append(flow[2])
            history['vdot4'].append(flow[3])
            history['vdot5'].append(flow[4])

            # model_inputs = alfalfa.inputs(siteid)

            # new_inputs must be dictionary format
            new_inputs["SA_FlowRate_Zone_Core_CMD"] = flow[0]
            new_inputs["SA_FlowRate_Zone_P1_CMD"] = flow[1]
            new_inputs["SA_FlowRate_Zone_P2_CMD"] = flow[2]
            new_inputs["SA_FlowRate_Zone_P3_CMD"] = flow[3]
            new_inputs["SA_FlowRate_Zone_P4_CMD"] = flow[4]

            # here the setpoints are dummy, for test only
            new_inputs["Cooling_Setpoint_CMD"] = cooling_setpoint
            new_inputs["Heating_Setpoint_CMD"] = heating_setpoint
            history['Tsetpoint_cooling'].append(cooling_setpoint)
            history['Tsetpoint_heating'].append(heating_setpoint)

            alfalfa.setInputs(siteid, new_inputs)

        # throttle the requests a bit
        time.sleep(0.01)

    alfalfa.stop_many(siteids)

    # storage for results
    file_basename = os.path.splitext(os.path.basename(__file__))[0]
    result_dir = f'results_{file_basename}'
    os.makedirs(result_dir, exist_ok=True)
    history_df = pd.DataFrame.from_dict(history)
    print(history_df)
    history_df.to_csv(f'{result_dir}/{file_basename}.csv')
Beispiel #2
0
def main():
    alfalfa = AlfalfaClient(url='http://localhost')

    # Denver weather
    # 1/1/2019 00:00:00  - Note that we have to start at 1/1 right now.
    # beg_time = datetime.datetime(2019, 1, 1, 0, 0, 0)
    start_time = datetime.datetime(2019, 1, 2, 0, 0, 0)
    end_time = datetime.datetime(2019, 1, 3, 0, 0, 0)

    step = 300  # 5 minutes
    sim_steps = int((end_time - start_time).total_seconds() /
                    step)  # total time (in seconds) / 5 minute steps

    heating_setpoint = 21
    cooling_setpoint = 25
    u = initialize_control(heating_setpoint, cooling_setpoint)

    file = os.path.join(os.path.dirname(__file__), 'fmus', 'single_zone_vav',
                        'wrapped.fmu')
    print(f"Uploading test case {file}")
    site = alfalfa.submit(file)

    print('Starting simulation')
    # all simulations start at time = 0 right now.
    alfalfa.start(
        site,
        external_clock=True,
        end_datetime=end_time,
    )

    historian = Historian(5)
    historian.add_point('timestamp', 'Time', None)
    historian.add_point('T1', 'degC', 'TRooAir_y', f_conversion=deg_k_to_c)
    historian.add_point('T1_Rad', 'degC', 'TRooRad_y', f_conversion=deg_k_to_c)
    historian.add_point('Toutdoor',
                        'degC',
                        'TOutdoorDB_y',
                        f_conversion=deg_k_to_c)
    historian.add_point('CoolingPower', 'W', 'PCoo_y')
    historian.add_point('HeatingPower', 'W', 'PHea_y')
    historian.add_point('FanPower', 'W', 'PFan_y')
    historian.add_point('PumpPower', 'W', 'PPum_y')
    historian.add_point('TotalHVACPower', 'W', 'power')
    historian.add_point(
        'TotalHVACEnergy', 'Ws',
        'ECumuHVAC_y')  # I think this is in Watt-seconds! (sorry)
    historian.add_point('HeatingSetpoint',
                        '',
                        'senTSetRooHea_y',
                        f_conversion=deg_k_to_c)
    historian.add_point('CoolingSetpoint',
                        '',
                        'senTSetRooCoo_y',
                        f_conversion=deg_k_to_c)
    historian.add_point('FanControlInput', '', 'senUSetFan_y')
    historian.add_point('u_CoolingSetpoint',
                        '',
                        'oveTSetRooCoo_u',
                        f_conversion=deg_k_to_c)
    historian.add_point('u_HeatingSetpoint',
                        '',
                        'oveTSetRooHea_u',
                        f_conversion=deg_k_to_c)
    historian.add_point('u_FanOverride', '', 'oveUSetFan_u')
    historian.add_point('PMV', '', 'pmv')
    historian.add_point('PPD', '', 'ppd')
    historian.add_point('Reward', '', 'reward')
    print('Stepping through time')
    for i in range(sim_steps):
        current_time = start_time + datetime.timedelta(seconds=(i * step))
        alfalfa.setInputs(site, u['u'])
        alfalfa.advance([site])
        model_outputs = alfalfa.outputs(site)
        sys.stdout.flush()

        reward_scalar, all_rewards = compute_rewards(model_outputs,
                                                     current_time)
        historian.add_data(all_rewards)

        # if datetime.time(8, 00) < current_time.time() < datetime.time(18, 00):
        #     heating_setpoint = 21 + 273.15
        #     cooling_setpoint = 25 + 273.15
        # else:
        #     heating_setpoint = 16 + 273.15
        #     cooling_setpoint = 29 + 273.15
        u = compute_control(model_outputs, reward_scalar, current_time,
                            heating_setpoint, cooling_setpoint)
        historian.add_data(u['historian'])

        print(f'Running time: {current_time.strftime("%m/%d/%Y %H:%M:%S")}')
        historian.add_datum('timestamp', current_time)
        historian.add_data(model_outputs)

        # throttle the requests a bit
        time.sleep(0.05)

    alfalfa.stop(site)

    # storage for results
    file_basename = os.path.splitext(os.path.basename(__file__))[0]
    result_dir = f'results_{file_basename}'
    historian.save_csv(result_dir, f'{file_basename}.csv')
    historian.save_pickle(result_dir, f'{file_basename}.pkl')
    print(historian.to_df().describe())
    kpis = historian.evaluate_performance()
    with open(f'{result_dir}/kpis_result.json', 'w') as f:
        f.write(json.dumps(kpis, indent=2))
    print(kpis)
def main():
    alfalfa = AlfalfaClient(url='http://localhost')

    start_time = datetime.datetime(2019, 2, 6, 9, 00, 0)
    end_time = datetime.datetime(2019, 2, 10, 9, 00, 0)
    length = 48 * 3600  # 48 hours
    step = 300  # 5 minutes -- this may be hard coded in step_fmu.py
    u = initialize_control()
    heating_setpoint = 21
    # cooling_setpoint = 25

    file = os.path.join(os.path.dirname(__file__), 'fmus', 'simple_1_zone_heating', 'simple_1_zone_heating.fmu')
    print(f"Uploading test case {file}")
    site = alfalfa.submit(file)

    print('Starting simulation')
    alfalfa.start(
        site,
        start_time=start_time,
        end_time=end_time,
        external_clock=True,
    )

    history = {
        'timestamp': [],
        'T1': [],
        # 'T2': [],
        # 'T3': [],
        # 'T4': [],
        # 'T5': [],
        # 'RadTemp1': [],
        # 'RadTemp2': [],
        # 'RadTemp3': [],
        # 'RadTemp4': [],
        # 'RadTemp5': [],
        'u1': [],
        'u2': [],
        # 'u3': [],
        # 'u4': [],
        # 'u5': [],
        # 'ZN1_Cooling_Rate': [],
        # 'ZN2_Cooling_Rate': [],
        # 'ZN3_Cooling_Rate': [],
        # 'ZN4_Cooling_Rate': [],
        # 'ZN5_Cooling_Rate': [],
        'ZN1_Heating_Rate': [],
        # 'ZN2_Heating_Rate': [],
        # 'ZN3_Heating_Rate': [],
        # 'ZN4_Heating_Rate': [],
        # 'ZN5_Heating_Rate': [],
        # 'vdot1': [],
        # 'vdot2': [],
        # 'vdot3': [],
        # 'vdot4': [],
        # 'vdot5': [],
        # 'Tsetpoint_cooling': [],
        'Tsetpoint_heating': [],
    }

    # Initialize the flow control to random values
    # flow = [1, 1, 1, 1, 1]
    # dual band thermostat

    print('Stepping through time')
    for i in range(int(length / step)):
        alfalfa.setInputs(site, u)
        alfalfa.advance([site])
        model_outputs = alfalfa.outputs(site)
        # print(u)
        # print(model_outputs)
        sys.stdout.flush()
        u = compute_control(model_outputs, heating_setpoint)

        # current_time = start_time + datetime.timedelta(minutes=i)
        # history['timestamp'].append(current_time.strftime('%m/%d/%Y %H:%M:%S'))
        # get zone mean air temperature
        # get zone cooling/heating rate
        # throttle the requests a bit
        current_time = start_time + datetime.timedelta(seconds=(i * step))
        print(f'Running time: {current_time.strftime("%m/%d/%Y %H:%M:%S")}')
        history['timestamp'].append(current_time)
        history['T1'].append(model_outputs['TRooAir_y'] - 273)
        history['ZN1_Heating_Rate'].append(model_outputs['PHea_y'])
        history['u1'].append(u['oveAct_u'])
        history['u2'].append(u['oveAct_activate'])
        history['Tsetpoint_heating'].append(heating_setpoint)
        time.sleep(0.01)

    alfalfa.stop(site)

    # storage for results
    file_basename = os.path.splitext(os.path.basename(__file__))[0]
    result_dir = f'results_{file_basename}'
    os.makedirs(result_dir, exist_ok=True)
    history_df = pd.DataFrame.from_dict(history)
    print(history_df)
    history_df.to_csv(f'{result_dir}/{file_basename}.csv')
def main():
    alfalfa = AlfalfaClient(url='http://localhost')

    # Denver weather
    # 1/1/2019 00:00:00  - Note that we have to start at 1/1 right now.
    start_time = datetime.datetime(2019, 1, 1, 0, 0, 0)
    end_time = datetime.datetime(2019, 1, 2, 0, 0, 0)

    step = 300  # 5 minutes
    sim_steps = int((end_time - start_time).total_seconds() / step)  # total time (in seconds) / 5 minute steps

    heating_setpoint = 21
    cooling_setpoint = 25
    u = initialize_control(heating_setpoint, cooling_setpoint)

    file = os.path.join(os.path.dirname(__file__), 'fmus', 'single_zone_vav', 'wrapped.fmu')
    print(f"Uploading test case {file}")
    site = alfalfa.submit(file)

    print('Starting simulation')
    alfalfa.start(site, external_clock=True, end_datetime=end_time)

    historian = Historian(5)
    historian.add_point('timestamp', 'Time', None)
    historian.add_point('T1', 'degC', 'TRooAir_y', f_conversion=deg_k_to_c)
    historian.add_point('T1_Rad', 'degC', 'TRooRad_y', f_conversion=deg_k_to_c)
    historian.add_point('Toutdoor', 'degC', 'TOutdoorDB_y', f_conversion=deg_k_to_c)
    historian.add_point('CoolingPower', 'W', 'PCoo_y')
    historian.add_point('HeatingPower', 'W', 'PHea_y')
    historian.add_point('FanPower', 'W', 'PFan_y')
    historian.add_point('PumpPower', 'W', 'PPum_y')
    historian.add_point('TotalHVACPower', 'W', 'power')
    historian.add_point('TotalHVACEnergy', 'Ws', 'ECumuHVAC_y')  # I think this is in Watt-seconds! (sorry)
    historian.add_point('HeatingSetpoint', '', 'senTSetRooHea_y', f_conversion=deg_k_to_c)
    historian.add_point('CoolingSetpoint', '', 'senTSetRooCoo_y', f_conversion=deg_k_to_c)
    historian.add_point('FanControlInput', '', 'senUSetFan_y')
    historian.add_point('u_CoolingSetpoint', '', 'oveTSetRooCoo_u', f_conversion=deg_k_to_c)
    historian.add_point('u_HeatingSetpoint', '', 'oveTSetRooHea_u', f_conversion=deg_k_to_c)
    historian.add_point('u_FanOverride', '', 'oveUSetFan_u')
    historian.add_point('PMV', '', 'pmv')
    historian.add_point('PPD', '', 'ppd')
    historian.add_point('Reward', '', 'reward')

    # Initialize the flow control to random values
    # flow = [1, 1, 1, 1, 1]
    # dual band thermostat

    print('Stepping through time')

    # initialize the first state
    current_state = np.array([[21.2, 0, 0]])

    # initialize the exploration term
    exploration = 0.01
    exploration_dot = (exploration - 0.001) / sim_steps

    for i in range(sim_steps):
        current_time = start_time + datetime.timedelta(seconds=(i * step))

        # compute action
        actor_mean = actor_network().predict(current_state)
        u = float(action_flowrate(actor_mean, exploration))

        exploration = exploration - exploration_dot  # decrease exploration gradually per time step

        alfalfa.setInputs(site, {'oveUSetFan_u': u})
        alfalfa.advance([site])
        model_outputs = alfalfa.outputs(site)

        next_state = states(model_outputs, current_time)  # get the next state

        # print(u)
        # print(model_outputs)
        sys.stdout.flush()

        reward, all_rewards = compute_rewards(model_outputs, current_time)  # get the current cost

        train_model(current_state, next_state, reward)

        current_state = next_state

        historian.add_data(all_rewards)

        # u = compute_control(model_outputs, costs, current_time, heating_setpoint, cooling_setpoint)
        historian.add_data({
            'oveTSetRooHea_u': heating_setpoint + 273.15,
            'oveTSetRooCoo_u': cooling_setpoint + 273.15,
            'oveUSetFan_u': u,
        })

        # current_time = start_time + datetime.timedelta(minutes=i)
        # history['timestamp'].append(current_time.strftime('%m/%d/%Y %H:%M:%S'))

        print(f'Running time: {current_time.strftime("%m/%d/%Y %H:%M:%S")}')
        historian.add_datum('timestamp', current_time)
        historian.add_data(model_outputs)

        # throttle the requests a bit
        time.sleep(0.05)

    alfalfa.stop(site)

    # storage for results
    file_basename = os.path.splitext(os.path.basename(__file__))[0]
    result_dir = f'results_{file_basename}'
    historian.save_csv(result_dir, f'{file_basename}.csv')
    historian.save_pickle(result_dir, f'{file_basename}.pkl')
    print(historian.to_df().describe())
    kpis = historian.evaluate_performance()
    with open(f'{result_dir}/kpis_result.json', 'w') as f:
        f.write(json.dumps(kpis, indent=2))
    print(kpis)