Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Mappings/mapping.py @ 14821

Last change on this file since 14821 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: 3.8 KB
Line 
1from HeuristicLab.Data import DoubleValue, DoubleMatrix, BoolValue, IntValue, PercentValue
2from HeuristicLab.Encodings.PermutationEncoding import Permutation, PermutationTypes
3from HeuristicLab.Problems.TravelingSalesman import TSPRoundedEuclideanPathEvaluator, TSPGeoPathEvaluator, TSPEuclideanPathEvaluator
4from System import Array
5
6class HeuristicLabMapper:
7  def __init__(self):
8    self.__params = source.InputParameters
9
10  def UseParameters(self, params):
11    self.__params = params
12
13  def MapDoubleValue(self, name):
14    return DoubleValue(self.__params.FindByName(name).Value.Value)
15
16  def MapPermutation(self, name):
17    entries = Array.CreateInstance(int, self.__params.FindByName(name).Value.Value.Length)
18    i = 0
19    for value in self.__params.FindByName(name).Value.Value:
20      entries[i] = int(value)
21      i = i + 1
22    return Permutation(PermutationTypes.RelativeDirected, entries) 
23
24  def MapDoubleMatrix(self, name):
25    matrixEntries = self.__params.FindByName(name).Value.Value;
26    result = Array.CreateInstance(float, matrixEntries.Length, matrixEntries[0].Length)
27    for i in range(matrixEntries.Length):   
28      for j in range(matrixEntries[i].Length):   
29        result[i, j] = matrixEntries[i][j]
30    return DoubleMatrix(result) 
31
32  def MapBoolValue(self, name):
33    return BoolValue(self.__params.FindByName(name).Value.Value)
34
35  def MapIntValue(self, name):
36    return IntValue(int(source.AlgorithmParameters.FindByName(name).Value.Value))
37
38  def MapPercentValue(self, name):
39    return PercentValue(self.__params.FindByName(name).Value.Value / 100)
40
41  def GetStringValue(self, name):
42    return self.__params.FindByName(name).Value.Value
43
44  def FindIn(self, name, lst):
45    param = self.__params.FindByName(name).Value.Value   
46    for operator in lst: 
47      if operator.Name == param:
48        return operator         
49    print "ERROR: Couldn't find " + name + " " + param
50    print "List: "
51    print lst   
52    return None
53
54  def FindOperator(self, name):
55    param = self.__params.FindByName(name).Value.Value   
56    for operator in problem.Operators: 
57      if operator.Name == param:
58        return operator     
59    return None
60
61
62mapper = HeuristicLabMapper()
63mapper.UseParameters(source.InputParameters)
64###### PROBLEM PARAMETERS ######
65problem.BestKnownQuality = mapper.MapDoubleValue("BestKnownQuality")
66problem.BestKnownSolution = mapper.MapPermutation("BestKnownSolution")
67problem.Coordinates = mapper.MapDoubleMatrix("Coordinates")
68evaluatorParameter = mapper.GetStringValue("EvaluatorParameter")
69if (evaluatorParameter == "HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"):
70  problem.EvaluatorParameter.Value = TSPRoundedEuclideanPathEvaluator()
71elif (evaluatorParameter == "HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"):
72  problem.EvaluatorParameter.Value = TSPGeoPathEvaluator()
73else:
74  problem.EvaluatorParameter.Value = TSPEuclideanPathEvaluator()
75problem.UseDistanceMatrix = mapper.MapBoolValue("UseDistanceMatrix")
76
77###### ALGORITHM PARAMETERS ######
78mapper.UseParameters(source.AlgorithmParameters)
79# CrossoverParameter - string
80algorithm.CrossoverParameter.Value = mapper.FindOperator("CrossoverParameter")
81algorithm.Elites = mapper.MapIntValue("Elites")
82algorithm.MaximumGenerations = mapper.MapIntValue("MaximumGenerations")
83algorithm.MutationProbability = mapper.MapPercentValue("MutationProbability")
84algorithm.Mutator = mapper.FindIn("Mutator", algorithm.MutatorParameter.ValidValues)
85algorithm.PopulationSize = mapper.MapIntValue("PopulationSize")
86algorithm.Seed = mapper.MapIntValue("Seed")
87algorithm.Selector = mapper.FindIn("Selector", algorithm.SelectorParameter.ValidValues)
88algorithm.SetSeedRandomly = mapper.MapBoolValue("SetSeedRandomly")
Note: See TracBrowser for help on using the repository browser.