Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Mockup/MockupDAL.cs @ 10169

Last change on this file since 10169 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: 10.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using Microsoft.WindowsAzure;
7using Microsoft.WindowsAzure.StorageClient;
8
9namespace HeuristicLab.Services.Optimization.ControllerService.Mockup {
10  public static class AzureConstants {
11    public static readonly string SCENARIO_TABLE = "Scenario";
12    public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
13    public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
14  }
15
16  public class ScenarioDao : IScenarioDao {
17    private static List<ScenarioEntity> scenarios;
18
19    static ScenarioDao() {
20      // TODO: Add tsp scenario to list
21      scenarios = new List<ScenarioEntity>() {
22        new ScenarioEntity("Benchmark Algorithm", "bmx", "bmm"),
23        new ScenarioEntity("Traveling Salesman Problem", "scenario1", "mapper1")
24      };     
25    }
26
27
28    public bool Add(ScenarioEntity entity) {
29      var dbEntity = scenarios.Find(e => e.Scenario == entity.Scenario);
30
31      if (dbEntity != null)
32        return false;
33
34      scenarios.Add(entity);
35      return true;
36    }
37
38    public bool DeleteByName(string scenarioName) {
39      return scenarios.RemoveAll(e => e.RowKey == scenarioName) == 1;
40    }
41
42    public ScenarioEntity FindByName(string scenarioName) {
43      return scenarios.Find(e => e.RowKey == scenarioName);     
44    }
45
46
47    public IEnumerable<ScenarioEntity> GetAllEntities() {
48      return new List<ScenarioEntity>(scenarios);
49    }
50  }
51
52  public class BlobDao : IBlobDao {
53    private static List<StringEntry> entries;
54
55    static BlobDao() {
56      entries = new List<StringEntry>() {
57        new StringEntry() {
58          Key="bmm",
59          Text=@"
60using System;
61using System.Collections.Generic;
62using System.Linq;
63using System.Text;
64using HeuristicLab.Data;
65using HeuristicLab.Services.Optimization.ControllerService.Model;
66using HeuristicLab.Optimization;
67using HeuristicLab.Services.Optimization.ControllerService;
68using HeuristicLab.Algorithms.Benchmarks;
69using HeuristicLab.Parameters;
70using HeuristicLab.PluginInfrastructure;
71
72
73namespace HeuristicLab.Mappers {
74  public class BenchmarkScenarioMapper : IScenarioMapper {
75    public void MapScenario(OptimizationScenario scenario, out IAlgorithm algorithm) {
76      var alg = scenario.FirstAlgorithm;
77      var bm = new BenchmarkAlgorithm();
78      var bmFullName = HLMapper.GetStringValue(alg.Parameters.FindByName(""Benchmark""));
79      bm.BenchmarkParameter.Value = bm.Benchmark = bm.BenchmarkParameter.ValidValues.First(b => b.GetType().FullName == bmFullName);     
80      ((ValueParameter<IntValue>)bm.Parameters[""ChunkSize""]).Value = HLMapper.ConvertToIntValue(alg.Parameters.FindByName(""ChunkSize""));
81      ((ValueParameter<DoubleValue>)bm.Parameters[""TimeLimit""]).Value = HLMapper.ConvertToDoubleValue(alg.Parameters.FindByName(""TimeLimit""));
82
83      algorithm = bm;
84    }
85  }
86}
87"
88        },
89        new StringEntry() {
90           Key="bmx",
91           Text=@"<?xml version='1.0'?>
92<scenario xmlns=""urn:scenario-schema"">
93  <name>Benchmark Algorithm</name>
94  <algorithm mapper=""HeuristicLab.Mappers.BenchmarkScenarioMapper"">
95    <parameters>
96    <param type=""Type"" name=""Benchmark"">
97      <choice name=""HeuristicLab.Algorithms.Benchmarks.Dhrystone"" selected=""true"" />
98      <choice name=""HeuristicLab.Algorithms.Benchmarks.Linpack"" />
99      <choice name=""HeuristicLab.Algorithms.Benchmarks.Whetstone""/>
100    </param>   
101    <param type=""Decimal"" name=""ChunkSize"" value=""0""/>
102    <param type=""Decimal"" name=""TimeLimit"" value=""0""/>
103  </parameters>
104  </algorithm> 
105</scenario>"
106        },
107        new StringEntry() {
108          Key = "scenario1",
109          Text = @"<?xml version='1.0'?>
110<scenario xmlns=""urn:scenario-schema"">
111  <name>Traveling Salesman Problem</name>
112  <algorithm mapper=""HeuristicLab.Mappers.TSPScenarioMapper"">
113    <parameters>
114    <param type=""Type"" name=""CrossoverParameter"">
115      <choice name=""OrderCrossover2""/>
116      <choice name=""CosaCrossover"" selected=""true""/>
117    </param>   
118    <param type=""Integer"" name=""Elites"" value=""1""/>
119    <param type=""Integer"" name=""MaximumGenerations"" value=""1000""/>
120    <param type=""Percent"" name=""MutationProbability"" value=""5""/>
121    <param type=""Type"" name=""Mutator"">
122      <choice name=""InversionManipulator"" selected=""true""/>
123      <choice name=""ScrambleManipulator""/>
124    </param>
125    <param type=""Integer"" name=""PopulationSize"" value=""100""/>
126    <param type=""Integer"" name=""Seed"" value=""100""/>
127    <param type=""Type"" name=""Selector"">
128      <choice name=""ProportionalSelector"" selected=""true""/>
129      <choice name=""BestSelector""/>
130    </param>
131    <param type=""Boolean"" name=""SetSeedRandomly"" value=""true""/>
132    </parameters>
133  <problem>
134    <parameters>
135      <param type=""Decimal"" name=""BestKnownQuality"" value=""6110.0""/>
136      <param type=""DecimalVector"" name=""BestKnownSolution"">
137        <value v1=""0""/>
138        <value v1=""40""/>
139        <value v1=""38""/>
140        <value v1=""60""/>
141      </param>
142      <param type=""DecimalMatrix"" name=""Coordinates"">
143        <value v1=""334.5909"" v2=""161.7809""/>
144        <value v1=""503.5909"" v2=""172.7809""/>
145        <value v1=""400.5909"" v2=""300.7809""/>
146        <value v1=""700.5909"" v2=""700.7809""/>
147      </param>
148      <param type=""Type"" name=""EvaluatorParameter"">
149        <choice name=""HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"" />
150        <choice name=""HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"" selected=""true""/>
151        <choice name=""HeuristicLab.Problems.TravelingSalesman.TSPEuclideanPathEvaluator""/>
152      </param>   
153      <param type=""Boolean"" name=""UseDistanceMatrix"" value=""true""/>
154    </parameters>
155  </problem>
156  </algorithm>
157</scenario>"
158        },
159        new StringEntry() {
160          Key = "mapper1",
161          Text = @"using System;
162using System.Collections.Generic;
163using System.Linq;
164using System.Text;
165using HeuristicLab.Data;
166using HeuristicLab.Services.Optimization.ControllerService.Model;
167using HeuristicLab.Optimization;
168using HeuristicLab.Services.Optimization.ControllerService;
169using HeuristicLab.Algorithms.GeneticAlgorithm;
170using HeuristicLab.Problems.TravelingSalesman;
171namespace HeuristicLab.Mappers {
172  public class TSPScenarioMapper : IScenarioMapper {
173    public void MapScenario(OptimizationScenario scenario, out IAlgorithm algorithm) {
174      Console.WriteLine(""Mapping scenario!"");
175      var ga = new GeneticAlgorithm();
176      var problem = new TravelingSalesmanProblem();
177      ga.Problem = problem;
178      algorithm = ga;
179
180      problem.BestKnownQuality = HLMapper.ConvertToDoubleValue(scenario.FirstAlgorithm.Problem.Parameters.FindByName(""BestKnownQuality""));
181      problem.BestKnownSolution = HLMapper.ConvertToPermutation(scenario.FirstAlgorithm.Problem.Parameters.FindByName(""BestKnownSolution""));
182      problem.Coordinates = HLMapper.ConvertToDoubleMatrix(scenario.FirstAlgorithm.Problem.Parameters.FindByName(""Coordinates""));
183      var evalParam = HLMapper.GetStringValue(scenario.FirstAlgorithm.Problem.Parameters.FindByName(""EvaluatorParameter""));
184      if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"") {
185        problem.EvaluatorParameter.Value = new TSPRoundedEuclideanPathEvaluator();
186      }
187      else if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"") {
188        problem.EvaluatorParameter.Value = new TSPGeoPathEvaluator();
189      }
190      else {
191        problem.EvaluatorParameter.Value = new TSPEuclideanPathEvaluator();
192      }
193      problem.UseDistanceMatrix = HLMapper.ConvertToBoolValue(scenario.FirstAlgorithm.Problem.Parameters.FindByName(""UseDistanceMatrix""));
194      Console.WriteLine(""Mapping algorithm..."");
195      ga.Mutator = HLMapper.FindInItemSet<HeuristicLab.Optimization.IManipulator>(ga.MutatorParameter.ValidValues, scenario.FirstAlgorithm.Parameters.FindByName(""Mutator""));
196      ga.CrossoverParameter.Value = HLMapper.FindOperator<HeuristicLab.Optimization.ICrossover>(problem, scenario.FirstAlgorithm.Parameters.FindByName(""CrossoverParameter""));
197      ga.Elites = HLMapper.ConvertToIntValue(scenario.FirstAlgorithm.Parameters.FindByName(""Elites""));
198      ga.MaximumGenerations = HLMapper.ConvertToIntValue(scenario.FirstAlgorithm.Parameters.FindByName(""MaximumGenerations""));
199      ga.MutationProbability = HLMapper.ConvertToPercentValue(scenario.FirstAlgorithm.Parameters.FindByName(""MutationProbability""));
200      ga.PopulationSize = HLMapper.ConvertToIntValue(scenario.FirstAlgorithm.Parameters.FindByName(""PopulationSize""));
201      ga.Seed = HLMapper.ConvertToIntValue(scenario.FirstAlgorithm.Parameters.FindByName(""Seed""));
202      ga.Selector = HLMapper.FindInItemSet<HeuristicLab.Optimization.ISelector>(ga.SelectorParameter.ValidValues, scenario.FirstAlgorithm.Parameters.FindByName(""Selector""));
203      ga.SetSeedRandomly = HLMapper.ConvertToBoolValue(scenario.FirstAlgorithm.Parameters.FindByName(""SetSeedRandomly""));
204    }
205  }
206}"
207        }
208      };
209    }
210
211    public bool Add(StringEntry entry) {
212      entries.RemoveAll(e => e.Key == entry.Key);
213      entries.Add(entry);     
214      return true;
215    }
216
217    public bool DeleteByKey(string entryKey) {
218      return entries.RemoveAll(e => e.Key == entryKey) == 1;     
219    }
220
221    public StringEntry FindByKey(string entryKey) {
222      return entries.Find(e => e.Key == entryKey);     
223    }
224  }
225
226  public class MockupDataAccessLayer : IDataAccessLayer {
227    private IScenarioDao scenarioDao;
228    private IBlobDao blobDao;
229
230    public IScenarioDao ScenarioDao {
231      get {
232        if (scenarioDao == null) {
233          scenarioDao = new ScenarioDao();
234        }
235        return scenarioDao;
236      }
237    }
238
239    public IBlobDao BlobDao {
240      get {
241        if (blobDao == null) {
242          blobDao = new BlobDao();
243        }
244        return blobDao;
245      }
246    }
247
248    private IExperimentDao experimentDao;
249
250    public IExperimentDao ExperimentDao {
251      get {         
252        return null;
253      }     
254    }
255
256
257    public IVisualExtensionDao VisualExtensionDao {
258      get { throw new NotImplementedException(); }
259    }
260
261
262    public IJobDao JobDao {
263      get { throw new NotImplementedException(); }
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.