Beispiel #1
0
def model():

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    R           = 8.314    			#gas constant
    T_F         = 25 + 273.15       #feed temperature
    E_a         = 8500.0     			#activation energy
    delH_R      = 950.0*1.00      			#sp reaction enthalpy
    A_tank      = 65.0       			#area heat exchanger surface jacket 65

    k_0         = 7.0*1.00      	#sp reaction rate
    k_U2        = 32.0     	#reaction parameter 1
    k_U1        = 4.0      	#reaction parameter 2
    w_WF        = .333      #mass fraction water in feed
    w_AF        = .667      #mass fraction of A in feed

    m_M_KW      = 5000.0      #mass of coolant in jacket
    fm_M_KW     = 300000.0    #coolant flow in jacket 300000;
    m_AWT_KW    = 1000.0      #mass of coolant in EHE
    fm_AWT_KW   = 100000.0    #coolant flow in EHE
    m_AWT       = 200.0       #mass of product in EHE
    fm_AWT      = 20000.0     #product flow in EHE
    m_S         = 39000.0     #mass of reactor steel

    c_pW        = 4.2      #sp heat cap coolant
    c_pS        = .47       #sp heat cap steel
    c_pF        = 3.0         #sp heat cap feed
    c_pR        = 5.0         #sp heat cap reactor contents

    k_WS        = 17280.0     #heat transfer coeff water-steel
    k_AS        = 3600.0      #heat transfer coeff monomer-steel
    k_PS        = 360.0       #heat transfer coeff product-steel

    alfa        = 5*20e4*3.6

    p_1         = 1.0

    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    delH_R      = SX.sym("delH_R")
    k_0         = SX.sym("k_0")
    bias_term   = SX.sym("bias_term")

    # Define the differential states as CasADi symbols

    m_W             = SX.sym("m_W")
    m_A             = SX.sym("m_A")
    m_P             = SX.sym("m_P")
    T_R             = SX.sym("T_R")
    T_S             = SX.sym("T_S")
    Tout_M          = SX.sym("Tout_M")
    T_EK            = SX.sym("T_EK")
    Tout_AWT        = SX.sym("Tout_AWT")
    accum_momom     = SX.sym("accum_monom")
    T_adiab         = SX.sym("T_adiab")

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols
    m_dot_f  		   = SX.sym("m_dot_f")
    T_in_M          = SX.sym("T_in_M")
    T_in_EK         = SX.sym("T_in_EK")

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Time constant for the hks is different if cooling or if heating
    ## --- Algebraic equations ---
    U_m    = m_P / (m_A + m_P)
    m_ges  = m_W + m_A + m_P
    k_R1   = k_0 * exp(- E_a/(R*T_R)) * ((k_U1 * (1 - U_m)) + (k_U2 * U_m))
    k_R2   = k_0 * exp(- E_a/(R*T_EK))* ((k_U1 * (1 - U_m)) + (k_U2 * U_m))
    k_K    = ((m_W / m_ges) * k_WS) + ((m_A/m_ges) * k_AS) + ((m_P/m_ges) * k_PS)

    ## --- Differential equations ---

    ddm_W   	= m_dot_f * w_WF
    ddm_A 		= (m_dot_f * w_AF) - (k_R1 * (m_A-((m_A*m_AWT)/(m_W+m_A+m_P)))) - (p_1 * k_R2 * (m_A/m_ges) * m_AWT)
    ddm_P  	= (k_R1 * (m_A-((m_A*m_AWT)/(m_W+m_A+m_P)))) + (p_1 * k_R2 * (m_A/m_ges) * m_AWT)

    ddT_R   	= 1./(c_pR * m_ges)   * ((m_dot_f * c_pF * (T_F - T_R)) - (k_K *A_tank* (T_R - T_S)) - (fm_AWT * c_pR * (T_R - T_EK)) + (delH_R * k_R1 * (m_A-((m_A*m_AWT)/(m_W+m_A+m_P)))))
    ddT_S   	= 1./(c_pS * m_S)     * ((k_K *A_tank* (T_R - T_S)) - (k_K *A_tank* (T_S - Tout_M)))
    ddTout_M  = 1./(c_pW * m_M_KW)  * ((fm_M_KW * c_pW * (T_in_M - Tout_M)) + (k_K *A_tank* (T_S - Tout_M)))
    ddT_EK   	= 1./(c_pR * m_AWT)   * ((fm_AWT * c_pR * (T_R - T_EK)) - (alfa * (T_EK - Tout_AWT)) + (p_1 * k_R2 * (m_A/m_ges) * m_AWT * delH_R))
    ddTout_AWT= 1./(c_pW * m_AWT_KW)* ((fm_AWT_KW * c_pW * (T_in_EK - Tout_AWT)) - (alfa * (Tout_AWT - T_EK)))

    ddaccum_momom   = m_dot_f
    ddT_adiab       = delH_R/(m_ges*c_pR)*ddm_A-(ddm_A+ddm_W+ddm_P)*(m_A*delH_R/(m_ges*m_ges*c_pR))+ddT_R

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(m_W, m_A, m_P, T_R, T_S, Tout_M, T_EK, Tout_AWT, accum_momom, T_adiab)

    _u = vertcat(m_dot_f,T_in_M,T_in_EK)

    _xdot = vertcat(ddm_W, ddm_A, ddm_P, ddT_R, ddT_S, ddTout_M, ddT_EK, ddTout_AWT, ddaccum_momom, ddT_adiab)

    _p = vertcat(delH_R, k_0)

    _z = []

    _tv_p = vertcat(tv_param_1, tv_param_2)

    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    # Initial conditions
    m_W_0 = 10000.0
    m_A_0 = 853.0*1.0  #3700.0
    m_P_0 = 26.5
    T_R_0  = 90 + 273.15
    T_S_0  = 90 + 273.15
    Tout_M_0  = 90 + 273.15
    T_EK_0 = 35 + 273.15
    Tout_AWT_0= 35 + 273.15

    accum_momom_0   = 300.0

    # This value is used here only to compute the initial condition of this state
    # This should be changed in case the real value is different
    delH_R_real = 950.0*1.00
    T_adiab_0		= m_A_0*delH_R_real/((m_W_0+m_A_0+m_P_0)*c_pR)+T_R_0

    x0   = NP.array([m_W_0, m_A_0, m_P_0, T_R_0, T_S_0, Tout_M_0, T_EK_0, Tout_AWT_0, accum_momom_0,T_adiab_0])

    # Bounds for the states and initial guess
    temp_range = 2.0
    m_W_lb          = 0;    					m_W_ub      = inf      # Kg
    m_A_lb       	= 0;    					m_A_ub      = inf      # Kg
    m_P_lb       	= 26.0;    					m_P_ub      = inf      # Kg
    T_R_lb     		= 363.15-temp_range;   		T_R_ub   	= 363.15+temp_range+10 # K
    T_S_lb 			= 298.0;    				T_S_ub 		= 400.0      # K
    Tout_M_lb       = 298.0;    				Tout_M_ub   = 400.0      # K
    T_EK_lb    		= 288.0;    				T_EK_ub    	= 400.0      # K
    Tout_AWT_lb     = 288.0;    				Tout_AWT_ub = 400.0      # K
    accum_momom_lb  = 0;						accum_momom_ub = 30000
    T_adiab_lb         =-inf;							T_adiab_ub	=  382.15 + 10 # (implemented as soft constraint)
    x_lb  = NP.array([m_W_lb, m_A_lb, m_P_lb, T_R_lb, T_S_lb, Tout_M_lb, T_EK_lb, Tout_AWT_lb, accum_momom_lb,T_adiab_lb])
    x_ub  = NP.array([m_W_ub, m_A_ub, m_P_ub, T_R_ub, T_S_ub, Tout_M_ub, T_EK_ub, Tout_AWT_ub, accum_momom_ub,T_adiab_ub])

    # Bounds for inputs
    m_dot_f_lb = 0.0;  	 m_dot_f_ub = 3.0e4;
    T_in_M_lb  = 333.15;	 T_in_M_ub  = 373.15;
    T_in_EK_lb = 333.15;   T_in_EK_ub = 373.15;

    u_lb=NP.array([m_dot_f_lb, T_in_M_lb, T_in_EK_lb])
    u_ub=NP.array([m_dot_f_ub, T_in_M_ub, T_in_EK_ub])

    # Initial guess for input (or initial condition if penalty for control movements)
    m_dot_f_0 = 0
    T_in_M_0  = 363.0
    T_in_EK_0 = 323.0

    u0   = NP.array([m_dot_f_0 , T_in_M_0, T_in_EK_0])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling=NP.array([10.0, 10.0, 10.0, 1.0, 1.0, 1.0, 1.0, 1.0, 10,1])
    u_scaling = NP.array([100.0, 1.0, 1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat(T_R, T_adiab)
    # Define the upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([363.15+temp_range, 382.15])
    #cons_ub = NP.array([])
    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 1
    # l1 - Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([1e5, 1e5])
    # Maximum violation for the soft constraints
    maximum_violation = NP.array([10, 10])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])

    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm =  - m_P
    # Mayer term
    mterm =  - m_P
    # Penalty term for the control movements
    rterm = 0.04 * NP.array([.05,.1,.05])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': x_lb,'x_ub': x_ub, 'u0':u0, 'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb,'tv_p':_tv_p, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #2
0
def model():

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """
    M   = 5.0                          # mass of the cart               [kg]
    m   = 1.0                          # mass of the pendulum           [kg]
    l   = 1.0                          # length of lever arm            [m]
    h   = 0.5                          # height of rod connection point [m]
    g   = 9.81                         # grav acceleration              [m/s^2]

    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertain/varying parameters as CasADi symbols
    alpha      = SX.sym("alpha")         # can be used for implementing uncertainty

    tv_param_1 = SX.sym("tv_param_1")    # can be used to implement setpoint change

    # Define the differential states as CasADi symbols
    x 	   = SX.sym("x")               # x position of the mass
    y 	   = SX.sym("y")		          # y position of the mass
    v     = SX.sym("v")              # linear velocity of the mass
    theta = SX.sym("theta")          # angle of the metal rod
    omega = SX.sym("omega")          # angular velocity of the mass
    # Define the algebraic states as CasADi symbols
    #y 		= SX.sym("y")          # the y coordinate is considered algebraic

    # Define the control inputs as CasADi symbols
    F  	= SX.sym("F")             # control force applied to the lever

    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the differential equations
    dd = SX.sym("dd",4)
    dd[0]  = v
    dd[1]  = (1.0/(M+m-m*casadi.cos(theta)))*(m*g*casadi.sin(theta)-m*l*casadi.sin(theta)*omega**2 +F)
    dd[2] = omega
    dd[3] = (1.0/l)*(dd[1]*casadi.cos(theta)+g*casadi.sin(theta))

    # Define the algebraic equations
    dy = h+l*casadi.cos(theta) - y            # the coordinates must fulfil the constraint of the lever arm length

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(x,v,theta,omega)

    _z = []                                # toggle if there are no AE in your model
    #_z = vertcat(y)

    _u = vertcat(F)

    _p = vertcat(alpha)

    _xdot = vertcat(dd)

    _zdot = []                          # toggle if there are no AE in your model
    #_zdot = vertcat(dtrajectory)

    _tv_p = vertcat(tv_param_1)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial conditions for Differential States
    x_init  = 2.5
    v_init  = 0.0
    t_init  = 1.5
    o_init  = 0.0

    #Initial conditions for Algebraic States
    y_init  = sqrt(3.0)/2.0

    x0 = NP.array([x_init, v_init, t_init, o_init])
    z0 = NP.array([])
    # Bounds on the states. Use "inf" for unconstrained states
    x_lb    =  -10.0;       x_ub  = 10.0
    v_lb    = -10.0;       v_ub  = 10.0
    t_lb    =   -100*pi;       t_ub  = 100*pi
    o_lb    = -10.0;       o_ub  = 10.0


    x_lb = NP.array([x_lb, v_lb, t_lb, o_lb])
    x_ub = NP.array([x_ub, v_ub, t_ub, o_ub])
    z_lb = NP.array([])
    z_ub = NP.array([])
    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    F_lb    = -250.0;       F_ub = 250.00 ;     F_init = 0.0	;


    u_lb=NP.array([F_lb])
    u_ub=NP.array([F_ub])
    u0 = NP.array([F_init])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0, 1.0])
    z_scaling = NP.array([])
    u_scaling = NP.array([1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([])
    # Maximum violation for the constraints
    maximum_violation = NP.array([0])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])


    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    ##-----Distance to vertical position---------------------
    lterm =  (x-tv_param_1)**2 + (theta-pi/2)**2
    # Mayer term
    mterm =  0
    # Penalty term for the control movements
    rterm = 0.0001*NP.array([1.0])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
     """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': x_lb,'x_ub': x_ub, 'u0':u0, 'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb,'tv_p':_tv_p, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #3
0
def model():
    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    K0_ab = 1.287e12  # K0 [h^-1]
    K0_bc = 1.287e12  # K0 [h^-1]
    K0_ad = 9.043e9  # K0 [l/mol.h]
    R_gas = 8.3144621e-3  # Universal gas constant
    E_A_ab = 9758.3 * 1.00  #* R_gas# [kj/mol]
    E_A_bc = 9758.3 * 1.00  #* R_gas# [kj/mol]
    E_A_ad = 8560.0 * 1.0  #* R_gas# [kj/mol]
    H_R_ab = 4.2  # [kj/mol A]
    H_R_bc = -11.0  # [kj/mol B] Exothermic
    H_R_ad = -41.85  # [kj/mol A] Exothermic
    Rou = 0.9342  # Density [kg/l]
    Cp = 3.01  # Specific Heat capacity [kj/Kg.K]
    Cp_k = 2.0  # Coolant heat capacity [kj/kg.k]
    A_R = 0.215  # Area of reactor wall [m^2]
    V_R = 10.01  #0.01 # Volume of reactor [l]
    m_k = 5.0  # Coolant mass[kg]
    T_in = 130.0  # Temp of inflow [Celsius]
    K_w = 4032.0  # [kj/h.m^2.K]
    C_A0 = (
        5.7 + 4.5
    ) / 2.0 * 1.0  # Concentration of A in input Upper bound 5.7 lower bound 4.5 [mol/l]
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha = SX.sym("alpha")
    beta = SX.sym("beta")
    # Define the differential states as CasADi symbols

    C_a = SX.sym("C_a")  # Concentration A
    C_b = SX.sym("C_b")  # Concentration B
    T_R = SX.sym("T_R")  # Reactor Temprature
    T_K = SX.sym("T_K")  # Jacket Temprature

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    F = SX.sym("F")  # Vdot/V_R [h^-1]
    Q_dot = SX.sym("Q_dot")  #Q_dot second control input

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    K_1 = beta * K0_ab * exp((-E_A_ab) / ((T_R + 273.15)))
    K_2 = K0_bc * exp((-E_A_bc) / ((T_R + 273.15)))
    K_3 = K0_ad * exp((-alpha * E_A_ad) / ((T_R + 273.15)))

    # Define the differential equations

    dC_a = F * (C_A0 - C_a) - K_1 * C_a - K_3 * (C_a**2)
    dC_b = -F * C_b + K_1 * C_a - K_2 * C_b
    dT_R = ((K_1 * C_a * H_R_ab + K_2 * C_b * H_R_bc + K_3 *
             (C_a**2) * H_R_ad) /
            (-Rou * Cp)) + F * (T_in - T_R) + (((K_w * A_R) * (T_K - T_R)) /
                                               (Rou * Cp * V_R))
    dT_K = (Q_dot + K_w * A_R * (T_R - T_K)) / (m_k * Cp_k)

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(C_a, C_b, T_R, T_K)

    _z = vertcat([])

    _u = vertcat(F, Q_dot)

    _xdot = vertcat(dC_a, dC_b, dT_R, dT_K)

    _p = vertcat(alpha, beta)

    _tv_p = vertcat(tv_param_1, tv_param_2)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    C_a_0 = 0.8  # This is the initial concentration inside the tank [mol/l]
    C_b_0 = 0.5  # This is the controlled variable [mol/l]
    T_R_0 = 134.14  #[C]
    T_K_0 = 130.0  #[C]
    x0 = NP.array([C_a_0, C_b_0, T_R_0, T_K_0])
    # No algebraic states
    z0 = NP.array([])

    # Bounds on the states. Use "inf" for unconstrained states
    C_a_lb = 0.1
    C_a_ub = 2.0
    C_b_lb = 0.1
    C_b_ub = 2.0
    T_R_lb = 50.0
    T_R_ub = 180
    T_K_lb = 50.0
    T_K_ub = 180
    x_lb = NP.array([C_a_lb, C_b_lb, T_R_lb, T_K_lb])
    x_ub = NP.array([C_a_ub, C_b_ub, T_R_ub, T_K_ub])

    # No algebraic states
    z_lb = NP.array([])
    z_ub = NP.array([])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    F_lb = 5.0
    F_ub = +100.0
    Q_dot_lb = -8500.0
    Q_dot_ub = 0.0
    u_lb = NP.array([F_lb, Q_dot_lb])
    u_ub = NP.array([F_ub, Q_dot_ub])
    u0 = NP.array([30.0, -6000.0])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0, 1.0])
    z_scaling = NP.array([])
    u_scaling = NP.array([1.0, 1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([])
    # Maximum violation for the constraints
    maximum_violation = NP.array([0])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])
    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm = (C_b - tv_param_1)**2
    #lterm =  - C_b
    # Mayer term
    mterm = (C_b - tv_param_1)**2
    #mterm =  - C_b
    # Penalty term for the control movements
    rterm = NP.array([0.0, 0.0])
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {
        'x': _x,
        'u': _u,
        'rhs': _xdot,
        'p': _p,
        'z': _z,
        'x0': x0,
        'x_lb': x_lb,
        'x_ub': x_ub,
        'u0': u0,
        'u_lb': u_lb,
        'u_ub': u_ub,
        'x_scaling': x_scaling,
        'u_scaling': u_scaling,
        'cons': cons,
        'tv_p': _tv_p,
        "cons_ub": cons_ub,
        'cons_terminal': cons_terminal,
        'cons_terminal_lb': cons_terminal_lb,
        'cons_terminal_ub': cons_terminal_ub,
        'soft_constraint': soft_constraint,
        'penalty_term_cons': penalty_term_cons,
        'maximum_violation': maximum_violation,
        'mterm': mterm,
        'lterm': lterm,
        'rterm': rterm
    }

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #4
0
def model():

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    mu_m	= 0.02
    K_m	= 0.05
    K_i	= 5.0
    v_par	= 0.004
    Y_p	= 1.2

    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    Y_x   = SX.sym("Y_x")
    S_in  = SX.sym("S_in")

    # Define the differential states as CasADi symbols

    X_s    = SX.sym("X_s") # Concentration A
    S_s    = SX.sym("S_s") # Concentration B
    P_s    = SX.sym("P_s") # Reactor Temprature
    V_s    = SX.sym("V_s") # Jacket Temprature

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    inp      = SX.sym("inp") # Vdot/V_R [h^-1]

    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    mu_S	= mu_m*S_s/(K_m+S_s+(S_s**2/K_i))

    # Define the differential equations

    dX_s	= mu_S*X_s - inp/V_s*X_s
    dS_s	= -mu_S*X_s/Y_x - v_par*X_s/Y_p + inp/V_s*(S_in-S_s)
    dP_s	= v_par*X_s - inp/V_s*P_s
    dV_s	= inp

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(X_s, S_s, P_s, V_s)

    _u = vertcat(inp)

    _xdot = vertcat(dX_s, dS_s, dP_s, dV_s)

    _p = vertcat(Y_x, S_in)

    _z = []



    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    X_s_0 = 1.0
    S_s_0 = 0.5
    P_s_0 = 0.0
    V_s_0 = 120.0
    x0 = NP.array([X_s_0, S_s_0, P_s_0, V_s_0])

    # Bounds on the states. Use "inf" for unconstrained states
    X_s_lb = 0.0;			X_s_ub = 3.7
    S_s_lb = -0.01;		      S_s_ub = inf
    P_s_lb = 0.0;		      P_s_ub = 3.0
    V_s_lb = 0.0;	           V_s_ub = inf
    x_lb = NP.array([X_s_lb, S_s_lb, P_s_lb, V_s_lb])
    x_ub = NP.array([X_s_ub, S_s_ub, P_s_ub, V_s_ub])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    inp_lb = 0.0;                 inp_ub = 0.2;

    u_lb = NP.array([inp_lb])
    u_ub = NP.array([inp_ub])
    u0 = NP.array([0.03])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0, 1.0])
    u_scaling = NP.array([1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the upper bounds of the constraint (leave it empty if not necessary)

    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # l1 - Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([1e4])
    # Maximum violation for the upper and lower bounds
    maximum_violation = NP.array([10])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])


    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm =  -P_s
    #lterm =  - C_b
    # Mayer term
    mterm =  -P_s
    #mterm =  - C_b
    # Penalty term for the control movements
    rterm = NP.array([0.0])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': x_lb,'x_ub': x_ub, 'u0':u0, 'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #5
0
def model():
    lbub = NP.load('Neural_Network\lbub_60.npy')
    x_lb_NN = lbub[0]
    x_ub_NN = lbub[1]
    y_lb_NN = lbub[2]
    y_ub_NN = lbub[3]

    json_file = open('Neural_Network\model_60.json', 'r')
    model_json = json_file.read()
    json_file.close()
    model = model_from_json(model_json)
    # load weights into new model
    model.load_weights("Neural_Network\model_60.h5")
    Theta = {}
    i = 0
    for j in range(len(model.layers)):
        weights = model.layers[j].get_weights()
        Theta['Theta' + str(i + 1)] = NP.insert(weights[0].transpose(),
                                                0,
                                                weights[1].transpose(),
                                                axis=1)
        i += 1
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha = MX.sym("alpha")
    # Define the differential states as CasADi symbols
    # T(t-1) + u_rad(t-1) + u_ahu(t-1) + v_IG(t-1) + u_blinds(t-1) + v(t-1)
    #+ len(zones_Heating) + len(zones_ahu) + len(zones)
    x = MX.sym("x", (numbers - 1) * (len(zones) + 12))
    # Define the control inputs as CasADi symbols
    u_blinds_E = MX.sym("u_blinds_E")
    u_blinds_N = MX.sym("u_blinds_N")
    u_blinds_S = MX.sym("u_blinds_S")
    u_blinds_W = MX.sym("u_blinds_W")

    u = vertcat(u_blinds_E, u_blinds_N, u_blinds_S, u_blinds_W)

    for i in range(len(zones_ahu)):
        tmp = 'u_AHU_' + zones_ahu[i]
        vars()[tmp] = MX.sym(tmp)
        u = vertcat(u, vars()[tmp])

    for i in range(len(zones_Heating)):
        tmp = 'u_rad_' + zones_Heating[i]
        vars()[tmp] = MX.sym(tmp)
        u = vertcat(u, vars()[tmp])
        if i == 0:
            u_rad = MX.sym('u_rad')
            u_rad = vars()[tmp]
        else:
            u_rad = vertcat(u_rad, vars()[tmp])

    # Define time-varying parameters that can chance at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions
    for i in range(len(zones)):
        tmp = 'v_IG_' + zones[i]
        vars()[tmp] = MX.sym(tmp)
        if i == 0:
            v = vars()[tmp]
        else:
            v = vertcat(v, vars()[tmp])

    v_Tamb = MX.sym("v_Tamb")
    v_solGlobFac_S = MX.sym("v_solGlobFac_S")
    v_solGlobFac_W = MX.sym("v_solGlobFac_W")
    v_solGlobFac_N = MX.sym("v_solGlobFac_N")
    v_solGlobFac_E = MX.sym("v_solGlobFac_E")
    v_windspeed = MX.sym("v_windspeed")
    v_Hum_amb = MX.sym("v_Hum_amb")
    v_P_amb = MX.sym("v_P_amb")

    u_ahu_ub = MX.sym("u_ahu_ub")
    setp_ub = MX.sym("setp_ub")
    setp_lb = MX.sym("setp_lb")

    v = vertcat(v, v_Tamb, v_solGlobFac_E, v_solGlobFac_N, v_solGlobFac_S,
                v_solGlobFac_W, v_windspeed, v_Hum_amb, v_P_amb)
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    input = vertcat(x, u, v)
    input = NP.divide(input - x_lb_NN, x_ub_NN - x_lb_NN)

    for i in range(1, len(model.layers)):
        tmp_a = 'a' + str(i) + '_'
        if i == 1:
            vars()[tmp_a] = input
        vars()[tmp_a] = vertcat(1, vars()[tmp_a])
        tmp_z = 'z' + str(i) + '_'
        vars()[tmp_z] = mtimes(Theta['Theta' + str(i)], vars()[tmp_a])
        tmp_a = 'a' + str(i + 1) + '_'
        if i < len(model.layers):
            vars()[tmp_a] = tanh(vars()[tmp_z])
        else:
            vars()[tmp_a] = vars()[tmp_z]

    dT = MX.sym('dT')
    dHeatrate = MX.sym('dHeatrate')
    for i in range(len(zones) + len(zones_Heating)):
        if i < len(zones):
            tmp = 'dT_' + zones[i]
        else:
            tmp = 'dHeatrate_' + zones_Heating[i - len(zones)]

        vars()[tmp] = MX.sym(tmp)
        vars()[tmp] = vars()[tmp_a][i]
        vars()[tmp] = NP.multiply(vars()[tmp_a][i],
                                  y_ub_NN[i] - y_lb_NN[i]) + y_lb_NN[i]

        if i == 0:
            dT = vars()[tmp]
        elif i < len(zones):
            dT = vertcat(dT, vars()[tmp])
        elif i == len(zones):
            dHeatrate = vars()[tmp]
        else:
            dHeatrate = vertcat(dHeatrate, vars()[tmp])

    # dT = vars()[tmp_a][0:14]
    dx = vertcat(dT, u[0:4], v[len(zones):])
    # Concatenate differential states, algebraic states, control inputs and right-hand-sides
    _x = x

    _z = vertcat([])

    _u = u

    _xdot = dx

    _zdot = vertcat([])

    _p = vertcat(alpha)

    _tv_p = vertcat(v, u_ahu_ub, setp_ub, setp_lb)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    disturbances = NP.load('Neural_Network\disturbances.npy').squeeze()
    x0 = NP.concatenate(
        (18 * NP.ones(len(zones)), NP.zeros(4), disturbances[len(zones):, 0]))
    # No algebraic states
    z0 = NP.array([])
    # Bounds on the states. Use "inf" for unconstrained states

    disturbances_lb = NP.min(disturbances, axis=1)
    disturbances_ub = NP.max(disturbances, axis=1)
    x_lb = NP.concatenate(
        (x_lb_NN[0:len(zones) + 0 * len(zones_Heating) + 0 * len(zones_ahu)] -
         1e-2, NP.zeros(4), disturbances_lb[len(zones):]))
    x_ub = NP.concatenate(
        (x_ub_NN[0:len(zones) + 0 * len(zones_Heating) + 0 * len(zones_ahu)] +
         1e-2, NP.ones(4), disturbances_ub[len(zones):]))
    # No algebraic states
    z_lb = NP.array([])
    z_ub = NP.array([])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    u_lb = NP.array([0, 0, 0, 0])
    u_lb = NP.concatenate(
        (u_lb, NP.zeros(len(zones_ahu)), 16 * NP.ones(len(zones_Heating))))
    u_ub = NP.array([1, 1, 1, 1])
    u_ub = NP.concatenate(
        (u_ub, NP.ones(len(zones_ahu)), 22 * NP.ones(len(zones_Heating))))
    u0 = NP.concatenate(
        (NP.zeros(4 + len(zones_ahu)), 18 * NP.ones(len(zones_Heating))))

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.ones(x.shape[0])
    z_scaling = NP.array([])
    u_scaling = NP.ones(4 + len(zones_ahu) + len(zones_Heating))
    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    # cons = vertcat(x[0:len(zones)]-setp_ub, -(x[0:len(zones)]-setp_lb))
    cons = vertcat(x[0] - setp_ub, -(x[0] - setp_lb))
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    # cons_ub = NP.zeros(2*len(zones))
    cons_ub = NP.zeros(2)

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 1
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = 1e6 * NP.ones(2)
    # Maximum violation for the constraints
    maximum_violation = 20 * NP.ones(2)

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])
    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    lterm = mtimes(0.5 * NP.ones(
        (dHeatrate.shape[1], dHeatrate.shape[0])), dHeatrate) + mtimes(
            10 * NP.ones((u_rad.shape[1], u_rad.shape[0])), u_rad)
    mterm = mtimes(0.5 * NP.ones(
        (dHeatrate.shape[1], dHeatrate.shape[0])), dHeatrate) + mtimes(
            10 * NP.ones((u_rad.shape[1], u_rad.shape[0])), u_rad)
    # Penalty term for the control movements 1e4, 100
    rterm = NP.concatenate((1e4 * NP.ones(4), 1e4 * NP.ones(len(zones_ahu)),
                            20 * NP.ones(len(zones_Heating))))
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    bp()
    model_dict = {
        'x': _x,
        'u': _u,
        'rhs': _xdot,
        'p': _p,
        'z': _z,
        'aes': _zdot,
        'x0': x0,
        'z0': z0,
        'x_lb': x_lb,
        'x_ub': x_ub,
        'z_lb': z_lb,
        'z_ub': z_ub,
        'u0': u0,
        'u_lb': u_lb,
        'u_ub': u_ub,
        'x_scaling': x_scaling,
        'z_scaling': z_scaling,
        'u_scaling': u_scaling,
        'cons': cons,
        'tv_p': _tv_p,
        "cons_ub": cons_ub,
        'cons_terminal': cons_terminal,
        'cons_terminal_lb': cons_terminal_lb,
        'cons_terminal_ub': cons_terminal_ub,
        'soft_constraint': soft_constraint,
        'penalty_term_cons': penalty_term_cons,
        'maximum_violation': maximum_violation,
        'mterm': mterm,
        'lterm': lterm,
        'rterm': rterm
    }
    model = core_do_mpc.model(model_dict)

    return model
Beispiel #6
0
def model(waypoint, obstacles,pose,twist,wt):

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha   = SX.sym("alpha")
    beta    = SX.sym("beta")
    # Define the differential states as CasADi symbols

    x    = SX.sym("x") # Concentration A
    y    = SX.sym("y") # Concentration B
    theta    = SX.sym("theta") # Reactor Temprature
    #T_K    = SX.sym("T_K") # Jacket Temprature

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    u      = SX.sym("u") # Vdot/V_R [h^-1]
    w  = SX.sym("w") #Q_dot second control input

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")

    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    #K_1 = beta * K0_ab * exp((-E_A_ab)/((T_R+273.15)))
    #K_2 =  K0_bc * exp((-E_A_bc)/((T_R+273.15)))
    #K_3 = K0_ad * exp((-alpha*E_A_ad)/((T_R+273.15)))

    # Define the differential equations

    dx=u*cos(theta)
    dy=u*sin(theta)
    dtheta=w
    #dC_a = F*(C_A0 - C_a) -K_1*C_a - K_3*(C_a**2)
    #dC_b = -F*C_b + K_1*C_a -K_2*C_b
    #dT_R = ((K_1*C_a*H_R_ab + K_2*C_b*H_R_bc + K_3*(C_a**2)*H_R_ad)/(-Rou*Cp)) + F*(T_in-T_R) +(((K_w*A_R)*(T_K-T_R))/(Rou*Cp*V_R))
    #dT_K = (Q_dot + K_w*A_R*(T_R-T_K))/(m_k*Cp_k)

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(x, y, theta)

    _u = vertcat(u, w)

    _xdot = vertcat(dx, dy, dtheta)

    _p = vertcat(alpha, beta)
    #_p = []

    _z = []

    _tv_p = vertcat(tv_param_1, tv_param_2)
    #_tv_p = vertcat()

    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    print "pose",pose
    x_0 = pose[0] # This is the initial concentration inside the tank [mol/l]
    y_0 = pose[1] # This is the controlled variable [mol/l]
    theta_0 = pose[2] #[C]
#    T_K_0 = 130.0 #[C]
    x0 = NP.array([x_0, y_0, theta_0])

    # Bounds on the states. Use "inf" for unconstrained states
    x_lb = -100;			x_ub = 100.0
    y_lb = -100;			y_ub = 100.0
    theta_lb = -3.14;			theta_ub = 3.14
    #T_K_lb = 50.0;			T_K_ub = 180
    X_lb = NP.array([x_lb, y_lb, theta_lb])
    X_ub = NP.array([x_ub, y_ub, theta_ub])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    u_lb = 0.0;                 u_ub = 1.0;
    w_lb = -1.0;         w_ub = 1.0;
    U_lb = NP.array([u_lb, w_lb])
    U_ub = NP.array([u_ub, w_ub])
    u0 = NP.array(twist)

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0])
    u_scaling = NP.array([1.0, 1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([])
    # Maximum violation for the constraints
    maximum_violation = NP.array([0])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])


    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    Y_ref=waypoint
    
    # Define the cost function
    # Lagrange term
    #lterm =  1e4*((C_b - 0.9)**2 + (C_a - 1.1)**2)
    #lterm = 10*(x-Y_ref[0])**2+10*(y-Y_ref[1])**2+(theta-Y_ref[2])**2 + (lam)*exp(-(gamma+((xo[0]-x)**2)/(rx**2)+((xo[1]-y)**2)/(ry**2)))
    if len(obstacles.a):
        lam=wt[3]
        gamma=1    
        V = 0 
        for i in xrange(len(obstacles.a)):
            if obstacles.a[i]:
                rx=obstacles.a[i]
                ry=obstacles.b[i]
                phi = atan(obstacles.m[i])
                xo=obstacles.centroid[i]
                

                R = [[cos(theta_0), -sin(theta_0)],[sin(theta_0),cos(theta_0)]]

                xo = np.matmul(R,xo) + pose[0:2]
                print "obs position",xo
                V = V + (lam)/((gamma+(( (xo[0]-x)*cos(phi) + (xo[1]-y)*sin(phi) )**2)/(rx**2) + (( (xo[0]-x)*sin(phi) - (xo[1]-y)*cos(phi) )**2)/(ry**2) ))


        lterm = wt[0]*(x-Y_ref[0])**2 + \
            wt[1]*(y-Y_ref[1])**2 + \
            wt[2]*(theta-Y_ref[2])**2 + \
            V        
        
    else:
        lterm = wt[0]*(x-Y_ref[0])**2 + \
                wt[1]*(y-Y_ref[1])**2 + \
                wt[2]*(theta-Y_ref[2])**2
            
    #lterm =  - C_b
    # Mayer term
#    mterm =  1e4*((C_b - 0.9)**2 + (C_a - 1.1)**2)
    mterm = 0
    #mterm =  - C_b
    # Penalty term for the control movements
    rterm = NP.array([0.0, 0.0])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': X_lb,'x_ub': X_ub, 'u0':u0, 'u_lb':U_lb, 'u_ub':U_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb,'tv_p':_tv_p, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #7
0
def model():
    # Load Discrete_Time_Model
    Discrete_Time_Model = scipy.io.loadmat('RKF_Discrete_Time_Model.mat')
    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha = SX.sym("alpha")
    # beta    = SX.sym("beta")
    # Define the differential states as CasADi symbols

    x = SX.sym("x", 37)

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    u_rad_OfficesZ1 = SX.sym("u_rad_OfficesZ1")

    u_AHU1_noERC = SX.sym("u_AHU1_noERC")

    u_blinds_N = SX.sym("u_blinds_N")
    u_blinds_W = SX.sym("u_blinds_W")

    u = vertcat(u_blinds_N, u_blinds_W, u_AHU1_noERC, u_rad_OfficesZ1)

    # Define time-varying parameters that can chance at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions
    v_IG_Offices = SX.sym("V_IG_Offices")
    v_Tamb = SX.sym("v_Tamb")
    v_Tgnd = SX.sym("v_Tgnd")
    v_solGlobFac_S = SX.sym("v_solGlobFac_S")
    v_solGlobFac_W = SX.sym("v_solGlobFac_W")
    v_solGlobFac_N = SX.sym("v_solGlobFac_N")
    v_solGlobFac_E = SX.sym("v_solGlobFac_E")
    v_windspeed = SX.sym('v_windspeed')
    setp_ub = SX.sym("setp_ub")
    setp_lb = SX.sym("setp_lb")
    u_ahu_ub = SX.sym("u_ahu_ub")

    v = vertcat(v_IG_Offices, v_Tamb, v_Tgnd, v_solGlobFac_E, v_solGlobFac_N,
                v_solGlobFac_S, v_solGlobFac_W, v_windspeed, u_ahu_ub, setp_ub,
                setp_lb)
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """

    # Define the differential equations
    Bvu = Discrete_Time_Model["Bvu"]
    Bxu = Discrete_Time_Model["Bxu"]

    dx = mtimes(Discrete_Time_Model["A"], x) + mtimes(
        Discrete_Time_Model["Bu"], u) + mtimes(Discrete_Time_Model["Bv"],
                                               v[0:7])
    sum = 0
    for i in range(u.shape[0]):
        sum = sum + (mtimes(Bvu[:, :, i], v[0:7]) +
                     mtimes(Bxu[:, :, i], x)) * u[i]
    dx = dx + sum
    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = x

    _z = vertcat([])

    _u = u

    _xdot = dx

    _zdot = vertcat([])

    # _p = vertcat(alpha, beta)
    _p = vertcat(alpha)

    # _tv_p = vertcat([])
    _tv_p = v
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    x0 = 18 * NP.ones(37)
    x0[[3, 4, 7, 8, 11, 12, 15, 16, 19, 20, 23, 24, 27, 28, 31, 32, 35]] = 5
    # No algebraic states
    z0 = NP.array([])

    # Bounds on the states. Use "inf" for unconstrained states
    x_lb = -50 * NP.ones(37)
    x_ub = 60 * NP.ones(37)
    # No algebraic states
    z_lb = NP.array([])
    z_ub = NP.array([])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    u_lb = NP.array([0, 0, 0, 0])
    u_ub = NP.array([1, 1, 0.2016, inf])
    u0 = NP.array([1, 1, 0, 0])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.ones(37)
    z_scaling = NP.array([])
    u_scaling = NP.array([1, 1, 1, 1])
    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat(x[0] - v[-2], -(x[0] - v[-1]))
    # cons = vertcat(x[0], x[1], -x[0], -x[1])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([0, -0])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 1
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([1e5, 1e5])
    # Maximum violation for the constraints
    maximum_violation = 100 * NP.array([1, 1])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])
    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    # Mayer term
    lterm = u_rad_OfficesZ1 + u_AHU1_noERC  #+ u_blinds_E + u_blinds_N + u_blinds_S + u_blinds_W #(5-u_rad_OfficesZ1)**2 + (5-u_rad_OfficesZ2)**2 + (0.7-u_blinds_S)**2 + (0.7-u_blinds_W)**2 + (0.7-u_blinds_N)**2 + (0.7-u_blinds_E)**2 + (0.1-u_AHU1_noERC)**2 + (0.1-u_AHU2_noERC)**2
    mterm = u_rad_OfficesZ1 + u_AHU1_noERC  #+ u_blinds_E + u_blinds_N + u_blinds_S + u_blinds_W #(5-u_rad_OfficesZ1)**2 + (5-u_rad_OfficesZ2)**2 + (0.7-u_blinds_S)**2 + (0.7-u_blinds_W)**2 + (0.7-u_blinds_N)**2 + (0.7-u_blinds_E)**2 + (0.1-u_AHU1_noERC)**2 + (0.1-u_AHU2_noERC)**2
    # Penalty term for the control movements
    rterm = 1e-3 * NP.array([1, 1, 1, 1])
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {
        'x': _x,
        'u': _u,
        'rhs': _xdot,
        'p': _p,
        'z': _z,
        'aes': _zdot,
        'x0': x0,
        'z0': z0,
        'x_lb': x_lb,
        'x_ub': x_ub,
        'z_lb': z_lb,
        'z_ub': z_ub,
        'u0': u0,
        'u_lb': u_lb,
        'u_ub': u_ub,
        'x_scaling': x_scaling,
        'z_scaling': z_scaling,
        'u_scaling': u_scaling,
        'cons': cons,
        'tv_p': _tv_p,
        "cons_ub": cons_ub,
        'cons_terminal': cons_terminal,
        'cons_terminal_lb': cons_terminal_lb,
        'cons_terminal_ub': cons_terminal_ub,
        'soft_constraint': soft_constraint,
        'penalty_term_cons': penalty_term_cons,
        'maximum_violation': maximum_violation,
        'mterm': mterm,
        'lterm': lterm,
        'rterm': rterm
    }

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #8
0
def model():

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    K0_ab = 1.287e12 # K0 [h^-1]
    K0_bc = 1.287e12 # K0 [h^-1]
    K0_ad = 9.043e9 # K0 [l/mol.h]
    R_gas = 8.3144621e-3 # Universal gas constant
    E_A_ab = 9758.3*1.00 #* R_gas# [kj/mol]
    E_A_bc = 9758.3*1.00 #* R_gas# [kj/mol]
    E_A_ad = 8560.0*1.0 #* R_gas# [kj/mol]
    H_R_ab = 4.2 # [kj/mol A]
    H_R_bc = -11.0 # [kj/mol B] Exothermic
    H_R_ad = -41.85 # [kj/mol A] Exothermic
    Rou = 0.9342 # Density [kg/l]
    Cp = 3.01 # Specific Heat capacity [kj/Kg.K]
    Cp_k = 2.0 # Coolant heat capacity [kj/kg.k]
    A_R = 0.215 # Area of reactor wall [m^2]
    V_R = 10.01 #0.01 # Volume of reactor [l]
    m_k = 5.0 # Coolant mass[kg]
    T_in = 130.0 # Temp of inflow [Celsius]
    K_w = 4032.0 # [kj/h.m^2.K]
    C_A0 = (5.7+4.5)/2.0*1.0 # Concentration of A in input Upper bound 5.7 lower bound 4.5 [mol/l]


    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha   = SX.sym("alpha")
    beta    = SX.sym("beta")
    # Define the differential states as CasADi symbols

    C_a    = SX.sym("C_a") # Concentration A
    C_b    = SX.sym("C_b") # Concentration B
    T_R    = SX.sym("T_R") # Reactor Temprature
    T_K    = SX.sym("T_K") # Jacket Temprature

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    F      = SX.sym("F") # Vdot/V_R [h^-1]
    Q_dot  = SX.sym("Q_dot") #Q_dot second control input

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")

    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    K_1 = beta * K0_ab * exp((-E_A_ab)/((T_R+273.15)))
    K_2 =  K0_bc * exp((-E_A_bc)/((T_R+273.15)))
    K_3 = K0_ad * exp((-alpha*E_A_ad)/((T_R+273.15)))

    # Define the differential equations

    dC_a = F*(C_A0 - C_a) -K_1*C_a - K_3*(C_a**2)
    dC_b = -F*C_b + K_1*C_a -K_2*C_b
    dT_R = ((K_1*C_a*H_R_ab + K_2*C_b*H_R_bc + K_3*(C_a**2)*H_R_ad)/(-Rou*Cp)) + F*(T_in-T_R) +(((K_w*A_R)*(T_K-T_R))/(Rou*Cp*V_R))
    dT_K = (Q_dot + K_w*A_R*(T_R-T_K))/(m_k*Cp_k)

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(C_a, C_b, T_R,T_K)

    _u = vertcat(F, Q_dot)

    _xdot = vertcat(dC_a, dC_b, dT_R, dT_K)

    _p = vertcat(alpha, beta)

    _z = []

    _tv_p = vertcat(tv_param_1, tv_param_2)

    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    C_a_0 = 0.8 # This is the initial concentration inside the tank [mol/l]
    C_b_0 = 0.5 # This is the controlled variable [mol/l]
    T_R_0 = 134.14 #[C]
    T_K_0 = 130.0 #[C]
    x0 = NP.array([C_a_0, C_b_0, T_R_0, T_K_0])

    # Bounds on the states. Use "inf" for unconstrained states
    C_a_lb = 0.1;			C_a_ub = 2.0
    C_b_lb = 0.1;			C_b_ub = 2.0
    T_R_lb = 50.0;			T_R_ub = 180
    T_K_lb = 50.0;			T_K_ub = 180
    x_lb = NP.array([C_a_lb, C_b_lb, T_R_lb, T_K_lb])
    x_ub = NP.array([C_a_ub, C_b_ub, T_R_ub, T_K_ub])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    F_lb = 5.0;                 F_ub = +100.0;
    Q_dot_lb = -8500.0;         Q_dot_ub = 0.0;
    u_lb = NP.array([F_lb, Q_dot_lb])
    u_ub = NP.array([F_ub, Q_dot_ub])
    u0 = NP.array([30.0,-6000.0])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0, 1.0])
    u_scaling = NP.array([1.0, 1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([])
    # Maximum violation for the constraints
    maximum_violation = NP.array([0])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])


    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm =  1e4*((C_b - 0.9)**2 + (C_a - 1.1)**2)
    #lterm =  - C_b
    # Mayer term
    mterm =  1e4*((C_b - 0.9)**2 + (C_a - 1.1)**2)
    #mterm =  - C_b
    # Penalty term for the control movements
    rterm = NP.array([0.0, 0.0])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': x_lb,'x_ub': x_ub, 'u0':u0, 'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb,'tv_p':_tv_p, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #9
0
def model():
    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    #    K0_ab = 1.287e12 # K0 [h^-1]
    #    K0_bc = 1.287e12 # K0 [h^-1]
    #    K0_ad = 9.043e9 # K0 [l/mol.h]
    #    R_gas = 8.3144621e-3 # Universal gas constant
    #    E_A_ab = 9758.3*1.00 #* R_gas# [kj/mol]
    #    E_A_bc = 9758.3*1.00 #* R_gas# [kj/mol]
    #    E_A_ad = 8560.0*1.0 #* R_gas# [kj/mol]
    #    H_R_ab = 4.2 # [kj/mol A]
    #    H_R_bc = -11.0 # [kj/mol B] Exothermic
    #    H_R_ad = -41.85 # [kj/mol A] Exothermic
    #    Rou = 0.9342 # Density [kg/l]
    #    Cp = 3.01 # Specific Heat capacity [kj/Kg.K]
    #    Cp_k = 2.0 # Coolant heat capacity [kj/kg.k]
    #    A_R = 0.215 # Area of reactor wall [m^2]
    #    V_R = 10.01 #0.01 # Volume of reactor [l]
    #    m_k = 5.0 # Coolant mass[kg]
    #    T_in = 130.0 # Temp of inflow [Celsius]
    #    K_w = 4032.0 # [kj/h.m^2.K]
    #    C_A0 = (5.7+4.5)/2.0*1.0 # Concentration of A in input Upper bound 5.7 lower bound 4.5 [mol/l]
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha = SX.sym("alpha")
    beta = SX.sym("beta")

    # Define the differential states as CasADi symbols
    x = SX.sym("x")  # x position of the robot
    y = SX.sym("y")  # y position of the robot
    theta = SX.sym("theta")  # heading angle of the robot

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols
    v = SX.sym("v")  # linear velocity of the robot
    w = SX.sym("w")  # angular velocity of the robot

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    #    K_1 = beta * K0_ab * exp((-E_A_ab)/((T_R+273.15)))
    #    K_2 =  K0_bc * exp((-E_A_bc)/((T_R+273.15)))
    #    K_3 = K0_ad * exp((-alpha*E_A_ad)/((T_R+273.15)))

    # Define the differential equations
    dd = SX.sym("dd", 3)
    dd[0] = v * cos(theta) * alpha
    dd[1] = v * sin(theta) * beta
    dd[2] = w

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(x, y, theta)

    _u = vertcat(v, w)

    _xdot = vertcat(dd)

    _p = vertcat(alpha, beta)

    _z = []  # toggle if there are no AE in your model

    _tv_p = vertcat(tv_param_1, tv_param_2)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states

    x_init = 5.0
    y_init = 5.0
    theta_init = 0.0
    x0 = NP.array([x_init, y_init, theta_init])

    # Bounds on the states. Use "inf" for unconstrained states

    x_lb = 0.0
    x_ub = 30.0
    y_lb = 0.0
    y_ub = 30.0
    theta_lb = -2 * pi
    theta_ub = 2 * pi
    x_lb = NP.array([x_lb, y_lb, theta_lb])
    x_ub = NP.array([x_ub, y_ub, theta_ub])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs

    v_lb = -1.4
    v_ub = 1.4
    v_init = 0.0
    w_lb = -1.0
    w_ub = 1.0
    w_init = 0.0
    u_lb = NP.array([v_lb, w_lb])
    u_ub = NP.array([v_ub, w_ub])
    u0 = NP.array([v_init, w_init])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0])
    u_scaling = NP.array([1.0, 1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([])
    # Maximum violation for the constraints
    maximum_violation = NP.array([0])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])
    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm = 14 * (x - 15)**2 + 6 * (y - 15)**2 + 8 * (theta -
                                                      pi / 2)**2 + v**2 + w**2

    # Mayer term
    mterm = 0

    # Penalty term for the control movements
    rterm = NP.array([0.0001, 0.0001])
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {
        'x': _x,
        'u': _u,
        'rhs': _xdot,
        'p': _p,
        'z': _z,
        'x0': x0,
        'x_lb': x_lb,
        'x_ub': x_ub,
        'u0': u0,
        'u_lb': u_lb,
        'u_ub': u_ub,
        'x_scaling': x_scaling,
        'u_scaling': u_scaling,
        'cons': cons,
        "cons_ub": cons_ub,
        'cons_terminal': cons_terminal,
        'cons_terminal_lb': cons_terminal_lb,
        'tv_p': _tv_p,
        'cons_terminal_ub': cons_terminal_ub,
        'soft_constraint': soft_constraint,
        'penalty_term_cons': penalty_term_cons,
        'maximum_violation': maximum_violation,
        'mterm': mterm,
        'lterm': lterm,
        'rterm': rterm
    }

    model = core_do_mpc.model(model_dict)

    return model
Beispiel #10
0
def model():
    lbub = NP.load('Neural_Network\lbub.npy')
    x_lb_NN = lbub[0]
    x_ub_NN = lbub[1]
    y_lb_NN = lbub[2]
    y_ub_NN = lbub[3]

    json_file = open('Neural_Network\model_3.json', 'r')
    model_json = json_file.read()
    json_file.close()
    model = model_from_json(model_json)
    # load weights into new model
    model.load_weights("Neural_Network\model_3.h5")
    Theta = {}
    # for i in range(len(model.layers)):
    i = 0
    for j in [0, 2, 4, 6, 8]:
        weights = model.layers[j].get_weights()
        Theta['Theta' + str(i + 1)] = NP.insert(weights[0].transpose(),
                                                0,
                                                weights[1].transpose(),
                                                axis=1)
        i += 1
    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    alpha = MX.sym("alpha")
    # Define the differential states as CasADi symbols

    # x = MX.sym("x", Theta['Theta1'].shape[1]-1)
    features = 15
    numbers = 2 - 1
    x = MX.sym("x", features * numbers)

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    u_rad_OfficesZ1 = MX.sym("u_rad_OfficesZ1")
    u_AHU1_noERC = MX.sym("u_AHU1_noERC")
    u_blinds_E = MX.sym("u_blinds_E")
    u_blinds_N = MX.sym("u_blinds_N")
    u_blinds_S = MX.sym("u_blinds_S")
    u_blinds_W = MX.sym("u_blinds_W")

    u = vertcat(u_blinds_E, u_blinds_N, u_blinds_S, u_blinds_W, u_AHU1_noERC,
                u_rad_OfficesZ1)

    # Define time-varying parameters that can chance at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions
    v_IG_Offices = MX.sym("V_IG_Offices")
    v_Tamb = MX.sym("v_Tamb")
    v_solGlobFac_S = MX.sym("v_solGlobFac_S")
    v_solGlobFac_W = MX.sym("v_solGlobFac_W")
    v_solGlobFac_N = MX.sym("v_solGlobFac_N")
    v_solGlobFac_E = MX.sym("v_solGlobFac_E")
    v_windspeed = MX.sym("v_windspeed")
    setp_ub = MX.sym("setp_ub")
    setp_lb = MX.sym("setp_lb")

    u_ahu_ub = MX.sym("u_ahu_ub")

    v = vertcat(v_IG_Offices, v_Tamb, v_solGlobFac_E, v_solGlobFac_N,
                v_solGlobFac_S, v_solGlobFac_W, v_windspeed, u_ahu_ub, setp_ub,
                setp_lb)
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    input = vertcat(x, u, v[0:7])
    input = NP.divide(input - x_lb_NN, x_ub_NN -
                      x_lb_NN)  #(input - x_lb_NN)*float(1)/(x_ub_NN - x_lb_NN)
    a1 = input
    a1 = vertcat(1, a1)
    z1 = mtimes(Theta['Theta1'], a1)
    a2 = tanh(z1)
    a2 = vertcat(1, a2)

    z2 = mtimes(Theta['Theta2'], a2)
    a3 = tanh(z2)
    a3 = vertcat(1, a3)

    z3 = mtimes(Theta['Theta3'], a3)
    a4 = tanh(z3)
    a4 = vertcat(1, a4)

    z4 = mtimes(Theta['Theta4'], a4)
    a5 = tanh(z4)
    a5 = vertcat(1, a5)

    z5 = mtimes(Theta['Theta5'], a5)
    a6 = z5

    dx1 = MX.sym("dx1")
    dx2 = MX.sym("dx2")

    dx1 = NP.multiply(a6[0], y_ub_NN[0] -
                      y_lb_NN[0]) + y_lb_NN[0]  #dx*(y_ub_NN-y_lb_NN) + y_lb_NN
    dx2 = NP.multiply(a6[1], y_ub_NN[1] - y_lb_NN[1]) + y_lb_NN[1]
    dx = vertcat(dx1, dx2, u, v[0:7])
    # Concatenate differential states, algebraic states, control inputs and right-hand-sides
    _x = x

    _z = vertcat([])

    _u = u

    _xdot = dx

    _zdot = vertcat([])

    # _p = vertcat(alpha, beta)
    _p = vertcat(alpha)

    # _tv_p = vertcat([])
    _tv_p = vertcat(v)
    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    x0 = NP.array([18, 0, 0, 0, 0, 0, 0, 18, 141, 5, 0, 0, 0, 0, 2])
    # No algebraic states
    z0 = NP.array([])
    # Bounds on the states. Use "inf" for unconstrained states
    x_lb = -20000 * NP.ones(features * numbers)  #x_lb_NN[0:features*numbers]
    x_lb[1] = 0
    x_ub = 20000 * NP.ones(features * numbers)  #x_ub_NN[0:features*numbers]
    # No algebraic states
    z_lb = NP.array([])
    z_ub = NP.array([])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    u_lb = NP.array([0, 0, 0, 0, 0, 16])
    u_ub = NP.array([1, 1, 1, 1, 1, 23])
    u0 = NP.array([0, 0, 0, 0, 0, 21])
    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.ones(features * numbers)
    z_scaling = NP.array([])
    u_scaling = NP.array([1, 1, 1, 1, 1, 1])
    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat(x[features * (numbers - 1)] - v[-2],
                   -(x[features * (numbers - 1)] - v[-1]))
    # cons = vertcat(x[0], -x[0])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_ub = NP.array([0, -0])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 1
    # Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([1e5, 1e5])
    # Maximum violation for the constraints
    maximum_violation = 100 * NP.array([1, 1])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat()
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])
    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    # Mayer term
    lterm = 1 * u_rad_OfficesZ1 + 0.1 * x[
        1]  #+ u_AHU1_noERC   # + (u_rad_OfficesZ1-18)*u_AHU1_noERC  #+ u_blinds_E + u_blinds_N + u_blinds_S + u_blinds_W # - (u_AHU1_noERC-25)**2  #(5-u_rad_OfficesZ1)**2 + (5-u_rad_OfficesZ2)**2 + (0.7-u_blinds_S)**2 + (0.7-u_blinds_W)**2 + (0.7-u_blinds_N)**2 + (0.7-u_blinds_E)**2 + (0.1-u_AHU1_noERC)**2 + (0.1-u_AHU2_noERC)**2
    mterm = 1 * u_rad_OfficesZ1 + 0.1 * x[
        1]  #+ u_AHU1_noERC  # + (u_rad_OfficesZ1-18)*u_AHU1_noERC  #+ u_blinds_E + u_blinds_N + u_blinds_S + u_blinds_W #- (u_AHU1_noERC-25)**2 #(5-u_rad_OfficesZ1)**2 + (5-u_rad_OfficesZ2)**2 + (0.7-u_blinds_S)**2 + (0.7-u_blinds_W)**2 + (0.7-u_blinds_N)**2 + (0.7-u_blinds_E)**2 + (0.1-u_AHU1_noERC)**2 + (0.1-u_AHU2_noERC)**2
    # Penalty term for the control movements 1e4, 100
    rterm = 5e1 * NP.array([1, 1, 1, 1, 1e-1, 1e-1])
    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {
        'x': _x,
        'u': _u,
        'rhs': _xdot,
        'p': _p,
        'z': _z,
        'aes': _zdot,
        'x0': x0,
        'z0': z0,
        'x_lb': x_lb,
        'x_ub': x_ub,
        'z_lb': z_lb,
        'z_ub': z_ub,
        'u0': u0,
        'u_lb': u_lb,
        'u_ub': u_ub,
        'x_scaling': x_scaling,
        'z_scaling': z_scaling,
        'u_scaling': u_scaling,
        'cons': cons,
        'tv_p': _tv_p,
        "cons_ub": cons_ub,
        'cons_terminal': cons_terminal,
        'cons_terminal_lb': cons_terminal_lb,
        'cons_terminal_ub': cons_terminal_ub,
        'soft_constraint': soft_constraint,
        'penalty_term_cons': penalty_term_cons,
        'maximum_violation': maximum_violation,
        'mterm': mterm,
        'lterm': lterm,
        'rterm': rterm
    }
    model = core_do_mpc.model(model_dict)

    return model
Beispiel #11
0
def model():

    """
    --------------------------------------------------------------------------
    template_model: define the non-uncertain parameters
    --------------------------------------------------------------------------
    """

    mu_m	= 0.02
    K_m	= 0.05
    K_i	= 5.0
    v_par	= 0.004
    Y_p	= 1.2

    """
    --------------------------------------------------------------------------
    template_model: define uncertain parameters, states and controls as symbols
    --------------------------------------------------------------------------
    """
    # Define the uncertainties as CasADi symbols

    Y_x   = SX.sym("Y_x")
    S_in  = SX.sym("S_in")

    # Define the differential states as CasADi symbols

    X_s    = SX.sym("X_s") # Concentration A
    S_s    = SX.sym("S_s") # Concentration B
    P_s    = SX.sym("P_s") # Reactor Temprature
    V_s    = SX.sym("V_s") # Jacket Temprature

    # Define the algebraic states as CasADi symbols

    # Define the control inputs as CasADi symbols

    inp      = SX.sym("inp") # Vdot/V_R [h^-1]

    # Define time-varying parameters that can change at each step of the prediction and at each sampling time of the MPC controller. For example, future weather predictions

    tv_param_1 = SX.sym("tv_param_1")
    tv_param_2 = SX.sym("tv_param_2")
    """
    --------------------------------------------------------------------------
    template_model: define algebraic and differential equations
    --------------------------------------------------------------------------
    """
    # Define the algebraic equations

    mu_S	= mu_m*S_s/(K_m+S_s+(S_s**2/K_i))

    # Define the differential equations

    dX_s	= mu_S*X_s - inp/V_s*X_s
    dS_s	= -mu_S*X_s/Y_x - v_par*X_s/Y_p + inp/V_s*(S_in-S_s)
    dP_s	= v_par*X_s - inp/V_s*P_s
    dV_s	= inp

    # Concatenate differential states, algebraic states, control inputs and right-hand-sides

    _x = vertcat(X_s, S_s, P_s, V_s)

    _u = vertcat(inp)

    _xdot = vertcat(dX_s, dS_s, dP_s, dV_s)

    _p = vertcat(Y_x, S_in)

    _z = []

    _tv_p = vertcat(tv_param_1, tv_param_2)

    """
    --------------------------------------------------------------------------
    template_model: initial condition and constraints
    --------------------------------------------------------------------------
    """
    # Initial condition for the states
    X_s_0 = 1.0
    S_s_0 = 0.5
    P_s_0 = 0.0
    V_s_0 = 120.0
    x0 = NP.array([X_s_0, S_s_0, P_s_0, V_s_0])

    # Bounds on the states. Use "inf" for unconstrained states
    X_s_lb = 0.0;			X_s_ub = 3.7
    S_s_lb = -0.01;		      S_s_ub = inf
    P_s_lb = 0.0;		      P_s_ub = 3.0
    V_s_lb = 0.0;	           V_s_ub = inf
    x_lb = NP.array([X_s_lb, S_s_lb, P_s_lb, V_s_lb])
    x_ub = NP.array([X_s_ub, S_s_ub, P_s_ub, V_s_ub])

    # Bounds on the control inputs. Use "inf" for unconstrained inputs
    inp_lb = 0.0;                 inp_ub = 0.2;

    u_lb = NP.array([inp_lb])
    u_ub = NP.array([inp_ub])
    u0 = NP.array([0.03])

    # Scaling factors for the states and control inputs. Important if the system is ill-conditioned
    x_scaling = NP.array([1.0, 1.0, 1.0, 1.0])
    u_scaling = NP.array([1.0])

    # Other possibly nonlinear constraints in the form cons(x,u,p) <= cons_ub
    # Define the expresion of the constraint (leave it empty if not necessary)
    cons = vertcat([])
    # Define the upper bounds of the constraint (leave it empty if not necessary)

    cons_ub = NP.array([])

    # Activate if the nonlinear constraints should be implemented as soft constraints
    soft_constraint = 0
    # l1 - Penalty term to add in the cost function for the constraints (it should be the same size as cons)
    penalty_term_cons = NP.array([1e4])
    # Maximum violation for the upper and lower bounds
    maximum_violation = NP.array([10])

    # Define the terminal constraint (leave it empty if not necessary)
    cons_terminal = vertcat([])
    # Define the lower and upper bounds of the constraint (leave it empty if not necessary)
    cons_terminal_lb = NP.array([])
    cons_terminal_ub = NP.array([])


    """
    --------------------------------------------------------------------------
    template_model: cost function
    --------------------------------------------------------------------------
    """
    # Define the cost function
    # Lagrange term
    lterm =  -P_s
    #lterm =  - C_b
    # Mayer term
    mterm =  -P_s
    #mterm =  - C_b
    # Penalty term for the control movements
    rterm = NP.array([0.0])



    """
    --------------------------------------------------------------------------
    template_model: pass information (not necessary to edit)
    --------------------------------------------------------------------------
    """
    model_dict = {'x':_x,'u': _u, 'rhs':_xdot,'p': _p, 'z':_z,'x0': x0,'x_lb': x_lb,'x_ub': x_ub, 'u0':u0, 'u_lb':u_lb, 'u_ub':u_ub, 'x_scaling':x_scaling, 'u_scaling':u_scaling, 'cons':cons,
    "cons_ub": cons_ub, 'cons_terminal':cons_terminal, 'cons_terminal_lb': cons_terminal_lb,'tv_p':_tv_p, 'cons_terminal_ub':cons_terminal_ub, 'soft_constraint': soft_constraint, 'penalty_term_cons': penalty_term_cons, 'maximum_violation': maximum_violation, 'mterm': mterm,'lterm':lterm, 'rterm':rterm}

    model = core_do_mpc.model(model_dict)

    return model