本文整理汇总了C++中SX::size1方法的典型用法代码示例。如果您正苦于以下问题:C++ SX::size1方法的具体用法?C++ SX::size1怎么用?C++ SX::size1使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SX
的用法示例。
在下文中一共展示了SX::size1方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(){
// Declare variables
SX u = SX::sym("u"); // control
SX r = SX::sym("r"), s = SX::sym("s"); // states
SX x = vertcat(r,s);
// Number of differential states
int nx = x.size1();
// Number of controls
int nu = u.size1();
// Bounds and initial guess for the control
vector<double> u_min = { -0.75 };
vector<double> u_max = { 1.0 };
vector<double> u_init = { 0.0 };
// Bounds and initial guess for the state
vector<double> x0_min = { 0, 1 };
vector<double> x0_max = { 0, 1 };
vector<double> x_min = {-inf, -inf };
vector<double> x_max = { inf, inf };
vector<double> xf_min = { 0, 0 };
vector<double> xf_max = { 0, 0 };
vector<double> x_init = { 0, 0 };
// Final time
double tf = 20.0;
// Number of shooting nodes
int ns = 50;
// ODE right hand side and quadrature
SX ode = vertcat((1 - s*s)*r - s + u, r);
SX quad = r*r + s*s + u*u;
SXDict dae = {{"x", x}, {"p", u}, {"ode", ode}, {"quad", quad}};
// Create an integrator (CVodes)
Function F = integrator("integrator", "cvodes", dae, {{"t0", 0}, {"tf", tf/ns}});
// Total number of NLP variables
int NV = nx*(ns+1) + nu*ns;
// Declare variable vector for the NLP
MX V = MX::sym("V",NV);
// NLP variable bounds and initial guess
vector<double> v_min,v_max,v_init;
// Offset in V
int offset=0;
// State at each shooting node and control for each shooting interval
vector<MX> X, U;
for(int k=0; k<ns; ++k){
// Local state
X.push_back( V.nz(Slice(offset,offset+nx)));
if(k==0){
v_min.insert(v_min.end(), x0_min.begin(), x0_min.end());
v_max.insert(v_max.end(), x0_max.begin(), x0_max.end());
} else {
v_min.insert(v_min.end(), x_min.begin(), x_min.end());
v_max.insert(v_max.end(), x_max.begin(), x_max.end());
}
v_init.insert(v_init.end(), x_init.begin(), x_init.end());
offset += nx;
// Local control
U.push_back( V.nz(Slice(offset,offset+nu)));
v_min.insert(v_min.end(), u_min.begin(), u_min.end());
v_max.insert(v_max.end(), u_max.begin(), u_max.end());
v_init.insert(v_init.end(), u_init.begin(), u_init.end());
offset += nu;
}
// State at end
X.push_back(V.nz(Slice(offset,offset+nx)));
v_min.insert(v_min.end(), xf_min.begin(), xf_min.end());
v_max.insert(v_max.end(), xf_max.begin(), xf_max.end());
v_init.insert(v_init.end(), x_init.begin(), x_init.end());
offset += nx;
// Make sure that the size of the variable vector is consistent with the number of variables that we have referenced
casadi_assert(offset==NV);
// Objective function
MX J = 0;
//Constraint function and bounds
vector<MX> g;
// Loop over shooting nodes
for(int k=0; k<ns; ++k){
// Create an evaluation node
MXDict I_out = F(MXDict{{"x0", X[k]}, {"p", U[k]}});
// Save continuity constraints
g.push_back( I_out.at("xf") - X[k+1] );
// Add objective function contribution
//.........这里部分代码省略.........
示例2: main
int main(){
// Declare variables
SX u = SX::sym("u"); // control
SX r = SX::sym("r"), s = SX::sym("s"); // states
SX x = vertcat(r,s);
// Number of differential states
int nx = x.size1();
// Number of controls
int nu = u.size1();
// Bounds and initial guess for the control
vector<double> u_min = { -0.75 };
vector<double> u_max = { 1.0 };
vector<double> u_init = { 0.0 };
// Bounds and initial guess for the state
vector<double> x0_min = { 0, 1 };
vector<double> x0_max = { 0, 1 };
vector<double> x_min = {-inf, -inf };
vector<double> x_max = { inf, inf };
vector<double> xf_min = { 0, 0 };
vector<double> xf_max = { 0, 0 };
vector<double> x_init = { 0, 0 };
// Final time
double tf = 20.0;
// Number of shooting nodes
int ns = 50;
// ODE right hand side and quadrature
SX ode = vertcat((1 - s*s)*r - s + u, r);
SX quad = r*r + s*s + u*u;
SXFunction rhs("rhs", daeIn("x", x, "p", u), daeOut("ode", ode, "quad", quad));
// Create an integrator (CVodes)
Integrator integrator("integrator", "cvodes", rhs, make_dict("t0", 0, "tf", tf/ns));
// Total number of NLP variables
int NV = nx*(ns+1) + nu*ns;
// Declare variable vector for the NLP
MX V = MX::sym("V",NV);
// NLP variable bounds and initial guess
vector<double> v_min,v_max,v_init;
// Offset in V
int offset=0;
// State at each shooting node and control for each shooting interval
vector<MX> X, U;
for(int k=0; k<ns; ++k){
// Local state
X.push_back( V[Slice(offset,offset+nx)] );
if(k==0){
v_min.insert(v_min.end(), x0_min.begin(), x0_min.end());
v_max.insert(v_max.end(), x0_max.begin(), x0_max.end());
} else {
v_min.insert(v_min.end(), x_min.begin(), x_min.end());
v_max.insert(v_max.end(), x_max.begin(), x_max.end());
}
v_init.insert(v_init.end(), x_init.begin(), x_init.end());
offset += nx;
// Local control
U.push_back( V[Slice(offset,offset+nu)] );
v_min.insert(v_min.end(), u_min.begin(), u_min.end());
v_max.insert(v_max.end(), u_max.begin(), u_max.end());
v_init.insert(v_init.end(), u_init.begin(), u_init.end());
offset += nu;
}
// State at end
X.push_back(V[Slice(offset,offset+nx)]);
v_min.insert(v_min.end(), xf_min.begin(), xf_min.end());
v_max.insert(v_max.end(), xf_max.begin(), xf_max.end());
v_init.insert(v_init.end(), x_init.begin(), x_init.end());
offset += nx;
// Make sure that the size of the variable vector is consistent with the number of variables that we have referenced
casadi_assert(offset==NV);
// Objective function
MX J = 0;
//Constraint function and bounds
vector<MX> g;
// Loop over shooting nodes
for(int k=0; k<ns; ++k){
// Create an evaluation node
map<string, MX> I_out = integrator(make_map("x0", X[k], "p", U[k]));
// Save continuity constraints
g.push_back( I_out.at("xf") - X[k+1] );
// Add objective function contribution
//.........这里部分代码省略.........
示例3: init
//.........这里部分代码省略.........
// Substitute out the v from the h
SX d_def = (v_eq + v)-d;
SXVector ex(3);
ex[0] = f1;
ex[1] = f2;
ex[2] = f;
substituteInPlace(v, d_def, ex, false);
SX f1_z = ex[0];
SX f2_z = ex[1];
SX f_z = ex[2];
// Modified function Z
enum ZIn{Z_U,Z_D,Z_LAM_X,Z_LAM_F2,Z_NUM_IN};
SXVector zfcn_in(Z_NUM_IN);
zfcn_in[Z_U] = u;
zfcn_in[Z_D] = d;
zfcn_in[Z_LAM_X] = lam_x;
zfcn_in[Z_LAM_F2] = lam_f2;
enum ZOut{Z_D_DEF,Z_F12,Z_NUM_OUT};
SXVector zfcn_out(Z_NUM_OUT);
zfcn_out[Z_D_DEF] = d_def;
zfcn_out[Z_F12] = vertcat(f1_z,f2_z);
SXFunction zfcn(zfcn_in,zfcn_out);
zfcn.init();
if(verbose_){
cout << "Generated reconstruction function ( " << zfcn.getAlgorithmSize() << " nodes)." << endl;
}
// Matrix A and B in lifted Newton
SX B = zfcn.jac(Z_U,Z_F12);
SX B1 = B(Slice(0,nf1),Slice(0,B.size2()));
SX B2 = B(Slice(nf1,B.size1()),Slice(0,B.size2()));
if(verbose_){
cout << "Formed B1 (dimension " << B1.size1() << "-by-" << B1.size2() << ", "<< B1.size() << " nonzeros) " <<
"and B2 (dimension " << B2.size1() << "-by-" << B2.size2() << ", "<< B2.size() << " nonzeros)." << endl;
}
// Step in u
SX du = ssym("du",nu);
SX dlam_f2 = ssym("dlam_f2",lam_f2.sparsity());
SX b1 = f1_z;
SX b2 = f2_z;
SX e;
if(nv > 0){
// Directional derivative of Z
vector<vector<SX> > Z_fwdSeed(2,zfcn_in);
vector<vector<SX> > Z_fwdSens(2,zfcn_out);
vector<vector<SX> > Z_adjSeed;
vector<vector<SX> > Z_adjSens;
Z_fwdSeed[0][Z_U].setZero();
Z_fwdSeed[0][Z_D] = -d;
Z_fwdSeed[0][Z_LAM_X].setZero();
Z_fwdSeed[0][Z_LAM_F2].setZero();
Z_fwdSeed[1][Z_U] = du;
Z_fwdSeed[1][Z_D] = -d;
Z_fwdSeed[1][Z_LAM_X].setZero();
Z_fwdSeed[1][Z_LAM_F2] = dlam_f2;
zfcn.eval(zfcn_in,zfcn_out,Z_fwdSeed,Z_fwdSens,Z_adjSeed,Z_adjSens);