Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Mockup/MockupScenarioManager.cs @ 9166

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

#1888:

  • Model: OptimizationScenario may be a tree of algorithms (and problems)
  • Model: Renamed InputParameters to ProblemParameters (as they are the parameters of a problem)
  • Model: Added JobExecutionDetails which contain Repetitions + Group (resource to use)
  • ScenarioParser parses the new XML scenario files
  • Website + Model: You are now able to add/remove rows from a table (no JavaScript involved yet)
  • Website + Controller: Added repetitions (enables batch jobs) and group (resource to use) to OaaS which will be used by the controller to schedule the job
  • Website: Updated templates to use new model structure
  • Website + Scenarios: Added the new algorithm Benchmark Algorithm
  • Controller: Added a singleton to make the (Azure/Mockup)-DAL exchangeable
  • Controller: Added mockup classes for DAL + IScenarioManager
  • Website/Result Page: Line Diagrams will be added via JavaScript, crawling their data using AJAX
  • Website: Most configuration parameters can be set in the ServiceDefinition directly
  • Added a mockup for the Membership classes: These can be used if no network connection is available or if other parts of the app shall be tested
  • Scenarios: Updated TSP mappings to new xsd
File size: 9.5 KB
RevLine 
[9166]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 void DispatchScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
134      // does nothign
135    }
136
137    public IList<Model.Job> GetJobs(User user) {
138      return jobs;
139    }
140
141    public Model.Job GetJob(User user, string id) {
142      return (from j in jobs where j.Id == id select j).FirstOrDefault();
143    }
144
145
146    public void DeleteJob(User user, string id) {
147      jobs.RemoveAll(j => j.Id == id);
148    }
149
150    public IList<Model.Run> GetJobResults(User user, string id) {     
151      return jobResults[id];
152    }
153
154    private static string AssemblyDirectory {
155      get {
156        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
157        UriBuilder uri = new UriBuilder(codeBase);
158        string path = Uri.UnescapeDataString(uri.Path);
159        return Path.GetDirectoryName(path);
160      }
161     }
162
163    private IScenarioMapper CompileMapper(string scenarioMapper) {
164       // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe
165      using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) {
166        var cp = new System.CodeDom.Compiler.CompilerParameters() {
167          GenerateInMemory = true
168        };
169
170        var referencedPaths = Directory.GetFiles(AssemblyDirectory, "*.dll");
171        foreach (var loadedAssembly in referencedPaths) {
172          cp.ReferencedAssemblies.Add(loadedAssembly);
173        }
174        cp.ReferencedAssemblies.Add("System.dll");
175        cp.ReferencedAssemblies.Add("System.Core.dll");
176        cp.ReferencedAssemblies.Add("System.Data.dll");
177        cp.ReferencedAssemblies.Add("System.Xml.dll");
178        cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
179
180        var res = csCodeProvider.CompileAssemblyFromSource(
181          cp,
182          scenarioMapper
183        );
184
185        foreach (var error in res.Errors) {
186          Console.WriteLine(error);
187        }
188       
189        var firstMapper = res.CompiledAssembly.GetTypes().Where(p => typeof(IScenarioMapper).IsAssignableFrom(p)).FirstOrDefault();       
190        return Activator.CreateInstance(firstMapper) as IScenarioMapper;
191      }
192    }
193
194    public bool AddScenario(User user, string scenarioName, string scenarioXml, string scenarioMapper) {     
195      // create scenario mapper
196      var mapper = CompileMapper(scenarioMapper);
197      if (mapper == null)
198        return false;
199     
200      // insert into table & blob store
201      var scenDao = dal.CreateScenarioDao();
202      var blobDao = dal.CreateBlobDao();
203     
204      Guid scenarioXmlGuid = Guid.NewGuid();
205      Guid scenarioMapperGuid = Guid.NewGuid();
206      string scenarioXmlId = scenarioName + "_" + scenarioXmlGuid.ToString();
207      string scenarioMapperId = scenarioName + "_" + scenarioMapperGuid.ToString();
208      if (!blobDao.Add(new StringEntry() { Key = scenarioXmlId, Text = scenarioXml }))
209        return false;
210      if (!blobDao.Add(new StringEntry() { Key = scenarioMapperId, Text = scenarioMapper }))
211        return false;
212      if (!scenDao.Add(new ScenarioEntity(scenarioName, scenarioXmlId, scenarioMapperId)))
213        return false;
214
215      // everything stored in the DB -> add mapper to dictionary
216      if (!mappers.ContainsKey(scenarioName)) {
217        lock (lockable) {
218          if (!mappers.ContainsKey(scenarioName))
219            mappers[scenarioName] = mapper;
220        }
221      }
222      return true;
223    }
224
225    public bool DeleteScenario(User user, string scenarioName) {
226      // delete from table & blob store
227      var scenarioDao = dal.CreateScenarioDao();
228      var blobDao = dal.CreateBlobDao();
229
230      var entity = scenarioDao.FindByName(scenarioName);
231      if (entity == null)
232        return false;
233
234      blobDao.DeleteByKey(entity.Mapper);
235      blobDao.DeleteByKey(entity.Scenario);
236      scenarioDao.DeleteByName(scenarioName);           
237      return true;
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.