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

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Programming for Social Scientists Lecture 8 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
2D Arrays in Java. Interfaces  Separating What from How  Phones: Dial  Cars Gas, Break, Steer.
Programming for Social Scientists Lecture 4 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
CS 280 Data Structures Professor John Peterson. Quiz 4 Recap Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}. Draw this in tree form and then show.
Programming for Social Scientists Lecture 3 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Programming for Social Scientists Lecture 9 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Programming for Social Scientists Lecture 5 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Games and Simulations O-O Programming in Java The Walker School
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
Guide to Programming with Python
1/1/20001 Topic >>>> Scan Conversion CSE Computer Graphics.
Image Formation CS418 Computer Graphics John C. Hart.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Objective Understand concepts used to create digital graphics. Course Weight : 15% Part Three : Concepts of Digital Graphics.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Today’s lecture 2-Dimensional indexing Color Format Thread Synchronization within for- loops Shared Memory Tiling Review example programs Using Printf.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
AGB 3/26/121 ++=. 2 Yes, believe it or not this is a complete webpage. It has a Head, Title and Body between the start and end HTML Tag.
June 14, ‘99 COLORS IN MATLAB.
Continuation of John Murphy’s RePast Tutorial Steps Charlie Gieseler
Prof. Dr. Lars-Erik Cederman ETH - Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2,
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CS122 – Quiz 2 Winter, 2013 Quiz Hints. Quiz 2 Hints Question 1 – working with the “if” statement  First, we did not fully explain the structure of the.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Integrating Repast Library and Running RePast Examples using JBuilder X 07/19/2004 by Deddy Koesrindartoto Department of Economics Iowa State University.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Modelling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
Swarm Idioms: Little Things Worth Knowing
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Reading Netpbm Images.
Engineering Innovation Center
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Basic Graphics Drawing Shapes 1.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Collections Framework
Data Structures & Algorithms
Introduction to RePast and Tutorial I
Presentation transcript:

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

POL SCI 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 Cederman / Stefansson 3 GridIPD: Adding 2D Space main EZGraph playerList Tournament ControlPanel Observer Swarm Object2dDisplay Raster ColorMap neighList Discrete2d ModelSwarm

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

POL SCI 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 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 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 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 Cederman / Stefansson 9 Shuffling agents on a list (cont) 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];

POL SCI 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];

POL SCI 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 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

POL SCI 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))); }

POL SCI 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 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 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

POL SCI 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

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

POL SCI 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

POL SCI 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 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 drawSelfOn: (id) raster { [raster drawPointX: x Y: y Color: type]; return self; } From Player.m:

POL SCI 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 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 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