1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
6 | using Newtonsoft.Json.Linq;
|
---|
7 | using Newtonsoft.Json;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Services.Optimization.ControllerService.Parsers {
|
---|
10 | public static class AlgorithmConverter {
|
---|
11 |
|
---|
12 | public static Algorithm Convert(string json) {
|
---|
13 | var o = JObject.Parse(json);
|
---|
14 | return ParseAlgorithm(o);
|
---|
15 | }
|
---|
16 |
|
---|
17 | private static Algorithm ParseAlgorithm(JToken obj) {
|
---|
18 | var algorithm = new Algorithm();
|
---|
19 |
|
---|
20 | foreach (JProperty param in obj["Algorithm Parameters"]) {
|
---|
21 | Parameter parameter = CreateParameter(param);
|
---|
22 | algorithm.Parameters.Items.Add(parameter);
|
---|
23 | }
|
---|
24 |
|
---|
25 | var problemParams = obj["Problem Parameters"];
|
---|
26 | if (problemParams != null) {
|
---|
27 | algorithm.Problem = new Problem();
|
---|
28 | foreach (JProperty param in problemParams) {
|
---|
29 | Parameter parameter = CreateParameter(param);
|
---|
30 | algorithm.Problem.Parameters.Items.Add(parameter);
|
---|
31 | }
|
---|
32 | }
|
---|
33 | return algorithm;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static Algorithm ConvertAppend(string json, Algorithm algorithm) {
|
---|
37 | var o = JObject.Parse(json);
|
---|
38 | foreach (JProperty param in o["Algorithm Parameters"]) {
|
---|
39 | Parameter parameter = CreateParameter(param);
|
---|
40 | var oldParameter = (from p in algorithm.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault();
|
---|
41 | if (oldParameter != null) {
|
---|
42 | oldParameter.Value = parameter.Value;
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | algorithm.Parameters.Items.Add(parameter);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | var problemParams = o["Problem Parameters"];
|
---|
50 | if (problemParams != null) {
|
---|
51 | algorithm.Problem = new Problem();
|
---|
52 | foreach (JProperty param in problemParams) {
|
---|
53 | Parameter parameter = CreateParameter(param);
|
---|
54 | var oldParameter = (from p in algorithm.Problem.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault();
|
---|
55 | if (oldParameter != null) {
|
---|
56 | oldParameter.Value = parameter.Value;
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | algorithm.Problem.Parameters.Items.Add(parameter);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return algorithm;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private static Parameter CreateParameter(JProperty property) {
|
---|
67 | JToken token = property.Value;
|
---|
68 | switch (token.Type) {
|
---|
69 | case JTokenType.Integer:
|
---|
70 | case JTokenType.Float:
|
---|
71 | return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = property.Name, Value = (double)property.Value } };
|
---|
72 | case JTokenType.Boolean:
|
---|
73 | return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = property.Name, Value = (bool)property.Value } };
|
---|
74 | case JTokenType.String:
|
---|
75 | return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = property.Name, Value = (string)property.Value } };
|
---|
76 | case JTokenType.Array:
|
---|
77 | var arr = (JArray)token;
|
---|
78 | // its a matrix
|
---|
79 | if (arr[0].Type == JTokenType.Array) {
|
---|
80 | return CreateMatrixParameter(property.Name, arr);
|
---|
81 | }
|
---|
82 | return CreateVectorParameter(property.Name, arr);
|
---|
83 | default:
|
---|
84 | throw new Exception("Unhandled datatype: " + property.Type);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private static Parameter CreateMatrixParameter(string name, JArray arr) {
|
---|
89 | double[][] entries = new double[arr.Count][];
|
---|
90 | for (int i = 0; i < entries.Length; i++) {
|
---|
91 | entries[i] = (from d in arr[i] select (double)d).ToArray<double>();
|
---|
92 | }
|
---|
93 | return new Parameter { Type = ParameterType.DecimalMatrix, Value = new DecimalMatrix() { Name = name, Value = entries } };
|
---|
94 | }
|
---|
95 |
|
---|
96 | private static Parameter CreateVectorParameter(string name, JArray arr) {
|
---|
97 | double[] entries = (from d in arr select (double)d).ToArray<double>();
|
---|
98 | return new Parameter { Type = ParameterType.DecimalVector, Value = new DecimalVector() { Name = name, Value = entries } };
|
---|
99 | }
|
---|
100 |
|
---|
101 | public static string ConvertJson(Algorithm algorithm) {
|
---|
102 | return JsonConvert.SerializeObject(algorithm);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | private class StackEntry {
|
---|
107 | public Algorithm Parent { get; set; }
|
---|
108 | public JToken Child { get; set; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public static Experiment ConvertExperimentSimple(string json) {
|
---|
112 | return JsonConvert.DeserializeObject<Experiment>(json, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
|
---|
113 | }
|
---|
114 |
|
---|
115 | public static string ConvertJson(Experiment experiment) {
|
---|
116 | return JsonConvert.SerializeObject(experiment, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static Experiment ConvertExperiment(string json) {
|
---|
120 | var experiment = new Experiment();
|
---|
121 | var jsonExperiment = JObject.Parse(json);
|
---|
122 | experiment.Name = (string)jsonExperiment["name"];
|
---|
123 | var stack = new Stack<StackEntry>();
|
---|
124 | var root = new Algorithm();
|
---|
125 |
|
---|
126 | if ((bool)jsonExperiment["run"]) {
|
---|
127 | experiment.JobDetails = new JobExecutionDetails() {
|
---|
128 | Group = (string)jsonExperiment["group"],
|
---|
129 | Repititions = (int)jsonExperiment["repititions"]
|
---|
130 | };
|
---|
131 | }
|
---|
132 |
|
---|
133 | foreach (var algo in jsonExperiment["algorithms"]["children"]) {
|
---|
134 | stack.Push(new StackEntry(){ Parent = root, Child = algo });
|
---|
135 | }
|
---|
136 |
|
---|
137 | while (stack.Count > 0) {
|
---|
138 | var entry = stack.Pop();
|
---|
139 | var data = entry.Child["data"];
|
---|
140 | var currentAlgo = data == null ? new Algorithm() : ParseAlgorithm(entry.Child["data"]);
|
---|
141 | currentAlgo.Name = (string)entry.Child["name"];
|
---|
142 | entry.Parent.ChildAlgorithms.Add(currentAlgo);
|
---|
143 | // push children on stack (inverse order to preserve ordering)
|
---|
144 | var cnt = entry.Child["children"].Count();
|
---|
145 | for (var i=0; i < cnt; i++) {
|
---|
146 | stack.Push(new StackEntry() { Parent = currentAlgo, Child = entry.Child["children"][cnt - 1 - i] });
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | foreach (var algo in root.ChildAlgorithms) {
|
---|
151 | experiment.Algorithm.Add(algo);
|
---|
152 | }
|
---|
153 |
|
---|
154 | return experiment;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|