-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.m
253 lines (215 loc) · 8.96 KB
/
simulation.m
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
rng(42);
villages = 1:8;
timesteps = 1:500;
maxUnitAmount = 15;
maxGoldExtractedAtTimestep = 5;
totalGoldAvailabilityAtVillage = 500;
priceUpdateParameter = 0.9;
requiredAmoutForWindow = 10;% TODO This should be calculated on demand
lostRelativeRentabilityOfSpace = 0; % For a single item the rentability is always the same
maxInventoryAtVillage = 300;
% Source and destination of trade caravans
s = [1 1 2 2 2 5 6 6 7 8];
t = [2 7 6 5 3 4 5 7 5 7];
% Transport price in proportion to the price of the good
costs = digraph(s,t,rand(1,length(s)).*0.25);
% Distances in days
distances = digraph(s,t,randi([1 7],1,length(s)));
% Producer villages
producers = [1 8];
% Consumer villages
consumers = [3 4];
producerCosts = [
sort(randi([1 50],1,maxUnitAmount))
sort(randi([1 50],1,maxUnitAmount))
];
consumerOffers = [
sort(randi([0 30],1,maxUnitAmount),'descend')
sort(randi([0 30],1,maxUnitAmount),'descend')
];
economicInformation = struct;
for village = villages
economicInformation(village).village = village;
economicInformation(village).price = zeros(1,maxUnitAmount);
economicInformation(village).offer = zeros(1,maxUnitAmount);
economicInformation(village).gold = 0;
economicInformation(village).availability = 0;
end
for i = 1:length(producers)
economicInformation(producers(i)).cost = producerCosts(i,:);
economicInformation(producers(i)).price = producerCosts(i,:);
end
for i = 1:length(consumers)
economicInformation(consumers(i)).offer = consumerOffers(i,:);
economicInformation(village).gold = sum(consumerOffers(i,:));
end
for village = villages
next = successors(distances,village);
meanDistance = mean(distances.Edges.Weight(findedge(distances,village,next)));
economicInformation(village).updateTime = 1+gamrnd(1,meanDistance);
end
oreAvailability = randomOreExtraction(repmat(timesteps,length(villages),1),maxGoldExtractedAtTimestep,totalGoldAvailabilityAtVillage);
pricesAllTime = zeros(length(timesteps),length(villages),maxUnitAmount);
offersAllTime = zeros(length(timesteps),length(villages),maxUnitAmount);
equilibriumPricesAllTime = zeros(length(timesteps),length(villages));
equilibriumAmountAllTime = zeros(length(timesteps),length(villages));
salesAllTime = zeros(length(timesteps));
for time = timesteps
for village = villages
economicInformation(village).gold = economicInformation(village).gold + oreAvailability(village,time);
if ismember(village,consumers)
maxOffer = max(economicInformation(village).offer);
totalOffer = sum(economicInformation(village).offer);
differenceOfGold = economicInformation(village).gold*(maxOffer/totalOffer);
economicInformation(village).offer = max(economicInformation(village).offer / maxOffer * differenceOfGold,0);
end
if ismember(village,producers) && max(economicInformation(village).offer) > min(economicInformation(village).cost)
amount = sum(economicInformation(village).offer > economicInformation(village).price);
if economicInformation(village).availability + amount < maxInventoryAtVillage
economicInformation(village).availability = economicInformation(village).availability + amount;
end
end
next = successors(distances,village);
if economicInformation(village).updateTime < time && ~isempty(next)
meanDistance = mean(distances.Edges.Weight(findedge(distances,village,next)));
nextUpdateDeltaTime = gamrnd(1,meanDistance);
economicInformation(village).updateTime = time + nextUpdateDeltaTime;
sourceVillage = village;
if length(next) > 1
targetVillage = randsample(next,1);
else
targetVillage = next;
end
tripCost = 1 + costs.Edges.Weight(findedge(costs,sourceVillage,targetVillage));
deltaPrice = priceUpdateParameter*(economicInformation(sourceVillage).price - economicInformation(targetVillage).price);
economicInformation(sourceVillage).price = economicInformation(sourceVillage).price - deltaPrice;
economicInformation(targetVillage).price = economicInformation(sourceVillage).price + deltaPrice;
deltaOffer = priceUpdateParameter*(economicInformation(sourceVillage).offer - economicInformation(targetVillage).offer);
economicInformation(sourceVillage).offer = economicInformation(sourceVillage).offer - deltaOffer;
economicInformation(targetVillage).offer = economicInformation(targetVillage).offer + deltaOffer;
buyer = targetVillage;
seller = sourceVillage;
[~,equilibriumPrice] = findPriceQuantity(economicInformation(buyer).offer,economicInformation(seller).price*tripCost);
if ~isempty(equilibriumPrice)
toBuy = floor(economicInformation(buyer).gold/equilibriumPrice);
toBuy = min(toBuy,economicInformation(seller).availability);
toBuy = min(toBuy,maxInventoryAtVillage - economicInformation(buyer).availability);
if toBuy > 0
economicInformation(seller).availability = economicInformation(seller).availability - toBuy;
economicInformation(buyer).availability = economicInformation(buyer).availability + toBuy;
economicInformation(seller).gold = economicInformation(seller).gold + equilibriumPrice*toBuy;
economicInformation(buyer).gold = economicInformation(buyer).gold - equilibriumPrice*toBuy;
salesAllTime(time) = salesAllTime(time) + 1;
end
end
end
offer = economicInformation(village).offer;
price = economicInformation(village).price;
[equilibriumAmounts,equilibriumPrices] = findPriceQuantity(offer,price);
if ~isempty(equilibriumPrices)
equilibriumPricesAllTime(time,village) = equilibriumPrices(1);
end
if ~isempty(equilibriumAmounts)
equilibriumAmountAllTime(time,village) = equilibriumAmounts(1);
end
pricesAllTime(time,village,:) = price;
offersAllTime(time,village,:) = offer;
end
end
close all;
print = 1;
g = figure;
plot(costs,'Layout','force','EdgeLabel',costs.Edges.Weight);
title('Transport costs proportional to item price');
if print
g = gcf;
exportgraphics(g,'images/Costs.png','Resolution',300);
end
figure;
plot(distances,'Layout','force','EdgeLabel',distances.Edges.Weight);
title('Transport round trip time');
if print
g = gcf;
exportgraphics(g,'images/Transport.png','Resolution',300);
end
figure;
plot(timesteps,meanOreExtraction(timesteps,maxGoldExtractedAtTimestep,totalGoldAvailabilityAtVillage));
xlabel('Time [day]');
ylabel('Gold [unit]');
title('Mean ore extraction per timestep');
if print
g = gcf;
exportgraphics(g,'images/Gold_means.png','Resolution',300);
end
figure;
yyaxis left;
plot(timesteps,mean(equilibriumPricesAllTime,2));
ylabel('Gold [unit]');
hold on;
yyaxis right;
plot(timesteps,mean(equilibriumAmountAllTime,2));
ylabel('Goods [unit]');
xlabel('Time [day]');
legend('Equilibrium price','Equilibrium amount');
title('Average of all equilibrium prices and amounts across time');
if print
g = gcf;
exportgraphics(g,'images/Prices.png','Resolution',300);
end
figure;
plot(timesteps,salesAllTime);
xlabel('Time [day]');
ylabel('Amount [unit]');
title('Trades per timestep');
if print
g = gcf;
exportgraphics(g,'images/Trades.png','Resolution',300);
end
figure;
yyaxis left;
plot(1:length(oreAvailability),sum(oreAvailability,1));
ylabel('Gold [unit]');
hold on;
yyaxis right;
plot(1:length(oreAvailability),cumsum(sum(oreAvailability,1)));
xlabel('Time [day]');
ylabel('Gold [unit]');
legend('Gold extracted at timestep','Total gold');
title('Aggregate of all gold extraction across time');
if print
g = gcf;
exportgraphics(g,'images/Gold_total.png','Resolution',600);
end
figure;
bar([economicInformation(:).gold]);
xlabel('Villages');
ylabel('Gold [unit]');
title('Gold per village at end');
if print
g = gcf;
exportgraphics(g,'images/Gold_per_village.png','Resolution',300);
end
figure;
bar([economicInformation(:).availability]);
xlabel('Villages');
ylabel('Goods [unit]');
title('Goods per village at end');
if print
g = gcf;
exportgraphics(g,'images/Goods_per_village.png','Resolution',300);
end
figure;
time = 50;
village = 6;
plot(1:maxUnitAmount,squeeze(pricesAllTime(time,village,:)));
hold on;
plot(1:maxUnitAmount,squeeze(offersAllTime(time,village,:)));
plot(1:maxUnitAmount,ones(maxUnitAmount)*sum(oreAvailability(village,1:time)));
xlabel('Goods [unit]');
ylabel('Gold [unit]');
legend('prices','offers','gold at village');
title('Price and offer for goods at time '+string(time)+' at village '+string(village));
if print
g = gcf;
exportgraphics(g,'images/Price_and_offer_at_'+string(time)+'_at_'+string(village)+'.png','Resolution',300);
end