Agentbased computational economics - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Agentbased computational economics

Description:

Java features Collections which are data structures holding objects. A type of Collections is ArrayList. We will use ... You can also concatenate two lists. ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 22
Provided by: michael1519
Category:

less

Transcript and Presenter's Notes

Title: Agentbased computational economics


1
Agent-based computational economics
  • Michael Neugart
  • Session 4

2
Agenda
  • Managing lots of agents using ArrayList
  • Inheritance
  • Useful things
  • Sorting
  • Generating random numbers

3
Lists
  • Java features Collections which are data
    structures holding objects
  • A type of Collections is ArrayList
  • We will use ArrayList to give our agents a home
  • You can create an ArrayList by typing
  • ArrayList list new ArrayList()

4
Lists
  • public class Worker
  • //what follows are the instance variables
  • int id
  • double humanCapital
  • double wage
  • double random
  • //what follows is the constructor
  • public Worker (int i, double h, double w)
  • id 0
  • humanCapital h
  • wage w
  • //what follows are the methods
  • public void luck()
  • random getNextDoubleFromTo(1,2)
  • wage random humanCapital

5
Lists
  • public class exampleLists
  • //what follows calls the main method
  • public static void main(String args)
  • int n 5
  • double random
  • //creates the list
  • ArrayList workerList new ArrayList()
  • //fills the list with workers
  • for(int i 0 i lt n i)
  • random getNextDoubleFromTo(1000,2000)
  • Worker aWorker new Worker(i,random,1500)
  • workerList.add(aWorker)
  • //continues on next slide

6
Lists
  • //this prints the worker ids
  • for(int i0 i lt workerList.size() i)
  • Worker aWorker (Worker) workerList.get(i)
  • System.out.println(aWorker.id)
  • //looks if list is empty
  • if(workerList.isEmpty())
  • System.out.println(workerList is empty
  • else
  • System.out.println(workerList is not
    empty
  • //removes the third object from the workerList
  • Worker aWorker (Worker) workerList.get(2)
  • workerList.remove(aWorker)
  • //checks if a workerList contains a certain
    object
  • if(!workerList.contains(aWorker))
  • System.out.println(does not contain
    aWorker)

7
Lists
  • Exercises for you
  • Calculate the maximum human capital among the
    workers
  • Calculate average human capital among the workers
  • Sort the list of workers in ascending order of
    workers human capital. (Hint Create a temporary
    additional list. You can use list2 list1 to
    write the contents of list1 into list2.)

8
Lists
  • There are several predefined routines you may use
    when working with lists. Quite helpful is
  • Collections.shuffle() which randomizes the
    order of objects in a list.
  • There is an alternative for going through lists.
    You may define an iterator
  • it workerList.iterator()
  • while(it.hasNext())
  • Worker aWorker (Worker)it.next()
  • System.out.println(aWorker.id)

9
Lists
  • You can also concatenate two lists. Say
    workerList is one of these lists and otherList
    shall be merged with it. Then you may do the
    following
  • ArrayList joinedList new ArrayList(workerList)
  • joinedList.addAll(otherList)

10
Inheritance
  • One of the three principles of Java programming
  • You can create a general class (superclass). This
    class can then be inherited by more specific
    classes (subclass).
  • In the specific class you add things that are
    unique to it
  • The general declaration is
  • Class subclass-name extends superclass-name
  • //body of class

11
Inheritance
  • Here is an example taken from Schildts book, p.
    252
  • First a superclass is defined for two-dimensional
    objects.
  • Then a subclass is added defining triangles.
  • Finally a little program determines the
    properties of some examples of triangles.

12
Inheritance
  • // A simple class hierarchy.
  • // A class for two-dimensional objects.
  • class TwoDShape
  • double width
  • double height
  • void showDim()
  • System.out.println("Width and height are "
  • width " and " height)

13
Inheritance
  • // A subclass of TwoDShape for triangles.
  • class Triangle extends TwoDShape
  • String style
  • double area()
  • return width height / 2
  • void showStyle()
  • System.out.println("Triangle is " style)

14
Inheritance
  • class Shapes
  • public static void main(String args)
  • Triangle t1 new Triangle()
  • Triangle t2 new Triangle()
  • t1.width 4.0
  • t1.height 4.0
  • t1.style "isosceles"
  • t2.width 8.0
  • t2.height 12.0
  • t2.style "right"
  • System.out.println("Info for t1 ")
  • t1.showStyle()
  • t1.showDim()
  • System.out.println("Area is " t1.area())
  • System.out.println()

15
Inheritance
  • Note, that the Triangle subclass adds style,
    area() and showStyle, and in addition it includes
    all the features of the TwoDShape class.

16
A solution to sorting
  • // Sorting workerList
  • ArrayList temp new ArrayList()
  • while (!workerList.isEmpty())
  • Worker poorest (Worker)workerList.get(0)
  • for (int i0 iltworkerList.size() i)
  • Worker aWorker (Worker)workerList.get(i)
  • if (aWorker.getWage() lt
    poorest.getWage())
  • poorest aWorker
  • workerList.remove(poorest)
  • temp.add(poorest)
  • workerList temp
  • System.out.println("Sorted list
    workerList)

17
Sorting objects in ArrayList using
Collections.sort()
  • Collections.sort(list, new Comparator()  
  • public int compare(Object o1, Object o2)
  • Person p1 (Person) o1
  • Person p2 (Person) o2
  • return p1.getFirstName().compareToIgnoreCase(
    p2.getFirstName())  
  • )  
  • System.out.println(list)

Source http//www.albeesonline.com/blog/2008/10/1
6/sorting-an-arraylist-of-objects/
18
Generating random numbers
  • We already saw in a previous example that we can
    draw a random number from a uniform distribution
  • int random getNextIntFromTo(0,2)
  • double random getNextDoubleFromTo(0,2)
  • double random Math.random()
  • Repast allows to draw random numbers from all
    kinds of distributions

19
Generating random numbers
  • This is what you have to do
  • Import package
  • import uchicago.src.sim.util.Random
  • at the beginning of your class file
  • Make sure that your Eclipse project has included
    the colt.jar. Check by right-clicking on the
    project folder, go to Properties and then to Java
    Build Path
  • You can draw a number from a normal distribution
    by

20
Generating random numbers
  • Random.createNormal()
  • double var Random.normal.nextDouble(0.5,
    0.3)
  • Or you do
  • Random.createNormal(0.5,0.3)
  • double var Random.normal.nextDouble()
  • Note that first entry denotes the mean and the
    second entry the standard deviation.

21
Generating random numbers
  • Check the uchicago.src.sim.util.Random class to
    find out about the other distributions which you
    may use with Repast
Write a Comment
User Comments (0)
About PowerShow.com