📄 tspdlg.cpp
字号:
// TSPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TSP.h"
#include "TSPDlg.h"
#include "math.h"
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#define N 10
#define A 500
#define B 500
#define C 200
#define D 500
#define JC 500;
#define KC 500;
#define tt 300;
#define X0 0.02
#define det 0.001//积分时间间隔
int city[200][10];
#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()
/////////////////////////////////////////////////////////////////////////////
// CTSPDlg dialog
CTSPDlg::CTSPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTSPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTSPDlg)
m_length = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTSPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTSPDlg)
DDX_Control(pDX, IDC_PLAY, m_display);
DDX_Text(pDX, IDC_EDIT1, m_length);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTSPDlg, CDialog)
//{{AFX_MSG_MAP(CTSPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnTSP)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTSPDlg message handlers
BOOL CTSPDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTSPDlg::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 CTSPDlg::OnPaint()
{
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 CTSPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTSPDlg::OnTSP()
{
// TODO: Add your control notification handler code here
int x,y,i,j,t;
int MAX=3000;
int city[N];
double distence1;
double Distence=0;
double MedA[N][N];
double MedB[N][N];
double Xinit=0.5*X0*log(N-1);
double V[N][N];
double X[N][N];
double detax[N][N];
double d[N][N];//城市归一化距离
double label[N][2]={{0.4,0.4439},{0.2439,0.1463},{0.1707,0.2293},{0.2293,0.7610},{0.5171,0.9414},
{0.8732,0.6536},{0.6878,0.5219},{0.8488,0.3609},{0.6683,0.2536},{0.6195,0.2634}};
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
d[i][j]=sqrt((label[i][0]-label[j][0])*(label[i][0]-label[j][0])+(label[i][1]-label[j][1])*(label[i][1]-label[j][1]));
}
//初始化
double r[10][10];
srand( (unsigned)time( NULL ) );
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
r[i][j]=(double)(rand()%100)/10000.0;
X[i][j]=Xinit+r[i][j];
}
}
for(t=0;t<MAX;t++)
{
for(x=0;x<N;x++)
{
for(i=0;i<N;i++)
{
V[x][i]=(1+tanh(X[x][i]/X0))/2;
}
}
for(x=0;x<N;x++)
{
for(i=0;i<N;i++)
{
detax[x][i]=this->TSPFUNC(x,i,V,d,X);
X[x][i]=X[x][i]+detax[x][i]*det;
}
}
//--------------判断收敛路径是否合理-------------------
for(x=0;x<N;x++)
{
for(i=0;i<N;i++)
{
MedA[x][i]=0;
MedB[x][i]=0;
}
}
for (x=0;x<N;x++)
{
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (j!=i)
{
MedA[x][i]+=V[x][i]*V[x][j];
}
}
}
}
for (i=0;i<N;i++)
{
for(x=0;x<N;x++)
{
for(y=0;y<N;y++)
{
if (x!=y)
{
MedB[x][i]+=V[x][i]*V[y][i];
}
}
}
}
double AddMA=0,AddMB=0,AddMV=0,Distence=0;
for (x=0;x<N;x++)
{
for (i=0;i<N;i++)
{
AddMA+=MedA[x][i];
AddMB+=MedB[x][i];
AddMV+=V[x][i];
}
}
if((AddMA+AddMB==0)&&AddMV==10)
{
for (i=0;i<N;i++)
{
city[i]=0;
}
for (i=0;i<N;i++)
{
for (x=0;x<N;x++)
{
if (V[x][i]==1)
{
city[i]=x;
}
}
}
Distence=0;
for(i=0;i<N;i++)
{
if(i==N-1)
Distence+=d[city[i]][city[0]];
else Distence+=d[city[i]][city[i+1]];
}
distence1=Distence;
break;
}
}
int ppp=0;
for(x=0;x<N;x++)
for(i=x+1;i<N;i++)
{
if(city[x]==city[i])
{
ppp=1;
break;
}
}
CStatic *ms;
ms=&m_display;
CClientDC dc1(ms);
CRect rect;
m_display.GetClientRect(&rect);
dc1.FillSolidRect(rect,RGB(151,255,255));
CPen pen1,pen2,pen3;
pen1.CreatePen(PS_SOLID,1,RGB(00,00,00));
pen2.CreatePen(PS_SOLID,1,RGB(191,62,255));
if(ppp==0)
{
char l[50];
_gcvt(distence1,10,l);
m_length=(LPCTSTR)l;
UpdateData(FALSE);
dc1.SelectObject(&pen1);
for (i=0;i<N;i++)
{
dc1.SelectObject(&pen1);
dc1.Ellipse(label[i][0]*rect.Width()-5,(1-label[i][1])*rect.Height()-5,label[i][0]*rect.Width()+5,(1-label[i][1])*rect.Height()+5);
char s[10];
sprintf(s,"%d",i+1);
if(i==8)
dc1.TextOut(label[i][0]*rect.Width()+5,(1-label[i][1])*rect.Height()+15,s);
else
dc1.TextOut(label[i][0]*rect.Width(),(1-label[i][1])*rect.Height()+10,s);
dc1.SelectObject(&pen2);
if (i==N-1)
{
dc1.MoveTo(label[city[i]][0]*rect.Width(),(1-label[city[i]][1])*rect.Height());
dc1.LineTo(label[city[0]][0]*rect.Width(),(1-label[city[0]][1])*rect.Height());
}
else
{
dc1.MoveTo(label[city[i]][0]*rect.Width(),(1-label[city[i]][1])*rect.Height());
dc1.LineTo(label[city[i+1]][0]*rect.Width(),(1-label[city[i+1]][1])*rect.Height());
}
}
}
else
{
dc1.FillSolidRect(rect,RGB(151,255,255));
MessageBox("不合理路径");
}
}
double CTSPDlg::TSPFUNC(int p, int q, double V[][N], double Dist[][N], double U[][N])
{
double DuDt=0;
double a,b,c,d,e,f;
int q1,q2;
a=b=c=d=e=f=0;
for (int j=0;j<N;j++)
{
if (j!=q)
{
a+=V[p][j];
}
}
a=a*A;
// cout<<a<<' ';
for (int y=0;y<N;y++)
{
if (y!=p)
{
b+=V[y][q];
}
}
b=b*B;
// cout<<b<<' ';
for (int x=0;x<N;x++)
{
for (j=0;j<N;j++)
{
c+=V[x][j];
}
}
c=(c-N)*C;
// cout<<c<<' ';
for (y=0;y<N;y++)
{
if(q==N-1)
{
q1=0;
}
else
{
q1=q+1;
}
if (q==0)
{
q2=N-1;
}
else
{
q2=q-1;
}
d+=Dist[p][y]*(V[y][q1]+V[y][q2]);
}
d=d*D;
for (int i=0;i<N;i++)
{
e+=V[p][i];
}
e=(e-1)*JC;
for (x=0;x<N;x++)
{
f+=V[x][q];
}
f=(f-1)*KC;
DuDt=-U[p][q]/tt;
DuDt=DuDt-a-b-c-d-e-f;
return DuDt;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -