Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Nodes/KSPTSPNetwork.cs @ 11520

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

#2205: Worked on optimization networks

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