def test_input_formatting_mixed_constraints(): """ Test formatting of user provided input with linear constraints. """ x = ca.MX.sym('x',1) u = ca.MX.sym('u',2) # case of mixed constraints sys = {} sys['F'] = ca.Function('F',[x,u],[x]) h = ca.Function('h',[x,u],[ca.vertcat(x+u[0], u[1], x**2*u[0])]) sys['h'] = h sys = preprocessing.input_formatting(sys) assert 'h' in sys, 'lin. inequalites are being removed' assert sys['h'].size1_out(0) == 3, 'lin inequalities have wrong dimension' assert 'g' in sys, 'nonlin. inequalities are not being created' assert sys['g'].size1_out(0) == 1, 'nonlin inequalities have wrong dimension' assert 'vars' in sys, 'system variables are not being created' assert sys['vars']['x'].shape == x.shape, 'x-variable dimensions are incorrect' assert sys['vars']['u'].shape == u.shape, 'u-variable dimensions are incorrect' assert sys['vars']['us'].shape == (1,1), 'us-variable dimensions are incorrect' x_num = 1.0 u_num = ca.vertcat(0.0, 2.0) us_num = ca.vertcat(3.0) h_eval = sys['h'](x_num, u_num, us_num).full() g_eval = sys['g'](x_num, u_num, us_num).full() assert ca.vertsplit(h_eval) == [1.0, 2.0, 3.0] assert ca.vertsplit(g_eval) == [-3.0]
def __init__(self, f, l, h=None, p=1): """ Constructor """ # print license information self.__log_license_info() Logger.logger.info(60 * '=') Logger.logger.info(18 * ' ' + 'Create Tuner instance...') Logger.logger.info(60 * '=') Logger.logger.info('') # construct system sys = {'f': f} if h is not None: sys['h'] = h # detect nonlinear constraints Logger.logger.info('Detect nonlinear constraints...') self.__sys = preprocessing.input_formatting(sys) # problem dimensions self.__nx = sys['vars']['x'].shape[0] self.__nu = sys['vars']['u'].shape[0] self.__nw = self.__nx + self.__nu self.__p = p if 'us' in sys['vars'].keys(): self.__nus = sys['vars']['us'].shape[0] else: self.__nus = 0 # construct p-periodic OCP if self.__p == 1: Logger.logger.info( 'Construct steady-state optimization problem...') else: Logger.logger.info( 'Construct {}-periodic optimal control problem...'.format( self.__p)) self.__l = l self.__ocp = pocp.Pocp(sys=sys, cost=l, period=p) Logger.logger.info('') Logger.logger.info('Tuner instance created:') self.__log_problem_dimensions() return None
def test_input_formatting_no_constraints(): """ Test formatting of user provided input w/o constraints. """ 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 = preprocessing.input_formatting(sys) assert 'h' not in sys, 'non-existent lin. inequalites are being created' assert 'g' not in sys, 'non-existent nonlin. inequalities are being created' assert 'vars' in sys, 'system variables are not being created' assert sys['vars']['x'].shape == x.shape, 'x-variable dimensions are incorrect' assert sys['vars']['u'].shape == u.shape, 'u-variable dimensions are incorrect'
def test_input_formatting_lin_constraints(): """ Test formatting of user provided input with linear constraints. """ x = ca.MX.sym('x',1) u = ca.MX.sym('u',2) # case of linear 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 = preprocessing.input_formatting(sys) assert 'h' in sys, 'lin. inequalites are being removed' assert sys['h'].size1_out(0) == 2, 'lin inequalities dimension is altered incorrectly' assert 'g' not in sys, 'non-existent nonlin. inequalities are being created' assert 'vars' in sys, 'system variables are not being created' assert sys['vars']['x'].shape == x.shape, 'x-variable dimensions are incorrect' assert sys['vars']['u'].shape == u.shape, 'u-variable dimensions are incorrect'