Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11823 was 11823, checked in by abeham, 9 years ago

#2205:

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