returnValue ExportArithmeticStatement::exportCodeAssign( FILE* file, const char* const _op, const char* _realString, const char* _intString, int _precision ) const { if ( ( lhs == 0 ) || ( rhs1 == 0 ) || ( rhs2 != 0 ) ) return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); if ( ( rhs1->getNumRows( ) != lhs->getNumRows( ) ) || ( rhs1->getNumCols( ) != lhs->getNumCols( ) ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); for( uint i=0; i<getNumRows( ); ++i ) for( uint j=0; j<getNumCols( ); ++j ) { if ( ( _op[0] == '=' ) || ( rhs1->isZero(i,j) == BT_FALSE ) ) { acadoFPrintf( file,"%s %s ", lhs->get(i,j),_op ); acadoFPrintf( file,"%s;\n", rhs1->get(i,j) ); } } return SUCCESSFUL_RETURN; }
returnValue writeDoublePointerToFile( double *x, int dim, FILE *file ){ int run1; if( !file ) return ACADOERROR(RET_CAN_NOT_WRITE_INTO_FILE); for( run1 = 0; run1 < dim; run1++ ){ if( x[run1] <= ACADO_NAN - 1.0 ) acadoFPrintf(file,"%.16e%c",x[run1], LINE_SEPARATOR ); else acadoFPrintf(file,"%s%c", NOT_A_NUMBER, LINE_SEPARATOR ); } acadoFPrintf(file,"%c",LINE_SEPARATOR); return SUCCESSFUL_RETURN; }
returnValue writeDoublePointerToFile( double *x, int nRows, int nCols, FILE *file ){ int run1, run2; if( !file ) return ACADOERROR(RET_CAN_NOT_WRITE_INTO_FILE); for( run1 = 0; run1 < nRows; run1++ ){ for( run2 = 0; run2 < nCols; run2++ ){ if( x[nCols*run1+run2] <= ACADO_NAN - 1.0 ) acadoFPrintf(file,"%.16e%c",x[nCols*run1+run2], TEXT_SEPARATOR ); else acadoFPrintf(file,"%s%c",NOT_A_NUMBER, TEXT_SEPARATOR ); } acadoFPrintf(file,"%c",LINE_SEPARATOR); } acadoFPrintf(file,"%c",LINE_SEPARATOR); return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::exportForwardDeclarations( FILE *file, const char *fcnName, const char *realString ) const { acadoFPrintf(file, "\n" "/** Export of an ACADO symbolic function.\n" " *\n" " * \\param in Input to the exported function.\n" " * \\param out Output of the exported function.\n" " */\n" ); acadoFPrintf(file, "void %s(const %s* in, %s* out);\n", fcnName, realString, realString); return SUCCESSFUL_RETURN; }
double OptimizationAlgorithmBase::getObjectiveValue( const char* fileName ) const{ FILE *file = fopen(fileName,"w"); acadoFPrintf( file , "%.16e \n", getObjectiveValue() ); fclose(file); return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::exportForwardDeclarations( FILE *file, const char *fcnName, const char *realString ) const{ acadoFPrintf(file,"void %s( const %s*, %s* );\n", fcnName,realString,realString ); return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::exportHeader( FILE *file, const char *fcnName, const char *realString ) const{ if( n > 0 ) acadoFPrintf(file,"%s %s[%d];\n", realString, globalExportVariableName.getName(), n ); return SUCCESSFUL_RETURN; }
returnValue ExportStatementString::exportCode( FILE *file, const char* _realString, const char* _intString, int _precision ) const { acadoFPrintf( file,"%s", statementString ); return SUCCESSFUL_RETURN; }
returnValue ExportArithmeticStatement::exportDataDeclaration( FILE *file, const char* _realString, const char* _intString, int _precision ) const { if ( outerLoopVariable.isGiven() == BT_FALSE ) acadoFPrintf( file,"%s %s;\n", _intString,outerLoopVariable.name ); return SUCCESSFUL_RETURN; }
returnValue ExportFunctionCall::exportCode( FILE *file, const char* _realString, const char* _intString, int _precision ) const { if ( fName != 0 ) { acadoFPrintf( file,"%s( ", fName ); arguments.exportCode( file,_realString,_intString,BT_FALSE ); acadoFPrintf( file," );\n"); } else { acadoFPrintf( file,"%s( ", f.name ); arguments.exportCode( file,_realString,_intString,BT_FALSE ); acadoFPrintf( file," );\n"); } return SUCCESSFUL_RETURN; }
returnValue VectorspaceElement::printToFile( FILE* file, const char* const name, PrintScheme printScheme ) const { char* string = 0; printToString( &string, name,printScheme ); acadoFPrintf( file,"%s",string ); if ( string != 0 ) delete[] string; return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::C_print( FILE *file , const char *fcnName , const char *realString, int precision ) const{ acadoFPrintf(file,"/*\n"); acadoFPrintf(file," * This file was auto-generated by ACADO Toolkit.\n"); acadoFPrintf(file," *\n"); acadoFPrintf(file," * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.\n"); acadoFPrintf(file," * Copyright (C) 2008-2011 by Boris Houska and Hans Joachim Ferreau, K.U.Leuven.\n"); acadoFPrintf(file," * Developed within the Optimization in Engineering Center (OPTEC) under\n"); acadoFPrintf(file," * supervision of Moritz Diehl. All rights reserved.\n"); acadoFPrintf(file," *\n"); acadoFPrintf(file," */\n"); exportHeader ( file,fcnName,realString ); exportForwardDeclarations( file,fcnName,realString ); exportCode ( file,fcnName,realString,precision ); return SUCCESSFUL_RETURN; }
FILE* ExportAlgorithm::openFile( const String& dirName, const String& fileName ) const { String fullFileName( dirName ); fullFileName << "/" << fileName; FILE* file = acadoFOpen( fullFileName.getName(), "w" ); if ( file == 0 ) return 0; acadoPrintAutoGenerationNotice( file ); if ( commonHeaderName.isEmpty() == BT_FALSE ) acadoFPrintf( file, "#include \"%s\"\n\n\n",commonHeaderName.getName() ); return file; }
returnValue VectorspaceElement::printToFile( FILE* file, const char* const name, const char* const startString, const char* const endString, uint width, uint precision, const char* const colSeparator, const char* const rowSeparator ) const { char* string = 0; printToString( &string, name,startString,endString,width,precision,colSeparator,rowSeparator ); acadoFPrintf( file,"%s",string ); if ( string != 0 ) delete[] string; return SUCCESSFUL_RETURN; }
returnValue ExportArithmeticStatement::exportCodeAddSubtract( FILE* file, const char* const _sign, const char* _realString, const char* _intString, int _precision ) const { if ( ( rhs1 == 0 ) || ( rhs2 == 0 ) ) return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); if ( ( rhs1->getNumRows() != rhs2->getNumRows() ) || ( rhs1->getNumCols() != rhs2->getNumCols() ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); if ( lhs != 0 ) { if ( ( rhs1->getNumRows() != lhs->getNumRows() ) || ( rhs1->getNumCols() != lhs->getNumCols() ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); } for( uint i=0; i<getNumRows( ); ++i ) for( uint j=0; j<getNumCols( ); ++j ) { if ( lhs != 0 ) acadoFPrintf( file,"%s = ", lhs->get(i,j) ); if ( rhs1->isZero(i,j) == BT_FALSE ) { acadoFPrintf( file,"%s", rhs1->get(i,j) ); if ( rhs2->isZero(i,j) == BT_FALSE ) acadoFPrintf( file," %s %s;\n", _sign,rhs2->get(i,j) ); else acadoFPrintf( file,";\n" ); } else { if ( rhs2->isZero(i,j) == BT_FALSE ) acadoFPrintf( file,"%s %s;\n", _sign,rhs2->get(i,j) ); else acadoFPrintf( file,"0.0;\n" ); } } return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::exportCode( FILE *file, const char *fcnName, const char *realString, int precision, uint _numX, uint _numXA, uint _numU, uint _numP, uint _numDX ) const{ int run1; int nni = 0; for (run1 = 0; run1 < n; run1++) if (lhs_comp[run1] + 1 > nni) nni = lhs_comp[run1] + 1; unsigned numX = _numX > 0 ? _numX : getNX(); unsigned numXA = _numXA > 0 ? _numXA : getNXA(); unsigned numU = _numU > 0 ? _numU : getNU(); unsigned numP = _numP > 0 ? _numP : getNP(); unsigned numDX = _numDX > 0 ? _numDX : getNDX(); acadoFPrintf(file, "void %s(const %s* in, %s* out)\n{\n", fcnName, realString, realString); if( numX > 0 ){ acadoFPrintf(file,"const %s* xd = in;\n", realString ); } if( numXA > 0 ){ acadoFPrintf(file,"const %s* xa = in + %d;\n", realString,numX ); } if( getNU() > 0 ){ acadoFPrintf(file,"const %s* u = in + %d;\n", realString,numX+numXA ); } if( getNUI() > 0 ){ acadoFPrintf(file,"const %s* v = in + %d;\n", realString,numX+numXA+numU ); } if( numP > 0 ){ acadoFPrintf(file,"const %s* p = in + %d;\n", realString,numX+numXA+numU+getNUI() ); } if( getNPI() > 0 ){ acadoFPrintf(file,"const %s* q = in + %d;\n", realString,numX+numXA+numU+getNUI()+numP ); } if( getNW() > 0 ){ acadoFPrintf(file,"const %s* w = in + %d;\n", realString,numX+numXA+numU+getNUI()+numP+getNPI() ); } if( numDX > 0 ){ acadoFPrintf(file,"const %s* dx = in + %d;\n", realString,numX+numXA+numU+getNUI()+numP+getNPI()+getNW() ); } if( getNT() > 0 ){ acadoFPrintf(file,"const %s* t = in + %d;\n", realString,numX+numXA+numU+getNUI()+numP+getNPI()+getNW()+numDX ); } if (n > 0) { acadoFPrintf(file, "/* Vector of auxiliary variables; number of elements: %d. */\n", n); acadoFPrintf(file, "%s* a = %s;\n", realString, globalExportVariableName.getName() ); acadoFPrintf(file, "\n/* Compute intermediate quantities; */\n"); } Stream *auxVarIndividualNames = new Stream[nni]; for( run1 = 0; run1 < n; run1++ ) auxVarIndividualNames[lhs_comp[run1]] << "a" << "[" << run1 << "]"; // Export intermediate quantities for (run1 = 0; run1 < n; run1++) { // Convert the name for intermediate variables for subexpressions sub[run1]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames); acadoFPrintf(file, "%s[%d] = ", "a", run1); file << *sub[run1]; acadoFPrintf(file, ";\n"); } acadoFPrintf(file,"\n/* Compute outputs: */\n"); // Export output quantities for (run1 = 0; run1 < dim; run1++) { // Convert names for interm. quantities for output expressions f[run1]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames); acadoFPrintf(file, "out[%d] = ", run1); file << *f[run1]; acadoFPrintf(file, ";\n"); } acadoFPrintf(file,"}\n\n"); delete [] auxVarIndividualNames; return SUCCESSFUL_RETURN; }
returnValue ExportArithmeticStatement::exportCodeMultiply( FILE* file, BooleanType transposeRhs1, const char* _realString, const char* _intString, int _precision ) const { if ( ( lhs == 0 ) || ( rhs1 == 0 ) || ( rhs2 == 0 ) ) return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); char assignString[3]; switch ( op0 ) { case ESO_ASSIGN: strcpy( assignString,"=" ); break; case ESO_ADD_ASSIGN: strcpy( assignString,"+=" ); break; case ESO_SUBTRACT_ASSIGN: strcpy( assignString,"-=" ); break; default: return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); } uint nRowsRhs1; uint nColsRhs1; if ( transposeRhs1 == BT_FALSE ) { nRowsRhs1 = rhs1->getNumRows( ); nColsRhs1 = rhs1->getNumCols( ); } else { nRowsRhs1 = rhs1->getNumCols( ); nColsRhs1 = rhs1->getNumRows( ); } if ( ( nColsRhs1 != rhs2->getNumRows( ) ) || ( nRowsRhs1 != lhs->getNumRows( ) ) || ( rhs2->getNumCols( ) != lhs->getNumCols( ) ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); char sign[2] = "+"; if ( op2 != ESO_UNDEFINED ) { if ( ( rhs3->getNumRows( ) != lhs->getNumRows( ) ) || ( rhs3->getNumCols( ) != lhs->getNumCols( ) ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); if ( op2 == ESO_SUBTRACT ) sign[0] = '-'; } BooleanType allZero; // uint ii, kk; // uint iiRhs1, kkRhs1; ExportIndex ii( "run1" ), iiRhs1( "run1" ); ExportIndex kk( "run3" ), kkRhs1( "run3" ); uint iMax = getNumRows( ); if ( outerLoopVariable.isGiven( ) == BT_FALSE ) iMax = 1; for( uint i=0; i<iMax; ++i ) { if ( outerLoopVariable.isGiven( ) == BT_FALSE ) acadoFPrintf( file,"for ( %s=0; %s<%d; ++%s ){\n", ii.name,ii.name,getNumRows(),ii.name ); else ii == i; for( uint j=0; j<getNumCols( ); ++j ) { allZero = BT_TRUE; acadoFPrintf( file,"%s %s", lhs->get(ii,j),assignString ); for( uint k=0; k<nColsRhs1; ++k ) { kk == k; if ( transposeRhs1 == BT_FALSE ) { iiRhs1 = ii; kkRhs1 = kk; } else { iiRhs1 = kk; kkRhs1 = ii; } if ( ( rhs1->isZero(iiRhs1,kkRhs1) == BT_FALSE ) && ( rhs2->isZero(kk,j) == BT_FALSE ) ) { allZero = BT_FALSE; if ( rhs1->isOne(iiRhs1,kkRhs1) == BT_FALSE ) { acadoFPrintf( file," %s %s", sign,rhs1->get(iiRhs1,kkRhs1) ); if ( rhs2->isOne(kk,j) == BT_FALSE ) acadoFPrintf( file,"*%s", rhs2->get(kk,j) ); } else { if ( rhs2->isOne(kk,j) == BT_FALSE ) acadoFPrintf( file," %s %s", sign,rhs2->get(kk,j) ); else acadoFPrintf( file," %s 1.0", sign ); } } } if ( ( op2 == ESO_ADD ) || ( op2 == ESO_SUBTRACT ) ) acadoFPrintf( file," + %s;\n", rhs3->get(ii,j) ); if ( op2 == ESO_UNDEFINED ) { if ( allZero == BT_TRUE ) acadoFPrintf( file," 0.0;\n" ); else acadoFPrintf( file,";\n" ); } } } if ( outerLoopVariable.isGiven( ) == BT_FALSE ) acadoFPrintf( file,"}\n" ); return SUCCESSFUL_RETURN; }
returnValue String::print( FILE *file ) const { acadoFPrintf( file, name ); return SUCCESSFUL_RETURN; }
returnValue FunctionEvaluationTree::exportCode( FILE *file, const char *fcnName, const char *realString, int precision, uint _numX, uint _numXA, uint _numU ) const{ int run1; int nni = 0; for( run1 = 0; run1 < n; run1++ ) if( lhs_comp[run1]+1 > nni ) nni = lhs_comp[run1]+1; uint numX; if( _numX > 0 ) { numX = _numX; } else { numX = getNX(); } uint numXA; if( _numXA > 0 ) { numXA = _numXA; } else { numXA = getNXA(); } uint numU; if( _numU > 0 ) { numU = _numU; } else { numU = getNU(); } acadoFPrintf(file,"void %s( const %s* acado_x, %s* acado_f ){\n", fcnName,realString,realString ); if( numX > 0 ){ acadoFPrintf(file,"const %s* acado_xd = acado_x;\n", realString ); } if( numXA > 0 ){ acadoFPrintf(file,"const %s* acado_xa = acado_x + %d;\n", realString,numX ); } if( getNU() > 0 ){ acadoFPrintf(file,"const %s* acado_u = acado_x + %d;\n", realString,numX+numXA ); } if( getNUI() > 0 ){ acadoFPrintf(file,"const %s* acado_v = acado_x + %d;\n", realString,numX+numXA+numU ); } if( getNP() > 0 ){ acadoFPrintf(file,"const %s* acado_p = acado_x + %d;\n", realString,numX+numXA+numU+getNUI() ); } if( getNPI() > 0 ){ acadoFPrintf(file,"const %s* acado_q = acado_x + %d;\n", realString,numX+numXA+numU+getNUI()+getNP() ); } if( getNW() > 0 ){ acadoFPrintf(file,"const %s* acado_w = acado_x + %d;\n", realString,numX+numXA+numU+getNUI()+getNP()+getNPI() ); } if( getNDX() > 0 ){ acadoFPrintf(file,"const %s* acado_dx = acado_x + %d;\n", realString,numX+numXA+numU+getNUI()+getNP()+getNPI()+getNW() ); } acadoFPrintf(file,"\n"); acadoFPrintf(file,"/* COMPUTE INTERMEDIATE STATES: */\n"); acadoFPrintf(file,"/* ---------------------------- */\n"); // // This is a (not so quick) hack to have a flexible name for interm. // quantities, but helps. It general case it should be done with // ExportVariable -- to be able to set a working structure, too. So: // // TODO: setGlobalExportvariable should set the full name once.... // String auxVarName; if ( auxVariableStructName.isEmpty() ) { auxVarName = auxVariableName; } else { auxVarName = auxVariableStructName; auxVarName += "."; auxVarName += auxVariableName; } Stream *auxVarIndividualNames = new Stream[nni]; for( run1 = 0; run1 < n; run1++ ) auxVarIndividualNames[lhs_comp[run1]] << auxVarName << "[" << run1 << "]"; // Export intermediate quantities for( run1 = 0; run1 < n; run1++ ) { // Convert the name for intermediate variables for subexpressions sub[ run1 ]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames ); acadoFPrintf(file,"%s[%d] = ", auxVarName.getName(), run1 ); file << *sub[run1]; acadoFPrintf(file,";\n"); } acadoFPrintf(file,"\n"); acadoFPrintf(file,"/* COMPUTE OUTPUT: */\n"); acadoFPrintf(file,"/* --------------- */\n"); // Export output quantities for( run1 = 0; run1 < dim; run1++ ) { // Convert names for interm. quantities for output expressions f[ run1 ]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames ); acadoFPrintf(file,"acado_f[%d] = ", run1 ); file << *f[run1]; acadoFPrintf(file,";\n"); } acadoFPrintf(file,"}\n\n"); delete [] auxVarIndividualNames; return SUCCESSFUL_RETURN; }
returnValue LogRecord::print( LogPrintMode _mode ) const { uint stringLength = determineRecordStringLength( )+1; char* string = new char[stringLength]; for( uint i=0; i<stringLength; ++i ) string[i] = '\0'; char* valueString = 0; LogRecordItem* current = first; switch( _mode ) { case PRINT_ITEM_BY_ITEM: while ( current != 0 ) { if ( current->getValueString( &valueString ) != SUCCESSFUL_RETURN ) { delete[] valueString; delete[] string; return ACADOERROR( RET_UNKNOWN_BUG ); } if ( valueString != 0 ) { strcat( string,valueString ); delete[] valueString; valueString = 0; } current = current->getNext( ); } break; case PRINT_ITER_BY_ITER: for( uint i=0; i<getMaxNumMatrices( ); ++i ) { current = first; while ( current != 0 ) { if ( current->getValueString( &valueString,i ) != SUCCESSFUL_RETURN ) { delete[] valueString; delete[] string; return ACADOERROR( RET_UNKNOWN_BUG ); } if ( valueString != 0 ) { strcat( string,valueString ); delete[] valueString; valueString = 0; } current = current->getNext( ); } } break; case PRINT_LAST_ITER: while ( current != 0 ) { if ( current->getValueString( &valueString,getMaxNumMatrices( )-1 ) != SUCCESSFUL_RETURN ) { delete[] valueString; delete[] string; return ACADOERROR( RET_UNKNOWN_BUG ); } if ( valueString != 0 ) { strcat( string,valueString ); delete[] valueString; valueString = 0; } current = current->getNext( ); } strcat( string,"\n" ); break; } if ( outputFile != 0 ) { acadoFPrintf( outputFile,string ); } else { if ( outputFileName != 0 ) { FILE* outputFileFromName = fopen( outputFileName,"w" ); acadoFPrintf( outputFileFromName,string ); fclose( outputFileFromName ); } else { delete[] string; return ACADOERROR( RET_INVALID_ARGUMENTS ); } } delete[] string; return SUCCESSFUL_RETURN; }