1 /*****************************************************************************
2   FILE           : $Source: /projects/higgs1/SNNS/CVS/SNNS/xgui/sources/bn_JordElm.c,v $
3   SHORTNAME      : bn_JordElm.c
4   SNNS VERSION   : 4.2
5 
6   PURPOSE        : Bignet panel for Jordan and Elman networks
7   NOTES          :
8 
9   AUTHOR         : Tobias Soyez
10   DATE           : 09.11.1993
11 
12   CHANGED BY     :
13   RCS VERSION    : $Revision: 2.13 $
14   LAST CHANGE    : $Date: 1998/03/03 14:09:36 $
15 
16     Copyright (c) 1990-1995  SNNS Group, IPVR, Univ. Stuttgart, FRG
17     Copyright (c) 1996-1998  SNNS Group, WSI, Univ. Tuebingen, FRG
18 
19 ******************************************************************************/
20 #include <config.h>
21 #include <X11/Intrinsic.h>
22 #include <X11/Shell.h>
23 #include <X11/Xaw3d/Box.h>
24 #include <X11/Xaw3d/Form.h>
25 
26 #include "ui.h"
27 #include "kr_ui.h"
28 #include "ui_mainP.h"
29 #include "ui_xWidgets.h"
30 #include "ui_control.h"
31 #include "ui_confirmer.h"
32 #include "bn_basics.h"
33 
34 #include "bn_JordElm.ph"
35 
36 
37 
38 /*****************************************************************************
39   FUNCTION : createUnitLayer
40 
41   PURPOSE  : creates a layer of no_of_units units
42   RETURNS  : the number of the first unit of the created layer or a kernel
43              error code
44   NOTES    :
45 
46   UPDATE   :
47 ******************************************************************************/
createUnitLayer(int no_of_units,int io_type,char * act_func_name,char * out_func_name,char * unit_name)48 static int createUnitLayer (int  no_of_units,
49   			    int  io_type,
50 		    	    char *act_func_name,
51 			    char *out_func_name,
52 			    char *unit_name)
53 
54 {
55     krui_err     error_code = KRERR_NO_ERROR ;
56     int          i, unit_no, first_unit ;
57     char         name[10] ;
58 
59 
60     if (no_of_units < 1) return (KRERR_NO_UNITS) ;
61 
62     for (i = 1 ; i <= no_of_units ; i++){
63 	sprintf (name, "%s%d", unit_name, i) ;
64 
65 	unit_no = krui_createUnit (name, out_func_name, act_func_name, 0, 0) ;
66 	if (unit_no < 0) IF_ERROR_RETURN (unit_no) ;
67 
68 	if (i == 1) first_unit = unit_no ;
69 
70 	error_code = krui_setUnitTType (unit_no, io_type) ;
71 
72 	IF_ERROR_RETURN (error_code) ;
73     }
74 
75     return (first_unit) ;
76 }
77 
78 
79 
80 /*****************************************************************************
81   FUNCTION : connectFull
82 
83   PURPOSE  : connects every unit of one set (no_of_source_units units,
84              beginning with from_unit) to every unit of a second set
85              no_of_target_units units, beginning with to_unit)
86   RETURNS  : kernel error code
87   NOTES    :
88 
89   UPDATE   :
90 ******************************************************************************/
connectFull(int from_unit,int no_of_source_units,int to_unit,int no_of_target_units,FlintType weight)91 static krui_err connectFull (int from_unit, int no_of_source_units,
92 			     int to_unit,   int no_of_target_units,
93 			     FlintType weight)
94 {
95     krui_err  error_code = KRERR_NO_ERROR ;
96     int       i, j ;
97 
98 
99     for (i = to_unit ; i < to_unit + no_of_target_units ; i++){
100 	error_code = krui_setCurrentUnit (i) ;
101 	IF_ERROR_RETURN (error_code) ;
102 
103 	for (j = from_unit ; j < from_unit + no_of_source_units ; j++){
104 	    error_code = krui_createLink (j, weight) ;
105 	    IF_ERROR_RETURN (error_code) ;
106 	}
107     }
108 
109     return (error_code) ;
110 }
111 
112 
113 
114 /*****************************************************************************
115   FUNCTION : connectOneToOne
116 
117   PURPOSE  : connects every unit of one set (no_of_units units, beginning
118              with from_unit) to the corresponding unit in the second set
119              (no_of_units units, beginning with to_unit)
120   RETURNS  : kernel error code
121   NOTES    :
122 
123   UPDATE   :
124 ******************************************************************************/
connectOneToOne(int from_unit,int to_unit,int no_of_units,FlintType weight)125 static krui_err connectOneToOne (int from_unit, int to_unit,
126 			         int no_of_units, FlintType weight)
127 {
128     krui_err  error_code = KRERR_NO_ERROR ;
129     int       i ;
130 
131 
132     for (i = 0 ; i < no_of_units ; i++){
133 	error_code = krui_setCurrentUnit (to_unit + i) ;
134 	IF_ERROR_RETURN (error_code) ;
135 
136 	error_code = krui_createLink (from_unit + i, weight) ;
137 	IF_ERROR_RETURN (error_code) ;
138     }
139 
140     return (error_code) ;
141 }
142 
143 
144 
145 /*****************************************************************************
146   FUNCTION : connectSelfRec
147 
148   PURPOSE  : connects every unit of a set (no_of_units units, beginning with
149              unit_no) to itself
150   RETURNS  : kernel error code
151   NOTES    :
152 
153   UPDATE   :
154 ******************************************************************************/
connectSelfRec(int unit_no,int no_of_units,FlintType weight)155 static krui_err connectSelfRec (int unit_no, int no_of_units, FlintType weight)
156 {
157     krui_err  error_code = KRERR_NO_ERROR ;
158     int       i ;
159 
160 
161     for (i = unit_no ; i < unit_no + no_of_units ; i++)
162     {
163 	error_code = krui_setCurrentUnit (i) ;
164 	IF_ERROR_RETURN (error_code) ;
165 
166 	error_code = krui_createLink (i, weight) ;
167 	IF_ERROR_RETURN (error_code) ;
168     }
169     return (error_code) ;
170 }
171 
172 
173 
174 /*****************************************************************************
175   FUNCTION : setPosRow
176 
177   PURPOSE  : sets the positions of a set of units (no_of_units units,
178              beginning with unit_no) starting at (x, y) with max. r units
179              per row
180   RETURNS  : void
181   NOTES    :
182 
183   UPDATE   :
184 ******************************************************************************/
setPosRow(int unit_no,int no_of_units,int x,int y,int r)185 static void setPosRow (int unit_no, int no_of_units, int x, int y, int r)
186 {
187     struct PosType  unit_pos ;
188     int             col, row ;
189     int             i ;
190 
191 
192     col = x ;
193     row = y ;
194 
195     for (i = unit_no ; i < unit_no + no_of_units ; i++){
196 	unit_pos.x = col ;
197 	unit_pos.y = row ;
198 	krui_setUnitPosition (i, &unit_pos) ;
199 	col++ ;
200 	if ((col - x) == r){
201 	    col = x ;
202 	    row++   ;
203 	}
204     }
205 }
206 
207 
208 
209 /*****************************************************************************
210   FUNCTION : fixWindowSize
211 
212   PURPOSE  : fixes the window size
213   RETURNS  : void
214   NOTES    :
215 
216   UPDATE   :
217 ******************************************************************************/
fixWindowSize(Widget window)218 static void fixWindowSize (Widget window)
219 {
220     Dimension  width, height ;
221     int        n ;
222     Arg        args[10] ;
223 
224 
225     if (bn_elman_open != 0)
226 	XtMapWidget (baseWidgetElman) ;
227     if (bn_jordan_open != 0)
228 	XtMapWidget (baseWidgetJordan) ;
229     n = 0 ;
230     XtSetArg    (args[n], XtNwidth , &width ) ; n++ ;
231     XtSetArg    (args[n], XtNheight, &height) ; n++ ;
232     XtGetValues (window , args, n) ;
233 
234     n = 0 ;
235     XtSetArg    (args[n], XtNminWidth , width ) ; n++ ;
236     XtSetArg    (args[n], XtNminHeight, height) ; n++ ;
237     XtSetArg    (args[n], XtNmaxWidth , width ) ; n++ ;
238     XtSetArg    (args[n], XtNmaxHeight, height) ; n++ ;
239 
240     XtSetValues (window , args, n) ;
241 }
242 
243 
244 
245 /*****************************************************************************
246   FUNCTION : bn_jordan_createNet
247 
248   PURPOSE  : generates the JORDAN network
249   RETURNS  : kernel error code
250   NOTES    :
251 
252   UPDATE   :
253 ******************************************************************************/
bn_jordan_createNet(int IUnits,int HUnits,int OUnits,int ICols,int HCols,int OCols)254 static krui_err bn_jordan_createNet (int IUnits, int HUnits, int OUnits,
255                                      int ICols , int HCols , int OCols )
256 {
257   krui_err  error_code = KRERR_NO_ERROR;
258   int       i_unit, h_unit, o_unit, s_unit ;
259   int       idy, hdy, maxdy ;
260 
261 
262   /* ---------------------------   create units  ----------------------------*/
263 
264   i_unit = createUnitLayer(IUnits,INPUT,"Act_Identity","Out_Identity","inp");
265   if (i_unit < 0) IF_ERROR_RETURN (i_unit) ;
266 
267   h_unit = createUnitLayer(HUnits,HIDDEN,"Act_Logistic","Out_Identity","hid") ;
268   if (h_unit < 0) IF_ERROR_RETURN (h_unit) ;
269 
270   o_unit = createUnitLayer(OUnits,OUTPUT,"Act_Logistic","Out_Identity","out") ;
271   if (o_unit < 0) IF_ERROR_RETURN (o_unit) ;
272 
273   s_unit =createUnitLayer(OUnits,SPECIAL_H,"Act_Identity","Out_Identity","con");
274   if (s_unit < 0) IF_ERROR_RETURN (s_unit) ;
275 
276 
277   /* ---------------------------   create links  ----------------------------*/
278 
279   error_code = connectFull     (i_unit, IUnits, h_unit, HUnits, 0.0) ;
280   IF_ERROR_RETURN (error_code) ;
281 
282   error_code = connectFull     (h_unit, HUnits, o_unit, OUnits, 0.0) ;
283   IF_ERROR_RETURN (error_code) ;
284 
285   error_code = connectFull     (s_unit, OUnits, h_unit, HUnits, 0.0) ;
286   IF_ERROR_RETURN (error_code) ;
287 
288   error_code = connectOneToOne (o_unit, s_unit, OUnits, 0.0) ;
289   IF_ERROR_RETURN (error_code) ;
290 
291   error_code = connectSelfRec  (s_unit, OUnits, 0.0) ;
292   IF_ERROR_RETURN (error_code) ;
293 
294 
295   /* -------------  calculate y-coordinate of state layer  ------------------*/
296 
297   idy = (int) ((IUnits + ICols - 1) / ICols) ;
298   hdy = (int) ((HUnits + HCols - 1) / HCols) ;
299 
300   if (idy > hdy) maxdy = idy ;
301   else           maxdy = hdy ;
302 
303 
304   /*  --------------------------  set positions  ----------------------------*/
305 
306   setPosRow (i_unit, IUnits, 1                  , 1        , ICols) ;
307   setPosRow (h_unit, HUnits, 5+ICols+OCols      , 1        , HCols) ;
308   setPosRow (o_unit, OUnits, 9+ICols+HCols+OCols, 1        , OCols) ;
309   setPosRow (s_unit, OUnits, 3+ICols            , 1 + maxdy, OCols) ;
310 
311 
312   /*  -----------------------  set default functions  -----------------------*/
313 
314   error_code = krui_setLearnFunc ("JE_BP") ;
315   IF_ERROR_RETURN (error_code) ;
316 
317   error_code = krui_setInitialisationFunc ("JE_Weights") ;
318   IF_ERROR_RETURN (error_code) ;
319 
320   ui_NumberOfLearnParamsChanged();
321   ui_NumberOfUpdateParamsChanged();
322   ui_NumberOfInitParamsChanged();
323 
324   error_code = krui_setUpdateFunc ("JE_Order") ;
325 
326   return (error_code) ;
327 }
328 
329 
330 
331 /*****************************************************************************
332   FUNCTION : bn_jordan_donePROC
333 
334   PURPOSE  : callback function for DONE button in 'Bignet (Jordan)'
335   NOTES    :
336 
337   UPDATE   :
338 ******************************************************************************/
bn_jordan_donePROC(void)339 static void bn_jordan_donePROC (void)
340 {
341    XtDestroyWidget (baseWidgetJordan) ;
342    bn_jordan_open = 0 ;
343 }
344 
345 
346 
347 /*****************************************************************************
348   FUNCTION : bn_jordan_createPROC
349 
350   PURPOSE  : callback function for CREATE NET button in 'BigNet (Jordan)'
351   NOTES    :
352 
353   UPDATE   :
354 ******************************************************************************/
bn_jordan_createPROC(void)355 static void bn_jordan_createPROC (void)
356 {
357   int  units[3], cols[3] ;
358   int  i ;
359   char buf[80] ;
360 
361 
362   if (bn_basics_check_existingNetwork())
363   {
364     /* -------------------------  get values  ------------------------------ */
365 
366     for (i=0; i<3; i++)
367     {
368       units[i] = ui_xIntFromAsciiWidget (jordanUnitWidget[i]) ;
369       cols[i]  = ui_xIntFromAsciiWidget (jordanColWidget[i])  ;
370     } /*for*/
371 
372 
373     /* ------------------------  check values  ----------------------------- */
374 
375     for (i=0; i<3; i++)
376     {
377       if (units[i] < 1)
378       {
379         sprintf (buf,"Number of units has to be greater than 0 in line %d!",
380                  i + 1) ;
381         ui_confirmOk (buf);
382         return ;
383       }
384 
385       if (cols[i] < 1)
386       {
387         sprintf (buf,"Number of cols has to be greater than 0 in line %d!",
388                  i + 1);
389         ui_confirmOk (buf) ;
390         return ;
391       }
392     }
393 
394 
395     /* -------------------------  create net  ------------------------------ */
396 
397     if (bn_jordan_createNet (units[0], units[1], units[2], cols[0] ,
398                              cols[1] , cols[2] ) != KRERR_NO_ERROR)
399       ui_confirmOk ("create net :  internal error") ;
400 
401     bn_basics_refresh () ;
402   }
403 
404   return ;
405 }
406 
407 
408 
409 
410 /*****************************************************************************
411   FUNCTION : bn_create_jordan
412 
413   PURPOSE  : creates the 'BigNet (Jordan)' window
414   RETURNS  : void
415   NOTES    :
416 
417   UPDATE   :
418 ******************************************************************************/
419 
bn_create_jordan(void)420 void bn_create_jordan(void)
421 
422 {
423     Widget      jordan_panel, jordan_form, button, w_left, w_top ;
424     int         i;
425     char        Name[4][20];
426 
427 
428     strcpy (Name[0], "Input  Layer: ") ;
429     strcpy (Name[1], "Hidden Layer: ") ;
430     strcpy (Name[2], "Output Layer: ") ;
431 
432 
433     if(bn_jordan_open) {
434 	XRaiseWindow (XtDisplay (baseWidgetJordan),
435 		      XtWindow (baseWidgetJordan));
436 	return;
437     }
438     baseWidgetJordan   =
439 	XtCreatePopupShell ("SNNS BigNet (Jordan)", topLevelShellWidgetClass,
440 			    ui_toplevel, NULL, 0) ;
441 
442     jordan_form  =
443 	XtCreateManagedWidget ("box" , boxWidgetClass , baseWidgetJordan ,
444 			       NULL, 0) ;
445 
446     jordan_panel =
447 	XtCreateManagedWidget ("form", formWidgetClass, jordan_form, NULL, 0) ;
448 
449 
450     /* ----------------------------  headline  -----------------------------*/
451 
452     w_left = w_top =
453 	ui_xCreateLabelItem (" "           , jordan_panel, 12*8, NULL  , NULL);
454     w_left =
455 	ui_xCreateLabelItem ("No. of Units", jordan_panel, 12*8, w_left, NULL);
456     w_left =
457 	ui_xCreateLabelItem ("No. of Col." , jordan_panel, 12*8, w_left, NULL);
458 
459 
460     /* --------------------------  dialog rows  ----------------------------*/
461 
462     for (i = 0 ; i < 3 ; i++)
463     {
464 	w_left =
465 	    ui_xCreateLabelItem  (Name[i], jordan_panel, 12*8, NULL  , w_top) ;
466 
467 	w_left =
468 	    ui_xCreateLabelItem  (" "    , jordan_panel,  2*8, w_left, w_top) ;
469 
470 	w_left = jordanUnitWidget[i] =
471 	    ui_xCreateDialogItem (" ", jordan_panel, "" , 6*8, w_left, w_top) ;
472 
473 	w_left =
474 	    ui_xCreateLabelItem  (" "    , jordan_panel,  5*8, w_left, w_top) ;
475 
476 	w_left = jordanColWidget[i]  =
477 	    ui_xCreateDialogItem (" ", jordan_panel, "1", 6*8, w_left, w_top) ;
478 
479 	w_top  = w_left ;
480     }
481 
482 
483     /* -----------------------------  buttons  -----------------------------*/
484 
485     button = ui_xCreateButtonItem ("create_net", jordan_form, NULL  , w_top) ;
486     XtAddCallback(button,XtNcallback,(XtCallbackProc)bn_jordan_createPROC,NULL);
487 
488     button = ui_xCreateButtonItem ("done"      , jordan_form, button, w_top) ;
489     XtAddCallback(button, XtNcallback,(XtCallbackProc)bn_jordan_donePROC, NULL);
490 
491     ui_checkWindowPosition(baseWidgetJordan) ;
492     XtPopup (baseWidgetJordan, XtGrabNone) ;
493 
494     bn_jordan_open = 1 ;
495 
496     fixWindowSize (baseWidgetJordan) ;
497 
498 
499 }
500 
501 
502 
503 
504 /*****************************************************************************
505   FUNCTION : bn_elman_createNet
506 
507   PURPOSE  : generates the ELMAN network
508   RETURNS  : kernel error code
509   NOTES    :
510 
511   UPDATE   :
512 ******************************************************************************/
513 
bn_elman_createNet(void)514 static krui_err bn_elman_createNet (void)
515 
516 {
517   krui_err  error_code = KRERR_NO_ERROR;
518   int       i_unit, o_unit ;
519   int       h_unit[MAX_NO_OF_LAYERS], c_unit[MAX_NO_OF_LAYERS] ;
520   int       i, no_of_context_layers ;
521   int       no_of_hidden_layers, o_layer ;
522   int       dx, cx, cy ;
523   char      name[10] ;
524 
525 
526   no_of_hidden_layers = no_of_layers - 2 ;
527   o_layer             = no_of_layers - 1 ;   /* no. of the output layer */
528 
529 
530   /* ---------------------------   create units  ----------------------------*/
531 
532   i_unit = createUnitLayer (layer[0], INPUT, "Act_Identity",
533                             "Out_Identity", "inp") ;
534   if (i_unit < 0) IF_ERROR_RETURN (i_unit) ;
535 
536   strcpy (name, "hid") ;
537   for (i = 1 ; i <= no_of_hidden_layers ; i++)
538   {
539     if (no_of_hidden_layers > 1) sprintf (name, "hid%d", i) ;
540     h_unit[i] = createUnitLayer(layer[i], HIDDEN, "Act_Logistic",
541                                 "Out_Identity", name) ;
542     if (h_unit[i] < 0) IF_ERROR_RETURN (h_unit[i]) ;
543   }
544 
545   o_unit = createUnitLayer (layer[o_layer], OUTPUT,  "Act_Logistic",
546                             "Out_Identity", "out") ;
547   if (o_unit < 0) IF_ERROR_RETURN (o_unit) ;
548 
549   if (out_context)
550   {
551     no_of_context_layers = no_of_hidden_layers + 1 ;
552     h_unit[no_of_context_layers] = o_unit ;
553   }
554   else
555     no_of_context_layers = no_of_hidden_layers ;
556 
557   strcpy (name, "con") ;
558   for (i = 1 ; i <= no_of_context_layers ; i++)
559   {
560     if (no_of_context_layers > 1) sprintf (name, "con%d", i) ;
561     c_unit[i] = createUnitLayer (layer[i], SPECIAL_H, "Act_Identity",
562                                  "Out_Identity", name ) ;
563     if (c_unit[i] < 0) IF_ERROR_RETURN (c_unit[i]) ;
564   }
565 
566 
567   /* ---------------------------   create links  ----------------------------*/
568 
569   error_code = connectFull (i_unit, layer[0], h_unit[1], layer[1], 0.0) ;
570   IF_ERROR_RETURN (error_code) ;
571 
572   for (i = 1 ; i < no_of_hidden_layers ; i++)
573   {
574     error_code =
575       connectFull (h_unit[i], layer[i], h_unit[i+1], layer[i+1], 0) ;
576     IF_ERROR_RETURN (error_code) ;
577   }
578 
579   error_code = connectFull (h_unit[i], layer[i], o_unit, layer[o_layer], 0) ;
580   IF_ERROR_RETURN (error_code) ;
581 
582   for (i = 1 ; i <= no_of_context_layers ; i++)
583   {
584     error_code = connectFull (c_unit[i], layer[i],  h_unit[i], layer[i], 0) ;
585     IF_ERROR_RETURN (error_code) ;
586 
587     error_code = connectOneToOne (h_unit[i], c_unit[i], layer[i], 0) ;
588     IF_ERROR_RETURN (error_code) ;
589 
590     error_code = connectSelfRec (c_unit[i], layer[i], 0) ;
591     IF_ERROR_RETURN (error_code) ;
592   }
593 
594 
595   /* --------------------------   set positions  ----------------------------*/
596 
597   cx = 1 ; /* position of the upper left */
598   cy = 1 ; /* corner of a context layer  */
599 
600   for (i = 0 ; i < no_of_layers ; i++)
601   {
602     if ((int) (layer[i] / columns[i]) > cy - 1)
603       cy = 1 + (int) (layer[i] / columns[i]) ;
604   }
605 
606   cy += 1 ;
607 
608   dx  = 1 ; /* x position of the upper left corner of an input, */
609             /* hidden or output layer                           */
610 
611   setPosRow (i_unit, layer[0], 1, 1, columns[0]) ;
612 
613   cx = dx + columns[0] + 2 ;
614   dx = cx + columns[1] + 2 ;
615 
616   for (i = 1 ; i <= no_of_context_layers ; i++)
617   {
618     setPosRow (h_unit[i], layer[i],  dx, 1 , columns[i]) ;
619     setPosRow (c_unit[i], layer[i],  cx, cy, columns[i]) ;
620 
621     cx = dx + columns[i]   + 2 ;
622     dx = cx + columns[i+1] + 2 ;
623   }
624 
625   if (! out_context)
626     setPosRow (o_unit, layer[o_layer], dx, 1, columns[o_layer]) ;
627 
628 
629   /* ----------------------   set default functions  ------------------------*/
630 
631   error_code = krui_setLearnFunc ("JE_BP") ;
632   IF_ERROR_RETURN (error_code) ;
633 
634   error_code = krui_setInitialisationFunc ("JE_Weights") ;
635   IF_ERROR_RETURN (error_code) ;
636 
637   error_code = krui_setUpdateFunc ("JE_Order") ;
638 
639   ui_NumberOfLearnParamsChanged();
640   ui_NumberOfUpdateParamsChanged();
641   ui_NumberOfInitParamsChanged();
642   return (error_code) ;
643 }
644 
645 
646 
647 /*****************************************************************************
648   FUNCTION : bn_elman_getFromWidget
649 
650   PURPOSE  : get values from 'BigNet (Elman)' window
651   RETURNS  : void
652   NOTES    :
653 
654   UPDATE   :
655 ******************************************************************************/
656 
bn_elman_getFromWidget(void)657 static void bn_elman_getFromWidget (void)
658 
659 {
660   int i ;
661 
662   for (i = 0 ; i < no_of_layers ; i++)
663   {
664     layer[i]   = ui_xIntFromAsciiWidget (elmanUnitWidget[i]) ;
665     columns[i] = ui_xIntFromAsciiWidget (elmanColWidget[i] ) ;
666   }
667 
668   out_context = ui_xGetToggleState (t_yes) ;
669 }
670 
671 
672 
673 /*****************************************************************************
674   FUNCTION : bn_elman_donePROC
675 
676   PURPOSE  : callback function for DONE button in 'BigNet (Elman)'
677   RETURNS  : void
678   NOTES    :
679 
680   UPDATE   :
681 ******************************************************************************/
682 
bn_elman_donePROC(void)683 static void bn_elman_donePROC (void)
684 
685 {
686    XtDestroyWidget (baseWidgetElman) ;
687    bn_elman_open = 0 ;
688 }
689 
690 
691 
692 /*****************************************************************************
693   FUNCTION : bn_elman_createPROC
694 
695   PURPOSE  : callback function for CREATE NET button in 'BigNet (Elman)'
696   RETURNS  : void
697   NOTES    :
698 
699   UPDATE   :
700 ******************************************************************************/
701 
bn_elman_createPROC(void)702 static void bn_elman_createPROC (void)
703 
704 {
705   int  i       ;
706   char buf[80] ;
707 
708 
709   if (bn_basics_check_existingNetwork())
710   {
711     bn_elman_getFromWidget () ;
712 
713     for (i = 0 ; i < no_of_layers ; i++)
714     {
715 
716       /* -------------------------  check values  -------------------------- */
717 
718       if (layer[i] < 1)
719       {
720         sprintf (buf, "Number of units has to be greater than 0 in line %d!",
721                  i + 1) ;
722         ui_confirmOk (buf) ;
723         return ;
724       }
725 
726       if (columns[i] < 1)
727       {
728         sprintf (buf, "Number of columns has to be greater than 0 in line %d!",
729                  i + 1) ;
730         ui_confirmOk (buf) ;
731         return ;
732       }
733     }
734 
735    if (bn_elman_createNet () != KRERR_NO_ERROR)
736      ui_confirmOk ("create net :  internal error") ;
737 
738    bn_basics_refresh  () ;
739   }
740 
741   return ;
742 }
743 
744 
745 
746 /*****************************************************************************
747   FUNCTION : bn_elman_create_dialogrow
748 
749   PURPOSE  : creates a dialog row in the 'BigNet (Elman)' window
750   RETURNS  : void
751   NOTES    :
752 
753   UPDATE   :
754 ******************************************************************************/
755 
bn_elman_create_dialogrow(int i,char * str)756 static void bn_elman_create_dialogrow (int i, char *str)
757 
758 {
759   Widget w_top, w_left ;
760   char   buf[256] ;
761 
762 
763   if (i == 0) w_top = w_headline ;
764   else        w_top = elmanLabelWidget [i - 1] ;
765 
766   sprintf (buf, "  %3d      %s", i+1, str) ;
767 
768   w_left = elmanLabelWidget[i] =
769     ui_xCreateLabelItem (buf, elman_form[0], 19 * 8, NULL, w_top) ;
770 
771   if (layer[i] == 0) strcpy  (buf, "") ;
772   else               sprintf (buf, "%d", layer[i]) ;
773 
774   w_left = elmanUnitWidget[i] =
775     ui_xCreateDialogItem (" ", elman_form[0], buf, 6 * 8, w_left, w_top) ;
776 
777   w_left = elmanSpaceWidget[i] =
778     ui_xCreateLabelItem  (" ", elman_form[0], 5 * 8, w_left, w_top) ;
779 
780   if (columns[i] == 0) strcpy  (buf, "") ;
781   else                 sprintf (buf, "%d", columns[i]) ;
782 
783   w_left = w_top = elmanColWidget[i]  =
784     ui_xCreateDialogItem (" ", elman_form[0], buf, 6 * 8, w_left, w_top) ;
785 
786 }
787 
788 
789 
790 /*****************************************************************************
791   FUNCTION : bn_elman_yesPROC
792 
793   PURPOSE  : callback function for YES button in 'BigNet (Elman)'
794   RETURNS  : void
795   NOTES    :
796 
797   UPDATE   :
798 ******************************************************************************/
799 
bn_elman_yesPROC(void)800 static void bn_elman_yesPROC (void)
801 
802 {
803   if (! ui_xGetToggleState (t_yes)) ui_xSetToggleState (t_yes, TRUE ) ;
804   else                              ui_xSetToggleState (t_no , FALSE) ;
805 }
806 
807 
808 
809 /*****************************************************************************
810   FUNCTION : bn_elman_noPROC
811 
812   PURPOSE  : callback function for NO button in 'BigNet (Elman)'
813   RETURNS  : void
814   NOTES    :
815 
816   UPDATE   :
817 ******************************************************************************/
818 
bn_elman_noPROC(void)819 static void bn_elman_noPROC (void)
820 
821 {
822   if (! ui_xGetToggleState (t_no)) ui_xSetToggleState (t_no , TRUE ) ;
823   else                             ui_xSetToggleState (t_yes, FALSE) ;
824 }
825 
826 
827 
828 /*****************************************************************************
829   FUNCTION : bn_elman_insertPROC
830 
831   PURPOSE  : callback function for INSERT button in 'BigNet (Elman)'
832              inserts a new hidden layer
833   RETURNS  : void
834   NOTES    :
835 
836   UPDATE   :
837 ******************************************************************************/
838 
bn_elman_insertPROC(void)839 static void bn_elman_insertPROC (void)
840 
841 {
842   if (no_of_layers < MAX_NO_OF_LAYERS)
843   {
844     bn_elman_getFromWidget () ;
845 
846     layer[no_of_layers]     = layer[no_of_layers - 1] ;
847     layer[no_of_layers - 1] = 0 ;
848 
849     columns[no_of_layers]     = columns[no_of_layers - 1] ;
850     columns[no_of_layers - 1] = 1 ;
851 
852 
853     /* ----------    delete output layer     ---------- */
854 
855     XtDestroyWidget (elmanLabelWidget[no_of_layers - 1]) ;
856     XtDestroyWidget (elmanUnitWidget [no_of_layers - 1]) ;
857     XtDestroyWidget (elmanSpaceWidget[no_of_layers - 1]) ;
858     XtDestroyWidget (elmanColWidget  [no_of_layers - 1]) ;
859 
860 
861     /* -------  insert hidden and output layer  ------- */
862 
863     no_of_layers++ ;
864     bn_elman_create_dialogrow (no_of_layers - 2, "hidden") ;
865     bn_elman_create_dialogrow (no_of_layers - 1, "output") ;
866 
867     fixWindowSize (baseWidgetElman) ;
868   }
869   else
870     ui_confirmOk ("No more hidden layers are supported !") ;
871 }
872 
873 
874 
875 /*****************************************************************************
876   FUNCTION : bn_elman_deletePROC
877 
878   PURPOSE  : callback function for DELETE button in 'BigNet (Elman)'
879              deletes the last hidden layer
880   NOTES    :
881 
882   UPDATE   :
883 ******************************************************************************/
884 
bn_elman_deletePROC(void)885 static void bn_elman_deletePROC (void)
886 
887 {
888   if (no_of_layers > 3)
889   {
890     bn_elman_getFromWidget () ;
891 
892     no_of_layers-- ;
893 
894     layer[no_of_layers - 1]   = layer[no_of_layers] ;
895     layer[no_of_layers]       = 0 ;
896 
897     columns[no_of_layers - 1] = columns[no_of_layers] ;
898     columns[no_of_layers]     = 1 ;
899 
900 
901     /* ----------  delete last hidden layer  ---------- */
902 
903     XtDestroyWidget (elmanLabelWidget[no_of_layers - 1]) ;
904     XtDestroyWidget (elmanUnitWidget [no_of_layers - 1]) ;
905     XtDestroyWidget (elmanSpaceWidget[no_of_layers - 1]) ;
906     XtDestroyWidget (elmanColWidget  [no_of_layers - 1]) ;
907 
908 
909     /* ----------    delete output layer     ---------- */
910 
911     XtDestroyWidget (elmanLabelWidget[no_of_layers]) ;
912     XtDestroyWidget (elmanUnitWidget [no_of_layers]) ;
913     XtDestroyWidget (elmanSpaceWidget[no_of_layers]) ;
914     XtDestroyWidget (elmanColWidget  [no_of_layers]) ;
915 
916 
917     /* ----------    insert output layer     ---------- */
918 
919     bn_elman_create_dialogrow (no_of_layers - 1, "output") ;
920 
921     fixWindowSize (baseWidgetElman) ;
922   }
923   else
924     ui_confirmOk ("You can not delete the last hidden layer !") ;
925 }
926 
927 
928 
929 /*****************************************************************************
930   FUNCTION : bn_create_elman
931 
932   PURPOSE  : creates the 'BigNet (Elman)' window
933   RETURNS  : void
934   NOTES    :
935 
936   UPDATE   :
937 ******************************************************************************/
938 
bn_create_elman(void)939 void bn_create_elman (void)
940 
941 {
942   Widget      button, w_left, w_top ;
943   int         i;
944 
945 
946   if(bn_elman_open) {
947       XRaiseWindow (XtDisplay (baseWidgetElman), XtWindow (baseWidgetElman));
948       return;
949   }
950 
951     no_of_layers = 3 ;                  /* 1 input, 1 hidden, 1 output layer */
952 
953     for (i = 0 ; i < MAX_NO_OF_LAYERS ; i++)
954     {
955       layer[i]   = 0 ;
956       columns[i] = 1 ;
957     }
958 
959     baseWidgetElman    = XtCreatePopupShell ("SNNS BigNet (Elman)",
960                                         topLevelShellWidgetClass,
961                                         ui_toplevel, NULL, 0);
962 
963     elman_box     = XtCreateManagedWidget ("box" , boxWidgetClass ,
964                                            baseWidgetElman, NULL , 0) ;
965 
966     elman_form[0] = XtCreateManagedWidget ("form", formWidgetClass,
967                                            elman_box, NULL , 0) ;
968 
969 
970     /* -----------------------------  headline  ----------------------------*/
971 
972     w_left = w_top = w_headline =
973       ui_xCreateLabelItem ("Layer No.  Type    No. of Units  No. of Col.  ",
974                            elman_form[0], (9 + 8 + 24) * 8, NULL  , NULL) ;
975 
976 
977     /* ---------------------------  dialog rows  ---------------------------*/
978 
979     for (i = 0 ; i < no_of_layers ; i++)
980     {
981       if      (i == 0)              bn_elman_create_dialogrow (i, "input ") ;
982       else if (i == no_of_layers-1) bn_elman_create_dialogrow (i, "output") ;
983       else                          bn_elman_create_dialogrow (i, "hidden") ;
984     }
985 
986 
987     /* -----------------  output context :  yes / no  ----------------------*/
988 
989     elman_form[1] = XtCreateManagedWidget ("form", formWidgetClass, elman_box,
990 					   NULL , 0) ;
991 
992     w_left = ui_xCreateLabelItem ("Output Context :", elman_form[1],
993                                   20 * 8, NULL, NULL) ;
994 
995     t_yes =
996       ui_xCreateToggleItem ("yes", elman_form[1], NULL, w_left, NULL) ;
997       XtAddCallback (t_yes, XtNcallback,
998                      (XtCallbackProc) bn_elman_yesPROC, NULL) ;
999 
1000     w_left = ui_xCreateLabelItem (" ", elman_form[1], 7 * 8, t_yes,
1001                                   NULL) ;
1002 
1003     t_no =
1004       ui_xCreateToggleItem ("no", elman_form[1], NULL, w_left, NULL) ;
1005       XtAddCallback (t_no, XtNcallback,
1006                      (XtCallbackProc) bn_elman_noPROC, NULL) ;
1007 
1008     w_left = ui_xCreateLabelItem (" ", elman_form[1], 3 * 8 + 4, t_no,
1009                                   NULL) ;
1010 
1011     ui_xSetToggleState (t_yes, FALSE) ;
1012     ui_xSetToggleState (t_no , TRUE ) ;
1013 
1014 
1015     /* --------------  hidden layers :  insert / delete  -------------------*/
1016 
1017     elman_form[2] = XtCreateManagedWidget ("form", formWidgetClass, elman_box,
1018 					   NULL , 0) ;
1019 
1020     w_left = ui_xCreateLabelItem ("Hidden Layers  :", elman_form[2],
1021                                   18 * 8 + 5, NULL, NULL) ;
1022 
1023     button = ui_xCreateButtonItem ("insert", elman_form[2], w_left, NULL) ;
1024     XtAddCallback (button, XtNcallback,
1025 		   (XtCallbackProc) bn_elman_insertPROC, NULL) ;
1026 
1027     w_left = ui_xCreateLabelItem (" ", elman_form[2], 4 * 8 + 5, button,
1028                                   NULL) ;
1029 
1030     button = ui_xCreateButtonItem ("delete", elman_form[2], w_left, NULL) ;
1031     XtAddCallback (button, XtNcallback,
1032                      (XtCallbackProc) bn_elman_deletePROC, NULL) ;
1033 
1034     w_left = ui_xCreateLabelItem (" ", elman_form[2], 2 * 8 + 4, button,
1035                                   NULL) ;
1036 
1037 
1038     /* -----------------------  done / create  -----------------------------*/
1039 
1040     button = ui_xCreateButtonItem ("create_net", elman_box, NULL  ,
1041                                    elman_form[2]) ;
1042       XtAddCallback (button, XtNcallback,
1043                      (XtCallbackProc) bn_elman_createPROC, NULL) ;
1044 
1045     button = ui_xCreateButtonItem ("done"      , elman_box, button,
1046                                    elman_form[2]) ;
1047       XtAddCallback (button, XtNcallback,
1048                      (XtCallbackProc) bn_elman_donePROC  , NULL) ;
1049 
1050     ui_checkWindowPosition (baseWidgetElman) ;
1051     XtPopup (baseWidgetElman, XtGrabNone) ;
1052 
1053     fixWindowSize (baseWidgetElman) ;
1054 
1055     bn_elman_open = 1 ;
1056 }
1057 
1058 
1059 
1060 
1061 
1062