📄 wealthmodel.java
字号:
StDAvg[stepModMAvgRange] = calcSD; calcSD = 0.0; for (int i = 0; i < MAvgRange; i++) calcSD = calcSD + StDAvg[i]; calcSD = calcSD / MAvgRange; return calcSD; } }); } if (DeathTax) { statsGraph.addSequence("Avg. Age", new Sequence() { public double getSValue() { double totalAge = 0; for (int i = 0; i < agentList.size(); i++) { WealthAgent a = (WealthAgent)agentList.get(i); totalAge += a.getAge(); } return totalAge / agentList.size(); } }); } } if (PlotLogLog) { logLogPlot = new Plot("Log(P_i) : Log(W_i) Plot"); } if (PlotPlain) { plainPlot = new Plot("Plain Plot"); if (PlainPlotXMax > 0.0) { plainPlot.setXRange(0.0, PlainPlotXMax); } if (PlainPlotYMax > 0.0) { plainPlot.setYRange(0.0, PlainPlotYMax); } plainPlot.addLegend(1, "> 250", java.awt.Color.magenta, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(2, "> 200", java.awt.Color.pink, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(3, "> 150", java.awt.Color.red, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(4, "> 125", java.awt.Color.orange, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(5, "> 100", java.awt.Color.yellow, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(6, "> 75", java.awt.Color.green, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(7, "> 50", java.awt.Color.cyan, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(8, "> 40", java.awt.Color.blue, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(9, "> 30", java.awt.Color.lightGray, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(10, "> 20", java.awt.Color.gray, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(11, "> 10", java.awt.Color.darkGray, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); plainPlot.addLegend(12, "<= 10", java.awt.Color.black, uchicago.src.sim.analysis.plot.OpenGraph.FILLED_CIRCLE); } // if (DeathTax) {// barAge.createHistogramItem("Age", agentList, new BinDataSource() {// public double getBinValue(Object o) {// WealthAgent agent = (WealthAgent)o;// return agent.getAge();// }// },4 ,0);//// barAge.setYRange(0, 1.0);// } //bar.createHistogramItem("Wealth", agentList, "getWealth"); // This causes the display surface to update the display whenever the // simulation run is paused or ended. The DisplaySurface now listens // for SimEvents produced by the WealthModel if ( WealthModel.Both ) { setROI(false); } addSimEventListener(dsurf); } // buildSchedule builds the schedule that changes the simulations state. // Under this scheme, a simulation is a state machine where all transitions // between states are the result of actions initiated by a schedule. private void buildSchedule() { // this is a static schedule (no need to add or replace actions) so // we can create an inner class that extends from basic action. The // execute method of the inner class will execute all the methods that // we wish to schedule on the agents and the environment. // this inner class could also be done anonymously with the // same result, but doing it this way is clearer for those // with less experience with Java. // we could also move everything in the execute method of the WealthRunner // into a method of this WealthModel class, a step() method for example, // and then schedule that as follows: // // schedule.scheduleActionBeginning(0, this, "step"); class WealthRunner extends BasicAction { public void execute() { step = step + 1; // call the birthAgents methods on this model birthAgents(); // call the shuffleAgents method on this model shuffleAgents(); // call the step method on each WealthAgent in the agentList for (int i = 0; i < agentList.size(); i++) { WealthAgent agent = (WealthAgent)agentList.get(i); agent.step(); } space.updateWealth(); // should call update display after the things that it displays // have changed their state. Otherwise, displays won't be in // synch with what it displays at end or at pause. dsurf.updateDisplay(); bar.step(); if (ViewPowerLawExp && (step > 300) && ((step % 10) == 0) ) powerlawGraph.step(); if (ViewEntropy && ((step % 10) == 0) ) entropyGraph.step(); if (ViewStats || ViewAvg || ViewStDev || ViewStDAvg) statsGraph.step(); // if (DeathTax)// barAge.step();// recorder.record(); reapAgents(); } }; // the schedule has been created in setup() schedule.scheduleActionBeginning(0, new WealthRunner()); // On a pause in the simulation run, call the writeToFile method // on the recorder object. (Writes the data collected by the recorder // to a file. // schedule.scheduleActionAtPause(recorder, "writeToFile"); if (WriteStats) { schedule.scheduleActionAtPause(recorder, "record"); schedule.scheduleActionAtPause(recorder, "writeToFile"); // When the simulation run ends, call the writeToFile method // on the recorder object. (Writes the data collected by the recorder // to a file. schedule.scheduleActionAtEnd(recorder, "record"); schedule.scheduleActionAtEnd(recorder, "writeToFile"); } class LogLogPlot extends BasicAction { public void execute() { int i, j; double logWealth; WealthAgent agent = (WealthAgent)agentList.get(0); double min = agent.wealth; double max = agent.wealth; for (i = 1; i < agentList.size(); i++) { agent = (WealthAgent)agentList.get(i); if (agent.wealth < min) min = agent.wealth; if (agent.wealth > max) max = agent.wealth; } double logMin = Math.log(min)/Math.log(10.0); double logMax = Math.log(max)/Math.log(10.0); double binStep = (logMax - logMin) / NumLogLogBins; double[] plotBins = new double[NumLogLogBins]; for (i = 0; i < NumLogLogBins; i++) plotBins[i] = 0.0; for (i = 0; i < agentList.size(); i++) { agent = (WealthAgent)agentList.get(i); logWealth = Math.log(agent.wealth)/Math.log(10.0); j = 0; while ( logWealth > (logMin + (j + 1) * binStep)) j++; if (j < NumLogLogBins) plotBins[j] += 1.0; else plotBins[NumLogLogBins -1] += 1; } logLogPlot.clear(1); logLogPlot.clear(2); double cum = 0.0; for (i = 0; i < NumLogLogBins; i++) if (plotBins[i] > 0.0) { logLogPlot.plotPoint(logMin + (i + 0.5) * binStep, Math.log(1 - cum)/Math.log(10.0), 1); logLogPlot.plotPoint(logMin + (i + 0.5) * binStep, Math.log(plotBins[i] / agentList.size())/Math.log(10.0), 2); cum += plotBins[i] / agentList.size(); } logLogPlot.fillPlot(); logLogPlot.updateGraph(); } }; if (PlotLogLog) { schedule.scheduleActionAtInterval(100, new LogLogPlot(), Schedule.LAST); }; class PlainPlot extends BasicAction { public void execute() { int i, j; double min = MinWealth; double max; WealthAgent agent = (WealthAgent)agentList.get(0); if (PlainPlotXMax > 0.0){ max = PlainPlotXMax; } else { max = agent.wealth; for (i = 1; i < agentList.size(); i++) { agent = (WealthAgent)agentList.get(i); if (agent.wealth > max) max = agent.wealth; } } double binStep = (max - min) / NumPlainBins; double[] plotBins = new double[NumPlainBins]; for (i = 0; i < NumPlainBins; i++) plotBins[i] = 0.0; for (i = 0; i < agentList.size(); i++) { agent = (WealthAgent)agentList.get(i); j = 0; while ( (agent.wealth > (min + (j + 1) * binStep)) && (j < NumPlainBins)) j++; if (j < NumPlainBins) plotBins[j] += 1.0;// else// plotBins[NumPlainBins -1] += 1; } for (i = 1; i <= 12; i++) plainPlot.clear(i); for (i = 0; i < NumPlainBins; i++) { plotBins[i] = plotBins[i] / agentList.size(); if (plotBins[i] > 0.0) { if (PlainPlotYMax > 0.0) { if ((plotBins[i]) <= PlainPlotYMax) { if (min + (i + 0.5) * binStep > 250) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 1); else if (min + (i + 0.5) * binStep > 200) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 2); else if (min + (i + 0.5) * binStep > 150) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 3); else if (min + (i + 0.5) * binStep > 125) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 4); else if (min + (i + 0.5) * binStep > 100) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 5); else if (min + (i + 0.5) * binStep > 75) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 6); else if (min + (i + 0.5) * binStep > 50) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 7); else if (min + (i + 0.5) * binStep > 40) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 8); else if (min + (i + 0.5) * binStep > 30) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 9); else if (min + (i + 0.5) * binStep > 20) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 10); else if (min + (i + 0.5) * binStep > 10) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 11); else plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 12); } } else { if (min + (i + 0.5) * binStep > 250) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 1); else if (min + (i + 0.5) * binStep > 200) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 2); else if (min + (i + 0.5) * binStep > 150) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 3); else if (min + (i + 0.5) * binStep > 125) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 4); else if (min + (i + 0.5) * binStep > 100) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 5); else if (min + (i + 0.5) * binStep > 75) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 6); else if (min + (i + 0.5) * binStep > 50) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 7); else if (min + (i + 0.5) * binStep > 40) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 8); else if (min + (i + 0.5) * binStep > 30) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 9); else if (min + (i + 0.5) * binStep > 20) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 10); else if (min + (i + 0.5) * binStep > 10) plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 11); else plainPlot.plotPoint(min + (i + 0.5) * binStep, (plotBins[i]), 12); } } } if ((PlainPlotXMax < 0.0) || (PlainPlotYMax < 0.0)) plainPlot.fillPlot(); plainPlot.updateGraph(); } }; if (PlotPlain) { schedule.scheduleActionAtInterval(100, new PlainPlot(), Schedule.LAST); }; } // Randomize the order of the object (the WealthAgents) in the agentList public void shuffleAgents() { SimUtilities.shuffle(agentList); } // Add a new agent. public void addNewAgent() { WealthAgent agent = new WealthAgent(space, this); int x, y; do { x = Uniform.staticNextIntFromTo(0, space.getXSize() - 1); y = Uniform.staticNextIntFromTo(0, space.getYSize() - 1); } while (agentGrid.getObjectAt(x, y) != null); agentGrid.putObjectAt(x, y, agent); agent.setXY(x, y); // Use the initial simulation parameters (maxMetabolism etc.) to construct //the agents if (Sync) agent.setWealth((double) (Uniform.staticNextIntFromTo(0, SyncMax))); else agent.setWealth((double) (Uniform.staticNextIntFromTo(MinInitialWealth, MaxInitialWealth))); if (DeathTax) { agent.setMaxAge(Uniform.staticNextIntFromTo(WealthModel.MaxAge / 2, WealthModel.MaxAge)); agent.setAge(Uniform.staticNextIntFromTo(0, agent.getMaxAge())); } else agent.setAge(0); if (Smarter) if (1 == Uniform.staticNextIntFromTo(1, SmarterNum)) agent.setSmart(Normal.staticNextDouble(0.01,0.005)); agentBirth(agent); } public void agentBirth(WealthAgent agent) {// if (this.getTickCount() == 0) {// agentList.add(agent);// } else { birthList.add(agent);// } } public void birthAgents() { agentList.addAll(birthList); birthList.clear(); } // When an agent "dies" it is added to the reaperQueue public void agentDeath(WealthAgent agent) { reaperQueue.add(agent); if (replace) { addNewAgent(); } } public static WealthAgent getAgentAt(int x, int y) { return (WealthAgent)agentGrid.getObjectAt(x, y); } public void reapAgents() { ListIterator li = reaperQueue.listIterator(); while (li.hasNext()) { WealthAgent agent = (WealthAgent)li.next(); agentList.remove(agent); agentGrid.putObjectAt(agent.getX(), agent.getY(), null); } reaperQueue.clear(); } public static void moveAgent(WealthAgent agent, int x, int y) { agentGrid.putObjectAt(agent.getX(), agent.getY(), null); agentGrid.putObjectAt(x, y, agent); agent.setXY(x, y); } // When a simulation is started through SimInit, some BaseController is // created to control the running of that model. This BaseController calls // getInitParam() on the model and receives a list of initial parameters // that can be displayed for modification to the user. In order to display // the value of these parameters the controller determines if the model // has implemented get and set methods for that parameter. If so, the // controller calls the get method and displays the result to the user. A // similar process occurs when a model's initial starting parameters // are written to a data file. // // What this means is that if a user wants to display some initial starting // parameter and have this parameter be modifiable, the name of the parameter // must be returned by the getInitParam() method and the model must contain // the appropriate get and set methods. For example, to do the above for // a parameter called NumAgents, "NumAgents" must be present in the array // return by getInitParam and the model must have a getNumAgents method and a // setNumAgents method. The parameter name must match the method names minus // the "get" and "set". So "numAgents" and getNumAgents won't work, but // "NumAgents" and getNumAgents will. // // The following methods are an example of this pattern. public int getNumAgents() { return NumAgents; } public void setNumAgents(int num) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -