Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.ControllerService.Tests/Program.cs @ 12394

Last change on this file since 12394 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: 12.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using HeuristicLab.Problems.TravelingSalesman;
7using System.Reflection;
8using System.IO;
9using Microsoft.Scripting.Hosting;
10using IronPython.Hosting;
11using HeuristicLab.Algorithms.GeneticAlgorithm;
12using HeuristicLab.Persistence.Core;
13using HeuristicLab.Core;
14using HeuristicLab.Optimization;
15
16namespace HeuristicLab.Services.Optimization.ControllerService.Tests {
17  class Program {
18    static void Main(string[] args) {
19      Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);   
20      TestScenarioParser();
21      //TestCodeDOM();
22      //TestPython();
23      //TestOtis();
24      //TestJobResults();
25    }
26
27    static void TestJobResults() {
28      IScenarioManager manager = new HiveScenarioManager();
29      Console.WriteLine("Press key to continue");
30      Console.ReadKey();
31      IList<Model.Run> results = manager.GetJobResults(new Model.User() { Password = "fschoeppl", Username = "fschoeppl" }, "cfd0cd8d-76a5-4bf6-bfcb-53fbfe3efe24");
32      foreach (var run in results) {
33        Console.WriteLine(run);
34      }
35    }
36
37    static void TestStorable() {
38      TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
39      GeneticAlgorithm algo = new GeneticAlgorithm();
40      algo.Problem = tsp;
41
42     
43    }
44
45    static void TestCodeDOM() {
46      ScenarioParser parser = new ScenarioParser();
47      var scen = parser.GetByName("Traveling Salesman Problem");
48      TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
49      GeneticAlgorithm algo = new GeneticAlgorithm();
50      algo.Problem = tsp;
51
52      // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe
53      using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) {               
54
55        var cp = new System.CodeDom.Compiler.CompilerParameters() {
56            GenerateInMemory = true
57          };
58
59        var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
60        var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
61
62        var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
63        foreach (var loadedAssembly in referencedPaths) {
64          cp.ReferencedAssemblies.Add(loadedAssembly);         
65        }
66        cp.ReferencedAssemblies.Add("System.dll");
67
68        tsp.BestKnownQuality = HLMapper.ConvertToDoubleValue(scen.FirstAlgorithm.Problem.Parameters.FindByName("BestKnownQuality"));
69       
70        var res = csCodeProvider.CompileAssemblyFromSource(
71         cp
72         ,
73          @"using System;
74            using System.Collections.Generic;
75            using System.Linq;
76            using System.Text;
77            using HeuristicLab.Data;
78            using HeuristicLab.Services.Optimization.ControllerService.Model;
79            using HeuristicLab.Optimization;
80            using HeuristicLab.Services.Optimization.ControllerService;
81            using HeuristicLab.Algorithms.GeneticAlgorithm;
82            using HeuristicLab.Problems.TravelingSalesman;
83            public class TSPScenarioMapper : IScenarioMapper {
84              public void MapScenario(OptimizationScenario scenario, IAlgorithm algorithm) {
85                Console.WriteLine(""Mapping scenario!"");
86                var ga = algorithm as GeneticAlgorithm;
87                var problem = algorithm.Problem as TravelingSalesmanProblem;
88               
89                problem.BestKnownQuality = HLMapper.ConvertToDoubleValue(scenario.InputParameters.FindByName(""BestKnownQuality""));
90                problem.BestKnownSolution = HLMapper.ConvertToPermutation(scenario.InputParameters.FindByName(""BestKnownSolution""));
91                problem.Coordinates = HLMapper.ConvertToDoubleMatrix(scenario.InputParameters.FindByName(""Coordinates""));
92                var evalParam = HLMapper.GetStringValue(scenario.InputParameters.FindByName(""EvaluatorParameter""));
93                if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"") {
94                  problem.EvaluatorParameter.Value = new TSPRoundedEuclideanPathEvaluator();
95                }
96                else if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"") {
97                  problem.EvaluatorParameter.Value = new TSPGeoPathEvaluator();
98                }
99                else {
100                  problem.EvaluatorParameter.Value = new TSPEuclideanPathEvaluator();
101                }
102                problem.UseDistanceMatrix = HLMapper.ConvertToBoolValue(scenario.InputParameters.FindByName(""UseDistanceMatrix""));
103                Console.WriteLine(""Mapping algorithm..."");
104                ga.Mutator = HLMapper.FindInItemSet<HeuristicLab.Optimization.IManipulator>(ga.MutatorParameter.ValidValues, scenario.AlgorithmParameters.FindByName(""Mutator""));
105                ga.CrossoverParameter.Value = HLMapper.FindOperator<HeuristicLab.Optimization.ICrossover>(problem, scenario.AlgorithmParameters.FindByName(""CrossoverParameter""));
106                ga.Elites = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""Elites""));
107                ga.MaximumGenerations = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""MaximumGenerations""));
108                ga.MutationProbability = HLMapper.ConvertToPercentValue(scenario.AlgorithmParameters.FindByName(""MutationProbability""));               
109                ga.PopulationSize = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""PopulationSize""));
110                ga.Seed = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""Seed""));
111                ga.Selector = HLMapper.FindInItemSet<HeuristicLab.Optimization.ISelector>(ga.SelectorParameter.ValidValues, scenario.AlgorithmParameters.FindByName(""Selector""));
112                ga.SetSeedRandomly = HLMapper.ConvertToBoolValue(scenario.AlgorithmParameters.FindByName(""SetSeedRandomly""));
113              }
114            }"
115        );       
116
117        foreach (var error in res.Errors) {
118          Console.WriteLine(error);
119        }       
120       
121        var type = res.CompiledAssembly.GetType("TSPScenarioMapper");
122        var mapper = Activator.CreateInstance(type) as IScenarioMapper;
123        //mapper.MapScenario(scen, algo);
124      }     
125    }
126
127    static void TestPython() {
128      ScenarioParser parser = new ScenarioParser();
129      var scen = parser.GetByName("Traveling Salesman Problem");
130      //TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
131      //GeneticAlgorithm algo = new GeneticAlgorithm();
132      //algo.Problem = tsp;
133      var rt = Python.CreateRuntime();
134      // workaround to load all referenced assemblies into the current AppDomain
135      var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
136      var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
137
138      var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
139      var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
140      toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
141
142      foreach (var assm in AppDomain.CurrentDomain.GetAssemblies()) {               
143        rt.LoadAssembly(assm); 
144      }
145     
146      /*var scope = rt.CreateScope();
147      scope.SetVariable("source", scen);
148      scope.SetVariable("target", tsp);*/
149      var engine = rt.GetEngine("py");
150      char op = ' ';
151      while (op != 'q') {
152        try {
153          TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
154          GeneticAlgorithm algo = new GeneticAlgorithm();
155          algo.Problem = tsp;
156          Console.WriteLine("\n\n===============================================================");
157          var script = engine.CreateScriptSourceFromFile(System.Configuration.ConfigurationManager.AppSettings["scenarioPath"] + @"\mapping.py");         
158          var scope = engine.CreateScope();
159          scope.SetVariable("source", scen);
160          scope.SetVariable("problem", tsp);
161          scope.SetVariable("algorithm", algo);         
162          script.Execute(scope);                   
163        }
164        catch (Exception e) {
165          Console.WriteLine(e);
166        }
167        Console.WriteLine("Hit 'q' to quit...");
168        op = Console.ReadKey().KeyChar;
169      }
170
171      /*dynamic script = rt.UseFile("C:/temp/scenarios/mapping.py");
172      script.SetVariable("source", scen);
173      script.SetVariable("target", tsp);
174     
175
176      script.mapObjects(scen, tsp);*/
177     
178    }
179
180    static void TestOtis() {
181      Console.WriteLine();
182      Otis.Configuration cfg = new Otis.Configuration();
183      Model.DecimalValue dv = new Model.DecimalValue();
184      dv.Value = 100;
185      Console.WriteLine(dv);
186      Console.WriteLine(typeof(HeuristicLab.Data.DoubleValue).AssemblyQualifiedName);
187      Console.WriteLine(typeof(System.Double).AssemblyQualifiedName);
188
189      // workaround to load all referenced assemblies into the current AppDomain
190      var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
191      var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
192
193      var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
194      var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
195      toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
196     
197      foreach (var assm in AppDomain.CurrentDomain.GetAssemblies()) {
198        //cfg.AddAssembly(assm);
199        cfg.AddAssemblyReference(assm);       
200      }
201      //cfg.AddAssemblyReference("HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec");
202      //cfg.AddAssemblyReference("HeuristicLab.Data-3.3.dll");
203      cfg.AddXmlFile(System.Configuration.ConfigurationManager.AppSettings["scenarioPath"] + @"\mapping.tsp.xml");
204
205      Otis.IAssembler<TravelingSalesmanProblem,Model.OptimizationScenario> asm = cfg.GetAssembler<TravelingSalesmanProblem,Model.OptimizationScenario>();
206      ScenarioParser parser = new ScenarioParser();
207      var scen = parser.GetByName("Traveling Salesman Problem");
208      TravelingSalesmanProblem tsp = new TravelingSalesmanProblem();
209      //tsp.BestKnownQuality = new Data.DoubleValue();
210      asm.Assemble(ref tsp, ref scen);
211      Console.WriteLine(tsp.BestKnownQuality);     
212    }
213
214    static void TestMapping() {
215    }
216
217    // template for mapping Model.OptimizationScenario & e.g. TSP
218    static void TestScenarioParser() {
219      ScenarioParser parser = new ScenarioParser();
220      //var scen = parser.GetByName("Traveling Salesman Problem");
221      var scen = parser.GetByName("Benchmark Algorithm");
222
223      var mapper = HiveScenarioManager.CompileMapper(
224@"
225using System;
226using System.Collections.Generic;
227using System.Linq;
228using System.Text;
229using HeuristicLab.Data;
230using HeuristicLab.Services.Optimization.ControllerService.Model;
231using HeuristicLab.Optimization;
232using HeuristicLab.Services.Optimization.ControllerService;
233using HeuristicLab.Algorithms.Benchmarks;
234using HeuristicLab.Parameters;
235using HeuristicLab.PluginInfrastructure;
236
237
238namespace HeuristicLab.Mappers {
239  public class BenchmarkScenarioMapper : IScenarioMapper {
240    public void MapScenario(OptimizationScenario scenario, out IAlgorithm algorithm) {
241      var alg = scenario.FirstAlgorithm;
242      var bm = new BenchmarkAlgorithm();
243      bm.Benchmark = (IBenchmark) Activator.CreateInstance((from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark))
244                        where t.Name == HLMapper.GetStringValue(alg.Parameters.FindByName(""Benchmark""))
245                       select t).FirstOrDefault());
246      ((ValueParameter<IntValue>)bm.Parameters[""ChunkSize""]).Value = HLMapper.ConvertToIntValue(alg.Parameters.FindByName(""ChunkSize""));
247      ((ValueParameter<DoubleValue>)bm.Parameters[""TimeLimit""]).Value = HLMapper.ConvertToDoubleValue(alg.Parameters.FindByName(""TimeLimit""));
248
249      algorithm = bm;
250    }
251  }
252}
253
254");
255      IAlgorithm algo;
256      //mapper = new BenchmarkScenarioMapper();
257      mapper.MapScenario(scen, out algo);
258      // proceed with mapping between scen and tsp
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.