📄 nn.c
字号:
rtemp = readreal(GE,0.0,'r');
if (readerror) return; else highalpha = rtemp;
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
if (ch == '\n')
{
lowalpha = highalpha;
npasses = 1;
run(npasses);
return;
};
rtemp = readreal(GE,0.0,'r');
if (readerror) return; else lowalpha = rtemp;
itemp = readint(1,MAXINT,'r');
if (readerror) return;
npasses = itemp;
run(npasses);
};
} /* end of scanrun */
/* for a SIGINT, restart in cmdloop */
#ifdef UNIX
void restartcmdloop()
#else
void restartcmdloop(int dummy)
#endif
{
while (data != stdin) popfile();
signal(SIGINT,restartcmdloop);
longjmp(cmdloopstate,1);
}
void cmdloop() /* read commands and process them */
{
int finished;
int i, itemp, itemp2;
char string[81];
REAL rtemp, *vec;
NODE *point, *answeraddr;
setjmp(cmdloopstate); /* restart here from SIGINT */
finished = 0;
ch = ' ';
do{
lineno = 0;
if (data == stdin) pg("[?!*AaCcdfhlmnPpqrst]? ");
while(ch == ' ' || ch == '\n') ch = readch();
switch (ch) {
case EOF:
pg("hit case EOF\n");
popfile();
if (data == stdin) pg("taking commands from stdin now\n");
break;
case '?':
sprintf(outstr,"\n%d iterations %d prototypes\n",s[TRAIN].iterno,nprotos);
pg(outstr);
sprintf(outstr,"data file = %s\n",datafilename);
pg(outstr);
sprintf(outstr,"input file = %s\n",inputfile);
pg(outstr);
sprintf(outstr,"nearest neighbors file = %s\n",nearestfile);
pg(outstr);
sprintf(outstr,"original prototype file = %s\n",original);
pg(outstr);
sprintf(outstr,"new prototype file = %s\n",newproto);
pg(outstr);
if (algorithm == 'l') pg("lvq1 "); else pg("dsm ");
sprintf(outstr,"%7.5f %7.5f %d\n",highalpha,lowalpha,npasses);
pg(outstr);
sprintf(outstr,"f c%c e%c P %1d s%c w%c\n",copyflag,echo,pagesize,summary,listpat);
pg(outstr);
sprintf(outstr,"%d training patterns from the file %s\n",npats,trainfile);
pg(outstr);
sprintf(outstr,"%3d iterations ",s[TRAIN].iterno); pg(outstr);
sprintf(outstr," %5.2f%% right ",s[TRAIN].pctright); pg(outstr);
sprintf(outstr,"(%d right, %d wrong)\n",s[TRAIN].right,s[TRAIN].wrong);
pg(outstr);
sprintf(outstr,"%d testing patterns from the file %s\n",ntestpats,testfile);
pg(outstr);
sprintf(outstr,"%3d iterations ",s[TEST].iterno); pg(outstr);
sprintf(outstr," %5.2f%% right ",s[TEST].pctright); pg(outstr);
sprintf(outstr,"(%d right, %d wrong)\n",s[TEST].right,s[TEST].wrong);
pg(outstr);
pg("for help, type h followed by the letter of the command\n\n");
break;
case '!':
i = 0;
ch = readch();
while (ch != '\n' && i <= 80)
{
string[i] = ch;
ch = readch();
i = i + 1;
};
bufferptr = bufferptr - 1;
string[i] = '\0';
system(string);
break;
case '*': break; /* * on a line is a comment */
case 'A':
do ch = readch(); while (ch == ' ' || ch == '\n');
if (ch == 'l' || ch == 'd') algorithm = ch;
break;
case 'a':
rtemp = readreal(GE,0,'a');
if (readerror) break;
alpha = rtemp;
break;
case 'c':
if (nopatterns(TRAIN)) goto endc;
point = protolist;
while (point != NULL) {point->nn = 0; point = point->next;};
point = trainlist;
while (point != NULL) {point->nn = 0; point = point->next;};
point = testlist;
while (point != NULL) {point->nn = 0; point = point->next;};
for (i=TRAIN;i<=TEST;i++)
{
s[i].right = 0;
s[i].wrong = 0;
s[i].pctright = 0;
s[i].iterno = 0;
};
{NODE *orig, *newnode;
REAL *origvector, *newvector;
int j;
orig = initialprotos;
newnode = protolist;
while (orig != NULL)
{
origvector = orig->vector;
newvector = newnode->vector;
for (j=1;j<=length;j++) *newvector++ = *origvector++;
newnode = newnode->next;
orig = orig->next;
};
};
endc: break;
case 'C':
if (nonetwork() || nopatterns(TRAIN)) goto endC;
initiallambda = readreal(GE,0.0,'C');
if (readerror) goto endC;
maxdist = readreal(GT,0.0,'C');
if (readerror) goto endC;
do ch = readch(); while (ch == ' ');
if (ch >= '0' && ch <= '9')
{
bufferptr = bufferptr - 1;
fract = readreal(GE,0.0,'C');
if (readerror) goto endC;
}
else fract = 0.0;
clusterall(trainlist);
s[TRAIN].iterno = 0;
s[TEST].iterno = 0;
for (i=TRAIN;i<=TEST;i++)
{
s[i].right = 0;
s[i].wrong = 0;
s[i].pctright = 0;
s[i].iterno = 0;
};
makecopy();
endC: break;
case 'h': help(); break;
case 'f':
while (ch != '\n' && ch != '*')
{
ch = readch();
if (ch == 'c')
{
do ch = readch(); while (ch == ' ');
if (ch == '+' || ch == '-')
{
copyflag = ch;
if (copyflag == '+') copy = fopen("copy","w");
else if (copy != NULL)
{
fflush(copy);
fclose(copy);
}
else pg("the copy file was never opened\n");
}
else texterror(1697);
}
else if (ch == 'e')
{
do ch = readch(); while (ch == ' ');
if (ch == '+' || ch == '-') echo = ch;
else texterror(1703);
}
else if (ch == 'P')
{
itemp = readint(0,MAXINT,'f');
if (!readerror) pagesize = itemp;
}
else if (ch == 's')
{
do ch = readch(); while (ch == ' ');
if (ch == '+' || ch == '-') summary = ch;
}
else if (ch == 'w')
{
do ch = readch(); while (ch == ' ');
if (ch == '+' || ch == '-') listpat = ch;
}
else if (ch == ' ' || ch == '*' || ch == '\n') /* nothing */;
else texterror(1721);
};
bufferptr = bufferptr - 1;
break;
case 'i':
do ch = readch(); while (ch == ' ');
if (ch != '\n')
{
bufferptr = bufferptr - 1;
inputfile = readstr();
}
pushfile(inputfile);
break;
case 'm':
length = readint(0,MAXINT,'m');
nclasses = readint(0,MAXINT,'m');
errmatsize = nclasses * (nclasses + 1);
errmat = (INT32 *) malloc(sizeof(INT32) * nclasses * (nclasses + 1));
errtotal = (INT32 *) malloc(sizeof(INT32) * (nclasses + 1));
votes = (INT32 *) malloc(sizeof(INT32) * (nclasses + 1));
blank = (NODE *) malloc(sizeof(NODE));
vec = (REAL *) malloc(sizeof(REAL) * (length + 1));
blank->vector = vec;
blank->next = NULL;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': /* case 'n' */
if (nonetwork() || nopatterns(TRAIN)) goto endn;
bufferptr = bufferptr - 1;
k = readint(1,npats,'n');
if (readerror) goto endn;
do ch = readch(); while (ch == ' ');
if (ch != 'n') texterror(1763);
else
{
do ch = readch(); while (ch == ' ');
if (ch == 'n') do ch = readch(); while (ch == ' ');
if (ch == '\n' || ch == '*') knn(trainlist,trainlist);
else
{
bufferptr = bufferptr - 1;
vec = blank->vector;
for (i=1;i<=length;i++)
{
rtemp = readreal(GT,-MAXVAL,'n');
if (readerror) goto endn;
*vec++ = rtemp;
};
blank->class = 0;
do ch = readch(); while (ch == ' ');
if (ch == '\n' || ch == '*') knn(trainlist,blank);
else if (ch == '>')
{PAT *c; NODE *point; REAL *coord; FILE *nearest;
do ch = readch(); while (ch == ' ');
if (ch != '\n' && ch != '*')
{
bufferptr = bufferptr - 1;
nearestfile = readstr();
};
knn(trainlist,blank);
c = closest->next;
nearest = fopen(nearestfile,"w");
coord = blank->vector;
fprintf(nearest,"* ");
for (i=1;i<=length;i++) fprintf(nearest," %6.3f",*coord++);
fprintf(nearest," * 0\n");
while (c != NULL)
{
point = c->point;
coord = point->vector;
for (i=1;i<=length;i++) fprintf(nearest," %6.3f",*coord++);
fprintf(nearest,"%3d * %6.3f\n",point->class,sqrt(c->distsq));
c = c->next;
};
fflush(nearest);
fclose(nearest);
};
};
};
endn: break;
case 'p':
if (nonetwork() || nopatterns(TRAIN) || noprotos()) goto endp;
do ch = readch(); while (ch == ' ');
if (ch == '0') eval(TRAIN,(int) 0);
else if (ch == 'm') printmatrix(TRAIN);
else if (ch == '\n' || ch == '*') eval(TRAIN,(int) 1);
else if (ch >= '1' && ch <= '9')
{
bufferptr = bufferptr - 1;
itemp = readint(1,npats,'p');
if (!readerror) evalone(TRAIN,itemp);
};
endp: break;
case 'P':
if (nonetwork() || noprotos()) goto endP;
vec = blank->vector;
for (i=1;i<=length;i++)
{
rtemp = readreal(GT,-MAXVAL,'P');
if (readerror) goto endP;
*vec++ = rtemp;
};
answeraddr = nn(blank,-1);
sprintf(outstr,"answer = %d\n",answeraddr->class); pg(outstr);
endP: break;
case 'q': return;
case 'd':
algorithm = 'd';
do ch = readch(); while (ch == ' ');
scanrun();
break;
case 'l':
algorithm = 'l';
do ch = readch(); while (ch == ' ');
scanrun();
break;
case 'r':
if (nonetwork()) goto endr;
do ch = readch(); while (ch == ' ');
if (ch == 'p' || ch == 'w')
{
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
original = readstr();
protolist = readpats(original,protolist);
if (noprotos()) goto endr;
nprotos = listcount;
for (i=TRAIN;i<=TEST;i++)
{
s[i].iterno = 0;
s[i].right = 0;
s[i].wrong = 0;
s[i].pctright = 0;
};
makecopy();
}
else if (ch == 't')
{
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
trainfile = readstr();
trainlist = readpats(trainfile,trainlist);
if (nopatterns(TRAIN)) goto endr;
npats = listcount;
s[TRAIN].iterno = 0;
s[TRAIN].right = 0;
s[TRAIN].wrong = 0;
s[TRAIN].pctright = 0.0;
s[TRAIN].npats = npats;
}
else scanrun();
endr: break;
case 's': /* sp <filename> for save prototypes */
do ch = readch(); while (ch == ' ');
if (ch == 'p' || ch == 'w')
{
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
if (ch == '*' || ch == '\n') /* nothing */ ; else newproto = readstr();
if (nonetwork() || noprotos()) break; else saveweights(newproto);
}
else texterror(1899);
break;
case 't':
if (nonetwork()) break;
do ch = readch(); while (ch == ' ');
if (ch == 'f')
{
do ch = readch(); while (ch == ' ');
bufferptr = bufferptr - 1;
testfile = readstr();
testlist = readpats(testfile,testlist);
ntestpats = listcount;
s[TEST].right = 0;
s[TEST].wrong = 0;
s[TEST].pctright = 0.0;
s[TEST].npats = ntestpats;
goto endt;
}
if (nonetwork() || noprotos() || nopatterns(TEST)) goto endt;
if (ch == '\n' || ch == '*') eval(TEST,(int) 1);
else if (ch == 'm') printmatrix(TEST);
else if (ch == '0') eval(TEST,(int) 0);
else if (ch >= '1' && ch <= '9')
{
bufferptr = bufferptr - 1;
itemp = readint(1,npats,'t');
if (!readerror) evalone(TEST,itemp);
}
else texterror(1928);
outofdate = 0;
endt: break;
default: texterror(1932);
break;
};
if (ch != '\n') do ch = readch(); while (ch != '\n');
}while (!finished);
}
void main(argc,argv)
int argc;
char *argv[];
{
FILE *tempfile;
setbuf(stdout,NULL); /* set unbuffered output */
lineno = 0;
pg("\n NN Copyright (c) 1995 by Donald R. Tveter\n");
pg(" drt@mcs.com - http://www.mcs.com/~drt/home.html\n");
pg(" October 14, 1995\n\n");
bufferend = 0;
bufferptr = BUFFSIZE + 1;
clusterstart = NULL;
closest = NULL;
copyflag = '-';
data = stdin;
/* debug = fopen("debug","w"); */
echo = '-';
emptystring = '\0';
filestack[0] = stdin;
filestackptr = 0;
initialprotos = NULL;
nearestfile = "nearest";
nprotos = 0;
pagesize = 24;
listpat = '+';
protolist = NULL;
summary = '+';
if (argc == 1)
{
pg("no data file, stdin assumed\n");
datafilename = &emptystring;
testfile = &emptystring;
}
else
{
datafilename = argv[1];
pushfile(datafilename);
if (argc == 3)
{
tempfile = fopen(argv[2],"r");
if (tempfile == NULL)
{
sprintf(outstr,"there is no file: %s\n",argv[2]);
pg(outstr);
testfile = &emptystring;
}
else
{
testfile = argv[2];
fclose(tempfile);
}
}
else testfile = &emptystring;
};
signal(SIGINT,restartcmdloop); /* restart from interrrupt */
cmdloop();
if (debug != NULL) fclose(debug);
if (copy != NULL) fclose(copy);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -