User Tools

Site Tools


dev:questions_ulam

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev:questions_ulam [2015/05/30 05:48] – [Q: How to access slot number when using ''WindowServices scan()''?] ackleydev:questions_ulam [2015/06/01 18:11] (current) – [Q: Do smaller grid geometries make the simulator run faster?] xychen
Line 1: Line 1:
 ===== Week One ===== ===== Week One =====
-To be copied here+==== Q: The range of Unary ====  
 +What is the range for an ''Unary'' number? How many bits does it take?
  
 +**A:** To understand the range of an Unary number, we first have to know how it is represented. An Unary(n) number takes n bits. It represents an integer of the number of 1's in it. For example 
 +''Unary(5) myUnary5 '' takes 5 bits. There could be 0 to 5 1's in it. So the range is from 0 to 5.
 +
 +That explains why the following code will output 1,3 and 7 when the value I expected is only 0,1,2,3.
 +<code>
 +DebugUtils du;
 +Unary(4) ns;
 +if(ns==0){
 +  ns=1;
 +}
 +else if(ns==1){
 +  ns=2;
 +}
 +else if(ns==2){
 +  ns=3;
 +}
 +else if(ns==3){
 +  ns=0;
 +}
 +du.printContext();
 +</code> 
 +
 +==== Q: More about the bits in Unary ====
 +If the value of an myUnary5 is ''1'', does it mean the possible bits that myUnary5 holds could be '00001', '00010', '00100', '01000' and '10000'?   
 +
 +**A: 30-May-2015 02:56:33PM-0600: ** Yes, a ''Unary'' value that contains a single 'on' bit will have value ''1'', regardless of its position.  So, for example, in a ''Unary(3)'', there is only one representation of //zero// (''000''), and one representation of //three// (''111''), but there are three possible representations for //one// (''001'', ''010'', ''100'') and three representations for //two// (''110'', ''101'', ''011'').  Only the number of one bits (known as the 'population count' or 'popcount') matters in determining a ''Unary'' value.
 ===== Week Two ===== ===== Week Two =====
  
Line 19: Line 46:
 **A: 29-May-2015 04:37:12AM-0600:** Probably the most straightforward is to use an 'is' expression combined with a cast: **A: 29-May-2015 04:37:12AM-0600:** Probably the most straightforward is to use an 'is' expression combined with a cast:
 <code> <code>
-Atom a = ew[slot];+Atom a = ew[index];
 if (a is Foo) {        if (a is Foo) {       
   Foo f = (Foo) a;    // Note that f is a copy of a!   Foo f = (Foo) a;    // Note that f is a copy of a!
Line 54: Line 81:
   if(density>0){   if(density>0){
     du.print((Int) ws.next());     du.print((Int) ws.next());
-    for (Int slot = ws.next(); slot >= 0; slot = ws.next()) { +    for (Int idx = ws.next(); idx >= 0; idx = ws.next()) { 
-       Atom a = ew[slot];+       Atom a = ew[idx];
        if (a is Signal) {        if (a is Signal) {
           //compare your density with mine           //compare your density with mine
Line 63: Line 90:
 </code> </code>
  
-**A: 29-May-2015 06:24:01PM-0600:** The ''WindowServices next()'' method returns -1 when there are no more slots to scan in your chosen range -- but the main issue here is: You don't want to use //both// a ''ws.scan()'' call //and// a for loop with ''ws.next'', you want one or the other.  If you use scan, then you use ''getHits'' to see if anything matched, and ''getPick'' to get a randomly-chosen match.  That code has got other issues too (e.g., ''WindowServi__**c**__es'').+**A: 29-May-2015 06:24:01PM-0600:** The ''WindowServices next()'' method returns -1 when there are no more event window indices to scan in your chosen range.. but the main issue here is: You don't want to use //both// a ''ws.scan()'' call //and// a for loop with ''ws.next'', you want one or the other.  If you use scan, then you use ''getHits'' to see if anything matched, and ''getPick'' to get a randomly-chosen match.  That code has got other issues too (e.g., ''WindowServi__**c**__es'').
  
-==== Q: How to access slot number when using ''WindowServices scan()''? ==== +==== Q: How to access event window site number when using ''WindowServices scan()''? ==== 
-//Thank you!// I fixed the code. This time I use the for loop on ws.next(). I wanted to use ''ws.scan(someType)'' but then if I used ''ws.getPick(0)'' to find a match, how would I know its actual 'slot number'+//Thank you!// I fixed the code. This time I use the for loop on ws.next(). I wanted to use ''ws.scan(someType)'' but then if I used ''ws.getPick(0)'' to find a match, how would I know its actual index in the event window
 <code> <code>
 WindowServices ws; WindowServices ws;
Line 73: Line 100:
 Request r; Request r;
 Int rt=au.getType((Atom) r); Int rt=au.getType((Atom) r);
-for(Int slot=ws.next();slot>=0;slot=ws.next()){ +for(Int idx=ws.next();idx>=0;idx=ws.next()){ 
-  Atom a= ew[slot];+  Atom a= ew[idx];
   if(a is Request){   if(a is Request){
     Request you=(Request) a;     Request you=(Request) a;
     if(density>you.density){     if(density>you.density){
       if(rdm.oneIn(2)){       if(rdm.oneIn(2)){
-        ew.swap(0,slot);+        ew.swap(0,idx);
       }       }
     }     }
Line 86: Line 113:
 </code> </code>
  
-**A: 29-May-2015 11:08:39PM-0600:** As long as the corresponding ''ws.getHits'' returns greater than zero, then return value of ''ws.getPick'' //is// the slot number!  You can do stuff like:+**A: 29-May-2015 11:08:39PM-0600:** As long as the corresponding ''ws.getHits'' returns greater than zero, then return value of ''ws.getPick'' //is// the event window index!  You can do stuff like:
  
 <code> <code>
Line 95: Line 122:
 if (ws.scan(rt)) {            // Any matches for rt? if (ws.scan(rt)) {            // Any matches for rt?
   Atom a = ew[ws.getPick(0)]; // Yes! Access chosen slot   Atom a = ew[ws.getPick(0)]; // Yes! Access chosen slot
-  if(a is Request){           // This if will always succeed.. +  if(a is Request){           // This 'ifwill always succeed.. 
-    Request r = (Request) a;+    r = (Request) a;
     .. use r.density etc ..     .. use r.density etc ..
   }   }
Line 109: Line 136:
 Int rt=au.getType((Atom) r);  // Get type of element Request Int rt=au.getType((Atom) r);  // Get type of element Request
 if (ws.scan(rt)) {            // Any matches for that? if (ws.scan(rt)) {            // Any matches for that?
-  Request r = (Request) ew[ws.getPick(0)]; // Yes! Access slot of chosen one+  r = (Request) ew[ws.getPick(0)]; // Yes! Access slot of chosen one
   .. use r.density etc ..   .. use r.density etc ..
 } }
 </code> </code>
-because that ''ws.scan(rt)'' can only return true if ''ws.getPick(0)'' is going to return the slot number of a ''Request'' atom.+because that particular ''ws.scan(rt)'' can only return true if ''ws.getPick(0)'' is going to return the event window index of a ''Request'' atom.  (It's not this easy, though, if you are scanning for multiple things simultaneously.) 
 + 
 +==== Q: Does ws.getPick(typeIndex) only return one site? How to access each site within an event window? ==== 
 +**Thank you**! Now I understand that ''ws.getPick(typeIndex)'' returns an event window index. ''ws.getPick(typeIndex)'' can randomly pick one atom of this type. When I want to select an atom of this type but also has the maximum value of a certain data member, I should use the for loop to check each atom of this type and get this maximum atom. Like here I want to find the ''Request ATOM'' which has the max density value within this EventWindow.  
 + 
 +**A: 31-May-2015 12:51:43AM-0600:** To select an atom based on anything other than its type, in general you'll need to write a loop explicitly, rather than using ''scan()'' -- although ''WindowServices'' can help with the event window indexing. 
 + 
 +But even when you do need to loop, we have ''SelectorServices'' to provide some help for simpler tasks like finding and picking fairly among maxima and minima.  Here are two ''SelectorServices'' examples.  The first, ''SSDemo1'', uses ''SelectorServices'' in a relatively general way, while the second, ''SSDemo2'', is a bit optimized for shorter code. 
 + 
 +<code - SSDemo1.ulam> 
 + 
 +/** 
 +   SSDemo1s share the highest Score among their connected group. 
 + */ 
 + 
 +element SSDemo1 { 
 +  // Typedefs 
 +  typedef Unsigned(8) Score; 
 + 
 +  // Utilities 
 +  EventWindow ew; 
 +  Random random; 
 +  DebugUtils du; 
 + 
 +  // Data members 
 +  Score val; 
 + 
 +  Void behave() { 
 +    if (val == 0) // randomize initial vals 
 +      val = random.bits(val.sizeof); 
 + 
 +    WindowServices ws;    // Scanning support 
 +    SelectorServices ss;  // Selection support 
 + 
 +    ws.reset(1,4); // Scan all but us (for this example) 
 +    ss.reset(); 
 + 
 +    for (Int idx = ws.next(); idx >= 0; idx = ws.next()) { 
 +      Atom a = ew[idx]; 
 +      if (a as SSDemo1) 
 +        ss.maximize(idx, (Int) a.val); 
 +    } 
 +    // This code is fairly general, but cumbersome.  Compare SSDemo2 
 +    if (ss.selectionMade()) { 
 +      Int sidx = ss.getSelectedKey(); // The site that maximized 
 +      SSDemo1 f = (SSDemo1) ew[sidx]; // which we know is a SSDemo1 
 +      if (f.val > 0)                  // ..if they've been initted 
 +        val = f.val;                  // ..pick up their val 
 +      du.printContext();              // and report for debugging. 
 +    } 
 +  } 
 +
 + 
 +</code> 
 + 
 +<code - SSDemo2.ulam> 
 + 
 +/** 
 +   SSDemo2s share the highest Score among their connected group. 
 + */ 
 + 
 +element SSDemo2 { 
 +  // Typedefs 
 +  typedef Unsigned(8) Score; 
 + 
 +  // Utilities 
 +  EventWindow ew; 
 +  Random random; 
 +  DebugUtils du; 
 + 
 +  // Data members 
 +  Score val; 
 + 
 +  Void behave() { 
 +    if (val == 0) // randomize initial vals 
 +      val = random.bits(val.sizeof); 
 + 
 +    WindowServices ws;    // Scanning support 
 +    SelectorServices ss;  // Selection support 
 + 
 +    ws.reset(0,4); // Scan everyone including us 
 +    ss.reset(); 
 + 
 +    for (Int idx = ws.next(); idx >= 0; idx = ws.next()) { 
 +      Atom a = ew[idx]; 
 +      if (a as SSDemo2) 
 +        ss.maximize((Int) a.val);  // Don't need the idx this way.. 
 +    } 
 +    // We scanned ourselves, so we know ss.selectionMade() will be 
 +    // true.  So we just take the chosen value (even if it was ours). 
 +    val = (Score) ss.getSelectedValue(); 
 +    du.printContext(); 
 +  } 
 +
 + 
 +</code> 
 + 
 + 
 +==== Q: How to change the size of the MFM simulator? ==== 
 +Sometimes the canvas is too big for my experiment. If I don't want those atoms diffuse too far away, I will draw a box of ''Wall''. Can we change the size of MFM simulator? 
 + 
 +A: In version 3, the ''mfms'' simulator has a nice new feature: You can specify the grid size 
 +(and even the tile size!) on the command line. 
 + 
 +Read the very beginning of the output from 'mfms -h' about geometry.  If the geometry 
 +is specified is has to be the first argument on the command line. 
 + 
 +Try something like:  
 + 
 +  you@linux$ ..path../mfms {2C2} -flags -what -ever 
 + 
 +or even  
 + 
 +  you@linux$ ..path../mfms {1H1} -flags -what -ever 
 + 
 +==== Q: Do smaller grid geometries make the simulator run faster? ==== 
 + 
 +**Thank you!** That's better than drawing ''Walls'' by hand. The right side is the resized MFM simulator by {2C2}. And isn't the smaller simulator running faster? 
 + 
 + 
 + 
 + 
 +**A: 31-May-2015 01:06:01AM-0600: ** It depends somewhat on your hardware -- in particular, how many //real// cores you have, but yes, in general a smaller grid geometry -- fewer and smaller tiles, down to some limit -- will typically be faster than more and larger tiles.  Any single tile geometry will be running single-threaded, though, so in some cases multiple smaller tiles will be faster than one bigger one -- you'll need to experiment some, if maximizing AER is crucial.   Also, if you type **''a''** a few times after typing **''i''** in the simulator, it will display the AER it is producing, so you can get a rough sense of what's faster and slower. 
 + 
 +On a separate point, note that the "edge of the grid" is somewhat different that a ring of ''Wall''s.  For example, non-existent sites return ''false'' from the ''EventWindow isLive'' method, but sites containing ''Wall''s return true.  For consistency you might want to draw a ring of ''Wall'' around the boundary of even a smaller grid. 
 + 
 +{{:dev:mfms_signalrequest_2.9.png?nolink&400 |}}{{:dev:mfms_resize.png?nolink&400 |}}
dev/questions_ulam.1432964901.txt.gz · Last modified: 2015/05/30 05:48 by ackley