Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14778 was 9395, checked in by fschoepp, 12 years ago

#1888:

  • Added visual extensions (dynamic JavaScript) which will be used to render additional result parameters specific to scenarios (e. g. create a graphical representation of a TSP).
  • Added relationship between jobs and experiments (otherwise, it's not possible to get the job's experiment).
  • Updated Admin page to allow removal/addition of visual extensions.
  • Added back-end logic to store/retrieve/delete visual extensions.
  • Added visual extension functionality to the JavaScript views/controllers (job.*.js).
  • Added tsp.js which is a visual extension for the "Genetic Algorithm - TSP" scenario. It adds a graphical representation of the TSP (just like the C# version does) to the results.
File size: 11.7 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    [DataMember]
45    public string[] RowNames { get; set; }
46
47    public void CopyRow(int index) {
48      var newData = new double[Value.Length + 1][];
49      var copyRow = new double[Value[index].Length];
50      for (var i = 0; i < copyRow.Length; i++) {
51        copyRow[i] = Value[index][i];
52      }
53      for (var i = 0; i < newData.Length; i++) {
54        if (i < index) {
55          newData[i] = Value[i];
56        }
57        else if (i == index) {
58          newData[i] = copyRow;
59        }
60        else if (i > index) {
61          newData[i] = Value[i - 1];
62        }
63      }
64      Value = newData;
65    }
66
67    public void DeleteRow(int index) {
68      if (Value.Length == 1)
69        return;
70
71      var newData = new double[Value.Length - 1][];
72      for (var i = 0; i < Value.Length; i++) {
73        if (i < index) {
74          newData[i] = Value[i];
75        }
76        else if (i > index) {
77          newData[i - 1] = Value[i];
78        }
79      }
80      Value = newData;
81    }
82
83    public override bool TrySetFromString(string input) {
84      var splitted = input.Split(':');
85      int i, j;
86      if (splitted.Length != 3)
87        return false;
88
89      if (!int.TryParse(splitted[0], out i))
90        return false;
91
92      if (!int.TryParse(splitted[1], out j))
93        return false;
94
95      double value;
96      if (!double.TryParse(splitted[2], out value))
97        return false;
98      // very, very buggy
99      /*if (i >= Value.Length || j >= Value[0].Length ) {
100        double[][] valueArr = new double[i >= Value.GetLength(0) ? i : Value.GetLength(0)][];       
101        for (int k = 0; k < Value.Length; k++) {
102          valueArr[k] = new double[j >= Value.GetLength(1) ? j : Value.GetLength(1)];
103          for (int l = 0; l < Value[k].Length; l++) {
104            valueArr[k][l] = Value[k][l];
105          }
106        }
107        Value = valueArr;
108      }*/
109
110      Value[i][j] = value;
111      return true;
112    }
113  }
114
115  [DataContract]
116  public class DecimalVector : Value {
117    [DataMember]
118    public double[] Value { get; set; }
119
120    public override bool TrySetFromString(string input) {
121      var splitted = input.Split(':');     
122      int index;     
123      if (splitted.Length != 2)
124        return false;
125
126      if (!int.TryParse(splitted[0], out index))
127        return false;
128
129      double value;
130      if (!double.TryParse(splitted[1], out value))
131        return false;
132
133      if (index >= Value.Length) {
134        double[] valueArr = new double[index + 1];
135        Array.Copy(Value, valueArr, Value.Length);
136        Value = valueArr;
137      }
138
139      Value[index] = value;
140      return true;
141    }
142  }
143
144  [DataContract]
145  public class TypeValue : Value {
146    [DataMember]
147    public string Value { get; set; }
148
149    [DataMember]
150    public string[] Options { get; set; }
151
152    public override bool TrySetFromString(string input) {
153      if (Options.Contains(input)) {
154        Value = input;
155        return true;
156      }
157      return false;
158    }
159  }
160
161  [DataContract]
162  public class DecimalValue : Value {
163    [DataMember]
164    public double Value { get; set; }
165
166    public override bool TrySetFromString(string input) {
167      double result;
168      if (double.TryParse(input, out result)) {
169        Value = result;
170        return true;
171      }
172      return false;
173    }
174  }
175
176  [DataContract]
177  public class BoolValue : Value {
178    [DataMember]
179    public bool Value { get; set; }
180
181    public override bool TrySetFromString(string input) {
182      bool result;
183      if (input.Contains(","))
184        input = input.Split(',')[0].Trim();
185      if (bool.TryParse(input, out result)) {
186        Value = result;
187        return true;
188      }
189      return false;
190    }
191  }
192
193  [DataContract]
194  public class StringValue : Value {
195    [DataMember]
196    public string Value { get; set; }
197
198    public override bool TrySetFromString(string input) {
199      Value = input;
200      return true;
201    }
202  }
203
204  [DataContract]
205  [KnownType(typeof(BoolValue))]
206  [KnownType(typeof(DecimalValue))]
207  [KnownType(typeof(TypeValue))]
208  [KnownType(typeof(DecimalVector))]
209  [KnownType(typeof(DecimalMatrix))]
210  [KnownType(typeof(StringValue))]
211  public class Parameter {       
212    // some kind of value type
213    [DataMember]
214    public ParameterType Type { get; set; }
215   
216    [DataMember]
217    public Value Value { get; set; }   
218    /*
219    //[DataMember]
220    public object[] Choices { get; set; }*/
221  }
222
223  [DataContract]
224  public class ProblemParameters {
225    private IList<Parameter> items = new List<Parameter>();
226
227    [DataMember]
228    public IList<Parameter> Items {
229      get { return items; }
230      set { items = value; }
231    }
232
233    public Parameter FindByName(string name) {
234      foreach (var param in items) {
235        if (param.Value.Name == name) {         
236          return param;
237        }
238      }
239      return null;
240    }
241  }
242
243  [DataContract]
244  public class AlgorithmParameters {
245    private IList<Parameter> items = new List<Parameter>();
246
247    [DataMember]
248    public IList<Parameter> Items {
249      get { return items; }
250      set { items = value; }
251    }
252
253    public Parameter FindByName(string name) {
254      foreach (var param in items) {
255        if (param.Value.Name == name) {
256          return param;
257        }
258      }
259      return null;
260    }
261  }
262
263  [DataContract]
264  public class Problem {
265    public Problem() { Parameters = new ProblemParameters(); }
266
267    [DataMember]
268    public ProblemParameters Parameters { get; set; }
269
270    [DataMember]
271    public string Class { get; set; }
272  }
273
274  [DataContract]
275  public class Algorithm {
276    public Algorithm() { Parameters = new AlgorithmParameters(); ChildAlgorithms = new List<Algorithm>(); }
277
278    [DataMember]
279    public string Id { get; set; }
280
281    [DataMember]
282    public AlgorithmParameters Parameters { get; set; }
283
284    [DataMember]
285    public Problem Problem{ get; set; }
286
287    [DataMember]
288    public IList<Algorithm> ChildAlgorithms { get; set; }
289
290    [DataMember]
291    public string Class { get; set; }
292
293    [DataMember]
294    public string Mapper { get; set; }
295
296    [DataMember]
297    public string Name { get; set; }
298
299    [DataMember]
300    public bool IsExperiment { get; set; }
301  }
302
303  [DataContract]
304  public class OptimizationScenario {
305    [DataMember]
306    public string Id { get; set; }
307
308    public OptimizationScenario() { Algorithm = new List<Algorithm>(); }
309
310    [DataMember]
311    public IList<Algorithm> Algorithm { get; set; }
312
313    public Algorithm FirstAlgorithm { get { return Algorithm.FirstOrDefault(); } } 
314  }
315
316  [DataContract]
317  public class Experiment {
318    [DataMember]
319    public string Name { get; set; }
320
321    [DataMember]
322    public IList<Algorithm> Algorithm { get; set; }
323
324    [DataMember]
325    public string Id { get; set; }
326
327    public Experiment() {
328      Algorithm = new List<Algorithm>();
329    }
330
331    [DataMember]
332    public JobExecutionDetails JobDetails { get; set; }
333  }
334
335  [DataContract]
336  public enum JobState {
337    [EnumMember]
338    Waiting,
339    [EnumMember]
340    Calculating,
341    [EnumMember]
342    Aborted,
343    [EnumMember]
344    Failed,
345    [EnumMember]
346    Finished
347  }
348
349  [DataContract]
350  public class JobExecutionDetails {
351    [DataMember]
352    public int Repititions { get; set; }
353
354    [DataMember]
355    public string JobTitle { get; set; }
356
357    [DataMember]
358    public string Group { get; set; }
359  }
360
361  [DataContract]
362  public class Property {
363    [DataMember]
364    public string Key { get; set; }
365    [DataMember]
366    public string Value { get; set; }
367  }
368 
369  [DataContract]
370  public class General {
371    [DataMember]
372    public string Name { get { return GetValue("Name"); } set { SetValue("Name", value); } }
373    [DataMember]
374    public string Id { get { return GetValue("Id"); } set { SetValue("Id", value); } }
375    [DataMember]
376    public string Priority { get { return GetValue("Priority"); } set { SetValue("Priority", value); } }
377    [DataMember]
378    public string LastChanged { get { return GetValue("LastChanged"); } set { SetValue("LastChanged", value); } }
379
380   
381    private IList<Property> properties = new List<Property>();
382    [DataMember]
383    public IList<Property> Properties
384    {
385      get {
386        if (properties == null)
387          properties = new List<Property>();
388        return properties;
389      }
390      set { properties = value;}
391    }
392
393    private string GetValue(string key) {     
394      return (from p in Properties where p.Key == key select p.Value).FirstOrDefault();
395    }
396
397    private void SetValue(string key, string value) {
398      var prop = (from p in Properties where p.Key == key select p).FirstOrDefault();
399      if (prop == null) {
400        prop = new Property();
401        Properties.Add(prop);
402      }
403      prop.Key = key;
404      prop.Value = value;
405    }
406  }
407
408  [DataContract]
409  public class TaskState {
410    [DataMember]
411    public string State { get; set; }
412    [DataMember]
413    public string ExecutionTime { get; set; }
414    [DataMember]
415    public string DateCreated { get; set; }
416    [DataMember]
417    public string DateFinished { get; set; }
418  }
419
420  [DataContract]
421  public class Task {
422    private General general = new General();
423    [DataMember]
424    public General General {
425      get { return general; }
426      set { general = value; }
427    }
428
429    private IList<Task> tasks = new List<Task>();
430    [DataMember]
431    public IList<Task> Children {
432      get { return tasks; }
433      set { tasks = value; }
434    }
435   
436    private TaskState state = new TaskState();
437
438    [DataMember]
439    public TaskState State {
440      get { return state; }
441      set { state = value; }
442    }
443  }
444
445  [DataContract]
446  public class Job {
447    [DataMember]
448    public string Id { get; set; }
449
450    [DataMember]
451    public JobState State { get; set; }
452
453    [DataMember]
454    public string Name { get; set; }
455
456    [DataMember]
457    public string Resource { get; set; }
458
459    [DataMember]
460    public DateTime DateCreated { get; set; }
461
462    private IList<Task> tasks = new List<Task>();
463
464    [DataMember]
465    public IList<Task> Tasks {
466      get { return tasks; }
467      set { tasks = value; }
468    }
469  }
470
471  [DataContract]
472  public class VisualExtension {
473    [DataMember]
474    public string ScenarioId { get; set; }
475
476    [DataMember]
477    public string ScenarioJs { get; set; }
478  }
479
480  [DataContract]
481  public class Run {
482    [DataMember]
483    public string Id { get; set; }
484
485    [DataMember]
486    public string Name { get; set; }
487
488    [DataMember]
489    public IList<Parameter> Results { get; set; }
490
491    [DataMember]
492    public IList<Parameter> InputParameters { get; set; }
493
494    [DataMember]
495    public string AlgorithmName { get; set; }
496  }
497
498  [DataContract]
499  public class User {
500    [DataMember]
501    public string Username { get; set; }
502    [DataMember]
503    public string Password { get; set; }
504  }
505}
Note: See TracBrowser for help on using the repository browser.