1 | #pragma warning disable 436
|
---|
2 |
|
---|
3 | #region License Information
|
---|
4 | /* HeuristicLab
|
---|
5 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
6 | *
|
---|
7 | * This file is part of HeuristicLab.
|
---|
8 | *
|
---|
9 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 3 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Core.Networks;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
33 | using HeuristicLab.Networks.Programmable;
|
---|
34 | using HeuristicLab.Operators;
|
---|
35 | using HeuristicLab.Parameters;
|
---|
36 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
37 | using HeuristicLab.Problems.Knapsack;
|
---|
38 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
39 | using HeuristicLab.Selection;
|
---|
40 |
|
---|
41 | namespace HeuristicLab.Networks {
|
---|
42 | [Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")]
|
---|
43 | public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
|
---|
44 | public static new Image StaticItemImage {
|
---|
45 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context)
|
---|
50 | : base(context) {
|
---|
51 | if (Nodes.Count == 0)
|
---|
52 | Initialize();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new CompiledKSPTSPConcurrentControlNetwork(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void Initialize() {
|
---|
60 | #region ParametersNode
|
---|
61 | var paramsNode = new UserDefinedNode("ParametersNode");
|
---|
62 | Nodes.Add(paramsNode);
|
---|
63 |
|
---|
64 | var paramsPort = new MessagePort("Parameters");
|
---|
65 | paramsNode.Ports.Add(paramsPort);
|
---|
66 |
|
---|
67 | paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
|
---|
68 | Type = PortParameterType.Output,
|
---|
69 | DefaultValue = new DoubleMatrix(new double[,] {
|
---|
70 | {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
|
---|
71 | {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
|
---|
72 | {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
|
---|
73 | {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
|
---|
74 | {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
|
---|
75 | })
|
---|
76 | });
|
---|
77 | paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
|
---|
78 | Type = PortParameterType.Output,
|
---|
79 | DefaultValue = new DoubleValue(0.1)
|
---|
80 | });
|
---|
81 | paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
|
---|
82 | Type = PortParameterType.Output,
|
---|
83 | DefaultValue = new IntValue(10)
|
---|
84 | });
|
---|
85 | paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
|
---|
86 | Type = PortParameterType.Output,
|
---|
87 | 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 })
|
---|
88 | });
|
---|
89 | paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
|
---|
90 | Type = PortParameterType.Output,
|
---|
91 | 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 })
|
---|
92 | });
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | #region ControlNode
|
---|
96 | var controlNode = new KSPTSPControl("KSPTSP Control");
|
---|
97 | Nodes.Add(controlNode);
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region KSPNode
|
---|
101 | var kspNode = new AlgorithmNode("KSPNode");
|
---|
102 | Nodes.Add(kspNode);
|
---|
103 |
|
---|
104 | var kspConfigPort = new ConfigurationPort("ConfigureKSP");
|
---|
105 | kspNode.Ports.Add(kspConfigPort);
|
---|
106 |
|
---|
107 | kspConfigPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
|
---|
108 | Type = PortParameterType.Input
|
---|
109 | });
|
---|
110 | kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Values") {
|
---|
111 | Type = PortParameterType.Input
|
---|
112 | });
|
---|
113 | kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
|
---|
114 | Type = PortParameterType.Input
|
---|
115 | });
|
---|
116 |
|
---|
117 | var kspControlPort = new MessagePort("ControlPort");
|
---|
118 | kspNode.Ports.Add(kspControlPort);
|
---|
119 |
|
---|
120 | kspControlPort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
|
---|
121 | Type = PortParameterType.Output
|
---|
122 | });
|
---|
123 | kspControlPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
|
---|
124 | Type = PortParameterType.Input
|
---|
125 | });
|
---|
126 |
|
---|
127 | var kspReportPort = new MessagePort("ReportPort");
|
---|
128 | kspNode.Ports.Add(kspReportPort);
|
---|
129 |
|
---|
130 | kspReportPort.Parameters.Add(new PortParameter<KnapsackSolution>("BestSolution") {
|
---|
131 | Type = PortParameterType.Output
|
---|
132 | });
|
---|
133 |
|
---|
134 | var kspGa = new GeneticAlgorithm {
|
---|
135 | Problem = new KnapsackProblem(),
|
---|
136 | MaximumGenerations = { Value = 500 },
|
---|
137 | PopulationSize = { Value = 200 }
|
---|
138 | };
|
---|
139 | kspGa.Mutator = kspGa.MutatorParameter.ValidValues.First(x => x.Name == "SinglePositionBitflipManipulator");
|
---|
140 | kspGa.Crossover = kspGa.CrossoverParameter.ValidValues.First(x => x.Name == "SinglePointCrossover");
|
---|
141 | kspGa.Selector = kspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
|
---|
142 | kspNode.Algorithm = kspGa;
|
---|
143 |
|
---|
144 | var kspEvalHook = new HookOperator() { Name = "KSP Eval Hook" };
|
---|
145 | kspEvalHook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
|
---|
146 | kspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
|
---|
147 | ((InstrumentedOperator)kspGa.Problem.Evaluator).AfterExecutionOperators.Add(kspEvalHook);
|
---|
148 |
|
---|
149 | var kspIterHook = new HookOperator() { Name = "KSP Iter Hook" };
|
---|
150 | kspIterHook.Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution") { Hidden = false });
|
---|
151 | kspGa.Analyzer.AfterExecutionOperators.Add(kspIterHook);
|
---|
152 | #endregion
|
---|
153 |
|
---|
154 | #region TSPNode
|
---|
155 | var tspNode = new AlgorithmNode("TSPNode");
|
---|
156 | Nodes.Add(tspNode);
|
---|
157 |
|
---|
158 | var tspConfigPort = new ConfigurationPort("ConfigureTSP");
|
---|
159 | tspNode.Ports.Add(tspConfigPort);
|
---|
160 |
|
---|
161 | tspConfigPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
|
---|
162 | Type = PortParameterType.Input
|
---|
163 | });
|
---|
164 |
|
---|
165 | var tspControlPort = new MessagePort("ControlPort");
|
---|
166 | tspNode.Ports.Add(tspControlPort);
|
---|
167 |
|
---|
168 | tspControlPort.Parameters.Add(new PortParameter<Permutation>("TSPTour") {
|
---|
169 | Type = PortParameterType.Output
|
---|
170 | });
|
---|
171 | tspControlPort.Parameters.Add(new PortParameter<DoubleValue>("TSPTourLength") {
|
---|
172 | Type = PortParameterType.Input
|
---|
173 | });
|
---|
174 |
|
---|
175 | var tspReportPort = new MessagePort("ReportPort");
|
---|
176 | tspNode.Ports.Add(tspReportPort);
|
---|
177 |
|
---|
178 | tspReportPort.Parameters.Add(new PortParameter<PathTSPTour>("BestSolution") {
|
---|
179 | Type = PortParameterType.Output
|
---|
180 | });
|
---|
181 |
|
---|
182 | var tspGa = new GeneticAlgorithm {
|
---|
183 | Problem = new TravelingSalesmanProblem(),
|
---|
184 | MaximumGenerations = { Value = 500 },
|
---|
185 | PopulationSize = { Value = 200 }
|
---|
186 | };
|
---|
187 | ((IValueParameter<BoolValue>)tspGa.Problem.MaximizationParameter).Value.Value = true;
|
---|
188 | tspGa.Crossover = tspGa.CrossoverParameter.ValidValues.First(x => x.Name == "EdgeRecombinationCrossover");
|
---|
189 | tspGa.Mutator = tspGa.MutatorParameter.ValidValues.First(x => x.Name == "InversionManipulator");
|
---|
190 | tspGa.Selector = tspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
|
---|
191 | ((TournamentSelector)tspGa.Selector).GroupSizeParameter.Value.Value = 3;
|
---|
192 | tspNode.Algorithm = tspGa;
|
---|
193 |
|
---|
194 | var tspEvalHook = new HookOperator() { Name = "TSP Eval Hook" };
|
---|
195 | tspEvalHook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = false });
|
---|
196 | tspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = false });
|
---|
197 | ((InstrumentedOperator)tspGa.Problem.Evaluator).AfterExecutionOperators.Add(tspEvalHook);
|
---|
198 |
|
---|
199 | var tspIterHook = new HookOperator() { Name = "TSP Iter Hook" };
|
---|
200 | tspIterHook.Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution") { Hidden = false });
|
---|
201 | tspGa.Analyzer.AfterExecutionOperators.Add(tspIterHook);
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 |
|
---|
205 | #region Wire Ports
|
---|
206 | ((ConfigurationPort)((INode)controlNode).Ports["Configure"]).ConnectedPort = paramsPort;
|
---|
207 | kspConfigPort.ConnectedPort = paramsPort;
|
---|
208 | tspConfigPort.ConnectedPort = paramsPort;
|
---|
209 |
|
---|
210 | kspEvalHook.Port = kspControlPort;
|
---|
211 | kspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate KSP"];
|
---|
212 | tspEvalHook.Port = tspControlPort;
|
---|
213 | tspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate TSP"];
|
---|
214 |
|
---|
215 | kspIterHook.Port = kspReportPort;
|
---|
216 | kspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add KSP Solution"];
|
---|
217 | tspIterHook.Port = tspReportPort;
|
---|
218 | tspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add TSP Solution"];
|
---|
219 | #endregion
|
---|
220 |
|
---|
221 | paramsPort.SendMessage(paramsPort.PrepareMessage());
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|