forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonlinearProgram.m
1660 lines (1536 loc) · 78.1 KB
/
NonlinearProgram.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef NonlinearProgram
% minimize_x objective(x)
% subject to
% cin_lb<=nonlinear_inequality_constraints(x) <= cin_ub
% nonlinear_equality_constraints(x) = 0
% Ain*x <= bin
% Aeq*x = beq
% x_lb <= x <= x_ub
properties (SetAccess=protected)
num_vars % An integer. The number of decision variables
num_cin % An integer. The number of nonlinear inequality constraints
num_ceq % An integer. The number of nonlinear equality constraints
Ain % A double matrix with num_vars columns
bin % A double vector, with same number of rows as Ain
Ain_name % A cell of strings. Ain_name{i} is the name of the i'th inequality linear constraint
Aeq % A double matrix with num_vars columns
beq % A double vector with the same number of rows as Aeq
Aeq_name % A cell of strings. Aeq_name{i} is the name of the i'th equality constraint
cin_lb % A num_cin x 1 double vector. The lower bound of the nonlinear inequality constraint
cin_ub % A num_cin x 1 double vector. The upper bound of the nonlinear inequality constraint
cin_name % A cell of num_cin x 1 strings. cin_name{i} is the name of the i'th nonlinear inequality constraint
ceq_name % A cell of num_ceq x 1 strings. ceq_name{i} is the name of the i'th nonlinear equality constraint
x_lb % A num_vars x 1 double vector. The lower bound of the decision variables
x_ub % A num_vars x 1 double vector. The upper bound of the decision variables
x_name % A cell of num_vars x 1 strings. x_name{i} is the name of the i'th decision variable
solver % The name of the solver. Currently accept snopt, ipopt and fmincon
solver_options
display_funs
display_fun_indices
check_grad % A boolean, True if the user gradient will be checked against
% numerical gradient at the begining and end of the nonlinear optimization
constraint_err_tol % A small scalar. Check whether the constraint are satisfied within the tolerance
nlcon % A cell array of NonlinearConstraint
lcon % A cell array of LinearConstraint
bbcon % A cell array of BoundingBoxConstraint
cost % A cell array of NonlinearConstraint or LinearConstraint.
nlcon_xind % A cell array, nlcon_xind{i} is a cell array of int vectors recording the indices of x that is used in evaluation the i'th NonlinearConstraint
% nlcon{i}.eval(x(nlcon_xind{i}{1},x(nlcon_xind{i}{2},...)
cost_xind_cell % A cell array, cost_xind{i} is a cell array of int vectors recording the indices of x that is used in evaluating obj.cost{i}
bbcon_xind % A cell array, bbcon_xind{i} is an int vector recording the indices of x used in i'th BoundingBoxConstraint
% a cell array like nlcon_xind, where shared_data_xind_cell{i} is a
% cell array of int vectors recording indices used in evaluating the
% shared_data_function
shared_data_xind_cell
% a cell array of function handles, each of which returns a data object
% so that shared_data{i} = shared_data_functions(x(shared_data_xind_cell{i}{1}),x(shared_data_xind_cell{i}{2}),...)
shared_data_functions
% cell arrays of vectors where nlcon_dataind{i} are indices into the
% shared_data used by nonlinear constraints and cost functions
nlcon_dataind
cost_dataind
end
properties (Access=private)
nlcon_xind_stacked % a cell array of vectors, the stacked values of nlcon_xind{i}
cost_xind_stacked % A cell array, cost_xind{i} is an int vector recording the indices of x that is used in evaluating obj.cost{i}
nlcon_ineq_idx % row index of nonlinear inequality constraint in the value [obj.nlcon{i}.eval for i = 1:length(obj.nlcon)]
nlcon_eq_idx % row index of nonlinear equality constraint in the value [obj.nlcon{i}.eval for i = 1:length(obj.nlcon)]
c2nlcon_idx % An integer vector. The j'th row of the the nonlinear constraints [obj.nlcon{i}.eval for i = 1:length(obj.nlcon)] comes from the Constraint object obj.nlcon{obj.c2nlcon_idx(j)}
cin2nlcon_idx % An integer vector. The j'th row of inequality constraint cin is from Constraint object obj.nlcon{obj.cin2nlcon_idx(j)}
ceq2nlcon_idx % An integer vector. The j'th row of equality constraint ceq is from the Constraint object obj.nlcon{obj.ceq2nlcon_idx(j)}
Ain2lcon_idx % An integer vector. The j'th row of the inequality linear constraint Ain*x<=bin is from the Constraint object obj.lincon{obj.Ain_lincon_idx(j)}
Aeq2lcon_idx % An integer vector. The j'th row of the inequality linear constraint Aeq*x=beq is from the Constraint object obj.lincon{obj.Aeq_lincon_idx(j)}
% Each Constraint object in the program is labeled with a unique ID
nlcon_id % nlcon_id(i) is the ID for obj.nlcon{i}
lcon_id % lcon_id(i) is the ID for obj.lcon{i}
bbcon_id % bbcon_id(i) is the ID for obj.bbcon{i}
next_nlcon_id = -1 % The id of the next nonlinear constraint Constraint object to be added
next_lcon_id = -2 % The id of the next linear constraint LinearConstraint object to be added
next_bbcon_id = -3 % The id of the next BoundingBoxConstraint object to be added
bbcon_lb % A obj.num_vars x length(obj.bbcon) matrix. bbcon_lb(:,i) is the lower bound of x coming from the BoundingBoxConstraint obj.bbcon{i}
bbcon_ub % A obj.num_vars x length(obj.bbcon) matrix. bbcon_lb(:,i) is the upper bound of x coming from the BoundingBoxConstraint obj.bbcon{i}
which_snopt % 1 if NonlinearProgramSnoptmex is used.
% 2 if user has their own snopt in MATLAB path
end
properties (Access = protected)
% iGfun,jGvar % sparsity pattern in objective and nonlinear constraints
iFfun,jFvar % sparsity pattern in the objective function
iCinfun,jCinvar % sparsity pattern in the nonlinear inequality constraints
iCeqfun,jCeqvar % sparsity pattern in the nonlinear equality constraints
end
methods
function obj = NonlinearProgram(num_vars,x_name)
% @param num_vars -- Number of decision variables
% @param x_name -- An optional argument. A cell of strings containing the name
% of each decision variable
if(nargin<2)
x_name = cellfun(@(i) sprintf('x%d',i),num2cell((1:num_vars)'),'UniformOutput',false);
else
if(~iscellstr(x_name) || numel(x_name) ~= num_vars)
error('Drake:NonlinearProgra:InvalidArgument','Argument x_name should be a cell containing %d strings',obj.num_vars);
end
x_name = x_name(:);
end
sizecheck(num_vars,[1,1]);
obj.num_vars = num_vars;
obj.num_cin = 0;
obj.num_ceq = 0;
obj.x_name = x_name;
obj.x_lb = -inf(num_vars,1);
obj.x_ub = inf(num_vars,1);
obj.Ain = zeros(0,num_vars);
obj.Aeq = zeros(0,num_vars);
obj.cin_ub = zeros(obj.num_cin,1);
obj.cin_lb = -inf(obj.num_cin,1);
obj.iFfun = ones(obj.num_vars,1);
obj.jFvar = (1:obj.num_vars)';
obj.iCinfun = [];
obj.jCinvar = [];
obj.iCeqfun = [];
obj.jCeqvar = [];
obj.nlcon = {};
obj.lcon = {};
obj.bbcon = {};
obj.nlcon_xind = {};
obj.nlcon_xind_stacked = {};
obj.nlcon_ineq_idx = [];
obj.nlcon_eq_idx = [];
obj.cost = {};
obj.cost_xind_cell = {};
obj.cost_xind_stacked = {};
obj.cin_name = {};
obj.ceq_name = {};
obj.Ain_name = {};
obj.Aeq_name = {};
obj.shared_data_xind_cell = {};
obj.shared_data_functions = {};
obj.nlcon_dataind = {};
obj.cost_dataind = {};
obj.c2nlcon_idx = [];
obj.cin2nlcon_idx = [];
obj.ceq2nlcon_idx = [];
obj.Ain2lcon_idx = [];
obj.Aeq2lcon_idx = [];
obj.nlcon_id = [];
obj.lcon_id = [];
obj.bbcon_id = [];
obj.bbcon_lb = [];
obj.bbcon_ub = [];
obj = obj.setSolver('default');
obj.solver_options.fmincon = optimset('Display','off');
obj.solver_options.snopt = struct();
obj.solver_options.snopt.MajorIterationsLimit = 1000;
obj.solver_options.snopt.MinorIterationsLimit = 500;
obj.solver_options.snopt.IterationsLimit = 10000;
obj.solver_options.snopt.MajorOptimalityTolerance = 1e-6;
obj.solver_options.snopt.MajorFeasibilityTolerance = 1e-6;
obj.solver_options.snopt.MinorFeasibilityTolerance = 1e-6;
obj.solver_options.snopt.SuperbasicsLimit = 300;
obj.solver_options.snopt.VerifyLevel = 0;
obj.solver_options.snopt.DerivativeOption = 1;
obj.solver_options.snopt.print = '';
obj.solver_options.snopt.ScaleOption = 0;
obj.solver_options.snopt.NewBasisFile = 0;
obj.solver_options.snopt.OldBasisFile = 0;
obj.solver_options.snopt.BackupBasisFile = 0;
obj.solver_options.snopt.LinesearchTolerance = 0.9;
obj.solver_options.fmincon.GradConstr = 'on';
obj.solver_options.fmincon.GradObj = 'on';
obj.solver_options.snopt.sense = 'Minimize';
obj.constraint_err_tol = 1e-4;
obj.check_grad = false;
end
function [obj,cnstr_id] = addCompositeConstraints(obj,cnstr,xind,data_ind)
% add a CompositeConstraint to the object, change the constraint evalation of the
% program.
% @param mgr -- A CompositeConstraint object
% @param xind -- Optional argument. The x(xind) is the decision variables used
% in evaluating the cnstr. Default value is (1:obj.num_vars)
% @param data_ind -- Optional argument. shared_data{data_ind} are the data objects used
% @retval cnstr_id -- A vector, cnstr_id(i) is the unique ID of the newly added constraint cnstr{i} in the program
if(~isa(cnstr,'CompositeConstraint'))
error('Drake:NonlinearProgram:UnsupportedConstraint','addCompositeConstraints expects a CompositeConstraint object');
end
if(nargin<3)
xind = {(1:obj.num_vars)'};
end
if ~iscell(xind)
xind = {xind(:)};
end
if size(xind,1) < size(xind,2)
xind = xind';
end
if size(xind,2) ~= 1
error('Drake:NonlinearProgram:InvalidArgument','xind must be a 1-D vector or 1-D cell array');
end
% add in slack variables to end, and adjust xind accordingly
n_slack = cnstr.n_slack;
for i=1:length(xind)
xind{i} = [xind{i};(obj.num_vars + 1 : obj.num_vars + n_slack)'];
end
obj = obj.addDecisionVariable(n_slack);
if nargin < 4
args = {xind};
else
args = {xind,data_ind};
end
% add constraints
cnstr_id = zeros(length(cnstr.constraints),1);
for k=1:length(cnstr.constraints),
[obj,cnstr_id(k)] = obj.addConstraint(cnstr.constraints{k}, args{:});
end
end
function [obj,cnstr_id] = addConstraint(obj,cnstr,varargin)
% obj = addConstraint(obj,cnstr,varargin)
% Queries the constraint type and calls the appropriate addConstraint
% method (e.g. addLinearConstraint, etc)
%
% @param cnstr a Constraint object. if cnstr is a cell array, then
% each of the constraints are added individually.
% @param varargin the remaining arguments are passed directly through
% to the specialized methods. Note that if cnstr is a cell array,
% then the same varargin is passed to all of the specialized methods.
% @retval cnstr_id The unique ID of the newly added constraint in the program.
if iscell(cnstr)
for i=1:numel(cnstr)
[obj,cnstr_id] = addConstraint(obj,cnstr{i},varargin{:});
end
elseif isa(cnstr,'BoundingBoxConstraint')
[obj,cnstr_id] = addBoundingBoxConstraint(obj,cnstr,varargin{:});
elseif isa(cnstr,'LinearConstraint')
[obj,cnstr_id] = addLinearConstraint(obj,cnstr,varargin{:});
elseif isa(cnstr,'CompositeConstraint')
[obj,cnstr_id] = addCompositeConstraints(obj,cnstr,varargin{:});
elseif isa(cnstr,'Constraint')
[obj,cnstr_id] = addNonlinearConstraint(obj,cnstr,varargin{:});
else
error('Drake:NonlinearProgram:UnsupportedConstraint','Unsupported constraint type');
end
end
function [obj,cnstr_id] = addNonlinearConstraint(obj,cnstr,xind, data_ind)
% add a NonlinearConstraint to the object, change the constraint evalation of the
% program.
% @param cnstr -- A NonlinearConstraint object
% @param xind -- Optional argument. The x(xind) is the decision variables used
% in evaluating the cnstr. Default value is (1:obj.num_vars)
% @param data_ind -- Optional argument. shared_data{data_ind} are the data objects used
% @retval cnstr_id -- The ID of stored in obj.nlcon_id, this is the unique ID of the newly
% added constraint in the program.
if(nargin<3)
xind = {(1:obj.num_vars)'};
end
if ~iscell(xind)
xind = {xind(:)};
end
if size(xind,1) < size(xind,2)
xind = xind';
end
if size(xind,2) ~= 1
error('Drake:NonlinearProgram:InvalidArgument','xind must be a 1-D vector or 1-D cell array');
end
xind_vec = cell2mat(xind);
if(nargin<4)
data_ind = [];
end
data_ind = data_ind(:);
if(~isa(cnstr,'Constraint'))
error('Drake:NonlinearProgram:UnsupportedConstraint','addNonlinearConstraint expects a Constraint object');
end
if length(xind_vec) ~= cnstr.xdim
error('Drake:NonlinearProgram:InvalidArgument','the length of xind must match the x-dimension of the constraint');
end
% obj.nlcon = [obj.nlcon,{cnstr}];
obj.nlcon{end+1} = cnstr;
obj.cin_ub = [obj.cin_ub;cnstr.ub(cnstr.cin_idx)];
obj.cin_lb = [obj.cin_lb;cnstr.lb(cnstr.cin_idx)];
obj.nlcon_ineq_idx = [obj.nlcon_ineq_idx;obj.num_cin+obj.num_ceq+cnstr.cin_idx];
obj.nlcon_eq_idx = [obj.nlcon_eq_idx;obj.num_cin+obj.num_ceq+cnstr.ceq_idx];
obj.c2nlcon_idx = [obj.c2nlcon_idx;length(obj.nlcon)*ones(cnstr.num_cnstr,1)];
obj.cin2nlcon_idx = [obj.cin2nlcon_idx;length(obj.nlcon)*ones(length(cnstr.cin_idx),1)];
obj.ceq2nlcon_idx = [obj.ceq2nlcon_idx;length(obj.nlcon)*ones(length(cnstr.ceq_idx),1)];
Geq_idx = cnstr.lb(cnstr.iCfun) == cnstr.ub(cnstr.iCfun);
Gin_idx = ~Geq_idx;
inv_ceq_idx = zeros(cnstr.num_cnstr,1);
inv_ceq_idx(cnstr.ceq_idx) = (1:length(cnstr.ceq_idx))';
inv_cin_idx = zeros(cnstr.num_cnstr,1);
inv_cin_idx(cnstr.cin_idx) = (1:length(cnstr.cin_idx))';
obj.iCinfun = [obj.iCinfun;obj.num_cin+inv_cin_idx(cnstr.iCfun(Gin_idx))];
obj.jCinvar = [obj.jCinvar;xind_vec(cnstr.jCvar(Gin_idx))];
obj.iCeqfun = [obj.iCeqfun;obj.num_ceq+inv_ceq_idx(cnstr.iCfun(Geq_idx))];
obj.jCeqvar = [obj.jCeqvar;xind_vec(cnstr.jCvar(Geq_idx))];
obj.cin_name = [obj.cin_name;cnstr.name(cnstr.cin_idx)];
obj.ceq_name = [obj.ceq_name;cnstr.name(cnstr.ceq_idx)];
obj.num_cin = obj.num_cin + length(cnstr.cin_idx);
obj.num_ceq = obj.num_ceq + length(cnstr.ceq_idx);
obj.nlcon_xind{end+1} = xind;
obj.nlcon_xind_stacked{end+1} = xind_vec;
if(length(unique(xind_vec)) ~= length(xind_vec))
error('Drake:NonlinearProgram:addNonlinearConstraint: The input xind argument has duplicate entries');
end
obj.nlcon_dataind{end+1} = data_ind;
cnstr_id = obj.next_nlcon_id;
obj.next_nlcon_id = obj.next_nlcon_id-3;
obj.nlcon_id = [obj.nlcon_id cnstr_id];
if(strcmpi(obj.solver,'studentsnopt'))
if(~(obj.num_cin+obj.num_ceq+size(obj.Ain,1)+size(obj.Aeq,1)<=300))
warning('Number of constraints exceeded studentSNOPT support: obj.num_cin+obj.num_ceq+size(obj.Ain,1)+size(obj.Aeq,1)>300. Switching to default solver.');
obj = obj.setSolver('default');
end
end
end
function [obj,cnstr_id] = addLinearConstraint(obj,cnstr,xind)
% add a LinearConstraint to the program
% @param cnstr -- A LinearConstraint object
% @param xind -- Optional argument. x(xind) is the decision variables used in
% evaluating the constraint. Default value is (1:obj.num_vars)
% @param cnstr_name -- An optional argument. A cell of strings. cnstr_name{i} is
% the name of the i'th constraint. If not given, the cnstr.name will be used instead
% @retval cnstr_id -- The ID stored in obj.lcon_id. This is the unique ID of the newly added
% constraint in the program.
% if cnstr.num_cnstr > 0
if(nargin<3)
xind = (1:obj.num_vars)';
end
if iscell(xind)
xind = cell2mat(xind);
end
xind = xind(:);
if(~isa(cnstr,'LinearConstraint'))
error('Drake:NonlinearProgram:UnsupportedConstraint','addLinearConstraint expects a LinearConstraint object');
end
if length(xind) ~= cnstr.xdim
error('Drake:NonlinearProgram:InvalidArgument','the length of xind must match the x-dimension of the constraint');
end
obj.lcon = [obj.lcon,{cnstr}];
cnstr_A = sparse(cnstr.iCfun,xind(cnstr.jCvar),cnstr.A_val,cnstr.num_cnstr,obj.num_vars,cnstr.nnz);
cnstr_beq = (cnstr.lb(cnstr.ceq_idx)+cnstr.ub(cnstr.ceq_idx))/2;
cnstr_Aeq = cnstr_A(cnstr.ceq_idx,:);
cnstr_Ain = cnstr_A(cnstr.cin_idx,:);
cnstr_ineq_name = cnstr.name(cnstr.cin_idx);
cnstr_bin_lb = cnstr.lb(cnstr.cin_idx);
cnstr_bin_ub = cnstr.ub(cnstr.cin_idx);
bin_ub_not_inf_idx = ~isinf(cnstr_bin_ub);
bin_lb_not_inf_idx = ~isinf(cnstr_bin_lb);
if(sum(bin_ub_not_inf_idx | bin_lb_not_inf_idx)>0)
obj.Ain = vertcat(obj.Ain,[cnstr_Ain(bin_ub_not_inf_idx,:);-cnstr_Ain(bin_lb_not_inf_idx,:)]);
obj.bin = vertcat(obj.bin,[cnstr_bin_ub(bin_ub_not_inf_idx);-cnstr_bin_lb(bin_lb_not_inf_idx)]);
obj.Ain_name = [obj.Ain_name;cnstr_ineq_name(bin_ub_not_inf_idx);cnstr_ineq_name(bin_lb_not_inf_idx)];
obj.Ain2lcon_idx = [obj.Ain2lcon_idx;length(obj.lcon)*ones(sum(bin_ub_not_inf_idx)+sum(bin_lb_not_inf_idx),1)];
end
obj.Aeq_name = [obj.Aeq_name;cnstr.name(cnstr.ceq_idx)];
if(numel(cnstr_Aeq)>0)
obj.Aeq = vertcat(obj.Aeq,cnstr_Aeq);
obj.beq = vertcat(obj.beq,cnstr_beq);
obj.Aeq2lcon_idx = [obj.Aeq2lcon_idx;length(obj.lcon)*ones(size(cnstr_Aeq,1),1)];
end
cnstr_id = obj.next_lcon_id;
obj.next_lcon_id = obj.next_lcon_id-3;
obj.lcon_id = [obj.lcon_id cnstr_id];
% end
if(strcmpi(obj.solver,'studentsnopt'))
if(~(obj.num_cin+obj.num_ceq+size(obj.Ain,1)+size(obj.Aeq,1)<=300))
warning('Number of constraints exceeds studentSNOPT support: obj.num_cin+obj.num_ceq+size(obj.Ain,1)+size(obj.Aeq,1)>300. Switching to default solver.');
obj = obj.setSolver('default');
end
end
end
function [obj,cnstr_id] = addBoundingBoxConstraint(obj,cnstr,xind)
% add a BoundingBoxConstraint to the program
% @param cnstr -- A BoundingBoxConstraint
% @param xind -- Optional argument. x(xind) is the decision variables to be
% set bounds
% @retval cnstr_id -- The ID stored in obj.bbcon_id. This is the unique ID of the newly added
% constraint in the program.
if(nargin < 3)
xind = (1:obj.num_vars)';
end
if iscell(xind)
xind = cell2mat(xind);
end
xind = xind(:);
if(~isa(cnstr,'BoundingBoxConstraint'))
error('Drake:NonlinearProgram:UnsupportedConstraint','addBoundingBoxConstraint expects a BoundingBoxConstraint object');
end
if length(xind) ~= cnstr.xdim
error('Drake:NonlinearProgram:InvalidArgument','the length of xind must match the x-dimension of the constraint');
end
obj.bbcon = [obj.bbcon,{cnstr}];
if(length(unique(xind)) ~= length(xind))
error('Drake:NonlinearProgram:addBoundingBoxConstraint: The input xind has duplicate entries, check the xind argument');
end
obj.x_lb(xind) = max([cnstr.lb obj.x_lb(xind)],[],2);
obj.x_ub(xind) = min([cnstr.ub obj.x_ub(xind)],[],2);
if (any(obj.x_lb(xind)>obj.x_ub(xind)))
error('Drake:NonlinearProgram:InvalidConstraint','adding this bounding box constraint resulted in some lb>ub');
end
obj.bbcon_xind{end+1} = xind;
cnstr_id = obj.next_bbcon_id;
obj.next_bbcon_id = obj.next_bbcon_id-3;
obj.bbcon_id = [obj.bbcon_id cnstr_id];
obj.bbcon_lb(:,end+1) = -inf(obj.num_vars,1);
obj.bbcon_lb(xind,end) = cnstr.lb;
obj.bbcon_ub(:,end+1) = inf(obj.num_vars,1);
obj.bbcon_ub(xind,end) = cnstr.ub;
end
function obj = addCost(obj,cnstr,xind,data_ind)
% Add a cost to the objective function
% @param cnstr -- A NonlinearConstraint or a LinearConstraint
% @param xind -- Optional argument. x(xind) is the decision variables used in
% evaluating the cost. Default value is (1:obj.num_vars)
% @param data_ind -- Optional argument. shared_data{data_ind} are the data objects used
if(nargin<3)
xind = {(1:obj.num_vars)'};
end
if ~iscell(xind)
xind = {xind(:)};
end
xind_vec = cell2mat(xind);
if(nargin<4)
data_ind = [];
end
data_ind = data_ind(:);
if ~isa(cnstr,'Constraint')
error('Drake:NonlinearProgram:UnsupportedConstraint','addCost expects a Constraint object');
end
if(cnstr.num_cnstr ~= 1)
error('Drake:NonlinearProgram:WrongCost','addCost only accept scalar function');
end
obj.cost = [obj.cost,{cnstr}];
obj.cost_xind_cell{end+1} = xind;
obj.cost_xind_stacked{end+1} = xind_vec;
if(length(unique(xind_vec)) ~= length(xind_vec))
error('Drake:NonlinearProgram:addCost: The xind argument has duplicate entries, check xind');
end
obj.cost_dataind{end+1} = data_ind;
% obj.cost_xind_cell = [obj.cost_xind_cell,{xind(cnstr.jCvar)}];
obj.jFvar = unique([obj.jFvar;xind_vec(cnstr.jCvar)]);
obj.iFfun = ones(length(obj.jFvar),1);
end
function obj = addQuadraticCost(obj,Q,x_desired,xind)
% helper function for the very common case of adding the objective
% g(x) = (x-xd)'*Q*(x-xd), Q = Q' >= 0
% @param Q a symmetric PSD cost matrix
% @param x_desired column vector of desired values
% @param xind optional subset of x to apply cost to
if nargin<3, xind = 1:obj.num_vars; end
obj = obj.addCost(QuadraticSumConstraint(0,inf,Q,x_desired),xind);
end
function args = getArgumentArray(obj,x,xind)
% Retrieves the elements from the vector x related to xind and returns
% them as a cell array where:
% args{i} = x(xind{i})
narg = length(xind);
args = cell(narg,1);
for j=1:narg,
args{j} = x(xind{j});
end
end
function [g,h,dg,dh] = nonlinearConstraints(obj,x)
% evaluate the nonlinear constraints
% @param x A num_vars x 1 double vector. The decision variables
% @retval g The value of the nonlinear inequality constraints
% @retval h The value of the nonlinear equality constraints
% @retval dg The gradient of g w.r.t x
% @retval dh The gradient of h w.r.t x
shared_data = obj.evaluateSharedDataFunctions(x);
f = zeros(obj.num_cin+obj.num_ceq,1);
G = zeros(obj.num_cin+obj.num_ceq,obj.num_vars);
f_count = 0;
for i = 1:length(obj.nlcon)
args = [getArgumentArray(obj,x,obj.nlcon_xind{i});shared_data(obj.nlcon_dataind{i})];
if(nargout>2)
[f(f_count+(1:obj.nlcon{i}.num_cnstr)),G(f_count+(1:obj.nlcon{i}.num_cnstr),obj.nlcon_xind_stacked{i})] = ...
obj.nlcon{i}.eval(args{:});
else
f(f_count+(1:obj.nlcon{i}.num_cnstr)) = obj.nlcon{i}.eval(args{:});
end
f(f_count+obj.nlcon{i}.ceq_idx) = f(f_count+obj.nlcon{i}.ceq_idx)-obj.nlcon{i}.ub(obj.nlcon{i}.ceq_idx);
f_count = f_count+obj.nlcon{i}.num_cnstr;
end
g = f(obj.nlcon_ineq_idx);
h = f(obj.nlcon_eq_idx);
if(nargout>2)
dg = G(obj.nlcon_ineq_idx,:);
dh = G(obj.nlcon_eq_idx,:);
end
end
function [f,df] = objective(obj,x)
% return the value of the objective
% @param x A obj.num_vars x 1 double vector. The decision variables
% @retval f A double scalar. The value of the objective function
% @retval df The gradient of f w.r.t x
shared_data = obj.evaluateSharedDataFunctions(x);
for i=1:length(obj.display_funs)
obj.display_funs{i}(x(obj.display_fun_indices{i}));
end
f = 0;
df = zeros(1,obj.num_vars);
for i = 1:length(obj.cost)
args = [getArgumentArray(obj,x,obj.cost_xind_cell{i});shared_data(obj.cost_dataind{i})];
if(nargout>1)
[fi,dfi] = obj.cost{i}.eval(args{:});
else
fi = obj.cost{i}.eval(args{:});
end
f = f+fi;
if(nargout>1)
df(obj.cost_xind_stacked{i}) = df(obj.cost_xind_stacked{i})+dfi;
end
end
end
function [f,G] = objectiveAndNonlinearConstraints(obj,x)
% evaluate the objective and the nonlinear constraints altogher
% @param x A obj.num_vars x 1 double vector. The decision variables
% @retval f A 1+obj.num_cin+obj.num_ceq x 1 double vector. f =
% [objective;nonlinear_inequality_constraints;nonlinear_equality_constraints]
% @retval df The gradient of f w.r.t x
shared_data = obj.evaluateSharedDataFunctions(x);
for i=1:length(obj.display_funs)
obj.display_funs{i}(x(obj.display_fun_indices{i}));
end
f = zeros(1+obj.num_cin+obj.num_ceq,1);
G = zeros(1+obj.num_cin+obj.num_ceq,obj.num_vars);
for i = 1:length(obj.cost)
args = [getArgumentArray(obj,x,obj.cost_xind_cell{i});shared_data(obj.cost_dataind{i})];
if(nargout>1)
[fi,dfi] = obj.cost{i}.eval(args{:});
else
fi = obj.cost{i}.eval(args{:});
end
f(1) = f(1)+fi;
if(nargout>1)
G(1,obj.cost_xind_stacked{i}) = G(1,obj.cost_xind_stacked{i})+dfi;
end
end
f_count = 1;
for i = 1:length(obj.nlcon)
args = [getArgumentArray(obj,x,obj.nlcon_xind{i});shared_data(obj.nlcon_dataind{i})];
if(nargout>1)
[f(f_count+(1:obj.nlcon{i}.num_cnstr)),G(f_count+(1:obj.nlcon{i}.num_cnstr),obj.nlcon_xind_stacked{i})] = ...
obj.nlcon{i}.eval(args{:});
else
f(f_count+(1:obj.nlcon{i}.num_cnstr)) = obj.nlcon{i}.eval(args{:});
end
f(f_count+obj.nlcon{i}.ceq_idx) = f(f_count+obj.nlcon{i}.ceq_idx)-obj.nlcon{i}.ub(obj.nlcon{i}.ceq_idx);
f_count = f_count+obj.nlcon{i}.num_cnstr;
end
f = [f(1);f(1+obj.nlcon_ineq_idx);f(1+obj.nlcon_eq_idx)];
if(nargout>1)
G = [G(1,:);G(1+obj.nlcon_ineq_idx,:);G(1+obj.nlcon_eq_idx,:)];
end
end
function obj = addDecisionVariable(obj,num_new_vars,var_name)
% appending new decision variables to the end of the current decision variables
% @param num_new_vars -- An integer. The newly added decision variable is an
% num_new_vars x 1 double vector.
% @param var_name -- An optional argument. A cell of strings containing the
% name of the new decision variables
if(nargin<3)
var_name = cellfun(@(i) sprintf('x%d',i),num2cell(obj.num_vars+(1:num_new_vars)'),'UniformOutput',false);
else
if(~iscellstr(var_name) || numel(var_name) ~= num_new_vars)
error('Drake:NonlinearProgram:addDecisionVariable:InvalidArgument','Argument var_name should be a cell of %d strings',num_new_vars);
end
var_name = var_name(:);
end
obj.num_vars = obj.num_vars+num_new_vars;
obj.x_name = [obj.x_name;var_name];
obj.x_lb = [obj.x_lb;-inf(num_new_vars,1)];
obj.x_ub = [obj.x_ub;inf(num_new_vars,1)];
obj.Aeq = [obj.Aeq zeros(length(obj.beq),num_new_vars)];
obj.Ain = [obj.Ain zeros(length(obj.bin),num_new_vars)];
if(~isempty(obj.bbcon))
obj.bbcon_lb(end+(1:num_new_vars),:) = -inf(num_new_vars,size(obj.bbcon_lb,2));
obj.bbcon_ub(end+(1:num_new_vars),:) = inf(num_new_vars,size(obj.bbcon_ub,2));
end
if(strcmpi(obj.solver,'studentsnopt'))
if(~(obj.num_vars<=300))
warning('Number of variables exceeds studentSNOPT support: obj.num_vars>300. Switching to default solver.');
obj = obj.setSolver('default');
end
end
end
function obj = replaceCost(obj,cost,cost_idx,xind)
% replace the cost_idx'th cost in the original problem with a new cost
% @param cost -- A Constraint object, currently accepts NonlinearConstraint and
% LinearConstraint
% @param cost_idx -- The index of the original cost to be replaced
% @param xind -- Optional argument. x(xind) is the decision variables used in
% evaluating the cost. Default value is (1:obj.num_vars)
if(nargin<4)
xind = {(1:obj.num_vars)'};
end
if ~iscell(xind)
xind = {xind(:)};
end
obj.iFfun = [];
obj.jFvar = [];
num_cost = length(obj.cost);
sizecheck(cost_idx,[1,1]);
if(cost_idx>num_cost || cost_idx<1)
error('Drake:NonlinearProgram:replaceCost:cost_idx is out of range');
end
cost_tmp = obj.cost;
cost_tmp{cost_idx} = cost;
cost_xind_tmp = obj.cost_xind_cell;
cost_xind_tmp{cost_idx} = xind;
obj.cost = {};
obj.cost_xind_cell = {};
obj.cost_xind_stacked = {};
for i = 1:num_cost
obj = obj.addCost(cost_tmp{i},cost_xind_tmp{i});
end
end
function [obj,ind] = addSharedDataFunction(obj,user_fun,xind)
% Adds the specified shared data function to be evaluated within each iteration of the program
% @param user_fun -- The function to be evaluated, where
% shared_data{ind} = user_fun(x(xind));
% @param xind -- Optional argument. The x(xind) is the decision variables used
% in evaluating the cnstr. Default value is (1:obj.num_vars)
% @return ind -- the shared data index
if(nargin<3)
xind = {(1:obj.num_vars)'};
end
if ~iscell(xind)
xind = {xind(:)};
end
if isa(user_fun,'FunctionWrapper')
obj.shared_data_functions{end+1} = user_fun;
else
obj.shared_data_functions{end+1} = FunctionWrapper(user_fun);
end
obj.shared_data_xind_cell{end+1} = xind;
ind = obj.getNumSharedDataFunctions();
end
function n = getNumSharedDataFunctions(obj)
n = length(obj.shared_data_functions);
end
function data = evaluateSharedDataFunctions(obj,x)
% Evaluate all shared data functions and return the data object
nData = length(obj.shared_data_functions);
data = cell(nData,1);
for i=1:nData
args = getArgumentArray(obj,x,obj.shared_data_xind_cell{i});
% data{i} = obj.shared_data_functions{i}.eval(args{:});
data{i} = obj.shared_data_functions{i}.eval(args{:});
end
end
function obj = addDisplayFunction(obj,display_fun,indices)
% add a dispay function that gets called on every iteration of the
% algorithm
% @param display_fun a function handle of the form displayFun(x(indices))
% @param indices optionally specify a subset of the decision
% variables to be passed to the displayFun @default 1:obj.num_vars
typecheck(display_fun,'function_handle');
if nargin<3, indices = 1:obj.num_vars; end
obj.display_funs = vertcat(obj.display_funs,{display_fun});
obj.display_fun_indices = vertcat(obj.display_fun_indices,{indices});
end
function obj = setCheckGrad(obj,check_grad)
sizecheck(check_grad,[1,1]);
obj.check_grad = logical(check_grad);
end
function obj = setConstraintErrTol(obj,tol)
if(~isnumeric(tol) || numel(tol) ~= 1)
error('Drake:NonlinearProgram:setConstraintErrTol:tol should be scalar');
end
if(tol<=0)
error('Drake:NonlinearProgram:setConstraintErrTol:tol should be positive');
end
obj.constraint_err_tol = tol;
end
function obj = setSolver(obj,solver)
% @param solver Can be 'snopt', 'ipopt', 'fmincon' and 'default'.
typecheck(solver,'char');
if(strcmp(solver,'snopt'))
if(checkDependency('NonlinearProgramSnoptmex'))
obj.which_snopt = 1;
elseif(checkDependency('snopt'))
obj.which_snopt = 2;
else
error('Drake:NonlinearProgram:UnsupportedSolver',' SNOPT not found. SNOPT support will be disabled.');
end
obj.solver = 'snopt';
elseif(strcmp(solver,'studentSnopt'))
if(~checkDependency('studentSnopt'))
error('Drake:NonlinearProgram:UnsupportedSolver',' studentSNOPT not found. studentSNOPT support will be disabled.');
end
obj.solver = 'snopt';
obj.which_snopt = 2;
elseif(strcmp(solver,'fmincon'))
if(~checkDependency('fmincon'))
error('Drake:NonlinearProgram:UnsupportedSolver',' fmincon support is disabled. To enable it, install MATLAB Optimization toolbox');
end
obj.solver = solver;
elseif(strcmp(solver,'ipopt'))
if(~checkDependency('ipopt'))
error('Drake:NonlinearProgram:UnsupportedSolver',' IPOPT not found. IPOPT support will be disabled.');
end
obj.solver = solver;
elseif(strcmp(solver,'default'))
if(checkDependency('snopt') || checkDependency('NonlinearProgramSnoptmex'))
obj = obj.setSolver('snopt');
elseif(checkDependency('studentSnopt')&&obj.num_vars<=300 && obj.num_cin+obj.num_ceq+size(obj.Ain,1)+size(obj.Aeq,1)<=300)
obj = obj.setSolver('studentSnopt');
elseif(checkDependency('fmincon'))
obj = obj.setSolver('fmincon');
elseif(checkDependency('ipopt'))
obj = obj.setSolver('ipopt');
end
end
end
function obj = setSolverOptions(obj,solver,optionname,optionval)
% @param solver - string name of the solver
% @param optionname -- string name of the option field
% @param optionval -- option value
if(strcmpi(solver,'snopt'))
if(strcmpi(optionname(~isspace(optionname)),'majorfeasibilitytolerance'))
sizecheck(optionval,[1,1]);
if(optionval<=0)
error('Drake:NonlinearProgram:setSolverOptions:MajorFeasibilityTolerance should be positive');
end
obj.solver_options.snopt.MajorFeasibilityTolerance = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'minorfeasibilitytolerance'))
sizecheck(optionval,[1,1]);
if(optionval<=0)
error('Drake:NonlinearProgram:setSolverOptions:MinorFeasibilityTolerance should be positive');
end
obj.solver_options.snopt.MinorFeasibilityTolerance = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'majoroptimalitytolerance'))
sizecheck(optionval,[1,1]);
if(optionval<=0)
error('Drake:NonlinearProgram:setSolverOptions:MajorOptimalityTolerance should be positive');
end
obj.solver_options.snopt.MajorOptimalityTolerance = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'majoriterationslimit'))
sizecheck(optionval,[1,1]);
if(optionval<1)
error('Drake:NonlinearProgram:setSolverOptions:MajorIterationsLimit should be positive integers');
end
obj.solver_options.snopt.MajorIterationsLimit = floor(optionval);
elseif(strcmpi(optionname(~isspace(optionname)),'minoriterationslimit'))
sizecheck(optionval,[1,1]);
if(optionval<1)
error('Drake:NonlinearProgram:setSolverOptions:MinorIterationsLimit should be positive integers');
end
obj.solver_options.snopt.MinorIterationsLimit = floor(optionval);
elseif(strcmpi(optionname(~isspace(optionname)),'iterationslimit'))
sizecheck(optionval,[1,1]);
if(optionval<1)
error('Drake:NonlinearProgram:setSolverOptions:IterationsLimit should be positive integers');
end
obj.solver_options.snopt.IterationsLimit = floor(optionval);
elseif(strcmpi(optionname(~isspace(optionname)),'superbasicslimit'))
sizecheck(optionval,[1,1]);
if(optionval<1)
error('Drake:NonlinearProgram:setSolverOptions:SuperbasicsLimit should be positive integers');
end
obj.solver_options.snopt.SuperbasicsLimit = floor(optionval);
elseif(strcmpi(optionname(~isspace(optionname)),'derivativeoption'))
sizecheck(optionval,[1,1]);
if(optionval ~= 0 && optionval ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:DerivativeOption can be either 0 or 1');
end
obj.solver_options.snopt.DerivativeOption = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'verifylevel'))
sizecheck(optionval,[1,1]);
if(optionval ~= 0 && optionval ~= 1 && optionval ~= 2 && optionval ~= 3 && optionval ~= -1)
error('Drake:NonlinearProgram:setSolverOptions:VerifyLevel can be either 0,1,2,3 or -1');
end
obj.solver_options.snopt.VerifyLevel = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'print'))
if(~ischar(optionval))
error('Drake:NonlinearProgram:setSolverOptions:print should be the file name string');
end
obj.solver_options.snopt.print = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'scaleoption'))
if(~isnumeric(optionval) || numel(optionval) ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:ScaleOption should be a scalar');
end
if(optionval ~= 0 && optionval ~= 1 && optionval ~= 2)
error('Drake:NonlinearProgram:setSolverOptions:ScaleOption should be either 0,1 or 2');
end
obj.solver_options.snopt.ScaleOption = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'oldbasisfile'))
if(~isnumeric(optionval) || numel(optionval) ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:OptionVal', 'OldBasisFile should be a scalar');
end
obj.solver_options.snopt.OldBasisFile = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'newbasisfile'))
if(~isnumeric(optionval) || numel(optionval) ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:OptionVal', 'NewBasisFile should be a scalar');
end
obj.solver_options.snopt.NewBasisFile = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'backupbasisfile'))
if(~isnumeric(optionval) || numel(optionval) ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:OptionVal', 'BackupBasisFile should be a scalar');
end
obj.solver_options.snopt.BackupBasisFile = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'linesearchtolerance'))
if(~isnumeric(optionval) || numel(optionval) ~= 1)
error('Drake:NonlinearProgram:setSolverOptions:LineSearchTolerance should be a scalar');
end
if(optionval < 0 || optionval > 1)
error('Drake:NonlinearProgram:setSolverOptions:OptionVal', 'LinesearchTolerance should be between 0 and 1');
end
obj.solver_options.snopt.LinesearchTolerance = optionval;
elseif(strcmpi(optionname(~isspace(optionname)),'sense'))
if(~ischar(optionval))
error('Drake:NonlinearProgram:setSolverOptions:OptionVal', 'sense should be a string');
end
if(~any(strcmp(optionval,{'Minimize','Maximize','Feasible point'})))
error('Drake:NonlinearProgram:setSolverOptions:Sense', ...
'sense must be one of the following: ''Minimize'', ''Maximize'', ''Feasible point''');
end
obj.solver_options.snopt.sense = optionval;
end
elseif(strcmpi((solver),'fmincon'))
obj.solver_options.fmincon = optimset(obj.solver_options.fmincon, optionname, optionval);
else
error('solver %s not supported yet',solver);
end
end
function [iGfun,jGvar] = getNonlinearGradientSparsity(obj)
% This function sets the nonlinear sparsity vector iGfun and jGvar based on the
% nonlinear sparsity of the objective, nonlinear inequality constraints and
% nonlinear equality constraints
% @param iGfun,jGvar. G(iGfun,jGvar) are the non-zero entries in the matrix G, which
% is the gradient of return value f in the objectiveAndNonlinearConstraints function
iGfun = [obj.iFfun;obj.iCinfun+1;obj.iCeqfun+1+obj.num_cin];
jGvar = [obj.jFvar;obj.jCinvar;obj.jCeqvar];
end
function [lb,ub] = bounds(obj)
% return the bounds for all the objective function, nonlinear constraints and linear
% constraints
lb = [-inf;obj.cin_lb;zeros(obj.num_ceq,1);-inf(length(obj.bin),1);obj.beq];
ub = [inf;obj.cin_ub;zeros(obj.num_ceq,1);obj.bin;obj.beq];
end
function [x,objval,exitflag,infeasible_constraint_name] = solve(obj,x0)
% @param x0 A obj.num_vars x 1 double vector. The initial seed
% @retval x A obj.num_vars x 1 double vector. The solution obtained after running the
% solver
% @retval objval A double scalar. The value of the objective function after running the
% solver
% @retval exitflag An integer scalar.
% 1 -- Solved successful
% *********************
% If the solver is SNOPT, then exitflag is the same as the INFO returned by
% the solver. Please refer to
% http://www.cam.ucsd.edu/~peg/papers/sndoc7.pdf for more information
% 2 -- Solved with SNOPT, but the accuracy of the linear constraints
% cannot be achieved.
% 3 -- Solved with SNOPT, but the accuracy of the nonlinear constraints
% cannot be achieved.
% 4 -- SNOPT thinks it fails to solve the problem, but the solution
% satisfies the constraints within obj.constraint_err_tol
% 5 -- SNOPT thinks it runs out of iterations limits, but the solution
% satisfies the constraints within obj.constraint_err_tol, try increase the
% iterations limits
% 6 -- SNOPT thinks it runs out of major iterations limits, but the
% solution satisfies the constraints within obj.constraint_err_tol. try
% increase the major iterations limits
% 11 -- SNOPT fails as the linear constraints are infeasible.
% This is most likely because the decision variables in
% some constraints (nonlinear or linear constraints) are fixed (due to the equality bounding
% box constraint on the decision variable). Consider either
% to remove the constraints, or relax the bounding box
% constraint on the decision variable.
% 12 -- SNOPT fails as the linear equality constraints are infeasible
% 13 -- SNOPT fails as the nonlinear constraints are infeasible
% 31 -- SNOPT fails by running out of iterations limit
% 32 -- SNOPT fails by running out of major iterations limit
% 33 -- SNOPT fails due to small super basics limit
% 41 -- SNOPT fails due to numerical problems
% *********************
% If the solver is fmincon, then the exitflag is 200 + fmincon_exitflag
% 200 -- In fmincon, number of iterations exceeds options.MaxIter
% 199 -- In fmincon, stopped by an output function or plot function
% 198 -- In fmincon, no feasible point was found.
% 202 -- In fmincon, change in x was less than options.TolX and maximum constraint violation was less than options.TolCon.
% 203 -- In fmincon, change in the objective function value was less than options.TolFun and maximum constraint violation was less than options.TolCon.
% 204 -- In fmincon, magnitude of the search direction was less than 2*options.TolX and maximum constraint violation was less than options.TolCon.
% 205 -- In fmincon, magnitude of directional derivative in search
% direction was less than 2*options.TolFun and maximum constraint violation was less than options.TolCon.
% 197 -- In fmincon, objective function at current iteration went below options.ObjectiveLimit and maximum constraint violation was less than options.TolCon.
% **********************
% If the solver is IPOPT, then the exitflag = -100 + ipopt_exitflag
% -99 -- In ipopt, solved to acceptable level
% -98 -- In ipopt, infeasible problem detected
% -97 -- In ipopt, search direction becomes too small
% -96 -- In ipopt, diverging iterates
% -95 -- In ipopt, user requested stop
% -101 -- In ipopt, maximum number of iterations exceeded
% -102 -- In ipopt, restoration phase failed
% -103 -- In ipopt, error in step computation
% -110 -- In ipopt, not enough degrees of freedom
% -111 -- In ipopt, invalid problem definition
% -112 -- In ipopt, invalid option
% -113 -- In ipopt, invalid number detected
% -200 -- In ipopt, unrecoverable exception
% -201 -- In ipopt, non-IPOPT exception thrown
% -202 -- In ipopt, insufficient memory
% -299 -- In ipopt, internal error
%
% When using fmincon, if the algorithm is not specified through
% setSolverOptions('fmincon','Algorithm',ALGORITHM), then it will
% iterate all possible algorithms in fmincon to search for a solution.
if(obj.num_vars == 0)
x = [];
objval = 0;
exitflag = 1;
infeasible_constraint_name = {};
else
switch lower(obj.solver)
case 'snopt'
[x,objval,exitflag,infeasible_constraint_name] = snopt(obj,x0);
case 'fmincon'
[x,objval,exitflag,infeasible_constraint_name] = fmincon(obj,x0);
case 'ipopt'
[x,objval,exitflag,infeasible_constraint_name] = ipopt(obj,x0);
otherwise
error('Drake:NonlinearProgram:UnknownSolver',['The requested solver, ',obj.solver,' is not known, or not currently supported']);
end
end
end
function [x,objval,exitflag,execution_time,solvers] = compareSolvers(obj,x0,solvers)
if nargin<3
solvers = {};
if(checkDependency('fmincon'))
solvers = [solvers,{'fmincon'}];
end
if(checkDependency('snopt'))
solvers = [solvers,{'snopt'}];
end
if(checkDependency('studentSnopt'))
solvers = [solvers,{'studentSnopt'}];
end
if(checkDependency('ipopt'))
solvers = [solvers,{'ipopt'}];
end
if(isempty(solvers))
error('Drake:NonlinearProgram:NoNLPSolver','Cannot find any nonlinear program solvers, please ensure that either fmincon, snopt or ipopt is installed');