programming for social scientists lecture 3 ucla political science 209-1: programming for social...

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

Post on 20-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Programming for Social Scientists

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

Scientists

Winter 1999

Lars-Erik Cederman & Benedikt Stefansson

POL SCI 209-1 Cederman / Stenfansson

2

Prisoner's Dilemma Game

Player 2

C D

C 3,3 0,5

Player 1

D 5,0 1,1

POL SCI 209-1 Cederman / Stenfansson

3

Chicken Game

Player 2

C D

C 3,3 1,5

Player 1

D 5,1 0,0

POL SCI 209-1 Cederman / Stenfansson

4

Exercise 4a: Chicken game

#include <stdio.h>int main(int argc, const char ** argv) { int m[2][2] = {{3,1},{5,0}}; int equil; int r, c; for (r=0; r<2; r++) { for (c=0; c<2; c++) { equil = (m[r][c]>m[1-r][c]) && (m[c][r]>m[1-c][r]); printf("%d ",equil); } printf("\n"); } return 0;}

POL SCI 209-1 Cederman / Stenfansson

5

Exercise 4b: Fancy Output

+---+---+| | |+---+---+| | * |+---+---+

POL SCI 209-1 Cederman / Stenfansson

6

Exercise 4b: Fancy Output

for (r=0; r<2; r++) { printf("+---+---+\n"); printf("|"); for (c=0; c<2; c++) { equil=(m[r][c]>m[!r][c]) && (m[c][r]>m[!c][r]); if (equil) printf(" * |"); else printf(" |"); } printf("\n"); } printf("+---+---+"); return 0;}

POL SCI 209-1 Cederman / Stenfansson

7

Exercise 4c: Pareto optima

for (r=0; r<2; r++) { for (c=0; c<2; c++) { equil=(m[r][c]>m[!r][c]) && (m[c][r]>m[!c][r]); ... better=0; for (i=0; j<2; i++) for (j=0; j<2; j++) if (!((i==r) && (j==c))) if ((m[i][j]>=m[r][c]) && (m[j][i]>=m[c][r])) better = 1; // if better==0 then (r,c) is a Pareto optimum } return 0;}

POL SCI 209-1 Cederman / Stenfansson

8

Exercise 5: Russian RouletteA

B

A

B

A

1/6 5/6

2/6 4/6

3/6

4/6

B5/6

3/6

2/6

1/6

POL SCI 209-1 Cederman / Stenfansson

9

Exercise 5: Russian Roulette

0: A B

1: A B A B

2: A B

3: A B

4: A

5: A B

6: A B A B A

7: A B A B

8: A B

9: A B A B

10: A B A B A B

11: A B A

12: A B

13: A B A

14: A B

15: A B A B

16: A B A B

...

A's survival prob.=

0.478395...

1,000,000 replications:

0.4777947

POL SCI 209-1 Cederman / Stenfansson

10

Exercise 5: main.mint main(int argc, const char ** argv) { long int repl; int i,p,shot; long int sum = 0; long int n = 1000000;

for (repl=0; repl<n; repl++) { p = 0;

i = 0; do { i++; shot = ((double)rand() / (double)RAND_MAX < (double)i/6.0); p = !p; } while (!shot); if (p==0) sum++;} printf("%8.6f \n", (double) sum/n); return 0;}

POL SCI 209-1 Cederman / Stenfansson

11

Object Oriented Programming

Variables

Methods

State

Behavior

An object A program

Message

POL SCI 209-1 Cederman / Stenfansson

12

OOP and agent-based modeling

• general advantages: triple abstraction– encapsulation hides functions and data– inheritance of classes saves work– polymorphism simplifies programming

• also useful metaphor: "objects as agents"– deciding, sending and receiving messages– autonomous, own data and methods

POL SCI 209-1 Cederman / Stenfansson

13

Why Objective-C?

• History: Created by Brad Cox, most intensively used by NeXT, now owned by Apple Computer and forms basis for Rhapsody OS

• Main difference between C++ and Objective-C: – Easy to learn: A simple superclass of C - no new

keywords

– Partially interpreted

– More flexible and dynamic than C++

POL SCI 209-1 Cederman / Stenfansson

14

A few Objective-C basics

@interface Bug : SwarmObject {

int xPos, yPos; int worldXSize, worldYSize; id foodSpace;

}

-setX: (int) x Y: (int) y;-step;

@end

Super class

Sub classes

InstanceVariables

Methods

POL SCI 209-1 Cederman / Stenfansson

15

Some basic syntax

• @interface– Declarations of instance

variables and methods

• -message– declares a method called

`message'.

• -message: (type) var– declares method called

‘message’ that takes one argument

@interface Bug:SwarmObject {

int xPos, yPos; int worldXSize,worldYSize; id foodSpace;

}

-setX: (int) x Y: (int) y;-step;

@end

POL SCI 209-1 Cederman / Stenfansson

16

More Objective-C syntax

• Defining methods-aMessage: (type) var-aMessage:(type)v1 with:(type)v2 and:(type) v3

• Calling methods[obj aMessage: val]

[obj aMessage: val1 with: val2 and: val3]

[obj aMessage: val1 with: val2 and: val3]

POL SCI 209-1 Cederman / Stenfansson

17

The id variable type etc.

• Default variable type for object in ObjC is id• Think of this as a special variable type (which is

actually a pointer to a special data structure - namely the object)

• All objects can refer to themselves by[self ...]

• All objects can refer to superclass by[super ...]

POL SCI 209-1 Cederman / Stenfansson

18

Declaring a class

• The header file or interface declares – Class name

– It’s superclass

– Instance variables

– Methods

@interface Obj:SuperClass

{vartype Ivar1

vartype Ivar2

...

vartype IvarN

}

-(vartype)aMethod

-anotherMethod: (vartype) arg;

3

21

4

1

2

34

POL SCI 209-1 Cederman / Stenfansson

19

Defining a class

4

#import “Obj.h”

@implementation Obj

-(vartype)aMethod {

(body)

return returnval;

}

-anotherMethod: (vartype) arg {

(body)

return self;}

• Based on the interface the object file contains the implementation of the class

• Methods are essentially C functions, same rules about return values and arguments, local vars etc. apply

• Method returning no value must return self

1

1 2 3

4

23

4

POL SCI 209-1 Cederman / Stenfansson

20

Typical ObjC File structure:

main.m ClassA.m ClassB.m

ClassA.h ClassB.h

POL SCI 209-1 Cederman / Stenfansson

21

C Functions vs. ObjC methods

Objective-C method:-(type) name: (type) arg1 argName2: (type) arg2 {

(body)

return returnval;

}

C function:-(type)name((type) arg1,(type) arg2)) {

(body)

return returnval;

}

Code in body could look exactly the same in C and Objective-C

POL SCI 209-1 Cederman / Stenfansson

22

SimplePD: File Structure

main.m Player.m

Player.h

POL SCI 209-1 Cederman / Stenfansson

23

SimplePD/main.mint main (argc, const char ** argv) {

id player1, player2;initSwarm(argc, argv);player1 = [Player create: globalZone];player2 = [Player create: globalZone];[player1 init: 1 rowPlayer: YES];[player2 init: 2 rowPlayer: NO];[player1 setRow: 1 Col: 1];[player2 setRow: 1 Col: 1];if ([player1 move] && [player2 move])

printf(“Equilibrium!\n”);else

printf(“No equilibrium!\n”);return 0;

}

POL SCI 209-1 Cederman / Stenfansson

24

SimplePD/Player.h

# import <objectbase.h>

@interface Player: SwarmObject {int name;BOOL rowPlayer;int matrix[2][2];int row, col;

}-init: (int) n rowPlayer: (BOOL) rp;-setRow: (int) r Col: (int) c;-(BOOL)move;}

POL SCI 209-1 Cederman / Stenfansson

25

SimplePD/Player.m

#import "Player.h"

@implementation Player-init:(int)n rowPlayer: (BOOL)rp { name = n; rowPlayer = rp; matrix[0][0]=3; matrix[1][1]=1; if (rowPlayer) {

matrix[0][1]=0; matrix[1][0]=5;}

else { matrix[0][1]=5; matrix[1][0]=0;}

return self;}

-setRow: (int)r Col: (int)c { row = r; col = c;}-(BOOL)move { BOOL moving; if (rowPlayer) moving=matrix[!row][col] > matrix[row][col]; else

moving=matrix[row][!col] > matrix[row][col]; return moving;}

@end

POL SCI 209-1 Cederman / Stenfansson

26

ObjRoulette: File Structure

main.m Player.m Revolver.m

Player.h Revolver.h

POL SCI 209-1 Cederman / Stenfansson

27

ObjRoulette/main.m...int main(int argc, const char ** argv) {

id playerA, playerB, revolver; long int repl;

long int sum = 0;long int n = 1000;

initSwarm(argc,argv);

playerA = [Player create: globalZone];playerB = [Player create: globalZone];

[playerA setOther: playerB];[playerB setOther: playerA];revolver = [Revolver create: globalZone];

// main simulation loop-body goes here

printf("%8.6f \n", (double)sum/n);return 0;

}

POL SCI 209-1 Cederman / Stenfansson

28

ObjRoulette/main.m (cond'd)...

for (repl=0; repl<n; repl++) {[playerA init: 0];[playerB init: 1];[revolver empty];[playerA play: revolver];if ([playerA isAlive])

sum++;}

...

POL SCI 209-1 Cederman / Stenfansson

29

ObjRoulette/Player.h...@interface Player: SwarmObject {

int name;int alive;id other;

}

-init: (int) n;-setOther: o;-(BOOL)isAlive;-play: r;

@end

POL SCI 209-1 Cederman / Stenfansson

30

ObjRoulette/Revolver.h...@interface Revolver: SwarmObject {

int bullets;}

-empty;-load;-(BOOL)trigger;

@end

POL SCI 209-1 Cederman / Stenfansson

31

Recursive Russian Roulette

playerA

playerB

playerA

playerB

playerA

[playerB play: r]

playerB

[playerA play: r]

[playerB play: r]

[playerA play: r]

[playerB play: r]

main [playerA play: r]