Exemplo n.º 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
Exemplo n.º 2
0
def test_add_mpc_slacks_no_constraints():

    """ Test automatic soft constraint generation if no constraints are present.
    """

    x = ca.MX.sym('x',1)
    u = ca.MX.sym('u',2)

    # case of no constraints
    sys = {}
    sys['F'] = ca.Function('F',[x,u],[x])
    sys['vars'] = collections.OrderedDict()
    sys['vars']['x'] = x
    sys['vars']['u'] = u
    sys = preprocessing.add_mpc_slacks(sys, None, None, slack_flag = 'active')
    
    assert 'h' not in sys, 'non-existent lin. inequalities are being created'
    assert 'usc' not in sys['vars'], 'non-existent slack variables are being created'
Exemplo n.º 3
0
def test_add_mpc_slacks_no_active_constraints():

    """ Test automatic soft constraint generation if no active constraints are present.
    """

    x = ca.MX.sym('x',1)
    u = ca.MX.sym('u',2)

    # case of no active constraints
    sys = {}
    sys['F'] = ca.Function('F',[x,u],[x])
    h = ca.Function('h',[x,u],[ca.vertcat(x+u[0], u[1])])
    sys['h'] = h
    sys['vars'] = collections.OrderedDict()
    sys['vars']['x'] = x
    sys['vars']['u'] = u
    active_set = [[],[]]
    sys = preprocessing.add_mpc_slacks(sys, None, active_set, slack_flag = 'active')
    
    assert 'h' in sys, 'lin. inequalities are being removed'
    assert 'usc' not in sys['vars'], 'non-existent slack variables are being created'
Exemplo n.º 4
0
def test_add_mpc_slacks_active_constraints():
    """ Test automatic soft constraint generation if active constraints are present.
    """

    x = ca.MX.sym('x', 1)
    u = ca.MX.sym('u', 2)

    # case of no active constraints
    sys = {}
    sys['f'] = ca.Function('f', [x, u], [x])
    h = ca.Function('h', [x, u], [ca.vertcat(x + u[0], u[1])])
    sys['h'] = h
    sys['vars'] = collections.OrderedDict()
    sys['vars']['x'] = x
    sys['vars']['u'] = u

    lam_g_struct = ca.tools.struct_symMX(
        [ca.tools.entry('h', shape=(2, 1), repeat=2)])
    lam_g = lam_g_struct(0.0)
    lam_g['h', 0, 0] = -5.0
    active_set = [[0], []]
    sys = preprocessing.add_mpc_slacks(sys,
                                       lam_g,
                                       active_set,
                                       slack_flag='active')

    assert 'h' in sys, 'lin. inequalities are being removed'
    assert 'usc' in sys['vars'], 'slack variables are not being created'
    assert sys['vars']['usc'].shape[
        0] == 1, 'slack variables are not being created'
    assert 'scost' in sys, 'slack cost is not being created'

    x_num = 1.0
    u_num = ca.vertcat(0.0, 2.0)
    usc_num = ca.vertcat(3.0)
    h_eval = sys['h'](x_num, u_num, usc_num).full()
    scost_eval = sys['scost'].full()
    print(scost_eval)
    assert ca.vertsplit(h_eval) == [4.0, 2.0, 3.0]
    assert scost_eval[0][0] == 5000.
Exemplo n.º 5
0
    sol['wsol']['x',0][2]**2
)

opts = {}
# add projection operator for terminal constraint
opts['p_operator'] = ca.Function(
    'p_operator',
    [sol['sys']['vars']['x']],
    [ct.vertcat(sol['sys']['vars']['x'][1:3],
    sol['sys']['vars']['x'][4:])]
)

# add MPC slacks to active constraints
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