Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/PlaceholderControllerService.cs @ 10915

Last change on this file since 10915 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: 9.9 KB
RevLine 
[9363]1using System.Collections.Generic;
[8384]2using System.Linq;
3using System.ServiceModel;
[9363]4using HeuristicLab.Services.Optimization.ControllerService.General;
[8506]5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
[8817]6using HeuristicLab.Services.Optimization.ControllerService.Model;
[8384]7
8namespace HeuristicLab.Services.Optimization.ControllerService {
9  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
10  public class PlaceholderControllerService : IControllerService {
[9363]11
[8384]12    private IList<Model.OptimizationScenario> scenarios;
13
[8817]14    private ScenarioParser parser = new ScenarioParser();
15
[8506]16    private IScenarioManager hiveManager;
17
[8384]18    public PlaceholderControllerService() {
[8817]19      /*var tsp = new Model.OptimizationScenario() { Name = "Traveling Salesman Problem" };
[8506]20      hiveManager = new HiveScenarioManager();
21      tsp.InputParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Decimal, Value = new Model.DecimalValue() { Name = "BestKnownQuality", Value = 6110.0d } });
22      tsp.InputParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.DecimalVector
23      ,Value =
24        new Model.DecimalVector() {
25          Name = "BestKnownSolution",
26          Value = new double[]{
27            0,40,38//,116,111,114,27,61,104,127,15,44,4,10,75,108,60,128,123,63,68,85,87,25,6,96,69,106,126,103,42,33,16,30,26,18,99,14,28,23,115,94,78,86,11,80,102,76,93,88,109,97,67,62,47,24,112,31,35,83,118,110,122,100,81,56,8,55,64,51,74,73,98,72,91,37,105,52,119,57,48,71,90,5,101,9,13,66,12,95,121,54,59,50,41,43,92,36,21,46,39,22,32,20,125,120,77,65,84,124,89,58,29,82,2,113,107,7,17,45,79,117,19,3,34,53,1,49,129,70
28      }}});
29      tsp.InputParameters.Items.Add(new Model.Parameter() {       
30        Type = Model.ParameterType.DecimalMatrix,
31        Value =
32          new Model.DecimalMatrix() {
33            Name = "Coordinates",
34            Value = Utility.ToJagged(new double[,] { { 334.5909245845, 161.7809319139 }, { 334.5909245845, 161.7809319139 }, { 503.8741827107, 172.8741151168 } })
35          }
36      });
37      tsp.InputParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Type, Value = new Model.TypeValue() { Name = "EvaluatorParameter", Value = "HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator", Options = new string[] { "HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator", "HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator", "HeuristicLab.Problems.TravelingSalesman.TSPEuclideanPathEvaluator" } } });
[8384]38      //tsp.InputParameters.Add(new Model.Parameter() { Name = "SolutionCreator" });
[8506]39      tsp.InputParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Boolean, Value = new Model.BoolValue() { Name = "UseDistanceMatrix", Value = true } });
[8384]40
[8506]41      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Type, Value = new Model.TypeValue() { Name = "CrossoverParameter", Value = "OrderCrossover2", Options = new string[] { "OrderCrossover2", "CosaCrossover" } } });
42      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Integer, Value = new Model.DecimalValue() { Name = "Elites", Value = 1 } });
43      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Integer, Value = new Model.DecimalValue() { Name = "MaximumGenerations", Value = 1000 } });//, Value = 1000});
44      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Percent, Value = new Model.DecimalValue() { Name = "MutationProbability", Value = 5 } });//, Value = 5});
45      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Type, Value = new Model.TypeValue() { Name = "Mutator", Value = "InversionManipulator", Options = new string[] { "InversionManipulator", "ScrambleManipulator" } } });
46      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Integer, Value = new Model.DecimalValue() { Name = "PopulationSize", Value = 100 } });//, Value = 100 });
47      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Integer, Value = new Model.DecimalValue() { Name = "Seed", Value = 100 } });//, Value = 100 });
48      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Type, Value = new Model.TypeValue() { Name = "Selector", Value = "ProportionalSelector", Options = new string[] { "ProportionalSelector", "BestSelector" } } });
49      tsp.AlgorithmParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Boolean, Value = new Model.BoolValue() { Name = "SetSeedRandomly", Value = true } });//, Value = true });
[9363]50      this.scenarios = new List<Model.OptimizationScenario>() { tsp };*/
[8506]51      //hiveManager.DispatchScenario(tsp);
[9166]52      //hiveManager = new MockupScenarioManager();
[8817]53      hiveManager = new HiveScenarioManager();
54      scenarios = parser.Scenarios;
55      //hiveManager.DispatchScenario(new User() { Username = "fschoeppl", Password = "fschoeppl" }, scenarios[0]);
[8384]56    }
57
58    public IEnumerable<Model.OptimizationScenario> GetOptimizationScenarios() {
[9363]59      return scenarios;
[8384]60    }
61
62
63    public Model.OptimizationScenario GetOptimizationScenarioByName(string name) {
[8958]64      var scen = (from e in scenarios
[9166]65                  where e.Id == name
[9363]66                  select e).FirstOrDefault();
[8958]67      return scen;
[8384]68    }
[8506]69
[9215]70    public bool ScheduleOptimizationScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
[9166]71      hiveManager.DispatchScenario(user, scenario, details);
[9215]72      return true;
[8506]73    }
74
[8545]75    public IEnumerable<Model.Job> GetJobs(Model.User user) {
76      return hiveManager.GetJobs(user);
77    }
78
79
80    public Model.Job GetJob(Model.User user, string id) {
81      return hiveManager.GetJob(user, id);
82    }
83
84
[9215]85    public bool DeleteJob(Model.User user, string id) {
86      return hiveManager.DeleteJob(user, id);
[8545]87    }
88
89
90    public IList<Model.Run> GetJobResults(Model.User user, string id) {
91      return hiveManager.GetJobResults(user, id);
92    }
[8958]93
94
95    public bool AddHiveScenario(User user, string scenarioXml, string scenarioMapper) {
96      var scenario = parser.ParseScenarioFromXml(scenarioXml);
97      if (scenario == null) {
98        return false;
[9363]99      }
[9166]100      var added = hiveManager.AddScenario(user, scenario.Id, scenarioXml, scenarioMapper);
[9363]101      if (added)
[9395]102        scenarios.Add(scenario);     
[8958]103      return added;
104    }
105
106    public bool DeleteHiveScenario(User user, string scenarioName) {
107      var scen = GetOptimizationScenarioByName(scenarioName);
108      if (scen != null) {
109        scenarios.Remove(scen);
110        return hiveManager.DeleteScenario(user, scenarioName);
111      }
112      return false;
113    }
114
115    public IEnumerable<string> GetOptimizationScenarioNames() {
116      return (from e in scenarios
[9363]117              select e.Id).AsEnumerable();
[8958]118    }
[9215]119
120
[9363]121    public string SaveExperiment(User user, Experiment experiment) {
[9227]122      // make sure all algorithms store their choices aswell
123      var scenarioMap = new Dictionary<string, OptimizationScenario>();
124      var algos = new Stack<Algorithm>();
125      foreach (var algo in experiment.Algorithm) algos.Push(algo);
126
[9363]127
128
[9227]129      while (algos.Count > 0) {
130        var algo = algos.Pop();
131        if (algo.ChildAlgorithms != null)
132          foreach (var child in algo.ChildAlgorithms) algos.Push(child);
133
134        if (AlgorithmHelper.HasToBeUpdated(algo)) {
135          OptimizationScenario scenario;
136          if (!scenarioMap.TryGetValue(algo.Name, out scenario)) {
137            scenario = GetOptimizationScenarioByName(algo.Name);
138            scenarioMap[algo.Name] = scenario;
139          }
[9363]140
[9227]141          AlgorithmHelper.Update(algo, scenario);
142        }
143      }
144
145      // finally store the experiment
[9305]146      var id = hiveManager.SaveExperiment(user, experiment);
[9227]147      // if it has been stored, and jobdetails are present, run the job!
[9305]148      if (id != null && experiment.JobDetails != null) {
149        ScheduleExperiment(user, experiment.Id, experiment.JobDetails);
[9227]150      }
[9305]151      return id;
[9215]152    }
153
154
[9305]155    public IEnumerable<string> GetExperimentNames(User user) {
156      return hiveManager.GetExperimentNames(user);
[9215]157    }
158
159
160    public bool DeleteExperiment(User user, string experiment) {
161      return hiveManager.DeleteExperiment(user, experiment);
162    }
163
164
165    public Job GetTasks(User u, string jobId) {
166      return hiveManager.GetTasks(u, jobId);
167    }
168
169    public Task GetTaskData(User u, string jobId, string taskId) {
170      return hiveManager.GetTaskData(u, jobId, taskId);
171    }
[9227]172
173
174    public Experiment GetExperimentByName(User user, string scenario) {
175      return hiveManager.GetExperimentByName(user, scenario);
176    }
177
178
[9305]179    public bool ScheduleExperiment(User user, string experimentId, JobExecutionDetails details) {
180      var exp = hiveManager.GetExperimentById(user, experimentId);
[9227]181      return hiveManager.DispatchExperiment(user, exp, details);
182    }
[9305]183
184
[9363]185    public IEnumerable<Experiment> GetExperiments(User user, bool namesOnly = false) {
[9324]186      return hiveManager.GetExperiments(user, namesOnly);
[9305]187    }
188
189    public Experiment GetExperimentById(User u, string nodeId) {
190      return hiveManager.GetExperimentById(u, nodeId);
191    }
[9362]192
193
[9395]194    public VisualExtension GetVisualExtension(string algorithmId) {
195      return new VisualExtension() { ScenarioId = algorithmId, ScenarioJs = hiveManager.GetVisualExtension(algorithmId) };
[9362]196    }
197
198
199    public bool AddVisualExtension(string algorithmId, string script) {
[9395]200      return hiveManager.AddVisualExtension(algorithmId, script);
[9362]201    }
202
203    public bool DeleteVisualExtension(string algorithmId) {
[9395]204      return hiveManager.DeleteVisualExtension(algorithmId);
[9362]205    }
[9395]206
207
208    public bool ExistsVisualExtension(string algorithmId) {
209      return hiveManager.ExistsVisualExtension(algorithmId);
210    }
[8384]211  }
212}
Note: See TracBrowser for help on using the repository browser.