source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ExperimentConverter.cs @ 17843

Last change on this file since 17843 was 17843, checked in by dpiringe, 7 weeks ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Optimization;
8
9namespace HeuristicLab.JsonInterface {
10  public class ExperimentConverter : BaseConverter {
11    public override int Priority => 10;
12
13    public override bool CanConvertType(Type t) =>
14      HEAL.Attic.Mapper.StaticCache.GetType(new Guid("A8A4536B-54C1-4A17-AB58-A6006F7F394B")).IsAssignableFrom(t);
15
16    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
17      dynamic experiment = (dynamic)value;
18      EmptyJsonItem experimentJI = new EmptyJsonItem() {
19        Name = experiment.Name,
20        Description = value.ItemDescription
21      };
22
23      OptimizerList optimizers = experiment.Optimizers;
24      foreach(var o in optimizers) {
25        var optimizerJI = root.Extract(o, root);
26        if (!(optimizerJI is UnsupportedJsonItem)) {
27          experimentJI.AddChildren(optimizerJI);
28        }
29      }
30
31      int worker = (int)experiment.NumberOfWorkers;
32      experimentJI.AddChildren(new IntJsonItem() {
33        Name = "NumberOfWorkers",
34        Description = "The number of workers for this experiment.",
35        Value = worker,
36        Minimum = 1,
37        Maximum = int.MaxValue
38      });
39
40      return experimentJI;
41    }
42
43    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
44      dynamic experiment = (dynamic)item;
45      OptimizerList optimizers = experiment.Optimizers;
46      if (data.Children != null) {
47        foreach (var i in data.Children) {
48          if (i.Path.EndsWith("NumberOfWorkers") && i is IntJsonItem worker) {
49            experiment.NumberOfWorkers = worker.Value;
50          } else {
51            var optimizer = optimizers.Find(o => i.Path.EndsWith(o.Name));
52            root.Inject(optimizer, i, root);
53          }
54        }
55      }
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.