-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathpsopt.h
executable file
·1550 lines (1142 loc) · 49.8 KB
/
psopt.h
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
/*********************************************************************************************
This file is part of the PSOPT library, a software tool for computational optimal control
Copyright (C) 2009-2020 Victor M. Becerra
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA,
or visit http://www.gnu.org/licenses/
Author: Professor Victor M. Becerra
Address: University of Portsmouth
School of Energy and Electronic Engineering
Portsmouth PO1 3DJ
United Kingdom
e-mail: v.m.becerra@ieee.org
**********************************************************************************************/
#ifndef PSOPT_H
#define PSOPT_H
#include <Eigen/Dense>
#include <limits>
using std::numeric_limits;
namespace PSOPT {
constexpr double inf = std::numeric_limits<double>::infinity();
constexpr double pi = 3.141592653589793;
}
// using namespace Eigen;
using Eigen::MatrixXd;
using Eigen::RowVectorXi;
// typedef Eigen::SparseMatrix<double> SpMat; // declares a column-major sparse matrix type of double
#define DMatrix MatrixXd // In case a change of type is forgotten
#ifdef WIN32
extern "C" {
_CRTIMP int * __cdecl errno(void) { static int i=0; return &i; };
}
#endif
#define CINDEX( i ) ((i)-1)
#define FREE_ARG char*
#ifndef WIN32
#include <stdlib.h>
#endif
#include <math.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <memory>
using namespace std;
#include <adolc/drivers/drivers.h>
#include <adolc/interfaces.h>
#include <adolc/adalloc.h>
#include <adolc/adouble.h>
#include <adolc/sparse/sparsedrivers.h>
#include <adolc/taping.h>
#include <string>
using std::string;
class TripletSparseMatrix;
class AutoDiffMatrix {
adouble* a;
int n;
int m;
public:
// Constructor
AutoDiffMatrix( int n, int m);
AutoDiffMatrix( int n );
AutoDiffMatrix();
// Destructor
~AutoDiffMatrix();
adouble elem(int i, int j) const;
adouble operator()(int i, int j) const;
adouble operator()(int i) const;
adouble& elem(int i, int j);
adouble& operator()(int i, int j);
adouble& operator()(int i);
int rows() const { return n;}
int cols() const { return m;}
adouble* GetPr() { return a;}
adouble* GetConstPr() const { return a;}
void Print(const char* text) const;
void resize(int nn, int mm);
void setZero();
void setOne();
};
class dual_str {
public:
MatrixXd* Hamiltonian;
MatrixXd* costates;
MatrixXd* path;
MatrixXd* events;
MatrixXd* linkages;
dual_str()
{
Hamiltonian = NULL;
costates = NULL;
path = NULL;
events = NULL;
linkages = NULL;
}
~dual_str()
{
if (Hamiltonian) delete [] Hamiltonian;
if (costates) delete [] costates;
if (path) delete [] path;
if (events) delete [] events;
if (linkages) delete linkages;
}
};
typedef class dual_str Dual;
typedef struct {
int nnodes;
int nvars;
int ncons;
int n_obj_evals;
int n_con_evals;
int n_jacobian_evals;
int n_hessian_evals;
int n_ode_rhs_evals;
double epsilon_max;
double CPU_time;
string method;
} MeshStats;
struct guess_str {
MatrixXd controls;
MatrixXd states;
MatrixXd parameters;
MatrixXd time;
};
typedef struct guess_str Guess;
struct alg_str {
int nlp_iter_max;
double nlp_tolerance;
string nlp_method;
string scaling;
string derivatives;
string constraint_scaling;
string ps_method;
string collocation_method;
string hessian;
string defect_scaling;
string diff_matrix;
string parameter_statistics;
string ipopt_linear_solver;
double jac_sparsity_ratio;
double hess_sparsity_ratio;
int print_level; // 1: detailed output on screen and files (default), 0: no output
int save_sparsity_pattern;
int nsteps_error_integration;
int parameter_estimation_norm;
double ode_tolerance;
double mr_max_increment_factor;
int mr_max_iterations;
int mr_min_extrapolation_points;
int mr_initial_increment;
double mr_kappa;
int mr_M1;
string mesh_refinement;
int switch_order;
double ipopt_max_cpu_time;
};
typedef struct alg_str Alg;
struct ulbounds_str {
MatrixXd events;
MatrixXd path;
MatrixXd states;
MatrixXd controls;
MatrixXd parameters;
double StartTime;
double EndTime;
};
typedef struct ulbounds_str ULBounds;
struct bounds_str {
ULBounds lower;
ULBounds upper;
};
struct name_str {
string* states_;
string* controls_;
string* parameters_;
string& states(int i) {return states_[i-1];}
string& controls(int i) { return controls_[i-1];}
string& parameters(int i) {return parameters_[i-1];}
};
typedef struct name_str Name;
struct units_str {
string* states_;
string* controls_;
string* parameters_;
string time;
string& states(int i) {return states_[i-1];}
string& controls(int i) { return controls_[i-1];}
string& parameters(int i) {return parameters_[i-1];}
};
typedef struct units_str Units;
typedef struct bounds_str Bounds;
struct scaling_str {
MatrixXd controls;
MatrixXd states;
MatrixXd parameters;
MatrixXd defects;
MatrixXd path;
MatrixXd events;
double time;
};
typedef struct scaling_str Scaling;
struct prob_scaling_str {
MatrixXd linkages;
double objective;
};
typedef struct prob_scaling_str ProbScaling;
struct phases_str {
int nstates;
int ncontrols;
int nparameters;
int nobserved;
int nsamples;
int nevents;
int npath;
int current_number_of_intervals;
Bounds bounds;
Guess guess;
RowVectorXi nodes;
Scaling scale;
bool zero_cost_integrand;
MatrixXd observations;
MatrixXd covariance;
MatrixXd residual_weights;
MatrixXd observation_nodes;
double regularization_factor;
Name name;
Units units;
};
typedef struct phases_str Phases;
struct prob_ul_bounds {
MatrixXd linkage;
MatrixXd times;
};
typedef prob_ul_bounds ProbULBounds;
struct prob_bounds_str {
ProbULBounds lower;
ProbULBounds upper;
};
typedef struct prob_bounds_str ProbBounds;
typedef class work_str Workspace;
class prob_str {
public:
prob_str()
{
phase = NULL;
}
~prob_str()
{
if (phase)
{
delete [] phase;
}
}
int nphases;
Phases* phase;
ProbScaling scale;
int nlinkages;
bool multi_segment_flag;
bool continuous_controls_flag;
ProbBounds bounds;
string name;
string outfilename;
void* user_data;
Phases& phases(int iphase);
adouble (*endpoint_cost)(adouble* initial_states, adouble* final_states, adouble* parameters,adouble& t0, adouble& tf, adouble* xad, int iphase, Workspace* workspace);
adouble (*integrand_cost)(adouble* states, adouble* controls, adouble* parameters, adouble& time, adouble* xad, int iphase, Workspace* workspace);
void (*dae)(adouble* derivatives, adouble* path, adouble* states, adouble* controls, adouble* parameters, adouble& time, adouble* xad, int iphase, Workspace* workspace);
void (*events)(adouble* e, adouble* initial_states, adouble* final_states, adouble* parameters,adouble& t0, adouble& tf, adouble* xad, int iphase, Workspace* workspace);
void (*linkages)( adouble* linkages, adouble* xad, Workspace* workspace);
void (*observation_function)(adouble* observed_variable, adouble* states, adouble* controls, adouble* parameters, adouble& time, int k, adouble* xad, int iphase, Workspace* workspace);
};
typedef class prob_str Prob;
class sol_str {
public:
sol_str()
{
states = NULL;
controls = NULL;
nodes = NULL;
parameters = NULL;
relative_errors = NULL;
integrand_cost = NULL;
endpoint_cost = NULL;
integrated_cost = NULL;
xad = NULL;
mesh_stats = NULL;
}
~sol_str()
{
if (this->states) delete [] this->states;
if (this->controls) delete [] this->controls;
if (this->nodes) delete [] this->nodes;
if (this->integrand_cost) delete [] this->integrand_cost;
if (this->parameters) delete [] this->parameters;
if (this->relative_errors) delete [] this->relative_errors;
if (this->endpoint_cost) delete [] this->endpoint_cost;
if (this->integrated_cost) delete [] this->integrated_cost;
if (this->mesh_stats) delete [] this->mesh_stats;
}
MatrixXd *states;
MatrixXd *controls;
MatrixXd *nodes;
MatrixXd *parameters;
MatrixXd *relative_errors;
MatrixXd *integrand_cost;
Dual dual;
double *endpoint_cost;
double *integrated_cost;
double cost;
adouble* xad;
int nlp_return_code;
double cpu_time;
int error_flag;
string error_msg;
int mesh_refinement_iterations;
MeshStats* mesh_stats;
string start_date_and_time;
string end_date_and_time;
Prob* problem;
MatrixXd& get_states_in_phase(int iphase);
MatrixXd& get_controls_in_phase(int iphase);
MatrixXd& get_time_in_phase(int iphase);
MatrixXd& get_parameters_in_phase(int iphase);
MatrixXd& get_dual_costates_in_phase(int iphase);
MatrixXd& get_dual_hamiltonian_in_phase(int iphase);
MatrixXd& get_dual_path_in_phase(int iphase);
MatrixXd& get_dual_events_in_phase(int iphase);
MatrixXd& get_dual_linkages();
MatrixXd& get_relative_local_error_in_phase(int iphase);
double get_cost() { return cost; }
};
typedef class sol_str Sol;
typedef struct {
MatrixXd* dfdx_j;
MatrixXd* F1;
MatrixXd* F2;
MatrixXd* F3;
MatrixXd* F4;
double* x;
double* g;
adouble* xad;
adouble* gad;
} GRWORK;
class MSdata {
public:
MSdata() {
nodes.resize(1);
}
MSdata(long n) {
nodes.resize(n);
}
int nsegments;
int nstates;
int nparameters;
int ncontrols;
int npath;
int ninitial_events;
int nfinal_events;
bool continuous_controls;
RowVectorXi nodes;
};
typedef struct {
int** colindex;
int* size;
int number;
} IGroup;
class work_str {
public:
work_str(Prob& problem, Alg& algorithm, Sol& solution);
~work_str();
long unsigned int nphases;
Sol* solution;
Prob* problem;
Alg* algorithm;
MatrixXd* P;
RowVectorXi* sindex;
MatrixXd* w;
MatrixXd* D;
MatrixXd* D2;
MatrixXd* snodes;
MatrixXd* old_snodes;
MatrixXd* xlb;
MatrixXd* xub;
MatrixXd* x0;
MatrixXd* lambda;
MatrixXd* dual_costates;
MatrixXd* dual_path;
MatrixXd* dual_events;
MatrixXd* Xsnopt;
MatrixXd* gsnopt;
MatrixXd* DerivResid;
MatrixXd* h;
MatrixXd* Xdotgg;
MatrixXd* e;
MatrixXd* hgg;
MatrixXd* Xip;
MatrixXd* JacRow;
MatrixXd* Gip;
MatrixXd* GFip;
TripletSparseMatrix* Ax;
TripletSparseMatrix* As;
TripletSparseMatrix* Gsp;
MatrixXd* control_scaling;
MatrixXd* control_shift;
MatrixXd* state_scaling;
MatrixXd* parameter_scaling;
MatrixXd* state_shift;
MatrixXd* deriv_scaling;
MatrixXd* path_scaling;
MatrixXd* event_scaling;
MatrixXd* constraint_scaling;
MatrixXd* linkage;
MatrixXd* prev_states;
MatrixXd* prev_costates;
MatrixXd* prev_controls;
MatrixXd* prev_param;
MatrixXd* prev_path;
MatrixXd* prev_nodes;
MatrixXd* prev_t0;
MatrixXd* prev_tf;
MatrixXd* Xdot;
MatrixXd* JacCol1;
MatrixXd* JacCol2;
MatrixXd* JacCol3;
MatrixXd* xp;
MatrixXd* emax_history;
MatrixXd* order_reduction;
MatrixXd* old_relative_errors;
MatrixXd* error_scaling_weights;
double obj_scaling;
GRWORK* grw;
int nvars;
int ncons;
int **current_nodes;
int jac_done;
int* iArow;
int* jAcol;
int* iGrow;
int* jGcol;
double* jac_values;
double* jac_Aij;
double* jac_Gij;
int jac_nnz;
int jac_nnzA;
int jac_nnzG;
double* nrm_row;
unsigned int* hess_ir;
unsigned int* hess_jc;
unsigned int* iGfun;
unsigned int* jGvar;
int* iGfun1;
int* jGvar1;
unsigned int* iGfun2;
unsigned int* jGvar2;
int use_constraint_scaling;
int F_nnz;
double* G2;
double* G3;
double* G4;
adouble* xad;
adouble* gad;
adouble** states;
adouble** controls;
adouble** parameters;
adouble** resid;
adouble** derivatives;
adouble** initial_states;
adouble** final_states;
adouble** initial_controls;
adouble** final_controls;
adouble** events;
adouble** path;
adouble** states_traj;
adouble** derivs_traj;
adouble** second_derivs_traj;
adouble* linkages;
adouble* fgad;
adouble* time_array_tmp;
adouble* single_trajectory_tmp;
adouble* L_ad_tmp;
adouble* u_spline;
adouble* z_spline;
adouble* y2a_spline;
adouble** states_next;
adouble** controls_next;
adouble** derivatives_next;
adouble** path_next;
adouble** path_bar;
adouble** states_bar;
adouble** controls_bar;
adouble** derivatives_bar;
adouble** observed_variable;
adouble** observed_residual;
adouble** lam_resid;
adouble** interp_states_pe;
adouble** interp_controls_pe;
double* lambda_d;
double* fg;
bool trace_f_done;
IGroup* igroup;
char text[2000];
FILE* psopt_solution_summary_file;
FILE* mesh_statistics;
FILE* mesh_statistics_tex;
int current_mesh_refinement_iteration;
bool auto_linked_flag;
bool enable_nlp_counters;
string differential_defects;
clock_t start_ticks;
// tape tags to be used by ADOL_C
int tag_f ;
int tag_g ;
int tag_hess ;
int tag_fg ;
int tag_gc ;
void *user_data;
// A persistent variable for warm starts in SNOPT7
int nS;
};
struct xad_str {
adouble *xad;
Workspace *workspace;
};
typedef xad_str XAD;
// Function prototypes
void ScalarGradientAD( adouble (*fun)(adouble *, Workspace*), MatrixXd& x, MatrixXd* grad, bool* flag, int itag, Workspace* workspace );
void EfficientlyComputeJacobianNonZeros( void fun(MatrixXd& x, MatrixXd* f, Workspace* ), MatrixXd& x,
int nf, double *nzvalue, int nnz, int* iArow, int* jAcol, IGroup* igroup, GRWORK* grw, Workspace* workspace );
void compute_jacobian_of_constraints_with_respect_to_variables(MatrixXd& Jc, MatrixXd& X, MatrixXd& XL, MatrixXd& XU, Workspace* workspace);
void compute_jacobian_of_residual_vector_with_respect_to_variables(MatrixXd& Jr, MatrixXd& X, MatrixXd& XL, MatrixXd& XU, Workspace* workspace);
void getIndexGroups( IGroup* igroup, int nrows, int ncols, int nnz, int* iArow, int* jAcol, Workspace* workspace);
void getIndexGroups( IGroup* igroup, int nrows, int ncols, int nnz, int* iArow, int* jAcol);
void deleteIndexGroups(IGroup* igroup, int ncols );
int psopt(Sol& solution, Prob& problem, Alg& algorithm);
void psopt_level2_setup(Prob& problem, Alg& algorithm);
void initialize_solution(Sol& solution, Prob& problem, Alg& algorithm, Workspace* workspace);
void lglnodes(int N, MatrixXd& x, MatrixXd& w, MatrixXd& P, MatrixXd& D, Workspace* workspace);
void cglnodes(int N, MatrixXd& x, MatrixXd& w, MatrixXd& D, Workspace* workspace);
void copy_decision_variables(Sol& solution, MatrixXd& x, Prob& problem, Alg& algorithm, Workspace* workspace);
double ff( MatrixXd& x );
void gg( MatrixXd& x, MatrixXd* g );
void fg_num(MatrixXd& x, MatrixXd* fg, Workspace* workspace);
void define_initial_nlp_guess(MatrixXd& x0, MatrixXd& lambda, Sol& solution, Prob& problem, Alg& algorithm, Workspace* workspace);
void determine_scaling_factors_for_variables(Sol& solution, Prob& problem, Alg& algorithm);
void determine_objective_scaling(MatrixXd& X,Sol& solution, Prob& problem, Alg& algorithm, Workspace* workspace );
void determine_constraint_scaling_factors(MatrixXd & X, Sol& solution, Prob& problem, Alg& algorithm, Workspace* workspace);
void define_nlp_bounds(MatrixXd& xlb,MatrixXd& xub,Prob& problem, Alg& algorithm, Workspace* workspace);
double convert_to_original_time(double tbar,double t0,double tf);
void resize_solution(Sol& solution, Prob& problem, Alg& algorithm);
void hot_start_nlp_guess(MatrixXd& x0,MatrixXd& lambda, Sol& solution,Prob& problem,Alg& algorithm, MatrixXd* prev_states, MatrixXd* prev_controls, MatrixXd* prev_costates, MatrixXd* prev_path, MatrixXd* prev_nodes, MatrixXd* prev_param, MatrixXd& prev_t0, MatrixXd& prev_tf, Workspace* workspace );
void lagrange_interpolation(MatrixXd& y, MatrixXd& x, MatrixXd& pointx, MatrixXd& pointy);
double smooth_fabs(double x, double eps);
adouble smooth_fabs(adouble x, double eps);
void Jacobian( void fun(MatrixXd& x, MatrixXd* f ), MatrixXd& x, MatrixXd* grad, GRWORK* grw );
void JacobianRow( void fun(MatrixXd& x, MatrixXd* f, Workspace* ), MatrixXd& x, int iRow, int nf,
MatrixXd* JacRow, GRWORK* grw, Workspace* workspace );
void JacobianColumn( void fun(MatrixXd& x, MatrixXd* f, Workspace* ), MatrixXd& x, MatrixXd& xlb, MatrixXd& xub, int jCol,
MatrixXd* JacColumn, GRWORK* grw, Workspace* workspace );
void evaluate_matrix_of_integrated_errors_in_phase(MatrixXd& eta, int iphase, adouble* xad, int nsteps, Workspace* workspace);
void evaluate_solution(Prob& problem,Alg& algorithm,Sol& solution, Workspace* workspace);
void compute_next_mesh_size( Prob& problem, Alg& algorithm, Sol& solution, Workspace* workspace );
int get_max_nodes(Prob& problem,int iphase, Alg* algorithm);
void estimate_order_reduction(Prob& problem,Alg& algorithm,Sol& solution, Workspace* workspace);
void zero_order_reduction(Prob& problem,Alg& algorithm,Sol& solution, Workspace* workspace);
void construct_new_mesh(Prob& problem,Alg& algorithm,Sol& solution, Workspace* workspace);
bool check_for_equidistributed_error(Prob& problem,Alg& algorithm,Sol& solution);
bool use_local_collocation(Alg & algorithm);
bool use_global_collocation(Alg & algorithm);
void bilinear_interpolation(adouble* z, adouble& x, adouble& y, MatrixXd& X, MatrixXd& Y, MatrixXd& Z);
void spline_2d_interpolation(adouble* z, adouble& x, adouble& y, MatrixXd& X, MatrixXd& Y, MatrixXd& Z, Workspace* workspace);
void smooth_bilinear_interpolation(adouble* z, adouble& x, adouble& y, MatrixXd& X, MatrixXd& Y, MatrixXd& Z);
void smooth_linear_interpolation(adouble* y, adouble& x, adouble* Xdata, adouble* Ydata, int n);
void get_times(adouble *t0, adouble *tf, adouble* xad, int iphase, Workspace* workspace);
void get_states(adouble* states, adouble* xad, int iphase, int k, Workspace* workspace);
void get_controls_bar(adouble* controls_bar, adouble* xad, int iphase, int k, Workspace* workspace);
void rr_num(MatrixXd& X, MatrixXd* residual_vector, Workspace* workspace);
void extract_parameter_covariance(MatrixXd& Cp, MatrixXd& C, Workspace* workspace);
void get_scaled_decision_variables_and_bounds(MatrixXd& x, MatrixXd& xlb, MatrixXd& xub, Workspace* workspace);
int NLP_interface(
Alg& algorithm,
MatrixXd* x0,
double (*f)(MatrixXd&, Workspace*),
void (*g)(MatrixXd&,MatrixXd*,Workspace*),
int m = 0,
int neqcstr = 0,
MatrixXd* xlb = NULL,
MatrixXd* xub = NULL,
MatrixXd* lambda= NULL,
int hotflag = 0,
int iprint = 1,
Workspace* workspace=NULL,
void* user_data = NULL );
void ScalarGradient( double (*fun)(MatrixXd& x, Workspace*), MatrixXd& x,MatrixXd* grad, GRWORK* grw, Workspace* workspace );
void DetectJacobianSparsity(void fun(MatrixXd& x, MatrixXd* f, Workspace* ), MatrixXd& x, int nf,
int* nnzA, int* iArow, int* jAcol, double* Aij,
int* nnzG, int* jGrow, int* jGcol,
GRWORK* grw, Workspace* workspace);
void DetectJacobianSparsityAD(void fun(MatrixXd& x, MatrixXd* f, Workspace* ), MatrixXd& x, int nf,
int* nnzA, int* iArow, int* jAcol, double* Aij,
int* nnzG, int* jGrow, int* jGcol,
GRWORK* grw, Workspace* workspace);
void ComputeJacobianNonZeros( void fun(MatrixXd& x, MatrixXd* f ), MatrixXd& x, int nf, double *nzvalue, int nnz, int* iArow, int* jAcol, GRWORK* grw, Workspace* workspace );
void Jacobian( void fun(MatrixXd& x, MatrixXd* f ), MatrixXd& x,
MatrixXd* grad, GRWORK* grw );
void initialize_workspace_vars(Prob& problem, Alg& algorithm, Sol& solution, Workspace* workspace);
void resize_workspace_vars(Prob& problem, Alg& algorithm, Sol& solution, Workspace* workspace);
int get_number_nlp_vars(Prob& problem, Workspace* workspace);
int get_number_nlp_constraints(Prob& problem, Workspace* workspace);
int get_max_number_nlp_vars(Prob& problem, Alg& algorithm);
int get_max_number_nlp_constraints(Prob& problem, Alg& algorithm);
void psopt_level1_setup(Prob& problem);
int get_max_nodes_in_all_phases(Prob& problem, Alg& algorithm);
adouble convert_to_original_time_ad(double tbar,adouble& t0,adouble& tf);
int get_nvars_phase_i(Prob& problem, int i, Workspace* workspace);
int get_ncons_phase_i(Prob& problem, int i, Workspace* workspace);
void get_constraint_bounds(double* g_l, double* g_u, Workspace* workspace);
void mtrx_mul_trans(adouble* a,double* b,adouble* ab,int na,int ma,int nb,int mb);
void get_initial_states(adouble* states, adouble* xad, int i, Workspace* workspace);
void get_final_states(adouble* states, adouble* xad, int i, Workspace* workspace);
void get_controls(adouble* controls, adouble* xad, int i, int k, Workspace* workspace);
void get_final_controls(adouble* controls, adouble* xad, int i, Workspace* workspace);
void get_initial_controls(adouble* controls, adouble* xad, int i, Workspace* workspace);
void get_individual_control_trajectory(adouble *control_traj, int control_index, int iphase, adouble* xad, Workspace* workspace);
void get_individual_state_trajectory(adouble *state_traj, int state_index, int iphase, adouble* xad, Workspace* workspace);
void get_delayed_control(adouble* delayed_control, int control_index, int iphase, adouble& time, double delay, adouble* xad, Workspace* workspace);
void get_delayed_state(adouble* delayed_state, int state_index, int iphase, adouble& time, double delay, adouble* xad, Workspace* workspace);
void get_interpolated_state(adouble* interp_state, int state_index, int iphase, adouble& time, adouble* xad, Workspace* workspace);
void get_interpolated_control(adouble* interp_control, int control_index, int iphase, adouble& time, adouble* xad, Workspace* workspace);
void get_state_derivative(adouble* state_derivative, int state_index, int iphase, adouble& time, adouble* xad, Workspace* workspace);
void get_control_derivative(adouble* control_derivative, int control_index, int iphase, adouble& time, adouble* xad, Workspace* workspace);
int get_number_of_controls(Prob& problem, int iphase);
int get_number_of_states(Prob& problem,int iphase);
int get_number_of_events(Prob& problem,int iphase);
int get_number_of_path_constraints(Prob& problem,int iphase);
int get_number_of_nodes(Prob& problem,int iphase);
int get_number_of_parameters(Prob& problem,int iphase);
int get_total_number_of_parameters(Prob& problem);
int get_number_of_linkages(Prob& problem);
int get_number_of_phases(Prob& problem);
void lagrange_interpolation_ad(adouble* y, adouble& x, adouble* pointx, adouble* pointy, int npoints, Workspace* workspace);
void linear_interpolation(adouble* y, adouble& x, adouble* pointx, adouble* pointy, int npoints);
void linear_interpolation(MatrixXd& y, double x, MatrixXd& pointx, MatrixXd& pointy, int npoints);
void linear_interpolation(MatrixXd& y, MatrixXd& x, MatrixXd& pointx, MatrixXd& pointy, int npoints);
void linear_interpolation(adouble* y, adouble x, MatrixXd& pointx, MatrixXd& pointy, int npoints);
void smooth_linear_interpolation(adouble* y, adouble& x, MatrixXd& Xdata, MatrixXd& Ydata, int n);
void spline_interpolation(adouble* y, adouble& x, adouble* pointx, adouble* pointy, int npoints, Workspace* workspace);
void spline_interpolation(adouble* y, adouble& x, MatrixXd& Xdata, MatrixXd& Ydata, int n);
void zoh_interpolation(adouble* y, adouble x, MatrixXd& pointx, MatrixXd& pointy, int npoints);
adouble get_initial_time(adouble* xad, int iphase, Workspace* workspace);
adouble get_final_time(adouble* xad, int iphase, Workspace* workspace);
void get_parameters(adouble* parameters, adouble* xad, int iphase, Workspace* workspace);
bool useAutomaticDifferentiation(Alg& algorithm);
void gg_ad( adouble* xad, adouble* gad, Workspace* workspace );
double ff_num(MatrixXd& x, Workspace* workspace);
void gg_num( MatrixXd& x, MatrixXd* g, Workspace* workspace );
void fg_ad( adouble* x, adouble* fg, Workspace* workspace);
void compute_derivatives_trajectory( MatrixXd& Xdot, Prob& problem, Sol& solution, int i, Workspace* workspace );
adouble integrate( adouble (*integrand)(adouble*,adouble*,adouble*,adouble&,adouble*,int, Workspace* workspace), adouble* xad, int i, Workspace* workspace );
void auto_link(adouble* linkages, int* index, adouble* xad, int iphase_a, int iphase_b, Workspace* workspace);
void auto_link_2(adouble* linkages, int* index, adouble* xad, int iphase_a, int iphase_b, Workspace* workspace);
void plot(const MatrixXd& x, const MatrixXd& y,const string& title,
const char* xlabel, const char* ylabel, const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void plot(const MatrixXd& x1, const MatrixXd& y1, const MatrixXd& x2, const MatrixXd& y2, const string& title,
const char* xlabel, const char* ylabel, const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void plot(const MatrixXd& x1, const MatrixXd& y1, const MatrixXd& x2, const MatrixXd& y2, const MatrixXd& x3, const MatrixXd& y3,
const string& title, const char* xlabel, const char* ylabel, const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void multiplot(const MatrixXd& x, const MatrixXd& y, const string& title, const char* xlabel, const char* ylabel, const char* legend, int nrows=0, int ncols=0, const char* terminal=NULL, const char* output=NULL ) ;
void spplot(const MatrixXd& x1a, const MatrixXd& y1a, const MatrixXd& x2a, const MatrixXd& y2a, const string& title, const char* xlabel, const char* ylabel, const char* legend, const char* terminal=NULL, const char* output=NULL);
void polar(const MatrixXd& theta, const MatrixXd& r, const string& title,
const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void polar(const MatrixXd& theta, const MatrixXd& r, const MatrixXd& theta2, const MatrixXd& r2, const string& title,
const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void polar(const MatrixXd& theta, const MatrixXd& r, const MatrixXd& theta2, const MatrixXd& r2, const MatrixXd& theta3, const MatrixXd& r3, const string& title,
const char* legend=NULL, const char* terminal=NULL, const char* output=NULL);
void surf(const MatrixXd& x, const MatrixXd& y, const MatrixXd& z, const string& title, const char* xlabel, const char* ylabel, const char* zlabel, const char* terminal=NULL, const char* output=NULL, const char* view=NULL);
void plot3(const MatrixXd& x, const MatrixXd& y, const MatrixXd& z, const string& title, const char* xlabel, const char* ylabel, const char* zlabel, const char* terminal=NULL, const char* output=NULL, const char* view=NULL);
void psopt_error_message(const char *error_text);
adouble dot(adouble* x, adouble* y, int n);
adouble smooth_heaviside(adouble x, double a);
adouble smooth_sign(adouble x, double a);
void cross(adouble* x, adouble* y, adouble* z);
void validate_user_input(Prob& problem, Alg& algorithm, Workspace* workspace);
void print_psopt_summary(Prob& problem, Alg& algorithm, Sol& solution, Workspace* workspace);
void psopt_main(Sol& solution, Prob& problem, Alg& algorithm, unique_ptr<Workspace>& workspace_up);
void clip_vector_given_bounds(MatrixXd& xp, MatrixXd& xlb, MatrixXd& xub);
void psopt_print(Workspace* workspace, const char* msg);
int auto_link_count(Prob& problem, int nstates);
void multi_segment_setup(Prob& problem, Alg& algorithm, MSdata& msdata);
int auto_link2_count(Prob& problem, int nstates, int ncontrols);
void auto_phase_setup(Prob& problem, int n_final_events,RowVectorXi& nodes);
void auto_phase_bounds(Prob& problem);
void auto_phase_guess(Prob& problem,MatrixXd& controls, MatrixXd& states, MatrixXd& param, MatrixXd& time);