[12945] | 1 | #pragma warning disable 436
|
---|
| 2 |
|
---|
[11564] | 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 |
|
---|
[11823] | 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[11564] | 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;
|
---|
[11823] | 32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[11577] | 33 | using HeuristicLab.Networks.Programmable;
|
---|
[11564] | 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;
|
---|
[11823] | 39 | using HeuristicLab.Selection;
|
---|
[11564] | 40 |
|
---|
[11577] | 41 | namespace HeuristicLab.Networks {
|
---|
[11823] | 42 | [Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")]
|
---|
| 43 | public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
|
---|
[11564] | 44 | public static new Image StaticItemImage {
|
---|
| 45 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[11823] | 48 | protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { }
|
---|
| 49 | public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context)
|
---|
[11564] | 50 | : base(context) {
|
---|
| 51 | if (Nodes.Count == 0)
|
---|
| 52 | Initialize();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[11823] | 56 | return new CompiledKSPTSPConcurrentControlNetwork(this, cloner);
|
---|
[11564] | 57 | }
|
---|
| 58 |
|
---|
[11823] | 59 | public void Initialize() {
|
---|
[11564] | 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 |
|
---|
[11823] | 67 | paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
|
---|
[11564] | 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 |
|
---|
[11823] | 95 | #region ControlNode
|
---|
| 96 | var controlNode = new KSPTSPControl("KSPTSP Control");
|
---|
| 97 | Nodes.Add(controlNode);
|
---|
| 98 | #endregion
|
---|
| 99 |
|
---|
[11564] | 100 | #region KSPNode
|
---|
| 101 | var kspNode = new AlgorithmNode("KSPNode");
|
---|
| 102 | Nodes.Add(kspNode);
|
---|
| 103 |
|
---|
[11823] | 104 | var kspConfigPort = new ConfigurationPort("ConfigureKSP");
|
---|
| 105 | kspNode.Ports.Add(kspConfigPort);
|
---|
[11564] | 106 |
|
---|
[11823] | 107 | kspConfigPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
|
---|
[11564] | 108 | Type = PortParameterType.Input
|
---|
| 109 | });
|
---|
[11823] | 110 | kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Values") {
|
---|
[11564] | 111 | Type = PortParameterType.Input
|
---|
| 112 | });
|
---|
[11823] | 113 | kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
|
---|
[11564] | 114 | Type = PortParameterType.Input
|
---|
| 115 | });
|
---|
| 116 |
|
---|
[11823] | 117 | var kspControlPort = new MessagePort("ControlPort");
|
---|
| 118 | kspNode.Ports.Add(kspControlPort);
|
---|
[11564] | 119 |
|
---|
[11823] | 120 | kspControlPort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
|
---|
[11564] | 121 | Type = PortParameterType.Output
|
---|
| 122 | });
|
---|
[11823] | 123 | kspControlPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
|
---|
[11564] | 124 | Type = PortParameterType.Input
|
---|
| 125 | });
|
---|
[11823] | 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
|
---|
[11564] | 132 | });
|
---|
| 133 |
|
---|
[11823] | 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;
|
---|
[11564] | 143 |
|
---|
[11823] | 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);
|
---|
[11564] | 152 | #endregion
|
---|
| 153 |
|
---|
| 154 | #region TSPNode
|
---|
| 155 | var tspNode = new AlgorithmNode("TSPNode");
|
---|
| 156 | Nodes.Add(tspNode);
|
---|
| 157 |
|
---|
[11823] | 158 | var tspConfigPort = new ConfigurationPort("ConfigureTSP");
|
---|
| 159 | tspNode.Ports.Add(tspConfigPort);
|
---|
[11564] | 160 |
|
---|
[11823] | 161 | tspConfigPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
|
---|
[11564] | 162 | Type = PortParameterType.Input
|
---|
| 163 | });
|
---|
[11823] | 164 |
|
---|
| 165 | var tspControlPort = new MessagePort("ControlPort");
|
---|
| 166 | tspNode.Ports.Add(tspControlPort);
|
---|
| 167 |
|
---|
| 168 | tspControlPort.Parameters.Add(new PortParameter<Permutation>("TSPTour") {
|
---|
[11564] | 169 | Type = PortParameterType.Output
|
---|
| 170 | });
|
---|
[11823] | 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") {
|
---|
[11564] | 179 | Type = PortParameterType.Output
|
---|
| 180 | });
|
---|
| 181 |
|
---|
[11823] | 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;
|
---|
[11564] | 193 |
|
---|
[11823] | 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);
|
---|
[11564] | 202 | #endregion
|
---|
| 203 |
|
---|
[11823] | 204 |
|
---|
[11564] | 205 | #region Wire Ports
|
---|
[11823] | 206 | ((ConfigurationPort)((INode)controlNode).Ports["Configure"]).ConnectedPort = paramsPort;
|
---|
| 207 | kspConfigPort.ConnectedPort = paramsPort;
|
---|
| 208 | tspConfigPort.ConnectedPort = paramsPort;
|
---|
[11564] | 209 |
|
---|
[11823] | 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 |
|
---|
[11564] | 221 | paramsPort.SendMessage(paramsPort.PrepareMessage());
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|