home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / NewDynamic 1.0.1 / original source / DYNAMIC.pas next >
Encoding:
Pascal/Delphi Source File  |  1995-12-01  |  12.3 KB  |  510 lines  |  [TEXT/PJMM]

  1. {$X-}
  2. program Dynamic;
  3.     uses
  4.    {$U Obj/MemTypes    }
  5.         Types, 
  6.    {$U Obj/QuickDraw   }
  7.         QuickDraw, 
  8.    {$U Obj/OSIntf      }
  9.         OSUtils, 
  10.    {$U Obj/ToolIntf    }
  11.         ToolUtils, 
  12.    {$U Obj/PackIntf    }
  13.         Packages, 
  14.    {$U Obj/MacPrint    }
  15.         PrintTraps;
  16.    {$U Obj/PasLibIntf  }
  17.   {$U Obj/WritelnWindow   }
  18.  
  19.     const
  20.         lastMenu = 3; { number of menus }
  21.         appleMenu = 1; { menu ID for desk accessory menu }
  22.         fileMenu = 256; { menu ID for File menu }
  23.         editMenu = 257; { menu ID for Edit menu }
  24.  
  25.         maxvalue = 512;
  26.         maxvectsize = 8;
  27.         vscaling = 1;
  28.         hscaling = 1;
  29.     type
  30.         matrixtype = array[1..maxvectsize, 1..maxvectsize] of real;
  31.         vectortype = array[1..maxvectsize] of real;
  32.         titletype = string[20];
  33.  
  34.         optiontype = record
  35.                 start: boolean;
  36.                 quit: boolean;
  37.                 edit: boolean;
  38.                 newfile: boolean;
  39.                 debug: boolean;
  40.             end;
  41.  
  42.     var
  43.         Trect, dragRect: Rect;
  44.         myevent: eventRecord;
  45.         code: integer;
  46.         MyWindow, whichWindow: WindowPtr;
  47.         wRecord: WindowRecord;
  48.         FamilyID: integer;
  49.         myMenus: array[1..lastMenu] of MenuHandle; {Handles to all of the menus}
  50.         theDialog: DialogPtr;
  51.         options: optiontype;
  52.  
  53.         need, deltaneed, response, deltaresponse: vectortype;
  54.         alphavector, stimulation, consumvector, tempvct1, tempvct2: vectortype;
  55.         generalization, consumation, excitation, inhibition, tempmatrix, ident: matrixtype;
  56.         vectsize, loop, stimuli: integer;
  57.         alpha, personality: real;
  58.         answer: char;
  59.  
  60. { procedure InitScreen;}
  61. { begin}
  62. {  HideAll;}
  63. {  GetPort(dPort);}
  64. {  GetDrawingRect(dRect);}
  65. {  GetTextRect(TRect);}
  66. {  SetRect(TextRect, 250, 30, 530, 250);}
  67. {  SetRect(DrawRect, 5, 40, 700, 380);}
  68. {  SetTextRect(TextRect);}
  69. {  SetDrawingRect(DrawRect);}
  70. {  ShowDrawing;}
  71. {  ShowText;}
  72. { end;}
  73.  
  74.     procedure SetUpMenus;     {adapted from Inside Mac example/edit}
  75.         var
  76.             i: integer;
  77.     begin
  78.         InitMenus;
  79.         mymenus[1] := GetMenu(applemenu);
  80.         AddResMenu(mymenus[1], 'DRVR');  {add desk accessories}
  81.         myMenus[2] := GetMenu(filemenu);
  82.         myMenus[3] := GetMenu(editmenu);   {not really used,  but just for practice}
  83.         for i := 1 to lastMenu do
  84.             InsertMenu(mymenus[i], 0);
  85.         DrawMenuBar;
  86.     end;  {setupMenus}
  87.  
  88.  
  89.     procedure Startup;           {basic initialization of MAC tools}
  90.     begin
  91.         InitGraf(@thePort);        {start initialization sequence}
  92.         InitFonts;
  93.         InitWindows;
  94.         TEInit;
  95.         InitDialogs(nil);
  96.  
  97.         SetupMenus;  {throw menu bar up on the screen and insert menu items}
  98.         InitCursor;  {make cursor an arrow}
  99.         FlushEvents(everyEvent, 0);
  100.  
  101.         myWindow := GetNewWindow(256, @wRecord, POINTER(-1));{show this one}
  102.         SetPort(mywindow);   {graphics will go into my window}
  103.         SetPort(mywindow); {Writelns and Readlns text will go to my window}
  104.  
  105. {   WWINIT;}
  106.  {initialize the WritelnWindow unit}
  107.   {not used anymore}
  108. {with Trect do}
  109. {begin}
  110. {top := 40;}
  111. {right := 480;}
  112. {bottom := 330;}
  113. {left := 40;}
  114. {end;}
  115. {GetFNum('Geneva', familyID);}
  116. {WWNew(Trect, 'Animals', false, true, 80, FamilyID, 9);}
  117.  
  118.  
  119.     end; {startup}
  120.  
  121.  
  122.  
  123.     procedure matrixXvector (var a: vectortype; b: matrixtype; {A:= B C                  A is a m dimensional vector}
  124.                                     c: vectortype;                                            {B is a m x n dimensional matrix}
  125.                                     n, m: integer);                          {C is a n dimensional vector}
  126.         var
  127.             i, j: integer;
  128.             aij: real;
  129.     begin
  130.         for i := 1 to n do   {rows}
  131.             begin
  132.                 aij := 0.0;
  133.                 for j := 1 to m do  {columns}
  134.                     begin
  135.                         aij := aij + b[i, j] * c[j];
  136.                     end;
  137.                 a[i] := aij;
  138.             end;
  139.     end;{matrixXvector}
  140.  
  141.     procedure vectoradd (var a: vectortype; b, c: vectortype; {A:=B+ C           A,B,C are n dimensional vectors}
  142.                                     n: integer);
  143.         var
  144.             i: integer;
  145.     begin
  146.         for i := 1 to n do
  147.             a[i] := b[i] + c[i];
  148.     end;
  149.  
  150.     procedure vectorsubtract (var a: vectortype; b, c: vectortype; n: integer);
  151.         var
  152.             i: integer;
  153.     begin
  154.         for i := 1 to n do
  155.             a[i] := b[i] - c[i];
  156.     end;
  157.  
  158.     procedure vectorzero (var a: vectortype; n: integer);
  159.         var
  160.             i: integer;
  161.     begin
  162.         for i := 1 to n do
  163.             if a[i] < 0.0 then
  164.                 a[i] := 0.0;
  165.     end;
  166.  
  167.     procedure vectorinit (var a: vectortype; n: integer);
  168.         var
  169.             i: integer;
  170.     begin
  171.         for i := 1 to n do
  172.             a[i] := 0.0;
  173.     end;
  174.  
  175.     procedure vectordraw (a, b: vectortype; n, loop, offset, vscaling: integer);
  176.         var
  177.             i, v, dh, dv, idiv2: integer;
  178.             OddorEven: Boolean;
  179.     begin
  180.         for i := 1 to n do
  181.             begin
  182.                 if a[i] = 0.0 then      {look out for zeroed out responses}
  183.                     begin
  184.                         v := offset;
  185.                         dv := 0;
  186.                     end
  187.                 else                  { normal condition}
  188.                     begin
  189.                         v := offset - round(a[i] * vscaling);
  190.                         dv := -round(b[i] * vscaling);
  191.                     end;
  192.                 moveto(loop * hscaling, v);
  193.                 PenSize(i, i);  {change pen size to indicate which activity is occuring}
  194.                 line(hscaling, dv);
  195.  
  196.             end;{for i loop}
  197.     end;
  198.  
  199.     procedure vectorread (var a: vectortype; n: integer; title: titletype);
  200.         var
  201.             i: integer;
  202.     begin
  203.         writeln('enter the ', title, ' vector');
  204.         for i := 1 to n do
  205.             read(a[i]);
  206.         writeln;
  207.     end;
  208.  
  209.     procedure scalarXvector (var a: vectortype; s: real; b: vectortype; n: integer);
  210.         var
  211.             i: integer;
  212.     begin
  213.         for i := 1 to n do
  214.             a[i] := s * b[i];
  215.     end;
  216.  
  217.     procedure scalarXmatrix (var a: matrixtype; s: real; b: matrixtype; n: integer);
  218.         var
  219.             i, j: integer;
  220.     begin
  221.         for i := 1 to n do
  222.             for j := 1 to n do
  223.                 a[i, j] := s * b[i, j];
  224.     end;
  225.  
  226.     procedure matrixread (var a: matrixtype; n: integer; title: titletype);
  227.         var
  228.             i, j: integer;
  229.     begin
  230.         writeln('enter the ', title, ' matrix');
  231.         for i := 1 to n do
  232.             begin
  233.                 for j := 1 to n do
  234.                     read(a[i, j]);
  235.                 writeln;
  236.             end;
  237.     end;
  238.  
  239.     procedure matrixwrite (a: matrixtype; n: integer);
  240.         var
  241.             i, j: integer;
  242.     begin
  243.         for i := 1 to n do
  244.             begin
  245.                 for j := 1 to n do
  246.                     write(a[i, j] : 5 : 2);
  247.                 writeln;
  248.             end;
  249.     end;
  250.  
  251.  
  252.     procedure identity (var temp: matrixtype; vectsize: integer);
  253.         var
  254.             i, j: integer;
  255.     begin
  256.         for i := 1 to vectsize do
  257.             begin
  258.                 if i > 1 then
  259.                     for j := 1 to i - 1 do
  260.                         begin
  261.                             temp[i, j] := 0.0;
  262.                             temp[j, i] := 0.0;
  263.                         end;
  264.                 temp[i, i] := 1.0;
  265.             end;
  266.     end;
  267.  
  268.  
  269.     procedure DynamicSimulation; {the old program dynamic}
  270.         var
  271.             stimuli, loop: integer;
  272.  
  273.     begin {dynamic}
  274.  
  275.  {InitScreen;}
  276.         writeln(' a model of reciprocal inhibition ');
  277. {write('enter alpha, personality ');}
  278. {readln(alpha, personality);}
  279.         alpha := 0.2;
  280.         personality := 1;
  281.  
  282.         writeln('do you want to specify the problem size? [no] ');
  283.         readln(answer);
  284.         if answer = 'y' then
  285.             begin
  286.                 write('enter the problem size  ');
  287.                 readln(vectsize);
  288.             end
  289.         else
  290.             vectsize := 3;
  291.         identity(ident, vectsize);
  292.         writeln('do you want to specify the stimulation vector? [no] ');
  293.         readln(answer);
  294.         if answer = 'y' then
  295.             vectorread(stimulation, vectsize, 'stimulation')
  296.         else
  297.             for stimuli := 1 to vectsize do
  298.                 stimulation[stimuli] := stimuli * 0.5;
  299.         writeln('do you want to specify the consummation matrix [no] ');
  300.         readln(answer);
  301.         if answer = 'y' then
  302.             matrixread(consumation, vectsize, 'consummation')
  303.         else
  304.             scalarXmatrix(consumation, 0.2, ident, vectsize);
  305.         writeln('do you want to specify the excitation matrix [no] ');
  306.         readln(answer);
  307.         if answer = 'y' then
  308.             matrixread(excitation, vectsize, 'excitation')
  309.         else
  310.             scalarXmatrix(excitation, alpha, ident, vectsize);
  311.         writeln('do you want to specify the inhibition matrix? [no] ');
  312.         readln(answer);
  313.         if answer = 'y' then
  314.             matrixread(inhibition, vectsize, 'inhibition')
  315.         else
  316.             begin
  317.                 for loop := 1 to vectsize do
  318.                     begin
  319.                         for stimuli := 1 to vectsize do
  320.                             inhibition[loop, stimuli] := 1.0;
  321.                         inhibition[loop, loop] := 0.1;
  322.                     end;
  323.             end;
  324.         vectorinit(need, vectsize);                                                {zero out initial values of need and response}
  325.         vectorinit(response, vectsize);
  326.         scalarXmatrix(generalization, personality, ident, vectsize);
  327.  
  328.         for loop := 1 to 25 do
  329.             writeln; {clear the screen}
  330.  
  331.         SelectWindow(mywindow);
  332.  
  333.         MoveTo(0, 140);
  334.         DrawString('Needs');
  335.         MoveTo(0, 250);
  336.         DrawString('Responses');
  337.         MoveTo(100, 30);
  338.         DrawString('Time -->');
  339.  
  340. {begin the main loop}
  341.         for loop := 1 to maxvalue do
  342.             begin
  343.    {if loop = 250 then}
  344. {    ShowDrawing;}
  345.                 matrixXvector(tempvct1, generalization, stimulation, vectsize, vectsize);
  346.                 matrixXvector(consumvector, consumation, response, vectsize, vectsize);
  347.                 if button then
  348.                     vectorread(stimulation, vectsize, 'new stimulation');
  349.                 vectorsubtract(deltaneed, tempvct1, consumvector, vectsize);
  350.                 matrixXvector(tempvct1, inhibition, response, vectsize, vectsize);
  351.                 matrixXvector(tempvct2, excitation, need, vectsize, vectsize);
  352.                 vectorsubtract(deltaresponse, tempvct2, tempvct1, vectsize);
  353.                 vectordraw(need, deltaneed, vectsize, loop, 125, vscaling);
  354.                 vectoradd(need, need, deltaneed, vectsize);
  355.                 vectordraw(response, deltaresponse, vectsize, loop, 300, vscaling);
  356.                 vectoradd(response, response, deltaresponse, vectsize);
  357.                 vectorzero(response, vectsize);
  358.             end;
  359.     end;  {dynamic}
  360.  
  361.     {the remaining code was added to allow for windows, menus, etc}
  362.  
  363.     procedure Message (dialognumber: integer);
  364.                                  {Throw up a Dialog Box and wait for keypress}
  365.         var
  366.             theDialog: DialogPtr;
  367.             myEvent: EventRecord;
  368.             itemHit: integer;
  369.             tempPort: GrafPtr;
  370.     begin
  371.         GetPort(tempPort);       {save the current status of the screen display}
  372.         theDialog := GetNewDialog(dialognumber, nil, Pointer(-1)); {display dialog # }
  373.         ModalDialog(nil, itemhit); {show the dialog and wait for a button}
  374.         DisposDialog(theDialog);  {get rid of the dialog and show the screen}
  375.         SetPort(tempPort);        {put back the original display}
  376.     end; {Message}
  377.  
  378.  
  379.  
  380.  
  381.     procedure DoCommand (mResult: Longint; var options: optiontype); {process menu selection}
  382.         var
  383.             name: str255;
  384.             themenu, theitem, refnum: integer;
  385.             AlertNum, AlertId: integer;
  386.  
  387.     begin
  388.         AlertId := 1;
  389.         themenu := HiWord(mResult);
  390.         theItem := loWord(mResult);
  391.         case theMenu of
  392.             applemenu: 
  393.                 begin
  394.                     if theItem = 1 then
  395.                         Message(1)  {throw up message window}
  396.                     else   {do a desk accessory}
  397.                         begin
  398.                             GetItem(mymenus[1], theitem, name);
  399.                             refnum := OpenDeskAcc(name);
  400.                         end;
  401.                 end; {applemenu}
  402.             Filemenu: 
  403.                 begin
  404.                     case theitem of
  405.                         1: 
  406.                             options.start := true;
  407.                         2: 
  408.                             options.debug := true;
  409.                         3: 
  410.                             begin
  411.                                { AlertNum:=CautionAlert(AlertId,nil);}
  412. {                                If AlertNum=1 then}
  413. {                                       initialize;}
  414.                             end;
  415.                         4: 
  416.                             options.quit := true;
  417.                     end;
  418.                 end;
  419.             Editmenu: 
  420.                 options.edit := true;
  421.  
  422.  
  423.         end; {case themenu}
  424.     end;  {DoCommand}
  425.  
  426.     procedure WaitForCommand; {get options from menu bar until start}
  427.         var
  428.             theChar: Char;
  429.             dummy: boolean;
  430.     begin
  431.         with options do
  432.             begin
  433.                 start := false;
  434.                 debug := false;
  435.                 edit := false;
  436.                 newfile := false;
  437.                 quit := false;
  438.             end;
  439.         repeat
  440.             begin
  441.                 SystemTask;
  442.                 dummy := GetNextEvent(everyevent, myevent);
  443.                 case myevent.what of
  444.                     mousedown: 
  445.                         begin
  446.                             code := FindWindow(myevent.where, whichwindow);
  447.                             case code of
  448.                                 inmenubar: 
  449.                                     DoCommand(MenuSelect(myevent.where), options);
  450.                                 inSysWindow: 
  451.                                     SystemClick(myevent, whichwindow);
  452.                                 inDrag: 
  453.                                     DragWindow(whichwindow, myevent.where, dragRect);
  454.                             end; {case code}
  455.                         end; {mouse down}
  456.                     keydown, autokey: 
  457.                         begin  {try processing a menu-key equivalent}
  458.                             theChar := CHR(myevent.message mod 256); {the last byte}
  459.                             if BitAnd(myevent.modifiers, CmdKey) <> 0 then
  460.                                 DoCommand(MenuKey(theChar), options);
  461.                         end;
  462.  
  463.                 end; {case myevent.what}
  464.  
  465.                 if options.start then
  466.                     begin
  467.                         dynamicsimulation;
  468.                         options.start := false;
  469.                     end;
  470.                 if options.edit then
  471.                     begin
  472.                                   { editfile;}
  473.                         options.edit := false;
  474.                     end;
  475.             end; {repeat loop}
  476.         until options.quit or options.edit; {set in DoCommand}
  477.     end; {waitfor command}
  478.  
  479. {=======================================================================================    }
  480.     procedure InitMac;
  481.  
  482.     begin
  483.         MaxApplZone;
  484.         InitGraf(@thePort);
  485.         InitFonts;
  486.         InitWindows;
  487.         InitCursor;
  488.         InitMenus;
  489.         TEInit;
  490.         InitDialogs(nil);
  491.     end;
  492.     procedure PutUpWindow;
  493.         var
  494.             drawwindptr: WindowPtr;
  495.             Screen: Rect;
  496.             offSet: point;
  497.     begin
  498.         DrawWindPtr := NewCWindow(nil, ScreenBits.bounds, '', true, plainDBox, WindowPtr(-1), false, longInt(nil));
  499.         SetPort(DrawWindPtr);
  500.     end;
  501. begin          {dynamic}
  502.  
  503.     Startup;  {various MAC type initializations}
  504. {PutUpWindow;}
  505.     writeln;   {the screen initialization seems to start 1 line too high}
  506.  
  507.     WaitForCommand;{the Main Event Loop}
  508.  
  509.  
  510. end.