Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15889 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
Line 
1using System.Collections.Generic;
2using System.Linq;
3using System.ServiceModel;
4using HeuristicLab.Services.Optimization.ControllerService.General;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using HeuristicLab.Services.Optimization.ControllerService.Model;
7
8namespace HeuristicLab.Services.Optimization.ControllerService {
9  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
10  public class PlaceholderControllerService : IControllerService {
11
12    private IList<Model.OptimizationScenario> scenarios;
13
14    private ScenarioParser parser = new ScenarioParser();
15
16    private IScenarioManager hiveManager;
17
18    public PlaceholderControllerService() {
19      /*var tsp = new Model.OptimizationScenario() { Name = "Traveling Salesman Problem" };
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" } } });
38      //tsp.InputParameters.Add(new Model.Parameter() { Name = "SolutionCreator" });
39      tsp.InputParameters.Items.Add(new Model.Parameter() { Type = Model.ParameterType.Boolean, Value = new Model.BoolValue() { Name = "UseDistanceMatrix", Value = true } });
40
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 });
50      this.scenarios = new List<Model.OptimizationScenario>() { tsp };*/
51      //hiveManager.DispatchScenario(tsp);
52      //hiveManager = new MockupScenarioManager();
53      hiveManager = new HiveScenarioManager();
54      scenarios = parser.Scenarios;
55      //hiveManager.DispatchScenario(new User() { Username = "fschoeppl", Password = "fschoeppl" }, scenarios[0]);
56    }
57
58    public IEnumerable<Model.OptimizationScenario> GetOptimizationScenarios() {
59      return scenarios;
60    }
61
62
63    public Model.OptimizationScenario GetOptimizationScenarioByName(string name) {
64      var scen = (from e in scenarios
65                  where e.Id == name
66                  select e).FirstOrDefault();
67      return scen;
68    }
69
70    public bool ScheduleOptimizationScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
71      hiveManager.DispatchScenario(user, scenario, details);
72      return true;
73    }
74
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
85    public bool DeleteJob(Model.User user, string id) {
86      return hiveManager.DeleteJob(user, id);
87    }
88
89
90    public IList<Model.Run> GetJobResults(Model.User user, string id) {
91      return hiveManager.GetJobResults(user, id);
92    }
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;
99      }
100      var added = hiveManager.AddScenario(user, scenario.Id, scenarioXml, scenarioMapper);
101      if (added)
102        scenarios.Add(scenario);     
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
117              select e.Id).AsEnumerable();
118    }
119
120
121    public string SaveExperiment(User user, Experiment experiment) {
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
127
128
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          }
140
141          AlgorithmHelper.Update(algo, scenario);
142        }
143      }
144
145      // finally store the experiment
146      var id = hiveManager.SaveExperiment(user, experiment);
147      // if it has been stored, and jobdetails are present, run the job!
148      if (id != null && experiment.JobDetails != null) {
149        ScheduleExperiment(user, experiment.Id, experiment.JobDetails);
150      }
151      return id;
152    }
153
154
155    public IEnumerable<string> GetExperimentNames(User user) {
156      return hiveManager.GetExperimentNames(user);
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    }
172
173
174    public Experiment GetExperimentByName(User user, string scenario) {
175      return hiveManager.GetExperimentByName(user, scenario);
176    }
177
178
179    public bool ScheduleExperiment(User user, string experimentId, JobExecutionDetails details) {
180      var exp = hiveManager.GetExperimentById(user, experimentId);
181      return hiveManager.DispatchExperiment(user, exp, details);
182    }
183
184
185    public IEnumerable<Experiment> GetExperiments(User user, bool namesOnly = false) {
186      return hiveManager.GetExperiments(user, namesOnly);
187    }
188
189    public Experiment GetExperimentById(User u, string nodeId) {
190      return hiveManager.GetExperimentById(u, nodeId);
191    }
192
193
194    public VisualExtension GetVisualExtension(string algorithmId) {
195      return new VisualExtension() { ScenarioId = algorithmId, ScenarioJs = hiveManager.GetVisualExtension(algorithmId) };
196    }
197
198
199    public bool AddVisualExtension(string algorithmId, string script) {
200      return hiveManager.AddVisualExtension(algorithmId, script);
201    }
202
203    public bool DeleteVisualExtension(string algorithmId) {
204      return hiveManager.DeleteVisualExtension(algorithmId);
205    }
206
207
208    public bool ExistsVisualExtension(string algorithmId) {
209      return hiveManager.ExistsVisualExtension(algorithmId);
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.