Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetworkCode.cs @ 11564

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

#2205: Continued working on programmable network items

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