📄 gaopt.cpp
字号:
// GAOpt.cpp : implementation file
//
#include "stdafx.h"
#include "optsoftware.h"
#include "GAOpt.h"
//#include "StringResolution.h"
//#include <math.h>
#include "GeneticDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString str,Objectfun;
double zdh5[30],LowerVariable[30];
int VariableNo;
double r;
/////////////////////////////////////////////////////////////////////////////
// CGAOpt
IMPLEMENT_DYNCREATE(CGAOpt, CFormView)
CGAOpt::CGAOpt()
: CFormView(CGAOpt::IDD)
{
//{{AFX_DATA_INIT(CGAOpt)
m_upperlimit = _T("");
m_lowerlimit = _T("");
m_variableamount = 0;
//}}AFX_DATA_INIT
}
CGAOpt::~CGAOpt()
{
}
void CGAOpt::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGAOpt)
DDX_Control(pDX, IDC_Mod_Path, m_cnModPath);
DDX_Control(pDX, IDC_FORMULA, m_cnFormula);
DDX_Control(pDX, IDC_GET_RESULT, m_cnComputer);
DDX_Text(pDX, IDC_Border_Restrict_Down, m_upperlimit);
DDX_Text(pDX, IDC_Border_Restrict_Up, m_lowerlimit);
DDX_Text(pDX, IDC_Veriable_Number, m_variableamount);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGAOpt, CFormView)
//{{AFX_MSG_MAP(CGAOpt)
ON_BN_CLICKED(IDC_GET_RESULT, OnGetResult)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_SHOWWINDOW()
ON_NOTIFY(NM_CLICK, IDC_Mod_Path, OnClickModPath)
ON_BN_CLICKED(IDC_DELETEMODEL, OnDeletemodel)
ON_BN_CLICKED(IDC_BUTTON3, OnGeneticClick)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGAOpt diagnostics
#ifdef _DEBUG
void CGAOpt::AssertValid() const
{
CFormView::AssertValid();
}
void CGAOpt::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGAOpt message handlers
void CGAOpt::OnGetResult()
{
// TODO: Add your control notification handler code here
// CString str; //作个全局变量用来接收除掉空格后组合的新的字符串****
CString str00;//用来接收变量以便变量依次进栈
// CString str000;
CString str0;//这里是设置变量来接收用户输入的表达式和约束条件
CString str1;//str1接收等式约束条件
CString str2;//str2接收不等式约束条件
CString str3;//str3用来接收总的表达式
CString str5;//得到用户在IDC_FORMULA输入的一切包括空格等
// CString str6;//过渡变量
CString str7;//过渡变量
CString str8;//过渡变量
CString str9;//过渡变量
CString str10;//过渡变量
CString str11;
CString str111;
CString str12;
CString zdh[1000];//还没有给他们赋初值""********
CString zdh1[1000];
CString zdh2[1000];
CString zdh3[100];
CString zdh4[100];
CString zdh6[100];
//double r; //罚因子,因为我是采用混和惩罚函数法来把有约束转无约束的
UpdateData(TRUE);
// GetDlgItem(IDC_GET_Variable)->GetWindowText(str00);
GetDlgItem(IDC_FORMULA)->GetWindowText(str5);//得到用户输入的字符串用str5接收
GetDlgItem(IDC_Equ_Restrict)->GetWindowText(str1);//这里我把等式和不等式的搞反了所以str1接收的是不等式的
GetDlgItem(IDC_Inequ_Restrict)->GetWindowText(str2);//str2接收的是等式约束的
//接收边界约束条件要传给优化的那边*******
GetDlgItem(IDC_Border_Restrict_Up)->GetWindowText(str11);//接受边界上限
GetDlgItem(IDC_Border_Restrict_Down)->GetWindowText(str111);//接受边界下限
VariableNo=m_variableamount;//接受变量个数
Objectfun=str5;
for(int yang=0,kun=0;yang<str11.GetLength();yang++)//上限的接受
{ CString xu;
xu=str11.GetAt(yang);
if (xu!='#')
{
zdh4[kun]=zdh4[kun]+xu;
}
else
{
zdh5[kun]=atof(zdh4[kun]);
kun++;
}
}
////////////
for(int y1=0,k1=0;y1<str111.GetLength();y1++)//下限的接受
{ CString x1;
x1=str111.GetAt(y1);
if (x1!='#')
{
zdh6[k1]=zdh6[k1]+x1;
}
else
{
LowerVariable[k1]=atof(zdh6[k1]);
k1++;
}
}
//////////////
/* for(int tan=0,jue=0;tan<str00.GetLength();tan++)//得到变量
{
str12=str00.GetAt(tan);
if (str12!='#')
zdh3[jue]=zdh3[jue]+str12;
else
{
jue++;
}
}
*/
// str000="0";
/* for(int na=0;na<jue;na++)//在表达式前添加变量的顺序号
{
str000=str000+"+"+zdh3[na];
}
*/
int m,n,j,k,l,p;
for(m=0,j=0;m<str1.GetLength();m++)//对不等式的处理
{
str7=str1.GetAt(m);
if (str7!='#')
zdh[j]=zdh[j]+str7; // 希望对得到的每一个字符累加得到一字符串
else
{
zdh2[j]="((1+sign("+zdh[j]+"))/2*("+zdh[j]+"))*((1+sign("+zdh[j]+"))/2*("+zdh[j]+"))";
zdh[j]="1/("+zdh[j]+")";
j++; //我希望把j值返回给下面的j???
}
}
// str6="0";
for(n=0;n<j;n++) //求和
{
// str6=str6+"+"+zdh[n]; //求和
str10=str10+"+"+zdh2[n]; //str10表【g(x)-b】^2的和,因为上面表达式包括可求平方
}
// str6="r*("+str6+")"; //用str6得第一项(不等式的约束)
for(k=0,l=0;k<str2.GetLength();k++)//对等式约束的处理
{
str8=str2.GetAt(k);
if (str8!='#')
zdh1[l]=zdh1[l]+str8;
else
{
zdh1[l]="("+zdh1[l]+")*("+zdh1[l]+")";//求其平方
l++;
}
}
str9="0";
for(p=0;p<l;p++) //用str9得第二项(等式约束)h^2
str9=str9+"+"+zdh1[p]; //求平方和
//下面一行中的str000这个字符名是为了使变量依次进栈对结果没有影响,因为相减使之总和为0
// str0=str000+"-("+str000+")+"+str5+str6+str9+str10;//以后目标是:str=str5+r*str6+1/sqrt(r)*(str9+str10)
//str=str5+r*str6+1/sqrt(r)*(str9+str10)
// str0=str000+"-("+str000+")+"+str5+"+r*("+str6+")+1/sqrt(r)*("+str9+str10+")";
// str0=str000+"-("+str000+")+"+str5+"-r*("+str6+")+1/sqrt(r)*("+str9+str10+")";
// str0=str000+"-("+str000+")+"+str5+"+1/sqrt(r)*("+str9+str10+")";
str0=str5+"+10000*("+str9+str10+")";
// if(str0.GetLength()==0) //对没有输入的处理因为str000赋了初值0,所以不可能字符串长度为0
// return;
int ding=str0.GetLength();//下面几行是处理掉空格的影响
for(int hua=0;hua<ding;hua++)
{
if(str0.GetAt(hua)!=' ')
str=str+str0.GetAt(hua);
}
// extern int w; //是全局变量W的值为0*******这个W不需要全局变量的
// w=0;
/*
//利用m_formula调用了CStringResolution类
m_formula.SetFormula(str.GetBuffer(str.GetLength()));// 重新设置时将所有的清除
//因为我这里不可能没有变量,所有下面的测试数据这一段要去掉
if(m_formula.GetVariantTableSize()==0)//得到的变量个数为0的话直接计算,不需要接收变量值
{
double value=m_formula.computer(NULL,0);//小写开头的computer的功能
CString str50;
if(m_formula.GetErrorNumber())
str50=m_formula.GetErrorInformation();
else
str50.Format("%g",value);
GetDlgItem(IDC_RESULT)->SetWindowText(str50);//显示结果
return;
}
// TODO: Add your control notification handler code here**********
int size=m_formula.GetVariantTableSize();
if(size==0)
return;
double* data;//=new double[size];
//10.16练习区
int num=m_formula.GetVariantTableSize();
data=new double[num];
for(int i=0;i<num;i++)
data[i]=i;//***********************这里就是我们以后的变量中转赋值的地方********************
//10.16日
CString str4;
double result=m_formula.computer(data,size);//得到了运算的结果
if(m_formula.GetErrorNumber())//输入有误时(比如最后多了一个-号)报错
str4=m_formula.GetErrorInformation();
else
str4.Format("%g",result);
GetDlgItem(IDC_RESULT)->SetWindowText(str4);// 在edit中显示运算的结果
//下面4行代码是显示变量给定后的表达式和其结果
// CStatic*pStatic=(CStatic*)GetDlgItem(IDC_STATIC0);
// str1=m_formula.GetDigitalString(data,size)+"=";
// pStatic->SetWindowText(str1);
delete[]data;
*/
}
void CGAOpt::OnButton2()
{
// TODO: Add your control notification handler code here
static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
static char BASED_CODE lpszDefExt[] = TEXT("txt");
//Create the dialog to select the network data file
CFileDialog dlg(TRUE,
lpszDefExt,
NULL,
OFN_READONLY |
OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST,
szFilter,
this);
if(dlg.DoModal ()==IDOK)
{
extern CString s[10];//这里我设的加入模型数为10。这可以改的
extern int w;
s[w]=dlg.GetPathName ();
HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_LOAD_MODEL);
::SetWindowText (hWnd,s[w]);
CString tjn;
tjn=(CString) (w+49);//想把一整型转为字符串通过ACSII码
int JustNowItem;
JustNowItem=m_cnModPath.InsertItem(0, tjn);
m_cnModPath.SetItemText(JustNowItem,1,s[w]);
w++;
}
else
{
return;
}
}
void CGAOpt::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFormView::OnShowWindow(bShow, nStatus);
// TODO: Add your message handler code here
UpdateData(false);
m_cnModPath.InsertColumn(0,"标志数字",LVCFMT_LEFT,80,-1);
m_cnModPath.InsertColumn(1,"模型所在路径",LVCFMT_LEFT,600,-1);
m_cnModPath.SetExtendedStyle(LVS_EX_FULLROWSELECT);
}
void CGAOpt::OnClickModPath(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int i=m_cnModPath.GetSelectionMark();
if (i==-1)
MessageBox("请先选择一条记录!");
else
{
m_cnModPath.SetHotItem(i);
}
*pResult = 0;
}
void CGAOpt::OnDeletemodel()
{
// TODO: Add your control notification handler code here
CString strSQL;
int ItemSel;
POSITION pos=m_cnModPath.GetFirstSelectedItemPosition();
if (pos==NULL)
{
AfxMessageBox("没有选择记录!");
return;
}
else while (pos)
{
ItemSel=m_cnModPath.GetNextSelectedItem(pos);
m_cnModPath.DeleteItem(ItemSel);
}
}
void CGAOpt::OnGeneticClick()
{
// TODO: Add your control notification handler code here
CGeneticDlg dlg;
dlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -