Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Social Scientists Lecture 7 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.

Similar presentations


Presentation on theme: "Programming for Social Scientists Lecture 7 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."— Presentation transcript:

1 Programming for Social Scientists Lecture 7 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson

2 POL SCI 209-1 Cederman / Stefansson 2 Today's topics Network of agents 2D Space GUI: Zoom Rasters Code example: GridIPD –Program that implements Cohen et al.'s framework graphically

3 POL SCI 209-1 Cederman / Stefansson 3 GridIPD: Adding 2D Space main EZGraph playerList Tournament ControlPanel Observer Swarm Object2dDisplay Raster ColorMap neighList Discrete2d ModelSwarm

4 POL SCI 209-1 Cederman / Stefansson 4 Changes to GraphIPD... Spatially represented agents Single list with shuffling Network of neighbors Adaptation rather than evolution Raster display

5 POL SCI 209-1 Cederman / Stefansson 5 Spatially represented agents Use these collection objects for players –Discrete2d –List Assign each player a list of neighbors in 4 adjacent squares (i.e. Von Neuman neighborhood) List Discrete2d List

6 POL SCI 209-1 Cederman / Stefansson 6 Creating and collecting players world = [Discrete2d createBegin: self]; [world setSizeX: worldSize Y: worldSize]; world = [world createEnd]; (HERE SKIP CODE TO CREATE PLAYERS AND PUT ON LIST) [self shuffle: playerList]; index = [playerList begin: self]; for (x = 0; x < worldSize; x++) for (y = 0; y < worldSize; y++) { aPlayer = [index next]; [world putObject: aPlayer atX: x Y: y]; [aPlayer setX: x Y: y]; } [index drop]; Create grid object and set size Shuffle players Put one player on each space on grid

7 POL SCI 209-1 Cederman / Stefansson 7 Using an index to iterate index = [list begin: self]; while(obj=[index next]) (DO SOMETHING) [index drop]; Each List object can create index to iterate over collection Here [index next] moves to next member and returns member In while(…) when reach end of list index returns nil or false Drop index after use to reclaim memory

8 POL SCI 209-1 Cederman / Stefansson 8 Shuffling agents on a list -shuffle: list { int j, k; id temp; j = [list getCount]; while (j>1) { k = [uniformIntRand getIntegerWithMin: 0 withMax: j-1]; j--; temp = [list atOffset: k]; [list atOffset: k put: [list atOffset: j]]; [list atOffset: j put: temp]; } return self; }

9 POL SCI 209-1 Cederman / Stefansson 9 Shuffling agents on a list (cont) 601234 5 78 j = [list getCount]; j=9 k=5 j=8 temp= k=5 j=8 temp= k = [...getIntegerWithMin: 0 withMax: j-1]; j-- temp = [list atOffset: k]; [list atOffset: k put: [list atOffset: j]]; [list atOffset: j put: temp]; 601234 5 78 601234 8 78 601234 8 75 5 5 601234 5 78

10 POL SCI 209-1 Cederman / Stefansson 10 Shuffling agents - 2nd iteration k=6 j=7 temp= k=5 j=7 temp= k = [...getIntegerWithMin: 0 withMax: j-1]; j-- temp = [list atOffset: k]; [list atOffset: k put: [list atOffset: j]]; [list atOffset: j put: temp]; 601234 8 75 701234 8 75 701234 8 65 6 6 601234 8 75

11 POL SCI 209-1 Cederman / Stefansson 11 Neighborhoods of agents -setNeighborhoods { id index, aPlayer; index = [playerList begin: self]; while ((aPlayer=[index next])) { [self setNeighborhood: aPlayer atDX: -1 DY: 0]; [self setNeighborhood: aPlayer atDX: 1 DY: 0]; [self setNeighborhood: aPlayer atDX: 0 DY: -1]; [self setNeighborhood: aPlayer atDX: 0 DY: 1]; } [index drop]; return self; } Each player has a list to hold neighbors Code here picks out players in “Von Neuman” neighborhood Note use of index

12 POL SCI 209-1 Cederman / Stefansson 12 The Von Neuman neighborhood NW N NE E W SWSE is in location x,y W is in location x-1,y N is in location x,y-1 E is in location x+1,y Must correct for border agents List S

13 POL SCI 209-1 Cederman / Stefansson 13 Populating neighborhoods ModelSwarm: Pick out neighbors, check if boundary conditions hold and call player to add to his list: - setNeighborhood: (id) player atDX: (int) dx DY: (int) dy { int x, y; x = [player getX]; y = [player getY]; if ([self validX: x+dx Y: y+dy]) { [[player getNeighborhood] addLast: [world getObjectAtX:x+dx Y:y+dy]]; } return self; } -(BOOL) validX: (int) x Y: (int) y { return (((x >= 0) && (x = 0) && (y < worldSize))); }

14 POL SCI 209-1 Cederman / Stefansson 14 Adaptation: Player.m -adaptType {... index = [neighbors begin: [self getZone]]; while ((neigh = [index next])) { currentPayoff = [neigh getAveragePayoff]; if ((currentPayoff > bestPayoff)) { bestPayoff = currentPayoff; bestType = [neigh getPlayerType]; } else if (currentPayoff == bestPayoff) if ([uniformDblRand getDoubleWithMin:0.0 withMax: 1.0]< 0.5){ bestPayoff = currentPayoff; bestType = [neigh getPlayerType]; }} [index drop]; if (bestPayoff > [self getAveragePayoff]) newType = bestType; return self; }

15 POL SCI 209-1 Cederman / Stefansson 15 Adaptation (cont) From Player.m: -adaptType: while ((neigh = [index next])) { currentPayoff = [neigh getAveragePayoff]; if ((currentPayoff > bestPayoff)) { bestPayoff = currentPayoff; bestType = [neigh getPlayerType]; } else if (currentPayoff == bestPayoff) { (BREAK TIES) } From Player.m: -updateType [self setPlayerType: newType] Player looks around neighborhood and picks neighbor type with highest payoff Put type value into temporary variable Finally set type to value of temporary variable

16 POL SCI 209-1 Cederman / Stefansson 16 Adaptation (cont) To avoid creating new population use agents as “buffers”: –In -adaptType new type is simply stored for future reference After all agents have found new type - updateType “creates” new population type=2 type=1 1 type=2 newType=1 2 type=1 3 = highest payoff

17 POL SCI 209-1 Cederman / Stefansson 17 Objects to display raster image Discrete2d Raster Holds x,y locations of agents Object2dDisplay Colormap ModelSwarmObserverSwarm Main purpose of Object2dDisplay to catch messages from Raster widget on a mouse-click. It then opens up a probe to the object that at this location. Agent

18 POL SCI 209-1 Cederman / Stefansson 18 The Space library classes Space Discrete2d DblBuffer 2d Grid2d Conway Life Ca2d Diffuse 2d Object2d Display Value2d Display Int2d Filer

19 POL SCI 209-1 Cederman / Stefansson 19 Space Discrete 2d DblBuffer 2d Grid2d The basic 2d classes Discrete2d Provides basic 2d lattice functions Subclasses –Grid2d Doesn’t allow you to overwrite members –DblBuffer2d Writes go to a buffer Then updates all at once

20 POL SCI 209-1 Cederman / Stefansson 20 Some basic Discrete2d syntax setSizeX: i Y: j –Set the world size. getSizeX, getSizeY –Get the dimensions of the lattice fillWithValue: val, fillWithObject: obj –puts same value or object at each site setDiscrete2d: o toFile: filename –Reads a PGM formatted file and fills lattice with values copyDiscrete2d: a toDiscrete2d: b –Fills lattice b with values from a

21 POL SCI 209-1 Cederman / Stefansson 21 Creating a Colormap colorMap = [Colormap create: self]; [colorMap setColor: 0 ToName: "red"]; [colorMap setColor: 1 ToName: "blue"]; [colorMap setColor: 2 ToName: "orange"]; [colorMap setColor: 3 ToName: "black"]; Maps integer values used in simulation to color info used in display Can use –Color names –RGB values Type is 0,1,2,3 so respective color appears 0123 -drawSelfOn: (id) raster { [raster drawPointX: x Y: y Color: type]; return self; } From Player.m:

22 POL SCI 209-1 Cederman / Stefansson 22 Creating Raster worldRaster = [ZoomRaster create: self]; [worldRaster setColormap: colorMap]; [worldRaster setZoomFactor: zoomFactor]; [worldRaster setWidth: [modelSwarm getSize] Height: [modelSwarm getSize]]; [worldRaster setWindowTitle: "Spatial PD"]; [worldRaster pack]; Raster needs –Colormap –Zoom factor (pixels per square) –Dimensions –Title

23 POL SCI 209-1 Cederman / Stefansson 23 Creating Object2dDisplay Purpose: To catch mouse-clicks on raster and open probe to agent at this grid square Note that we need to define both Discrete2d, List, method to display agents and method to display probe worldDisplay = [Object2dDisplay createBegin: self]; [worldDisplay setDisplayWidget: worldRaster]; [worldDisplay setDiscrete2dToDisplay: [modelSwarm getWorld]]; [worldDisplay setObjectCollection: [modelSwarm getPlayers]]; [worldDisplay setDisplayMessage: M(drawSelfOn:)]; worldDisplay = [worldDisplay createEnd]; [worldRaster setButton: ButtonRight Client: worldDisplay Message: M(makeProbeAtX:Y:)];

24 POL SCI 209-1 Cederman / Stefansson 24 Exercise: Moore neighborhood NW N NE E W SWSE List S Now all 8 neighbors included List can accomdate more members In agent creation add code to define Moore neighborhood Hint: Consider for- next loop


Download ppt "Programming for Social Scientists Lecture 7 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."

Similar presentations


Ads by Google