home *** CD-ROM | disk | FTP | other *** search
- /* 5:30 July 7, 1988 TSPIO.c TSP interface routine */
-
- /************************************************************************
- * Copyright(C) 1988-1989 NeuralWare Inc *
- * Penn Center West, IV-227, Pittsburgh, PA 15276 *
- * *
- * All rights reserved. No part of this program may be reproduced, *
- * stored in a retrieval system, or transmitted, in any form or by any *
- * means, electronic, mechanical, photocopying, recording or otherwise *
- * without the prior written permission of the copyright owner, *
- * NeuralWare, Inc. *
- ************************************************************************
- */
-
- #include "userutl.h"
- #ifndef SUN
- #ifndef DLC
- #include <stdlib.h>
- #endif
- #endif
- #include <string.h>
-
- #include "tsp.h"
-
- #ifdef MAC
- #include "macuio.redef"
- #endif
-
- /************************************************************************
- *
- * Traveling Sales Person Problem Interface
- *
- ************************************************************************
- */
-
- #define X 0
- #define Y 1
-
- #define CITY_WIDTH 3 /* Half width of a displayed city */
-
- static int city_pos[MAX_NUM_CITIES][2]; /* screen positions of cities */
- static int city_stop[MAX_NUM_CITIES]; /* itinerary index */
-
- /* window parameters */
- static int xstrt, ystrt, width = {200}, height, xmargin = {20}, ymargin;
-
- #define TSP_STEPSIZE 0
- #define TSP_NEXTSTEP 1
- #define TSP_QUIT 2
-
- char response[100];
- static CITY cities[MAX_NUM_CITIES], *city_ptr;
- static int num_cities;
-
- /* Local menu definitions */
- #define STPSZ_STR "Step size: ***"
- #define NXTST_STR "Next step"
- #define QUIT_STR "Quit"
-
- static GMENU_ITEM TspList[] = {
- { TSP_STEPSIZE, 0, 0 },
- { TSP_NEXTSTEP, 0, 0 },
- { TSP_QUIT, 0, 0 }
- };
-
- static GMENU TspMenu = {
- 0,
- 3, /* 3 items */
- 2, /* key */
- 0x0000
- };
-
- static int xmwdw,ymwdw; /* menu window position */
- static int stepsize = {10}; /* how often display is updated */
- static int curr_iteration, prev_iteration;
-
- extern double sqrt( );
-
- #ifdef DLC
- /* --- prototypes --- */
- double calc_itin( );
- get_line( FILE * );
- disp_itin( int, int );
- #endif
-
- get_line(fp)
- FILE *fp;
- {
- #ifdef MAC
- #define NEWLN 015
- #else
- #define NEWLN 012
- #endif
-
- int i;
- char c;
- int notempty=0;
-
- for (i=0;i<100;i++)
- {
- if ((response[i] = fgetc(fp)) == EOF)
- return EOF;
- if (response[i] == NEWLN) {
- if ( notempty == 0 ) {
- i = -1;
- continue;
- } else break;
- }
- if (response[i] == '!') /* comment */
- {
- /* skip to line end*/
- while(((c=fgetc(fp)) != NEWLN) && (c != EOF)) ;
- if (c == EOF)
- return EOF;
- if (i == 0 || notempty == 0)
- { /* if at beginning just skip */
- i = -1;
- continue;
- }
- else
- break; /* return to calling program */
- }
- /* Must be valid to fall through - flag non-empty line */
- if (response[i] > ' ' && response[i] <= 0x7f ) notempty = 1;
- }
- if (i==100)
- return EOF;
- response[i]='\0';
- return 0;
- }
-
- disp_itin( citycolor, linecolor )
- int citycolor;
- int linecolor;
- {
- int wx, wy, wz;
-
- /* Draw itinerary */
- for (wx = 0; wx < num_cities - 1; ) {
- wy = city_stop[wx++];
- wz = city_stop[wx];
- ug_line( 1, linecolor, 0,
- city_pos[wy][X], city_pos[wy][Y],
- city_pos[wz][X], city_pos[wz][Y], 0 );
- }
- /* Complete the tour */
- wy = city_stop[num_cities - 1];
- wz = city_stop[0];
- ug_line( 1, linecolor, 0,
- city_pos[wy][X], city_pos[wy][Y],
- city_pos[wz][X], city_pos[wz][Y], 0 );
-
- /* Display cities */
- for ( wx=0; wx < num_cities; wx++ )
- ug_boxf( 1, citycolor, 0,
- city_pos[wx][X]-CITY_WIDTH, city_pos[wx][Y]-CITY_WIDTH,
- city_pos[wx][X]+CITY_WIDTH, city_pos[wx][Y]+CITY_WIDTH);
- }
-
- double calc_itin( ) /* Calculate length of itinerary */
- {
- int wx, wy, wz;
- double dt; /* distance travelled */
- double xdiff, ydiff;
-
- dt = 0.0;
-
- for (wx = 0; wx < num_cities; ) {
- wy = city_stop[wx++];
- wz = city_stop[wx%num_cities];
- xdiff = cities[wz].xloc - cities[wy].xloc;
- ydiff = cities[wz].yloc - cities[wy].yloc;
- dt += sqrt( xdiff * xdiff + ydiff * ydiff );
- }
- return( dt );
- }
-
-
- int nocase_strcmp(str1,str2)
- char *str1;
- char *str2;
- {
- int diff;
- int mask;
-
- mask = ~('a' ^ 'A');
- diff = (*str1 & mask) - (*str2 & mask);
- while((*str1 != '\0') && (*str2 != '\0') && (diff == 0))
- {
- diff = (*str1 & mask) - (*str2 & mask);
- str1++;
- str2++;
- }
- return diff;
- }
- /* */
- /************************************************************************
- *
- * UsrIO - user I/O routine to handle requests from NWORKS
- *
- ************************************************************************
- */
-
- static int AbortFlag = 0;
- static int InitFlag = 0; /* initialize things flag */
-
- int UsrIO() /* handle NWORKS requests */
- {
- int wx, wy, wz; /* work indices */
- int button; /* mouse interface */
- int xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
- GMENU_ITEM *gmip;
- char *city_fname; /* city file name */
- int nlayp, ninp, noutp, ltype; /* Network parameters */
- char *csp; /* Control strategy pointer*/
- float xlow, xhigh, ylow, yhigh, diff, scl; /* world city params */
- float scrn_ratio; /* Aspect ratio of screen */
- float maxout;
- FILE *fp;
- char sp[60]; /* string pointer */
-
- if ( InitFlag == 0 ) {
- TspList[0].text = STPSZ_STR;
- TspList[1].text = NXTST_STR;
- TspList[2].text = QUIT_STR;
-
- TspMenu.item=TspList;
-
- InitFlag = 1;
- }
-
- IORTNCDE = 0; /* good return for data */
- switch( IOREQCDE ) {
- case RQ_ATTENTION:
- /* This is invoked at the request of the user from the
- execute menu. It allows parameters to be changed or
- altered. Any graphics or other interactions are allowable
- as usual for the other options.
- */
- break;
-
- case RQ_LSTART: /* starting learn */
- /* This tells the user that the program is about to start
- learning. It is called once for a LEARN ALL, LEARN ONE,
- LEARN N, or LEARN START
- */
- break;
-
- case RQ_LEARNOUT: /* read desired output */
- /* IODATA points to an empty array of IOCOUNT values. The
- elements of the array will become the desired outputs for
- training purposes. These desired outputs correspond to
- the most recent "RQ_LEARNIN" request.
- */
- case RQ_RCLTST: /* read desired output during recall test */
- /* IODATA points to an empty array of IOCOUNT values. The
- elements of the array will become the desired outputs for
- recall purposes. This request is only made during a
- Execute Network/Recall Test.
- */
- break;
-
- case RQ_LEARNRSLT:
- /* IODATA points to an array of IOCOUNT values. These are the
- output of the network caused by the inputs from RQ_LEARNIN.
- */
- break;
-
- case RQ_RSTART: /* starting recall */
- /* This tells the user that the program is about to start
- a recall. It is called once for a RECALL ALL, RECALL ONE,
- RECALL N, or RECALL START.
- */
- AbortFlag = 0;
- /* Read network parameters */
- ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &city_fname );
- /* Get city file information */
- if ((fp=fopen(city_fname,"r")) == NULL) {
- PutStr("Cannot find default city file; please enter file name:");
- city_fname = GetStr();
- PutStr("\n");
-
- if ((fp=fopen(city_fname,"r")) == NULL) {
- PutStr("Bad filename\n");
- IORTNCDE = -1;
- AbortFlag = 1;
- return(0);
- }
- }
- do {
- if (get_line(fp) == EOF) {
- PutStr("Unexpected EOF found\n");
- IORTNCDE = -1;
- AbortFlag = 1;
- fclose(fp);
- return(0);
- }
- } while (nocase_strcmp(response,"CITIES") != 0);
-
- num_cities = 0;
- city_ptr = cities;
- while(1) {
- if (get_line(fp) == EOF) break;
- if (nocase_strcmp(response,"DISTANCES") == 0) break;
- #ifdef THINK_C
- sscanf(response," %s %Lf %Lf",
- city_ptr->name,
- &city_ptr->xloc,
- &city_ptr->yloc);
- #else
- sscanf(response," %s %lf %lf",
- city_ptr->name,
- &city_ptr->xloc,
- &city_ptr->yloc);
- #endif
- city_ptr++;
- num_cities++;
- }
-
- fclose(fp);
-
- if ( num_cities * num_cities != noutp ) {
- PutStr("Mismatch between city file and network\n");
- IORTNCDE = -1;
- AbortFlag = 1;
- fclose(fp);
- return(0);
- }
-
- /* Calculate city coordinate extremes */
- city_ptr = cities;
- xlow = xhigh = city_ptr->xloc;
- ylow = yhigh = city_ptr->yloc;
- for ( wx = 1; wx < num_cities; wx++ ) {
- city_ptr++;
- if (xlow > city_ptr->xloc ) xlow = city_ptr->xloc;
- if (xhigh < city_ptr->xloc ) xhigh = city_ptr->xloc;
- if (ylow > city_ptr->yloc ) ylow = city_ptr->yloc;
- if (yhigh < city_ptr->yloc ) yhigh = city_ptr->yloc;
- }
- diff = (xhigh - xlow) - (yhigh - ylow);
-
- if ( diff > 0.0 ) {
- ylow = ylow - diff/2.0;
- yhigh = yhigh + diff/2.0;
- } else {
- xlow = xlow + diff/2.0;
- xhigh = xhigh - diff/2.0;
- }
- scl = xhigh - xlow;
- if ( scl < 0.01 ) scl = 0.01;
-
- /* Get screen parameters */
- ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
-
- /* Assume screen aspect ratio of 4:3 */
- scrn_ratio = (4.0 * ysize) / ( 3.0 * xsize );
- height = width * scrn_ratio;
- ymargin = xmargin * scrn_ratio;
- xstrt = (xsize - width) / 2 - xmargin;
- ystrt = (ysize - height) / 2 - ymargin;
-
- /* Calculate city screen coordinates */
- for ( wx=0, city_ptr=cities; wx < num_cities; wx++, city_ptr++ ) {
- city_pos[wx][X] =
- xmargin + (int) ( (city_ptr->xloc - xlow) * width / scl );
- city_pos[wx][Y] =
- ymargin + (int) ( (city_ptr->yloc - ylow) * height / scl );
- }
-
- if ( ncolor < 8 ) {
- gm_intcolor = 1;
- gm_txtcolor = 0;
- gm_outcolor = 0;
- } else {
- gm_intcolor = 7;
- gm_txtcolor = 4;
- gm_outcolor = 0;
- }
-
- InitGMenu( &TspMenu, chrx, chry );
- xmwdw = (xsize - TspMenu.x1) / 2;
- ymwdw = ystrt / 2;
- TspMenu.x0 = 0; /* Menu position relative to window */
- TspMenu.y0 = 0;
-
- /* Unlock all menu items */
- for (gmip = TspMenu.item, wx = 0; wx < TspMenu.num_items;
- gmip++, wx++ )
- gmip->flag &= ~GM_LOCKED;
-
- /* Other initializations */
- sprintf( sp, "%3ld", (long)stepsize );
- strncpy( &TspList[TSP_STEPSIZE].text[11], sp, 3 );
- for ( wx = 0; wx < num_cities; wx++ ) city_stop[wx] = wx;
- curr_iteration = prev_iteration = 0;
-
- /* define windows */
- ug_window( 1, gm_intcolor, xstrt, ystrt,
- xstrt + 2*xmargin + width - 1,
- ystrt + 2*ymargin + height - 1 );
- ug_boxf( 1, gm_outcolor, 0, 0, 0,
- 2*xmargin + width - 1,
- 2*ymargin + height - 1 );
- ug_boxf( 1, gm_intcolor, 0, 3, 3,
- 2*xmargin + width - 4,
- 2*ymargin + height - 4 );
-
-
- ug_window( TspMenu.key, gm_intcolor, xmwdw, ymwdw,
- xmwdw + TspMenu.x1 + 2, ymwdw + TspMenu.y1 + 2);
-
- DispGMenu( &TspMenu );
-
- break;
-
- case RQ_LEARNIN: /* read training input */
- /* IODATA points to an empty array of IOCOUNT elements. The
- values placed in this array by the user will become the
- inputs to the network for training purposes.
- */
- break;
-
- case RQ_READ: /* read test data */
- /* IODATA points to an empty array of IOCOUNT values. The
- user must fill in these values. The elements of the
- array will become the "sum" of the inputs to the input
- layer of processing elements.
- */
- break;
-
- case RQ_WRSTEP: /* write interim results */
- /* each recall cycle for the Hopfield and BAM control strategies,
- the intermediate output is made available to userio to test
- for convergence or other desired states. This option is
- not used for other control strategies.
- */
- if (AbortFlag) {
- IORTNCDE = -1;
- return(0);
- }
- curr_iteration++;
- if( curr_iteration - prev_iteration == stepsize ) {
- prev_iteration = curr_iteration;
- sprintf( sp, "Iteration number %3ld\n", (long)curr_iteration );
- PutStr( sp );
-
- disp_itin( gm_intcolor, gm_intcolor ); /* Erase itinerary */
- /* Calculate city stops */
- for ( wx = 0; wx < num_cities; wx++ ) {
- maxout = -1.0;
- for ( wy = wx, wz = 0; wz < num_cities; wz++, wy += num_cities) {
- if ( IODATA[wy] >= maxout ) {
- maxout = IODATA[wy];
- city_stop[wx] = wz;
- }
- }
- }
- disp_itin( gm_txtcolor, gm_txtcolor );
- sprintf( sp, "Length of itinerary = %f\n", calc_itin( ) );
- PutStr( sp );
-
- for ( ; ; )
- {
- while ( (gmip=LookGMenu(&TspMenu,&button )) == (GMENU_ITEM *) 0) ;
- switch( gmip->code )
- {
- case TSP_STEPSIZE:
- #ifdef MAC
- if (button == MBUT_LEFT)
- #else
- if (button == MBUT_RIGHT)
- #endif
- stepsize++;
- #ifdef MAC
- if (button == MBUT_RIGHT)
- #else
- if (button == MBUT_LEFT)
- #endif
- {
- stepsize--;
- if ( stepsize < 1 ) stepsize = 1;
- }
- if (button == MBUT_LEFT || button == MBUT_RIGHT)
- {
- sprintf( sp, "%3ld", (long)stepsize );
- strncpy( &TspList[TSP_STEPSIZE].text[11], sp, 3 );
- DispGItem( &TspMenu, gmip, gm_intcolor, gm_txtcolor );
- }
- break;
- case TSP_NEXTSTEP:
- if ( button == MBUT_RIGHT || button == MBUT_LEFT )
- {
- goto end_wr;
- }
- break;
- case TSP_QUIT:
- if ( button == MBUT_RIGHT || button == MBUT_LEFT )
- {
- IORTNCDE = -1;
- goto end_wr;
- }
- break;
- }
- }
- }
- end_wr:
- break;
-
- case RQ_WRITE: /* write out results */
- /* IODATA points to an array of IOCOUNT "float" type values.
- The values are the outputs of the top-most layer of the
- network.
- */
-
- if (AbortFlag) {
- IORTNCDE = -1;
- return(0);
- }
- /* Calculate final itinerary */
- for ( wx = 0; wx < num_cities; wx++ ) {
- maxout = -1.0;
- for ( wy = wx, wz = 0; wz < num_cities; wz++, wy += num_cities) {
- if ( IODATA[wy] >= maxout ) {
- maxout = IODATA[wy];
- city_stop[wx] = wz;
- }
- }
- }
- disp_itin( gm_txtcolor, gm_txtcolor );
- PutStr("Final Itinerary\n");
- TspList[TSP_STEPSIZE].flag |= GM_LOCKED;
- TspList[TSP_NEXTSTEP].flag |= GM_LOCKED;
-
- for ( ; ; ) {
- while ( (gmip=LookGMenu(&TspMenu,&button )) == (GMENU_ITEM *) 0) ;
- if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
- IORTNCDE = -1;
- goto end_write;
- }
- }
- end_write:
- break;
-
-
- case RQ_TERM: /* terminate interface */
- /* close any files which may be open. Deallocate any memory
- which was allocated. (This is VERY VERY important. If
- allocated memory is NOT released, dos memory will become
- fragmented and it will become necessary to reboot.
- */
-
- PutStr( "bye bye" );
- break;
- }
-
- return;
- }
-