Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPNetworkCode.cs @ 16279

Last change on this file since 16279 was 12945, checked in by jkarder, 9 years ago

#2205: disabled compiler warning cs0436 for all code resources

File size: 7.3 KB
Line 
1#pragma warning disable 436
2
3#region License Information
4/* HeuristicLab
5 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
6 *
7 * This file is part of HeuristicLab.
8 *
9 * HeuristicLab is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * HeuristicLab is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
21 */
22#endregion
23
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Networks;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Networks.Programmable;
31using HeuristicLab.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.Knapsack;
35using HeuristicLab.Problems.TravelingSalesman;
36using System.Linq;
37
38namespace HeuristicLab.Networks {
39  [Item("KSPTSPNetwork", "An optimization network which contains a KSP and a TSP.")]
40  public class CompiledKSPTSPNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
41    protected CompiledKSPTSPNetwork(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
42    public CompiledKSPTSPNetwork(ProgrammableNetwork context)
43      : base(context) {
44      if (Nodes.Count == 0)
45        Initialize();
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new CompiledKSPTSPNetwork(this, cloner);
50    }
51
52    public override void Initialize() {
53      base.Initialize();
54
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      ((KnapsackProblem)kspGA.Problem).Penalty.Value = 100;
126      kspGA.MaximumGenerations.Value = 50;
127      kspGA.PopulationSize.Value = 10;
128      kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
129      kspNode.Algorithm = kspGA;
130
131      var hook = new HookOperator();
132      hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
133      hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
134      hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
135      hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
136      ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
137      #endregion
138
139      #region TSPNode
140      var tspNode = new AlgorithmNode("TSPNode");
141      Nodes.Add(tspNode);
142
143      var executePort = new ExecutionPort("Execute");
144      tspNode.Ports.Add(executePort);
145
146      executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
147        Type = PortParameterType.Input
148      });
149      executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
150        Type = PortParameterType.Output
151      });
152      executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
153        Type = PortParameterType.Output
154      });
155
156      var tspGA = new GeneticAlgorithm();
157      tspGA.Problem = new TravelingSalesmanProblem();
158      tspGA.MaximumGenerations.Value = 100;
159      tspGA.PopulationSize.Value = 50;
160      tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
161      tspNode.Algorithm = tspGA;
162      #endregion
163
164      #region KSPTSPConnector
165      var ksptspConnector = (INode)new KSPTSPConnector();
166      Nodes.Add(ksptspConnector);
167      #endregion
168
169      #region Wire Ports
170      hook.Port = evaluatePort;
171      configPort.ConnectedPort = paramsPort;
172      evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
173      ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
174      ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
175
176      paramsPort.SendMessage(paramsPort.PrepareMessage());
177      #endregion
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.