c++ - Function call different behavior in main and library -
i met strange thing, 2 days ago try debug code. i'm running code on windows 7 64bit os. in main calculate mathematical model knowing input signal, applied in control algorithm soop
. calculation ok in main, in soop
function while doing same thing have close not identical results. why? tired float
, got same result.
does double
rounded if calculate in function not in main?
main:
#include <unistd.h> #include <iostream> #include <windows.h> #include <fstream> #include <math.h> #include "model.h" #include "soop.h" using namespace std; int main( void ) { ofstream myfile; myfile.open ("modeltest.txt"); statee current_state; // = new statee; initstatee(current_state); current_state.variables[0] = 0.0; current_state.variables[0] = -3.0*pi/2.0; cout << "program init" << endl; // ==============model testing myfile << current_state.variables[0] <<", "<< current_state.variables[2] <<", "<<current_state.reward << ", "<< fmod((current_state.variables[0])+ pi, 2.0 * pi) -pi <<endl; double utmp = -10.0; (int i=0;i<3;i++) { current_state.variables[0] = -3.0*pi/2.0; copystatee(current_state, nextstatee(current_state, utmp)); utmp = utmp+10.0; myfile << current_state.variables[0] <<", "<< current_state.variables[2] <<", "<<current_state.reward << ", "<< fmod((current_state.variables[0])+ pi, 2.0 * pi) -pi <<endl; } // ==============soop testing initstatee(current_state); current_state.variables[0] = -3.0*pi/2.0; double commnadsignal[initlen]; for(int i=0; i<1; i++) { memcpy(commnadsignal,soop(current_state),sizeof(double)*initlen); copystatee(current_state,nextstatee(current_state, commnadsignal[0])); } myfile.close(); return 0; }
the model library:
statee nextstatee(statee s, double votlagesignal) { std::cout<< "in nextstatee"<< std::endl; statee newstatee;// = (statee*)malloc(sizeof(statee)); memcpy(newstatee.variables, s.variables, sizeof(double) * nbhiddenstateevariables); newstatee.isterminal = s.isterminal; std::cout << " v "<< votlagesignal << " l "<< s.variables[0] << std::endl; double vm = votlagesignal; double lambda = s.variables[0]; double dlambda = s.variables[1]; double theta = s.variables[2]; double dtheta = s.variables[3]; double thetanext = theta + timestep*dtheta; double dthetanext = dtheta + timestep*( (1/(ap*cp-pow(bp,2)*pow(cos(lambda),2))) *(-bp*cp*sin(lambda)*pow(dlambda,2)+bp*dp*sin(lambda)*cos(lambda)-cp*ep*dtheta+cp*fp*vm) ); double lambdanext = lambda + timestep*dlambda; double dlambdanext = dlambda + timestep*( (1/(ap*cp-pow(bp,2)*pow(cos(lambda),2))) *(ap*dp*sin(lambda)-pow(bp,2)*sin(lambda)*cos(lambda)*pow(dlambda,2)-bp*ep*cos(lambda)*dtheta+bp*fp*cos(lambda)*vm) ); //========== normalization of lambda , theta if (lambdanext>0) lambdanext = fmod(lambdanext+pi, 2.0*pi)-pi; else lambdanext = fmod(lambdanext-pi, 2.0*pi)+pi; if (thetanext>0) thetanext = fmod(thetanext+pi, 2.0*pi)-pi; else thetanext = fmod(thetanext-pi, 2.0*pi)+pi; newstatee.variables[0] = lambdanext; newstatee.variables[1] = dlambdanext; newstatee.variables[2] = thetanext; newstatee.variables[3] = dthetanext; double newreward; if(s.isterminal) { newreward = 0.0; } else { newreward = 1 - (pow(lambdanext,2.0)*qreward + pow(votlagesignal,2.0)*rreward)/(pow(pi,2.0)*qreward+ pow(maxvoltage,2.0)*rreward); } newstatee.reward=newreward; return newstatee; } void copystatee(statee& destination, statee original) { initstatee(destination); destination.variables[0] = original.variables[0]; // lambda destination.variables[1] = original.variables[1]; // dlambda destination.variables[2] = original.variables[2]; // theta destination.variables[3] = original.variables[3]; // dtheta destination.isterminal = 0; destination.reward = original.reward; }
the controller library uses model library:
double * soop(statee& s0) { ofstream myfile; myfile.open ("sooptest.txt"); bool stopflag = false; unsigned int counter = 0; unsigned int currentbudget = 0; unsigned int listlenght = 0; unsigned int maxlengthsequence = 0; double tmpu = -10.0; (int i=0;i<3;i++) { double sptmp[initlen] = {0}; unsigned int stmp[initlen] = {0}; statee newstatee[initlen]; statee tmpe; sptmp[0] = ((double)i)/3.0; stmp[0] = 1; initstatee(tmpe); tmpe.variables[0]=-3.0*pi/2.0; copystatee(tmpe, nextstatee(tmpe , tmpu )); tmpu = tmpu + 10.0; newstatee[0] = nextstatee(s0, unnormu(middleofbox((double)i/3.0, stmp[0])) ); myfile << "u norm "<< middleofbox((double)i/3.0, stmp[0])<< " unorm "<<unnormu(middleofbox((double)i/3.0, stmp[0])) << endl; myfile <<"alp " << tmpe.variables[0] << " th " << tmpe.variables[2] << " r " << tmpe.reward << endl; myfile <<"alp " << newstatee[0].variables[0] << " th " << newstatee[0].variables[2] << " r " << newstatee[0] .reward << endl; myfile << endl; } myfile.close(); }
the 2 text files modeltest.txt
, soop test.txt
should show same results, cuz applay same input parameters (s0 , [-10,0,10]) in main , in soop function. different rusults in text files. soop test
file:
u norm 0.166667 unorm -10 alp 1.5708 th 0 r 0.75 alp 1.5708 th 0 r 0.75 u norm 0.5 unorm 0 alp 1.5708 th 0 r 0.75 alp 1.5708 th 0 r 0.75 u norm 0.833333 unorm 10 alp 1.5708 th 0 r 0.75 alp 1.5708 th 0 r 0.75
modeltest
file:
alp 1.5708, th 0, r 0.75 (for s0 , -10) alp 1.57084, th -0.000115852, r 0.749986 (for s0 , 0) alp 1.57088, th -0.000230942, r 0.749972 (for s0 , 10)
my mistake, in main
didn't initialized states of current_state
(only 1 angle). initstatee(current_state)
missing in main
loop.
next mistake, reward factor voltagesignal
, 'rreward = 0' reward wont change in function of control value.
the biggest mistake suppose systems angular state parameter change after first simulation. i'm using euler discrezitation f(q(t),u(t)) - 2nd order differential equation, control signal has effect on first derivative in first iteration , on position has effect after 2nd iteration.
p_{k+1} = p_k + daltat * q_k q_{k+1} = q_k + deltat * f(q_k, u_k)
where deltat sampling time.
Comments
Post a Comment