📄 jspsolutionview.cpp
字号:
// JSPSolutionView.cpp : implementation of the CJSPSolutionView class
//
#include "stdafx.h"
#include "JSPSolution.h"
#include "JSPSolutionDoc.h"
#include "JSPSolutionView.h"
#include "Problem.h"
#include "NN.h"
#include "RunTime.h"
#include "SAdlg.h"
#include "TC.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView
IMPLEMENT_DYNCREATE(CJSPSolutionView, CScrollView)
BEGIN_MESSAGE_MAP(CJSPSolutionView, CScrollView)
//{{AFX_MSG_MAP(CJSPSolutionView)
ON_COMMAND(ID_SetProblem, OnSetProblem)
ON_COMMAND(ID_SetNNetwork, OnSetNN)
ON_COMMAND(ID_RunMore, OnRunMore)
ON_COMMAND(ID_Run, OnRun)
ON_COMMAND(ID_directDraw, OndirectDraw)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView construction/destruction
CJSPSolutionView::CJSPSolutionView()
{
A = 500; B = 300; C = 200; D1 = 500; D2 = 300; D3 = 300;
c = 1; R = 1;
u0 = 0.2;
dt = 0.00001;
Tmax = 100;
Tmin = 0.18;
r = 0.9;
k = 5;
ktc = 0.995;
a = 0.0012;
b = 0.03;
I0 = 0.65;
z0 = 0.08;
nntype = 0;
fileAddr = "D:\\exercise\\JSP神经网络演示\\samples\\tasks23.txt";
selnn = false;
selpro = false;
runtime = 100;
mincost = -1;
his = new GanttHis();
}
CJSPSolutionView::~CJSPSolutionView()
{
if (selpro == true)
{
for (int i = 0; i < n; i++)
{
delete []machine[i];
delete []protime[i];
}
delete []machine;
delete []protime;
}
delete his;
}
BOOL CJSPSolutionView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView drawing
void CJSPSolutionView::OnDraw(CDC* pDC)
{
CJSPSolutionDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int len,i;
if (his == NULL) return;
CPen pen(PS_SOLID,2,RGB(0,0,0));
CPen *oldpen = pDC->SelectObject(&pen);
vector<LineHis> linehis = his->linehis;
vector<TextHis> texthis = his->texthis;
len= texthis.size();
for (i = 0; i < len; i++)
{
pDC->TextOut(texthis[i].pos.x, texthis[i].pos.y, texthis[i].word);
}
len = linehis.size();
for (i = 0; i < len; i++)
{
pDC->MoveTo(linehis[i].start);
pDC->LineTo(linehis[i].end);
}
pDC->SelectObject(oldpen);
/*
RECT rc;
GetWindowRect(&rc);
CSize sizeTotal(rc.right, rc.bottom);
SetScrollSizes(MM_TEXT,sizeTotal);
*/
}
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView printing
BOOL CJSPSolutionView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CJSPSolutionView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CJSPSolutionView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView diagnostics
#ifdef _DEBUG
void CJSPSolutionView::AssertValid() const
{
CScrollView::AssertValid();
}
void CJSPSolutionView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CJSPSolutionDoc* CJSPSolutionView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJSPSolutionDoc)));
return (CJSPSolutionDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJSPSolutionView message handlers
void CJSPSolutionView::OnSetProblem()
{
CProblem prodlg;
prodlg.m_fileAddr = fileAddr;
if (prodlg.DoModal() == IDOK)
{
fileAddr = prodlg.m_fileAddr;
if (fileAddr != "")
{
int i, j;
if (selpro == true)
{
for (i = 0; i < n; i++)
{
delete []machine[i];
delete []protime[i];
}
delete []machine;
delete []protime;
}
ifstream fin(fileAddr);
fin>>n>>m;
machine = new int*[n];
protime = new int*[n];
for (i = 0; i < n; i++)
{
machine[i] = new int[m];
protime[i] = new int[m];
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
fin>>machine[i][j];
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
fin>>protime[i][j];
}
}
fin>>mincost;
fin.close();
selpro = true;
}
}
}
void CJSPSolutionView::OnSetNN()
{
CNN dlg;
dlg.m_A = A;
dlg.m_B = B;
dlg.m_C = C;
dlg.m_D1 = D1;
dlg.m_D2 = D2;
dlg.m_D3 = D3;
dlg.m_nntype = nntype;
if (dlg.DoModal() == IDOK)
{
nntype = dlg.m_nntype;
A = dlg.m_A;
B = dlg.m_B;
C = dlg.m_C;
D1 = dlg.m_D1;
D2 = dlg.m_D2;
D3 = dlg.m_D3;
if (nntype == 1)
{
CSAdlg sa;
sa.m_r = r;
sa.m_tmax = Tmax;
sa.m_tmin = Tmin;
sa.m_k = k;
if (sa.DoModal() == IDOK)
{
r = sa.m_r;
Tmax = sa.m_tmax;
Tmin = sa.m_tmin;
k = sa.m_k;
}
}
else if (nntype == 2)
{
CTC tc;
tc.m_a = a;
tc.m_b = b;
tc.m_I0 = I0;
tc.m_z0 = z0;
tc.m_ktc = ktc;
if (tc.DoModal() == IDOK)
{
a = tc.m_a;
b = tc.m_b;
ktc = tc.m_ktc;
z0 = tc.m_z0;
I0 = tc.m_I0;
}
}
selnn = true;
}
}
void CJSPSolutionView::OnRunMore()
{
CRunTime dlg;
dlg.m_runtime = runtime;
if (dlg.DoModal()==IDOK)
{
runtime = dlg.m_runtime;
if (selpro == false)
{
AfxMessageBox("请先设定调度问题所对应的数据文件");
return;
}
if (selnn == false)
{
AfxMessageBox("请先设定神经网络相关参数");
return;
}
int valid = 0;
int opt = 0;
int i;
CString msg;
delete his;
his = new GanttHis();
switch(nntype)
{
case 0:
//初始化hopfield神经网络对象
hnet = new HNN(n, m, machine, protime);
hnet->setPara(A, B, C, D1, D2, D3);
//运行hopfield神经网络
for (i = 0; i < runtime; i++)
{
if (hnet->run())
{
hnet->drawGantt(his,valid);
valid++;
if (hnet->getCost() == mincost) opt++;
}
}
Invalidate(true);
msg.Format("Hopfield 神经网络:\n在 %d 次运行中得到:\n%d 个有效解\n其中%d个为最优解\n", runtime, valid, opt);
AfxMessageBox(msg);
delete hnet;
break;
case 1:
//初始化随机神经网络对象
rnet = new RNN(n, m, machine, protime, mincost);
rnet->setPara(A, B, C, D1, D2, D3);
rnet->setAPPara(Tmax, Tmin, r, k);
//运行随机神经网络
for (i = 0; i < runtime; i++)
{
if (rnet->run())
{
rnet->drawGantt(his,valid);
valid++;
if (rnet->getCost() == mincost) opt++;
}
}
Invalidate(true);
msg.Format("随机神经网络:\n在 %d 次运行中得到:\n%d 个有效解\n其中%d个为最优解", runtime, valid, opt);
AfxMessageBox(msg);
delete rnet;
break;
case 2:
//初始化暂态混沌神经网络对象
tcnet = new TCNN(n, m, machine, protime, mincost);
tcnet->setPara(A, B, C, D1, D2, D3);
tcnet->setTCPara(ktc,a,b,I0,z0);
//运行暂态混沌神经网络
for (i = 0; i < runtime; i++)
{
if (tcnet->run())
{
tcnet->drawGantt(his,valid);
valid++;
if (tcnet->getCost() == mincost) opt++;
}
}
Invalidate(true);
msg.Format("暂态混沌神经网络:\n在 %d 次运行中得到:\n%d 个有效解\n其中%d个为最优解", runtime, valid, opt);
AfxMessageBox(msg);
delete tcnet;
break;
}
}
}
void CJSPSolutionView::OnRun()
{
if (selpro == false)
{
AfxMessageBox("请先设定调度问题所对应的数据文件");
return;
}
if (selnn == false)
{
AfxMessageBox("请先设定神经网络相关参数");
return;
}
delete his;
his = new GanttHis();
switch(nntype)
{
case 0:
//初始化hopfield神经网络对象
hnet = new HNN(n, m, machine, protime);
hnet->setPara(A, B, C, D1, D2, D3);
//运行hopfield神经网络
if (hnet->run())
{
//输出hopfield神经网络所得结果,绘制甘特图
hnet->drawNeroOutputs(his);
hnet->drawGantt(his,2);
Invalidate(true);
AfxMessageBox("运行时间为"+hnet->getRunTime());
}
else
{
AfxMessageBox("调度方案为非法!\n请重新运行神经网络");
}
delete hnet;
break;
case 1:
//初始化随机神经网络对象
rnet = new RNN(n, m, machine, protime,mincost);
rnet->setPara(A, B, C, D1, D2, D3);
rnet->setAPPara(Tmax, Tmin, r, k);
//运行随机神经网络
if (rnet->run())
{
//输出随机神经网络所得结果,绘制甘特图
rnet->drawNeroOutputs(his);
rnet->drawGantt(his,2);
Invalidate(true);
AfxMessageBox("运行时间为"+rnet->getRunTime());
}
else
{
AfxMessageBox("调度方案为非法!\n请重新运行神经网络");
}
delete rnet;
break;
case 2:
//初始化暂态混沌神经网络对象
tcnet = new TCNN(n, m, machine, protime, mincost);
tcnet->setPara(A, B, C, D1, D2, D3);
tcnet->setTCPara(ktc,a,b,I0,z0);
//运行暂态混沌神经网络
if (tcnet->run())
{
//输出暂态混沌神经网络所得结果,绘制甘特图
tcnet->drawNeroOutputs(his);
tcnet->drawGantt(his,2);
Invalidate(true);
AfxMessageBox("运行时间为"+tcnet->getRunTime());
}
else
{
AfxMessageBox("调度方案为非法!\n请重新运行神经网络");
}
delete tcnet;
break;
}
}
void CJSPSolutionView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal(2500, 15000);
SetScrollSizes(MM_TEXT,sizeTotal);
}
void CJSPSolutionView::OndirectDraw()
{
CProblem prodlg;
prodlg.m_fileAddr = fileAddr;
if (prodlg.DoModal() == IDOK)
{
fileAddr = prodlg.m_fileAddr;
if (fileAddr != "")
{
int i, j;
ifstream fin(fileAddr);
fin>>n>>m;
int **machine = new int*[n];
int **protime = new int*[n];
for (i = 0; i < n; i++)
{
machine[i] = new int[m];
protime[i] = new int[m];
}
double **resmatrix = new double*[n*m];
for (i = 0; i < n*m; i++)
{
resmatrix[i] = new double[n*m+1];
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
fin>>machine[i][j];
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
fin>>protime[i][j];
}
}
for(i = 0; i < n*m; i++)
{
for(j = 0; j < n*m+1; j++)
{
fin>>resmatrix[i][j];
}
}
delete his;
his = new GanttHis();
GanttChart *chart;
chart = new GanttChart(n, m, machine, protime, resmatrix);
chart->constructGantt();
if (chart->isValid() == true)
{
chart->drawGantt(his);
}
else
AfxMessageBox("调度矩阵表示非法调度!");
Invalidate(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -