Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Model/ControllerModel.cs @ 9035

Last change on this file since 9035 was 8817, checked in by fschoepp, 12 years ago

#1888:

  • Added a parser for independent scenarios (using the model of the optimization backend)
  • Optimization scenario sample can be found in mappings folder of the web project.
  • Added IScenarioMapper interface which provides functionality to map from the optimization data model to a backend model (e.g. Heuristic Lab data model)
  • Implementations of IScenarioMapper have to be provided as C# code (strings) which will be compiled by using a CSharpCodeProvider. Once compiled, the implementations of the IScenarioMapper are being cached within the platform for further usage.
  • Fixed a bug in web template DecimalMatrix (using i instead of j)
  • Added missing thumprint of localhost certificate to the optimization web project (ServiceConfiguration.Local.cscfg / ServiceConfiguration.Cloud.cscfg)
  • Test project now provides following test cases: Mapping types using IronPython and mapping types using Otis
File size: 6.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Runtime.Serialization;
6using System.Globalization;
7using System.Xml;
8
9namespace HeuristicLab.Services.Optimization.ControllerService.Model {
10  [DataContract]
11  public enum ParameterType {
12    [EnumMember]
13    Decimal,
14    [EnumMember]
15    Integer,
16    [EnumMember]
17    Boolean,
18    [EnumMember]
19    Type,
20    [EnumMember]
21    DecimalMatrix,
22    [EnumMember]
23    DecimalVector,
24    [EnumMember]
25    Percent,
26    [EnumMember]
27    String
28  }
29
30  [DataContract]
31  public abstract class Value {
32    [DataMember]
33    public string Name { get; set; }
34
35    public abstract bool TrySetFromString(string input);
36  }
37
38
39  [DataContract]
40  public class DecimalMatrix : Value {
41    [DataMember]
42    public double[][] Value { get; set; }
43
44    public override bool TrySetFromString(string input) {
45      var splitted = input.Split(':');
46      int i, j;
47      if (splitted.Length != 3)
48        return false;
49
50      if (!int.TryParse(splitted[0], out i))
51        return false;
52
53      if (!int.TryParse(splitted[1], out j))
54        return false;
55
56      double value;
57      if (!double.TryParse(splitted[2], out value))
58        return false;
59      // very, very buggy
60      if (i >= Value.Length || j >= Value[0].Length ) {
61        double[][] valueArr = new double[i >= Value.GetLength(0) ? i : Value.GetLength(0)][];       
62        for (int k = 0; k < Value.Length; k++) {
63          valueArr[k] = new double[j >= Value.GetLength(1) ? j : Value.GetLength(1)];
64          for (int l = 0; l < Value[k].Length; l++) {
65            valueArr[k][l] = Value[k][l];
66          }
67        }
68        Value = valueArr;
69      }
70
71      Value[i][j] = value;
72      return true;
73    }
74  }
75
76  [DataContract]
77  public class DecimalVector : Value {
78    [DataMember]
79    public double[] Value { get; set; }
80
81    public override bool TrySetFromString(string input) {
82      var splitted = input.Split(':');     
83      int index;     
84      if (splitted.Length != 2)
85        return false;
86
87      if (!int.TryParse(splitted[0], out index))
88        return false;
89
90      double value;
91      if (!double.TryParse(splitted[1], out value))
92        return false;
93
94      if (index >= Value.Length) {
95        double[] valueArr = new double[index + 1];
96        Array.Copy(Value, valueArr, Value.Length);
97        Value = valueArr;
98      }
99
100      Value[index] = value;
101      return true;
102    }
103  }
104
105  [DataContract]
106  public class TypeValue : Value {
107    [DataMember]
108    public string Value { get; set; }
109
110    [DataMember]
111    public string[] Options { get; set; }
112
113    public override bool TrySetFromString(string input) {
114      if (Options.Contains(input)) {
115        Value = input;
116        return true;
117      }
118      return false;
119    }
120  }
121
122  [DataContract]
123  public class DecimalValue : Value {
124    [DataMember]
125    public double Value { get; set; }
126
127    public override bool TrySetFromString(string input) {
128      double result;
129      if (double.TryParse(input, out result)) {
130        Value = result;
131        return true;
132      }
133      return false;
134    }
135  }
136
137  [DataContract]
138  public class BoolValue : Value {
139    [DataMember]
140    public bool Value { get; set; }
141
142    public override bool TrySetFromString(string input) {
143      bool result;
144      if (input.Contains(","))
145        input = input.Split(',')[0].Trim();
146      if (bool.TryParse(input, out result)) {
147        Value = result;
148        return true;
149      }
150      return false;
151    }
152  }
153
154  [DataContract]
155  public class StringValue : Value {
156    [DataMember]
157    public string Value { get; set; }
158
159    public override bool TrySetFromString(string input) {
160      Value = input;
161      return true;
162    }
163  }
164
165  [DataContract]
166  [KnownType(typeof(BoolValue))]
167  [KnownType(typeof(DecimalValue))]
168  [KnownType(typeof(TypeValue))]
169  [KnownType(typeof(DecimalVector))]
170  [KnownType(typeof(DecimalMatrix))]
171  [KnownType(typeof(StringValue))]
172  public class Parameter {       
173    // some kind of value type
174    [DataMember]
175    public ParameterType Type { get; set; }
176   
177    [DataMember]
178    public Value Value { get; set; }   
179    /*
180    //[DataMember]
181    public object[] Choices { get; set; }*/
182  }
183
184  [DataContract]
185  public class InputParameters {
186    private IList<Parameter> items = new List<Parameter>();
187
188    [DataMember]
189    public IList<Parameter> Items {
190      get { return items; }
191      set { items = value; }
192    }
193
194    public Parameter FindByName(string name) {
195      foreach (var param in items) {
196        if (param.Value.Name == name) {         
197          return param;
198        }
199      }
200      return null;
201    }
202  }
203
204  [DataContract]
205  public class AlgorithmParameters {
206    private IList<Parameter> items = new List<Parameter>();
207
208    [DataMember]
209    public IList<Parameter> Items {
210      get { return items; }
211      set { items = value; }
212    }
213
214    public Parameter FindByName(string name) {
215      foreach (var param in items) {
216        if (param.Value.Name == name) {
217          return param;
218        }
219      }
220      return null;
221    }
222  }
223
224  [DataContract]
225  public class OptimizationScenario {
226    [DataMember]
227    public string Name { get; set; }
228
229    [DataMember]
230    public string ProblemType { get; set; }
231
232    [DataMember]
233    public string AlgorithmType { get; set; }
234
235    [DataMember]
236    public string MapperType { get; set; }
237
238    private InputParameters inputParams = new InputParameters();
239
240    [DataMember]
241    public InputParameters InputParameters {
242      get { return inputParams; }
243      set { inputParams = value; }
244    }
245
246
247    private AlgorithmParameters algorithmParameters = new AlgorithmParameters();
248
249    [DataMember]
250    public AlgorithmParameters AlgorithmParameters {
251      get { return algorithmParameters; }
252      set { algorithmParameters = value; }
253    }
254  }
255
256  [DataContract]
257  public enum JobState {
258    [EnumMember]
259    Waiting,
260    [EnumMember]
261    Calculating,
262    [EnumMember]
263    Aborted,
264    [EnumMember]
265    Failed,
266    [EnumMember]
267    Finished
268  }
269
270  [DataContract]
271  public class Job {
272    [DataMember]
273    public string Id { get; set; }
274
275    [DataMember]
276    public JobState State { get; set; }
277
278    [DataMember]
279    public string Name { get; set; }
280
281    [DataMember]
282    public string Resource { get; set; }   
283  }
284
285  [DataContract]
286  public class Run {
287    [DataMember]
288    public string Id { get; set; }
289
290    [DataMember]
291    public string Name { get; set; }
292
293    [DataMember]
294    public IList<Parameter> Results { get; set; }
295  }
296
297  [DataContract]
298  public class User {
299    [DataMember]
300    public string Username { get; set; }
301    [DataMember]
302    public string Password { get; set; }
303  }
304}
Note: See TracBrowser for help on using the repository browser.