const char DialogTitle[] = "Ýêñïåðòíàÿ ñèñòåìà";
enum {
WM_ASKQUESTION = WM_USER+100
};
HINSTANCE hInst;
std::string dbName;
std::string txtMain;
HWND hwndMain;
unsigned hcount = 0;
unsigned ecount = 0;
int bestvar;
struct P {
int j;
float py;
float pn;
};
struct Hypotheses {
Hypotheses():
questions(0)
{ }
float p;
float prior;
std::vector
pevid;
int questions;
float mini;
float maxi;
// unsigned int var;
std::string text;
} *h = NULL;
struct Evidence {
Evidence():
rulevalue(0),
varflag(true)
{ }
int rulevalue;
bool varflag;
std::string text;
} *e = NULL;
void AskQuestion(HWND hwndDlg)
{ int rv = -1;
bestvar = -1;
for(int j=0; j<ecount; j++)
{ if(e[j].rulevalue > rv)
{ bestvar = j;
rv = e[j].rulevalue;
}
e[j].rulevalue = 0;
}
if(bestvar == -1)
MessageBox(hwndDlg, "Ñèìïòîìîâ áîëüøå íåò", "Ýêñïåðòíàÿ ñèñòåìà", MB_ICONINFORMATION);
else
{ e[bestvar].varflag = false;
SetWindowText(GetDlgItem(hwndDlg, IDC_QUEST), e[bestvar].text.c_str());
CheckRadioButton(hwndDlg, IDC_REZ0, IDC_REZ10, IDC_REZ5);
}
}
void HandleResponse(HWND hwndDlg)
{ int response =
(IsDlgButtonChecked(hwndDlg, IDC_REZ0) == BST_CHECKED)? -5:
(IsDlgButtonChecked(hwndDlg, IDC_REZ1) == BST_CHECKED)? -4:
(IsDlgButtonChecked(hwndDlg, IDC_REZ2) == BST_CHECKED)? -3:
(IsDlgButtonChecked(hwndDlg, IDC_REZ3) == BST_CHECKED)? -2:
(IsDlgButtonChecked(hwndDlg, IDC_REZ4) == BST_CHECKED)? -1:
(IsDlgButtonChecked(hwndDlg, IDC_REZ5) == BST_CHECKED)? 0:
(IsDlgButtonChecked(hwndDlg, IDC_REZ6) == BST_CHECKED)? 1:
(IsDlgButtonChecked(hwndDlg, IDC_REZ7) == BST_CHECKED)? 2:
(IsDlgButtonChecked(hwndDlg, IDC_REZ8) == BST_CHECKED)? 3:
(IsDlgButtonChecked(hwndDlg, IDC_REZ9) == BST_CHECKED)? 4: 5;
std::vector
::iterator k;
int i;
txtMain = "Èñêëþ÷àþòñÿ èñõîäû:\n";
// àíàëèç îòâåòà ïîëüçîâàòåëÿ è êîððåêòèðîâêà âåðîÿòíîñòåé ////////////////
for(i=0; i<hcount; i++)
{ for(k=h.pevid.begin(); k!=h.pevid.end(); k++)
{ if(k->j == bestvar && h.questions)
{ h.questions--;
float p = h.p;
float pe = p * k->py + (1-p) * k->pn;
h.p += 0.2 * p *response * (k->py - pe) / ((response > 0)? pe : (1-pe));
if(h.p < 0.0001 || h.p > 0.9999)
h.questions = 0;
}
}
}
// îïðåäåëåíèå íîâûõ çíà÷åíèé ïðàâèë è ìèíèìàëüíûõ çíà÷åíèé âåðîÿòíîñòèé //
float maxofmin = 0;
int best = 0;
for(i=0; i<hcount; i++)
{ float a1 = 1.0;
float a2 = 1.0;
float a3 = 1.0; // ?
float a4 = 1.0;
for(k=h.pevid.begin(); k!=h.pevid.end(); k++)
{ if(e[k->j].varflag && h.questions)
{ if(k->pn > k->py)
{ k->py = 1.0 - k->py;
k->pn = 1.0 - k->pn;
}
e[k->j].rulevalue ++;
a1 = a1 * k->py;
a2 = a2 * k->pn;
a3 = a3 * (1-k->py);
a4 = a4 * (1-k->pn);
}
}
h.maxi = h.p*a1 / (h.p*a1 + (1-h.p)*a2);
h.mini = h.p*a3 / (h.p*a3 + (1-h.p)*a4);
if(h.maxi < h.prior)
{ h.questions = 0;
txtMain += h.text;
txtMain += "\n";
}
if(h.mini > maxofmin)
{ best = i;
maxofmin = h.mini;
}
}
SetWindowText(hwndMain, txtMain.c_str());
for(i=0; i<hcount; i++)
{ if(h[best].mini<=h.maxi && i!=best)
{ AskQuestion(hwndDlg);
return;
}
}
txtMain += "\nÍàèáîëåå âåðîÿòíûé èñõîä:\n";
txtMain += h[best].text;
txtMain += "\nñ âåðîÿòíîñòüþ ðàâíîé P = ";
char buf[10];
txtMain += _gcvt(h[best].p, 4, buf);
EnableWindow(GetDlgItem(hwndDlg, ID_NEXT), false);
SetWindowText(hwndMain, txtMain.c_str());
}
void FailLoadDatabase(HWND hwndDlg)
{ dbName.insert(0, "Ôàéë '");
dbName += "'\nÍå ÿâëÿåòñÿ ôàéëîì áàçû äàííûõ ýêñïåðòíîé ñèñòåìû";
MessageBox(hwndDlg, dbName.c_str(), NULL, MB_ICONEXCLAMATION);
txtMain += "Îøèáêà ïðè çàãðóçêå áàçû äàííûõ.\n";
SetWindowText(hwndMain, txtMain.c_str());
}
ЭС (перевод)
Парюсь уже 4-й день по переводу C++ в Delphi, ПОЖАЛУЙСТА, ПОМОГИТЕ!!!
Код на С++:
Код: