Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPNetworkCode.cs @ 11712

Last change on this file since 11712 was 11712, checked in by swagner, 9 years ago

#2205: Implemented review comments:

  • checked and adapted item images
File size: 7.2 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 HeuristicLab.Algorithms.GeneticAlgorithm;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Networks;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Networks.Programmable;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.Knapsack;
33using HeuristicLab.Problems.TravelingSalesman;
34using System.Linq;
35
36namespace HeuristicLab.Networks {
37  [Item("KSPTSPNetwork", "An optimization network which connects a KSP and a TSP.")]
38  public class CompiledKSPTSPNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
39    protected CompiledKSPTSPNetwork(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
40    public CompiledKSPTSPNetwork(ProgrammableNetwork context)
41      : base(context) {
42      if (Nodes.Count == 0)
43        Initialize();
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new CompiledKSPTSPNetwork(this, cloner);
48    }
49
50    public override void Initialize() {
51      base.Initialize();
52
53      #region ParametersNode
54      var paramsNode = new UserDefinedNode("ParametersNode");
55      Nodes.Add(paramsNode);
56
57      var paramsPort = new MessagePort("Parameters");
58      paramsNode.Ports.Add(paramsPort);
59
60      paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {
61        Type = PortParameterType.Output,
62        DefaultValue = new DoubleMatrix(new double[,] {
63          {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
64          {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
65          {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
66          {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
67          {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
68        })
69      });
70      paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
71        Type = PortParameterType.Output,
72        DefaultValue = new DoubleValue(0.1)
73      });
74      paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
75        Type = PortParameterType.Output,
76        DefaultValue = new IntValue(10)
77      });
78      paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
79        Type = PortParameterType.Output,
80        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 })
81      });
82      paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
83        Type = PortParameterType.Output,
84        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 })
85      });
86      #endregion
87
88      #region KSPNode
89      var kspNode = new AlgorithmNode("KSPNode");
90      Nodes.Add(kspNode);
91
92      var configPort = new ConfigurationPort("ConfigureKSP");
93      kspNode.Ports.Add(configPort);
94
95      configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
96        Type = PortParameterType.Input
97      });
98      configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
99        Type = PortParameterType.Input
100      });
101      configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
102        Type = PortParameterType.Input
103      });
104
105      var evaluatePort = new MessagePort("EvaluateRoute");
106      kspNode.Ports.Add(evaluatePort);
107
108      evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
109        Type = PortParameterType.Output
110      });
111      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
112        Type = PortParameterType.Output | PortParameterType.Input
113      });
114      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
115        Type = PortParameterType.Input
116      });
117      evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
118        Type = PortParameterType.Input
119      });
120
121      var kspGA = new GeneticAlgorithm();
122      kspGA.Problem = new KnapsackProblem();
123      kspGA.MaximumGenerations.Value = 50;
124      kspGA.PopulationSize.Value = 10;
125      kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
126      kspNode.Algorithm = kspGA;
127
128      var hook = new HookOperator();
129      hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
130      hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
131      hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
132      hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
133      ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
134      #endregion
135
136      #region TSPNode
137      var tspNode = new AlgorithmNode("TSPNode");
138      Nodes.Add(tspNode);
139
140      var executePort = new ExecutionPort("Execute");
141      tspNode.Ports.Add(executePort);
142
143      executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
144        Type = PortParameterType.Input
145      });
146      executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
147        Type = PortParameterType.Output
148      });
149      executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
150        Type = PortParameterType.Output
151      });
152
153      var tspGA = new GeneticAlgorithm();
154      tspGA.Problem = new TravelingSalesmanProblem();
155      tspGA.MaximumGenerations.Value = 100;
156      tspGA.PopulationSize.Value = 50;
157      tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
158      tspNode.Algorithm = tspGA;
159      #endregion
160
161      #region KSPTSPConnector
162      var ksptspConnector = (INode)new KSPTSPConnector();
163      Nodes.Add(ksptspConnector);
164      #endregion
165
166      #region Wire Ports
167      hook.Port = evaluatePort;
168      configPort.ConnectedPort = paramsPort;
169      evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
170      ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
171      ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
172
173      paramsPort.SendMessage(paramsPort.PrepareMessage());
174      #endregion
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.