-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
276 lines (231 loc) · 10.4 KB
/
Program.cs
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
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;
namespace generator
{
internal static class Program
{
public static async Task Main(string[] args)
{
var seed = await File.ReadAllBytesAsync(args[0]);
var random = new Arcfour(seed);
await File.WriteAllBytesAsync(args[2], Generate(args[1], random));
}
private static byte[] Generate(string configPath, Arcfour random)
{
// import config
var configData = JsonSerializer.Deserialize<ConfigData>(File.ReadAllBytes(configPath))!;
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 period = configData.Period;
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 });
}
private static Dictionary<int, List<(string, int)>> GenerateTypeSchedule(int period, ConfigData configData, Arcfour 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];
}
private static double GetNormRandom(Arcfour random, double mu, double sigma)
{
return (mu + sigma * GetNormRandom(random));
}
private static double GetNormRandom(Arcfour 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 string GetRandomType(Arcfour 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");
}
// ReSharper disable once ClassNeverInstantiated.Local
private class ConfigData
{
[JsonPropertyName("period")]
public int Period { 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 period, int resourceTimeResolution, int targetNumResource, int minNumResource, int maxNumResource, int weightEnd, TypeData[] types) =>
(Period, ResourceTimeResolution, TargetNumResource, MinNumResource, MaxNumResource, WeightEnd, Types) =
(period, 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);
}
}
}