Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/19/13 08:46:01 (12 years ago)
Author:
fschoepp
Message:

#1888:

  • Experiments will be saved as JSON elements within the blob store.
  • Added simple model and JSON converters.
  • Backend stores and runs experiments.
  • Updated interfaces to save/read experiments.
  • Added a binding to automatically map incoming JSON ajax requests to experiment models.
  • Added the javascript DatatypeMapper to map parameter inputs to the right html elements and vice versa.
  • Added smartwizard to generate Wizards for creating new experiments (New.cshtml).
File:
1 edited

Legend:

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

    r9215 r9227  
    55using HeuristicLab.Services.Optimization.ControllerService.Model;
    66using Newtonsoft.Json.Linq;
     7using Newtonsoft.Json;
    78
    89namespace HeuristicLab.Services.Optimization.ControllerService.Parsers {
     
    1011
    1112    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) {
    1218      var algorithm = new Algorithm();
    13       var o = JObject.Parse(json);
    14       foreach (JProperty param in o["Algorithm Parameters"]) {
     19
     20      foreach (JProperty param in obj["Algorithm Parameters"]) {
    1521        Parameter parameter = CreateParameter(param);
    1622        algorithm.Parameters.Items.Add(parameter);
    1723      }
    1824
    19       var problemParams = o["Problem Parameters"];
    20       // TODO: Store problem parameters
     25      var problemParams = obj["Problem Parameters"];
    2126      if (problemParams != null) {
    2227        algorithm.Problem = new Problem();
     
    6974        case JTokenType.String:
    7075          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);
    7183        default:
    7284          throw new Exception("Unhandled datatype: " + property.Type);
    7385      }
    7486    }
     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    }
    75156  }
    76157}
Note: See TracChangeset for help on using the changeset viewer.