0001 function demo_2_link_arm
0002
0003
0004
0005 clear all;clc;close all;rng('default');
0006 fprintf('<=========================================================================>\r');
0007 fprintf('<=========================================================================>\r');
0008 fprintf('<=========== Constraint Consistent Learning Library =================>\r');
0009 fprintf('< This demo will go through a 2 link arm example to demonstrate this CCL >\r');
0010 fprintf('< toolbox. The CCL is formulated in the following format: > \r');
0011 fprintf('< Consider the set of consistent k-dimensional constraints: >\r');
0012 fprintf('< A(x)U(x) = b(x) where A(x) = lamba(x)J(x) >\r');
0013 fprintf('< U(x) = pinv(A(x))b(x) + (I-pinv(A(x))A(x))Pi(x) >\r');
0014 fprintf('< U(x) = U_ts + U_ns >\r');
0015 fprintf('< The task is defined in 2D. The constraints are either random or state >\r');
0016 fprintf('< dependant parabola. The null space control policies are either linear >\r');
0017 fprintf('< attractor or limit cycle. This demo will execute section by section and >\r');
0018 fprintf('< allow the user to configure the training parameters. >\r');
0019 fprintf('< List of sections: >\r');
0020 fprintf('< SECTION 1: PARAMETER CONFIGURATION >\r');
0021 fprintf('< SECTION 2: LEARNING NULL SPACE COMPONENTS >\r');
0022 fprintf('< SECTION 3: LEARNING NULL SPACE CONSTRAINTS >\r');
0023 fprintf('< SECTION 4: LEARNING NULL SPACE POLICY >\r');
0024 fprintf('< Configuration options: >\r');
0025 fprintf('< Constraints: >\r');
0026 fprintf('< State independant: >\r');
0027 fprintf('< linear (random) >\r');
0028 fprintf('< State dependant: >\r');
0029 fprintf('< parabola >\r');
0030 fprintf('< Null space policy: >\r');
0031 fprintf('< linear attractor >\r');
0032 fprintf('< Task space policy: >\r');
0033 fprintf('< random >\n');
0034 fprintf('<=========================================================================>\r');
0035 fprintf('<=========================================================================>\r');
0036 fprintf('<=========================================================================>\n\n\n');
0037 fprintf('<=========================================================================>\n');
0038 fprintf('<=========================================================================>\n');
0039 fprintf('<=========================================================================>\n');
0040 fprintf('< SECTION 1: PARAMETER CONFIGURATION >\r');
0041 fprintf('\n< User specified configurations are: >\r');
0042
0043 settings.dim_x = 2 ;
0044 settings.dim_u = 2 ;
0045 settings.dim_r = 2 ;
0046 settings.dim_k = 1 ;
0047 settings.dt = 0.01 ;
0048 settings.s2y = .01;
0049 settings.null.alpha = 1 ;
0050 settings.null.target = pi/180*[10, -10]';
0051 settings.task.target = @(n)(randi([-2,2])*rand(1)) ;
0052 settings.link.length = [1, 1] ;
0053 settings.projection = 'state_dependant';
0054 settings.task_policy_type = 'linear_attractor';
0055 settings.null_policy_type = 'linear_attractor';
0056 settings.f_n = @(q) settings.null.alpha .* (settings.null.target - q) ;
0057
0058 J = @(q)ccl_rob_jacobian(q,settings.link.length);
0059 settings.output.show_traj = 0 ;
0060 settings.control_space = 'joint';
0061 fprintf('< Dim_x = %d >\r',settings.dim_x);
0062 fprintf('< Dim_u = %d >\r',settings.dim_u);
0063 fprintf('< Dim_r = %d >\r',settings.dim_r);
0064 fprintf('< Dim_k = %d >\r',settings.dim_k);
0065 fprintf('< Constraint = %s >\r',settings.projection);
0066 fprintf('< Null_policy_type = %s >\r',settings.null_policy_type);
0067 fprintf('< Task_policy_type = %s >\r',settings.task_policy_type);
0068 fprintf('<=========================================================================>\n');
0069 fprintf('<=========================================================================>\n');
0070 fprintf('<=========================================================================>\n');
0071 pause();
0072
0073
0074
0075
0076 fprintf('\n\n\n<=========================================================================>\n');
0077 fprintf('<=========================================================================>\n');
0078 fprintf('<=========================================================================>\n');
0079 fprintf('< SECTION 2: LEARNING NULL SPACE COMPONENTS >\r');
0080 fprintf('< In this section,we are trying to learn U_ns out of the observations(X,U)>\r');
0081 fprintf('< The cost function to be minimised is: >\r');
0082 fprintf('< E[Pi_ns] = sum(||P*U - Pi_ns)||.^2 >\r');
0083 fprintf('< In order to achieve good performance, for each constraint, enough data >\r');
0084 fprintf('< samples and model complexity are necessary >\n');
0085 fprintf('\n< For details please refer to: >\r');
0086 fprintf('< Towell, M. Howard, and S. Vijayakumar. IEEE International Conference >\r');
0087 fprintf('< Intelligent Robots and Systems, 2010. >\n');
0088 fprintf(1,'\n< Start learning Null space component ... > \r');
0089 settings.dim_exp = 1 ;
0090 settings.dim_n = 40 ;
0091 settings.nTraj = 40 ;
0092 settings.grid_on = 0 ;
0093 settings.learn_nc = 1 ;
0094 model = [];
0095 for i = 1:settings.dim_exp
0096
0097
0098 settings.fix_joint = 1;
0099 model.num_basis = 100 ;
0100
0101 fprintf(1,'\n Experiment %d \n', i) ;
0102 fprintf(1,'\t Dimensionality of kernel: %d \n', model.num_basis) ;
0103 fprintf(1,'\t Number of trajectories: %d \n', settings.nTraj) ;
0104 fprintf(1,'\t Number of time steps in each trajectory: %d \n', settings.dim_n) ;
0105
0106 fprintf('\n< Generating training dataset for learning null space components ... >\r');
0107 Dtr = ccl_data_gen (settings) ;
0108 Xtr = Dtr.X ; Ytr = Dtr.U ; TStr = Dtr.TS ; NStr = Dtr.NS ; Ftr = Dtr.F ;
0109 fprintf(1,'#Data (train): %5d, \r',settings.dim_n*settings.nTraj);
0110 fprintf('< Generating testing dataset for learning null space components ... >\n');
0111 Dte = ccl_data_gen (settings) ;
0112 Xte = Dte.X ; Yte = Dte.U ; TSte = Dtr.TS ; NSte = Dte.NS ; Fte = Dte.F ;
0113 fprintf(1,'#Data (test): %5d, \n',settings.dim_n*settings.nTraj);
0114
0115
0116 model.c = ccl_math_gc (Xtr, model.num_basis) ;
0117 model.s2 = mean(mean(sqrt(ccl_math_distances(model.c, model.c))))^2 ;
0118 model.phi = @(x)ccl_basis_rbf ( x, model.c, model.s2 );
0119
0120
0121 model = ccl_learnv_ncl (Xtr, Ytr, model) ;
0122 f_ncl = @(x) ccl_learnv_pred_ncl ( model, x ) ;
0123
0124
0125 NSptr = f_ncl (Xtr) ;
0126 NSpte = f_ncl (Xte) ;
0127
0128
0129 NUPEtr = ccl_error_nupe(NStr, NSptr) ;
0130 NUPEte = ccl_error_nupe(NSte, NSpte) ;
0131 YNSptr = ccl_error_npe (Ytr, NSptr) ;
0132 YNSpte = ccl_error_npe (Yte, NSpte) ;
0133 fprintf(1,'NUPE (train) = %5.3e, ', NUPEtr);
0134 fprintf(1,'NNPE (train) = %5.3e, ', YNSptr);
0135 fprintf(1,'\n');
0136 fprintf(1,'NUPE (test) = %5.3e, ', NUPEte);
0137 fprintf(1,'NNPE (test) = %5.3e, ', YNSpte);
0138 fprintf(1,'\n');
0139 fprintf('<=========================================================================>\n');
0140 fprintf('<=========================================================================>\n');
0141 fprintf('<=========================================================================>\n');
0142 end
0143 pause();
0144
0145
0146
0147
0148 fprintf('\n\n\n<=========================================================================>\n');
0149 fprintf('<=========================================================================>\n');
0150 fprintf('<=========================================================================>\n');
0151 fprintf('< SECTION 3: LEARNING NULL SPACE CONSTRAINTS >\r');
0152 fprintf('< In this section,we will start addressing state independant constraint A >\r');
0153 fprintf('< and state dependant constraint A(x). The exploration policy will change >\r');
0154 fprintf('< change the performance of the learnt constraint >\r');
0155 fprintf('< The cost function to be minimised is: >\r');
0156 fprintf('< E[A] = min(sum||A*Un||^2) >\n');
0157 fprintf('\n< For details please refer to: >\r');
0158 fprintf('< H.-C. Lin, M. Howard, and S. Vijayakumar. IEEE International Conference >\r');
0159 fprintf('< Robotics and Automation, 2015 >\n');
0160 settings.dim_b = 10 ;
0161 settings.dim_n = 40 ;
0162 settings.nTraj = 40 ;
0163 settings.dim_exp = 1 ;
0164 settings.task_policy_type = ' ';
0165 settings.learn_nc = 0 ;
0166 settings.learn_lambda = 1 ;
0167 for i = 1:settings.dim_exp
0168 if strcmp(settings.projection, 'state_independant')
0169 settings.lambda = orth( rand(2, 1) )';
0170 fprintf(1,'\n< Start learning state independant null space projection ... > \n');
0171
0172 fprintf('\n< Generating training dataset for learning constraints ... >\r');
0173 Dtr = ccl_data_gen (settings) ;
0174 fprintf(1,'#Data (train): %5d, \r',settings.dim_n*settings.nTraj);
0175 fprintf('< Generating testing dataset for learning constraints ... >\r');
0176 Dte = ccl_data_gen (settings) ;
0177 fprintf(1,'#Data (test): %5d, \n',settings.dim_n*settings.nTraj);
0178
0179 elseif strcmp(settings.projection, 'state_dependant')
0180 settings.lambda = @(q)ccl_learna_retlambda(q,settings.link.length);
0181 fprintf(1,'\n< Start learning state dependant null space projection ... > \n');
0182 fprintf('\n< Generating training dataset for learning constraints ... >\r');
0183 Dtr = ccl_data_gen (settings) ;
0184 fprintf(1,'#Data (train): %5d, \r',settings.dim_n*settings.nTraj);
0185 fprintf('< Generating testing dataset for learning constraints ... >\r');
0186 Dte = ccl_data_gen (settings) ;
0187 fprintf(1,'#Data (test): %5d, \n',settings.dim_n*settings.nTraj);
0188
0189 end
0190
0191 Xtr = Dtr.X ; Ytr = Dtr.U ; TStr = Dtr.TS ; NStr = Dtr.NS ; Ftr = Dtr.F ;
0192 Xte = Dte.X ; Yte = Dte.U ; TSte = Dtr.TS ; NSte = Dte.NS ; Fte = Dte.F ;
0193
0194 fprintf(1,'\n Experiment %d \n', i) ;
0195 fprintf(1,'\t Dimensionality of kernel: %d \n', settings.dim_b) ;
0196 fprintf(1,'\t Number of trajectories: %d \n', settings.nTraj) ;
0197 fprintf(1,'\t Number of time steps in each trajectory: %d \n', settings.dim_n) ;
0198
0199
0200 fprintf(1,'\n Learning state-dependent constraint vectors withiout prior knowledge ... >\r') ;
0201 model_alpha_ccl = ccl_learna_alpha (NStr, Xtr, settings);
0202 ppe.alpha_ccl.tr = ccl_error_ppe_alpha (model_alpha_ccl.f_proj, Xtr, Ftr, NStr) ;
0203 ppe.alpha_ccl.te = ccl_error_ppe_alpha (model_alpha_ccl.f_proj, Xte, Fte, NSte) ;
0204 poe.alpha_ccl.tr = ccl_error_poe_alpha (model_alpha_ccl.f_proj, Xtr, NStr) ;
0205 poe.alpha_ccl.te = ccl_error_poe_alpha (model_alpha_ccl.f_proj, Xte, NSte) ;
0206
0207 fprintf(1,'\n Result learn alpha %d \n', i ) ;
0208 fprintf(1,'\t ===============================\n' ) ;
0209 fprintf(1,'\t | NPPE | NPOE \n' ) ;
0210 fprintf(1,'\t -------------------------------\n' ) ;
0211 fprintf(1,'\t Train | %4.2e | %4.2e \n', ppe.alpha_ccl.tr, poe.alpha_ccl.tr ) ;
0212 fprintf(1,'\t Test | %4.2e | %4.2e \n', ppe.alpha_ccl.te, poe.alpha_ccl.te ) ;
0213 fprintf(1,'\t ===============================\n' ) ;
0214
0215
0216 fprintf(1,'\n Learning state-dependent constraint vectors with prior knowledge ... >\r') ;
0217 model_lambda_ccl = ccl_learna_lambda (NStr, Xtr, J, settings);
0218 ppe.lambda_ccl.tr = ccl_error_ppe_alpha (model_lambda_ccl.f_proj, Xtr, Ftr, NStr) ;
0219 ppe.lambda_ccl.te = ccl_error_ppe_alpha (model_lambda_ccl.f_proj, Xte, Fte, NSte) ;
0220 poe.lambda_ccl.tr = ccl_error_poe_alpha (model_lambda_ccl.f_proj, Xtr, NStr) ;
0221 poe.lambda_ccl.te = ccl_error_poe_alpha (model_lambda_ccl.f_proj, Xte, NSte) ;
0222
0223 fprintf(1,'\n Result learn lambda %d \n', i ) ;
0224 fprintf(1,'\t ===============================\n' ) ;
0225 fprintf(1,'\t | NPPE | NPOE \n' ) ;
0226 fprintf(1,'\t -------------------------------\n' ) ;
0227 fprintf(1,'\t Train | %4.2e | %4.2e \n', ppe.lambda_ccl.tr, poe.lambda_ccl.tr ) ;
0228 fprintf(1,'\t Test | %4.2e | %4.2e \n', ppe.lambda_ccl.te, poe.lambda_ccl.te ) ;
0229 fprintf(1,'\t ===============================\n' ) ;
0230
0231
0232 ccl_rob_vis_sbs (settings,model_lambda_ccl.f_proj)
0233
0234 end
0235 fprintf('<=========================================================================>\n');
0236 fprintf('<=========================================================================>\n');
0237 fprintf('<=========================================================================>\n');
0238 pause();
0239
0240
0241
0242
0243 fprintf('\n\n\n<=========================================================================>\n');
0244 fprintf('<=========================================================================>\n');
0245 fprintf('<=========================================================================>\n');
0246 fprintf('< SECTION 4: LEARNING NULL SPACE POLICY >\r');
0247 fprintf('< This section is for learning null space policy models. The methods we >\r');
0248 fprintf('< use are parametric model and locally weighted parametric model >\r');
0249 fprintf('< The cost function to be minimised is: >\r');
0250 fprintf('< E[Pi] = min(sum||U_ns - U_ns^2*Pi||^2) >\r');
0251 fprintf('< To achiev a better unconstrained null space policy, more constraints >\r');
0252 fprintf('< need to be demonstrated in the training dataset >\n');
0253 fprintf('\n< For details please refer to: >\r');
0254 fprintf('< Howard, Matthew, et al. "A novel method for learning policies from >\r');
0255 fprintf('< variable constraint data." Autonomous Robots 27.2 (2009): 105-121. >\n');
0256 fprintf('\n< Start learning null space policy ... > \n');
0257 Dtr = [];
0258 Dte = [];
0259 settings.dim_n = 40 ;
0260 settings.nTraj = 40 ;
0261 model.dim_b = 25;
0262 settings.dim_exp = 1;
0263 settings.learn_pi = 1 ;
0264 if strcmp(settings.projection, 'state_independant')
0265 settings.A = @(q)(orth( rand(2, 1) )' * ccl_rob_jacobian (q, settings.link.length));
0266 elseif strcmp(settings.projection, 'state_dependant')
0267 lambda = @(q)ccl_learna_retlambda(q,settings.link.length,2);
0268 settings.A = @(q)(lambda(q) * ccl_rob_jacobian (q, settings.link.length));
0269 end
0270
0271 for i = 1:settings.dim_exp
0272 fprintf('\n< Generating training dataset for learning null space constraints ... >\n');
0273 Dtr = ccl_data_gen (settings) ;
0274 fprintf(1,'#Data (train): %5d, \r',settings.dim_n*settings.nTraj);
0275 fprintf('\n< Generating testing dataset for learning null space constraints ... >\n');
0276 Dte = ccl_data_gen (settings) ;
0277 fprintf(1,'#Data (test): %5d, \n',settings.dim_n*settings.nTraj);
0278 Xtr = Dtr.X ; Ytr = Dtr.U ; TStr = Dtr.TS ; NStr = Dtr.NS ; Ftr = Dtr.F ; Ptr = Dtr.P;
0279 Xte = Dte.X ; Yte = Dte.U ; TSte = Dtr.TS ; NSte = Dte.NS ; Fte = Dte.F ; Pte = Dte.P;
0280 fprintf(1,'\n Experiment %d \n', i) ;
0281 fprintf(1,'\t Dimensionality of kernel: %d \n', settings.dim_b) ;
0282 fprintf(1,'\t Number of trajectories: %d \n', settings.nTraj) ;
0283 fprintf(1,'\t Number of time steps in each trajectory: %d \n', settings.dim_n) ;
0284
0285
0286 model.c = ccl_math_gc (Xtr, model.dim_b) ;
0287 model.s2 = mean(mean(sqrt(ccl_math_distances(model.c, model.c))))^2 ;
0288 model.phi = @(x)ccl_basis_rbf ( x, model.c, model.s2 );
0289
0290
0291 model = ccl_learnp_pi(Xtr,NStr,model); fp = @(x)ccl_learnp_pred_linear(x,model);
0292
0293
0294 Fptr = fp(Xtr);
0295
0296
0297 NUPEtr = ccl_error_nupe(Ftr,Fptr);
0298 fprintf(1,'NUPE (train) = %5.3e, ',NUPEtr);
0299 NCPEtr = ccl_error_ncpe(NStr,Fptr,Ptr);
0300 fprintf(1,'NCPE (train) = %5.3e, ',NCPEtr);
0301
0302
0303 Fpte = fp(Xte);
0304
0305
0306 NUPEte = ccl_error_nupe(Fte,Fpte);
0307 fprintf(1,'NUPE (test) = %5.3e, ',NUPEte);
0308 NCPEte = ccl_error_ncpe(NSte,Fpte,Pte);
0309 fprintf(1,'NCPE (test) = %5.3e',NCPEte);
0310 fprintf(1,'\n');
0311 end
0312 pause();
0313
0314
0315
0316 fprintf(1,'\n< Start learning Null space policy with locally weighted model ... > \n');
0317 model = [];
0318 Dtr = [];
0319 Dte = [];
0320 model.dim_b = 25;
0321 settings.dim_exp = 1;
0322
0323 for i = 1:settings.dim_exp
0324 fprintf(1,'\n Experiment %d \n', i) ;
0325 fprintf(1,'\t Dimensionality of kernel: %d \n', settings.dim_b) ;
0326 fprintf(1,'\t Number of trajectories: %d \n', settings.nTraj) ;
0327 fprintf(1,'\t Number of time steps in each trajectory: %d \n', settings.dim_n) ;
0328
0329 model.c = ccl_math_gc (Xtr, model.dim_b) ;
0330 model.s2 = mean(mean(sqrt(ccl_math_distances(model.c, model.c))))^2 ;
0331 model.phi = @(x)ccl_basis_rbf( x, model.c, model.s2 );
0332
0333 model.W = @(x)ccl_basis_rbf( x, model.c, model.s2 );
0334 model.phi = @(x)ccl_basis_linear( x );
0335
0336
0337
0338 model = ccl_learnp_lwpi(Xtr,NStr,model); fp = @(x)ccl_learnp_pred_lwlinear(x,model);
0339
0340
0341 Fptr = fp(Xtr);
0342
0343
0344 NUPEtr = ccl_error_nupe(Ftr,Fptr);
0345 fprintf(1,'NUPE (train) = %5.3e, ',NUPEtr);
0346 NCPEtr = ccl_error_ncpe(NStr,Fptr,Ptr);
0347 fprintf(1,'NCPE (train) = %5.3e, ',NCPEtr);
0348
0349
0350 Fpte = fp(Xte);
0351
0352
0353 NUPEte = ccl_error_nupe(Fte,Fpte);
0354 fprintf(1,'NUPE (test) = %5.3e, ',NUPEte);
0355 NCPEte = ccl_error_ncpe(NSte,Fpte,Pte);
0356 fprintf(1,'NCPE (test) = %5.3e',NCPEte);
0357 fprintf(1,'\n');
0358 end
0359 fprintf('<=========================================================================>\n');
0360 fprintf('<=========================================================================>\n');
0361 fprintf('<=========================================================================>\n');
0362 pause();
0363 close all;
0364 end