Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Social Scientists Lecture 3 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 3 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."— Presentation transcript:

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

2 POL SCI 209-1 Cederman / Stenfansson 2 Prisoner's Dilemma Game Player 2 C D C 3,30,5 Player 1 D 5,01,1

3 POL SCI 209-1 Cederman / Stenfansson 3 Chicken Game Player 2 C D C 3,31,5 Player 1 D 5,10,0

4 POL SCI 209-1 Cederman / Stenfansson 4 Exercise 4a: Chicken game #include 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; }

5 POL SCI 209-1 Cederman / Stenfansson 5 Exercise 4b: Fancy Output +---+---+ | | | +---+---+ | | * | +---+---+

6 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; }

7 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; }

8 POL SCI 209-1 Cederman / Stenfansson 8 Exercise 5: Russian Roulette A B A B A 1/6 5/6 2/6 4/6 3/6 4/6 B 5/6 3/6 2/6 1/6

9 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

10 POL SCI 209-1 Cederman / Stenfansson 10 Exercise 5: main.m int 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; }

11 POL SCI 209-1 Cederman / Stenfansson 11 Object Oriented Programming Variables Methods State Behavior An object A program Message

12 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

13 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++

14 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 Instance Variables Methods

15 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

16 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]

17 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...]

18 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 2 1 4 1 2 3 4

19 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 2 3 4

20 POL SCI 209-1 Cederman / Stenfansson 20 Typical ObjC File structure: main.m ClassA.m ClassB.m ClassA.h ClassB.h

21 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

22 POL SCI 209-1 Cederman / Stenfansson 22 SimplePD: File Structure main.m Player.m Player.h

23 POL SCI 209-1 Cederman / Stenfansson 23 SimplePD/main.m int 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; }

24 POL SCI 209-1 Cederman / Stenfansson 24 SimplePD/Player.h # import @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; }

25 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

26 POL SCI 209-1 Cederman / Stenfansson 26 ObjRoulette: File Structure main.m Player.m Revolver.m Player.h Revolver.h

27 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; }

28 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++; }...

29 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

30 POL SCI 209-1 Cederman / Stenfansson 30 ObjRoulette/Revolver.h... @interface Revolver: SwarmObject { int bullets; } -empty; -load; -(BOOL)trigger; @end

31 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]


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

Similar presentations


Ads by Google