-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
406 lines (237 loc) · 12.2 KB
/
Model.java
File metadata and controls
406 lines (237 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package eWAGI_Firms;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.commons.math3.util.Precision;
import repast.simphony.context.Context;
import repast.simphony.context.space.continuous.ContinuousSpaceFactory;
import repast.simphony.context.space.continuous.ContinuousSpaceFactoryFinder;
import repast.simphony.context.space.grid.GridFactory;
import repast.simphony.context.space.grid.GridFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.parameter.Parameters;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.continuous.RandomCartesianAdder;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridBuilderParameters;
import repast.simphony.space.grid.SimpleGridAdder;
import repast.simphony.space.grid.WrapAroundBorders;
import repast.simphony.util.ContextUtils;
public class Model implements ContextBuilder<Object>{
@Override
public Context build(Context<Object> context) {
context.setId("EWAGI_Firms");
int numPositions = 104; // number of positions on Salop circle - set it equal to number of workers
//further must define number of positions such that it gives an even number when divided by 2 and divided by 4, e.g. 104
ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
//defines space for numPositionsX1 and torus (with WrapAroundBorders)
ContinuousSpace<Object> space = spaceFactory.createContinuousSpace(
"space", context, new RandomCartesianAdder<Object>(),
new repast.simphony.space.continuous.WrapAroundBorders(), numPositions,
1);
//discretize the space
GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
Grid<Object> grid = gridFactory.createGrid("grid", context,
new GridBuilderParameters<Object>(new WrapAroundBorders(),
new SimpleGridAdder<Object>(), true, numPositions, 1));
// Agent demography
int numSectors = 12;
int numGoodsSectors = 8;
int serviceSectors = 3;
int numEnergySectors = 1;
int numHouseholds =10000;
int numFirmsPerSector = 10;
int numBanks = 2;
double employmentShare = 1.0;
double totalSizeLaborForce = employmentShare*numHouseholds;
int ID = 0;
ArrayList<Sector> tempSectorList = new ArrayList<Sector> ();
ArrayList<Bank> tempBankList = new ArrayList<Bank> ();
ArrayList<ConsumptionCoefficient> consumptionCoefficientList = new ArrayList<ConsumptionCoefficient>();
double totalLaborIncome = 0;
double totalLaborForceData = 0;
//Start agent initialization
// Central bank
CentralBank aCentralBank = new CentralBank(ID);
int centralBankID=ID;
context.add(aCentralBank);
ID++;
//Foreign Bank; for export and import with RoW (rest of world)
ForeignBank aForeignBank = new ForeignBank(ID);
context.add(aForeignBank);
ID++;
//Local Banks
for(int i=0; i < numBanks; i++) {
Bank aBank = new Bank(ID, centralBankID,aForeignBank.ID);
context.add(aBank);
tempBankList.add(aBank);
//Add Bank accout and standing facility to CB
aCentralBank.bankReserveList.add(aBank.centralBankReserves);
aCentralBank.bankStandingFacilitiesList.add(aBank.standingFacility);
ID++;
}
// For the next agents, banks are assigned randomly
int bankIndex = (int)(Math.random() * ((numBanks-1) + 1));
int bankID = tempBankList.get(bankIndex).ID;
//Clearing House; for distributing diviends, and finacial market operations (not implemented yet)
ClearingHouse aClearingHouse = new ClearingHouse(ID, bankID);
context.add(aClearingHouse);
//Add Payment account to bank
tempBankList.get(bankIndex).paymentAccountList.add(aClearingHouse.paymentAccount);
tempBankList.get(bankIndex).centralBankReserves.accountBalance += aClearingHouse.paymentAccount.accountBalance;
ID++;
// Create sectors; attention: this is not generic at the moment. The initialization is linked to the 12 sector input output table, in which sector 1-7 and sector 9 are industrial sectors
// sector 8 is the energy sector, and sector 10 to 12 are service sectors
for (int i = 1; i < numSectors+1; i++) {
//Insutrial sectors, hereafter goods sector
if(i < 8 || i== 9) {
GoodsSector aSector = new GoodsSector(space, grid, i,totalSizeLaborForce , numFirmsPerSector);
totalLaborIncome += aSector.totalLaborCosts;
totalLaborForceData+= aSector.employmentData;
// Add (one) mall to the sectors
aSector.mallList.add(new GoodsMall(space, grid, i, i));
context.add(aSector);
tempSectorList.add(aSector);
//Add create firms and add them to the sector
for (int j = 0; j < numFirmsPerSector; j++) {
int index = (int)(Math.random() * ((numBanks-1) + 1));
int tempBankID = tempBankList.get(index).ID;
GoodsFirm aFirm = new GoodsFirm(space, grid,ID, aSector,tempBankID, aClearingHouse.ID);
tempBankList.get(index).paymentAccountList.add(aFirm.paymentAccount);
tempBankList.get(index).centralBankReserves.accountBalance += aFirm.paymentAccount.accountBalance;
//Add instance f inventory; which is put in the memory of the firm and the mall.
GoodsInventory aInventory = new GoodsInventory(aFirm.ID, aSector.sectorID, tempBankList.get(index).ID, aFirm.price);
/*TODO Chnage code here if more malls per sector are allowed*/
aInventory.inventoryStock = aFirm.initialOutput;
aSector.mallList.get(0).firmInventories.add(aInventory);
aFirm.mallInventoryList.add(aInventory);
aSector.initialOutput += aFirm.initialOutput;
context.add(aFirm);
ID++;
}
// Energy sector
}else if(i == 8) {
EnergySector aSector = new EnergySector(space, grid, i,totalSizeLaborForce , numFirmsPerSector);
totalLaborIncome += aSector.totalLaborCosts;
totalLaborForceData+= aSector.employmentData;
aSector.mallList.add(new EnergyMall(space, grid, i, i));
context.add(aSector);
tempSectorList.add(aSector);
//Create energy firms and add to sector
for (int j = 0; j < numFirmsPerSector; j++) {
int index = (int)(Math.random() * ((numBanks-1) + 1));
int tempBankID = tempBankList.get(index).ID;
EnergyFirm aFirm = new EnergyFirm(space, grid,ID, aSector,tempBankID,aClearingHouse.ID);
tempBankList.get(index).paymentAccountList.add(aFirm.paymentAccount);
tempBankList.get(index).centralBankReserves.accountBalance += aFirm.paymentAccount.accountBalance;
EnergyInventory aInventory = new EnergyInventory(aFirm.ID, aSector.sectorID, tempBankList.get(index).ID, aFirm.price);
aFirm.mallInventoryList.add(aInventory);
aInventory.plannedDelivery = aFirm.initialOutput;
aInventory.inventoryStock = aFirm.initialOutput;
/*TODO: change if more malls are allowed*/
aSector.mallList.get(0).firmInventories.add(aInventory);
aSector.firmList.add(aFirm);
aSector.initialOutput += aFirm.initialOutput;
context.add(aFirm);
ID++;
}
// Service sector
}else {
ServiceSector aSector = new ServiceSector(space, grid, i,totalSizeLaborForce , numFirmsPerSector);
totalLaborIncome += aSector.totalLaborCosts;
totalLaborForceData+= aSector.employmentData;
aSector.mallList.add(new ServiceMall(space, grid, i, i));
context.add(aSector);
tempSectorList.add(aSector);
for (int j = 0; j < numFirmsPerSector; j++) {
int index = (int)(Math.random() * ((numBanks-1) + 1));
int tempBankID = tempBankList.get(index).ID;
ServiceFirm aFirm = new ServiceFirm(space, grid,ID, aSector,tempBankID,aClearingHouse.ID);
tempBankList.get(index).paymentAccountList.add(aFirm.paymentAccount);
tempBankList.get(index).centralBankReserves.accountBalance += aFirm.paymentAccount.accountBalance;
ServiceInventory aInventory = new ServiceInventory(aFirm.ID, aSector.sectorID, tempBankList.get(index).ID, aFirm.price);
aFirm.mallInventoryList.add(aInventory);
aInventory.plannedDelivery = aFirm.initialOutput; // ToDo: Check the correct variable
aInventory.inventoryStock = aFirm.initialOutput;
/*TODO: change if more malls are allower*/
aSector.mallList.get(0).firmInventories.add(aInventory);
aSector.initialOutput += aFirm.initialOutput;
aSector.firmList.add(aFirm);
context.add(aFirm);
ID++;
}
}
//Read in consumption coefficents of households, i.e. how much of the consumption budget is spent on this product category
String line = "";
try {
line = Files.readAllLines(Paths.get("consumption.csv")).get(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> items2 = Arrays.asList(line.split("\\s*,\\s*"));
//Goods
if(i < 8 || i== 9) {
consumptionCoefficientList.add(new ConsumptionCoefficientGoods(i,Double.parseDouble(items2.get(1) )));
//Energy
}else if(i==8) {
consumptionCoefficientList.add(new ConsumptionCoefficientEnergy(i,Double.parseDouble(items2.get(1) )));
//Service
}else {
consumptionCoefficientList.add(new ConsumptionCoefficientService(i,Double.parseDouble(items2.get(1) )));
}
}
//Create households
double cumLabForce = 0;
int j = 0;
for(int i=0; i < numHouseholds; i++) {
//Assign workers to sectoral labor force
if(i - cumLabForce > tempSectorList.get(j).laborForce) {
cumLabForce += tempSectorList.get(j).laborForce;
j++;
}
// Assign bank
int index = (int)(Math.random() * ((numBanks-1) + 1));
int tempBankID = tempBankList.get(index).ID;
Household aHousehold = new Household(ID,tempBankID,aClearingHouse.ID);
tempBankList.get(index).paymentAccountList.add(aHousehold.paymentAccount);
tempBankList.get(index).centralBankReserves.accountBalance += aHousehold.paymentAccount.accountBalance;
aHousehold.consumptionCoefficientList = consumptionCoefficientList;
aHousehold.disposableIncome = totalLaborIncome/totalLaborForceData;
/*TODO: this line should be made generic*/
aHousehold.propensityToConsume = 1519948.0/totalLaborIncome;
aHousehold.currentWorkingSector = tempSectorList.get(j);
context.add(aHousehold);
ID++;
}
// Create rest of world
ROW restOfWorld = new ROW(ID,aForeignBank.ID);
restOfWorld.nominalExpotVolumeTarget = totalLaborIncome/totalLaborForceData*numHouseholds * (1596846/1853274.0) ;
aForeignBank.paymentAccountList.add(restOfWorld.paymentAccount);
context.add(restOfWorld);
ID++;
double totalOutputID=0;
//Create statistic agency
StatisticAgency aStatisticAgency = new StatisticAgency(space, grid,ID,totalOutputID, numHouseholds);
context.add(aStatisticAgency);
for ( Object obj : context ) {
NdPoint pt = space . getLocation ( obj );
grid . moveTo ( obj , ( int ) pt . getX () , ( int ) pt . getY ());
}
//System.out.println("Test");
//necessary to end batch runs, does XXX iterations (or XXX ticks)
//to generate multiple runs for a particular parameter constellation
//define as many random sees as you want runs
// if (RunEnvironment.getInstance().isBatch()) {
// RunEnvironment.getInstance().endAt(numIterationsBatchRuns);
// }
return context;
}
}