辅导data留学生编程、c/c++程序语言讲解、辅导Python,Java编程

- 首页 >> Database
Goal
Racial segregation has always been a pernicious social problem in the United States. Although much effort has been extended to desegregate our schools, churches, and neighborhoods, the US continues to remain segregated (Links to an external site.) by race and economic lines. The New York Times has an interesting interactive map (Links to an external site.) showing the distribution of racial and ethnic groups in cities and states around the country as recorded during the 2010 census. Why is segregation such a difficult problem to eradicate?
The forces that lead people to select where they live are complex and it is unlikely that any simple model can truly answer the questions of how such segregation arises and why it persists. However, it is still possible to ask simple questions that may shed some light on the situation.
In 1971, the American economist Thomas Schelling (Links to an external site.) created an agent-based model that might help explain why segregation is so difficult to combat. He was trying to answer a simple question: is it possible that even if a group of individuals would prefer to live in integrated neighborhoods, might they still end up in segregated neighborhoods as the result of their collective choices?
His simple model of segregation showed that even when individuals (or "agents") didn't mind being surrounded or living by agents of a different race, they would still choose to segregate themselves from other agents over time! Although the model is quite simple, it gives a fascinating look at how individuals might self-segregate, even when they have no explicit desire to do so. In this assignment, students will create a simulation of Schelling's model, and see how it operates.
How the Model Works
We will work with a slight simplification of Schelling's model. To explain the model, suppose there are two types of agents: X and O. For this assignment, these two types of agents will represent: those who prefer their iced tea sweetened, and those who prefer their iced tea unsweetened. We'll use separate visual representations for these individuals so it is easier to see how agents move. Green elephants are sweet tea drinkers, , while orange monkeys prefer unsweetened tea, , instead.
Two populations of the two agent types are initially placed into random locations of a neighborhood represented by a grid. After placing all the agents in the grid, each cell is either occupied by an agent or is empty as shown below.

Now we must determine if each agent is satisfied with its current location. A satisfied agent is one that is surrounded by at least t percent of agents that are like itself. That means each agent is perfectly happy to live in an integrated neighborhood where 1 - t percent of its neighbors have the opposite tea preference. We'll call t the agent's satisfaction threshold. Note that the higher the threshold, the higher the likelihood the agents will not be satisfied with their current location.
For example, if t = 30%, agent X is satisfied if at least 30% of its neighbors are also X, meaning up to 70% of its neighbors can be O's. If fewer than 30% are X, then the agent is not satisfied and it will want to change its location in the grid. For the remainder of this explanation, let's assume a threshold t of 30%. This means every agent is fine with being in the minority as long as there are at least 30% of similar agents in adjacent cells.
The picture below (left) shows a satisfied agent because 50% of X's neighbors are also X (50% > t). The next X (right) is not satisfied because only 25% of its neighbors are X (25% < t). Notice that empty cells are not counted when calculating satisfaction.
     
When an agent is not satisfied, it can move to any vacant location in the grid where it would be satisfied.
In the image below, all dissatisfied agents have an asterisk next to them. These are the agents that would choose to move when it is their turn, although they will only move to a different cell if that cell is empty, and if they would be satisfied after the move. Note that such a move may cause some agents that were previously satisfied to become dissatisfied!

Design Exercise 1
In our implementation, on every call to act() an Agent will either move or stay put. You'll have to write the logic to determine whether it will move or not, and to where. Recall that the World provides a getOneObjectAt() method that takes an (x, y) location and returns the object, if any, that is located there (if multiple objects are located there, it just returns one of them at random).
Before you read any further in this assignment, take a piece of paper and outline which method(s) you would create for your Agent class to implement this choice. What logic would you use to decide whether you are satisfied? What logic would you use to decide where to move?
Outline your logic now, and the methods you would use to implement it. Then proceed to read the rest of the assignment.
Do NOT read below until you have finished your Agent class method design. The preliminary design should contain for each method a descriptive name and parameter types.
Starting Materials
Download the scenario for this assignment, which contains all the classes you need to start.: program3.zip.
The starting scenario does not contain any starting classes--you'll have to create all the classes yourself from scratch. The starting scenario only contains the necessary images for the green elephants and the orange monkeys.
Classes You Create
You will create two classes: one to represent the world, and one to represent the "agents" (that is, residents) who live in and move around in the world. For this simulation, we will divide our population into two groups of agents: those who prefer their iced tea sweetened, and those who prefer their iced tea unsweetened. We'll use separate visual representations for these individuals so it is easier to see how agents move. Green elephants, , are sweet tea drinkers, while orange monkeys, , prefer unsweetened tea instead.
A City Class
Create a subclass of World called City that represents a single rectangular region where your agents will live. You can use any image background you wish for your city.
Your City class must provide a constructor that takes its width and height as parameters. Grid cells in your city should be 24 pixels square. Each City should be initially empty (that is, no agents).
Your City class must also define a populate() method that takes three double parameters between 0.0 - 1.0. The first parameter represents the percentage of elephants in the city, and the second represents the percentage of monkeys. The third parameter represents the satisfaction threshold for the agents. For example, consider this call:
city.populate(0.3, 0.4, 0.3);
This call indicates that approximately 30% of the cells in the city should be filled with elephants, and approximately 40% of the cells should be filled with monkeys, which will leave approximately 30% of the cells unoccupied or empty. All of the agents will use 0.3 (30%) as their satisfaction threshold, according to the third parameter.
You can implement populate using a pair of loops:
For each possible X coordinate ...
And for each possible Y coordinate ...
Generate one random number between 0.0 - 1.0 for the cell at (x, y).
If that random number is less than or equal to the "elephant" parameter, then place an elephant at (x, y), or
If that random number is less than or equal to the sum of the "elephant" and "monkey" parameters, then put a monkey at (x, y), or
Otherwise, leave that cell empty.
Note that you must limit yourself to just a single random number per cell. For example, for a 10x10 city, you should make exactly 100 calls to generate random numbers, no more, no less. Use the same random value for all tests you perform to determine the contents of a single cell location in the city.
Finally, your City class must also provide a default constructor that initializes the city to a size of your choice (something that looks good on your screen), and then calls populate() with population ratios and a threshold that you pick. You should implement your default constructor by using this to call the City constructor that takes the width and height as parameters instead of using super().
An Agent Class
Create a subclass of Actor called Agent that represents a single agent.
Your Agent should provide a constructor that takes two parameters: a String indicating the kind of animal, and a double value between 0.0 - 1.0 indicating its satisfaction threshold.
Note that while we are describing this scenario in terms of elephants and monkeys (i.e., the kind of animal might be "elephant" or "monkey"), you may not assume these are the only kinds of agents. Your code should work for any kind specified, without assuming only these two values might be provided.
Your constructor should set the image used for the Agent using the animal name. For example, the elephants use the "elephant.png" image, while the monkeys use the "monkey.png" image (and rabbits use the "rabbit.png" image, and kangaroos use the "kangaroo.png" image, etc.).
Your Agent must also provide the following methods:
getKind()
A getter method that returns the kind of agent, as a string (such as "elephant", for example).
getThreshold()
A getter method that returns the agent's satisfaction threshold, as a double value.
isSameKindAs(agent)
A boolean method that returns true if this agent is the same kind of agent as the one provided.
isSatisfiedAt(x, y)
A boolean method that returns true if this agent would be satisfied at the specified location, or not. Remember to watch out for out of bounds errors when an agent is on an edge or corner of the city.
isSatisfied()
A boolean method that returns true if this agent is satisfied at its current location. Note: you may not simply repeat the code from isSatisfiedAt(). Your implementation here should be a single line (no more). Be careful with how you compare this agent with its neighbors to count up the number of neighbors of the same kind.
relocate()
A method that moves the Agent to a new location in the grid where it will be satisfied, if there is one. You may use a pair of nested loops to examine each (x, y) location to find the first empty location where this agent will be satisfied, then move there. Remember that if there are no empty spaces where this agent will be satisfied, it should stay put. You can use the agent's setGridLocation() method (which is a combination of setGridX() and setGridY()) to move it to a new location.
act()
Executes one "turn" for this agent, which means determining if the agent is satisfied, and relocating if it is not.
Design Exercise 2
Go back to the outline you made of your program logic and your methods earlier (from Design Exercise 1). Compare your method breakdown with the breakdown given above.
Does the method breakdown given above help simplify the problem? Does it make it easier to outline the logic of the act() method?
Identify the key differences in your own outline and the method breakdown shown above for what is required. Try to determine why those differences are present, and look for the reasons behind the methods specified above.
Comments on Design
As in other assignments, you will be graded in part on the design and readability of your solution using the posted grading criteria, so consider these factors when devising and naming your methods. The Program Grading Rubric (same as on Programs 1 and 2) describes the grading criteria. Note that a portion of your grade will be based on your approach to testing your solution.
For testing, remember that you can create very small, completely empty cities (even as small as 1x2, 2x2, or 3x3), and place exactly the agents you want at exactly the positions you want in order to test behaviors. All of the methods for cities can be tested without calling act(). Constructing simple, tiny situations to test out your behaviors is much cleaner and easier than trying to "run" a big simulation with a large population of elephants and monkeys spread around.


站长地图