Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11682 was 11577, checked in by swagner, 10 years ago

#2205: Restructured solution and projects and switched all projects to .NET 4.5

File size: 7.3 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.Drawing;
35using System.Linq;
36
37namespace HeuristicLab.Networks {
38  [Item("KSPTSPNetwork", "An optimization network which connects a KSP and a TSP.")]
39  public class CompiledKSPTSPNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
40    public static new Image StaticItemImage {
41      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
42    }
43
44    protected CompiledKSPTSPNetwork(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
45    public CompiledKSPTSPNetwork(ProgrammableNetwork context)
46      : base(context) {
47      if (Nodes.Count == 0)
48        Initialize();
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new CompiledKSPTSPNetwork(this, cloner);
53    }
54
55    public override void Initialize() {
56      base.Initialize();
57
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>("Cities") {
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 KSPNode
94      var kspNode = new AlgorithmNode("KSPNode");
95      Nodes.Add(kspNode);
96
97      var configPort = new ConfigurationPort("ConfigureKSP");
98      kspNode.Ports.Add(configPort);
99
100      configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
101        Type = PortParameterType.Input
102      });
103      configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
104        Type = PortParameterType.Input
105      });
106      configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
107        Type = PortParameterType.Input
108      });
109
110      var evaluatePort = new MessagePort("EvaluateRoute");
111      kspNode.Ports.Add(evaluatePort);
112
113      evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
114        Type = PortParameterType.Output
115      });
116      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
117        Type = PortParameterType.Output | PortParameterType.Input
118      });
119      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
120        Type = PortParameterType.Input
121      });
122      evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
123        Type = PortParameterType.Input
124      });
125
126      var kspGA = new GeneticAlgorithm();
127      kspGA.Problem = new KnapsackProblem();
128      kspGA.MaximumGenerations.Value = 50;
129      kspGA.PopulationSize.Value = 10;
130      kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
131      kspNode.Algorithm = kspGA;
132
133      var hook = new HookOperator();
134      hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
135      hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
136      hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
137      hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
138      ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
139      #endregion
140
141      #region TSPNode
142      var tspNode = new AlgorithmNode("TSPNode");
143      Nodes.Add(tspNode);
144
145      var executePort = new ExecutionPort("Execute");
146      tspNode.Ports.Add(executePort);
147
148      executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
149        Type = PortParameterType.Input
150      });
151      executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
152        Type = PortParameterType.Output
153      });
154      executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
155        Type = PortParameterType.Output
156      });
157
158      var tspGA = new GeneticAlgorithm();
159      tspGA.Problem = new TravelingSalesmanProblem();
160      tspGA.MaximumGenerations.Value = 100;
161      tspGA.PopulationSize.Value = 50;
162      tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
163      tspNode.Algorithm = tspGA;
164      #endregion
165
166      #region KSPTSPConnector
167      var ksptspConnector = (INode)new KSPTSPConnector();
168      Nodes.Add(ksptspConnector);
169      #endregion
170
171      #region Wire Ports
172      hook.Port = evaluatePort;
173      configPort.ConnectedPort = paramsPort;
174      evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
175      ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
176      ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
177
178      paramsPort.SendMessage(paramsPort.PrepareMessage());
179      #endregion
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.