-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdomain.cpp
More file actions
1251 lines (1027 loc) · 30.6 KB
/
domain.cpp
File metadata and controls
1251 lines (1027 loc) · 30.6 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* domain.cpp
*/
#include "account.h"
#include <math.h>
#include "QtCore/qdebug.h"
#include <QSettings>
#include <QListWidgetItem>
#include <QSettings>
#include <QMessageBox>
#define NUMBER_OF_BANKS 3
#define CLEARING_FREQUENCY 10
/*
* Statics
*/
QList<Domain*> Domain::domains; // static
const QMap<ParamType,QString> Domain::parameterKeys // static
{
{ParamType::procurement, "govt-procurement"},
//{ParamType::emp_rate, "employment-rate"}, // probably redundant
{ParamType::prop_con, "propensity-to-consume"},
{ParamType::inc_tax_rate, "income-tax-rate"},
{ParamType::inc_thresh, "income-threshold"},
{ParamType::sales_tax_rate, "sales-tax-rate"},
{ParamType::firm_creation_prob, "firm-creation-prob"},
{ParamType::dedns, "pre-tax-dedns-rate"},
{ParamType::unemp_ben_rate, "unempl-benefit-rate"},
{ParamType::pop, "population"},
{ParamType::distrib, "reserve-rate"},
{ParamType::prop_inv, "prop-invest"},
{ParamType::boe_int, "boe-interest"},
{ParamType::bus_int, "bus-interest"},
{ParamType::loan_prob, "loan-prob"},
{ParamType::recoup, "capex-recoup-periods"},
{ParamType::std_wage, "standard-wage"},
{ParamType::gov_size, "government-size"},
};
QMap<QString,Property> Domain::propertyMap; // static, can't be const as we have to initialise it
void Domain::initialisePropertyMap() // static
{
static bool is_initialised = false;
if (!is_initialised)
{
propertyMap[tr("100 reference line")] = Property::hundred;
propertyMap[tr("Average business size")] = Property::bus_size;
propertyMap[tr("Bank loans")] = Property::amount_owed;
propertyMap[tr("Benefits paid")] = Property::bens_paid;
propertyMap[tr("Bonuses paid")] = Property::bonuses;
propertyMap[tr("Businesses balance")] = Property::prod_bal;
propertyMap[tr("Consumption")] = Property::consumption;
propertyMap[tr("Deficit (absolute)")] = Property::deficit;
propertyMap[tr("Deficit as % GDP")] = Property::deficit_pc;
propertyMap[tr("GINI coefficient")] = Property::gini;
propertyMap[tr("Mean wages")] = Property::mean;
propertyMap[tr("Wages spread (97 percentile)")] = Property::spread;
propertyMap[tr("Government receipts (cumulative)")] = Property::gov_recpts;
propertyMap[tr("Govt direct support")] = Property::unbudgeted;
propertyMap[tr("Govt exp excl benefits")] = Property::gov_exp;
propertyMap[tr("Govt exp incl benefits")] = Property::gov_exp_plus;
propertyMap[tr("Households balance")] = Property::dom_bal;
propertyMap[tr("Income tax paid")] = Property::inc_tax;
propertyMap[tr("National Debt")] = Property::gov_bal;
propertyMap[tr("Number employed")] = Property::num_emps;
propertyMap[tr("Number of businesses")] = Property::num_firms;
propertyMap[tr("Number of govt employees")] = Property::num_gov_emps;
propertyMap[tr("Number of new hires")] = Property::num_hired;
propertyMap[tr("Number of new fires")] = Property::num_fired;
propertyMap[tr("Number unemployed")] = Property::num_unemps;
propertyMap[tr("Percent active")] = Property::pc_active;
propertyMap[tr("Percent employed")] = Property::pc_emps;
propertyMap[tr("Percent unemployed")] = Property::pc_unemps;
propertyMap[tr("Population size")] = Property::pop_size;
propertyMap[tr("Pre-tax deductions")] = Property::dedns;
propertyMap[tr("Procurement expenditure")] = Property::procurement;
propertyMap[tr("Productivity")] = Property::productivity;
propertyMap[tr("Productivity (relative)")] = Property::rel_productivity;
propertyMap[tr("Sales tax paid")] = Property::sales_tax;
propertyMap[tr("Wages paid")] = Property::wages;
propertyMap[tr("Zero reference line")] = Property::zero;
is_initialised = true;
}
}
/*
* This function creates a new domain having the given name and currency, and
* default parameters, and returns a pointer to it. If the domain already exists
* (i.e. is already on the list) it returns a nullptr and doesn't create a new
* domain.
*/
Domain *Domain::createDomain(const QString &name)
{
Domain *dom = nullptr;
if (getDomain(name) == nullptr)
{
// A domain with given name is not in list. Create a new domain having
// the required name and currency, and default parameters, and add it
// to the end of the list
dom = new Domain(name);
}
// Return a pointer to the domain or nullptr if it already exists
return dom;
}
/*
* Return a pointer to the the domain having the given name, or a nullptr if it
* is not listed.
*/
Domain *Domain::getDomain(const QString &name)
{
for (int i = 0; i < domains.count(); i++)
{
Domain *dom = Domain::domains.at(i);
if (dom->getName() == name) {
return dom;
}
}
return nullptr;
}
void Domain::reset()
{
qDebug() << "Initialising domain" << getName();
last_period = -1;
int pop = getParameterVal(ParamType::pop) * 100; // for internal use. For
// display, divide this by
// 100 to get the result
// in millions, or
// multiply by 10,000 for
// result in units
/*
* Remove old instances from the list of workers
*/
for (int i = 0 ; i < workers.count(); i++)
{
delete(workers[i]);
}
workers.clear();
for (int i = 0 ; i < pop; i++)
{
workers.append(new Worker(this));
}
/*
* Remove old instances from list of firms (this will include _gov)
*/
for (int i = 0 ; i < firms.count() ; i++)
{
delete(firms[i]);
}
firms.clear();
/*
* Create a government with the required number of employees
*/
if (_gov != nullptr)
{
delete (_gov);
}
_gov = new Government(this, (pop * static_cast<int>(getGovSize()) / 100));
/*
* Add firms
*/
QSettings settings;
int n = settings.value("start-ups", 10).toInt();
for (int i = 0; i < n; i++)
{
firms.append(new Firm(this));
}
/*
* Reomove old Bank instances and re-create
*/
for (int i = 0; i < banks.count(); i++)
{
delete(banks[i]);
}
banks.clear();
for (int i = 0; i < NUMBER_OF_BANKS; i++)
{
banks.append(new Bank(this));
}
foreach(Firm *firm, firms)
{
firm->init();
}
}
/*
* This constructor is private and is only called via createDomain, which
* handles all associated admin.
*/
Domain::Domain(const QString &name)
{
qDebug() << "Domain::Domain(" << name << ")";
/*
* Set the domain's parameters. Note that this is driven by the
* parameters we expect (i.e. that are listed in parameterKeys),
* Parameters don't have default values because we refer to them
* indirectly (we could change this but it would be a hassle) so
* if a key is missing from settings the parameter will not be set.
*/
QSettings settings;
settings.beginGroup("Domains");
QString group = settings.group();
QStringList keys = settings.childGroups();
bool is_default;
if (keys.contains(name))
{
settings.beginGroup(name);
is_default = false;
}
else
{
/*
* The default settings are stored in the group [Default]
*/
settings.endGroup(); // leave the Domains group
settings.beginGroup("Default"); // start the Default group
is_default = true;
}
_name = name;
_currency = settings.value("Currency", "Units").toString();
_abbrev = settings.value("Abbrev", "CU").toString();
foreach (ParamType p, parameterKeys.keys())
{
QString key_string = parameterKeys.value(p);
if (settings.contains(key_string))
{
params[p] = settings.value(key_string).toInt();
}
else
{
QMessageBox msgBox;
QString msgText;
msgText = "Parameter \"" + key_string
+ "\" is missing from settings for "
+ name;
msgBox.setText(msgText);
msgBox.exec();
}
}
if (!is_default)
{
settings.endGroup(); // <name>
}
settings.endGroup(); // Domains or Default
/*
* We separate out initialisation so we can reset everything before
* (re)drawing the charts.
*/
_gov = nullptr; // so we don't try to delete it before it's created
reset(); // set initial conditiions
/*
* Add this domain to the list of domains
*/
domains.append(this);
}
/*
* Restore all domains from settings
*/
int Domain::restoreDomains(QStringList &domainNameList)
{
foreach (QString name, domainNameList)
{
createDomain(name);
}
qDebug() << domains.count() << "domains created";
return domains.count();
}
void Domain::drawCharts(QListWidget *propertyList)
{
qDebug() << "Domain::drawCharts() called. propertyList contains"
<< propertyList->count() << "properties";
/*
* Draw an unpopulated chart for each domain, creating the necessary
* series in a QMap but not yet attaching them to the chart
*/
foreach(Domain *dom, domains)
{
qDebug() << "Initialising domain" << dom->getName();
dom->reset();
dom->drawChart(propertyList);
}
/*
* Iterate for the required number of periods, populating the series
*/
QSettings settings;
int iterations = settings.value("iterations", 100).toInt();
int start_period = settings.value("start-period").toInt();
for (int period = 0; period <= iterations + start_period; period++)
{
foreach(Domain *dom, domains)
{
dom->iterate(period, period < start_period);
}
}
/*
* Now add the populated series to each of the charts...
*/
foreach(Domain *dom, domains)
{
dom->addSeriesToChart();
}
}
void Domain::addSeriesToChart()
{
auto it = series.begin();
while (it != series.end())
{
chart->addSeries(it.value());
++it;
}
chart->createDefaultAxes();
/* TODO
*
* prod should be a dynamic variable (_prod) accessible as a property
*/
double prod = getProductivity();
// We will need to re-instate these labels on the status bars
// inequalityLabel->setText(tr("Inequality: ") + QString::number(round(gini * 100)) + "%");
// productivityLabel->setText(tr("Productivity: ") + QString::number(round(prod + 0.5)) + "%");
// emit drawingCompleted();
}
Firm *Domain::createFirm(bool state_supported)
{
Firm *firm = new Firm(this, state_supported);
if (state_supported)
{
// Come back to this later
// QSettings settings;
// hireSome(firm, getStdWage(), 0, settings.value("government-employees").toInt());
}
firms.append(firm);
return firm;
}
/*
* Returns any firm except the government (which is not in the list of firms)
* and the firm indicated by the exclude argument. If there are none nullptr is
* returned.
*/
Firm *Domain::selectRandomFirm(Firm *exclude)
{
Firm *res = nullptr;
int n = firms.size();
if (n == 0)
{
res = nullptr;
}
else
{
/*
* Warning: this could go on forever if there's only one firm and it's
* excluded. This shouldn't be possible normally.
*/
while ( (res = firms[ (qrand() % (firms.size() - 1)) ]) == exclude );
}
return res;
}
/*
* This function returns a pointer to one of the banks belonging to the domain,
* chosen at random.
*/
Bank *Domain::selectRandomBank()
{
return banks[(qrand() % (banks.size() - 1))]; // *** CHECK THIS! ***
}
/*
* This function takes the name of the property as listed in the dock widget
* and returns the atual property (i.e. the enum)
*/
Property Domain::getProperty(QString propertyName)
{
QMap<QString,Property>::const_iterator it = propertyMap.find(propertyName);
Q_ASSERT(it != propertyMap.end());
return it.value();
}
/*
* Properties are domain values that are liable to change at each iteration.
* This function returns the current value of each property. Note that is is
* important for the order of properties inside the switch to ne maintained
* as some properties are dependent of properties defined earlier.
*/
double Domain::getPropertyVal(Property p)
{
switch(p)
{
case Property::num_gov_emps:
return _gov->getNumEmployees();
case Property::pop_size:
_pop_size = getPopulation();
return double(_pop_size);
case Property::gov_exp:
_exp = _gov->getExpenditure();
return _exp;
case Property::bens_paid:
_bens = _gov->getBenefitsPaid();
return _bens;
case Property::gov_exp_plus:
return _exp + _bens;
case Property::gov_recpts:
_rcpts = _gov->getReceipts();
return _rcpts;
case Property::deficit:
_deficit = _exp + _bens - _rcpts;
return _deficit;
case Property::gov_bal:
/*
* This will always be negative as the government is never in receipt
* of its own currency. It amounts to the sum of all government
* expenditures in the currency of account. It will always (numerically)
* exceed the National Debt as it ignores tax receipts -- conventionally
* taken as offsetting expenditure. To find the 'deficit' (see above)
* you have to add tax receipts.
*/
_gov_bal = _gov->getBalance();
return _gov_bal;
case Property::num_firms:
_num_firms = firms.count();
//qDebug() << "_num_firms =" << _num_firms;
return double(_num_firms);
case Property::num_emps:
_num_emps = getNumEmployed();
return double(_num_emps);
case Property::pc_emps:
return (_pop_size > 0 ? double(_num_emps * 100) / _pop_size : 0.0);
case Property::num_unemps:
_num_unemps = getNumUnemployed();
return double(_num_unemps);
case Property::pc_unemps:
return (_pop_size > 0 ? double(_num_unemps * 100) / _pop_size : 0);
case Property::pc_active:
_pc_active = double(_num_emps + _num_unemps) / 10; // assuming granularity 1000
return _pc_active;
case Property::num_hired:
return double(_num_hired);
case Property::num_fired:
return double(_num_fired);
case Property::prod_bal:
_amount_owed = getAmountOwed();
_prod_bal = getProdBal(); // ignoring loans
return _prod_bal - _amount_owed; // but show minus loans
// see http://bilbo.economicoutlook.net/blog/?p=32396
// where domestic sector is taken
// to include banks
case Property::wages:
_wages = getWagesPaid(); // not cumulative -- consider adding cumulative amount
return _wages;
case Property::consumption:
_consumption = getPurchasesMade(); // not cumulative -- consider adding cumulative amount
return _consumption;
case Property::deficit_pc:
return abs(_consumption) < 1.0 ? 0.0 : (_deficit * 100) / _consumption;
case Property::gini:
return _gini;
case Property::mean:
return _mean;
case Property::spread:
return _spread;
case Property::bonuses:
_bonuses = getBonusesPaid();
return _bonuses;
case Property::dedns:
return _dedns;
case Property::inc_tax:
_inc_tax = getIncTaxPaid();
return _inc_tax;
case Property::sales_tax:
_sales_tax = getSalesTaxPaid();
return _sales_tax;
case Property::dom_bal:
_dom_bal = getWorkersBal();
return _dom_bal;
case Property::amount_owed:
//_amount_owed = getAmountOwed();
return _amount_owed;
case Property::bus_size:
_num_firms = 1; // government is a firm
_num_emps = _gov->employees.count();
foreach(Firm *f, firms)
{
++_num_firms;
//qDebug() << "Firm has" << f->employees.count();
_num_emps += f->employees.count();
}
qDebug() << "_num_emps =" << _num_emps;
_bus_size = _num_emps / _num_firms;
return _bus_size;
case Property::hundred:
return 100.0;
case Property::zero:
return 0.0;
case Property::procurement:
_proc_exp = getProcurementExpenditure(); // government purchases
return _proc_exp;
case Property::productivity:
_productivity = getProductivity();
return _productivity;
case Property::rel_productivity:
if (_num_emps == 0) {
Q_ASSERT(_rel_productivity != 0.0);
_rel_productivity = 1.0; // default
} else {
_rel_productivity = (_productivity * _pop_size) / _num_emps;
}
return _rel_productivity;
case Property::unbudgeted:
return _gov->getUnbudgetedExp();
/*
* The following properties may need reinstating
*
*
case Property::investment:
_investment = getInvestment();
return _investment;
case Property::gdp:
//_gdp = _consumption + _investment + _exp + _bens;
_gdp = _consumption - _investment; // https://en.wikipedia.org/wiki/Gross_domestic_product
return _gdp;
case Property::profit:
// ***** I don't think we should be subtracting income tax here!
_profit = _gdp - _wages - _inc_tax - _sales_tax;
return _profit;
*/
case Property::num_properties:
Q_ASSERT(false);
return 0; // prevent compiler warning
}
}
/*
* Procurement expenditure is government spending on purchases
*/
double Domain::getProcurementExpenditure()
{
return _gov->getProcExp();
}
double Domain::getProductivity()
{
double tot = 0.0;
int count = firms.count();
for (int i = 0; i < count; i++)
{
Firm *f = firms[i];
tot += double(f->getNumEmployees()) * f->getProductivity();
}
double res = count == 0 ? 0 : (tot * 100) / _pop_size;
return res;
}
void Domain::setChartView(QChartView *chartView)
{
_chartView = chartView;
chart = chartView->chart();
}
/*
* drawChart() simply sets up a chart but doesn't populate it.
* See drawCharts...
*/
void Domain::drawChart(QListWidget *propertyList)
{
qDebug() << "Domain::drawChart(...) called";
/*
* Note that the parameters may have been changed since the last time
* drawChart was called. However they should have been updated in
* params[ParamType] and should be retrieved from there rather than using
* specific instance-global values. (E.g. params[ParamType::std_wage
* rather than _std_wage, which has been discontinued).
*/
chart->removeAllSeries(); // built-in chart series
series.clear(); // our global copy, used to hold generated data points
chart->legend()->setAlignment(Qt::AlignTop);
chart->legend()->show();
chart->setTitle("<h2 style=\"text-align:center;\">" + getName() + "</h2>");
Q_ASSERT(propertyList->count() > 0);
for (int i = 0; i < int(Property::num_properties); i++)
{
/*
* We need to construct a line series for each selected property. We
* will let Qt look after values on axes
*/
QListWidgetItem *item;
item = propertyList->item(i);
bool selected = item->checkState();
if (selected)
{
QString series_name = item->text();
QLineSeries *ser = new QLineSeries();
Property p = propertyMap[series_name];
ser->setName(series_name);
/*
* This just inserts the series into our list of series. It doesn't
* add it to the chart
*/
//series.insert(static_cast<Property>(i), ser);
series.insert(p, ser);
}
}
}
// NEXT: IN PROGRESS...
void Domain::iterate(int period, bool silent)
{
Q_ASSERT(period > last_period);
// -------------------------------------------
// Initialisation phase
// -------------------------------------------
last_period = period;
if (period == 0)
{
/*
* Reset government
*/
_gov->reset();
// TODO: The government will also be initialised as a firm, so it would
// be better to remove the reset() function and overload init() instead
/*
* Initialise firms
*/
for (int i = 0; i < firms.count(); i++)
{
firms[i]->init();
}
/*
* Initialise workers
*/
for (int i = 0; i < workers.count(); i++)
{
workers[i]->init();
}
}
/*
* Reset counters
*/
_num_hired = 0;
_num_fired = 0;
_dedns = 0; // TODO: CHECK THIS
// -------------------------------------------
// Trigger phase
// -------------------------------------------
/*
* Triggering government will direct payments to firms and benefits to
* workers before they are triggered
*/
_gov->trigger(period);
// Triggered firms will pay deductions to government and wages to
// workers. Firms will also fire any workers they can't afford to pay.
// Workers receiving payment will pay income tax to the government
for (int i = 0; i < firms.count(); i++)
{
firms[i]->trigger(period);
}
// Trigger workers to make purchases
for (int i = 0; i < workers.count(); i++)
{
workers[i]->trigger(period);
}
// -------------------------------------------
// Post-trigger (epilogue) phase
// -------------------------------------------
// Post-trigger for firms so they can pay tax on sales just made, pay
// bonuses, and hire more employees (investment)
for (int i = 0, c = firms.count(); i < c; i++)
{
firms[i]->epilogue();
}
// Same for workers so they can keep rolling averages up to date
for (int i = 0, c = workers.count(); i < c; i++)
{
workers[i]->epilogue(period);
}
/*
* Wage-related derived properties (Gini, spread and mean)
*/
const int pop = workers.count();
double total = 0;
double rms = 0;
double a = 0;
double n[pop];
int i;
for (i = 0; i < pop; i++)
{
Worker *w = workers[i];
n[i] = w->getAverageWages(); // extend as required
Q_ASSERT(n[i] >= 0);
total += n[i]; // for RMS
}
_mean = double(total / pop);
Q_ASSERT(_mean >= 0.0);
if (period == 0)
{
_gini = 0;
_spread = 0;
}
else
{
std::sort(n, n + pop); // ascending order
for (i = 1; i < pop; i++)
{
double d = (n[i - 1] - _mean);
rms += d * d;
}
rms = sqrt(rms / pop);
for (i = 1; i < pop; i++)
{
n[i] += n[i - 1]; // make values cumulative
}
_spread = _mean > 0 ? ((rms * 3) / _mean) : 0;
double a_tot = (total * pop) / 2; // area A+B
for (int i = 1; i < pop; i++)
{
double diff = ((total * i) / pop) - n[i - 1];
Q_ASSERT(diff >= 0);
a += diff; // area A
}
_gini = (round(double(a * 100) / double(a_tot)))/100;
if (_gini > 100 || _gini < 0)
{
Q_ASSERT(_gini >= 0 && _gini <= 1);
}
qDebug() << "a =" << a << ", a_tot =" << a_tot << "gini =" << _gini
<< "RMS =" << rms << "range ±" << (_spread * 100)
<< "% of mean, mean =" << _mean;
}
/*
* TODO: Record the maximum, minimum and average values of the
* property (non-silent entries only)
*/
// ...
// -------------------------------------------
// Stats
// -------------------------------------------
/*
* Append the values from this iteration to the series
*/
for (auto it = series.begin(); it != series.end(); ++it)
{
Property p = it.key();
QLineSeries *s = series[p];
double value = getPropertyVal(p);
if (!silent)
{
s->append(period, value);
}
}
// -------------------------------------------
// Exogenous changes
// -------------------------------------------
// Create a new firm, possibly
if (qrand() % 100 < getFCP())
{
qDebug() << "Creating new firm";
createFirm();
qDebug() << "*** Number of firms =" << firms.count();
}
}
Government *Domain::government()
{
return _gov;
}
const QString &Domain::getName() const
{
return _name;
}
/*
* This refers to the property <population>, not the starting parameter
*/
int Domain::getPopulation()
{
return _population * 100;
}
int Domain::getNumEmployed()
{
int n = 0;
for (int i = 0; i < firms.count(); i++)
{
n += firms[i]->employees.count();
}
return n;
}
int Domain::getNumEmployedBy(Firm *firm)
{
int n = 0;
for (int i = 0; i < workers.count(); i++)
{
if (workers[i]->isEmployedBy(firm))
{
n++;
}
}
return n;
}
int Domain::getNumUnemployed()
{
int n = 0;
for (int i = 0; i < workers.count(); i++)
{
if (!workers[i]->isEmployed())
{
n++;
}
}
return n;
}
double Domain::getPurchasesMade()
{
double tot = 0;
for (int i = 0; i < workers.count(); i++)
{
tot += workers[i]->getPurchasesMade();
}
return tot;
}
double Domain::getSalesReceipts()
{
double tot = 0;
for (int i = 0; i < firms.count(); i++)
{
tot += firms[i]->getSalesReceipts();
}
return tot;
}
double Domain::getBonusesPaid()
{
double tot = 0;
for (int i = 0; i < firms.count(); i++)