Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPConcurrentControlNetworkCode.cs @ 12327

Last change on this file since 12327 was 11823, checked in by abeham, 10 years ago

#2205:

  • Added cosolving KSPTSP network
  • Fixed output path in projects for release target
  • Fixed penalty in seqsolving KSPTSP network (would produce infeasible solutions)
  • Added some additional references
File size: 9.5 KB
Line 
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
22using System.Drawing;
23using System.Linq;
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Networks;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Networks.Programmable;
32using HeuristicLab.Operators;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.Problems.Knapsack;
36using HeuristicLab.Problems.TravelingSalesman;
37using HeuristicLab.Selection;
38
39namespace HeuristicLab.Networks {
40  [Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")]
41  public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
42    public static new Image StaticItemImage {
43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
44    }
45
46    protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { }
47    public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context)
48      : base(context) {
49      if (Nodes.Count == 0)
50        Initialize();
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new CompiledKSPTSPConcurrentControlNetwork(this, cloner);
55    }
56
57    public void Initialize() {
58      #region ParametersNode
59      var paramsNode = new UserDefinedNode("ParametersNode");
60      Nodes.Add(paramsNode);
61
62      var paramsPort = new MessagePort("Parameters");
63      paramsNode.Ports.Add(paramsPort);
64
65      paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
66        Type = PortParameterType.Output,
67        DefaultValue = new DoubleMatrix(new double[,] {
68          {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
69          {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
70          {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
71          {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
72          {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
73        })
74      });
75      paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
76        Type = PortParameterType.Output,
77        DefaultValue = new DoubleValue(0.1)
78      });
79      paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
80        Type = PortParameterType.Output,
81        DefaultValue = new IntValue(10)
82      });
83      paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
84        Type = PortParameterType.Output,
85        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 })
86      });
87      paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
88        Type = PortParameterType.Output,
89        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 })
90      });
91      #endregion
92
93      #region ControlNode
94      var controlNode = new KSPTSPControl("KSPTSP Control");
95      Nodes.Add(controlNode);
96      #endregion
97
98      #region KSPNode
99      var kspNode = new AlgorithmNode("KSPNode");
100      Nodes.Add(kspNode);
101
102      var kspConfigPort = new ConfigurationPort("ConfigureKSP");
103      kspNode.Ports.Add(kspConfigPort);
104
105      kspConfigPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
106        Type = PortParameterType.Input
107      });
108      kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Values") {
109        Type = PortParameterType.Input
110      });
111      kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
112        Type = PortParameterType.Input
113      });
114
115      var kspControlPort = new MessagePort("ControlPort");
116      kspNode.Ports.Add(kspControlPort);
117
118      kspControlPort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
119        Type = PortParameterType.Output
120      });
121      kspControlPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
122        Type = PortParameterType.Input
123      });
124
125      var kspReportPort = new MessagePort("ReportPort");
126      kspNode.Ports.Add(kspReportPort);
127
128      kspReportPort.Parameters.Add(new PortParameter<KnapsackSolution>("BestSolution") {
129        Type = PortParameterType.Output
130      });
131
132      var kspGa = new GeneticAlgorithm {
133        Problem = new KnapsackProblem(),
134        MaximumGenerations = { Value = 500 },
135        PopulationSize = { Value = 200 }
136      };
137      kspGa.Mutator = kspGa.MutatorParameter.ValidValues.First(x => x.Name == "SinglePositionBitflipManipulator");
138      kspGa.Crossover = kspGa.CrossoverParameter.ValidValues.First(x => x.Name == "SinglePointCrossover");
139      kspGa.Selector = kspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
140      kspNode.Algorithm = kspGa;
141
142      var kspEvalHook = new HookOperator() { Name = "KSP Eval Hook" };
143      kspEvalHook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
144      kspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
145      ((InstrumentedOperator)kspGa.Problem.Evaluator).AfterExecutionOperators.Add(kspEvalHook);
146
147      var kspIterHook = new HookOperator() { Name = "KSP Iter Hook" };
148      kspIterHook.Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution") { Hidden = false });
149      kspGa.Analyzer.AfterExecutionOperators.Add(kspIterHook);
150      #endregion
151
152      #region TSPNode
153      var tspNode = new AlgorithmNode("TSPNode");
154      Nodes.Add(tspNode);
155
156      var tspConfigPort = new ConfigurationPort("ConfigureTSP");
157      tspNode.Ports.Add(tspConfigPort);
158
159      tspConfigPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
160        Type = PortParameterType.Input
161      });
162
163      var tspControlPort = new MessagePort("ControlPort");
164      tspNode.Ports.Add(tspControlPort);
165
166      tspControlPort.Parameters.Add(new PortParameter<Permutation>("TSPTour") {
167        Type = PortParameterType.Output
168      });
169      tspControlPort.Parameters.Add(new PortParameter<DoubleValue>("TSPTourLength") {
170        Type = PortParameterType.Input
171      });
172
173      var tspReportPort = new MessagePort("ReportPort");
174      tspNode.Ports.Add(tspReportPort);
175
176      tspReportPort.Parameters.Add(new PortParameter<PathTSPTour>("BestSolution") {
177        Type = PortParameterType.Output
178      });
179
180      var tspGa = new GeneticAlgorithm {
181        Problem = new TravelingSalesmanProblem(),
182        MaximumGenerations = { Value = 500 },
183        PopulationSize = { Value = 200 }
184      };
185      ((IValueParameter<BoolValue>)tspGa.Problem.MaximizationParameter).Value.Value = true;
186      tspGa.Crossover = tspGa.CrossoverParameter.ValidValues.First(x => x.Name == "EdgeRecombinationCrossover");
187      tspGa.Mutator = tspGa.MutatorParameter.ValidValues.First(x => x.Name == "InversionManipulator");
188      tspGa.Selector = tspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
189      ((TournamentSelector)tspGa.Selector).GroupSizeParameter.Value.Value = 3;
190      tspNode.Algorithm = tspGa;
191
192      var tspEvalHook = new HookOperator() { Name = "TSP Eval Hook" };
193      tspEvalHook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = false });
194      tspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = false });
195      ((InstrumentedOperator)tspGa.Problem.Evaluator).AfterExecutionOperators.Add(tspEvalHook);
196
197      var tspIterHook = new HookOperator() { Name = "TSP Iter Hook" };
198      tspIterHook.Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution") { Hidden = false });
199      tspGa.Analyzer.AfterExecutionOperators.Add(tspIterHook);
200      #endregion
201
202
203      #region Wire Ports
204      ((ConfigurationPort)((INode)controlNode).Ports["Configure"]).ConnectedPort = paramsPort;
205      kspConfigPort.ConnectedPort = paramsPort;
206      tspConfigPort.ConnectedPort = paramsPort;
207
208      kspEvalHook.Port = kspControlPort;
209      kspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate KSP"];
210      tspEvalHook.Port = tspControlPort;
211      tspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate TSP"];
212
213      kspIterHook.Port = kspReportPort;
214      kspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add KSP Solution"];
215      tspIterHook.Port = tspReportPort;
216      tspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add TSP Solution"];
217      #endregion
218
219      paramsPort.SendMessage(paramsPort.PrepareMessage());
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.