1 | from HeuristicLab.Data import DoubleValue, DoubleMatrix, BoolValue, IntValue, PercentValue
|
---|
2 | from HeuristicLab.Encodings.PermutationEncoding import Permutation, PermutationTypes
|
---|
3 | from HeuristicLab.Problems.TravelingSalesman import TSPRoundedEuclideanPathEvaluator, TSPGeoPathEvaluator, TSPEuclideanPathEvaluator
|
---|
4 | from System import Array
|
---|
5 |
|
---|
6 | class 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 |
|
---|
62 | mapper = HeuristicLabMapper()
|
---|
63 | mapper.UseParameters(source.InputParameters)
|
---|
64 | ###### PROBLEM PARAMETERS ######
|
---|
65 | problem.BestKnownQuality = mapper.MapDoubleValue("BestKnownQuality")
|
---|
66 | problem.BestKnownSolution = mapper.MapPermutation("BestKnownSolution")
|
---|
67 | problem.Coordinates = mapper.MapDoubleMatrix("Coordinates")
|
---|
68 | evaluatorParameter = mapper.GetStringValue("EvaluatorParameter")
|
---|
69 | if (evaluatorParameter == "HeuristicLab.Problems.TravelingSalesman.TSPRoundedEuclideanPathEvaluator"):
|
---|
70 | problem.EvaluatorParameter.Value = TSPRoundedEuclideanPathEvaluator()
|
---|
71 | elif (evaluatorParameter == "HeuristicLab.Problems.TravelingSalesman.TSPGeoPathEvaluator"):
|
---|
72 | problem.EvaluatorParameter.Value = TSPGeoPathEvaluator()
|
---|
73 | else:
|
---|
74 | problem.EvaluatorParameter.Value = TSPEuclideanPathEvaluator()
|
---|
75 | problem.UseDistanceMatrix = mapper.MapBoolValue("UseDistanceMatrix")
|
---|
76 |
|
---|
77 | ###### ALGORITHM PARAMETERS ######
|
---|
78 | mapper.UseParameters(source.AlgorithmParameters)
|
---|
79 | # CrossoverParameter - string
|
---|
80 | algorithm.CrossoverParameter.Value = mapper.FindOperator("CrossoverParameter")
|
---|
81 | algorithm.Elites = mapper.MapIntValue("Elites")
|
---|
82 | algorithm.MaximumGenerations = mapper.MapIntValue("MaximumGenerations")
|
---|
83 | algorithm.MutationProbability = mapper.MapPercentValue("MutationProbability")
|
---|
84 | algorithm.Mutator = mapper.FindIn("Mutator", algorithm.MutatorParameter.ValidValues)
|
---|
85 | algorithm.PopulationSize = mapper.MapIntValue("PopulationSize")
|
---|
86 | algorithm.Seed = mapper.MapIntValue("Seed")
|
---|
87 | algorithm.Selector = mapper.FindIn("Selector", algorithm.SelectorParameter.ValidValues)
|
---|
88 | algorithm.SetSeedRandomly = mapper.MapBoolValue("SetSeedRandomly")
|
---|