1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Services.Optimization.ControllerService {
|
---|
12 | public interface IScenarioMapper {
|
---|
13 | void MapScenario(OptimizationScenario scenario, out 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 T Create<T>(Parameter typeParameter) {
|
---|
23 | var type = Type.GetType((typeParameter.Value as TypeValue).Value);
|
---|
24 | return (T)Activator.CreateInstance(type, null);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public static Permutation ConvertToPermutation(Parameter parameter) {
|
---|
28 | double[] input = (parameter.Value as DecimalVector).Value;
|
---|
29 | int[] arr = new int[input.Length];
|
---|
30 | for (int i = 0; i < input.Length; i++) {
|
---|
31 | arr[i] = Convert.ToInt32(input[i]);
|
---|
32 | }
|
---|
33 | var valueObj = new HeuristicLab.Encodings.PermutationEncoding.Permutation(Encodings.PermutationEncoding.PermutationTypes.RelativeUndirected, arr);
|
---|
34 | return valueObj;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static DoubleMatrix ConvertToDoubleMatrix(Parameter parameter) {
|
---|
38 | var matrix = parameter.Value as DecimalMatrix;
|
---|
39 | // TODO: Check for empty matrices
|
---|
40 | double[,] data = new double[matrix.Value.Length, matrix.Value[0].Length];
|
---|
41 | for (int i=0; i < matrix.Value.Length; i++) {
|
---|
42 | for (int j=0; j < matrix.Value[i].Length; j++) {
|
---|
43 | data[i,j] = matrix.Value[i][j];
|
---|
44 | }
|
---|
45 | }
|
---|
46 | return new DoubleMatrix(data);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static HeuristicLab.Data.BoolValue ConvertToBoolValue(Parameter parameter) {
|
---|
50 | return new HeuristicLab.Data.BoolValue((parameter.Value as HeuristicLab.Services.Optimization.ControllerService.Model.BoolValue).Value);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static IntValue ConvertToIntValue(Parameter parameter) {
|
---|
54 | return new IntValue((int)(parameter.Value as DecimalValue).Value);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static PercentValue ConvertToPercentValue(Parameter parameter) {
|
---|
58 | return new PercentValue((parameter.Value as DecimalValue).Value / 100);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static string GetStringValue(Parameter parameter) {
|
---|
62 | return (parameter.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static T FindInItemSet<T>(Core.IItemSet<T> items, Parameter what) where T : class,Core.INamedItem {
|
---|
66 | return items.FirstOrDefault((entry) => { return entry.Name == (what.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value; });
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static T FindOperator<T>(IProblem problem, Parameter what) where T : IOperator {
|
---|
70 | return problem.Operators.OfType<T>().FirstOrDefault((item) => { return item.ItemName == (what.Value as HeuristicLab.Services.Optimization.ControllerService.Model.TypeValue).Value; });
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|