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 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 bool SaveExperiment(User user, Model.Experiment experiment) {
|
---|
244 | throw new NotImplementedException();
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | public IEnumerable<string> GetExperiments(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 | }
|
---|