1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
|
/*****************************************************************************
FILE : $Source: /usr/local/bv/SNNS/SNNSv4.1/kernel/sources/RCS/kernel.c,v $
SHORTNAME : kernel.c
SNNS VERSION : 4.1
PURPOSE : SNNS Kernel
NOTES :
AUTHOR : Niels Mache
DATE : 20.02.90
CHANGED BY : Sven Doering, Michael Vogt, Guenter Mamier,Christine Bagdi,
Thomas Gern
IDENTIFICATION : $State: Exp $ $Locker: $
RCS VERSION : $Revision: 2.15 $
LAST CHANGE : $Date: 1995/11/16 12:02:46 $
Copyright (c) 1990-1995 SNNS Group, IPVR, Univ. Stuttgart, FRG
******************************************************************************/
#define SNNS_KERNEL
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <memory.h>
#include <math.h>
#include "kr_typ.h" /* Kernel Types and Constants */
#include "kr_const.h" /* Constant Declarators for SNNS-Kernel */
#include "kr_def.h" /* Default Values */
#ifndef rand
#include "random.h" /* Randomize Library Function Prototypes */
#endif
#include "kernel.ph" /* Function Prototypes */
#include "kr_mem.h" /* Function Prototypes */
#include "kr_funcs.h" /* Function Prototypes */
#include "kr_mac.h" /* Kernel Macros */
#include "cc_rcc_topo.h"
#include "kr_newpattern.h"
#ifdef MASPAR_KERNEL
#include "kr_feedf.h" /* Function Prototypes */
#endif
#include "kr_art.h" /* Function Prototypes */
#include "kr_art1.h" /* Prototypes and global defs for ART1 */
#include "kr_art2.h" /* Prototypes and global defs for ART2 */
#include "kr_amap.h" /* Prototypes and global defs for ARTMAP */
#include "kr_JordElm.h"
/*****************************************************************************
FUNCTION : kr_countUnits
PURPOSE : count units according to their topological type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static void kr_countUnits(struct Unit *unit_ptr, int mode)
{
if (mode == UNIT_ADD) {
/* add unit */
switch (unit_ptr->flags & UFLAG_TTYP_PAT) {
case UFLAG_TTYP_IN:
NoOfInputUnits++;
break;
case UFLAG_TTYP_OUT:
NoOfOutputUnits++;
break;
case UFLAG_TTYP_HIDD:
NoOfHiddenUnits++;
break;
}
return;
}
if (mode == UNIT_DELETE) {
/* delete unit */
switch (unit_ptr->flags & UFLAG_TTYP_PAT) {
case UFLAG_TTYP_IN:
--NoOfInputUnits;
break;
case UFLAG_TTYP_OUT:
--NoOfOutputUnits;
break;
case UFLAG_TTYP_HIDD:
--NoOfHiddenUnits;
break;
}
return;
}
}
/*****************************************************************************
FUNCTION : kr_symbolCheck
PURPOSE : spell checker (check identifiers for matching [A-Za-z]^[|, ]*
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
bool kr_symbolCheck(char *symbol)
{
register char c;
KernelErrorCode = KRERR_SYMBOL;
if (!isalpha( *symbol ))
/* Symbol pattern invalid (must match [A-Za-z]^[|, ]*) */
return( FALSE );
while ( (c = *(++symbol)) != '\0' )
{
if (!isgraph( c ))
/* Symbol pattern invalid (must match [A-Za-z]^[|, ]*) */
return( FALSE );
if ( c == '|' || c == ',')
/* Symbol pattern invalid (must match [A-Za-z]^[|, ]*) */
return( FALSE );
}
KernelErrorCode = KRERR_NO_ERROR;
return( TRUE );
}
/*****************************************************************************
FUNCTION : kr_getUnitPtr
PURPOSE :
NOTES :
RETURNS : returns the pointer to the given unit, returns NULL if unit
doesn't exist
UPDATE :
******************************************************************************/
struct Unit *kr_getUnitPtr(int unit_no)
{
struct Unit *unit_ptr;
KernelErrorCode = KRERR_NO_ERROR;
if ((unit_no != 0) &&
(unit_no >= MinUnitNo) && (unit_no <= MaxUnitNo) &&
UNIT_IN_USE( unit_ptr = unit_array + unit_no ))
return( unit_ptr );
/* invalid unit no. */
KernelErrorCode = KRERR_UNIT_NO;
return( NULL );
}
/*****************************************************************************
FUNCTION : kr_getUnitValues
PURPOSE :
NOTES :
RETURNS : Returns the value of the specified unit component
UPDATE :
******************************************************************************/
FlintType kr_getUnitValues(int unit_no, int component_selector)
{
struct Unit *unit_ptr;
unit_ptr = kr_getUnitPtr( unit_no );
if (KernelErrorCode != KRERR_NO_ERROR)
return( (FlintType) 0); /* invalid unit no. */
switch (component_selector)
{
case SEL_UNIT_ACT:
return( (FlintType) unit_ptr->act );
case SEL_UNIT_OUT:
return( (FlintType) unit_ptr->Out.output );
case SEL_UNIT_IACT:
return( (FlintType) unit_ptr->i_act );
case SEL_UNIT_BIAS:
return( (FlintType) unit_ptr->bias );
case SEL_UNIT_VALA:
return( (FlintType) unit_ptr->value_a );
default:
KernelErrorCode = KRERR_PARAMETERS;
return ((FlintType) 0); /* invalid selector */
}
}
/*****************************************************************************
FUNCTION : kr_setUnitValues
PURPOSE : Sets the value of the specified unit component
NOTES :
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_setUnitValues(int unit_no, int component_selector,
FlintTypeParam value)
{
struct Unit *unit_ptr;
unit_ptr = kr_getUnitPtr( unit_no );
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode ); /* invalid unit no. */
switch (component_selector)
{
case SEL_UNIT_ACT:
unit_ptr->act = (FlintType) value;
break;
case SEL_UNIT_OUT:
unit_ptr->Out.output = (FlintType) value;
break;
case SEL_UNIT_IACT:
unit_ptr->i_act = (FlintType) value;
break;
case SEL_UNIT_BIAS:
unit_ptr->bias = (FlintType) value;
break;
case SEL_UNIT_VALA:
unit_ptr->value_a = (FlintType) value;
break;
default:
KernelErrorCode = KRERR_PARAMETERS;
break; /* invalid selector */
}
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_setAllUnitValues
PURPOSE : Sets all unit components of the specified unit
NOTES :
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_setAllUnitValues(int unit_no, FlintTypeParam out,
FlintTypeParam act, FlintTypeParam i_act,
FlintTypeParam bias)
{
struct Unit *unit_ptr;
unit_ptr = kr_getUnitPtr( unit_no );
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode );
unit_ptr->Out.output = (FlintType) out;
unit_ptr->act = (FlintType) act;
unit_ptr->i_act = (FlintType) i_act;
unit_ptr->bias = (FlintType) bias;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_makeDefaultUnit
PURPOSE : Creates a unit with default values
NOTES :
RETURNS :
UPDATE : Thomas Gern, 07.09.95 -> actbuf is initialized
******************************************************************************/
int kr_makeDefaultUnit(void)
{
struct Unit *unit_ptr;
FunctionPtr func_ptr;
int unit_no;
int i;
unit_no = krm_getUnit();
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode );
(void) kr_setAllUnitValues( unit_no, (FlintTypeParam) DEF_OUT, DefaultIAct,
DefaultIAct, DefaultBias );
unit_ptr = unit_array + unit_no;
unit_ptr->Ftype_entry = NULL;
unit_ptr->value_a = (FlintType) 0; /*previous bias change*/
unit_ptr->value_b = (FlintType) 0; /*previous bias slope*/
unit_ptr->value_c = (FlintType) 0; /*actual bias slope*/
for (i = 0; i < MAX_BPTT_BACKSTEP; i++)
unit_ptr->actbuf[i] = 0.0;
if (DefaultUFuncAct == NULL) {
if (!krf_funcSearch( krf_getCurrentNetworkFunc( ACT_FUNC ),
ACT_FUNC, &func_ptr))
return( KernelErrorCode );
DefaultUFuncAct = (ActFuncPtr) func_ptr;
if (!krf_funcSearch( krf_getCurrentNetworkFunc( ACT_FUNC ),
ACT_DERIV_FUNC, &func_ptr))
return( KernelErrorCode );
DefaultUFuncActDeriv = (ActDerivFuncPtr) func_ptr;
if (!krf_funcSearch( krf_getCurrentNetworkFunc( ACT_FUNC ),
ACT_2_DERIV_FUNC, &func_ptr))
return( KernelErrorCode );
DefaultUFuncAct2Deriv = (ActDerivFuncPtr) func_ptr;
if (!krf_funcSearch( krf_getCurrentNetworkFunc( OUT_FUNC ),
OUT_FUNC, &func_ptr))
return( KernelErrorCode );
DefaultUFuncOut = (OutFuncPtr) func_ptr;
}
unit_ptr->out_func = DefaultUFuncOut; /* default output function */
unit_ptr->act_func = DefaultUFuncAct; /* default activation function */
unit_ptr->act_deriv_func = DefaultUFuncActDeriv; /* def. derivation actfunc */
unit_ptr->act_2_deriv_func = DefaultUFuncAct2Deriv; /* default derivation act. function */
unit_ptr->unit_name= NULL; /* default is no unit name */
unit_ptr->subnet_no = DefaultSubnetNo;
unit_ptr->layer_no = DefaultLayerNo;
unit_ptr->unit_pos.x = DefaultPosX;
unit_ptr->unit_pos.y = DefaultPosY;
unit_ptr->unit_pos.z = DefaultPosZ;
/* set unit flags */
unit_ptr->flags = UFLAG_INITIALIZED | DefaultSType;
/* count units */
kr_countUnits( unit_ptr, UNIT_ADD );
return( unit_no );
}
/*****************************************************************************
FUNCTION : kr_createUnit
PURPOSE : Creates a user defined unit
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_createUnit(char *unit_name, char *out_func_name, char *act_func_name,
FlintTypeParam i_act, FlintTypeParam bias)
{
FunctionPtr out_func_ptr, act_func_ptr, act_deriv_func_ptr,
act_2_deriv_func_ptr;
char *str_ptr;
int unit_no;
struct Unit *unit_ptr;
if (!kr_symbolCheck( unit_name ))
return( KernelErrorCode ); /* Symbol pattern invalid
(must match [A-Za-z]^[|, ]*) */
if ( !krf_funcSearch( out_func_name, OUT_FUNC, &out_func_ptr ) )
return( KernelErrorCode );
if ( !krf_funcSearch( act_func_name, ACT_FUNC, &act_func_ptr ) )
return( KernelErrorCode );
/* set the derivation function of the activation function */
if ( !krf_funcSearch( act_func_name, ACT_DERIV_FUNC, &act_deriv_func_ptr ))
return( KernelErrorCode );
/* set the second derivation function of the activation function */
if ( !krf_funcSearch( act_func_name, ACT_2_DERIV_FUNC, &act_2_deriv_func_ptr ))
return( KernelErrorCode );
if ( (str_ptr = krm_NTableInsertSymbol( unit_name, UNIT_SYM ) ) == NULL)
return( KernelErrorCode );
unit_no = kr_makeDefaultUnit();
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode );
(void) kr_setAllUnitValues( unit_no, (FlintTypeParam) DEF_OUT,
i_act, i_act, bias );
unit_ptr = unit_array + unit_no;
unit_ptr->out_func = (OutFuncPtr) out_func_ptr;
unit_ptr->act_func = (ActFuncPtr) act_func_ptr;
unit_ptr->act_deriv_func = (ActDerivFuncPtr) act_deriv_func_ptr;
unit_ptr->act_2_deriv_func = (ActDerivFuncPtr) act_2_deriv_func_ptr;
unit_ptr->unit_name = str_ptr;
NetModified = TRUE;
return( unit_no );
}
/*****************************************************************************
FUNCTION : kr_unitSetTType
PURPOSE : Sets the topologic type of the unit
NOTES :
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_unitSetTType(int unit_no, int UnitTType)
{
struct Unit *unit_ptr;
int intflags;
if ((unit_ptr = kr_getUnitPtr( unit_no )) == NULL)
return( KernelErrorCode );
intflags = kr_TType2Flags( UnitTType );
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode );
if (((FlagWord) intflags == UFLAG_TTYP_SPEC_X) ||
((FlagWord) intflags == UFLAG_TTYP_N_SPEC_X)) {
if ((FlagWord) intflags == UFLAG_TTYP_SPEC_X) {
/* the topologic type of the unit will change */
NetModified = TRUE;
/* count units */
kr_countUnits( unit_ptr, UNIT_DELETE );
/* change topologic type of the unit, add special Flag */
unit_ptr->flags |= (FlagWord) UFLAG_TTYP_SPEC;
/* count units */
kr_countUnits( unit_ptr, UNIT_ADD );
}else{
if((unit_ptr->flags & UFLAG_TTYP_PAT)!= UFLAG_TTYP_SPEC){
/* the topologic type of the unit will change */
NetModified = TRUE;
/* count units */
kr_countUnits( unit_ptr, UNIT_DELETE );
/* change topologic type of the unit, delete special Flag */
unit_ptr->flags &= (FlagWord) ~UFLAG_TTYP_SPEC;
/* count units */
kr_countUnits( unit_ptr, UNIT_ADD );
}
}
}else{
if ((unit_ptr->flags & UFLAG_TTYP_PAT) != (FlagWord) intflags) {
/* the topologic type of the unit will change */
NetModified = TRUE;
/* count units */
kr_countUnits( unit_ptr, UNIT_DELETE );
/* change topologic type of the unit */
unit_ptr->flags &= ~UFLAG_TTYP_PAT;
unit_ptr->flags |= (FlagWord) intflags;
/* count units */
kr_countUnits( unit_ptr, UNIT_ADD );
}
}
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_setSite
PURPOSE : initialize the first/next site or the named site at the current
unit for access
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_setSite(int mode, char *site_name)
{
struct SiteTable *stbl_ptr;
if (unitPtr == NULL) {
KernelErrorCode = KRERR_UNIT_NO;
return( KernelErrorCode );
}
KernelErrorCode = KRERR_NO_ERROR;
switch (mode) {
case FIRST:
prevSitePtr = NULL;
if UNIT_HAS_SITES( unitPtr )
{ /* Unit has sites */
sitePtr = unitPtr->sites;
return( TRUE );
}
else {
sitePtr = NULL;
return( FALSE );
}
case NEXT:
if ((sitePtr == NULL) || (sitePtr->next == NULL)) return( FALSE );
prevSitePtr = sitePtr;
sitePtr = sitePtr->next;
return( TRUE );
case NAME:
if (!UNIT_HAS_SITES( unitPtr ))
{ /* Current unit doesn't have sites */
KernelErrorCode = KRERR_NO_SITES;
return( KernelErrorCode );
}
if ((stbl_ptr = krm_STableSymbolSearch( site_name )) == NULL)
{ /* site name isn't defined */
KernelErrorCode = KRERR_UNDEF_SITE_NAME;
return( KernelErrorCode );
}
for (sitePtr = unitPtr->sites, prevSitePtr = NULL;
sitePtr != NULL;
prevSitePtr = sitePtr, sitePtr = sitePtr->next)
if (sitePtr->site_table == stbl_ptr)
return( KRERR_NO_ERROR ); /* site was found */
sitePtr = prevSitePtr = NULL;
/* Current unit doesn't have a site with this name */
KernelErrorCode = KRERR_NO_SUCH_SITE;
return( KernelErrorCode );
default:
KernelErrorCode = KRERR_PARAMETERS;
return( KernelErrorCode );
}
}
/*****************************************************************************
FUNCTION : kr_getUnit
PURPOSE : returns the number of the first/next/current unit of the unit array
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_getUnit(int mode)
{
register struct Unit *unit_ptr;
if (NoOfUnits == 0) return( 0 );
switch (mode)
{
case FIRST:
unitNo = MinUnitNo;
unitPtr = unit_array + MinUnitNo;
if UNIT_HAS_SITES( unitPtr )
{ /* Initialize current site pointer to the first available site */
prevSitePtr = NULL;
sitePtr = unitPtr->sites;
}
else
{ /* No sites available */
prevSitePtr = NULL;
sitePtr = NULL;
}
return( unitNo );
case NEXT:
unit_ptr = unitPtr;
if ((unit_ptr - unit_array) >= MaxUnitNo) return( 0 );
while (!UNIT_IN_USE( ++unit_ptr )) ;
unitNo = unit_ptr - unit_array;
unitPtr = unit_ptr;
if UNIT_HAS_SITES( unit_ptr )
{ /* Initialize current site pointer to the first available site */
prevSitePtr = NULL;
sitePtr = unit_ptr->sites;
}
else
{ /* No sites available */
prevSitePtr = NULL;
sitePtr = NULL;
}
return( unitNo );
case CURRENT:
return( unitNo );
default:
KernelErrorCode = KRERR_PARAMETERS;
return( 0 );
}
}
/*****************************************************************************
FUNCTION : kr_setCurrUnit
PURPOSE : initializes the given unit for access
NOTES :
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_setCurrUnit(int unit_no)
{
struct Unit *unit_ptr;
if ((unit_ptr = kr_getUnitPtr( unit_no )) == NULL)
return( KernelErrorCode );
unitNo = unit_no;
unitPtr = unit_ptr;
if UNIT_HAS_SITES( unit_ptr )
{ /* Initialize current site pointer to the first available site */
prevSitePtr = NULL;
sitePtr = unit_ptr->sites;
}
else
{ /* No sites available */
prevSitePtr = NULL;
sitePtr = NULL;
}
return( KRERR_NO_ERROR );
}
/*****************************************************************************
FUNCTION : kr_getPredecessorUnit
PURPOSE : Returns the no. of first, next or current predecessor unit of the
current unit/site and the connection weight
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_getPredecessorUnit(int mode, FlintType *weight)
{
static struct Link *link_ptr = NULL;
if (unitPtr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( 0 );
}
switch (mode)
{
case FIRST: /* first predecessor link wanted */
if UNIT_HAS_SITES( unitPtr )
{
if (sitePtr == NULL)
/* site not initialized */
link_ptr = unitPtr->sites->links;
else
link_ptr = sitePtr->links;
}
else
link_ptr = (struct Link *) unitPtr->sites;
linkPtr = link_ptr;
prevLinkPtr = NULL;
if (link_ptr == NULL) return( 0 ); /* No inputs */
*weight = link_ptr->weight;
return( link_ptr->to - unit_array ); /* Return unit number */
case NEXT:
if (link_ptr == NULL)
{ /* no current link */
KernelErrorCode = KRERR_NO_CURRENT_LINK;
return( 0 );
}
prevLinkPtr = link_ptr;
if ((linkPtr = link_ptr = link_ptr->next) == NULL)
{
prevLinkPtr = NULL;
return( 0 ); /* no successor unit */
}
*weight = link_ptr->weight;
return( link_ptr->to - unit_array ); /* Return unit number */
case CURRENT:
if (link_ptr == NULL)
{ /* no current link */
KernelErrorCode = KRERR_NO_CURRENT_LINK;
return( 0 );
}
*weight = link_ptr->weight;
return( link_ptr->to - unit_array ); /* Return unit number */
default:
KernelErrorCode = KRERR_PARAMETERS;
return( 0 );
}
}
/*****************************************************************************
FUNCTION : kr_searchOutputConnection
PURPOSE :
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static int kr_searchOutputConnection(struct Unit *start_unit_ptr,
struct Unit *source_unit_ptr,
FlintType *weight)
{
register struct Link *link_ptr, *prev_link_ptr;
register struct Unit *source_unit;
register struct Site *site_ptr, *prev_site_ptr;
register struct Unit *unit_ptr;
source_unit = source_unit_ptr;
if ((sitePtr != NULL))
{ /* current unit has sites, so search for another connection at the
other sites of the unit */
for (site_ptr = sitePtr->next, prev_site_ptr = sitePtr;
site_ptr != NULL;
prev_site_ptr = site_ptr, site_ptr = site_ptr->next)
for (link_ptr = site_ptr->links, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit)
{
sitePtr = site_ptr; /* set current site */
prevSitePtr = prev_site_ptr; /* set previous site */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( unitNo );
}
start_unit_ptr++; /* no connection found at the current site,
so start search at the next units */
}
for(unit_ptr = start_unit_ptr; unit_ptr <= unit_array + MaxUnitNo; unit_ptr++)
if UNIT_IN_USE( unit_ptr )
{
if UNIT_HAS_DIRECT_INPUTS( unit_ptr )
{
for (link_ptr = (struct Link *) unit_ptr->sites, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit)
{
unitPtr = unit_ptr; /* set current unit pointer */
unitNo = unit_ptr - unit_array; /* set current unit no. */
sitePtr = prevSitePtr = NULL; /* no current site */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( unitNo );
}
}
else
if UNIT_HAS_SITES( unit_ptr )
{
for (site_ptr = unit_ptr->sites, prev_site_ptr = NULL;
site_ptr != NULL;
prev_site_ptr = site_ptr, site_ptr = site_ptr->next)
for (link_ptr = site_ptr->links, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit)
{
unitPtr = unit_ptr; /* set current unit pointer */
unitNo = unit_ptr - unit_array; /* set current unit no. */
sitePtr = site_ptr; /* set current site */
prevSitePtr = prev_site_ptr; /* set previous site */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( unitNo );
}
}
}
/* no successor unit found */
unitPtr = NULL; unitNo = 0; /* no current unit */
sitePtr = prevSitePtr = NULL; /* no current site */
linkPtr = prevLinkPtr = NULL; /* no current link */
return( 0 );
}
/*****************************************************************************
FUNCTION : kr_getSuccessorUnit
PURPOSE : Returns the no. of first or next succecessor unit of the
given unit and the connection strenght.
Sets the current unit/site.
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_getSuccessorUnit(int mode, int source_unit_no, FlintType *weight)
{
static struct Unit *source_unit_ptr,
*current_unit_ptr = NULL;
static struct Site *current_site_ptr = NULL;
int unit_no;
switch (mode)
{
case FIRST: /* first successor link wanted */
if ((source_unit_ptr = kr_getUnitPtr( source_unit_no )) == NULL)
return( KernelErrorCode );
sitePtr = NULL; /* no current Site */
unit_no = kr_searchOutputConnection( unit_array + MinUnitNo,
source_unit_ptr, weight );
current_unit_ptr = unitPtr;
current_site_ptr = sitePtr;
return( unit_no );
case NEXT: /* next successor link wanted */
if (current_unit_ptr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( 0 );
}
sitePtr = current_site_ptr;
unit_no = kr_searchOutputConnection( current_unit_ptr + 1,
source_unit_ptr, weight );
current_unit_ptr = unitPtr;
current_site_ptr = sitePtr;
return( unit_no );
default:
KernelErrorCode = KRERR_PARAMETERS;
return( 0 );
}
}
/*****************************************************************************
FUNCTION : kr_areConnected
PURPOSE : True if there exists a connection between source unit
<source_unit_no> and target unit <target_unit_no>, otherwise false.
If there exist a connection between these units, kr_areConnected
returns the connection strength also.
NOTES : This function is slow (Units are backward chained only)
IMPORTANT: If there exist a connection, the current unit and site will be
set to the target unit/site.
RETURNS : Returns FALSE if unit doesn't exist.
UPDATE :
******************************************************************************/
bool kr_areConnected(int source_unit_no, int target_unit_no, FlintType *weight)
{
register struct Link *link_ptr, *prev_link_ptr;
register struct Unit *source_unit_ptr;
register struct Site *site_ptr, *prev_site_ptr;
struct Unit *target_unit_ptr;
if ( (source_unit_ptr = kr_getUnitPtr( source_unit_no ) ) == NULL)
return( FALSE ); /* invalid unit # */
if ( (target_unit_ptr = kr_getUnitPtr( target_unit_no ) ) == NULL)
return( FALSE ); /* invalid unit # */
if UNIT_HAS_DIRECT_INPUTS( target_unit_ptr )
{
for(link_ptr = (struct Link *) target_unit_ptr->sites, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit_ptr)
{ /* connection found */
unitPtr = target_unit_ptr; /* set current unit pointer */
unitNo = target_unit_no; /* set current unit no. */
sitePtr = prevSitePtr = NULL; /* no current site */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( TRUE );
}
}
else
if UNIT_HAS_SITES( target_unit_ptr )
for (site_ptr = target_unit_ptr->sites, prev_site_ptr = NULL;
site_ptr != NULL;
prev_site_ptr = site_ptr, site_ptr = site_ptr->next)
for (link_ptr = site_ptr->links, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit_ptr)
{ /* connection found */
unitPtr = target_unit_ptr; /* set current unit pointer */
unitNo = target_unit_no; /* set current unit no. */
sitePtr = site_ptr; /* set current site */
prevSitePtr = prev_site_ptr; /* set previous site */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( TRUE );
}
/* no successor unit found */
unitPtr = NULL; unitNo = 0; /* no current unit */
sitePtr = prevSitePtr = NULL; /* no current site */
linkPtr = prevLinkPtr = NULL; /* no current link */
return( FALSE );
}
/*****************************************************************************
FUNCTION : kr_isConnected
PURPOSE :
NOTES : If there exists a connection between the two units, the current
link is set to the link between the two units.
RETURNS : True if there exists a connection between source unit
<source_unit_no> and the current unit/site, otherwise false.
UPDATE :
******************************************************************************/
bool kr_isConnected(int source_unit_no, FlintType *weight)
{
register struct Link *link_ptr, *prev_link_ptr;
register struct Unit *source_unit_ptr;
struct Link *start_link_ptr;
if (unitPtr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( FALSE );
}
if ((source_unit_ptr = kr_getUnitPtr( source_unit_no ) ) == NULL)
return( FALSE ); /* invalid unit # */
if UNIT_HAS_DIRECT_INPUTS( unitPtr )
start_link_ptr = (struct Link *) unitPtr->sites;
else
if UNIT_HAS_SITES( unitPtr )
start_link_ptr = sitePtr->links;
else
return( FALSE );
for (link_ptr = start_link_ptr, prev_link_ptr = NULL;
link_ptr != NULL;
prev_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit_ptr)
{ /* connection found */
linkPtr = link_ptr; /* set current link */
prevLinkPtr = prev_link_ptr; /* set previous link */
*weight = link_ptr->weight;
return( TRUE );
}
/* no successor unit found */
linkPtr = prevLinkPtr = NULL; /* no current link */
return( FALSE );
}
/*****************************************************************************
FUNCTION : kr_getLinkWeight
PURPOSE :
NOTES :
RETURNS : Returns the link weight of the current link
UPDATE :
******************************************************************************/
FlintType kr_getLinkWeight(void)
{
if (linkPtr != NULL) return( linkPtr->weight );
KernelErrorCode = KRERR_NO_CURRENT_LINK;
return( (FlintType) 0 );
}
/*****************************************************************************
FUNCTION : kr_setLinkWeight
PURPOSE : Sets the link weight of the current link
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_setLinkWeight(FlintTypeParam weight)
{
if (linkPtr != NULL)
{
linkPtr->weight = weight;
return;
}
KernelErrorCode = KRERR_NO_CURRENT_LINK;
}
/*****************************************************************************
FUNCTION : kr_createLink
PURPOSE : Creates a link between source unit and the current unit/site
NOTES : kr_createLink DO NOT set the current link
If you want to create a link and its unknown if there exists
already a connection between the two units, use krui_createLink
and test the return code, instead of the sequence kr_isConnected
and kr_createLink
RETURNS : Returns an error code:
- if memory allocation fails
- if source unit doesn't exist or
- if there exists already a connection between current unit/site
and the source unit
0 otherwise.
UPDATE :
******************************************************************************/
krui_err kr_createLink(int source_unit_no, FlintTypeParam weight)
{
register struct Link *link_ptr;
register struct Unit *source_unit_ptr;
KernelErrorCode = KRERR_NO_ERROR;
if (unitPtr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( KernelErrorCode );
}
if ((source_unit_ptr = kr_getUnitPtr( source_unit_no ) ) == NULL)
return( KernelErrorCode ); /* invalid unit # */
switch ((int) (unitPtr->flags & UFLAG_INPUT_PAT))
{
case UFLAG_NO_INP: /* current unit doesn't have inputs */
if ((link_ptr = krm_getLink()) == NULL)
return( KernelErrorCode );
link_ptr->to = source_unit_ptr;
link_ptr->weight = (FlintType) weight;
link_ptr->next = NULL;
unitPtr->sites = (struct Site *) link_ptr;
unitPtr->flags |= UFLAG_DLINKS; /* unit has direkt inputs now */
break;
case UFLAG_DLINKS: /* current unit has direct inputs */
FOR_ALL_LINKS( unitPtr, link_ptr )
if (link_ptr->to == source_unit_ptr)
{ /* there exists already a connection */
KernelErrorCode = KRERR_ALREADY_CONNECTED;
return( KRERR_ALREADY_CONNECTED );
}
if ((link_ptr = krm_getLink()) == NULL)
return( KernelErrorCode );
link_ptr->to = source_unit_ptr;
link_ptr->weight = (FlintType) weight;
link_ptr->next = (struct Link *) unitPtr->sites;
unitPtr->sites = (struct Site *) link_ptr;
break;
case UFLAG_SITES: /* current unit has sites */
FOR_ALL_LINKS_AT_SITE( sitePtr, link_ptr )
if (link_ptr->to == source_unit_ptr)
{ /* there exists already a connection */
KernelErrorCode = KRERR_ALREADY_CONNECTED;
return( KRERR_ALREADY_CONNECTED );
}
if ((link_ptr = krm_getLink()) == NULL)
return( KernelErrorCode );
link_ptr->to = source_unit_ptr;
link_ptr->weight = (FlintType) weight;
link_ptr->next = (struct Link *) sitePtr->links;
sitePtr->links = link_ptr;
break;
default:
KernelErrorCode = KRERR_PARAMETERS;
return( KernelErrorCode );
}
NetModified = TRUE;
return( KRERR_NO_ERROR );
}
/*****************************************************************************
FUNCTION : kr_deleteLink
PURPOSE : Deletes the current link
NOTES : To delete a link between the current unit/site and the source unit
<source_unit_no>, call krui_isConnected( source_unit_no ) and
krui_deleteLink()
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_deleteLink(void)
{
register struct Link *next_link_ptr;
if (linkPtr == NULL)
{ /* no current link */
KernelErrorCode = KRERR_NO_CURRENT_LINK;
return( KernelErrorCode );
}
if (unitPtr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( KernelErrorCode );
}
KernelErrorCode = KRERR_NO_ERROR;
switch ((int) (unitPtr->flags & UFLAG_INPUT_PAT))
{
case UFLAG_NO_INP: /* current unit doesn't have inputs */
KernelErrorCode = KRERR_UNIT_NO_INPUTS;
return( KernelErrorCode );
case UFLAG_DLINKS: /* current unit has direct inputs */
next_link_ptr = linkPtr->next;
krm_releaseLink( linkPtr );
linkPtr = next_link_ptr;
if (prevLinkPtr != NULL) /* current link isn't first link at the unit */
prevLinkPtr->next = next_link_ptr; /* chain previous link pointer
with next link pointer */
else
{ /* current link is the first link at the unit */
unitPtr->sites = (struct Site *) next_link_ptr;
if (next_link_ptr == NULL)
unitPtr->flags &= (~UFLAG_INPUT_PAT); /* last input deleted:
the unit has no inputs now*/
}
NetModified = TRUE;
return( KRERR_NO_ERROR );
case UFLAG_SITES: /* current unit has sites */
next_link_ptr = linkPtr->next;
krm_releaseLink( linkPtr );
linkPtr = next_link_ptr;
if (prevLinkPtr != NULL) /* current link isn't first link at the unit */
prevLinkPtr->next = next_link_ptr; /* chain previous link pointer
with next link pointer */
else /* current link is the first link at the unit */
sitePtr->links = next_link_ptr;
NetModified = TRUE;
return( KRERR_NO_ERROR );
}
KernelErrorCode = KRERR_PARAMETERS;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_deleteAllLinks
PURPOSE : Deletes all input links at current unit/site
NOTES :
RETURNS : Returns the errorcode
UPDATE :
******************************************************************************/
krui_err kr_deleteAllLinks(int mode)
{
if (unitPtr == NULL)
{ /* no current unit */
KernelErrorCode = KRERR_NO_CURRENT_UNIT;
return( KernelErrorCode );
}
linkPtr = NULL;
NetModified = TRUE;
switch (mode)
{
case INPUTS: /* delete all inputs */
if UNIT_HAS_DIRECT_INPUTS( unitPtr )
{
krm_releaseAllLinks( (struct Link *) unitPtr->sites );
unitPtr->sites = NULL;
unitPtr->flags &= (~UFLAG_INPUT_PAT); /* unit don't has inputs now */
return( KernelErrorCode );
}
if UNIT_HAS_SITES( unitPtr )
{
krm_releaseAllLinks( sitePtr->links );
sitePtr->links = NULL; /* site has no inputs now */
return( KernelErrorCode );
}
return( KernelErrorCode );
case OUTPUTS: /* delete all outputs */
kr_deleteAllOutputLinks( unitPtr );
return( KernelErrorCode );
}
KernelErrorCode = KRERR_PARAMETERS;
return( KernelErrorCode );
}
/*#################################################
GROUP: Low-Level Kernel Functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_deleteAllInputs
PURPOSE : delete all links and sites at the given unit
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_deleteAllInputs(struct Unit *unit_ptr)
{
register struct Site *site_ptr;
if (UNIT_HAS_SITES( unit_ptr ))
{ /* Unit has sites */
FOR_ALL_SITES( unit_ptr, site_ptr )
/* Release all links */
krm_releaseAllLinks( site_ptr->links );
krm_releaseAllSites( unit_ptr->sites );
}
else
{ /* Unit don't has sites */
if (UNIT_HAS_DIRECT_INPUTS( unit_ptr ))
krm_releaseAllLinks( (struct Link *) unit_ptr->sites );
}
unit_ptr->sites = NULL;
/* The unit has no inputs now */
unit_ptr->flags &= (~UFLAG_INPUT_PAT);
}
/*****************************************************************************
FUNCTION : kr_deleteAllOutputLinks
PURPOSE : Deletes all output links at <source_unit>
NOTES : This function is slow
RETURNS :
UPDATE :
******************************************************************************/
void kr_deleteAllOutputLinks(struct Unit *source_unit_ptr)
{
register struct Link *link_ptr,
*pred_link_ptr;
register struct Site *site_ptr;
register struct Unit *unit_ptr;
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr )
if UNIT_HAS_SITES( unit_ptr )
{ /* unit has sites */
FOR_ALL_SITES( unit_ptr, site_ptr )
for (link_ptr = site_ptr->links, pred_link_ptr = NULL;
link_ptr != NULL;
pred_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit_ptr)
{ /* Connection between unit and source_unit found */
if (pred_link_ptr == NULL)
site_ptr->links = link_ptr->next;
else
pred_link_ptr->next = link_ptr->next;
krm_releaseLink( link_ptr );
break; /* next site/unit */
}
}
else /* unit has no sites */
if UNIT_HAS_DIRECT_INPUTS( unit_ptr )
for (link_ptr = (struct Link *) unit_ptr->sites, pred_link_ptr = NULL;
link_ptr != NULL;
pred_link_ptr = link_ptr, link_ptr = link_ptr->next)
if (link_ptr->to == source_unit_ptr)
{ /* Connection between unit and source_unit found */
if (pred_link_ptr == NULL)
{
unit_ptr->sites = (struct Site *) link_ptr->next;
if (link_ptr->next == NULL)
/* The unit has no inputs now */
unit_ptr->flags &= (~UFLAG_INPUT_PAT);
}
else
pred_link_ptr->next = link_ptr->next;
krm_releaseLink( link_ptr );
break; /* next unit */
}
}
/*****************************************************************************
FUNCTION : kr_copyOutputLinks
PURPOSE : Copies all output links at <source_unit> to <new_unit>.
NOTES : This function is slow
RETURNS : Returns error code if memory allocation fails.
UPDATE :
******************************************************************************/
static krui_err kr_copyOutputLinks(struct Unit *source_unit_ptr,
struct Unit *new_unit_ptr)
{
register struct Link *link_ptr,
*new_link;
register struct Site *site_ptr;
register struct Unit *unit_ptr;
KernelErrorCode = KRERR_NO_ERROR;
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr )
if UNIT_HAS_DIRECT_INPUTS( unit_ptr )
{
FOR_ALL_LINKS( unit_ptr, link_ptr )
if (link_ptr->to == source_unit_ptr)
{ /* Connection between unit and source_unit found */
if ( (new_link = krm_getLink() ) == NULL)
return( KernelErrorCode );
memcpy( (char *) new_link, (char *) link_ptr, LINK_SIZE );
new_link->next = (struct Link *) unit_ptr->sites;
unit_ptr->sites = (struct Site *) new_link;
new_link->to = new_unit_ptr;
new_link->weight = link_ptr->weight;
break; /* next unit */
}
}
else
if UNIT_HAS_SITES( unit_ptr )
FOR_ALL_SITES_AND_LINKS( unit_ptr, site_ptr, link_ptr )
if (link_ptr->to == source_unit_ptr)
{ /* Connection between unit and source_unit found */
if ( (new_link = krm_getLink() ) == NULL)
return( KernelErrorCode );
new_link->next = site_ptr->links;
site_ptr->links = new_link;
new_link->to = new_unit_ptr;
new_link->weight = link_ptr->weight;
break; /* next site/unit */
}
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_copyInputLinks
PURPOSE : Copy all input links from <source_unit> to <new_unit>
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
static krui_err kr_copyInputLinks(struct Unit *source_unit_ptr,
struct Unit *new_unit_ptr)
{
register struct Link *link_ptr, *new_link,
*last_link_ptr;
register struct Site *source_site_ptr, *dest_site_ptr;
KernelErrorCode = KRERR_NO_ERROR;
if UNIT_HAS_DIRECT_INPUTS( source_unit_ptr )
{
last_link_ptr = new_link = NULL;
FOR_ALL_LINKS( source_unit_ptr, link_ptr )
{
if ((new_link = krm_getLink()) == NULL)
{
new_unit_ptr->sites = (struct Site *) last_link_ptr;
return( KernelErrorCode );
}
memcpy( (char *) new_link, (char *) link_ptr, LINK_SIZE );
new_link->next = last_link_ptr;
last_link_ptr = new_link;
}
new_unit_ptr->sites = (struct Site *) new_link;
new_unit_ptr->flags &= ~UFLAG_INPUT_PAT;
if (new_link != NULL) new_unit_ptr->flags |= UFLAG_DLINKS;
}
else
if UNIT_HAS_SITES( source_unit_ptr )
FOR_ALL_SITES( source_unit_ptr, source_site_ptr )
FOR_ALL_SITES( new_unit_ptr, dest_site_ptr )
if (source_site_ptr->site_table == dest_site_ptr->site_table)
{
last_link_ptr = new_link = NULL;
FOR_ALL_LINKS_AT_SITE( source_site_ptr, link_ptr )
{
if ((new_link = krm_getLink()) == NULL)
{
dest_site_ptr->links = last_link_ptr;
return( KernelErrorCode );
}
memcpy( (char *) new_link, (char *) link_ptr, LINK_SIZE );
new_link->next = last_link_ptr;
last_link_ptr = new_link;
}
dest_site_ptr->links = new_link;
}
return( KernelErrorCode );
}
/*#################################################
GROUP: Site Name/Func functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_searchUnitSite
PURPOSE : search for a site at a unit
NOTES :
RETURNS : Returns the site or NULL
UPDATE :
******************************************************************************/
struct Site *kr_searchUnitSite(struct Unit *unit_ptr,struct SiteTable *stbl_ptr)
{
register struct Site *site_ptr;
FOR_ALL_SITES( unit_ptr, site_ptr )
if (site_ptr->site_table == stbl_ptr)
return( site_ptr );
return( NULL ); /* there is no site at this unit with this name */
}
/*****************************************************************************
FUNCTION : kr_searchNetSite
PURPOSE : searches for a site in the network
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_searchNetSite(struct SiteTable *stbl_ptr)
{
register struct Site *site_ptr;
register struct Unit *unit_ptr;
if (NoOfUnits == 0)
return( 0 ); /* no units -> no sites */
FOR_ALL_UNITS( unit_ptr )
if (UNIT_HAS_SITES( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{ /* unit has sites and is in use */
FOR_ALL_SITES( unit_ptr, site_ptr )
if (site_ptr->site_table == stbl_ptr)
return( unit_ptr - unit_array ); /* return unit no. */
}
return( 0 ); /* site isn't in use */
}
/*#################################################
GROUP: Link Functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_jogWeights
PURPOSE : Add random uniform distributed values to connection weights.
<minus> must be less then <plus>.
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_jogWeights(FlintTypeParam minus, FlintTypeParam plus)
{
register struct Link *link_ptr;
FlagWord flags;
struct Unit *unit_ptr;
struct Site *site_ptr;
register FlintType range, min;
if (NoOfUnits == 0) return; /* no. units */
range = plus - minus;
min = minus;
FOR_ALL_UNITS( unit_ptr ) {
flags = unit_ptr->flags;
if(((flags & UFLAG_IN_USE) == UFLAG_IN_USE)
&& !IS_SPECIAL_UNIT(unit_ptr))
/* unit is in use */
if (flags & UFLAG_DLINKS)
/* unit has direct links */
FOR_ALL_LINKS( unit_ptr, link_ptr )
link_ptr->weight += (FlintType) drand48() * range + min;
else
if (flags & UFLAG_SITES)
/* unit has sites */
FOR_ALL_SITES_AND_LINKS( unit_ptr, site_ptr, link_ptr )
link_ptr->weight += (FlintType) drand48() * range + min;
}
}
/*#################################################
GROUP: Site Functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_createDefaultSite
PURPOSE : Creates a new site with default initialisation
NOTES :
RETURNS : the new site
UPDATE :
******************************************************************************/
struct Site *kr_createDefaultSite(void)
{
struct Site *site_ptr;
if ( (site_ptr = krm_getSite() ) == NULL) return( NULL );
site_ptr->links = NULL;
site_ptr->next = NULL;
return( site_ptr );
}
/*#################################################
GROUP: Unit Functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_unitNameSearch
PURPOSE : Searches for a unit with the given symbol pointer.
NOTES :
RETURNS : Returns the first unit no. if a unit with the given name was found,
0 otherwise
UPDATE :
******************************************************************************/
int kr_unitNameSearch(int min_unit_no, char *unit_symbol_ptr)
{
register char *symbol;
register struct Unit *unit_ptr;
if ((symbol = unit_symbol_ptr) == NULL)
return( 0 );
/* search for symbol pointer */
for (unit_ptr = unit_array + min_unit_no; unit_ptr <= unit_array + MaxUnitNo; unit_ptr++)
if UNIT_IN_USE( unit_ptr )
if (unit_ptr->unit_name == symbol)
return( unit_ptr - unit_array );
return( 0 );
}
/*****************************************************************************
FUNCTION : kr_copyUnitFrame
PURPOSE : copy the source unit with sites, but no links
NOTES :
RETURNS : Returns the error code
UPDATE :
******************************************************************************/
static krui_err kr_copyUnitFrame(struct Unit *source_unit_ptr,
struct Unit *new_unit_ptr)
{
struct Site *site_ptr,
*new_site_ptr,
*last_site_ptr;
KernelErrorCode = KRERR_NO_ERROR;
memcpy( (char *) new_unit_ptr, (char *) source_unit_ptr, UNIT_SIZE );
if (source_unit_ptr->unit_name != NULL)
(void) krm_NTableInsertSymbol( source_unit_ptr->unit_name, UNIT_SYM );
/* unit has no inputs now */
new_unit_ptr->flags &= ~UFLAG_INPUT_PAT;
new_unit_ptr->sites = NULL;
if UNIT_HAS_SITES( source_unit_ptr )
{ /* Copy all sites, but no links. */
last_site_ptr = new_site_ptr = NULL;
FOR_ALL_SITES( source_unit_ptr, site_ptr ) {
if ((new_site_ptr = krm_getSite()) == NULL) {
new_unit_ptr->sites = last_site_ptr;
return( KernelErrorCode );
}
memcpy( (char *) new_site_ptr, (char *) site_ptr, SITE_SIZE );
new_site_ptr->links = NULL;
new_site_ptr->next = last_site_ptr;
last_site_ptr = new_site_ptr;
}
new_unit_ptr->sites = new_site_ptr;
if (new_site_ptr != NULL) new_unit_ptr->flags |= UFLAG_SITES;
}
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_removeUnit
PURPOSE : Remove unit and all links from network
NOTES :
RETURNS : Returns the error code
UPDATE :
******************************************************************************/
krui_err kr_removeUnit(struct Unit *unit_ptr)
{
/* delete inputs */
kr_deleteAllInputs( unit_ptr );
/* delete output links */
kr_deleteAllOutputLinks( unit_ptr );
/* check references to the unit symbol */
krm_NTableReleaseSymbol( unit_ptr->unit_name, UNIT_SYM );
/* count units */
kr_countUnits( unit_ptr, UNIT_DELETE );
/* delete Unit */
krm_releaseUnit( unit_ptr - unit_array );
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_copyUnit
PURPOSE : Copy a given unit, according to the copy mode
1. copy unit (with it sites, if available) and input/output links
2. copy unit (with it sites, if available) and input links
3. copy unit (with it sites, if available) and output links
4. copy unit (with it sites, if available) but no links
Function has no effect on the current unit.
NOTES : Copying of output links is slow.
If return code < 0, an error occured.
RETURNS : Returns the unit number of the new unit or error message < 0 ,
if errors occured.
UPDATE :
******************************************************************************/
krui_err kr_copyUnit(int copy_mode, int source_unit)
{
struct Unit *source_unit_ptr,
*new_unit_ptr;
int new_unit_no;
KernelErrorCode = KRERR_NO_ERROR;
if ((source_unit_ptr = kr_getUnitPtr( source_unit )) == NULL)
return( KernelErrorCode );
if ((new_unit_no = krm_getUnit()) == 0)
return( KernelErrorCode );
new_unit_ptr = unit_array + new_unit_no;
/* copy unit (with it sites, if available) but no input/output links */
if (kr_copyUnitFrame( source_unit_ptr, new_unit_ptr ) != KRERR_NO_ERROR)
return( KernelErrorCode );
switch (copy_mode)
{
case ONLY_UNIT:
break;
case ONLY_INPUTS:
/* copy unit (with it sites, if available) and input links */
(void) kr_copyInputLinks( source_unit_ptr, new_unit_ptr );
break;
case ONLY_OUTPUTS:
/* copy unit (with it sites, if available) and output links */
(void) kr_copyOutputLinks( source_unit_ptr, new_unit_ptr);
break;
case INPUTS_AND_OUTPUTS:
/* copy unit (with it sites, if available) and input/output links */
if (kr_copyOutputLinks( source_unit_ptr, new_unit_ptr) != KRERR_NO_ERROR)
break;
(void) kr_copyInputLinks( source_unit_ptr, new_unit_ptr );
break;
default:
KernelErrorCode = KRERR_COPYMODE;
}
if (KernelErrorCode != KRERR_NO_ERROR)
{
kr_removeUnit( new_unit_ptr ); /* delete Unit */
return( KernelErrorCode );
}
else
{ /* Successful copy */
new_unit_ptr->flags = source_unit_ptr->flags; /* copy flags */
/* count units */
kr_countUnits( new_unit_ptr, UNIT_ADD );
NetModified = TRUE;
return( new_unit_no );
}
}
/*#################################################
GROUP: Ftype Unit Functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_changeFtypeUnits
PURPOSE : changes all units in the network with the given functionality type
to the new functions of the (new) functionality type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_changeFtypeUnits(struct FtypeUnitStruct *Ftype_entry)
{
register struct Unit *unit_ptr;
if (NoOfUnits == 0) return; /* no units */
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr )
{ /* unit is in use */
if (unit_ptr->Ftype_entry == Ftype_entry)
{ /* unit with this type was found. Now change the transfer functions
of the unit to the modified functionality type */
unit_ptr->act_func = Ftype_entry->act_func;
unit_ptr->out_func = Ftype_entry->out_func;
unit_ptr->act_deriv_func = Ftype_entry->act_deriv_func;
unit_ptr->act_2_deriv_func = Ftype_entry->act_2_deriv_func;
}
}
NetModified = TRUE;
}
/*****************************************************************************
FUNCTION : kr_deleteUnitsFtype
PURPOSE : delete the functionality type of the units with the given type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_deleteUnitsFtype(struct FtypeUnitStruct *ftype_ptr)
{
register struct Unit *unit_ptr;
if (NoOfUnits == 0) return; /* no units */
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr )
/* unit is in use */
if (unit_ptr->Ftype_entry == ftype_ptr)
unit_ptr->Ftype_entry = NULL;
}
/*****************************************************************************
FUNCTION : kr_makeFtypeUnit
PURPOSE : create a new unit with the given functionality type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_makeFtypeUnit(char *Ftype_symbol)
{
register struct Site *ftype_site, *site_ptr;
struct Unit *unit_ptr;
struct FtypeUnitStruct *ftype_ptr;
int unit_no;
KernelErrorCode = KRERR_NO_ERROR;
if (!kr_symbolCheck( Ftype_symbol ))
return( KernelErrorCode );
if ((ftype_ptr = krm_FtypeSymbolSearch( Ftype_symbol ) ) == NULL)
{ /* Ftype name isn't defined */
KernelErrorCode = KRERR_FTYPE_SYMBOL;
return( KernelErrorCode );
}
unit_no = kr_makeDefaultUnit();
if (KernelErrorCode != KRERR_NO_ERROR)
return( KernelErrorCode );
unit_ptr = unit_array + unit_no;
unit_ptr->Ftype_entry = ftype_ptr;
unit_ptr->out_func = ftype_ptr->out_func;
unit_ptr->act_func = ftype_ptr->act_func;
unit_ptr->act_deriv_func = ftype_ptr->act_deriv_func;
unit_ptr->act_2_deriv_func = ftype_ptr->act_2_deriv_func;
ftype_site = ftype_ptr->sites;
/* make sites */
while (ftype_site != NULL)
{ /* Ftype has sites */
if ((site_ptr = krm_getSite()) == NULL)
{ /* memory alloc failed */
krm_releaseAllSites( unit_ptr->sites );
unit_ptr->sites = NULL;
KernelErrorCode = KRERR_INSUFFICIENT_MEM;
return( KernelErrorCode );
}
site_ptr->next = unit_ptr->sites;
unit_ptr->sites = site_ptr;
site_ptr->site_table = ftype_site->site_table;
ftype_site = ftype_site->next;
}
if (ftype_ptr->sites != NULL)
unit_ptr->flags |= UFLAG_SITES; /* unit has now sites */
return( unit_no );
}
/*****************************************************************************
FUNCTION : kr_FtypeSiteSearch
PURPOSE :
NOTES :
RETURNS : returns TRUE, if there exists the given site at the given ftype
entry
UPDATE :
******************************************************************************/
bool kr_FtypeSiteSearch(struct Site *ftype_first_site,
struct SiteTable *site_table_ptr)
{
register struct Site *site_ptr;
for (site_ptr = ftype_first_site; site_ptr != NULL; site_ptr = site_ptr->next)
if (site_ptr->site_table == site_table_ptr)
return( TRUE );
return( FALSE );
}
/*****************************************************************************
FUNCTION : kr_changeFtypeUnit
PURPOSE : change the properties of the given unit to the properties of the
given F-Type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_changeFtypeUnit(struct Unit *unit_ptr,
struct FtypeUnitStruct *ftype_ptr)
{
FlagWord flags;
struct Site *site_ptr,
*pred_site_ptr,
*tmp_ptr,
*ftype_site;
unit_ptr->out_func = ftype_ptr->out_func;
unit_ptr->act_func = ftype_ptr->act_func;
unit_ptr->act_deriv_func = ftype_ptr->act_deriv_func;
unit_ptr->act_2_deriv_func = ftype_ptr->act_2_deriv_func;
flags = unit_ptr->flags & UFLAG_INPUT_PAT;
switch (flags)
{
case UFLAG_NO_INP:
/* Unit has no inputs */
if (ftype_ptr->sites != NULL)
/* Ftype has sites, delete unit's Ftype */
unit_ptr->Ftype_entry = NULL;
else
/* Ftype and unit don't have sites */
unit_ptr->Ftype_entry = ftype_ptr; /* unit accept Ftype and inputs */
return; /* done ! */
case UFLAG_SITES:
/* Unit has sites */
ftype_site = ftype_ptr->sites;
if (ftype_site == NULL)
{ /* unit has sites, but Ftype hasn't sites,
delete unit's Ftype and all inputs */
unit_ptr->Ftype_entry = NULL;
kr_deleteAllInputs( unit_ptr );
unit_ptr->flags = UFLAG_INITIALIZED; /* unit has no inputs now ! */
}
else
{ /* both unit and Ftype have sites: check sites */
unit_ptr->Ftype_entry = ftype_ptr;
site_ptr = unit_ptr->sites;
pred_site_ptr = NULL;
do
{
if ( ! kr_FtypeSiteSearch( ftype_site, site_ptr->site_table ))
{ /* Ftype and unit site definitions are not equivalent:
remove site */
if (pred_site_ptr == NULL)
{ /* this is the first site at the unit */
unit_ptr->sites = site_ptr->next;
if (site_ptr->next == NULL)
/* unit don't has any inputs */
unit_ptr->flags &= (~UFLAG_INPUT_PAT);
}
else
{ /* this site isn't the first site at the unit */
pred_site_ptr->next = site_ptr->next;
pred_site_ptr = site_ptr;
}
/* work with temporary pointer and get */
tmp_ptr = site_ptr;
/* next site pointer BEFORE krm_releaseSite */
site_ptr = site_ptr->next;
/* (important in a multiprocessor system */
krm_releaseAllLinks( tmp_ptr->links );
krm_releaseSite( tmp_ptr );
/* delete unit's Ftype */
unit_ptr->Ftype_entry = NULL;
}
else
{
pred_site_ptr = site_ptr;
site_ptr = site_ptr->next;
}
}
while (site_ptr != NULL);
if (unit_ptr->sites == NULL)
unit_ptr->flags = UFLAG_INITIALIZED; /* unit has no inputs now ! */
}
return;
case UFLAG_DLINKS:
/* Unit has direct links */
if (ftype_ptr->sites != NULL)
{ /* unit has direct links, but Ftype entry has sites: delete links */
unit_ptr->Ftype_entry = NULL;
kr_deleteAllInputs( unit_ptr );
unit_ptr->flags = UFLAG_INITIALIZED; /* unit has no inputs now ! */
}
else
{ /* unit has direct links and Ftype has no sites: use direct links */
unit_ptr->Ftype_entry = ftype_ptr;
}
}
}
/*****************************************************************************
FUNCTION : kr_changeFtypeSites
PURPOSE : change a site at the F-Type
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_changeFtypeSites(struct FtypeUnitStruct *Ftype_entry,
struct SiteTable *old_site_table,
struct SiteTable *new_site_table)
{
struct Unit *unit_ptr;
struct Site *site_ptr;
if (NoOfUnits == 0) return; /* no units */
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr )
{ /* unit is in use */
if (unit_ptr->Ftype_entry == Ftype_entry)
{
FOR_ALL_SITES( unit_ptr, site_ptr )
if (site_ptr->site_table == old_site_table)
site_ptr->site_table = new_site_table;
}
}
NetModified = TRUE;
}
/*#################################################
GROUP: Miscellanous
#################################################*/
/*****************************************************************************
FUNCTION : kr_flags2TType
PURPOSE : translate unit flags to the topological type of the unit
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_flags2TType(int flags)
{
KernelErrorCode = KRERR_NO_ERROR;
switch (flags)
{
case UFLAG_TTYP_UNKN: return( UNKNOWN );
case UFLAG_TTYP_IN : return( INPUT );
case UFLAG_TTYP_OUT : return( OUTPUT );
case UFLAG_TTYP_DUAL: return( DUAL );
case UFLAG_TTYP_HIDD: return( HIDDEN );
case UFLAG_TTYP_SPEC: return( SPECIAL );
case UFLAG_TTYP_SPEC_I: return (SPECIAL_I) ;
case UFLAG_TTYP_SPEC_O: return (SPECIAL_O) ;
case UFLAG_TTYP_SPEC_H: return (SPECIAL_H) ;
case UFLAG_TTYP_SPEC_D: return (SPECIAL_D) ;
/* case UFLAG_TTYP_SPEC_X and
case UFLAG_TTYP_N_SPEC_X are no true TType*/
default: KernelErrorCode = KRERR_TTYPE;
return( UNKNOWN );
}
}
/*****************************************************************************
FUNCTION : kr_TType2Flags
PURPOSE : translate the topological type to unit flags
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_TType2Flags(int ttype)
{
KernelErrorCode = KRERR_NO_ERROR;
switch (ttype)
{
case UNKNOWN: return( UFLAG_TTYP_UNKN );
case INPUT : return( UFLAG_TTYP_IN );
case OUTPUT : return( UFLAG_TTYP_OUT );
case DUAL : return( UFLAG_TTYP_DUAL );
case HIDDEN : return( UFLAG_TTYP_HIDD );
case SPECIAL: return( UFLAG_TTYP_SPEC );
case SPECIAL_I: return (UFLAG_TTYP_SPEC_I) ;
case SPECIAL_O: return (UFLAG_TTYP_SPEC_O) ;
case SPECIAL_H: return (UFLAG_TTYP_SPEC_H) ;
case SPECIAL_D: return (UFLAG_TTYP_SPEC_D) ;
case SPECIAL_X: return (UFLAG_TTYP_SPEC_X) ;
case N_SPECIAL_X: return (UFLAG_TTYP_N_SPEC_X) ;
default: KernelErrorCode = KRERR_TTYPE;
/* return( KernelErrorCode ); */
return( -1 );
}
}
/*****************************************************************************
FUNCTION : kr_updateUnitOutputs
PURPOSE : update the outputs of all units in the network
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_updateUnitOutputs(void)
{
register struct Unit *unit_ptr;
FOR_ALL_UNITS( unit_ptr )
if ( (unit_ptr->flags & UFLAG_INITIALIZED) == UFLAG_INITIALIZED)
{ /* unit is in use and enabled */
if (unit_ptr->out_func == NULL)
/* Identity Function */
unit_ptr->Out.output = unit_ptr->act;
else
unit_ptr->Out.output = (*unit_ptr->out_func) (unit_ptr->act);
}
}
/*****************************************************************************
FUNCTION : kr_getNoOfUnits
PURPOSE : returns the no. of units of the specified topologic type
(i.e. Input, Hidden, Output or Special units)
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_getNoOfUnits(int UnitTType)
{
register struct Unit *unit_ptr;
register int no_of_units;
register FlagWord ttyp_flg;
int flg;
if ((NoOfUnits == 0) || ((flg = kr_TType2Flags( UnitTType )) == -1))
return( 0 ); /* no units or this topologic type doesn't exist */
ttyp_flg = (FlagWord) flg;
no_of_units = 0;
FOR_ALL_UNITS( unit_ptr )
if ( ((unit_ptr->flags & UFLAG_TTYP_PAT) == ttyp_flg) &&
UNIT_IN_USE( unit_ptr ) )
no_of_units++;
return( no_of_units );
}
/*****************************************************************************
FUNCTION : kr_getNoOfSpecialUnits
PURPOSE : returns the no. of special units of the specified topologic type
(i.e. Input, Hidden, Output or Special units)
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
int kr_getNoOfSpecialUnits(int UnitTType)
{
register struct Unit *unit_ptr;
register int no_of_units;
register FlagWord ttyp_flg;
int flg;
if ((NoOfUnits == 0) || ((flg = kr_TType2Flags( UnitTType )) == -1))
return( 0 ); /* no units or this topologic type doesn't exist */
ttyp_flg = (FlagWord) flg;
no_of_units = 0;
FOR_ALL_UNITS( unit_ptr )
if ( ((unit_ptr->flags & UFLAG_TTYP_PAT) == (ttyp_flg | UFLAG_TTYP_SPEC)) &&
UNIT_IN_USE( unit_ptr ) )
no_of_units++;
return( no_of_units );
}
/*****************************************************************************
FUNCTION : kr_forceUnitGC
PURPOSE : force unit array garbage collection
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
void kr_forceUnitGC(void)
{
krm_unitArrayGC();
}
/*#################################################
GROUP: Functions default presettings
#################################################*/
/*****************************************************************************
FUNCTION : kr_getUnitDefaults
PURPOSE :
NOTES :
RETURNS : Returns information about the unit default settings.
UPDATE :
******************************************************************************/
void kr_getUnitDefaults(FlintType *act, FlintType *bias, int *ttflags,
int *subnet_no, int *layer_no, char **act_func,
char **out_func)
{
static char activation_func[FUNCTION_NAME_MAX_LEN],
output_func[FUNCTION_NAME_MAX_LEN];
*act = DefaultIAct;
*bias = DefaultBias;
*ttflags = (int) DefaultSType;
*subnet_no = DefaultSubnetNo;
*layer_no = DefaultLayerNo;
strcpy( activation_func, krf_getCurrentNetworkFunc( ACT_FUNC ) );
*act_func = activation_func;
strcpy( output_func, krf_getCurrentNetworkFunc( OUT_FUNC ) );
*out_func = output_func;
}
/*****************************************************************************
FUNCTION : kr_setUnitDefaults
PURPOSE : Changes the unit default settings.
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
krui_err kr_setUnitDefaults(FlintTypeParam act, FlintTypeParam bias,
int ttflags, int subnet_no, int layer_no,
char *act_func, char *out_func)
{
FunctionPtr act_func_ptr,
act_deriv_func_ptr,
act_2_deriv_func_ptr,
out_func_ptr;
KernelErrorCode = KRERR_NO_ERROR;
if (!krf_funcSearch( act_func, ACT_FUNC, &act_func_ptr))
return( KernelErrorCode );
if (!krf_funcSearch( act_func, ACT_DERIV_FUNC, &act_deriv_func_ptr))
return( KernelErrorCode );
if (!krf_funcSearch( act_func, ACT_2_DERIV_FUNC, &act_2_deriv_func_ptr))
return( KernelErrorCode );
if (!krf_funcSearch( out_func, OUT_FUNC, &out_func_ptr))
return( KernelErrorCode );
if (krf_setCurrentNetworkFunc( act_func, ACT_FUNC ) != KRERR_NO_ERROR)
return( KernelErrorCode );
if (krf_setCurrentNetworkFunc( out_func, OUT_FUNC ) != KRERR_NO_ERROR)
return( KernelErrorCode );
DefaultIAct = (FlintType) act;
DefaultBias = (FlintType) bias;
DefaultSType = (FlagWord) ttflags;
DefaultPosX = DEF_POS_X;
DefaultPosY = DEF_POS_Y;
DefaultPosZ = DEF_POS_Z;
DefaultSubnetNo = subnet_no;
DefaultLayerNo = layer_no;
DefaultUFuncOut = (OutFuncPtr) out_func_ptr;
DefaultUFuncAct = (ActFuncPtr) act_func_ptr;
DefaultUFuncActDeriv = (ActDerivFuncPtr) act_deriv_func_ptr;
DefaultUFuncAct2Deriv = (ActDerivFuncPtr) act_2_deriv_func_ptr;
return( KernelErrorCode );
}
/*#################################################
GROUP: Topological Sorting Functions
#################################################*/
/*****************************************************************************
FUNCTION : clr_T_flags
PURPOSE : Clears the 'touch' (refresh) flag of all units
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static void clr_T_flags(void)
{
register struct Unit *unit_ptr;
FOR_ALL_UNITS( unit_ptr )
if (UNIT_IN_USE( unit_ptr ))
{
unit_ptr->flags &= ~UFLAG_REFRESH;
unit_ptr->lln = 0;
}
}
/*****************************************************************************
FUNCTION : DepthFirst1
PURPOSE : Depth search routine for topological sorting
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static void DepthFirst1(struct Unit *unit_ptr, int depth)
{
struct Site *site_ptr;
struct Link *link_ptr;
if (unit_ptr->flags & UFLAG_REFRESH)
{ /* the 'touch' flag is set: don't continue search */
if (unit_ptr->lln == 0)
{ /* logical layer no. isn't set => Cycle found */
topo_msg.no_of_cycles++;
if (topo_msg.error_code == KRERR_NO_ERROR)
{ /* remember the cycle unit */
topo_msg.src_error_unit = unit_ptr - unit_array;
topo_msg.error_code = KRERR_CYCLES;
}
}
return;
}
else
/* set the 'touch' flag */
unit_ptr->flags |= UFLAG_REFRESH;
switch (unit_ptr->flags & UFLAG_INPUT_PAT)
{
case UFLAG_DLINKS: /* unit has direct links */
FOR_ALL_LINKS( unit_ptr, link_ptr )
DepthFirst1( link_ptr->to, depth + 1 ); /* increase depth */
break;
case UFLAG_SITES: /* unit has sites */
FOR_ALL_SITES_AND_LINKS( unit_ptr, site_ptr, link_ptr )
DepthFirst1( link_ptr->to, depth + 1 ); /* increase depth */
break;
}
/* remember the depth (for cycle detection and statistics) */
unit_ptr->lln = depth;
*global_topo_ptr++ = unit_ptr; /* store sorted unit pointer */
}
/*****************************************************************************
FUNCTION : DepthFirst2
PURPOSE : Depth search routine for topology check function
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static void DepthFirst2(struct Unit *unit_ptr, int depth)
{
struct Site *site_ptr;
struct Link *link_ptr;
if (unit_ptr->flags & UFLAG_REFRESH)
{ /* the 'touch' flag is set: don't continue search */
if (unit_ptr->lln == 0)
{ /* logical layer no. isn't set => Cycle found */
topo_msg.no_of_cycles++;
if (topo_msg.error_code == KRERR_NO_ERROR)
{ /* remember the cycle unit */
topo_msg.src_error_unit = unit_ptr - unit_array;
topo_msg.error_code = KRERR_CYCLES;
}
}
return;
}
else
/* set the 'touch' flag */
unit_ptr->flags |= UFLAG_REFRESH;
switch (unit_ptr->flags & UFLAG_INPUT_PAT)
{
case UFLAG_DLINKS: /* unit has direct links */
FOR_ALL_LINKS( unit_ptr, link_ptr )
DepthFirst2( link_ptr->to, depth + 1 ); /* increase depth */
break;
case UFLAG_SITES: /* unit has sites */
FOR_ALL_SITES_AND_LINKS( unit_ptr, site_ptr, link_ptr )
DepthFirst2( link_ptr->to, depth + 1 ); /* increase depth */
break;
}
/* remember the depth (for cycle detection and statistics) */
unit_ptr->lln = depth;
/* store highest layer no. */
if (depth > topo_msg.no_of_layers) topo_msg.no_of_layers = depth;
}
/*****************************************************************************
FUNCTION : DepthFirst3
PURPOSE : Depth search routine for topological sorting in feedforward networks
NOTES :
RETURNS :
UPDATE :
******************************************************************************/
static void DepthFirst3(struct Unit *unit_ptr, int depth)
{
struct Site *site_ptr;
struct Link *link_ptr;
if (unit_ptr->flags & UFLAG_REFRESH)
{ /* the 'touch' flag is set: don't continue search */
topo_msg.src_error_unit = unit_ptr - unit_array; /* store unit number */
if IS_OUTPUT_UNIT( unit_ptr )
{ /* this output unit has a output connection to another unit */
if (topo_msg.error_code == KRERR_NO_ERROR)
topo_msg.error_code = KRERR_O_UNITS_CONNECT;
}
else
if (unit_ptr->lln == 0)
{ /* logical layer no. isn't set => Cycle found */
topo_msg.no_of_cycles++;
if (topo_msg.error_code == KRERR_NO_ERROR)
topo_msg.error_code = KRERR_CYCLES;
}
return;
}
else
/* set the 'touch' flag */
unit_ptr->flags |= UFLAG_REFRESH;
switch (unit_ptr->flags & UFLAG_INPUT_PAT)
{
case UFLAG_DLINKS: /* unit has direct links */
FOR_ALL_LINKS( unit_ptr, link_ptr )
DepthFirst3( link_ptr->to, depth + 1 ); /* increase depth */
break;
case UFLAG_SITES: /* unit has sites */
FOR_ALL_SITES_AND_LINKS( unit_ptr, site_ptr, link_ptr )
DepthFirst3( link_ptr->to, depth + 1 ); /* increase depth */
break;
}
/* remember the depth (for cycle detection and statistics) */
unit_ptr->lln = depth;
/* store only hidden units */
if IS_HIDDEN_UNIT( unit_ptr )
*global_topo_ptr++ = unit_ptr; /* store sorted unit pointer */
}
/*****************************************************************************
FUNCTION : kr_topoSortT
PURPOSE : Sort units topological (general version) and stores the
pointers to this units in the topologic array
NOTES : Units are not sorted by their topologic type (that's not possible
in the general case)
RETURNS : error code
UPDATE :
******************************************************************************/
static krui_err kr_topoSortT(void)
{
register struct Unit *unit_ptr;
int io_units;
KernelErrorCode = KRERR_NO_ERROR; /* reset return code */
clr_T_flags(); /* reset units 'touch' flags */
global_topo_ptr = topo_ptr_array; /* initialize global pointer */
/* limit left side of the topological array with NULL pointer */
*global_topo_ptr++ = NULL;
/* put all input units in the topologic array */
io_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_INPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
io_units++; /* there is a input unit */
if ((NoOfInputUnits = io_units) == 0)
{ /* no input units */
KernelErrorCode = KRERR_NO_INPUT_UNITS;
return( KernelErrorCode );
}
/* begin depth search at the first output unit */
io_units = 0;
FOR_ALL_UNITS( unit_ptr )
if ( IS_OUTPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ) )
{
io_units++; /* there is a output unit */
/* sort the units topological (using depth search algorithm,
starting at this output unit */
DepthFirst1( unit_ptr, 1 );
if (topo_msg.error_code != KRERR_NO_ERROR)
{ /* stop if an error occured */
KernelErrorCode = topo_msg.error_code;
return( KernelErrorCode );
}
}
if ((NoOfOutputUnits = io_units) == 0)
{ /* no output units */
KernelErrorCode = KRERR_NO_OUTPUT_UNITS;
return( KernelErrorCode );
}
/* limit right side of the topologic array with NULL pointer */
*global_topo_ptr++ = NULL;
/* calc. no. of sorted units */
no_of_topo_units = (global_topo_ptr - topo_ptr_array) - 2;
/* search for dead units i.e. units without inputs */
FOR_ALL_UNITS( unit_ptr )
if ( !(unit_ptr->flags & (UFLAG_REFRESH | UFLAG_TTYP_SPEC)) &&
UNIT_IN_USE( unit_ptr ) )
{
topo_msg.no_of_dead_units++;
if (topo_msg.src_error_unit == 0)
topo_msg.src_error_unit = unit_ptr - unit_array; /* store the unit no.*/
}
if (topo_msg.no_of_dead_units != 0)
KernelErrorCode = KRERR_DEAD_UNITS;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_topoSortT
PURPOSE : Sorts units topological in feed-forward networks and stores the
pointers to these units in the topologic array in the following
order:
- input,
- hidden and
- output units
This function make following assumtions (like all learning
functions for feed-forward networks):
a) input units doesn't have input connections to other units and
b) output units doesn't have outputs connections to other units.
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
static krui_err kr_topoSortFF(void)
{
register struct Unit *unit_ptr;
int io_units;
KernelErrorCode = KRERR_NO_ERROR; /* reset return code */
clr_T_flags(); /* reset units 'touch' flags */
global_topo_ptr = topo_ptr_array; /* initialize global pointer */
/* limit left side of the topological array with NULL pointer */
*global_topo_ptr++ = NULL;
/* put all input units in the topologic array */
io_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_INPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{
if UNIT_HAS_INPUTS( unit_ptr )
{ /* this input unit has a connection to another unit */
/* store the unit no. */
topo_msg.dest_error_unit = unit_ptr - unit_array;
KernelErrorCode = KRERR_I_UNITS_CONNECT; /* input unit has input */
return( KernelErrorCode );
}
io_units++; /* there is a input unit */
*global_topo_ptr++ = unit_ptr; /* save input unit */
}
if ((NoOfInputUnits = io_units) == 0)
{ /* no input units */
KernelErrorCode = KRERR_NO_INPUT_UNITS;
return( KernelErrorCode );
}
/* limit input units in the topological array with NULL pointer */
*global_topo_ptr++ = NULL;
/* begin depth search at the first output unit */
io_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_OUTPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{
io_units++; /* there is a output unit */
/* sort the units topological (using depth search algorithm,
starting at this output unit */
DepthFirst3( unit_ptr, 1 );
if (topo_msg.error_code != KRERR_NO_ERROR)
{ /* stop if an error occured */
KernelErrorCode = topo_msg.error_code;
return( KernelErrorCode );
}
}
if ((NoOfOutputUnits = io_units) == 0)
{ /* no output units */
KernelErrorCode = KRERR_NO_OUTPUT_UNITS;
return( KernelErrorCode );
}
/* limit hidden units in the topological array with NULL pointer */
*global_topo_ptr++ = NULL;
/* put all output units in the topological array */
FOR_ALL_UNITS( unit_ptr )
if (IS_OUTPUT_UNIT(unit_ptr ) && UNIT_IN_USE( unit_ptr ))
*global_topo_ptr++ = unit_ptr; /* save output unit */
/* limit right side of the topologic array with NULL pointer */
*global_topo_ptr++ = NULL;
/* calc. no. of sorted units */
no_of_topo_units = (global_topo_ptr - topo_ptr_array) - 4;
/* search for dead units i.e. units without inputs */
FOR_ALL_UNITS( unit_ptr )
if (!(unit_ptr->flags & (UFLAG_REFRESH | UFLAG_TTYP_SPEC)) && UNIT_IN_USE( unit_ptr ))
{
topo_msg.no_of_dead_units++;
if (topo_msg.src_error_unit == 0)
topo_msg.src_error_unit = unit_ptr - unit_array; /* store unit no. */
}
if (topo_msg.no_of_dead_units != 0)
KernelErrorCode = KRERR_DEAD_UNITS;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_topoSortIHO
PURPOSE : Sort units by their topologic type, i.e. Input, Hidden, Output
units and stores the pointers to this units in the topologic array.
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
static krui_err kr_topoSortIHO(void)
{
TopoPtrArray topo_ptr;
register struct Unit *unit_ptr;
int no_of_units;
int has_no_dual;
KernelErrorCode = KRERR_NO_ERROR; /* reset return code */
topo_ptr = topo_ptr_array;
/* limit left side of the topological array with NULL pointer */
*topo_ptr++ = NULL;
/* get input units */
no_of_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_INPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{
*topo_ptr++ = unit_ptr;
no_of_units++;
}
if ((NoOfInputUnits = no_of_units) == 0)
{
KernelErrorCode = KRERR_NO_INPUT_UNITS;
return( KernelErrorCode );
}
/* limit input units in the topological array with NULL pointer */
*topo_ptr++ = NULL;
/* get hidden units */
no_of_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_HIDDEN_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{
*topo_ptr++ = unit_ptr;
no_of_units++;
}
if ((NoOfHiddenUnits = no_of_units) == 0)
{
/* In special case for DUAL units */
FOR_ALL_UNITS( unit_ptr )
if (IS_DUAL_UNIT( unit_ptr ) )
has_no_dual = FALSE;
if ( has_no_dual ){
KernelErrorCode = KRERR_NO_HIDDEN_UNITS;
return( KernelErrorCode );
}
}
/* limit hidden units in the topological array with NULL pointer */
*topo_ptr++ = NULL;
/* get output units */
no_of_units = 0;
FOR_ALL_UNITS( unit_ptr )
if (IS_OUTPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ))
{
*topo_ptr++ = unit_ptr;
no_of_units++;
}
if ((NoOfOutputUnits = no_of_units) == 0)
{
KernelErrorCode = KRERR_NO_OUTPUT_UNITS;
return( KernelErrorCode );
}
/* limit right side of the topologic array with NULL pointer */
*topo_ptr++ = NULL;
/* calc. no. of sorted units */
no_of_topo_units = (topo_ptr - topo_ptr_array) - 4;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_topoSortLOG()
PURPOSE : Sort units by their logical Layer- and Unitnumber
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
static int llncompare(const struct Unit **a, const struct Unit **b)
{
int llndiff, lundiff;
llndiff = ((*a)->lln - (*b)->lln);
lundiff = ((*a)->lun - (*b)->lun);
return(llndiff != 0 ? llndiff : lundiff);
}
static krui_err kr_topoSortLOG(void)
{
struct Unit *unit_ptr;
TopoPtrArray topo_ptr;
TopoPtrArray topo_ptr_save;
int no_of_units = 0;
topo_ptr = topo_ptr_array;
*topo_ptr++ = (struct Unit *) NULL;
FOR_ALL_UNITS( unit_ptr )
if ( (unit_ptr->flags & UFLAG_IN_USE) == UFLAG_IN_USE)
{
*topo_ptr = unit_ptr;
topo_ptr++;
no_of_units++;
}
*topo_ptr = (struct Unit *) NULL;
topo_ptr_save = topo_ptr;
no_of_topo_units = no_of_units;
topo_ptr = topo_ptr_array;
topo_ptr++;
qsort(topo_ptr, no_of_units, sizeof(*topo_ptr),
(int (*)(const void *, const void *)) llncompare);
/* insert NULL pointer between input units and rest */
topo_ptr = topo_ptr_save;
while (*topo_ptr == (struct Unit *) NULL ||
!(IS_INPUT_UNIT(*topo_ptr)))
{
*(topo_ptr + 1) = *topo_ptr;
topo_ptr--;
}
topo_ptr++;
*topo_ptr = (struct Unit *) NULL;
topo_ptr_save++;
/* insert NULL pointer between output units and rest */
topo_ptr = topo_ptr_save;
while (*topo_ptr == (struct Unit *) NULL ||
(IS_OUTPUT_UNIT(*topo_ptr)))
{
*(topo_ptr + 1) = *topo_ptr;
topo_ptr--;
}
topo_ptr++;
*topo_ptr = (struct Unit *) NULL;
topo_ptr_save++;
/* create pointers from units to topo_ptr_array */
topo_ptr = topo_ptr_array;
while (topo_ptr != topo_ptr_save)
{
if (*topo_ptr != (struct Unit *) NULL)
{
(*topo_ptr) -> TD.my_topo_ptr = topo_ptr;
}
topo_ptr++;
}
return (KRERR_NO_ERROR);
}
/*****************************************************************************
FUNCTION : kr_topoSort
PURPOSE :
Sort units according to the given mode:
TOPOLOGICAL:
Sort units topological (general version) and stores the
pointers to this units in the topologic array.
NOTE: Units are not sorted by their topologic type (that's not
possible in general case).
TOPOLOGICAL_FF:
Sorts unit topological in feed-forward networks and stores the
pointers to this units in the topologic array in the following order:
- input,
- hidden and
- output units
This function make following assumtions (like all learning functions for
feed-forward networks):
a) input units doesn't have input connections to other units and
b) output units doesn't have outputs connections to other units.
TOPOLOGIC_TYPE:
Sort units by their topologic type, i.e. Input, Hidden, Output units and
stores the pointers to this units in the topologic array.
TOPOLOGIC_LOGICAL:
Sort Units according to their logical Layers- and Unitsnumbers.
The entry TD.my_topo_ptr in every unit is set to point to coresponding
entry in the topo_ptr_array.
ART1_TOPO_TYPE:
Sort units in ART1 manner. For informations about the structure of
ART1 networks see Diplomarbeit No.929; Kai-Uwe Herrmann; University of
Stuttgart; Germany 1992. The pointers are sorted as follows:
NULL, pointers to input units, NULL, pointers to comparison units,
NULL, pointers to recognition units, NULL, pointers to delay units,
NULL, pointers to local reset units, NULL, pointers to special units,
NULL, NULL, ...
ART2_TOPO_TYPE:
Sort units in ART2 manner. For informations about the structure of
ART2 networks see Diplomarbeit No.929; Kai-Uwe Herrmann; University of
Stuttgart; Germany 1992. The pointers are sorted as follows:
NULL, pointers to input units, NULL, pointers to w units,
NULL, pointers to x units, NULL, pointers to u units,
NULL, pointers to v units, NULL, pointers to p units,
NULL, pointers to q units, NULL, pointers to r units,
NULL, pointers to recognition units, NULL, pointers to delay units,
NULL, pointers to local reset units, NULL, pointers to special units,
NULL, NULL, ...
ARTMAP_TOPO_TYPE:
Sort units in ARTMAP manner. For informations about the structure of
ARTMAP networks see Diplomarbeit No.929; Kai-Uwe Herrmann; University of
Stuttgart; Germany 1992. The pointers are sorted as follows:
NULL, ARTa inp units, NULL, ARTa cmp units, NULL, ARTa rec units ...,
NULL, ARTb inp units, NULL, ARTb cmp units, NULL, ARTb rec units ...,
NULL, map field units, NULL, map field special units, NULL
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
krui_err kr_topoSort(int topo_sorting_mode)
{
KernelErrorCode = KRERR_NO_ERROR; /* reset return code */
TopoSortID = NOT_SORTED;
if (NoOfUnits == 0)
{ /* No units defined */
KernelErrorCode = KRERR_NO_UNITS;
return( KernelErrorCode );
}
if (krm_allocUnitTopoArray( NoOfUnits + 15) != KRERR_NO_ERROR)
return( KernelErrorCode );
/* clear error codes */
topo_msg.no_of_cycles = topo_msg.no_of_dead_units =
topo_msg.dest_error_unit = topo_msg.src_error_unit = 0;
topo_msg.error_code = KRERR_NO_ERROR;
switch (topo_sorting_mode)
{
case TOPOLOGICAL:
(void) kr_topoSortT();
break;
case TOPOLOGICAL_FF:
(void) kr_topoSortFF();
break;
case TOPOLOGIC_TYPE:
(void) kr_topoSortIHO();
break;
case TOPOLOGIC_LOGICAL:
KernelErrorCode = kr_topoSortLOG();
break;
case ART1_TOPO_TYPE:
KernelErrorCode = kra1_sort ();
break;
case ART2_TOPO_TYPE:
KernelErrorCode = kra2_sort ();
break;
case ARTMAP_TOPO_TYPE:
KernelErrorCode = kram_sort ();
break;
case TOPOLOGICAL_CC:
(void) cc_topoSort(TOPOLOGICAL_CC);
break;
case TOPOLOGICAL_RCC:
(void) cc_topoSort(TOPOLOGICAL_RCC);
break;
case TOPOLOGICAL_BCC:
(void) cc_topoSort(TOPOLOGICAL_BCC);
break;
case TOPOLOGICAL_JE:
KernelErrorCode = kr_topoSortJE () ;
break ;
default:
KernelErrorCode = KRERR_TOPOMODE;
}
if ((KernelErrorCode == KRERR_NO_ERROR) ||
(KernelErrorCode == KRERR_DEAD_UNITS))
TopoSortID = topo_sorting_mode;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_topoCheck
PURPOSE : Checks the topology of the network:
a) counts the number of layers of the network and
b) determines if the network has cycles.
NOTES :
RETURNS : Returns the no. of layers of the network.
UPDATE :
******************************************************************************/
int kr_topoCheck(void)
{
struct Unit *unit_ptr;
bool o_units;
topo_msg.no_of_cycles = topo_msg.no_of_dead_units =
topo_msg.dest_error_unit = topo_msg.src_error_unit =
topo_msg.no_of_layers = 0;
topo_msg.error_code = KernelErrorCode = KRERR_NO_ERROR;
if (NoOfUnits == 0)
{ /* no units defined */
KernelErrorCode = KRERR_NO_UNITS;
return( KernelErrorCode );
}
clr_T_flags(); /* reset units 'touch' flags */
/* begin depth search at the first output unit */
o_units = FALSE;
FOR_ALL_UNITS( unit_ptr )
if ( IS_OUTPUT_UNIT( unit_ptr ) && UNIT_IN_USE( unit_ptr ) )
{
o_units = TRUE;
DepthFirst2( unit_ptr, 1 );
if (topo_msg.error_code != KRERR_NO_ERROR)
{ /* stop if an error occured */
KernelErrorCode = topo_msg.error_code;
return( KernelErrorCode );
}
}
if (!o_units)
{ /* no output units */
KernelErrorCode = KRERR_NO_OUTPUT_UNITS;
return( KernelErrorCode );
}
/* return the no. of layers of the network */
return( topo_msg.no_of_layers );
}
/*****************************************************************************
FUNCTION : kr_makeUnitPermutation
PURPOSE :
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
krui_err kr_makeUnitPermutation(void)
{
register struct Unit *unit_ptr;
register int no_of_units, i;
TopoPtrArray topo_ptr, t_ptr1, t_ptr2;
TopoSortID = NOT_SORTED;
if (NoOfUnits == 0) return( KRERR_NO_UNITS ); /* no units defined */
if ( krm_allocUnitTopoArray( NoOfUnits + 2) != 0)
return( KRERR_INSUFFICIENT_MEM );
topo_ptr = topo_ptr_array;
/* limit left side of the topological array with NULL pointer */
*topo_ptr++ = NULL;
/* initialize permutation array */
FOR_ALL_UNITS( unit_ptr )
if ( (unit_ptr->flags & UFLAG_INITIALIZED) == UFLAG_INITIALIZED)
/* unit is in use and enabled */
*topo_ptr++ = unit_ptr;
no_of_topo_units = topo_ptr - topo_ptr_array; /* calc no. of sorted units */
no_of_units = no_of_topo_units;
topo_ptr = topo_ptr_array;
/* permutate unit order */
for (i = 0; i < no_of_units; i++)
{
t_ptr1 = topo_ptr + (lrand48() % no_of_units);
t_ptr2 = topo_ptr + (lrand48() % no_of_units);
unit_ptr = *t_ptr1;
*t_ptr1 = *t_ptr2;
*t_ptr2 = unit_ptr;
}
/* limit right side of the topologic array with NULL pointer */
*topo_ptr++ = NULL;
TopoSortID = PERMUTATION;
NetModified = FALSE;
return( KRERR_NO_ERROR );
}
/*#################################################
GROUP: Functions for pattern management
#################################################*/
/*****************************************************************************
FUNCTION : kr_IOCheck
PURPOSE : Count the no. of input and output units and return an error code
if the no. do not fit to the loaded patterns.
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
krui_err kr_IOCheck(void)
{
register struct Unit *unit_ptr;
register int no_of_i_units, no_of_o_units;
KernelErrorCode = KRERR_NO_ERROR; /* reset return code */
/* count no. of input and output units */
no_of_i_units = no_of_o_units = 0;
FOR_ALL_UNITS( unit_ptr )
if UNIT_IN_USE( unit_ptr ){
if IS_INPUT_UNIT( unit_ptr )
no_of_i_units++;
if IS_OUTPUT_UNIT( unit_ptr )
no_of_o_units++;
}
NoOfInputUnits = no_of_i_units;
NoOfOutputUnits = no_of_o_units;
return( KernelErrorCode );
}
/*#################################################
GROUP: other functions
#################################################*/
/*****************************************************************************
FUNCTION : kr_NA_Error
PURPOSE : calculates the error for the network-analyzer tool
NOTES :
RETURNS : Returns the float value of the error
UPDATE :
******************************************************************************/
float kr_NA_Error(int currentPattern, int error_unit, int error, bool ave)
{
register struct Unit *unit_ptr, *error_unit_ptr ;
register Patterns out_pat ;
register float error_lin, error_sqr, error_su, devit ;
int pattern_no, sub_pat_no;
kr_initSubPatternOrder(currentPattern, currentPattern);
kr_getSubPatternByOrder(&pattern_no, &sub_pat_no);
out_pat = kr_getSubPatData(pattern_no,sub_pat_no,OUTPUT, NULL);
error_lin = 0 ;
error_sqr= 0 ;
error_su = 0 ;
if (error_unit != 0)
error_unit_ptr = kr_getUnitPtr (error_unit) ;
FOR_ALL_UNITS (unit_ptr)
{
if (IS_OUTPUT_UNIT (unit_ptr))
{
devit = (float) (*(out_pat++) - unit_ptr->Out.output) ;
error_lin += fabs (devit) ;
error_sqr += devit * devit ;
if (unit_ptr == error_unit_ptr) error_su = fabs (devit) ;
}
}
switch (error)
{
case NA_ERROR_LIN :
{
if (ave) return (error_lin / (float) NoOfOutputUnits);
else return (error_lin) ;
break ;
}
case NA_ERROR_SQR :
{
if (ave) return (error_sqr / (float) NoOfOutputUnits);
else return (error_sqr) ;
break ;
}
case NA_ERROR_SU :
{
return (error_su) ;
break ;
}
}
/* Only for the warning */
return(0.0);
}
/*#################################################
GROUP: Functions for handeling network propagation,
update and learning functions.
#################################################*/
/*****************************************************************************
FUNCTION : kr_callNetworkFunctionSTD
PURPOSE : calls the current network function
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
static krui_err kr_callNetworkFunctionSTD(int type, float *parameterInArray,
int NoOfInParams,
float **parameterOutArray,
int *NoOfOutParams,
int start_pattern, int end_pattern)
{
FunctionPtr func_ptr;
NetFunctionPtr net_func_ptr;
char *curr_func;
int size;
if ( (curr_func = krf_getCurrentNetworkFunc( type )) == NULL)
return( KernelErrorCode );
if (!krf_funcSearch( curr_func, type, &func_ptr ) )
return( KernelErrorCode );
KernelErrorCode = KRERR_NO_ERROR;
net_func_ptr = (NetFunctionPtr) func_ptr;
switch (type) {
case UPDATE_FUNC:
KernelErrorCode =
(*(UpdateFuncPtr)net_func_ptr) ( parameterInArray, NoOfInParams );
return( KernelErrorCode );
case TEST_FUNC:
case LEARN_FUNC:
if (kr_TotalNoOfPattern() == 0)
{ /* no patterns defined */
KernelErrorCode = KRERR_NO_PATTERNS;
return( KernelErrorCode );
}
if ((start_pattern < 0) || (end_pattern >= kr_TotalNoOfPattern()) )
{ /* Invalid pattern number */
KernelErrorCode = KRERR_PATTERN_NO;
return( KernelErrorCode );
}
/* check whether sub pattern fits onto network */
if (NetModified)
kr_IOCheck();
size = kr_SizeOfInputSubPat();
if (NoOfInputUnits != size)
{
if (size < 0)
KernelErrorCode = size;
else
KernelErrorCode = KRERR_NP_DOES_NOT_FIT;
return KernelErrorCode;
}
size = kr_SizeOfOutputSubPat();
if (NoOfOutputUnits != size)
{
if (size < 0)
KernelErrorCode = size;
else if (size == 0)
KernelErrorCode = KRERR_NP_NO_OUTPUT_PATTERN;
else
KernelErrorCode = KRERR_NP_DOES_NOT_FIT;
return KernelErrorCode;
}
/* call current learning function */
KernelErrorCode =
(*(LearnFuncPtr)net_func_ptr) (start_pattern, end_pattern,
parameterInArray, NoOfInParams,
parameterOutArray, NoOfOutParams);
if (KernelErrorCode == KRERR_NO_ERROR)
{ /* learning function has initialized the network */
NetInitialize = FALSE;
LearnFuncHasChanged = FALSE;
}
return( KernelErrorCode );
case (FF_LEARN_FUNC | LEARN_FUNC):
/* check whether sub pattern fits onto network */
if (NetModified)
kr_IOCheck();
size = kr_SizeOfInputSubPat();
if (NoOfInputUnits != size)
{
if (size < 0)
KernelErrorCode = size;
else
KernelErrorCode = KRERR_NP_DOES_NOT_FIT;
return KernelErrorCode;
}
size = kr_SizeOfOutputSubPat();
if (NoOfOutputUnits != size)
{
if (size < 0)
KernelErrorCode = size;
else if (size == 0)
KernelErrorCode = KRERR_NP_NO_OUTPUT_PATTERN;
else
KernelErrorCode = KRERR_NP_DOES_NOT_FIT;
return KernelErrorCode;
}
/* call embedded feed forward learning function for pruning */
KernelErrorCode =
(*(LearnFuncPtr) net_func_ptr) (start_pattern, end_pattern,
parameterInArray, NoOfInParams,
parameterOutArray, NoOfOutParams);
if (KernelErrorCode == KRERR_NO_ERROR)
/* learning function has initialized the network */
{
NetInitialize = FALSE;
LearnFuncHasChanged = FALSE;
}
return( KernelErrorCode );
case INIT_FUNC:
NetInitialize = TRUE;
KernelErrorCode =
(*(InitFuncPtr)net_func_ptr) ( parameterInArray, NoOfInParams );
return( KernelErrorCode );
default:
KernelErrorCode = KRERR_PARAMETERS;
return( KernelErrorCode );
}
}
/*****************************************************************************
FUNCTION : kr_callNetworkFunction
PURPOSE : calls the current network function
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
krui_err kr_callNetworkFunction(int type, float *parameterInArray,
int NoOfInParams, float **parameterOutArray,
int *NoOfOutParams, int start_pattern,
int end_pattern)
{
#ifdef MASPAR_KERNEL
static struct NetFuncParameters net_func_params;
#endif
krui_err dummy;
if (NoOfUnits == 0)
{ /* No Units defined */
KernelErrorCode = KRERR_NO_UNITS;
return( KRERR_NO_UNITS );
}
KernelErrorCode = KRERR_NO_ERROR;
switch (specialNetworkType) {
case NET_TYPE_GENERAL:
/* normal network presentation */
/* the result of this call has been void which is not compatible */
/* to the declaration; therefor the dummy error variable is included */
dummy = kr_callNetworkFunctionSTD( type, parameterInArray, NoOfInParams,
parameterOutArray, NoOfOutParams,
start_pattern, end_pattern );
break;
#ifdef MASPAR_KERNEL
case NET_TYPE_FF1:
/* feedforward net on MasPar */
net_func_params.start_pattern_no = start_pattern;
net_func_params.end_pattern_no = end_pattern;
net_func_params.no_of_input_parameters = NoOfInParams;
memcpy( net_func_params.input_parameters, parameterInArray,
sizeof (float) * NoOfInParams );
(void) krff_callMasParNetworkFunction( type, &net_func_params );
if (NoOfOutParams != NULL)
*NoOfOutParams = net_func_params.no_of_output_parameters;
if (parameterOutArray != NULL)
*parameterOutArray = net_func_params.output_parameters;
break;
#endif
default:
KernelErrorCode = KRERR_PARAMETERS;
}
return( KernelErrorCode );
}
#ifdef HAVE_QSORT
static int transTableCompare( const void *node1, const void *node2)
{
short z1, z2;
z1=((struct TransTable *) node1)->z;
z2=((struct TransTable *) node2)->z;
if (z1 < z2) return -1;
if (z1 > z2) return 1;
return 0;
}
#endif
krui_err kr_xyTransTable(int op, int *x, int *y, int z)
{
struct TransTable *transTablePtr,
*new_transTable,
transTableEntry;
switch(op) {
case OP_TRANSTABLE_GET:
if (transTable != NULL) {
#ifdef HAVE_QSORT
transTableEntry.z = z;
transTablePtr =
(struct TransTable *) bsearch( &transTableEntry,
(struct TransTable *) transTable,
transTableSize,
sizeof(struct TransTable),
transTableCompare );
#else
for (transTablePtr = transTable;
transTablePtr < (transTable + transTableSize);
transTablePtr++)
if (transTablePtr->z == z) break;
if (transTablePtr == (transTable + transTableSize))
transTablePtr=NULL;
#endif
if (transTablePtr == NULL) {
*x = *y = 0;
}
else {
*x=transTablePtr->x;
*y=transTablePtr->y;
}
}
else {
*x = *y = 0;
}
KernelErrorCode = KRERR_NO_ERROR;
return( KRERR_NO_ERROR );
case OP_TRANSTABLE_SET:
if (transTable == NULL) {
if((new_transTable =
(struct TransTable *) malloc( sizeof(struct TransTable) )) == NULL){
KernelErrorCode = KRERR_INSUFFICIENT_MEM;
return( KRERR_INSUFFICIENT_MEM );
}
transTable=new_transTable;
transTable->z = z;
transTable->x = *x;
transTable->y = *y;
transTableSize=1;
}
else {
#ifdef HAVE_QSORT
transTableEntry.z = z;
transTablePtr =
(struct TransTable *) bsearch( &transTableEntry,
(struct TransTable *) transTable,
transTableSize,
sizeof(struct TransTable),
transTableCompare );
#else
for (transTablePtr = transTable;
transTablePtr < (transTable + transTableSize);
transTablePtr++)
if (transTablePtr->z == z) break;
if (transTablePtr == (transTable + transTableSize))
transTablePtr=NULL;
#endif
if (transTablePtr == NULL) {
if ((new_transTable =
(struct TransTable *) realloc( (void *) transTable,
sizeof(struct TransTable) * (transTableSize + 1) )) == NULL) {
KernelErrorCode = KRERR_INSUFFICIENT_MEM;
return( KRERR_INSUFFICIENT_MEM );
}
transTable=new_transTable;
transTable[transTableSize].z = z;
transTable[transTableSize].x = *x;
transTable[transTableSize].y = *y;
++transTableSize;
#ifdef HAVE_QSORT
qsort( (struct TransTable *) transTable,
transTableSize,
sizeof(struct TransTable),
transTableCompare );
#endif
}
else {
transTablePtr->x = *x;
transTablePtr->y = *y;
}
}
KernelErrorCode=KRERR_NO_ERROR;
return( KRERR_NO_ERROR );
case OP_TRANSTABLE_CLEAR:
if (transTable != NULL) {
free( (void *) transTable );
transTable = NULL;
transTableSize = 0;
}
KernelErrorCode=KRERR_NO_ERROR;
return( KRERR_NO_ERROR );
default:
KernelErrorCode=KRERR_PARAMETERS;
return( KRERR_PARAMETERS );
}
}
/*#################################################
GROUP: Functions for the parallel kernel
#################################################*/
/*****************************************************************************
FUNCTION : kr_setSpecialNetworkType
PURPOSE : Sets the topologic type of the current network and checks the
topology of the current network.
Returns an error if the topologic type of the current network
doesn't fit to this type.
Topologic types are:
- NET_TYPE_GENERAL
general purpose network type with no limitations
- NET_TYPE_FF1
feedforward network with fully connected units in
neighbour layers
NOTES :
RETURNS : Returns error code
UPDATE :
******************************************************************************/
krui_err kr_setSpecialNetworkType(int net_type)
{
KernelErrorCode = KRERR_NO_ERROR;
if (net_type == specialNetworkType) return( KRERR_NO_ERROR );
if (NoOfUnits == 0)
{ /* no units defined */
KernelErrorCode = KRERR_NO_UNITS;
return( KernelErrorCode );
}
switch (net_type) {
case NET_TYPE_GENERAL:
switch (specialNetworkType) {
case NET_TYPE_FF1:
/* change special network presentation to standard presentation */
#ifdef MASPAR_KERNEL
(void) krff_standardNetPresentationFF1();
specialNetworkType = NET_TYPE_GENERAL;
#else
KernelErrorCode = KRERR_NO_MASPAR_KERNEL;
#endif
break;
default:
KernelErrorCode = KRERR_PARAMETERS;
}
break;
case NET_TYPE_FF1:
/* change standart network presentation to special presentation */
#ifdef MASPAR_KERNEL
(void) krff_determineNetFF1Params();
/* change internal network presentation */
if (KernelErrorCode != KRERR_NO_ERROR) break;
(void) krff_initMasPar();
if (KernelErrorCode != KRERR_NO_ERROR) break;
(void) krff_changeNetPresentationFF1();
#else
KernelErrorCode = KRERR_NO_MASPAR_KERNEL;
#endif
break;
default:
KernelErrorCode = KRERR_PARAMETERS;
}
if (KernelErrorCode == KRERR_NO_ERROR)
specialNetworkType = net_type;
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_getSpecialNetworkType
PURPOSE : Returns the special topologic type of the current network, if set.
NOTES :
RETURNS : Returns the special topologic type of the current network, if set.
UPDATE :
******************************************************************************/
int kr_getSpecialNetworkType(void)
{
return( specialNetworkType );
}
/*****************************************************************************
FUNCTION : kr_validateOperation
PURPOSE : Validate a network modifying operation according to
the kernel mode
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
krui_err kr_validateOperation(void)
{
switch (specialNetworkType)
{
case NET_TYPE_GENERAL:
/* normal network presentation, no limitations */
KernelErrorCode = KRERR_NO_ERROR;
break;
case NET_TYPE_FF1:
/* feedforward net with limitations */
KernelErrorCode = KRERR_MODE_FF1_INVALID_OP;
break;
}
return( KernelErrorCode );
}
/* #############################################################
Functions for the MasPar kernel
############################################################# */
#ifdef MASPAR_KERNEL
/*****************************************************************************
FUNCTION : kr_initMasPar
PURPOSE : Connects and Disconnects the MasPar.
The mode switches are: MASPAR_CONNECT and MASPAR_DISCONNECT.
NOTES :
RETURNS : error code
UPDATE :
******************************************************************************/
krui_err kr_initMasPar(int mode )
{
if (specialNetworkType == NET_TYPE_GENERAL) {
KernelErrorCode = KRERR_NOT_PARALLEL_MODE;
return( KernelErrorCode );
}
KernelErrorCode = KRERR_NO_ERROR;
switch (mode)
{
case MASPAR_CONNECT:
/* connect maspar */
if (krff_initMasPar() == KRERR_NO_ERROR)
masParStatus = MASPAR_CONNECT;
break;
case MASPAR_DISCONNECT:
/* disconnect maspar */
masParStatus = MASPAR_DISCONNECT;
break;
default:
KernelErrorCode = KRERR_PARAMETERS;
}
return( KernelErrorCode );
}
/*****************************************************************************
FUNCTION : kr_getMasParStatus
PURPOSE :
NOTES :
RETURNS : Returns the Status of the MasPar or an error code
UPDATE :
******************************************************************************/
krui_err kr_getMasParStatus(void)
{
KernelErrorCode = KRERR_NO_ERROR;
return( masParStatus );
}
#endif
|