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

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

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Programming for Social Scientists

Lecture 7UCLA Political Science 209-1: Programming for Social

Scientists

Winter 1999

Lars-Erik Cederman & Benedikt Stefansson

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

POL SCI 209-1 Cederman / Stefansson

3

GridIPD: Adding 2D Spacemain

EZGraph

playerList

Tournament

ControlPanel

ObserverSwarm

Object2dDisplay

Raster

ColorMap

neighListDiscrete2d

ModelSwarm

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

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

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

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

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;

}

POL SCI 209-1 Cederman / Stefansson

9

Shuffling agents on a list (cont)

60 1 2 3 4 5 7 8 j = [list getCount];j=9

k=5j=8

temp=

k=5j=8

j=8temp=

k = [...getIntegerWithMin: 0 withMax: j-1];j--

temp = [list atOffset: k];

[list atOffset: k put: [list atOffset: j]];

[list atOffset: j put: temp];

60 1 2 3 4 5 7 8

60 1 2 3 4 8 7 8

60 1 2 3 4 8 7 5

5

5

60 1 2 3 4 5 7 8

POL SCI 209-1 Cederman / Stefansson

10

Shuffling agents - 2nd iteration

k=6j=7

temp=

k=5j=7

j=7temp=

k = [...getIntegerWithMin: 0 withMax: j-1];j--

temp = [list atOffset: k];

[list atOffset: k put: [list atOffset: j]];

[list atOffset: j put: temp];

60 1 2 3 4 8 7 5

70 1 2 3 4 8 7 5

70 1 2 3 4 8 6 5

6

6

60 1 2 3 4 8 7 5

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

POL SCI 209-1 Cederman / Stefansson

12

The Von Neuman neighborhood

NW N NE

EW

SW SE

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

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 < worldSize)) && ((y >= 0) && (y < worldSize)));

}

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;

}

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

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=11

type=2newType=12

type=13

= highest payoff

POL SCI 209-1 Cederman / Stefansson

17

Objects to display raster image

Discrete2d

RasterHolds x,ylocations 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

POL SCI 209-1 Cederman / Stefansson

18

The Space library classesSpace

Discrete2d

DblBuffer2d

Grid2d

ConwayLife Ca2d

Diffuse2d

Object2dDisplay

Value2dDisplay

Int2dFiler

POL SCI 209-1 Cederman / Stefansson

19

Space

Discrete2d

DblBuffer2d

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

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

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

0 1 2 3

-drawSelfOn: (id) raster {[raster drawPointX: x Y: y Color: type];return self;}

From Player.m:

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

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:)];

POL SCI 209-1 Cederman / Stefansson

24

Exercise: Moore neighborhood

NW N NE

EW

SW SE

List

S

Now all 8 neighborsincluded

List can accomdatemore members

• In agent creation add code to define Moore neighborhood

• Hint: Consider for-next loop