swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A gift from Hungary - two bits of code to manipulate Discrete2ds


From: Nelson Minar
Subject: A gift from Hungary - two bits of code to manipulate Discrete2ds
Date: Sun, 6 Jul 1997 00:42:26 +0200

Hello from Budapest! Things are going well here. The CEU SysLab has a
great group of folks working with Swarm. We're just about to start
teaching Swarm modelling to about 30 people from Central and Eastern
Europe. The food is good.

Among other things, we're going to have people work with the
SugarScape models presented in Epstein and Axtell's book "Growing
Artificial Societies". It's a fun book with some well described models
in it. They're pretty straightforward to do in Swarm.

To make this work I had to write a couple of extra methods for
Discrete2d that should be of general use. They're written for
inclusion into any object, but they should probably be added to the
Discrete2d class. I hereby give this code to the Swarm user community
with no restrictions. No warranty, either - I just hacked this
together, but it seems to work :-)

The simple one is just a long-needed method to copy one lattice to another.
  -copyDiscrete2d: (Discrete2d *) a toDiscrete2d: (Discrete2d *) b;

The more interesting one is
  -(int) setDiscrete2d: (Discrete2d *) a toFile: (char *) filename;

It reads in a Discrete2d's values from a file. The file format is PGM,
a standard, trivial greyscale graphics format. There's a big suite of
Unix tools calls "netpbm" that handles this format, you can also just
edit it by hand.  I've appended an image in the format so you can get
an idea (it's my best guess at duplicating the SugarScape initial world).

Greyscale is great for 2d lattices - one value per square in a grid.
The only limitation of PGM is it's an 8 bit format - the max value at
one square is 255.

A method to *save* a Discrete2d to a file would be quite simple to
implement as well.


Here's the code, followed by an image.

----------------------------------------------------------------------

// Utility methods - these should be in the Swarm libraries.
// Read in a file in PGM format and load it into a discrete 2d.
// PGM is a simple image format. It stores grey values for a 2d array.
-(int) setDiscrete2d: (Discrete2d *) a toFile: (char *) filename {
  InFile * f;
  char c1, c2;
  int x, y, fileXSize, fileYSize, maxValue;

  // open the file
  f = [InFile create: [self getZone] withName: filename];

  // The first two characters should be P2, the PGM ASCII header (not P5 - raw)
  [f getChar: &c1];
  [f getChar: &c2];
  if (c1 != 'P' && c2 != '2')
    [WarningMessage raiseEvent: "File is not in PGM ascii format. Faking 
it.\n"];

  // Next two entries are the size in pixels
  [f getInt: &fileXSize];
  [f getInt: &fileYSize];
  if (fileXSize != [a getSizeX] || fileYSize != [a getSizeY]) {
    [WarningMessage raiseEvent: "PGM File is not the right size. Faking it.\n"];
  }

  // Finally, the maximum value (typically 255, but sometimes less).
  [f getInt: &maxValue];
  maxValue++;                                     // [0, maxValue)

  // Yay! Now we can read in a bunch of integers for the values themselves.
  // This code could be modified to read P5 type PGMs by reading raw bytes.
  for (y = 0; y < ysize; y++) {
    for (x = 0; x < xsize; x++) {
      int v;
      if ([f getInt: &v] != 1) {
        [WarningMessage raiseEvent: "Ran out of data reading PGM file. 
Aborting.\n"];
        goto finishReading;
      }
      [a putValue: v atX: x Y: y];
    }
  }
  
 finishReading:
  [f drop];                               // close the file
  return maxValue;
}

// A similar method should be written to *write* a Discrete2d to a file.

// Copy one Discrete2d's contents to another.
// This could probably use the fast accessor macros.
-copyDiscrete2d: (Discrete2d *) a toDiscrete2d: (Discrete2d *) b {
  int x, y;
  if ([a getSizeX] != [b getSizeX] || [a getSizeY] != [b getSizeY])
    [InvalidArgument raiseEvent: "Two Discrete2ds aren't the same size."];
  for (x = 0; x < [a getSizeX]; x++)
    for (y = 0; y < [b getSizeY]; y++)
      [b putValue: [a getValueAtX: x Y: y] atX: x Y: y];
  return self;
}

----------------------------------------------------------------------

P2
50 50
4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 
3 3 2 2 2 2 2 2 2 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 
3 3 3 3 2 2 2 2 2 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 
3 3 3 3 3 3 2 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2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]