Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: Continued working on programmable network items

  • allowed code changes only in user-defined nodes and networks
  • added manual initialization of compiled 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      base.Initialize();
56
57      #region ParametersNode
58      var paramsNode = new UserDefinedNode("ParametersNode");
59      Nodes.Add(paramsNode);
60
61      var paramsPort = new MessagePort("Parameters");
62      paramsNode.Ports.Add(paramsPort);
63
64      paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {
65        Type = PortParameterType.Output,
66        DefaultValue = new DoubleMatrix(new double[,] {
67          {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
68          {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
69          {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
70          {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
71          {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
72        })
73      });
74      paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
75        Type = PortParameterType.Output,
76        DefaultValue = new DoubleValue(0.1)
77      });
78      paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
79        Type = PortParameterType.Output,
80        DefaultValue = new IntValue(10)
81      });
82      paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
83        Type = PortParameterType.Output,
84        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 })
85      });
86      paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
87        Type = PortParameterType.Output,
88        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 })
89      });
90      #endregion
91
92      #region KSPNode
93      var kspNode = new AlgorithmNode("KSPNode");
94      Nodes.Add(kspNode);
95
96      var configPort = new ConfigurationPort("ConfigureKSP");
97      kspNode.Ports.Add(configPort);
98
99      configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
100        Type = PortParameterType.Input
101      });
102      configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
103        Type = PortParameterType.Input
104      });
105      configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
106        Type = PortParameterType.Input
107      });
108
109      var evaluatePort = new MessagePort("EvaluateRoute");
110      kspNode.Ports.Add(evaluatePort);
111
112      evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
113        Type = PortParameterType.Output
114      });
115      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
116        Type = PortParameterType.Output | PortParameterType.Input
117      });
118      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
119        Type = PortParameterType.Input
120      });
121      evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
122        Type = PortParameterType.Input
123      });
124
125      var kspGA = new GeneticAlgorithm();
126      kspGA.Problem = new KnapsackProblem();
127      kspGA.MaximumGenerations.Value = 50;
128      kspGA.PopulationSize.Value = 10;
129      kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
130      kspNode.Algorithm = kspGA;
131
132      var hook = new HookOperator();
133      hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
134      hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
135      hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
136      hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
137      ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
138      #endregion
139
140      #region TSPNode
141      var tspNode = new AlgorithmNode("TSPNode");
142      Nodes.Add(tspNode);
143
144      var executePort = new ExecutionPort("Execute");
145      tspNode.Ports.Add(executePort);
146
147      executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
148        Type = PortParameterType.Input
149      });
150      executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
151        Type = PortParameterType.Output
152      });
153      executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
154        Type = PortParameterType.Output
155      });
156
157      var tspGA = new GeneticAlgorithm();
158      tspGA.Problem = new TravelingSalesmanProblem();
159      tspGA.MaximumGenerations.Value = 100;
160      tspGA.PopulationSize.Value = 50;
161      tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
162      tspNode.Algorithm = tspGA;
163      #endregion
164
165      #region KSPTSPConnector
166      var ksptspConnector = (INode)new KSPTSPConnector();
167      Nodes.Add(ksptspConnector);
168      #endregion
169
170      #region Wire Ports
171      hook.Port = evaluatePort;
172      configPort.ConnectedPort = paramsPort;
173      evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
174      ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
175      ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
176
177      paramsPort.SendMessage(paramsPort.PrepareMessage());
178      #endregion
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.