-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMasterDataGenerator.cs
More file actions
336 lines (280 loc) · 12.4 KB
/
MasterDataGenerator.cs
File metadata and controls
336 lines (280 loc) · 12.4 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace api
{
internal static class MasterDataGenerator
{
public static void SetTime(ConnectionMultiplexer redis, int startOffset)
{
var db = redis.GetDatabase();
db.StringSet("start_at", Api.GetTime() + startOffset);
var period = db.StringGet("period");
if (period.IsNull)
{
db.StringSet("period", 1000000);
}
}
public static void SetPeriod(ConnectionMultiplexer redis, int period)
{
var db = redis.GetDatabase();
db.StringSet("period", period);
}
public static void SetTimeAndPeriod(ConnectionMultiplexer redis, long startAt, int period)
{
var db = redis.GetDatabase();
db.StringSet("start_at", startAt);
db.StringSet("period", period);
}
public static int GetPeriod(ConnectionMultiplexer redis)
{
var db = redis.GetDatabase();
return int.Parse(db.StringGet("period").ToString());
}
private static double GetNormRandom(Random random)
{
double x = random.NextDouble();
double y = random.NextDouble();
double z = Math.Sqrt(-2.0 * Math.Log(x)) * Math.Cos(2.0 * Math.PI * y);
return z;
}
private static double GetNormRandom(Random random, double mu, double sigma)
{
return (mu + sigma * GetNormRandom(random));
}
// ReSharper disable once ClassNeverInstantiated.Local
private class ConfigData
{
[JsonPropertyName("seed")]
public int Seed { get; }
[JsonPropertyName("resource_time_resolution")]
public int ResourceTimeResolution { get; }
[JsonPropertyName("target_num_resource")]
public int TargetNumResource { get; }
[JsonPropertyName("min_num_resource")]
public int MinNumResource { get; }
[JsonPropertyName("max_num_resource")]
public int MaxNumResource { get; }
[JsonPropertyName("weight_end")]
public int WeightEnd { get; }
[JsonPropertyName("types")]
public TypeData[] Types { get; }
[JsonConstructor]
public ConfigData(int seed, int resourceTimeResolution, int targetNumResource, int minNumResource, int maxNumResource, int weightEnd, TypeData[] types) =>
(Seed, ResourceTimeResolution, TargetNumResource, MinNumResource, MaxNumResource, WeightEnd, Types) =
(seed, resourceTimeResolution, targetNumResource, minNumResource, maxNumResource, weightEnd, types);
}
// ReSharper disable once ClassNeverInstantiated.Local
private class TypeData
{
[JsonPropertyName("type")]
public string Type { get; }
[JsonPropertyName("min_time")]
public int MinTime { get; }
[JsonPropertyName("max_time")]
public int MaxTime { get; }
[JsonPropertyName("probability")]
public int Probability { get; }
[JsonPropertyName("weight_params")]
public WeightParam[] WeightParams { get; }
[JsonConstructor]
public TypeData(string type, int minTime, int maxTime, int probability, WeightParam[] weightParams) =>
(Type, MinTime, MaxTime, Probability, WeightParams) =
(type, minTime, maxTime, probability, weightParams);
}
// ReSharper disable once ClassNeverInstantiated.Local
private class WeightParam
{
[JsonPropertyName("start")]
public int Start { get; }
[JsonPropertyName("mu")]
public double Mu { get; }
[JsonPropertyName("sigma")]
public double Sigma { get; }
[JsonConstructor]
public WeightParam(int start, double mu, double sigma) =>
(Start, Mu, Sigma) =
(start, mu, sigma);
}
private static string GetRandomType(Random random, int sumProbability, IEnumerable<(string, int)> typeProbabilityList)
{
var rnd = random.Next(sumProbability);
foreach (var (type, probability) in typeProbabilityList)
{
if (rnd < probability)
{
return type;
}
rnd -= probability;
}
throw new Exception("invalid type probability");
}
private static Dictionary<int, List<(string, int)>> GenerateTypeSchedule(int period, ConfigData configData, Random random)
{
Debug.Assert(period > 0 && period % configData.ResourceTimeResolution == 0);
foreach (var t in configData.Types)
{
Debug.Assert(t.MinTime % configData.ResourceTimeResolution == 0);
Debug.Assert(t.MaxTime % configData.ResourceTimeResolution == 0);
}
var typeProbabilityList = configData.Types.Select(t => (t.Type, t.Probability)).ToList();
var sumProbability = configData.Types.Sum(t => t.Probability);
var typeTime = configData.Types.ToDictionary(
t => t.Type,
t => (t.MinTime / configData.ResourceTimeResolution, t.MaxTime / configData.ResourceTimeResolution));
var counter = new int[period / configData.ResourceTimeResolution];
var sumCounter = 0L;
var typeSchedule = new Dictionary<int, List<(string, int)>>();
for (var k = 0; k < configData.MinNumResource; k++)
{
for (var s = 0; s < counter.Length; )
{
var type = GetRandomType(random, sumProbability, typeProbabilityList);
var (min, max) = typeTime[type];
var t = random.Next(min, max + 1);
if (s + t > counter.Length) s = counter.Length - t;
if (!typeSchedule.ContainsKey(s)) typeSchedule.Add(s, new List<(string, int)>());
typeSchedule[s].Add((type, t));
sumCounter += t;
for (var i = 0; i < t; i++) ++ counter[s + i];
s += t;
}
}
while (sumCounter < counter.Length * configData.TargetNumResource)
{
var type = GetRandomType(random, sumProbability, typeProbabilityList);
var (min, max) = typeTime[type];
var t = random.Next(min, max + 1);
for (var numTry = 0;; numTry++)
{
if (numTry >= 1000000) throw new Exception("GenerateTypeSchedule failure");
var s = random.Next(0, counter.Length - t + 1);
var ok = true;
for (var i = 0; i < t; i++)
{
if (counter[s + i] >= configData.MaxNumResource)
{
ok = false;
break;
}
}
if (ok)
{
if (!typeSchedule.ContainsKey(s)) typeSchedule.Add(s, new List<(string, int)>());
typeSchedule[s].Add((type, t));
sumCounter += t;
for (var i = 0; i < t; i++) ++ counter[s + i];
break;
}
}
}
return typeSchedule;
}
private static WeightParam SelectWeightParam(int weightEnd, IReadOnlyList<WeightParam> weightParams, long t0, long period)
{
for (var i = 1; i < weightParams.Count; i++)
{
var w = weightParams[i];
// if (t0 / period < w.Start / weightEnd)
if (t0 * weightEnd < w.Start * period)
{
return weightParams[i-1];
}
}
return weightParams[^1];
}
public static byte[] Generate(int period)
{
Console.WriteLine("MasterDataGenerator.Generate(period: {0})", period);
// import config
var configData = JsonSerializer.Deserialize<ConfigData>(File.ReadAllBytes(@"./map_config.json"))!;
var typeDict = configData.Types.ToDictionary(t => t.Type, t => t);
foreach (var t in configData.Types)
{
for (var i = 1; i < t.WeightParams.Length; i++)
{
Debug.Assert(t.WeightParams[i - 1].Start < t.WeightParams[i].Start);
}
}
var seed = configData.Seed;
Console.WriteLine("seed: {0}", seed);
var random = new Random(seed);
var typeSchedule = GenerateTypeSchedule(period, configData, random);
var points = new List<(int, int)>();
for (var x = 0; x <= 30; x++)
{
for (var y = 0; y <= 30; y++)
{
if (x == 0 && y == 0) continue;
if (x == 0 && y == 30) continue;
if (x == 15 && y == 15) continue;
if (x == 30 && y == 0) continue;
if (x == 30 && y == 30) continue;
points.Add((x, y));
}
}
var resource = new List<object>();
var endCount = new Dictionary<int, List<(int, int)>>();
for (var t = 0; t < period / configData.ResourceTimeResolution; t++)
{
// 同じ点でリソースが重複しないように管理。リソース消滅時に追加して復活
if (endCount.ContainsKey(t))
{
foreach (var (x, y) in endCount[t]) points.Add((x, y));
}
if (typeSchedule.ContainsKey(t))
{
foreach (var (type, dt) in typeSchedule[t])
{
var id = resource.Count + 1;
// 座標決定
var pIndex = random.Next(points.Count);
var (x, y) = points[pIndex];
points.RemoveAt(pIndex);
if (!endCount.ContainsKey(t + dt)) endCount.Add(t + dt, new List<(int, int)>());
endCount[t + dt].Add((x, y));
// 時間計算
var t0 = t * configData.ResourceTimeResolution;
var t1 = (t + dt) * configData.ResourceTimeResolution;
// 重み決定
var weightParam = SelectWeightParam(configData.WeightEnd, typeDict[type].WeightParams, t0, period);
var weight = Math.Max(1, (long)Math.Round(GetNormRandom(random, weightParam.Mu, weightParam.Sigma)));
resource.Add(new { id, x, y, t0, t1, type, weight });
}
}
}
return JsonSerializer.SerializeToUtf8Bytes(new { resource, period });
}
public static void Import(ConnectionMultiplexer redis, byte[] json, bool setPeriod)
{
Console.WriteLine("Import start");
var data = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
var db = redis.GetDatabase();
if (setPeriod)
{
db.StringSet("period", data!["period"].GetInt32());
}
db.KeyDelete("resource");
var tasks = new List<Task>();
foreach (var r in data!["resource"].EnumerateArray())
{
var id = r.GetProperty("id").GetInt32();
var x = r.GetProperty("x").GetInt32();
var y = r.GetProperty("y").GetInt32();
var t0 = r.GetProperty("t0").GetInt32();
var t1 = r.GetProperty("t1").GetInt32();
var type = r.GetProperty("type").GetString();
var weight = r.GetProperty("weight").GetInt32();
tasks.Add(db.ListRightPushAsync("resource", $"{id} {x} {y} {t0} {t1} {type} {weight}"));
}
db.WaitAll(tasks.ToArray());
Console.WriteLine("Import end");
}
}
}