1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
8 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
9 | using HeuristicLab;
|
---|
10 | using System.Reflection;
|
---|
11 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
12 | using HeuristicLab.Core;
|
---|
13 | using System.Collections;
|
---|
14 | using HeuristicLab.Clients.Hive;
|
---|
15 | using System.Threading;
|
---|
16 | using HeuristicLab.Data;
|
---|
17 | using System.IO;
|
---|
18 | using Microsoft.WindowsAzure;
|
---|
19 | using Microsoft.WindowsAzure.StorageClient;
|
---|
20 | using HeuristicLab.Services.Optimization.ControllerService.Azure;
|
---|
21 | using System.Data;
|
---|
22 |
|
---|
23 | namespace 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 | }
|
---|