[8958] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
|
---|
| 6 | using HeuristicLab.Optimization;
|
---|
| 7 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 8 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
| 9 | using HeuristicLab;
|
---|
| 10 | using System.Reflection;
|
---|
| 11 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
| 12 | using HeuristicLab.Core;
|
---|
| 13 | using System.Collections;
|
---|
| 14 | using HeuristicLab.Clients.Hive;
|
---|
| 15 | using System.Threading;
|
---|
| 16 | using HeuristicLab.Data;
|
---|
| 17 | using System.IO;
|
---|
| 18 | using Microsoft.WindowsAzure;
|
---|
| 19 | using Microsoft.WindowsAzure.StorageClient;
|
---|
| 20 | using HeuristicLab.Services.Optimization.ControllerService.Azure;
|
---|
[9062] | 21 | using System.Data;
|
---|
[9166] | 22 | using HeuristicLab.Services.Optimization.ControllerService.General;
|
---|
[9350] | 23 | using HeuristicLab.Services.Optimization.ControllerService.Parameters.HL;
|
---|
[8958] | 24 |
|
---|
| 25 | namespace HeuristicLab.Services.Optimization.ControllerService {
|
---|
[9350] | 26 | public class ScenarioEntity : TableServiceEntity {
|
---|
[8958] | 27 | public ScenarioEntity() {
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public ScenarioEntity(string scenarioName, string scenario, string mapper) {
|
---|
| 31 | PartitionKey = "ScenarioPartition";
|
---|
| 32 | RowKey = scenarioName;
|
---|
| 33 | Scenario = scenario;
|
---|
| 34 | Mapper = mapper;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public string Scenario { get; set; }
|
---|
| 38 |
|
---|
| 39 | public string Mapper { get; set; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public class HiveScenarioManager : IScenarioManager {
|
---|
[9350] | 43 | private static HLParameterMapper parameterMapper = new HLParameterMapper();
|
---|
[8958] | 44 | private static IScenarioMapper tspMapper;
|
---|
| 45 | private static object lockable;
|
---|
| 46 | private Dictionary<string, IScenarioMapper> mappers = new Dictionary<string, IScenarioMapper>();
|
---|
[9166] | 47 | private IDataAccessLayer dal = DataAccessLayerProvider.GetLayer();
|
---|
[8958] | 48 |
|
---|
| 49 | public static readonly string SCENARIO_TABLE = "Scenario";
|
---|
| 50 |
|
---|
| 51 | static HiveScenarioManager() {
|
---|
| 52 | lockable = new object();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[9215] | 55 | public string DispatchScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) {
|
---|
[9166] | 56 | // Experiment experiment = new Experiment();
|
---|
| 57 | // var problem = new TravelingSalesmanProblem();
|
---|
| 58 | // var algo = new GeneticAlgorithm();
|
---|
| 59 | // algo.Problem = problem;
|
---|
[8958] | 60 |
|
---|
[9227] | 61 | IScenarioMapper mapper = GetMapper(scenario.Id);
|
---|
[9166] | 62 | IAlgorithm algo;
|
---|
| 63 | mapper.MapScenario(scenario, out algo);
|
---|
[9227] | 64 | /*if (details.Repititions > 1) {
|
---|
[9166] | 65 | BatchRun br = new BatchRun();
|
---|
| 66 | //br.Name = details.JobTitle;
|
---|
| 67 | br.Optimizer = algo;
|
---|
| 68 | br.Repetitions = details.Repititions;
|
---|
[9215] | 69 | return SendExperimentToHive(user, br, details);
|
---|
[9166] | 70 | }
|
---|
| 71 | else {
|
---|
[9227] | 72 |
|
---|
| 73 | }*/
|
---|
| 74 | return SendExperimentToHive(user, algo, details);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private sealed class StackEntry {
|
---|
| 78 | public HeuristicLab.Optimization.Experiment Parent { get; set; }
|
---|
| 79 | public IList<Model.Algorithm> Children { get; set; }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public bool DispatchExperiment(User user, Model.Experiment exp, JobExecutionDetails details) {
|
---|
| 83 | // For now the experiment will be flatened for execution
|
---|
| 84 | HeuristicLab.Optimization.Experiment hiveExperiment = new HeuristicLab.Optimization.Experiment(exp.Name);
|
---|
| 85 | var stack = new Stack<StackEntry>();
|
---|
| 86 | var children = new List<Model.Algorithm>();
|
---|
| 87 | foreach (var child in exp.Algorithm) {
|
---|
| 88 | children.Add(child);
|
---|
[9166] | 89 | }
|
---|
[9227] | 90 | stack.Push(new StackEntry() { Parent = hiveExperiment, Children = children });
|
---|
| 91 |
|
---|
| 92 | while (stack.Count > 0) {
|
---|
| 93 | var entry = stack.Pop();
|
---|
| 94 | // handle current entry
|
---|
| 95 | foreach (var child in entry.Children) {
|
---|
| 96 | // This is a template experiment;
|
---|
[9305] | 97 | if (child.IsExperiment) {
|
---|
| 98 | var parent = new HeuristicLab.Optimization.Experiment();
|
---|
[9227] | 99 | entry.Parent.Optimizers.Add(parent);
|
---|
| 100 | stack.Push(new StackEntry() { Parent = parent, Children = child.ChildAlgorithms });
|
---|
| 101 | }
|
---|
| 102 | // This entity is mapable
|
---|
| 103 | else {
|
---|
[9305] | 104 | IScenarioMapper mapper = GetMapper(child.Id);
|
---|
| 105 | var optScen = new OptimizationScenario() { Id = child.Id };
|
---|
| 106 | optScen.Algorithm.Add(child);
|
---|
| 107 | IAlgorithm algo;
|
---|
| 108 | mapper.MapScenario(optScen, out algo);
|
---|
[9395] | 109 | algo.Name = child.Id;
|
---|
[9305] | 110 | entry.Parent.Optimizers.Add(algo);
|
---|
[9227] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | details.JobTitle = exp.Name;
|
---|
[9395] | 115 | var jobId = SendExperimentToHive(user, hiveExperiment, details);
|
---|
| 116 |
|
---|
| 117 | // add relationship to azure tablestorage
|
---|
| 118 | if (jobId != null) {
|
---|
| 119 | dal.JobDao.Add(user.Username, exp, jobId);
|
---|
| 120 | }
|
---|
| 121 | return jobId != null;
|
---|
[8958] | 122 | }
|
---|
| 123 |
|
---|
| 124 | static public string AssemblyDirectory {
|
---|
| 125 | get {
|
---|
| 126 | string codeBase = Assembly.GetExecutingAssembly().CodeBase;
|
---|
| 127 | UriBuilder uri = new UriBuilder(codeBase);
|
---|
| 128 | string path = Uri.UnescapeDataString(uri.Path);
|
---|
| 129 | return Path.GetDirectoryName(path);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[9166] | 133 | public static IScenarioMapper CompileMapper(string scenarioMapper) {
|
---|
[8958] | 134 | // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe
|
---|
| 135 | using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) {
|
---|
| 136 | var cp = new System.CodeDom.Compiler.CompilerParameters() {
|
---|
| 137 | GenerateInMemory = true
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | var referencedPaths = Directory.GetFiles(AssemblyDirectory, "*.dll");
|
---|
| 141 | foreach (var loadedAssembly in referencedPaths) {
|
---|
| 142 | cp.ReferencedAssemblies.Add(loadedAssembly);
|
---|
| 143 | }
|
---|
| 144 | cp.ReferencedAssemblies.Add("System.dll");
|
---|
| 145 | cp.ReferencedAssemblies.Add("System.Core.dll");
|
---|
| 146 | cp.ReferencedAssemblies.Add("System.Data.dll");
|
---|
| 147 | cp.ReferencedAssemblies.Add("System.Xml.dll");
|
---|
| 148 | cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
|
---|
| 149 |
|
---|
| 150 | var res = csCodeProvider.CompileAssemblyFromSource(
|
---|
| 151 | cp,
|
---|
| 152 | scenarioMapper
|
---|
| 153 | );
|
---|
| 154 |
|
---|
| 155 | foreach (var error in res.Errors) {
|
---|
| 156 | Console.WriteLine(error);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | var firstMapper = res.CompiledAssembly.GetTypes().Where(p => typeof(IScenarioMapper).IsAssignableFrom(p)).FirstOrDefault();
|
---|
| 160 | return Activator.CreateInstance(firstMapper) as IScenarioMapper;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[9227] | 164 | private IScenarioMapper GetMapper(string scenarioId) {
|
---|
| 165 | var id = scenarioId;
|
---|
[9305] | 166 | IScenarioMapper mapper = null;
|
---|
| 167 | if (!mappers.TryGetValue(id, out mapper)) {
|
---|
[8958] | 168 | lock (lockable) {
|
---|
[9166] | 169 | if (mappers.ContainsKey(id))
|
---|
| 170 | return mappers[id];
|
---|
[8958] | 171 |
|
---|
[9166] | 172 | var mapperString = GetMapperFromBlobStore(id);
|
---|
[9227] | 173 | if (mapperString == null) return null;
|
---|
[9305] | 174 | mapper = CompileMapper(mapperString);
|
---|
[8958] | 175 |
|
---|
[9166] | 176 | mappers[id] = mapper;
|
---|
[9062] | 177 | return mapper;
|
---|
[8958] | 178 | } // lock
|
---|
| 179 | } // if
|
---|
[9305] | 180 | return mapper;
|
---|
[8958] | 181 | }
|
---|
| 182 |
|
---|
[9166] | 183 | private void MapExperiment(Model.OptimizationScenario scenario, out IAlgorithm algorithm) {
|
---|
[9227] | 184 | IScenarioMapper mapper = GetMapper(scenario.Id);
|
---|
[9166] | 185 | mapper.MapScenario(scenario, out algorithm);
|
---|
[8958] | 186 | }
|
---|
| 187 |
|
---|
[9227] | 188 | private HiveServiceLocator ConfigureHive(Model.User user) {
|
---|
| 189 | var serviceLocator = new HiveServiceLocator();
|
---|
| 190 | serviceLocator.Username = user.Username;
|
---|
| 191 | serviceLocator.Password = user.Password;
|
---|
| 192 | serviceLocator.EndpointConfigurationName = Configuration.HiveEndpointName;
|
---|
| 193 | return serviceLocator;
|
---|
[9166] | 194 | }
|
---|
| 195 |
|
---|
[9215] | 196 | private string SendExperimentToHive(Model.User user, IOptimizer exp, JobExecutionDetails details) {
|
---|
[8958] | 197 | var job = new RefreshableJob();
|
---|
| 198 | job.IsAllowedPrivileged = true;
|
---|
[9166] | 199 | job.Job.Name = details.JobTitle;
|
---|
| 200 | job.Job.ResourceNames = details.Group;
|
---|
[9227] | 201 | job.RefreshAutomatically = false;
|
---|
| 202 |
|
---|
| 203 | if (details.Repititions > 1) {
|
---|
| 204 | BatchRun br = new BatchRun();
|
---|
| 205 | br.Optimizer = exp;
|
---|
| 206 | br.Repetitions = details.Repititions;
|
---|
| 207 | exp = br;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[9166] | 210 | if (exp.ExecutionState != ExecutionState.Prepared) {
|
---|
| 211 | exp.Prepare();
|
---|
| 212 | }
|
---|
[8958] | 213 | job.HiveTasks.Add(new OptimizerHiveTask(exp));
|
---|
[9508] | 214 |
|
---|
| 215 | var locator = ConfigureHive(user);
|
---|
| 216 | HiveClient.Store(job, new CancellationToken(), locator);
|
---|
[8958] | 217 |
|
---|
| 218 | job.StopResultPolling();
|
---|
[9215] | 219 | return job.Id != null ? job.Id.ToString() : null;
|
---|
[8958] | 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | public IList<Model.Job> GetJobs(User user) {
|
---|
[9227] | 224 | var serviceLocator = ConfigureHive(user);
|
---|
| 225 | var jobsLoaded = serviceLocator.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.Job>>(s => s.GetJobs());
|
---|
[8958] | 226 | IList<Model.Job> jobs = new List<Model.Job>();
|
---|
[9166] | 227 |
|
---|
[8958] | 228 | foreach (var job in jobsLoaded) {
|
---|
| 229 | jobs.Add(ConvertJob(user, job));
|
---|
[9215] | 230 | }
|
---|
[8958] | 231 | return jobs;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | private Model.Job ConvertJob(User user, HeuristicLab.Clients.Hive.Job job)
|
---|
| 235 | {
|
---|
| 236 | var waitingJobs = job.JobCount - job.CalculatingCount - job.FinishedCount;
|
---|
| 237 | Model.JobState? state = null;
|
---|
[9062] | 238 | if (job.CalculatingCount > 0)
|
---|
| 239 | state = JobState.Calculating;
|
---|
[9166] | 240 | else if (job.JobCount > 0 && job.JobCount == job.FinishedCount)
|
---|
[9062] | 241 | state = JobState.Finished;
|
---|
| 242 | else
|
---|
| 243 | state = JobState.Waiting;
|
---|
[9215] | 244 |
|
---|
[9062] | 245 | return new Model.Job() { Id = job.Id.ToString(), Name = job.Name, Resource = job.ResourceNames, State = state.Value, DateCreated = job.DateCreated };
|
---|
[8958] | 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | public Model.Job GetJob(User user, string id) {
|
---|
[9227] | 250 | var serviceLocator = ConfigureHive(user);
|
---|
[8958] | 251 | var guid = Guid.Parse(id);
|
---|
[9227] | 252 | return ConvertJob(user, serviceLocator.CallHiveService<HeuristicLab.Clients.Hive.Job>(s => s.GetJob(guid)));
|
---|
[8958] | 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
[9215] | 256 | public bool DeleteJob(User user, string id) {
|
---|
[9227] | 257 | var serviceLocator = ConfigureHive(user);
|
---|
[9215] | 258 | var guid = Guid.Parse(id);
|
---|
[9227] | 259 | serviceLocator.CallHiveService(s => s.DeleteJob(guid));
|
---|
[9215] | 260 | return true;
|
---|
[8958] | 261 | }
|
---|
| 262 |
|
---|
| 263 | public IList<Model.Run> GetJobResults(User user, string id) {
|
---|
[9227] | 264 | var serviceLocator = ConfigureHive(user);
|
---|
[8958] | 265 | var guid = Guid.Parse(id);
|
---|
[9227] | 266 | var jobTasks = serviceLocator.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.LightweightTask>>(s => s.GetLightweightJobTasks(guid));
|
---|
[8958] | 267 |
|
---|
| 268 | IList<Guid> taskIds = new List<Guid>();
|
---|
| 269 | foreach (var task in jobTasks) {
|
---|
| 270 | taskIds.Add(task.Id);
|
---|
| 271 | }
|
---|
[9508] | 272 |
|
---|
| 273 | TaskDownloader downloader = new TaskDownloader(taskIds, ConfigureHive(user));
|
---|
[8958] | 274 | downloader.StartAsync();
|
---|
| 275 | while (!downloader.IsFinished) {
|
---|
| 276 | Thread.Sleep(500);
|
---|
| 277 |
|
---|
| 278 | if (downloader.IsFaulted) {
|
---|
| 279 | throw downloader.Exception;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
[9395] | 282 |
|
---|
[8958] | 283 | IDictionary<Guid, HiveTask> hiveTasks = downloader.Results;
|
---|
| 284 | IList<Model.Run> runs = new List<Model.Run>();
|
---|
| 285 | foreach (var keyTask in hiveTasks.Keys) {
|
---|
| 286 | var oht = hiveTasks[keyTask] as OptimizerHiveTask;
|
---|
| 287 | if (oht != null) {
|
---|
| 288 | foreach (var run in oht.ItemTask.Item.Runs) {
|
---|
| 289 | Model.Run taskRun = new Model.Run();
|
---|
| 290 | taskRun.Id = taskRun.Name = run.Name;
|
---|
[9395] | 291 | // We try to extract the original algorithm name here
|
---|
| 292 | var index = taskRun.Name.LastIndexOf(" Run ");
|
---|
| 293 | var algorithName = taskRun.Name.Substring(0, index);
|
---|
| 294 | taskRun.AlgorithmName = algorithName;
|
---|
| 295 | if (taskRun.AlgorithmName == null)
|
---|
| 296 | taskRun.AlgorithmName = run.Algorithm != null ? run.Algorithm.Name : run.Name;
|
---|
| 297 |
|
---|
[8958] | 298 | IList<Parameter> resultValues = new List<Model.Parameter>();
|
---|
| 299 | foreach (var key in run.Results.Keys) {
|
---|
| 300 | var value = run.Results[key];
|
---|
| 301 | Parameter result = MapHiveDataType(key, value);
|
---|
[9350] | 302 | resultValues.Add(result);
|
---|
[8958] | 303 | }
|
---|
| 304 | taskRun.Results = resultValues;
|
---|
[9350] | 305 | IList<Parameter> inputParameters = new List<Model.Parameter>();
|
---|
| 306 | foreach (var key in run.Parameters.Keys) {
|
---|
| 307 | var value = run.Parameters[key];
|
---|
| 308 | Parameter param = MapHiveDataType(key, value);
|
---|
| 309 | inputParameters.Add(param);
|
---|
| 310 | }
|
---|
| 311 | // crawl the copied experiment of the job
|
---|
| 312 | //taskRun.Experiment = dal.JobDao.FindByJobId(id);
|
---|
| 313 | taskRun.InputParameters = inputParameters;
|
---|
[8958] | 314 | runs.Add(taskRun);
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | return runs;
|
---|
| 319 | }
|
---|
[9062] | 320 |
|
---|
[8958] | 321 | private Parameter MapHiveDataType(string name, IItem item) {
|
---|
[9350] | 322 |
|
---|
| 323 | if (parameterMapper.IsHandlerAvailable(item)) {
|
---|
| 324 | return parameterMapper.Map(name, item);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | var result = new Parameter();
|
---|
[8958] | 328 | result.Type = ParameterType.String;
|
---|
| 329 | if (item is IStringConvertibleValue) {
|
---|
| 330 | var value = (item as IStringConvertibleValue).GetValue();
|
---|
| 331 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.StringValue() { Name = name, Value = value };
|
---|
| 332 | }
|
---|
| 333 | else if (item is IStringConvertibleValueTuple) {
|
---|
| 334 | var value1 = (item as IStringConvertibleValueTuple).Item1.GetValue();
|
---|
| 335 | var value2 = (item as IStringConvertibleValueTuple).Item2.GetValue();
|
---|
| 336 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.StringValue() { Name = name, Value = "{" + value1 + ", " + value2 + "}" };
|
---|
[9062] | 337 | }
|
---|
| 338 | else if (item is DoubleArray) {
|
---|
| 339 | var array = item as DoubleArray;
|
---|
| 340 | double[] arrayValue = new double[array.Length];
|
---|
| 341 | for (int i = 0; i < arrayValue.Length; ++i) {
|
---|
| 342 | arrayValue[i] = array[i];
|
---|
| 343 | }
|
---|
| 344 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalVector() { Name = name, Value = arrayValue };
|
---|
[8958] | 345 | }
|
---|
[9062] | 346 | else if (item is DoubleMatrix) {
|
---|
| 347 | var matrix = item as DoubleMatrix;
|
---|
[9324] | 348 | /*double[][] matrixValue = new double[matrix.Rows][];
|
---|
[9062] | 349 | for (int i = 0; i < matrixValue.Length; ++i) {
|
---|
| 350 | matrixValue[i] = new double[matrix.Columns];
|
---|
| 351 | for (int j = 0; j < matrixValue[i].Length; ++j) {
|
---|
| 352 | matrixValue[i][j] = matrix[i, j];
|
---|
| 353 | }
|
---|
[9324] | 354 | } */
|
---|
| 355 | double[][] matrixValue = new double[matrix.Columns][];
|
---|
| 356 | for (int i = 0; i < matrix.Columns; ++i) {
|
---|
| 357 | matrixValue[i] = new double[matrix.Rows];
|
---|
| 358 | for (int j = 0; j < matrix.Rows; ++j) {
|
---|
| 359 | matrixValue[i][j] = matrix[j, i];
|
---|
| 360 | }
|
---|
[9350] | 361 | }
|
---|
| 362 | matrixValue = transpose(matrixValue);
|
---|
[9324] | 363 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalMatrix() { Name = name, Value = matrixValue, RowNames = (matrix.ColumnNames.Count() > 0 ? matrix.ColumnNames.ToArray() : null) };
|
---|
[9062] | 364 | }
|
---|
[8958] | 365 | else if (item is IStringConvertibleArray) {
|
---|
| 366 | StringBuilder sb = new StringBuilder();
|
---|
| 367 | var array = item as IStringConvertibleArray;
|
---|
| 368 | if (array.Length == 0) {
|
---|
| 369 | sb.Append("[ ]");
|
---|
[9062] | 370 | }
|
---|
| 371 | else {
|
---|
[8958] | 372 | sb.Append("[");
|
---|
[9062] | 373 | for (int i = 0; i < array.Length - 1; i++) {
|
---|
| 374 | sb.Append(array.GetValue(i)).Append(", ");
|
---|
[8958] | 375 | }
|
---|
| 376 | sb.Append(array.GetValue(array.Length - 1));
|
---|
| 377 | sb.Append(" ]");
|
---|
| 378 | }
|
---|
| 379 | var value = sb.ToString();
|
---|
| 380 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.StringValue() { Name = name, Value = value };
|
---|
| 381 | }
|
---|
[9062] | 382 | else if (item is HeuristicLab.Analysis.DataTable) {
|
---|
| 383 | var table = item as HeuristicLab.Analysis.DataTable;
|
---|
| 384 | string[] names = new string[table.Rows.Count];
|
---|
| 385 | double[][] results = new double[table.Rows.Count][];
|
---|
| 386 | for (int i = 0; i < table.Rows.Count; i++ ) {
|
---|
| 387 | var columns = table.Rows.ToList()[i];
|
---|
| 388 | names[i] = columns.Name;
|
---|
[9324] | 389 | results[i] = new double[columns.Values.Count];
|
---|
[9062] | 390 | for (int j=0; j < columns.Values.Count; j++) {
|
---|
| 391 | results[i][j] = columns.Values[j];
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
[9324] | 394 | results = transpose(results);
|
---|
[9062] | 395 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalMatrix() { Name = name, Value = results, RowNames = names };
|
---|
| 396 | }
|
---|
[8958] | 397 | else if (item is IStringConvertibleMatrix) {
|
---|
| 398 | StringBuilder sb = new StringBuilder();
|
---|
| 399 | var matrix = item as IStringConvertibleMatrix;
|
---|
[9062] | 400 |
|
---|
[8958] | 401 | if (matrix.Rows == 0 || matrix.Columns == 0) {
|
---|
| 402 | sb.Append("[ ]");
|
---|
| 403 | }
|
---|
| 404 | else {
|
---|
[9062] | 405 |
|
---|
[8958] | 406 | sb.Append("[ ");
|
---|
| 407 | for (int r = 0; r < matrix.Rows; r++) {
|
---|
| 408 | sb.Append("( ");
|
---|
| 409 | for (int c = 0; c < matrix.Columns - 1; c++) {
|
---|
[9062] | 410 | matrix.GetValue(r, c);
|
---|
[8958] | 411 | sb.Append(matrix.GetValue(r, c)).Append(", ");
|
---|
| 412 | }
|
---|
| 413 | sb.Append(matrix.GetValue(r, matrix.Columns - 1)).Append(r < matrix.Rows - 1 ? " ), " : " )");
|
---|
| 414 | }
|
---|
| 415 | sb.Append(" ]");
|
---|
| 416 | }
|
---|
| 417 | var value = sb.ToString();
|
---|
| 418 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.StringValue() { Name = name, Value = value };
|
---|
| 419 | }
|
---|
| 420 | else {
|
---|
[9350] | 421 | result.Value = new HeuristicLab.Services.Optimization.ControllerService.Model.StringValue() { Name = name, Value = item.ItemName != null ? item.ItemName + " (Cannot be displayed properly as string)" : "Cannot be displayed properly as string" };
|
---|
[8958] | 422 | }
|
---|
| 423 | return result;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | public bool AddScenario(User user, string scenarioName, string scenarioXml, string scenarioMapper) {
|
---|
| 427 | // create scenario mapper
|
---|
| 428 | var mapper = CompileMapper(scenarioMapper);
|
---|
| 429 | if (mapper == null)
|
---|
| 430 | return false;
|
---|
| 431 |
|
---|
| 432 | // insert into table & blob store
|
---|
[9215] | 433 | var scenDao = dal.ScenarioDao;
|
---|
| 434 | var blobDao = dal.BlobDao;
|
---|
[8958] | 435 |
|
---|
| 436 | Guid scenarioXmlGuid = Guid.NewGuid();
|
---|
| 437 | Guid scenarioMapperGuid = Guid.NewGuid();
|
---|
| 438 | string scenarioXmlId = scenarioName + "_" + scenarioXmlGuid.ToString();
|
---|
| 439 | string scenarioMapperId = scenarioName + "_" + scenarioMapperGuid.ToString();
|
---|
| 440 | if (!blobDao.Add(new StringEntry() { Key = scenarioXmlId, Text = scenarioXml }))
|
---|
| 441 | return false;
|
---|
| 442 | if (!blobDao.Add(new StringEntry() { Key = scenarioMapperId, Text = scenarioMapper }))
|
---|
| 443 | return false;
|
---|
| 444 | if (!scenDao.Add(new ScenarioEntity(scenarioName, scenarioXmlId, scenarioMapperId)))
|
---|
| 445 | return false;
|
---|
| 446 |
|
---|
| 447 | // everything stored in the DB -> add mapper to dictionary
|
---|
| 448 | if (!mappers.ContainsKey(scenarioName)) {
|
---|
| 449 | lock (lockable) {
|
---|
| 450 | if (!mappers.ContainsKey(scenarioName))
|
---|
| 451 | mappers[scenarioName] = mapper;
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 | return true;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[9324] | 457 | private double[][] transpose(double[][] arr) {
|
---|
| 458 | var width = arr.Length;
|
---|
| 459 | var height = arr[0].Length;
|
---|
| 460 |
|
---|
| 461 | var res = new double[height][];
|
---|
| 462 | for (var i = 0; i < height; i++)
|
---|
| 463 | res[i] = new double[width];
|
---|
| 464 |
|
---|
| 465 | for (var i = 0; i < width; i++) {
|
---|
| 466 | for (var j = 0; j < height; j++) {
|
---|
| 467 | res[j][i] = arr[i][j];
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 | return res;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[8958] | 473 | private string GetMapperFromBlobStore(string scenarioName) {
|
---|
[9215] | 474 | var scenarioDao = dal.ScenarioDao;
|
---|
| 475 | var blobDao = dal.BlobDao;
|
---|
[8958] | 476 | var entity = scenarioDao.FindByName(scenarioName);
|
---|
| 477 | if (entity == null)
|
---|
| 478 | return null;
|
---|
| 479 |
|
---|
| 480 | var mapper = blobDao.FindByKey(entity.Mapper);
|
---|
| 481 | if (mapper == null)
|
---|
| 482 | return null;
|
---|
| 483 | return mapper.Text;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | public bool DeleteScenario(User user, string scenarioName) {
|
---|
| 487 | // delete from table & blob store
|
---|
[9215] | 488 | var scenarioDao = dal.ScenarioDao;
|
---|
| 489 | var blobDao = dal.BlobDao;
|
---|
[8958] | 490 |
|
---|
| 491 | var entity = scenarioDao.FindByName(scenarioName);
|
---|
| 492 | if (entity == null)
|
---|
| 493 | return false;
|
---|
| 494 |
|
---|
| 495 | blobDao.DeleteByKey(entity.Mapper);
|
---|
| 496 | blobDao.DeleteByKey(entity.Scenario);
|
---|
| 497 | scenarioDao.DeleteByName(scenarioName);
|
---|
| 498 | return true;
|
---|
| 499 | }
|
---|
[9215] | 500 |
|
---|
[9305] | 501 | public string SaveExperiment(User user, Model.Experiment experiment) {
|
---|
| 502 | if (experiment.Id != null) {
|
---|
| 503 | dal.ExperimentDao.Update(user.Username, experiment);
|
---|
| 504 | }
|
---|
| 505 | else {
|
---|
| 506 | dal.ExperimentDao.Add(user.Username, experiment);
|
---|
| 507 | }
|
---|
| 508 | return experiment.Id;
|
---|
[9215] | 509 | }
|
---|
| 510 |
|
---|
[9305] | 511 | public IEnumerable<string> GetExperimentNames(User user) {
|
---|
[9215] | 512 | return (from exp in dal.ExperimentDao.GetExperiments(user.Username)
|
---|
| 513 | select exp.Name);
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 |
|
---|
| 517 | public bool DeleteExperiment(User user, string experiment) {
|
---|
[9324] | 518 | return dal.ExperimentDao.Delete(user.Username, experiment);
|
---|
[9215] | 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
| 522 | public Model.Task GetTaskData(User u, string jobId, string taskId) {
|
---|
[9508] | 523 | TaskDownloader downloader = new TaskDownloader(new List<Guid>(){Guid.Parse(taskId)}, ConfigureHive(u));
|
---|
[9215] | 524 | downloader.StartAsync();
|
---|
| 525 | while (!downloader.IsFinished) {
|
---|
| 526 | Thread.Sleep(250);
|
---|
| 527 | if (downloader.IsFaulted) {
|
---|
| 528 | throw downloader.Exception;
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | IDictionary<Guid, HiveTask> hiveTasks = downloader.Results;
|
---|
| 533 | var task = hiveTasks[Guid.Parse(taskId)];
|
---|
| 534 | if (task == null)
|
---|
| 535 | return null;
|
---|
| 536 |
|
---|
| 537 | return new Model.Task() {
|
---|
| 538 | State = new Model.TaskState() {
|
---|
| 539 | DateCreated = task.Task.DateCreated.ToString(),
|
---|
| 540 | DateFinished = task.Task.DateFinished.ToString(),
|
---|
| 541 | ExecutionTime = task.Task.ExecutionTime.ToString(),
|
---|
| 542 | State = task.Task.State.ToString()
|
---|
| 543 | },
|
---|
| 544 | General = new Model.General() {
|
---|
| 545 | Id = task.Task.Id.ToString(),
|
---|
| 546 | LastChanged = task.Task.Modified.ToString(),
|
---|
| 547 | Name = task.ItemTask.Name
|
---|
| 548 | }
|
---|
| 549 | };
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | public Model.Job GetTasks(User u, string jobId) {
|
---|
[9227] | 553 | var serviceLocator = ConfigureHive(u);
|
---|
| 554 | var jobTasks = serviceLocator.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.LightweightTask>>(s => s.GetLightweightJobTasks(Guid.Parse(jobId)));
|
---|
[9215] | 555 |
|
---|
| 556 | var job = new Model.Job();
|
---|
| 557 | job.Id = jobId;
|
---|
| 558 | var tasks = new Dictionary<Guid, Model.Task>();
|
---|
| 559 | // push all elements to dictionary
|
---|
| 560 | foreach (var task in jobTasks) {
|
---|
[9227] | 561 | var children = serviceLocator.CallHiveService<IEnumerable<HeuristicLab.Clients.Hive.LightweightTask>>(s => s.GetLightweightChildTasks(Guid.Parse(jobId), true, true));
|
---|
[9215] | 562 | foreach (var child in children) {
|
---|
| 563 | tasks[child.Id] = new Model.Task() {
|
---|
| 564 | State = new Model.TaskState() {
|
---|
| 565 | DateCreated = child.DateCreated.ToString(),
|
---|
| 566 | DateFinished = child.DateFinished.ToString(),
|
---|
| 567 | ExecutionTime = child.ExecutionTime.ToString(),
|
---|
| 568 | State = child.State.ToString()
|
---|
| 569 | },
|
---|
| 570 | General = new Model.General() {
|
---|
| 571 | Id = child.Id.ToString(),
|
---|
| 572 | LastChanged = child.Modified.ToString(),
|
---|
| 573 | Name = child.ItemName
|
---|
| 574 | }
|
---|
| 575 | };
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | // traverse all tasks again and create tree of tasks
|
---|
| 580 | foreach (var task in jobTasks) {
|
---|
| 581 | if (task.ParentTaskId.HasValue)
|
---|
| 582 | tasks[task.Id].Children.Add(tasks[task.ParentTaskId.Value]);
|
---|
| 583 | else // its a root task
|
---|
| 584 | job.Tasks.Add(tasks[task.Id]);
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | return job;
|
---|
| 588 | }
|
---|
[9227] | 589 |
|
---|
| 590 |
|
---|
| 591 | public Model.Experiment GetExperimentByName(User user, string scenario) {
|
---|
| 592 | return dal.ExperimentDao.GetExperimentByName(user.Username, scenario);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[9324] | 595 | public IEnumerable<Model.Experiment> GetExperiments(User user, bool namesOnly=false) {
|
---|
| 596 | return dal.ExperimentDao.GetExperiments(user.Username, namesOnly);
|
---|
[9305] | 597 | }
|
---|
[9227] | 598 |
|
---|
[9305] | 599 | public Model.Experiment GetExperimentById(User user, string nodeId) {
|
---|
| 600 | return dal.ExperimentDao.GetExperimentById(user, nodeId);
|
---|
| 601 | }
|
---|
[9362] | 602 |
|
---|
| 603 |
|
---|
| 604 | public string GetVisualExtension(string algorithmId) {
|
---|
| 605 | return dal.VisualExtensionDao.FindById(algorithmId);
|
---|
| 606 | }
|
---|
[9395] | 607 |
|
---|
| 608 |
|
---|
| 609 | public bool AddVisualExtension(string algorithmId, string script) {
|
---|
| 610 | return dal.VisualExtensionDao.Add(algorithmId, script);
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | public bool DeleteVisualExtension(string algorithmId) {
|
---|
| 614 | return dal.VisualExtensionDao.DeleteById(algorithmId);
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 |
|
---|
| 618 | public bool ExistsVisualExtension(string algorithmId) {
|
---|
| 619 | return dal.VisualExtensionDao.Exists(algorithmId);
|
---|
| 620 | }
|
---|
[8958] | 621 | }
|
---|
| 622 | }
|
---|