Beispiel #1
0
    def create_mpc(self, mpc_type, N, opts={}, tuning=None):
        """ Create MPC controller of user-defined type  and horizon
        for given system dynamics, constraints and cost function.
        """

        if mpc_type not in ['economic', 'tuned', 'tracking']:
            raise ValueError('Provided MPC type not supported.')

        if 'slack_flag' in opts.keys():
            mpc_sys = preprocessing.add_mpc_slacks(
                copy.deepcopy(self.__sys),
                self.__ocp.lam_g,
                self.__ocp.indeces_As,
                slack_flag=opts['slack_flag'])
        else:
            mpc_sys = self.__sys

        if mpc_type == 'economic':
            mpc = pmpc.Pmpc(N=N,
                            sys=mpc_sys,
                            cost=self.__l,
                            wref=self.__w_sol,
                            lam_g_ref=self.__ocp.lam_g,
                            options=opts)
        else:
            cost = self.__tracking_cost(self.__nx + self.__nu + self.__nus)
            lam_g0 = copy.deepcopy(self.__ocp.lam_g)
            lam_g0['dyn'] = 0.0
            if 'g' in lam_g0.keys():
                lam_g0['g'] = 0.0

            if mpc_type == 'tracking':
                if tuning is None:
                    raise ValueError(
                        'Tracking type MPC controller requires user-provided tuning!'
                    )
            elif mpc_type == 'tuned':
                tuning = {'H': self.__S['Hc'], 'q': self.__S['q']}
            mpc = pmpc.Pmpc(N=N,
                            sys=mpc_sys,
                            cost=cost,
                            wref=self.__w_sol,
                            tuning=tuning,
                            lam_g_ref=lam_g0,
                            options=opts)

        return mpc
Beispiel #2
0
mpc_sys = preprocessing.add_mpc_slacks(
    sol['sys'],
    sol['lam_g'],
    sol['indeces_As'],
    slack_flag = 'active'
)
# create controllers
ctrls = {}

# # economic MPC
tuning = {'H': sol['S']['Hc'], 'q': sol['S']['q']}
ctrls['EMPC'] = pmpc.Pmpc(
    N = Nmpc,
    sys = mpc_sys,
    cost = user_input['l'],
    wref = sol['wsol'],
    lam_g_ref = sol['lam_g'],
    sensitivities= sol['S'],
    options = opts
)

# prepare tracking cost and initialization
tracking_cost = mtools.tracking_cost(nx+nu+ns)
lam_g0 = copy.deepcopy(sol['lam_g'])
lam_g0['dyn'] = 0.0
lam_g0['g'] = 0.0
  
# standard tracking MPC
tuningTn = {'H': [np.diag((nx+nu)*[1]+ns*[1e-10])]*user_input['p'], 'q': sol['S']['q']}
ctrls['TMPC_1'] = pmpc.Pmpc(
    N = Nmpc,