Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/HLMapper.cs @ 8932

Last change on this file since 8932 was 8817, checked in by fschoepp, 12 years ago

#1888:

  • Added a parser for independent scenarios (using the model of the optimization backend)
  • Optimization scenario sample can be found in mappings folder of the web project.
  • Added IScenarioMapper interface which provides functionality to map from the optimization data model to a backend model (e.g. Heuristic Lab data model)
  • Implementations of IScenarioMapper have to be provided as C# code (strings) which will be compiled by using a CSharpCodeProvider. Once compiled, the implementations of the IScenarioMapper are being cached within the platform for further usage.
  • Fixed a bug in web template DecimalMatrix (using i instead of j)
  • Added missing thumprint of localhost certificate to the optimization web project (ServiceConfiguration.Local.cscfg / ServiceConfiguration.Cloud.cscfg)
  • Test project now provides following test cases: Mapping types using IronPython and mapping types using Otis
File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Data;
6using HeuristicLab.Services.Optimization.ControllerService.Model;
7using HeuristicLab.Optimization;
8using HeuristicLab.Encodings.PermutationEncoding;
9using HeuristicLab.Core;
10
11namespace HeuristicLab.Services.Optimization.ControllerService {
12  public interface IScenarioMapper {
13    void MapScenario(OptimizationScenario scenario, IAlgorithm algorithm);
14  }
15
16  public static class HLMapper {
17    public static DoubleValue ConvertToDoubleValue(Parameter parameter) {
18      var dv = parameter.Value as DecimalValue;
19      return new DoubleValue(dv.Value);     
20    }
21
22    public static Permutation ConvertToPermutation(Parameter parameter) {
23      double[] input = (parameter.Value as DecimalVector).Value;
24      int[] arr = new int[input.Length];
25      for (int i = 0; i < input.Length; i++) {
26        arr[i] = Convert.ToInt32(input[i]);
27      }
28      var valueObj = new HeuristicLab.Encodings.PermutationEncoding.Permutation(Encodings.PermutationEncoding.PermutationTypes.RelativeUndirected, arr);
29      return valueObj;
30    }
31
32    public static DoubleMatrix ConvertToDoubleMatrix(Parameter parameter) {
33      var matrix = parameter.Value as DecimalMatrix;
34      // TODO: Check for empty matrices
35      double[,] data = new double[matrix.Value.Length, matrix.Value[0].Length];
36      for (int i=0; i < matrix.Value.Length; i++) {
37        for (int j=0; j < matrix.Value[i].Length; j++) {
38          data[i,j] = matrix.Value[i][j];
39        }
40      }
41      return new DoubleMatrix(data);
42    }
43
44    public static HeuristicLab.Data.BoolValue ConvertToBoolValue(Parameter parameter) {
45      return new HeuristicLab.Data.BoolValue((parameter.Value as HeuristicLab.Services.Optimization.ControllerService.Model.BoolValue).Value);
46    }
47
48    public static IntValue ConvertToIntValue(Parameter parameter) {
49      return new IntValue((int)(parameter.Value as DecimalValue).Value);
50    }
51
52    public static PercentValue ConvertToPercentValue(Parameter parameter) {
53      return new PercentValue((parameter.Value as DecimalValue).Value / 100);
54    }
55
56    public static string GetStringValue(Parameter parameter) {
57      return (parameter.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value;
58    }
59
60    public static T FindInItemSet<T>(Core.IItemSet<T> items, Parameter what) where T : class,Core.INamedItem {
61      return items.FirstOrDefault((entry) => { return entry.Name == (what.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value; });
62    }
63
64    public static T FindOperator<T>(IProblem problem, Parameter what) where T : IOperator {
65      return problem.Operators.OfType<T>().FirstOrDefault((item) => { return item.ItemName == (what.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value; });
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.