⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 funchart.cpp

📁 这是一个用遗传算法来求解目标函数优化问题的源代码
💻 CPP
字号:
#include "StdAfx.h"
#include ".\funchart.h"

CFunChart::CFunChart(CWnd *pWnd)
{

	m_pWnd = pWnd;

	m_Xstart = -1;
	m_Xend = 2;

	m_PtCnt = 5000;

	m_Pt = new POINT[m_PtCnt];

	double y = 0.0;
	double step = (m_Xend - m_Xstart)/(double) (m_PtCnt-1);
	
	double x = m_Xstart;

	GetMinMax();

	GetPts();

}


CFunChart::~CFunChart(void)
{

	delete []m_Pt;
	m_Pt = NULL;
}


double CFunChart::Function(double x)
{
	return x * sin(x * 10 * atan(1.0) * 4) + 2.0;
}

double CFunChart::GetMinMax()
{

	double step = (m_Xend - m_Xstart)/(double) (m_PtCnt-1);
	
	double x = m_Xstart;
	double y =  Function(x);

	m_yMin = 9999999;
	m_yMax = -99999999;

	for (int i =0; i<m_PtCnt-1; i++)
	{
		y = Function(x);

		if (y> m_yMax)
		{
			m_yMax = y;
		}
		
		if (y < m_yMin)
		{
			m_yMin = y;
		}

		x = x + step;
	}
	
	y = Function(m_Xend);
	if (y> m_yMax)
	{
		m_yMax = y;
	}
		
	if (y < m_yMin)
	{
		m_yMin = y;
	}

	return 0;
}

double CFunChart::GetPts()
{
	double step = (m_Xend - m_Xstart)/(double) (m_PtCnt-1);
	double x = m_Xstart;
	double y = 0.0;

	for (int i =0; i<m_PtCnt-1; i++)
	{
		y = Function(x);
		m_Pt[i].x = GetChangX(x);
		m_Pt[i].y = GetChangY(y);

		x = x + step;
	}

	y = Function(m_Xend);
	m_Pt[i].x = GetChangX(m_Xend);
	m_Pt[i].y = GetChangY(y);

	return 1;
}

double CFunChart::GetChangX(double X)
{
	CRect rc;
	m_pWnd->GetClientRect(rc);

	double XS = m_Xstart;
	if (XS > 0)
	{
		XS = 0;
	}

	double  K = 0 - XS;
	double N = (rc.Width()-50)/(m_Xend-XS);
	double Vale = (K+X)*N+25;

	return Vale;	
}

double CFunChart::GetChangY(double Y)
{
	CRect rc;
	m_pWnd->GetClientRect(rc);
	
	double YS = m_yMin;
	if (YS > 0)
	{
		YS = 0;
	}

	double  K = 0 - YS;
	double N = (rc.Height()-50)/(m_yMax-YS);
	double Vale = (K+Y)*N+25;

	return rc.bottom - Vale;
}


int CFunChart::LineX(CDC *pDC)
{

	CString str;
	double N = 10;
	double XS = m_Xstart; 

	if (XS > 0)
	{
		XS = 0;
	}

	double step = (m_Xend - XS)/(double) (N-1);

	double x = m_Xstart;
	double y = 0.0;

	x = GetChangX(x);
	y = GetChangY(y);
	pDC->MoveTo(x, y);

	x= m_Xend;
	x = GetChangX(x);
	pDC->LineTo(x, y);

	x = XS;
	double x1 =0.0;
	for (int i=0; i<N-1; i++)
	{
		str.Format("%.1f", x);
		x1 = GetChangX(x);
		pDC->TextOut(x1, y+5, str);
		pDC->MoveTo(x1, y);
		pDC->LineTo(x1, y+2);

		x += step;
	}

	x1 = GetChangX(m_Xend);
	str.Format("%.1f", m_Xend);
	pDC->TextOut(x1, y+5, str);
	pDC->MoveTo(x1, y);
	pDC->LineTo(x1, y+2);

	return 0;
}

int CFunChart::LineY(CDC *pDC)
{
	double N = 10;
	double YS = m_yMin;
	CString str;

	if (YS > 0)
	{
		YS = 0;
	}

	double step = (m_yMax - YS)/(double) (N-1);
	double x = 0.0;
	double y = YS;

	x = GetChangX(x);
	y = GetChangY(y);
	pDC->MoveTo(x, y);

	y = m_yMax;
	y = GetChangY(y);
	pDC->LineTo(x, y);

	y = YS;
	double y1 = 0.0;
	for (int i=0; i<N-1; i++)
	{

		str.Format("%.1f", y);
		double y1 = GetChangY(y);
		pDC->TextOut(x+5, y1+5, str);
		pDC->MoveTo(x, y1+5);
		pDC->LineTo(x+2, y1+5);
		y += step;
	}

	y1 = GetChangY(m_yMax);
	str.Format("%.1f",  m_yMax);
	pDC->TextOut(x+5, y1+5, str);
	pDC->MoveTo(x, y1+5);
	pDC->LineTo(x+2, y1+5);

	return 0;

}


int CFunChart::PanitFunChart(CDC *pDC)
{
	CString str;

	LineX(pDC);
	LineY(pDC);

	str.Format("%s", "y= x * sin(x * 10 * atan(1.0) * 4) + 2.0;");
	pDC->TextOut(10, 5, str);

	pDC->Polyline(m_Pt, 5000);

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -