1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Core.Networks;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.Knapsack;
|
---|
32 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
33 | using System.Linq;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Optimization.Networks.KSPTSP {
|
---|
36 | [Item("KSPTSPNetwork", "An optimization network which connects a KSP and a TSP.")]
|
---|
37 | [Creatable("Optimization Networks")]
|
---|
38 | [StorableClass]
|
---|
39 | public class KSPTSPNetwork : Network {
|
---|
40 | [StorableConstructor]
|
---|
41 | protected KSPTSPNetwork(bool deserializing) : base(deserializing) { }
|
---|
42 | protected KSPTSPNetwork(KSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
|
---|
43 | public KSPTSPNetwork()
|
---|
44 | : base("KSPTSPNetwork") {
|
---|
45 | Initialize();
|
---|
46 | }
|
---|
47 | public KSPTSPNetwork(string name)
|
---|
48 | : base(name) {
|
---|
49 | Initialize();
|
---|
50 | }
|
---|
51 | public KSPTSPNetwork(string name, string description)
|
---|
52 | : base(name, description) {
|
---|
53 | Initialize();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new KSPTSPNetwork(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected virtual void Initialize() {
|
---|
61 | #region ParametersNode
|
---|
62 | var paramsNode = new UserDefinedNode("ParametersNode");
|
---|
63 | Nodes.Add(paramsNode);
|
---|
64 |
|
---|
65 | var paramsPort = new MessagePort("Parameters");
|
---|
66 | paramsNode.Ports.Add(paramsPort);
|
---|
67 |
|
---|
68 | paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {
|
---|
69 | Type = PortParameterType.Output,
|
---|
70 | DefaultValue = new DoubleMatrix(new double[,] {
|
---|
71 | {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
|
---|
72 | {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
|
---|
73 | {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
|
---|
74 | {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
|
---|
75 | {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
|
---|
76 | })
|
---|
77 | });
|
---|
78 | paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
|
---|
79 | Type = PortParameterType.Output,
|
---|
80 | DefaultValue = new DoubleValue(0.1)
|
---|
81 | });
|
---|
82 | paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
|
---|
83 | Type = PortParameterType.Output,
|
---|
84 | DefaultValue = new IntValue(10)
|
---|
85 | });
|
---|
86 | paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
|
---|
87 | Type = PortParameterType.Output,
|
---|
88 | DefaultValue = new IntArray(new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 })
|
---|
89 | });
|
---|
90 | paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
|
---|
91 | Type = PortParameterType.Output,
|
---|
92 | DefaultValue = new IntArray(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 })
|
---|
93 | });
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | #region KSPNode
|
---|
97 | var kspNode = new AlgorithmNode("KSPNode");
|
---|
98 | Nodes.Add(kspNode);
|
---|
99 |
|
---|
100 | var configPort = new ConfigurationPort("ConfigureKSP");
|
---|
101 | kspNode.Ports.Add(configPort);
|
---|
102 |
|
---|
103 | configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
|
---|
104 | Type = PortParameterType.Input
|
---|
105 | });
|
---|
106 | configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
|
---|
107 | Type = PortParameterType.Input
|
---|
108 | });
|
---|
109 | configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
|
---|
110 | Type = PortParameterType.Input
|
---|
111 | });
|
---|
112 |
|
---|
113 | var evaluatePort = new MessagePort("EvaluateRoute");
|
---|
114 | kspNode.Ports.Add(evaluatePort);
|
---|
115 |
|
---|
116 | evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
|
---|
117 | Type = PortParameterType.Output
|
---|
118 | });
|
---|
119 | evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
|
---|
120 | Type = PortParameterType.Output | PortParameterType.Input
|
---|
121 | });
|
---|
122 | evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
|
---|
123 | Type = PortParameterType.Input
|
---|
124 | });
|
---|
125 | evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
|
---|
126 | Type = PortParameterType.Input
|
---|
127 | });
|
---|
128 |
|
---|
129 | var kspGA = new GeneticAlgorithm();
|
---|
130 | kspGA.Problem = new KnapsackProblem();
|
---|
131 | kspGA.MaximumGenerations.Value = 50;
|
---|
132 | kspGA.PopulationSize.Value = 10;
|
---|
133 | kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
|
---|
134 | kspNode.Algorithm = kspGA;
|
---|
135 |
|
---|
136 | var hook = new HookOperator();
|
---|
137 | hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
|
---|
138 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
|
---|
139 | hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
|
---|
140 | hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
|
---|
141 | ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
|
---|
142 | #endregion
|
---|
143 |
|
---|
144 | #region TSPNode
|
---|
145 | var tspNode = new AlgorithmNode("TSPNode");
|
---|
146 | Nodes.Add(tspNode);
|
---|
147 |
|
---|
148 | var executePort = new ExecutionPort("Execute");
|
---|
149 | tspNode.Ports.Add(executePort);
|
---|
150 |
|
---|
151 | executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
|
---|
152 | Type = PortParameterType.Input
|
---|
153 | });
|
---|
154 | executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
|
---|
155 | Type = PortParameterType.Output
|
---|
156 | });
|
---|
157 | executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
|
---|
158 | Type = PortParameterType.Output
|
---|
159 | });
|
---|
160 |
|
---|
161 | var tspGA = new GeneticAlgorithm();
|
---|
162 | tspGA.Problem = new TravelingSalesmanProblem();
|
---|
163 | tspGA.MaximumGenerations.Value = 100;
|
---|
164 | tspGA.PopulationSize.Value = 50;
|
---|
165 | tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
|
---|
166 | tspNode.Algorithm = tspGA;
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | #region KSPTSPConnector
|
---|
170 | var ksptspConnector = (INode)new KSPTSPConnector();
|
---|
171 | Nodes.Add(ksptspConnector);
|
---|
172 | #endregion
|
---|
173 |
|
---|
174 | #region Wire Ports
|
---|
175 | hook.Port = evaluatePort;
|
---|
176 | configPort.ConnectedPort = paramsPort;
|
---|
177 | evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
|
---|
178 | ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
|
---|
179 | ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
|
---|
180 |
|
---|
181 | paramsPort.SendMessage(paramsPort.PrepareMessage());
|
---|
182 | #endregion
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|