Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/13 14:40:04 (12 years ago)
Author:
fschoepp
Message:

#1888:

  • Added an Update / GetExperiment... methods to the controller for updating and querying experiments.
  • The AlgorithmConverter class now properly converts from/to JSON format.
  • Integrated backbone js as MVC provider for JavaScript + jquery.
  • Added experiment.model.js + experiment.view.js + experiment.controller.js containing the MVC impl. for the Experiment pages.
  • Added new methods to the ExperimentController usable by the backbone js model implementation.
  • Added the experiment dialog from HL 3.3.7 (variate experiment parameters). It's capable of variating the algorithm parameters.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OaaS/HeuristicLab.Services.Optimization.Controller/Parsers/AlgorithmConverter.cs

    r9227 r9305  
    99namespace HeuristicLab.Services.Optimization.ControllerService.Parsers {
    1010  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) {
     11    #region private methods
     12    private static Parameter CreateParameter(JObject property) {
     13      var name = (string)property["Name"];
     14      var value = property["Value"];
     15      switch (value.Type) {
    6916        case JTokenType.Integer:
    7017        case JTokenType.Float:
    71           return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = property.Name, Value = (double)property.Value } };
     18          return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = name, Value = (double)value } };
    7219        case JTokenType.Boolean:
    73           return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = property.Name, Value = (bool)property.Value } };
     20          return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = name, Value = (bool)value } };
    7421        case JTokenType.String:
    75           return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = property.Name, Value = (string)property.Value } };                 
     22          return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = name, Value = (string)value, Options = (from e in property["Options"] select (string)e).ToArray() } };
    7623        case JTokenType.Array:
    77           var arr = (JArray)token;
     24          var arr = (JArray)value;
    7825          // its a matrix
    7926          if (arr[0].Type == JTokenType.Array) {
    80             return CreateMatrixParameter(property.Name, arr);
     27            return CreateMatrixParameter(name, arr);
    8128          }
    82           return CreateVectorParameter(property.Name, arr);
     29          return CreateVectorParameter(name, arr);
    8330        default:
    8431          throw new Exception("Unhandled datatype: " + property.Type);
     
    8936      double[][] entries = new double[arr.Count][];
    9037      for (int i = 0; i < entries.Length; i++) {
    91         entries[i] = (from d in arr[i] select (double)d).ToArray<double>(); 
     38        entries[i] = (from d in arr[i] select (double)d).ToArray<double>();
    9239      }
    9340      return new Parameter { Type = ParameterType.DecimalMatrix, Value = new DecimalMatrix() { Name = name, Value = entries } };
     
    9946    }
    10047
    101     public static string ConvertJson(Algorithm algorithm) {
    102       return JsonConvert.SerializeObject(algorithm);
    103     }
    104 
    105    
    10648    private class StackEntry {
    10749      public Algorithm Parent { get; set; }
     
    10951    }
    11052
    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) {
     53    private static JArray ConvertParametersToJson(IList<Parameter> parameters) {
     54      var array = new JArray();
     55      foreach (var param in parameters) {
     56        array.Add(JObject.FromObject(param.Value));
     57      }
     58      return array;
     59    }
     60
     61    private struct Entry {
     62      public JObject TargetAlgorithm { get; set; }
     63
     64      public Algorithm SourceAlgorithm { get; set; }
     65    }
     66
     67    private static Algorithm ParseAlgorithm(JToken jsonAlgorithm) {
     68      var algorithm = new Algorithm();
     69
     70      foreach (JObject param in jsonAlgorithm["AlgorithmParameters"]) {
     71        Parameter parameter = CreateParameter(param);
     72        algorithm.Parameters.Items.Add(parameter);
     73      }
     74
     75      var problemParams = jsonAlgorithm["ProblemParameters"];
     76      if (problemParams != null) {
     77        algorithm.Problem = new Problem();
     78        foreach (JObject param in problemParams) {
     79          Parameter parameter = CreateParameter(param);
     80          algorithm.Problem.Parameters.Items.Add(parameter);
     81        }
     82      }
     83      return algorithm;
     84    }
     85    #endregion
     86
     87
     88    #region public methods
     89
     90
     91    public static JObject ConvertExperimentToJson(Experiment experiment) {
     92      var exp = new JObject();
     93      exp["title"] = experiment.Name;
     94      exp["children"] = new JArray();
     95      exp["nodeId"] = experiment.Id;
     96      exp["isExperiment"] = true;
     97
     98      var stack = new Stack<Entry>();
     99      foreach (var algo in experiment.Algorithm)
     100        stack.Push(new Entry() { TargetAlgorithm = exp, SourceAlgorithm = algo });
     101
     102      while (stack.Count > 0) {
     103        var entry = stack.Pop();
     104        var algorithm = ConvertAlgorithmToJson(entry.SourceAlgorithm);
     105        (entry.TargetAlgorithm["children"] as JArray).Add(algorithm);
     106
     107        if (entry.SourceAlgorithm.ChildAlgorithms.Count > 0) {
     108          // push children
     109          foreach (var child in entry.SourceAlgorithm.ChildAlgorithms) {
     110            stack.Push(new Entry() { TargetAlgorithm = algorithm, SourceAlgorithm = child });
     111          }
     112        }
     113
     114      }
     115      return exp;
     116    }
     117
     118    public static JArray ConvertExperimentsToJson(IEnumerable<Experiment> experiments) {
     119      var jarray = new JArray();
     120      foreach (var exp in experiments)
     121        jarray.Add(ConvertExperimentToJson(exp));
     122      return jarray;
     123    }
     124
     125    public static JArray ConvertAlgorithmsToJson(IEnumerable<Algorithm> algorithms) {
     126      var jarray = new JArray();
     127      foreach (var algo in algorithms)
     128        jarray.Add(ConvertAlgorithmToJson(algo));
     129      return jarray;
     130    }
     131
     132
     133    public static JArray ConvertScenariosToJson(IEnumerable<OptimizationScenario> scenarios) {
     134      var jarray = new JArray();
     135      foreach (var scen in scenarios)
     136        jarray.Add(ConvertScenarioToJson(scen));
     137      return jarray;
     138    }
     139
     140    public static JObject ConvertScenarioToJson(OptimizationScenario scenario) {
     141      var exp = new JObject();
     142      exp["title"] = scenario.Id;
     143      exp["children"] = new JArray();
     144      exp["nodeId"] = scenario.Id;
     145      exp["isExperiment"] = false;
     146
     147      var stack = new Stack<Entry>();
     148      var baseAlgorithm = ConvertAlgorithmToJson(scenario.FirstAlgorithm);
     149      exp["data"] = baseAlgorithm["data"];
     150      if (scenario.FirstAlgorithm.ChildAlgorithms.Count > 0) {
     151        foreach (var child in scenario.FirstAlgorithm.ChildAlgorithms)
     152          stack.Push(new Entry() { TargetAlgorithm = exp, SourceAlgorithm = child});
     153      }
     154
     155      while (stack.Count > 0) {
     156        var entry = stack.Pop();
     157        var algorithm = ConvertAlgorithmToJson(entry.SourceAlgorithm);
     158        (entry.TargetAlgorithm["children"] as JArray).Add(algorithm);
     159
     160        if (entry.SourceAlgorithm.ChildAlgorithms.Count > 0) {
     161          // push children
     162          foreach (var child in entry.SourceAlgorithm.ChildAlgorithms) {
     163            stack.Push(new Entry() { TargetAlgorithm = algorithm, SourceAlgorithm = child });
     164          }
     165        }
     166
     167      }
     168      return exp;
     169    }
     170
     171    public static JObject ConvertAlgorithmToJson(Algorithm algorithm) {
     172      var jalgo = new JObject();
     173      jalgo["title"] = algorithm.Name;
     174      jalgo["nodeId"] = algorithm.Id;
     175      var jalgoData = new JObject();
     176      jalgo["data"] = jalgoData;
     177      jalgo["isExperiment"] = algorithm.IsExperiment;
     178      jalgoData["AlgorithmParameters"] = ConvertParametersToJson(algorithm.Parameters.Items);
     179      if (algorithm.Problem != null && algorithm.Problem.Parameters != null)
     180        jalgoData["ProblemParameters"] = ConvertParametersToJson(algorithm.Problem.Parameters.Items);
     181
     182      jalgo["children"] = new JArray();
     183      return jalgo;
     184    }
     185
     186    public static Experiment ConvertJsonToExperiment(string json) {
    120187      var experiment = new Experiment();
    121188      var jsonExperiment = JObject.Parse(json);
    122       experiment.Name = (string)jsonExperiment["name"];
     189      experiment.Name = (string)jsonExperiment["title"];
     190      experiment.Id = (string)jsonExperiment["nodeId"];     
    123191      var stack = new Stack<StackEntry>();
    124192      var root = new Algorithm();
    125193
    126       if ((bool)jsonExperiment["run"]) {
     194      if (jsonExperiment["run"] != null && (bool)jsonExperiment["run"]) {
    127195        experiment.JobDetails = new JobExecutionDetails() {
    128            Group = (string)jsonExperiment["group"],
    129            Repititions = (int)jsonExperiment["repititions"]
     196          Group = (string)jsonExperiment["group"],
     197          Repititions = (int)jsonExperiment["repititions"]
    130198        };
    131199      }
    132200
    133       foreach (var algo in jsonExperiment["algorithms"]["children"]) {
    134         stack.Push(new StackEntry(){ Parent = root, Child = algo });
    135       }
     201      // ignore experiment node, skip to its children
     202      if (jsonExperiment["experiment"] != null)
     203        foreach (var algo in jsonExperiment["experiment"]["children"]) {
     204          stack.Push(new StackEntry() { Parent = root, Child = algo });
     205        }
     206
     207      if (jsonExperiment["children"] != null)
     208        foreach (var algo in jsonExperiment["children"]) {
     209          stack.Push(new StackEntry() { Parent = root, Child = algo });
     210        }
    136211
    137212      while (stack.Count > 0) {
    138213        var entry = stack.Pop();
    139214        var data = entry.Child["data"];
    140         var currentAlgo = data == null ? new Algorithm() : ParseAlgorithm(entry.Child["data"]);
    141         currentAlgo.Name = (string)entry.Child["name"];
     215        var currentAlgo = data == null || !data.HasValues ? new Algorithm() : ParseAlgorithm(entry.Child["data"]);
     216        currentAlgo.Name = (string)entry.Child["title"];
     217        currentAlgo.Id = (string)entry.Child["nodeId"];
     218        currentAlgo.IsExperiment = (bool)entry.Child["isExperiment"];
    142219        entry.Parent.ChildAlgorithms.Add(currentAlgo);
    143220        // push children on stack (inverse order to preserve ordering)
    144221        var cnt = entry.Child["children"].Count();
    145         for (var i=0; i < cnt; i++) {
     222        for (var i = 0; i < cnt; i++) {
    146223          stack.Push(new StackEntry() { Parent = currentAlgo, Child = entry.Child["children"][cnt - 1 - i] });
    147224        }
     
    154231      return experiment;
    155232    }
     233    #endregion
    156234  }
    157235}
Note: See TracChangeset for help on using the changeset viewer.