Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Services.Optimization.Controller/Mockup/MockupScenarioManager.cs @ 16189

Last change on this file since 16189 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.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using HeuristicLab.Optimization;
7using HeuristicLab.Algorithms.GeneticAlgorithm;
8using HeuristicLab.Problems.TravelingSalesman;
9using HeuristicLab;
10using System.Reflection;
11using HeuristicLab.Services.Optimization.ControllerService.Model;
12using HeuristicLab.Core;
13using System.Collections;
14using HeuristicLab.Clients.Hive;
15using System.Threading;
16using HeuristicLab.Data;
17using System.IO;
18using Microsoft.WindowsAzure;
19using Microsoft.WindowsAzure.StorageClient;
20using HeuristicLab.Services.Optimization.ControllerService.Azure;
21using System.Data;
22
23namespace HeuristicLab.Services.Optimization.ControllerService {
24  public class MockupScenarioManager : IScenarioManager {
25    private static List<Model.Job> jobs;
26    private static Dictionary<string, List<Model.Run>> jobResults;
27    private static IDataAccessLayer dal = DataAccessLayerProvider.GetLayer();
28    private Dictionary<string, IScenarioMapper> mappers = new Dictionary<string, IScenarioMapper>();
29    private static object lockable = new object();
30
31    static MockupScenarioManager() {
32      jobs = new List<Model.Job>() {
33        new Model.Job() { DateCreated = DateTime.Now, Id = "1", Name="Webscheduled TSP", Resource="TESTGROUP", State=JobState.Finished },
34        new Model.Job() { DateCreated = DateTime.Now, Id = "2", Name="Webscheduled TSP 2", Resource="TESTGROUP", State=JobState.Calculating }
35      };
36      jobResults = new Dictionary<string, List<Model.Run>>();
37      jobResults["1"] = new List<Model.Run>() {
38        new Model.Run() {
39          Id = "Webscheduled TSP Run 1",
40          Name = "Webscheduled TSP Run 1",
41          Results = new List<Parameter>() {
42            new Parameter() {
43              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
44                Name = "AbsoluteDifferenceBestKnownToBest",
45                Value = 0
46              },
47              Type = ParameterType.Decimal
48            },
49            new Parameter() {
50              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
51                Name = "BestKnownQuality",
52                Value = 1600
53              },             
54              Type = ParameterType.Decimal
55            },
56            new Parameter() {
57              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
58                Name = "BestQuality",
59                Value = 1600
60              },             
61              Type = ParameterType.Decimal
62            },
63            new Parameter() {
64              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
65                Name = "CurrentAverageQuality",
66                Value = 1600
67              },             
68              Type = ParameterType.Decimal
69            },
70            new Parameter() {
71              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
72                Name = "CurrentBestQuality",
73                Value = 1600
74              },             
75              Type = ParameterType.Decimal
76            },
77            new Parameter() {
78              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
79                Name = "CurrentWorstQuality",
80                Value = 1600
81              },             
82              Type = ParameterType.Decimal
83            },
84            new Parameter() {
85              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
86                Name = "Evaluated Solutions",
87                Value = 99100
88              },             
89              Type = ParameterType.Decimal
90            },
91            new Parameter() {
92              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
93                Name = "Generations",
94                Value = 1000
95              },             
96              Type = ParameterType.Decimal
97            },
98            new Parameter() {
99              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){
100                Name = "RelativeDifferenceBestKnownToBest",
101                Value = 0
102              },             
103              Type = ParameterType.Decimal
104            },
105            new Parameter() {
106              Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalMatrix {
107                Name = "Qualities",
108                RowNames = new string[] { "CurrentBestQuality", "CurrentAverageQuality", "CurrentWorstQuality", "BestQuality", "BestKnownQuality" },
109                Value = new double[][] {
110                  // Best Quality
111                  new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600},
112                  // Average
113                  new double[]{6000, 5000, 5650, 3750, 4500, 4050, 3450, 3300, 2550, 2775, 2400},
114                  // Worst Quality
115                  new double[]{8000, 7200, 7350, 5000, 6000, 5400, 4600, 4400, 3400, 3700, 3200},
116                  // Best Quality
117                  new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600},
118                  // Best Known Quality
119                  new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600},
120                }
121              },             
122              Type = ParameterType.Decimal
123            }
124          }
125        }
126      };
127    }
128
129    public MockupScenarioManager() {
130     
131    }
132
133    public string DispatchScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
134      // does nothing
135      return "guid";
136    }
137
138    public IList<Model.Job> GetJobs(User user) {
139      return jobs;
140    }
141
142    public Model.Job GetJob(User user, string id) {
143      return (from j in jobs where j.Id == id select j).FirstOrDefault();
144    }
145
146
147    public bool DeleteJob(User user, string id) {
148      jobs.RemoveAll(j => j.Id == id);
149      return true;
150    }
151
152    public IList<Model.Run> GetJobResults(User user, string id) {     
153      return jobResults[id];
154    }
155
156    private static string AssemblyDirectory {
157      get {
158        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
159        UriBuilder uri = new UriBuilder(codeBase);
160        string path = Uri.UnescapeDataString(uri.Path);
161        return Path.GetDirectoryName(path);
162      }
163     }
164
165    private IScenarioMapper CompileMapper(string scenarioMapper) {
166       // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe
167      using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) {
168        var cp = new System.CodeDom.Compiler.CompilerParameters() {
169          GenerateInMemory = true
170        };
171
172        var referencedPaths = Directory.GetFiles(AssemblyDirectory, "*.dll");
173        foreach (var loadedAssembly in referencedPaths) {
174          cp.ReferencedAssemblies.Add(loadedAssembly);
175        }
176        cp.ReferencedAssemblies.Add("System.dll");
177        cp.ReferencedAssemblies.Add("System.Core.dll");
178        cp.ReferencedAssemblies.Add("System.Data.dll");
179        cp.ReferencedAssemblies.Add("System.Xml.dll");
180        cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
181
182        var res = csCodeProvider.CompileAssemblyFromSource(
183          cp,
184          scenarioMapper
185        );
186
187        foreach (var error in res.Errors) {
188          Console.WriteLine(error);
189        }
190       
191        var firstMapper = res.CompiledAssembly.GetTypes().Where(p => typeof(IScenarioMapper).IsAssignableFrom(p)).FirstOrDefault();       
192        return Activator.CreateInstance(firstMapper) as IScenarioMapper;
193      }
194    }
195
196    public bool AddScenario(User user, string scenarioName, string scenarioXml, string scenarioMapper) {     
197      // create scenario mapper
198      var mapper = CompileMapper(scenarioMapper);
199      if (mapper == null)
200        return false;
201     
202      // insert into table & blob store
203      var scenDao = dal.ScenarioDao;
204      var blobDao = dal.BlobDao;
205     
206      Guid scenarioXmlGuid = Guid.NewGuid();
207      Guid scenarioMapperGuid = Guid.NewGuid();
208      string scenarioXmlId = scenarioName + "_" + scenarioXmlGuid.ToString();
209      string scenarioMapperId = scenarioName + "_" + scenarioMapperGuid.ToString();
210      if (!blobDao.Add(new StringEntry() { Key = scenarioXmlId, Text = scenarioXml }))
211        return false;
212      if (!blobDao.Add(new StringEntry() { Key = scenarioMapperId, Text = scenarioMapper }))
213        return false;
214      if (!scenDao.Add(new ScenarioEntity(scenarioName, scenarioXmlId, scenarioMapperId)))
215        return false;
216
217      // everything stored in the DB -> add mapper to dictionary
218      if (!mappers.ContainsKey(scenarioName)) {
219        lock (lockable) {
220          if (!mappers.ContainsKey(scenarioName))
221            mappers[scenarioName] = mapper;
222        }
223      }
224      return true;
225    }
226
227    public bool DeleteScenario(User user, string scenarioName) {
228      // delete from table & blob store
229      var scenarioDao = dal.ScenarioDao;
230      var blobDao = dal.BlobDao;
231
232      var entity = scenarioDao.FindByName(scenarioName);
233      if (entity == null)
234        return false;
235
236      blobDao.DeleteByKey(entity.Mapper);
237      blobDao.DeleteByKey(entity.Scenario);
238      scenarioDao.DeleteByName(scenarioName);           
239      return true;
240    }
241
242
243    public string SaveExperiment(User user, Model.Experiment experiment) {
244      throw new NotImplementedException();
245    }
246
247
248    public IEnumerable<string> GetExperimentNames(User user) {
249      throw new NotImplementedException();
250    }
251
252
253    public bool DeleteExperiment(User user, string experiment) {
254      throw new NotImplementedException();
255    }
256
257
258    public Model.Task GetTaskData(User u, string jobId, string taskId) {
259      throw new NotImplementedException();
260    }
261
262    public Model.Job GetTasks(User u, string jobId) {
263      throw new NotImplementedException();
264    }
265
266
267    public Model.Experiment GetExperimentByName(User user, string scenario) {
268      throw new NotImplementedException();
269    }
270
271
272    public bool DispatchExperiment(User user, Model.Experiment exp, JobExecutionDetails details) {
273      throw new NotImplementedException();
274    }
275
276
277    string IScenarioManager.SaveExperiment(User user, Model.Experiment experiment) {
278      throw new NotImplementedException();
279    }
280
281    public IEnumerable<Model.Experiment> GetExperiments(User user, bool namesOnly = false) {
282      throw new NotImplementedException();
283    }
284
285    public Model.Experiment GetExperimentById(User user, string nodeId) {
286      throw new NotImplementedException();
287    }
288
289
290    public string GetVisualExtension(string algorithmId) {
291      throw new NotImplementedException();
292    }
293
294
295    public bool AddVisualExtension(string algorithmId, string script) {
296      throw new NotImplementedException();
297    }
298
299    public bool DeleteVisualExtension(string algorithmId) {
300      throw new NotImplementedException();
301    }
302
303
304    public bool ExistsVisualExtension(string algorithmId) {
305      throw new NotImplementedException();
306    }
307  }
308}
Note: See TracBrowser for help on using the repository browser.