Thread: buffer memories flood? HELP

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    buffer memories flood? HELP

    Hi all,

    I am doing my program for my thesis. The target is to find the value of R, S which lead to minimum AC function. When i code and run the program it stuck. By checking the debug function of C++, i see that when the value of AC have over fifteen digits after comma it will get stuck. I guess that is one of 02 below reasons:

    1. The power function inside AC get overload when value of R, S increase too big.
    2. The double type can not obtain the big value.

    But i still do not know how to solve this problem, please help me on this.

    Below is my program in Visual C++, please check for me.

    The program include 02 part: the first part is calculate value of AC, the second part is to use Hook and Jeeve method to find the value of R, S which give AC the minimum value.

    Code:
    void CHJDlg::OnAc()
    {
    // TODO: Add your control notification handler code here
    UpdateData(TRUE); // update data from control to present variance.
    // vice versa update the present variance to control
    m_R=m_LD*m_T2;
    m_S=m_LD*m_T;
    m_AC = AC();
    UpdateData(FALSE);
    
    }
    
    // calculate the value of OC
    double CHJDlg::OC()
    {
    int i=int(m_S-m_R-1);
    double r,Lamda_T;
    Lamda_T = m_LD * m_T;
    
    r=TotalFormula(0,i,Lamda_T,exp(-Lamda_T));
    m_OC = double(1)/m_T*((m_K1-m_K2)*r-m_K1/exp(Lamda_T)+m_K2);
    UpdateData(FALSE);
    return m_OC;
    }
    
    //build the totalfunction
    double CHJDlg::TotalFormula(int m_From, int m_To, double x, double y)
    {
    double ResultTF,luythua,giaithua;
    ResultTF=0;
    for (int i=m_From;i<=m_To;i++){
    //luythua = pow(x,i);
    luythua = exp(i * log(x));
    giaithua = Factorial(i);
    ResultTF=ResultTF+luythua/giaithua*y;
    }
    return ResultTF;
    }
    
    //build the factorial function.
    double CHJDlg::Factorial(int x)
    {
    double Result;
    Result=1;
    for (int i=2;i<=x;i++)
    Result=Result*i;
    return Result;
    }
    
    //calculate the value of BO1
    double CHJDlg::BO1()
    {
    double Lamda_T, e_Lamda_T, total1, total2;
    
    Lamda_T = m_LD * m_T;
    e_Lamda_T = exp(-Lamda_T);
    
    total1 = TotalFormula(0,m_S-1,Lamda_T,1);
    total2 = TotalFormula(0,m_S,Lamda_T,1);
    
    m_BO1 = Lamda_T - (Lamda_T * e_Lamda_T * total1) - m_S + (m_S * e_Lamda_T * total2);
    UpdateData(FALSE);
    return m_BO1;
    }
    
    //calculate the value of BO2
    double CHJDlg::BO2(int From, int To, double m_lamda_t)
    {
    double lamda_t1, lamda_t, e_lamda_t1, e_lamda_t, Result, total1, total2;
    Result = 0;
    total1 = 0;
    total2 = 0;
    lamda_t1 = m_lamda_t;
    e_lamda_t1 = exp(-lamda_t1);
    
    lamda_t = m_LD * m_T;
    e_lamda_t = exp(-lamda_t);
    
    for(int j=From;j<=To;j++)
    {
    total1 = TotalFormula(0, j-2, lamda_t1, double(1));
    total2 = TotalFormula(0, j-1, lamda_t1, double(1));
    Result = Result + (lamda_t1*(double(1)-e_lamda_t1*total1) - j*(double(1)-e_lamda_t1*total2))*pow(lamda_t,m_S-j)/Factorial(m_S-j)*e_lamda_t;
    }
    return Result;
    }
    
    //calculate the value of BO2n
    double CHJDlg::BO2n()
    {
    m_BO2N = BO2(m_R+1, m_S-1, m_LD*m_T1);
    UpdateData(FALSE);
    return m_BO2N;
    }
    
    //calculate the value of BO2e1
    double CHJDlg::BO2e1()
    {
    double lamda_t, lamda_t2, total1, total2;
    double Result;
    Result = 0;
    
    lamda_t = m_LD* m_T;
    lamda_t2 = m_LD*m_T2;
    
    total1 = TotalFormula(0, m_S-1, lamda_t, exp(-lamda_t));
    total2 = TotalFormula(0, m_S-2, lamda_t, exp(-lamda_t));
    m_BO2E1 = lamda_t2 + lamda_t - m_S + (m_S - lamda_t2)*total1 - lamda_t*total2;
    UpdateData(FALSE);
    return m_BO2E1;
    }
    
    //calculate the value of BO2e2
    double CHJDlg::BO2e2()
    {
    m_BO2E2 = BO2(1,m_R,m_LD* m_T2);
    UpdateData(FALSE);
    return m_BO2E2;
    }
    
    //take sum of BO2e1 and BO2e2 to get BO2e
    double CHJDlg::BO2e()
    {
    m_BO2E = BO2e1() + BO2e2();
    UpdateData(FALSE);
    return m_BO2E;
    }
    
    //calculate value of BO
    double CHJDlg::BO()
    {
    m_BO = double(m_P)/m_T * (BO1() + BO2n() + BO2e());
    UpdateData(FALSE);
    return m_BO;
    }
    
    //calculate value of AC
    double CHJDlg::AC()
    {
    return OC() + BO();
    }
    
    
    //////////////////////STAR USING HOOK AND JEEVE METHOD TO FIND THE VALUE OF R AND S WHICH GIVE THE MINIMUM VALUE OF AC.///
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void CHJDlg::OnHj()
    {
    // TODO: Add your control notification handler code here
    UpdateData(TRUE); // update date from control to present variance
    
    // define the array value for Xo, X1....
    
    int Xo[2],X10[2],X11[2],X12[2];
    
    CString str;
    m_R=m_LD*m_T2;
    m_S=m_LD*m_T;
    
    // set the start value for Xo: Xo{x1,x2}={R,S}
    Xo[0] = m_R;
    Xo[1] = m_S;
    
    // calculate AC
    m_AC = AC();
    
    // the program get stuck, then debug from this line to find the problem
    //The problem still not solved.
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    m_View = str;
    UpdateData(false);
    
    // set X10=Xo
    X10[0] = Xo[0];
    X10[1] = Xo[1];
    
    bool flag=true;
    
    while (flag)
    {
    // step 1: increase/decrease x1 by deltaX.
    //first: to increase x1
    m_R = X10[0] + m_deltaXi;
    m_S = X10[1];
    // calculate AC+ then add to ac_temp
    ac_temp = AC();
    
    if (ac_temp<m_AC) // if AC+ less than AC then change the value of X11
    {
    X11[0] = X10[0] + m_deltaXi;
    X11[1] = X10[1];
    
    // keep the new value AC+, update value m_AC by AC+ for using of next step (step 2).
    m_AC =ac_temp;
    str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    m_View = m_View + str;
    UpdateData(false);
    // still do not save the value of m_R, m_S, m_AC and deltaX into file.
    }
    
    else// if increase x1 do not give lower value of AC then decrease x1.
    {
    //second: decrease x1
    m_R = X10[0] - m_deltaXi;
    // reculculate AC-
    ac_temp = AC();
    
    if (ac_temp<m_AC)
    {// if AC- < AC => take the new value for X11
    X11[0] = X10[0] - m_deltaXi;
    X11[1] = X10[1];
    
    // keep the new value AC-, update value m_AC by AC- for using of next step(step 2).
    m_AC =ac_temp;
    
    // still do not save the value of m_R, m_S, m_AC and deltaX into file.
    
    str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    m_View = m_View + str;
    UpdateData(false);
    }
    else// if increase/decrease x1 do not give lower value of AC then take X11=X10
    {
    X11[0] = X10[0];
    X11[1] = X10[1];
    }
    }
    
    // step 2: increase/decrease x2 by deltaX.
    //first: increase x2
    m_R = X11[0];
    m_S = X11[1] + m_deltaXi;
    
    // calculate AC+
    ac_temp = AC();
    
    if (ac_temp<m_AC)// if AC+ < AC then X12 = X11 + delta
    {
    X12[0] = X11[0];
    X12[1] = X11[1] + m_deltaXi;
    
    // update value of m_AC by AC+ for the comparing of next step.
    
    m_AC = ac_temp;
    
    // still do not save the value of m_R, m_S, m_AC and deltaX into file.
    
    str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    m_View = m_View + str;
    UpdateData(false);
    }
    else // if increase x1 do not give lower value of AC then decrease x1.
    {
    //decrease x2
    m_S = X11[1] - m_deltaXi;
    ac_temp = AC();
    
    if (ac_temp<m_AC)
    {
    X12[0] = X11[0];
    X12[1] = X11[1] - m_deltaXi;
    // update value of AC by AC- for the next comparing.
    m_AC = ac_temp;
    
    // still do not save value of m_R, m_S, m_AC and deltaX into file
    
    str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    m_View = m_View + str;
    UpdateData(false);
    }
    else// if increase/decrease x2 do not give lower value of AC then take X12=X11
    {
    X12[0] = X11[0];
    X12[1] = X11[1];
    }
    }
    
    //******************************
    // check the value of X12 and X10, they are equal or not.
    
    if (X12[0]==X10[0] && X12[1]==X10[1])// if X12=X10 then check deltaX < epsilon ?
    {
    if (m_deltaXi<=m_epsilon)// if deltaX < epsilon, then stop.
    {
    flag=false;//add the flag = false to stop the bool.
    break; // exit the bool
    }
    else // if deltaX > epsilon, then decrease deltaX.
    {
    m_deltaXi = m_deltaXi/2;
    }
    }
    
    else // if X12# X10
    {
    // then find the new value of X10 by X10 = X12 - (X12 - X10).
    X10[0] = 2*X12[0] - Xo[0];
    X10[1] = 2*X12[1] - Xo[1];
    
    //update the value of R, S, AC.
    m_R = X10[0];
    m_S = X10[1];
    m_AC = AC();
    //update the value of Xo.
    Xo[0] = X10[0];
    Xo[1] = X10[1];
    }
    }// end while
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you want anyone to take a look at that, please indent the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Not really that hard to indent code TDBNDVVDSC
    Code:
    void CHJDlg::OnAc()
    {
    	// TODO: Add your control notification handler code here
    	UpdateData(TRUE); // update data from control to present variance.
    	// vice versa update the present variance to control
    	m_R=m_LD*m_T2;
    	m_S=m_LD*m_T;
    	m_AC = AC();
    	UpdateData(FALSE);
    
    }
    
    // calculate the value of OC
    double CHJDlg::OC()
    {
    	int i=int(m_S-m_R-1);
    	double r,Lamda_T;
    	Lamda_T = m_LD * m_T;
    
    	r=TotalFormula(0,i,Lamda_T,exp(-Lamda_T));
    	m_OC = double(1)/m_T*((m_K1-m_K2)*r-m_K1/exp(Lamda_T)+m_K2);
    	UpdateData(FALSE);
    	return m_OC;
    }
    
    //build the totalfunction
    double CHJDlg::TotalFormula(int m_From, int m_To, double x, double y)
    {
    	double ResultTF,luythua,giaithua;
    	ResultTF=0;
    	for (int i=m_From;i<=m_To;i++){
    		//luythua = pow(x,i);
    		luythua = exp(i * log(x));
    		giaithua = Factorial(i);
    		ResultTF=ResultTF+luythua/giaithua*y;
    	}
    	return ResultTF;
    }
    
    //build the factorial function.
    double CHJDlg::Factorial(int x)
    {
    	double Result;
    	Result=1;
    	for (int i=2;i<=x;i++)
    		Result=Result*i;
    	return Result;
    }
    
    //calculate the value of BO1
    double CHJDlg::BO1()
    {
    	double Lamda_T, e_Lamda_T, total1, total2;
    
    	Lamda_T = m_LD * m_T;
    	e_Lamda_T = exp(-Lamda_T);
    
    	total1 = TotalFormula(0,m_S-1,Lamda_T,1);
    	total2 = TotalFormula(0,m_S,Lamda_T,1);
    
    	m_BO1 = Lamda_T - (Lamda_T * e_Lamda_T * total1) - m_S + (m_S * e_Lamda_T * total2);
    	UpdateData(FALSE);
    	return m_BO1;
    }
    
    //calculate the value of BO2
    double CHJDlg::BO2(int From, int To, double m_lamda_t)
    {
    	double lamda_t1, lamda_t, e_lamda_t1, e_lamda_t, Result, total1, total2;
    	Result = 0;
    	total1 = 0;
    	total2 = 0;
    	lamda_t1 = m_lamda_t;
    	e_lamda_t1 = exp(-lamda_t1);
    
    	lamda_t = m_LD * m_T;
    	e_lamda_t = exp(-lamda_t);
    
    	for(int j=From;j<=To;j++)
    	{
    		total1 = TotalFormula(0, j-2, lamda_t1, double(1));
    		total2 = TotalFormula(0, j-1, lamda_t1, double(1));
    		Result = Result + (lamda_t1*(double(1)-e_lamda_t1*total1) - j*(double(1)-e_lamda_t1*total2))*pow(lamda_t,m_S-j)/Factorial(m_S-j)*e_lamda_t;
    	}
    	return Result;
    }
    
    //calculate the value of BO2n
    double CHJDlg::BO2n()
    {
    	m_BO2N = BO2(m_R+1, m_S-1, m_LD*m_T1);
    	UpdateData(FALSE);
    	return m_BO2N;
    }
    
    //calculate the value of BO2e1
    double CHJDlg::BO2e1()
    {
    	double lamda_t, lamda_t2, total1, total2;
    	double Result;
    	Result = 0;
    
    	lamda_t = m_LD* m_T;
    	lamda_t2 = m_LD*m_T2;
    
    	total1 = TotalFormula(0, m_S-1, lamda_t, exp(-lamda_t));
    	total2 = TotalFormula(0, m_S-2, lamda_t, exp(-lamda_t));
    	m_BO2E1 = lamda_t2 + lamda_t - m_S + (m_S - lamda_t2)*total1 - lamda_t*total2;
    	UpdateData(FALSE);
    	return m_BO2E1;
    }
    
    //calculate the value of BO2e2
    double CHJDlg::BO2e2()
    {
    	m_BO2E2 = BO2(1,m_R,m_LD* m_T2);
    	UpdateData(FALSE);
    	return m_BO2E2;
    }
    
    //take sum of BO2e1 and BO2e2 to get BO2e
    double CHJDlg::BO2e()
    {
    	m_BO2E = BO2e1() + BO2e2();
    	UpdateData(FALSE);
    	return m_BO2E;
    }
    
    //calculate value of BO
    double CHJDlg::BO()
    {
    	m_BO = double(m_P)/m_T * (BO1() + BO2n() + BO2e());
    	UpdateData(FALSE);
    	return m_BO;
    }
    
    //calculate value of AC
    double CHJDlg::AC()
    {
    	return OC() + BO();
    }
    
    
    //////////////////////STAR USING HOOK AND JEEVE METHOD TO FIND THE VALUE OF R AND S WHICH GIVE THE MINIMUM VALUE OF AC.///
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void CHJDlg::OnHj()
    {
    	// TODO: Add your control notification handler code here
    	UpdateData(TRUE); // update date from control to present variance
    
    	// define the array value for Xo, X1....
    
    	int Xo[2],X10[2],X11[2],X12[2];
    
    	CString str;
    	m_R=m_LD*m_T2;
    	m_S=m_LD*m_T;
    
    	// set the start value for Xo: Xo{x1,x2}={R,S}
    	Xo[0] = m_R;
    	Xo[1] = m_S;
    
    	// calculate AC
    	m_AC = AC();
    
    	// the program get stuck, then debug from this line to find the problem
    	//The problem still not solved.
    	////////////////////////////////////////////////////////////////////////////////////////////////////////
    	str.Format("&#37;d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    	m_View = str;
    	UpdateData(false);
    
    	// set X10=Xo
    	X10[0] = Xo[0];
    	X10[1] = Xo[1];
    
    	bool flag=true;
    
    	while (flag)
    	{
    		// step 1: increase/decrease x1 by deltaX.
    		//first: to increase x1
    		m_R = X10[0] + m_deltaXi;
    		m_S = X10[1];
    		// calculate AC+ then add to ac_temp
    		ac_temp = AC();
    
    		if (ac_temp<m_AC) // if AC+ less than AC then change the value of X11
    		{
    			X11[0] = X10[0] + m_deltaXi;
    			X11[1] = X10[1];
    
    			// keep the new value AC+, update value m_AC by AC+ for using of next step (step 2).
    			m_AC =ac_temp;
    			str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    			m_View = m_View + str;
    			UpdateData(false);
    			// still do not save the value of m_R, m_S, m_AC and deltaX into file.
    		}
    
    		else// if increase x1 do not give lower value of AC then decrease x1.
    		{
    			//second: decrease x1
    			m_R = X10[0] - m_deltaXi;
    			// reculculate AC-
    			ac_temp = AC();
    
    			if (ac_temp<m_AC)
    			{// if AC- < AC => take the new value for X11
    				X11[0] = X10[0] - m_deltaXi;
    				X11[1] = X10[1];
    
    				// keep the new value AC-, update value m_AC by AC- for using of next step(step 2).
    				m_AC =ac_temp;
    
    				// still do not save the value of m_R, m_S, m_AC and deltaX into file.
    
    				str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    				m_View = m_View + str;
    				UpdateData(false);
    			}
    			else// if increase/decrease x1 do not give lower value of AC then take X11=X10
    			{
    				X11[0] = X10[0];
    				X11[1] = X10[1];
    			}
    		}
    
    		// step 2: increase/decrease x2 by deltaX.
    		//first: increase x2
    		m_R = X11[0];
    		m_S = X11[1] + m_deltaXi;
    
    		// calculate AC+
    		ac_temp = AC();
    
    		if (ac_temp<m_AC)// if AC+ < AC then X12 = X11 + delta
    		{
    			X12[0] = X11[0];
    			X12[1] = X11[1] + m_deltaXi;
    
    			// update value of m_AC by AC+ for the comparing of next step.
    
    			m_AC = ac_temp;
    
    			// still do not save the value of m_R, m_S, m_AC and deltaX into file.
    
    			str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    			m_View = m_View + str;
    			UpdateData(false);
    		}
    		else // if increase x1 do not give lower value of AC then decrease x1.
    		{
    			//decrease x2
    			m_S = X11[1] - m_deltaXi;
    			ac_temp = AC();
    
    			if (ac_temp<m_AC)
    			{
    				X12[0] = X11[0];
    				X12[1] = X11[1] - m_deltaXi;
    				// update value of AC by AC- for the next comparing.
    				m_AC = ac_temp;
    
    				// still do not save value of m_R, m_S, m_AC and deltaX into file
    
    				str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m_R,m_S,m_AC);
    				m_View = m_View + str;
    				UpdateData(false);
    			}
    			else// if increase/decrease x2 do not give lower value of AC then take X12=X11
    			{
    				X12[0] = X11[0];
    				X12[1] = X11[1];
    			}
    		}
    
    		//******************************
    		// check the value of X12 and X10, they are equal or not.
    
    		if (X12[0]==X10[0] && X12[1]==X10[1])// if X12=X10 then check deltaX < epsilon ?
    		{
    			if (m_deltaXi<=m_epsilon)// if deltaX < epsilon, then stop.
    			{
    				flag=false;//add the flag = false to stop the bool.
    				break; // exit the bool
    			}
    			else // if deltaX > epsilon, then decrease deltaX.
    			{
    				m_deltaXi = m_deltaXi/2;
    			}
    		}
    
    		else // if X12# X10
    		{
    			// then find the new value of X10 by X10 = X12 - (X12 - X10).
    			X10[0] = 2*X12[0] - Xo[0];
    			X10[1] = 2*X12[1] - Xo[1];
    
    			//update the value of R, S, AC.
    			m_R = X10[0];
    			m_S = X10[1];
    			m_AC = AC();
    			//update the value of Xo.
    			Xo[0] = X10[0];
    			Xo[1] = X10[1];
    		}
    	}// end while
    }

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    After check the debug, i had changed the end of program like:

    Code:
    //******************************
    		// check the value of X12 and X10, they are equal or not.
    		
    		if (X12[0]==X10[0] && X12[1]==X10[1])// if X12=X10 then check deltaX < epsilon ?
    		{
    			if (m_deltaXi<=m_epsilon)// if deltaX < epsilon, then stop.
    			{
    				flag=false;//add the flag = false to stop the bool.
    				break; // exit the bool
    			}
    			else // if deltaX > epsilon, then decrease deltaX.
    			{
    				m_deltaXi = m_deltaXi/2;
    			}
    		}
    		
    		else // X12# X10
    		{
    			// then find the new value of X10 by X10 = X12 - (X12 - X10).
    			X10[0] = 2*X12[0] - Xo[0];
    			X10[1] = 2*X12[1] - Xo[1];
    			
    			//update the value of R, S, AC.
    			m_R = X10[0];
    			m_S = X10[1];
    			m_AC = AC();
    						
    			if (ac_temp - m_AC < 0.0000000001)
    			{
    				if (m_deltaXi<=m_epsilon)// if deltaX < epsilon, then stop.
    				{
    					flag=false;//add the flag = false to stop the bool.
    					break; // exit the bool
    				}
    				else // if deltaX > epsilon, then decrease deltaX.
    				{
    					m_deltaXi = m_deltaXi/2;
    				}
    			}
    
    			else//update the value of Xo.
    			{
    				Xo[0] = X10[0];
    				Xo[1] = X10[1];
    			}
    		}
    	}// end while
    }
    But it still stuck. Please help me

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    I guess the problem is calculating the value of AC: OC + BO, when R, S go too big, it make the pow() overload. But i still do not know how to solve it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM