def mhe_init(): apm(s, b, 'clear all') # load model and data apm_load(s, b, 'model.apm') csv_load(s, b, 'mhe.csv') # configure MV / CV apm_info(s, b, 'FV', 'Kp') apm_info(s, b, 'FV', 'tau') apm_info(s, b, 'FV', 'zeta') apm_info(s, b, 'FV', 'TC_ss') apm_info(s, b, 'MV', 'Q1') apm_info(s, b, 'CV', 'TC') # dynamic estimation apm_option(s, b, 'nlc.imode', 5) apm_option(s, b, 'nlc.solver', 3) # tune FV apm_option(s, b, 'Kp.dmax', 0.05) apm_option(s, b, 'Kp.lower', 0.3) apm_option(s, b, 'Kp.upper', 0.6) apm_option(s, b, 'tau.dmax', 2.0) apm_option(s, b, 'tau.lower', 40.0) apm_option(s, b, 'tau.upper', 60.0) apm_option(s, b, 'zeta.dmax', 0.05) apm_option(s, b, 'zeta.lower', 1.3) apm_option(s, b, 'zeta.upper', 1.7) apm_option(s, b, 'TC_ss.dmax', 1.0) apm_option(s, b, 'TC_ss.lower', 22.0) apm_option(s, b, 'TC_ss.upper', 25.0) # turn on FVs as degrees of freedom apm_option(s, b, 'Kp.status', 1) apm_option(s, b, 'tau.status', 1) apm_option(s, b, 'zeta.status', 1) apm_option(s, b, 'TC_ss.status', 1) apm_option(s, b, 'Kp.fstatus', 0) apm_option(s, b, 'tau.fstatus', 0) apm_option(s, b, 'zeta.fstatus', 0) apm_option(s, b, 'TC_ss.fstatus', 0) # read Q, don't let optimize use MV apm_option(s, b, 'Q1.status', 0) apm_option(s, b, 'Q1.fstatus', 1) # include CV in objective function apm_option(s, b, 'TC.status', 1) apm_option(s, b, 'TC.fstatus', 1) # web-viewer option, update every second apm_option(s, b, 'nlc.web_plot_freq', 2) msg = 'initialization complete' return msg
def mpc_init(): apm(s, c, 'clear all') # load model and data apm_load(s, c, 'model.apm') csv_load(s, c, 'control.csv') # configure MV / CV apm_info(s, c, 'FV', 'Kp') apm_info(s, c, 'FV', 'tau') apm_info(s, c, 'FV', 'zeta') apm_info(s, c, 'FV', 'TC_ss') apm_info(s, c, 'MV', 'Q1') apm_info(s, c, 'CV', 'TC') # dynamic control apm_option(s, c, 'nlc.imode', 6) apm_option(s, c, 'nlc.solver', 3) apm_option(s, c, 'nlc.hist_hor', 600) # tune MV # delta MV movement penalty apm_option(s, c, 'Q1.dcost', 0.01) # penalize voltage use (energy saQ1gs) apm_option(s, c, 'Q1.cost', 0.01) # limit MV movement each cycle apm_option(s, c, 'Q1.dmax', 50) # MV limits apm_option(s, c, 'Q1.upper', 100) apm_option(s, c, 'Q1.lower', 0) # tune CV # how fast to reach setpoint apm_option(s, c, 'TC.tau', 10) # trajectory type apm_option(s, c, 'TC.tr_init', 2) # let optimizer use MV apm_option(s, c, 'Q1.status', 1) # include CV in objective function apm_option(s, c, 'TC.status', 1) # feedback status (whether we have measurements) apm_option(s, c, 'Q1.fstatus', 0) apm_option(s, c, 'TC.fstatus', 1) apm_option(s, c, 'Kp.fstatus', 1) apm_option(s, c, 'tau.fstatus', 1) apm_option(s, c, 'zeta.fstatus', 1) apm_option(s, c, 'TC_ss.fstatus', 1) # web-viewer option, update every second apm_option(s, c, 'nlc.web_plot_freq', 1) msg = 'initialization complete' return msg
def mhe(T_meas,Q1): params = np.empty(4) # input measurements apm_meas(s,b,'TC',T_meas) apm_meas(s,b,'Q1',Q1) # solve MPC output = apm(s,b,'solve') #print(output) # test for successful solution if (apm_tag(s,b,'nlc.appstatus')==1): # retrieve the K and tau values # option #2 retrieval (apm_tag) Kp = apm_tag(s,b,'Kp.Newval') tau = apm_tag(s,b,'tau.Newval') zeta = apm_tag(s,b,'zeta.Newval') TC_ss = apm_tag(s,b,'TC_ss.Newval') else: # display output for debugging print(output) # not successful, set default parameters Kp = 0.359806899452 tau = 47.7311112863 zeta = 1.56206738412 TC_ss = 300.0 params[0] = Kp params[1] = tau params[2] = zeta params[3] = TC_ss return params
def mhe(T_meas, Q1): params = np.empty(4) # input measurements apm_meas(s, b, 'TC', T_meas) apm_meas(s, b, 'Q1', Q1) # solve MPC output = apm(s, b, 'solve') #print(output) # test for successful solution if (apm_tag(s, b, 'nlc.appstatus') == 1): # retrieve the K and tau values # option #2 retrieval (apm_tag) Kp = apm_tag(s, b, 'Kp.Newval') tau = apm_tag(s, b, 'tau.Newval') TC_ss = apm_tag(s, b, 'TC_ss.Newval') TC = apm_tag(s, b, 'TC.Model') else: # display output for debugging print(output) # not successful, set default parameters Kp = 0.4 tau = 160.0 TC_ss = 23.0 TC = 23.0 params[0] = Kp params[1] = tau params[2] = TC_ss params[3] = TC return params
def mpc(T_meas, params): # input new parameter values K = params[0] tau = params[1] zeta = params[2] TC_ss = params[3] apm_meas(s, c, 'Kp', K) apm_meas(s, c, 'tau', tau) apm_meas(s, c, 'zeta', zeta) apm_meas(s, c, 'TC_ss', TC_ss) # input measurement apm_meas(s, c, 'TC', T_meas) # solve MPC output = apm(s, c, 'solve') #print(output) # test for successful solution if (apm_tag(s, c, 'nlc.appstatus') == 1): # retrieve the first Q1 value Q1 = apm_tag(s, c, 'Q1.Newval') else: # display output for debugging print(output) # not successful, set voltage to zero Q1 = 0 return Q1
def mpc(T1_meas,T1_sp,T2_meas,T2_sp): # input measurement apm_meas(s,c,'TC1',T1_meas) apm_meas(s,c,'TC2',T2_meas) # input setpoint with deadband +/- DT DT = 0.1 apm_option(s,c,'TC1.sphi',T1_sp+DT) apm_option(s,c,'TC1.splo',T1_sp-DT) apm_option(s,c,'TC2.sphi',T2_sp+DT) apm_option(s,c,'TC2.splo',T2_sp-DT) # solve MPC output = apm(s,c,'solve') # test for successful solution if (apm_tag(s,c,'apm.appstatus')==1): # retrieve the Q values Q1 = apm_tag(s,c,'Q1.Newval') Q2 = apm_tag(s,c,'Q2.Newval') else: # display output for debugging print(output) # not successful, set voltage to zero Q1 = 0 Q2 = 0 return [Q1,Q2]
def mpc(T_meas, T_sp): # input measurement apm_meas(s, c, 'T', T_meas) # input setpoint with deadband +/- DT DT = 0.1 apm_option(s, c, 'T.sphi', T_sp + DT) apm_option(s, c, 'T.splo', T_sp - DT) # solve MPC output = apm(s, c, 'solve') # test for successful solution if (apm_tag(s, c, 'nlc.appstatus') == 1): # retrieve the first Q value Q = apm_tag(s, c, 'Q.Newval') else: # display output for debugging print(output) # not successful, set voltage to zero Q = 0 return Q
# Basic Setup ---> from APMonitor.apm import * import json import numpy as np # apm setup server = 'http://byu.apmonitor.com' # run optimization on a provided server app = 'DEN' # intern name of the optimization apm(server, app, 'clear all') # ensure to start from scratch # <--- Basic Setup # Declare Model ---> # get model import make_splinemodel # generates model.apm version of the model in model.py from make_splinemodel import xi from make_splinemodel import eta from make_splinemodel import theta apm_load(server, app, 'model.apm' ) # loads the model.apm file # load data csv_load(server, app, 'data.csv') # <--- Declare Model # Set Optimization Options ---> # apm_options(server, app , 'options', value) see documentation for more details apm_option(server, app, 'nlc.imode', 6) # dynamic optimization # hidden input settings for i in theta: apm_info(server, app, 'MV', i ) # hidden inputs as Manipulated Variable (subject of optimization) apm_option(server, app, i + '.status' , 1) # optW activate variable for manipulation
# Import APM package try: from APMonitor.apm import * except: try: from pip import main as pipmain except: from pip._internal import main as pipmain pipmain(['install', 'APMonitor']) from APMonitor.apm import * # Solve optimization problem s = 'http://127.0.0.1' a = 'test_python' apm(s, a, 'clear all') apm_load(s, a, 'hs71.apm') output = apm(s, a, 'solve') print(output) sol = apm_sol(s, a) print('--- Results of the Optimization Problem ---') print('x[1]: ' + str(sol['x[1]'])) print('x[2]: ' + str(sol['x[2]'])) print('x[3]: ' + str(sol['x[3]'])) print('x[4]: ' + str(sol['x[4]']))
# Import APM Python library "pip install APMonitor" from APMonitor.apm import * # Select the server server = 'https://byu.apmonitor.com' # Give the application a name app = 'production' # Clear any previous applications by that name apm(server, app, 'clear all') # Write the model file fid = open('softdrink.apm', 'w') fid.write('Variables \n') fid.write(' x1 > 0 , < 5 ! Product 1 \n') fid.write(' x2 > 0 , < 4 ! Product 2 \n') fid.write(' profit \n') fid.write(' \n') fid.write('Equations \n') fid.write(' ! profit function \n') fid.write(' maximize profit \n') fid.write(' profit = 100 * x1 + 125 * x2 \n') fid.write(' 3 * x1 + 6 * x2 <= 30 \n') fid.write(' 8 * x1 + 4 * x2 <= 44 \n') fid.close() apm_load(server, app, 'softdrink.apm') # Solve on APM server solver_output = apm(server, app, 'solve')