using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Services.Optimization.ControllerService.Interfaces; using HeuristicLab.Problems.TravelingSalesman; using System.Reflection; using System.IO; using Microsoft.Scripting.Hosting; using IronPython.Hosting; using HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Persistence.Core; using HeuristicLab.Core; using HeuristicLab.Optimization; namespace HeuristicLab.Services.Optimization.ControllerService.Tests { class Program { static void Main(string[] args) { Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); TestScenarioParser(); //TestCodeDOM(); //TestPython(); //TestOtis(); //TestJobResults(); } static void TestJobResults() { IScenarioManager manager = new HiveScenarioManager(); Console.WriteLine("Press key to continue"); Console.ReadKey(); IList results = manager.GetJobResults(new Model.User() { Password = "fschoeppl", Username = "fschoeppl" }, "cfd0cd8d-76a5-4bf6-bfcb-53fbfe3efe24"); foreach (var run in results) { Console.WriteLine(run); } } static void TestStorable() { TravelingSalesmanProblem tsp = new TravelingSalesmanProblem(); GeneticAlgorithm algo = new GeneticAlgorithm(); algo.Problem = tsp; } static void TestCodeDOM() { ScenarioParser parser = new ScenarioParser(); var scen = parser.GetByName("Traveling Salesman Problem"); TravelingSalesmanProblem tsp = new TravelingSalesmanProblem(); GeneticAlgorithm algo = new GeneticAlgorithm(); algo.Problem = tsp; // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) { var cp = new System.CodeDom.Compiler.CompilerParameters() { GenerateInMemory = true }; var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray(); var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); foreach (var loadedAssembly in referencedPaths) { cp.ReferencedAssemblies.Add(loadedAssembly); } cp.ReferencedAssemblies.Add("System.dll"); tsp.BestKnownQuality = HLMapper.ConvertToDoubleValue(scen.FirstAlgorithm.Problem.Parameters.FindByName("BestKnownQuality")); var res = csCodeProvider.CompileAssemblyFromSource( cp , @"using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Data; using HeuristicLab.Services.Optimization.ControllerService.Model; using HeuristicLab.Optimization; using HeuristicLab.Services.Optimization.ControllerService; using HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Problems.TravelingSalesman; public class TSPScenarioMapper : IScenarioMapper { public void MapScenario(OptimizationScenario scenario, IAlgorithm algorithm) { Console.WriteLine(""Mapping scenario!""); var ga = algorithm as GeneticAlgorithm; var problem = algorithm.Problem as TravelingSalesmanProblem; problem.BestKnownQuality = HLMapper.ConvertToDoubleValue(scenario.InputParameters.FindByName(""BestKnownQuality"")); problem.BestKnownSolution = HLMapper.ConvertToPermutation(scenario.InputParameters.FindByName(""BestKnownSolution"")); problem.Coordinates = HLMapper.ConvertToDoubleMatrix(scenario.InputParameters.FindByName(""Coordinates"")); var evalParam = HLMapper.GetStringValue(scenario.InputParameters.FindByName(""EvaluatorParameter"")); if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"") { problem.EvaluatorParameter.Value = new TSPRoundedEuclideanPathEvaluator(); } else if (evalParam == ""HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"") { problem.EvaluatorParameter.Value = new TSPGeoPathEvaluator(); } else { problem.EvaluatorParameter.Value = new TSPEuclideanPathEvaluator(); } problem.UseDistanceMatrix = HLMapper.ConvertToBoolValue(scenario.InputParameters.FindByName(""UseDistanceMatrix"")); Console.WriteLine(""Mapping algorithm...""); ga.Mutator = HLMapper.FindInItemSet(ga.MutatorParameter.ValidValues, scenario.AlgorithmParameters.FindByName(""Mutator"")); ga.CrossoverParameter.Value = HLMapper.FindOperator(problem, scenario.AlgorithmParameters.FindByName(""CrossoverParameter"")); ga.Elites = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""Elites"")); ga.MaximumGenerations = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""MaximumGenerations"")); ga.MutationProbability = HLMapper.ConvertToPercentValue(scenario.AlgorithmParameters.FindByName(""MutationProbability"")); ga.PopulationSize = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""PopulationSize"")); ga.Seed = HLMapper.ConvertToIntValue(scenario.AlgorithmParameters.FindByName(""Seed"")); ga.Selector = HLMapper.FindInItemSet(ga.SelectorParameter.ValidValues, scenario.AlgorithmParameters.FindByName(""Selector"")); ga.SetSeedRandomly = HLMapper.ConvertToBoolValue(scenario.AlgorithmParameters.FindByName(""SetSeedRandomly"")); } }" ); foreach (var error in res.Errors) { Console.WriteLine(error); } var type = res.CompiledAssembly.GetType("TSPScenarioMapper"); var mapper = Activator.CreateInstance(type) as IScenarioMapper; //mapper.MapScenario(scen, algo); } } static void TestPython() { ScenarioParser parser = new ScenarioParser(); var scen = parser.GetByName("Traveling Salesman Problem"); //TravelingSalesmanProblem tsp = new TravelingSalesmanProblem(); //GeneticAlgorithm algo = new GeneticAlgorithm(); //algo.Problem = tsp; var rt = Python.CreateRuntime(); // workaround to load all referenced assemblies into the current AppDomain var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray(); var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList(); toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)))); foreach (var assm in AppDomain.CurrentDomain.GetAssemblies()) { rt.LoadAssembly(assm); } /*var scope = rt.CreateScope(); scope.SetVariable("source", scen); scope.SetVariable("target", tsp);*/ var engine = rt.GetEngine("py"); char op = ' '; while (op != 'q') { try { TravelingSalesmanProblem tsp = new TravelingSalesmanProblem(); GeneticAlgorithm algo = new GeneticAlgorithm(); algo.Problem = tsp; Console.WriteLine("\n\n==============================================================="); var script = engine.CreateScriptSourceFromFile(System.Configuration.ConfigurationManager.AppSettings["scenarioPath"] + @"\mapping.py"); var scope = engine.CreateScope(); scope.SetVariable("source", scen); scope.SetVariable("problem", tsp); scope.SetVariable("algorithm", algo); script.Execute(scope); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Hit 'q' to quit..."); op = Console.ReadKey().KeyChar; } /*dynamic script = rt.UseFile("C:/temp/scenarios/mapping.py"); script.SetVariable("source", scen); script.SetVariable("target", tsp); script.mapObjects(scen, tsp);*/ } static void TestOtis() { Console.WriteLine(); Otis.Configuration cfg = new Otis.Configuration(); Model.DecimalValue dv = new Model.DecimalValue(); dv.Value = 100; Console.WriteLine(dv); Console.WriteLine(typeof(HeuristicLab.Data.DoubleValue).AssemblyQualifiedName); Console.WriteLine(typeof(System.Double).AssemblyQualifiedName); // workaround to load all referenced assemblies into the current AppDomain var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray(); var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList(); toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)))); foreach (var assm in AppDomain.CurrentDomain.GetAssemblies()) { //cfg.AddAssembly(assm); cfg.AddAssemblyReference(assm); } //cfg.AddAssemblyReference("HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec"); //cfg.AddAssemblyReference("HeuristicLab.Data-3.3.dll"); cfg.AddXmlFile(System.Configuration.ConfigurationManager.AppSettings["scenarioPath"] + @"\mapping.tsp.xml"); Otis.IAssembler asm = cfg.GetAssembler(); ScenarioParser parser = new ScenarioParser(); var scen = parser.GetByName("Traveling Salesman Problem"); TravelingSalesmanProblem tsp = new TravelingSalesmanProblem(); //tsp.BestKnownQuality = new Data.DoubleValue(); asm.Assemble(ref tsp, ref scen); Console.WriteLine(tsp.BestKnownQuality); } static void TestMapping() { } // template for mapping Model.OptimizationScenario & e.g. TSP static void TestScenarioParser() { ScenarioParser parser = new ScenarioParser(); //var scen = parser.GetByName("Traveling Salesman Problem"); var scen = parser.GetByName("Benchmark Algorithm"); var mapper = HiveScenarioManager.CompileMapper( @" using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Data; using HeuristicLab.Services.Optimization.ControllerService.Model; using HeuristicLab.Optimization; using HeuristicLab.Services.Optimization.ControllerService; using HeuristicLab.Algorithms.Benchmarks; using HeuristicLab.Parameters; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Mappers { public class BenchmarkScenarioMapper : IScenarioMapper { public void MapScenario(OptimizationScenario scenario, out IAlgorithm algorithm) { var alg = scenario.FirstAlgorithm; var bm = new BenchmarkAlgorithm(); bm.Benchmark = (IBenchmark) Activator.CreateInstance((from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark)) where t.Name == HLMapper.GetStringValue(alg.Parameters.FindByName(""Benchmark"")) select t).FirstOrDefault()); ((ValueParameter)bm.Parameters[""ChunkSize""]).Value = HLMapper.ConvertToIntValue(alg.Parameters.FindByName(""ChunkSize"")); ((ValueParameter)bm.Parameters[""TimeLimit""]).Value = HLMapper.ConvertToDoubleValue(alg.Parameters.FindByName(""TimeLimit"")); algorithm = bm; } } } "); IAlgorithm algo; //mapper = new BenchmarkScenarioMapper(); mapper.MapScenario(scen, out algo); // proceed with mapping between scen and tsp } } }