Repository navigation
Expand file tree
/
Copy pathConfigManager.cs
More file actions
464 lines (413 loc) · 13.2 KB
/
Copy pathConfigManager.cs
File metadata and controls
464 lines (413 loc) · 13.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
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
using UnityEngine;
using System.IO;
using System;
/// <summary>
/// Configuration manager for NeuralNetworkRL-Unity
/// Handles loading, saving, and validation of configuration files
/// </summary>
public class ConfigManager : MonoBehaviour
{
[Header("Configuration Files")]
public string configFileName = "rl_config.json";
public string defaultConfigFileName = "default_config.json";
[Header("Auto-load Settings")]
public bool loadConfigOnStart = true;
public bool saveConfigOnExit = true;
public bool validateConfigOnLoad = true;
[Header("Current Configuration")]
public RL.RLConfig currentConfig;
private string configPath;
private string defaultConfigPath;
#region Unity Lifecycle
private void Awake()
{
InitializePaths();
LoadDefaultConfig();
}
private void Start()
{
if (loadConfigOnStart)
{
LoadConfig();
}
}
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus && saveConfigOnExit)
{
SaveConfig();
}
}
private void OnApplicationFocus(bool hasFocus)
{
if (!hasFocus && saveConfigOnExit)
{
SaveConfig();
}
}
private void OnDestroy()
{
if (saveConfigOnExit)
{
SaveConfig();
}
}
#endregion
#region Initialization
private void InitializePaths()
{
configPath = Path.Combine(Application.persistentDataPath, configFileName);
defaultConfigPath = Path.Combine(Application.streamingAssetsPath, defaultConfigFileName);
// Create streaming assets directory if it doesn't exist
if (!Directory.Exists(Application.streamingAssetsPath))
{
Directory.CreateDirectory(Application.streamingAssetsPath);
}
}
private void LoadDefaultConfig()
{
try
{
if (File.Exists(defaultConfigPath))
{
string json = File.ReadAllText(defaultConfigPath);
currentConfig = JsonUtility.FromJson<RL.RLConfig>(json);
Debug.Log("Default configuration loaded successfully");
}
else
{
// Create default configuration
currentConfig = CreateDefaultConfig();
SaveDefaultConfig();
Debug.Log("Default configuration created");
}
}
catch (Exception e)
{
Debug.LogError($"Error loading default config: {e.Message}");
currentConfig = CreateDefaultConfig();
}
}
private RL.RLConfig CreateDefaultConfig()
{
return new RL.RLConfig
{
// Exploration settings
epsilonStart = 1.0f,
epsilonEnd = 0.01f,
epsilonDecay = 0.995f,
epsilonDecaySteps = 1000,
// Learning settings
learningRate = 0.001f,
gamma = 0.99f,
targetUpdateFrequency = 100,
experienceReplaySize = 10000,
batchSize = 32,
// Advanced features
useDoubleDQN = true,
useTargetNetwork = true,
usePrioritizedReplay = true,
prioritizedReplayAlpha = 0.6f,
prioritizedReplayBeta = 0.4f,
prioritizedReplayBetaIncrement = 0.001f,
// Network configuration
networkConfig = new NN.NetworkConfig
{
numInputs = 14,
numHiddens = 64,
numOutputs = 66,
numHiddenLayers = 2,
learningRate = 0.001f,
batchSize = 32,
experienceReplaySize = 10000,
useBatchNormalization = true,
useDropout = true,
dropoutRate = 0.2f,
activationType = NN.ActivationType.ReLU,
weightInitType = NN.WeightInitType.Xavier,
optimizerType = NN.OptimizerType.Adam,
gradientClipThreshold = 1.0f,
adamBeta1 = 0.9f,
adamBeta2 = 0.999f,
adamEpsilon = 1e-8f
}
};
}
#endregion
#region Configuration Management
public void LoadConfig()
{
try
{
if (File.Exists(configPath))
{
string json = File.ReadAllText(configPath);
var loadedConfig = JsonUtility.FromJson<RL.RLConfig>(json);
if (validateConfigOnLoad && ValidateConfig(loadedConfig))
{
currentConfig = loadedConfig;
Debug.Log("Configuration loaded successfully");
}
else
{
Debug.LogWarning("Loaded configuration failed validation, using default");
}
}
else
{
Debug.Log("No configuration file found, using default");
}
}
catch (Exception e)
{
Debug.LogError($"Error loading configuration: {e.Message}");
}
}
public void SaveConfig()
{
try
{
if (validateConfigOnLoad && !ValidateConfig(currentConfig))
{
Debug.LogWarning("Current configuration failed validation, not saving");
return;
}
string json = JsonUtility.ToJson(currentConfig, true);
File.WriteAllText(configPath, json);
Debug.Log($"Configuration saved to: {configPath}");
}
catch (Exception e)
{
Debug.LogError($"Error saving configuration: {e.Message}");
}
}
public void SaveDefaultConfig()
{
try
{
string json = JsonUtility.ToJson(currentConfig, true);
File.WriteAllText(defaultConfigPath, json);
Debug.Log($"Default configuration saved to: {defaultConfigPath}");
}
catch (Exception e)
{
Debug.LogError($"Error saving default configuration: {e.Message}");
}
}
public void ResetToDefault()
{
currentConfig = CreateDefaultConfig();
Debug.Log("Configuration reset to default");
}
#endregion
#region Configuration Validation
public bool ValidateConfig(RL.RLConfig config)
{
if (config == null)
{
Debug.LogError("Configuration is null");
return false;
}
// Validate exploration parameters
if (config.epsilonStart < 0f || config.epsilonStart > 1f)
{
Debug.LogError($"Invalid epsilonStart: {config.epsilonStart}");
return false;
}
if (config.epsilonEnd < 0f || config.epsilonEnd > 1f)
{
Debug.LogError($"Invalid epsilonEnd: {config.epsilonEnd}");
return false;
}
if (config.epsilonDecay <= 0f || config.epsilonDecay >= 1f)
{
Debug.LogError($"Invalid epsilonDecay: {config.epsilonDecay}");
return false;
}
// Validate learning parameters
if (config.learningRate <= 0f || config.learningRate > 1f)
{
Debug.LogError($"Invalid learningRate: {config.learningRate}");
return false;
}
if (config.gamma < 0f || config.gamma > 1f)
{
Debug.LogError($"Invalid gamma: {config.gamma}");
return false;
}
if (config.batchSize <= 0 || config.batchSize > 1000)
{
Debug.LogError($"Invalid batchSize: {config.batchSize}");
return false;
}
if (config.experienceReplaySize <= 0 || config.experienceReplaySize > 1000000)
{
Debug.LogError($"Invalid experienceReplaySize: {config.experienceReplaySize}");
return false;
}
// Validate network configuration
if (!ValidateNetworkConfig(config.networkConfig))
{
return false;
}
return true;
}
private bool ValidateNetworkConfig(NN.NetworkConfig networkConfig)
{
if (networkConfig == null)
{
Debug.LogError("Network configuration is null");
return false;
}
if (networkConfig.numInputs <= 0)
{
Debug.LogError($"Invalid numInputs: {networkConfig.numInputs}");
return false;
}
if (networkConfig.numHiddens <= 0)
{
Debug.LogError($"Invalid numHiddens: {networkConfig.numHiddens}");
return false;
}
if (networkConfig.numOutputs <= 0)
{
Debug.LogError($"Invalid numOutputs: {networkConfig.numOutputs}");
return false;
}
if (networkConfig.numHiddenLayers <= 0 || networkConfig.numHiddenLayers > 10)
{
Debug.LogError($"Invalid numHiddenLayers: {networkConfig.numHiddenLayers}");
return false;
}
if (networkConfig.learningRate <= 0f || networkConfig.learningRate > 1f)
{
Debug.LogError($"Invalid network learningRate: {networkConfig.learningRate}");
return false;
}
if (networkConfig.dropoutRate < 0f || networkConfig.dropoutRate >= 1f)
{
Debug.LogError($"Invalid dropoutRate: {networkConfig.dropoutRate}");
return false;
}
return true;
}
#endregion
#region Configuration Presets
public void LoadPreset(string presetName)
{
switch (presetName.ToLower())
{
case "fast":
LoadFastPreset();
break;
case "stable":
LoadStablePreset();
break;
case "exploration":
LoadExplorationPreset();
break;
case "exploitation":
LoadExploitationPreset();
break;
default:
Debug.LogWarning($"Unknown preset: {presetName}");
break;
}
}
private void LoadFastPreset()
{
currentConfig.learningRate = 0.01f;
currentConfig.batchSize = 64;
currentConfig.targetUpdateFrequency = 50;
currentConfig.networkConfig.learningRate = 0.01f;
currentConfig.networkConfig.batchSize = 64;
Debug.Log("Fast preset loaded");
}
private void LoadStablePreset()
{
currentConfig.learningRate = 0.0001f;
currentConfig.batchSize = 16;
currentConfig.targetUpdateFrequency = 200;
currentConfig.networkConfig.learningRate = 0.0001f;
currentConfig.networkConfig.batchSize = 16;
currentConfig.networkConfig.useBatchNormalization = true;
currentConfig.networkConfig.useDropout = true;
Debug.Log("Stable preset loaded");
}
private void LoadExplorationPreset()
{
currentConfig.epsilonStart = 1.0f;
currentConfig.epsilonEnd = 0.1f;
currentConfig.epsilonDecay = 0.99f;
Debug.Log("Exploration preset loaded");
}
private void LoadExploitationPreset()
{
currentConfig.epsilonStart = 0.1f;
currentConfig.epsilonEnd = 0.001f;
currentConfig.epsilonDecay = 0.999f;
Debug.Log("Exploitation preset loaded");
}
#endregion
#region Public API
public RL.RLConfig GetConfig() => currentConfig;
public void SetConfig(RL.RLConfig config)
{
if (ValidateConfig(config))
{
currentConfig = config;
Debug.Log("Configuration updated");
}
else
{
Debug.LogError("Invalid configuration provided");
}
}
public string GetConfigAsJson() => JsonUtility.ToJson(currentConfig, true);
public void LoadConfigFromJson(string json)
{
try
{
var config = JsonUtility.FromJson<RL.RLConfig>(json);
SetConfig(config);
}
catch (Exception e)
{
Debug.LogError($"Error loading config from JSON: {e.Message}");
}
}
public void ExportConfig(string filePath)
{
try
{
string json = JsonUtility.ToJson(currentConfig, true);
File.WriteAllText(filePath, json);
Debug.Log($"Configuration exported to: {filePath}");
}
catch (Exception e)
{
Debug.LogError($"Error exporting configuration: {e.Message}");
}
}
public void ImportConfig(string filePath)
{
try
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
LoadConfigFromJson(json);
Debug.Log($"Configuration imported from: {filePath}");
}
else
{
Debug.LogError($"Configuration file not found: {filePath}");
}
}
catch (Exception e)
{
Debug.LogError($"Error importing configuration: {e.Message}");
}
}
#endregion
}