Programming for Social Scientists Lecture 3 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson
POL SCI Cederman / Stenfansson 2 Prisoner's Dilemma Game Player 2 C D C 3,30,5 Player 1 D 5,01,1
POL SCI Cederman / Stenfansson 3 Chicken Game Player 2 C D C 3,31,5 Player 1 D 5,10,0
POL SCI 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; }
POL SCI Cederman / Stenfansson 5 Exercise 4b: Fancy Output | | | | | * |
POL SCI 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 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 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
POL SCI 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.= ,000,000 replications:
POL SCI 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 = ; 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 Cederman / Stenfansson 11 Object Oriented Programming Variables Methods State Behavior An object A program Message
POL SCI 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 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 Cederman / Stenfansson 14 A few Objective-C Bug : SwarmObject { int xPos, yPos; int worldXSize, worldYSize; id foodSpace; } -setX: (int) x Y: (int) y; Super class Sub classes Instance Variables Methods
POL SCI Cederman / Stenfansson 15 Some basic –Declarations of instance variables and methods -message –declares a method called `message'. -message: (type) var –declares method called ‘message’ that takes one Bug:SwarmObject { int xPos, yPos; int worldXSize,worldYSize; id foodSpace; } -setX: (int) x Y: (int) y;
POL SCI 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]
POL SCI 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 Cederman / Stenfansson 18 Declaring a class The header file or interface declares –Class name –It’s superclass –Instance variables interface Obj:SuperClass { vartype Ivar1 vartype Ivar2... vartype IvarN } -(vartype)aMethod -anotherMethod: (vartype) arg;
POL SCI Cederman / Stenfansson 19 Defining a class 4 #import 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
POL SCI Cederman / Stenfansson 20 Typical ObjC File structure: main.m ClassA.m ClassB.m ClassA.h ClassB.h
POL SCI 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 Cederman / Stenfansson 22 SimplePD: File Structure main.m Player.m Player.h
POL SCI 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; }
POL SCI Cederman / Stenfansson 24 SimplePD/Player.h # 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 Cederman / Stenfansson 25 SimplePD/Player.m #import 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;
POL SCI Cederman / Stenfansson 26 ObjRoulette: File Structure main.m Player.m Revolver.m Player.h Revolver.h
POL SCI 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 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 Cederman / Stenfansson 29 Player: SwarmObject { int name; int alive; id other; } -init: (int) n; -setOther: o; -(BOOL)isAlive; -play:
POL SCI Cederman / Stenfansson 30 Revolver: SwarmObject { int bullets; } -empty; -load;
POL SCI 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]