1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.JsonInterface {
|
---|
10 | public class ExperimentConverter : BaseConverter {
|
---|
11 | public override int Priority => 10;
|
---|
12 | public override Type ConvertableType => HEAL.Attic.Mapper.StaticCache.GetType(new Guid("A8A4536B-54C1-4A17-AB58-A6006F7F394B"));
|
---|
13 |
|
---|
14 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
15 | dynamic experiment = (dynamic)value;
|
---|
16 | EmptyJsonItem experimentJI = new EmptyJsonItem() {
|
---|
17 | Name = experiment.Name,
|
---|
18 | Description = value.ItemDescription
|
---|
19 | };
|
---|
20 |
|
---|
21 | OptimizerList optimizers = experiment.Optimizers;
|
---|
22 | foreach(var o in optimizers) {
|
---|
23 | var optimizerJI = root.Extract(o, root);
|
---|
24 | if (!(optimizerJI is UnsupportedJsonItem)) {
|
---|
25 | experimentJI.AddChildren(optimizerJI);
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | int worker = (int)experiment.NumberOfWorkers;
|
---|
30 | experimentJI.AddChildren(new IntJsonItem() {
|
---|
31 | Name = "NumberOfWorkers",
|
---|
32 | Description = "The number of workers for this experiment.",
|
---|
33 | Value = worker,
|
---|
34 | Minimum = 1,
|
---|
35 | Maximum = int.MaxValue
|
---|
36 | });
|
---|
37 |
|
---|
38 | return experimentJI;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
42 | dynamic experiment = (dynamic)item;
|
---|
43 | OptimizerList optimizers = experiment.Optimizers;
|
---|
44 | if (data.Children != null) {
|
---|
45 | foreach (var i in data.Children) {
|
---|
46 | if (i.Path.EndsWith("NumberOfWorkers") && i is IntJsonItem worker) {
|
---|
47 | experiment.NumberOfWorkers = worker.Value;
|
---|
48 | } else {
|
---|
49 | var optimizer = optimizers.Find(o => i.Path.EndsWith(o.Name));
|
---|
50 | root.Inject(optimizer, i, root);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|