Dvector::Dvector(const Dvector & P) { std::cout << "Appel de Dvector(const Dvector & P) \n"; dim=P.size(); if(dim == 0) return; vect=new double[dim]; for(int i=0;i<dim;i++){ vect[i]=P.getVect(i); } }
Dvector operator -(const Dvector & v) { if (v.size() == 0) { Dvector vect(0); return vect; } else { Dvector vec = (Dvector)v ; Dvector vect(v.size()); for (int i=0; i<v.size() ; i++) vect(i) = -vec(i) ; return(vect) ; } }
int main() { //int sizeOfX = 1000000000; int sizeOfX = 100; Dvector x(sizeOfX); x.fillRandomly(); Dvector y; y = x; assert(y.size() == sizeOfX); for(int i=0; i<sizeOfX; i++) assert(x[i] == y[i]); std::cout<<"OK"<<std::endl; }
int main() { cout<<"Début du test du constructeur à partir du fichier tp1_test1.txt."<<endl; std::string name("tp1_test1.txt"); //Dvector P = Dvector("tp1_test1.txt"); // marche aussi Dvector P = Dvector(name); assert(P.size()==10); std::stringstream str; P.display(str); //Il y a des arrondis en sortie, ce qui justifie les modifications par rapport au fichier d'entrée. assert(str.str() == "0.162182\n0.794285\n0.311215\n0.528533\n0.165649\n0.601982\n0.262971\n0.654079\n0.689214\n0.748152\n"); std::cout <<"OK\n"; return 0 ; }
Dvector operator *(const double &r ,const Dvector &v) { if (v.size() == 0) { Dvector vect(0); return vect; } else if (r == 1) { return v ; } else { Dvector vec = (Dvector)v ; Dvector vect(v.size()); for (int i=0; i<v.size() ; i++) vect(i) = vec(i) * r ; return(vect) ; } }
Dvector operator +(const double & r ,const Dvector & v) { if (v.size() == 0) { Dvector vect(0); return vect; } else if (r == 0) { Dvector vect(v); return vect ; } else { Dvector vec = (Dvector)v ; // à verifier pas d'autre solution ..car v = const Dvector vect(v.size()); for (int i=0; i<v.size() ; i++) vect(i) = vec(i) + r ; return(vect) ; } }
Dvector operator -(const Dvector & v1 ,const Dvector & v2) { if (v1.size() != v2.size()) { cout << "Erreur : different size \n" ; exit(1) ; } else if (v1.size() == 0) { Dvector vect(0); return vect; } else { Dvector vec1 = (Dvector)v1 ; // à verifier pas d'autre solution ..car v = const Dvector vec2 = (Dvector)v2 ; Dvector vect(v1.size()); for (int i=0; i<v1.size() ; i++) vect(i) = vec1(i) - vec2(i) ; return(vect) ; } }
Dvector operator /(const Dvector &v ,const double &r) { if (r == 0) { cout << "Erreur : Division par zero \n" ; exit(1) ; } else if (v.size() == 0) { Dvector vect(0); return vect; } else if (r == 1) { return v ; } else { Dvector vec = (Dvector)v ; Dvector vect(v.size()); for (int i=0; i<v.size() ; i++) vect(i) = vec(i)/r ; return(vect) ; } }
Dvector Dmatrix::operator * (const Dvector& vector){ assert(m == vector.size()) ; Dvector vect = Dvector(m,0) ; for (int i = 0; i < n ; i++){ for (int j = 0 ; j < m; j++){ vect(i) += (*this)(i,j) * vector(j) ; } } return vect ; }
int main() { cout<<"Debut du test de l'opérateur d'accession en lecture:"<<endl; Dvector P(2,1.0); assert(P.size()==2); std::stringstream str; P.display(str); assert(str.str() == "1\n1\n"); assert(P(1)==1); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur d'accession en écriture:"<<endl; Dvector Q(2,1.0); assert(Q.size()==2); str.str(std::string()); //réinitialisation de str Q.display(str); assert(str.str() == "1\n1\n"); Q(1)=3; assert(Q(1)==3); str.str(std::string()); Q.display(str); assert(str.str() == "1\n3\n"); cout<<"OK"<<endl; cout<<"Debut du test de ==:"<<endl; bool b=P==Q; cout<<"ok"; assert(b==false); Dvector N(P); cout<<"ok"; b=P==N; cout<<"ok"; assert(b==true); cout<<"OK"<<endl; cout<<"Debut du test de !=:"<<endl; b=P!=Q; assert(b==true); b=P!=N; assert(b==false); cout<<"OK"<<endl; cout<<"Debut du test de /= avec un double':"<<endl; N/=2; str.str(std::string()); N.display(str); assert(str.str() == "0.5\n0.5\n"); cout<<"OK"<<endl; cout<<"Debut du test de / avec un double':"<<endl; N=N/2; str.str(std::string()); N.display(str); assert(str.str() == "0.25\n0.25\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'addition de deux vecteurs':"<<endl; Dvector R = P + Q ; str.str(std::string()); R.display(str); assert(str.str() == "2\n4\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'addition de deux vecteurs sur un des deux':"<<endl; R = R + Q ; str.str(std::string()); R.display(str); assert(str.str() == "3\n7\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur += avec un vecteur:"<<endl; R += P ; str.str(std::string()); R.display(str); assert(str.str() == "4\n8\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur -= avec un vecteur:"<<endl; R -= P ; str.str(std::string()); R.display(str); assert(str.str() == "3\n7\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur - avec un vecteur:"<<endl; R = R-P ; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur *= avec un vecteur:"<<endl; R *= P; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur * avec un vecteur:"<<endl; R = R*P ; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur = avec un vecteur:"<<endl; Dvector T(1,3); T=P; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); T.resize(7,8); T=P; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); T.resize(2,0); T=P; str.str(std::string()); R.display(str); assert(str.str() == "2\n6\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur + avec un reel en externe:"<<endl; R = R+1; str.str(std::string()); R.display(str); assert(str.str() == "3\n7\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur * avec un reel en externe:"<<endl; R = R*2; str.str(std::string()); R.display(str); assert(str.str() == "6\n14\n"); cout<<"OK"<<endl; cout<<"Debut du test de l'opérateur - avec un reel en externe:"<<endl; R = R-1 ; str.str(std::string()); R.display(str); assert(str.str() == "5\n13\n"); cout<<"OK"<<endl; /* cout<<"Debut du test de l'opérateur - :"<<endl; R.Darray:operator - (R); str.str(std::string()); R.display(str); assert(str.str() == "-5\n-13\n"); cout<<"OK"<<endl;*/ /*R.Darray::operator=(-R);*/ return 0; }
void solve( const std::string& options , const Dvector& xi , const Dvector& xl , const Dvector& xu , const Dvector& gl , const Dvector& gu , FG_eval& fg_eval , ipopt::solve_result<Dvector>& solution ) { bool ok = true; typedef typename FG_eval::ADvector ADvector; CPPAD_ASSERT_KNOWN( xi.size() == xl.size() && xi.size() == xu.size() , "ipopt::solve: size of xi, xl, and xu are not all equal." ); CPPAD_ASSERT_KNOWN( gl.size() == gu.size() , "ipopt::solve: size of gl and gu are not equal." ); size_t nx = xi.size(); size_t ng = gl.size(); // Create an IpoptApplication using Ipopt::IpoptApplication; Ipopt::SmartPtr<IpoptApplication> app = new IpoptApplication(); // process the options argument size_t begin_1, end_1, begin_2, end_2, begin_3, end_3; begin_1 = 0; bool retape = false; bool sparse_forward = false; bool sparse_reverse = false; while( begin_1 < options.size() ) { // split this line into tokens while( options[begin_1] == ' ') begin_1++; end_1 = options.find_first_of(" \n", begin_1); begin_2 = end_1; while( options[begin_2] == ' ') begin_2++; end_2 = options.find_first_of(" \n", begin_2); begin_3 = end_2; while( options[begin_3] == ' ') begin_3++; end_3 = options.find_first_of(" \n", begin_3); // check for errors CPPAD_ASSERT_KNOWN( (end_1 != std::string::npos) & (end_2 != std::string::npos) & (end_3 != std::string::npos) , "ipopt::solve: missing '\\n' at end of an option line" ); CPPAD_ASSERT_KNOWN( (end_1 > begin_1) & (end_2 > begin_2) , "ipopt::solve: an option line does not have two tokens" ); // get first two tokens std::string tok_1 = options.substr(begin_1, end_1 - begin_1); std::string tok_2 = options.substr(begin_2, end_2 - begin_2); // get third token std::string tok_3; bool three_tok = false; three_tok |= tok_1 == "Sparse"; three_tok |= tok_1 == "String"; three_tok |= tok_1 == "Numeric"; three_tok |= tok_1 == "Integer"; if( three_tok ) { CPPAD_ASSERT_KNOWN( (end_3 > begin_3) , "ipopt::solve: a Sparse, String, Numeric, or Integer\n" "option line does not have three tokens." ); tok_3 = options.substr(begin_3, end_3 - begin_3); } // switch on option type if( tok_1 == "Retape" ) { CPPAD_ASSERT_KNOWN( (tok_2 == "true") | (tok_2 == "false") , "ipopt::solve: Retape value is not true or false" ); retape = (tok_2 == "true"); } else if( tok_1 == "Sparse" ) { CPPAD_ASSERT_KNOWN( (tok_2 == "true") | (tok_2 == "false") , "ipopt::solve: Sparse value is not true or false" ); CPPAD_ASSERT_KNOWN( (tok_3 == "forward") | (tok_3 == "reverse") , "ipopt::solve: Sparse direction is not forward or reverse" ); if( tok_2 == "false" ) { sparse_forward = false; sparse_reverse = false; } else { sparse_forward = tok_3 == "forward"; sparse_reverse = tok_3 == "reverse"; } } else if ( tok_1 == "String" ) app->Options()->SetStringValue(tok_2.c_str(), tok_3.c_str()); else if ( tok_1 == "Numeric" ) { Ipopt::Number value = std::atof( tok_3.c_str() ); app->Options()->SetNumericValue(tok_2.c_str(), value); } else if ( tok_1 == "Integer" ) { Ipopt::Index value = std::atoi( tok_3.c_str() ); app->Options()->SetIntegerValue(tok_2.c_str(), value); } else CPPAD_ASSERT_KNOWN( false, "ipopt::solve: First token is not one of\n" "Retape, Sparse, String, Numeric, Integer" ); begin_1 = end_3; while( options[begin_1] == ' ') begin_1++; if( options[begin_1] != '\n' ) CPPAD_ASSERT_KNOWN( false, "ipopt::solve: either more than three tokens " "or no '\\n' at end of a line" ); begin_1++; } CPPAD_ASSERT_KNOWN( ! ( retape & (sparse_forward | sparse_reverse) ) , "ipopt::solve: retape and sparse both true is not supported." ); // Initialize the IpoptApplication and process the options Ipopt::ApplicationReturnStatus status = app->Initialize(); ok &= status == Ipopt::Solve_Succeeded; if( ! ok ) { solution.status = solve_result<Dvector>::unknown; return; } // Create an interface from Ipopt to this specific problem. // Note the assumption here that ADvector is same as cppd_ipopt::ADvector size_t nf = 1; Ipopt::SmartPtr<Ipopt::TNLP> cppad_nlp = new CppAD::ipopt::solve_callback<Dvector, ADvector, FG_eval>( nf, nx, ng, xi, xl, xu, gl, gu, fg_eval, retape, sparse_forward, sparse_reverse, solution ); // Run the IpoptApplication app->OptimizeTNLP(cppad_nlp); return; }