📄 ga_tycpdlg.cpp
字号:
// GA_tycpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GA_tycp.h"
#include "GA_tycpDlg.h"
#include "math.h"
#include "myexit.h"
#include "mypassword.h"
#define PERIOD 1//使用期1天
#define PI1 3.1415926
#define PI2 6.2831852
#define PI4 12.5663704
#define PI6 18.8495556
#define PI8 25.1327408
#define HMWS 7//单个号码的位数
#define MINJLS 6//最少记录数
#define MAXJLS 30//最少记录数
#define MAXLP 10000//轮盘最大值
#define YHCSGS 100//保存的优化参数的最多个数
#define QTGM 600//群体规模,偶数
#define OMIGA 300//正弦曲线中x的系数欧米伽的个数
#define DGCSSM 2//单个参数数目
#define QBB() (bb=PI8/(1+omiga))//PI#
#define QCC(g) (cc=(1.+omiga)*qt[g].qx[omiga].c[1]/1024)//768=256*#/2
int lsjls;//历史记录数
int cshms,zhhms;//产生号码数,组合号码数
int sjsjg;//随机数间隔
int ckjl;
class jjll
{
public:
char w[HMWS];
};
class jjll *lsjl;//历史纪录
////////////////////////////////////////////////
class dt
{
public:
unsigned char c[DGCSSM];
};
class gt//码空间一个个体
{
public:
class dt qx[OMIGA];
unsigned char ssxx;//将曲线总和缩小,
unsigned char ttgg;
};
int gtlll;
class gt dggt;//单个个体
class gt qt[QTGM];//码空间群体
class gt old_qt[QTGM];//码空间群体
unsigned int i_lpd[QTGM];//轮盘赌
unsigned long l_lpd[QTGM];//轮盘赌
unsigned long syz[QTGM];//适应值
double sx,tg;//缩小,提高
double min,max;
double *qx;
int omiga;
char flag;//类型
char *lrhm,*shm,*zhhm;//冷热号码,同步号码,组合号码
int num,start;//参考号码数,开始位置
int qtgm;//群体规模
char wz;//单个号码中的各位
char dg;
////////////////////
int yhnd;//演化年代
int fz_num;//复制数目
int jbjc_num;//基本交叉
int ysjc_num;//原生交叉
double bygl;//变异概率
int mincks ;//最少参考记录数
int yhjls;//优化记录数
int yh_start;//使用的优化记录的开始位置
unsigned char mb[8]={1,2,4,8,16,32,64,128};//位运算时用到
class myexit exitbox;
char right;//使用权
class mypassword passwordbox;
CString week[7]={"日","一","二","三","四","五","六"};
//SYSTEMTIME dtime;
CFile yhjl_fp;//优化记录文件
class rgb{
public:
unsigned char c[3];
};
rgb *image;
int width,height;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGA_tycpDlg dialog
CGA_tycpDlg::CGA_tycpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGA_tycpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGA_tycpDlg)
m_lsjl = _T("");
m_jls = _T("");
m_ychm = _T("");
m_qtgms = _T("");
m_gtzs = _T("");
m_jdyq = FALSE;
m_jjqq = FALSE;
m_zhhm = _T("");
m_lrhm = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGA_tycpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGA_tycpDlg)
DDX_Control(pDX, IDC_SSJSJG, m_sjsjg);
DDX_Control(pDX, IDC_SZHHMS, m_zhhms);
DDX_Control(pDX, IDC_SCKJL, m_ckjl);
DDX_Control(pDX, IDC_PIC, m_pic2);
DDX_Control(pDX, IDC_TIME, m_time);
DDX_Control(pDX, IDC_PPP, m_pic);
DDX_Control(pDX, IDC_PROGRESS2, m_prg1);
DDX_Control(pDX, IDC_PROGRESS1, m_prg0);
DDX_Control(pDX, IDC_SYSCS, m_yscs);
DDX_Control(pDX, IDC_SHMS, m_hms);
DDX_Text(pDX, IDC_LSJL, m_lsjl);
DDX_Text(pDX, IDC_JLS, m_jls);
DDX_Text(pDX, IDC_YCHM, m_ychm);
DDX_Text(pDX, IDC_QTGMS, m_qtgms);
DDX_Text(pDX, IDC_GTZS, m_gtzs);
DDX_Check(pDX, IDC_CHECK1, m_jdyq);
DDX_Check(pDX, IDC_CHECK2, m_jjqq);
DDX_Text(pDX, IDC_YCHM2, m_zhhm);
DDX_Text(pDX, IDC_LRHM, m_lrhm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGA_tycpDlg, CDialog)
//{{AFX_MSG_MAP(CGA_tycpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LOAD, OnLoad)
ON_BN_CLICKED(IDC_PRODUCE, OnProduce)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_WM_TIMER()
ON_NOTIFY(UDN_DELTAPOS, IDC_SCKJL, OnDeltaposSckjl)
ON_BN_CLICKED(IDC_PRODUCE2, OnProduce2)
ON_BN_CLICKED(IDC_PRODUCE3, OnProduce3)
ON_BN_CLICKED(IDC_PRODUCE4, OnProduce4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGA_tycpDlg message handlers
BOOL CGA_tycpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_hms.SetBuddy(GetDlgItem(IDC_EHMS));
//m_hms.SetRange(1,1000);
m_hms.SetPos(5);
m_yscs.SetBuddy(GetDlgItem(IDC_EYSCS));
m_yscs.SetRange(1,500);
m_yscs.SetPos(100);
m_ckjl.SetBuddy(GetDlgItem(IDC_ECKJL));
m_ckjl.SetRange(MINJLS,MAXJLS);
m_ckjl.SetPos(MINJLS);
m_zhhms.SetBuddy(GetDlgItem(IDC_EZHHMS));
m_zhhms.SetRange(1,100);
m_zhhms.SetPos(5);
m_sjsjg.SetBuddy(GetDlgItem(IDC_ESJSJG));
m_sjsjg.SetRange(0,999);
m_sjsjg.SetPos(100);
m_prg0.SetRange(0,1000);
m_prg1.SetRange(0,1000);
jbjc_num=(int)(0.4*QTGM);//基本交叉
jbjc_num=2*(jbjc_num/2+1);
ysjc_num=(int)(0.2*QTGM);//原生交叉
ysjc_num=2*(ysjc_num/2+1);
fz_num=QTGM-jbjc_num-ysjc_num;//复制数目
bygl=6;///1/1000
GetDlgItem(IDC_PRODUCE)->EnableWindow(FALSE);
GetDlgItem(IDC_PRODUCE2)->EnableWindow(FALSE);
GetDlgItem(IDC_PRODUCE3)->EnableWindow(FALSE);
GetDlgItem(IDC_PRODUCE4)->EnableWindow(FALSE);
GetDlgItem(IDC_SAVE)->EnableWindow(FALSE);
inputpw();////////////////
m_qtgms.Format("%d",QTGM);
m_gtzs.Format("%d",OMIGA);
m_jdyq=0;m_jjqq=0;
loadldy();
// drawldy();
// display_date_time();
UpdateData(FALSE);
srand(time(NULL));
SetTimer(1,1000,NULL);
gtlll=sizeof(class gt);
/////////////////////////////////////////////////////////////////////
OnLoad();/////////////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CGA_tycpDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGA_tycpDlg::OnPaint()
{
drawldy();
//display_date_time();
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGA_tycpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGA_tycpDlg::OnLoad()
{
// TODO: Add your control notification handler code here
CFile fp;
if(fp.Open("qxclsjl.txt",CFile::modeRead)==NULL)
{ AfxMessageBox("不能打开历史记录文件qxclsjl.txt。");
return;
}
long l,i;
char t;
l=fp.GetLength();
lsjl=(class jjll *)malloc(sizeof(jjll)*(l/HMWS));
char *lsjl_text;
lsjls=0;
char hmwz=0;
fp.SeekToBegin();
for(i=0L;i<l;i++)
{
fp.Read(&t,1);
if(t<'0'||t>'9') continue;
t-='0';
lsjl[lsjls].w[hmwz]=t;
hmwz++;
if(hmwz==HMWS)
{
lsjls++;hmwz=0;
}
}
fp.Close();
UpdateData();
ckjl=m_ckjl.GetPos();
if(lsjls<MAXJLS)
{
AfxMessageBox("历史记录太少。");
exit(0);
}
lsjl_text=(char *)malloc(lsjls*(HMWS+2)+2);
int m,n;
long kl=0L;
for(m=0;m<lsjls;m++)
{
for(n=0;n<HMWS;n++)
lsjl_text[kl++]=(char)(lsjl[m].w[n]+'0');
lsjl_text[kl++]='\r';
lsjl_text[kl++]='\n';
}
lsjl_text[kl]=0;
m_lsjl.Format("%s",lsjl_text);
free(lsjl_text);
m_jls.Format("%d",lsjls);
m_hms.SetRange(1,lsjls-ckjl+1);
if(lsjls-ckjl>=4)
m_hms.SetPos(5);
else
m_hms.SetPos(lsjls-ckjl+1);
GetDlgItem(IDC_LOAD)->EnableWindow(FALSE);
GetDlgItem(IDC_PRODUCE)->EnableWindow(TRUE);
GetDlgItem(IDC_PRODUCE2)->EnableWindow(TRUE);
GetDlgItem(IDC_PRODUCE)->SetFocus();
UpdateData(FALSE);
drawldy();
}
int myabs(int k)
{
if(k<0) k=-k;
return(k);
}
void zl_yhcs()//整理优化参数
{
int i;char w;CFile tfp;
yhjl_fp.Open("qxcyhcs.ldy",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
yhjls=yhjl_fp.GetLength()/(HMWS*gtlll);//优化记录数
if(yhjls<=YHCSGS)
{
yhjl_fp.Close();return;
}
tfp.Open("ttqxcyhcs.ldy",CFile::modeCreate|CFile::modeWrite);
yhjl_fp.Seek(-YHCSGS*HMWS*gtlll,CFile::end);
for(i=0;i<YHCSGS;i++)
for(w=0;w<HMWS;w++)
{
yhjl_fp.Read(&dggt,gtlll);
tfp.Write(&dggt,gtlll);
}
tfp.Close();
yhjl_fp.Close();
CFile::Remove("qxcyhcs.ldy");
CFile::Rename("ttqxcyhcs.ldy","qxcyhcs.ldy");
}
void CGA_tycpDlg::OnProduce()
{
// TODO: Add your control notification handler code here
int n;
double bb,cc;
int thm;
UpdateData();
if(right==0) myexitf();
yhjl_fp.Open("qxcyhcs.ldy",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
yhjls=yhjl_fp.GetLength()/(HMWS*gtlll);//优化记录数
if(yhjls<QTGM) yh_start=0;
else yh_start=yhjls-QTGM;
if(lsjls<ckjl)
{ AfxMessageBox("记录数太少。"); return;yhjl_fp.Close();
}
CWaitCursor wait;
cshms=m_hms.GetPos();
if(cshms<1) {cshms=1;m_hms.SetPos(1);}
else if(cshms>lsjls-ckjl+1) {cshms=lsjls-ckjl+1;m_hms.SetPos(lsjls-ckjl+1);}
yhnd=m_yscs.GetPos();
if(yhnd<1) {yhnd=1;m_yscs.SetPos(1);}
else if(yhnd>500) {yhnd=500;m_yscs.SetPos(500);}
free(shm);
shm=(char *)malloc((HMWS+2)*sizeof(char)*cshms+2);
m_prg0.SetPos(0);
for(int k=0;k<cshms;k++)//产生不同号码
{
clear_pic();
num=ckjl+k;start=lsjls-num;//使用的历史记录数和起始位置
free(qx);
qx=(double *)malloc(sizeof(double)*(num+1));///一条曲线
for(wz=0;wz<HMWS;wz++)//一个号码的各位
{
inherit();//////遗传算法
for(n=0;n<=num;n++) qx[n]=0.;//各点
for(omiga=0;omiga<OMIGA;omiga++)//每条曲线
{ QBB();
QCC(0);
for(n=0;n<=num;n++)//各点
qx[n]+=((qt[0].qx[omiga].c[0]-127)*sin(bb*(start+n)+cc));
}
min=max=qx[0];
for(n=1;n<num;n++)
{
if(min>qx[n]) min=qx[n];
if(max<qx[n]) max=qx[n];
}
draw();
sx=12*(qt[0].ssxx+1)/256;
tg=qt[0].ttgg/64.;
qx[num]=sx*(qx[num]-min)/(max-min)+tg;//////////////////
thm=(int)qx[num];
if(thm<0) thm=0; else if(thm>9) thm=9;
shm[k*(HMWS+2)+wz]=(char)(thm+'0');
m_prg0.SetPos(1000*(k*HMWS+wz+1)/cshms/HMWS);
yhjl_fp.SeekToEnd();
yhjl_fp.Write(&(qt[0]),gtlll);
}
shm[k*(HMWS+2)+HMWS]='\r';
shm[k*(HMWS+2)+HMWS+1]='\n';
yhjl_fp.Close();
yhjl_fp.Open("qxcyhcs.ldy",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
yhjls++;
if(yhjls<QTGM) yh_start=0;
else yh_start=yhjls-QTGM;
}
shm[cshms*(HMWS+2)]=0;
m_ychm.Format("%s",shm);
yhjl_fp.Close();
wait.Restore();
GetDlgItem(IDC_PRODUCE3)->EnableWindow(TRUE);
//GetDlgItem(IDC_SAVE)->EnableWindow(TRUE);
//GetDlgItem(IDC_SAVE)->SetFocus();
UpdateData(FALSE);
///////////////////////////////////////////////////////
flag=0;
OnSave();/////////////////////////////////////////////////////////
zl_yhcs();//整理优化参数
}
void CGA_tycpDlg::OnSave()
{
// TODO: Add your control notification handler code here
CFile fp;
CString ss;
ss.Format("\r\n\r\n");
fp.Open("qxcychm.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
fp.SeekToEnd();
fp.Write(ss,ss.GetLength());
if(flag==0)
{
ss.Format("同步号码:\r\n\r\n");
fp.Write(ss,ss.GetLength());
fp.Write(shm,(HMWS+2)*sizeof(char)*cshms);
}
else if(flag==1)
{
ss.Format("冷热号码:\r\n\r\n");
fp.Write(ss,ss.GetLength());
fp.Write(lrhm,(HMWS+2)*sizeof(char)*cshms);
}
else if(flag==2)
{
ss.Format("同步组合号码:\r\n\r\n");
fp.Write(ss,ss.GetLength());
fp.Write(zhhm,(HMWS+2)*sizeof(char)*zhhms);
}
else
{
ss.Format("冷热组合号码:\r\n\r\n");
fp.Write(ss,ss.GetLength());
fp.Write(zhhm,(HMWS+2)*sizeof(char)*zhhms);
}
fp.Close();
GetDlgItem(IDOK)->SetFocus();
UpdateData(FALSE);
}
void init_code()//初始化编码
{
int qtgm;
for(qtgm=yh_start;qtgm<yhjls;qtgm++)//使用优化记录
{
yhjl_fp.Seek(1L*sizeof(char)*qtgm*HMWS*gtlll+wz*gtlll,CFile::begin);
yhjl_fp.Read(&(qt[qtgm]),gtlll);
}
for(qtgm=yhjls;qtgm<QTGM;qtgm++)//随即产生,群体规模
{
for(omiga=0;omiga<OMIGA;omiga++)//个体的每条曲线
for(dg=0;dg<DGCSSM;dg++)
qt[qtgm].qx[omiga].c[dg]=(unsigned char)(rand()%256);
qt[qtgm].ssxx=(unsigned char)(rand()%256);
qt[qtgm].ttgg=(unsigned char)(rand()%256);
}
}
void sort_adapt()//依适应值排序前reproduce_num名,同时将编码排序
{
int m,n;
int w;unsigned long k;
unsigned char uc;
for(m=0;m<fz_num;m++)
{
k=syz[m];w=m;
for(n=m+1;n<QTGM;n++)
if(k<syz[n])
{ k=syz[n];w=n; }
syz[w]=syz[m];
syz[m]=k;
for(omiga=0;omiga<OMIGA;omiga++)//个体的每条曲线
for(dg=0;dg<DGCSSM;dg++)
{
uc=qt[w].qx[omiga].c[dg];
qt[w].qx[omiga].c[dg]=qt[m].qx[omiga].c[dg];
qt[m].qx[omiga].c[dg]=uc;
}
uc=qt[w].ssxx;qt[w].ssxx=qt[m].ssxx;qt[m].ssxx=uc;
uc=qt[w].ttgg;qt[w].ttgg=qt[m].ttgg;qt[m].ttgg=uc;
}
}
void bet2(int &a,int &b)//轮盘赌产生两个位置
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -