Changeset 11564 for branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs
- Timestamp:
- 11/23/14 05:32:41 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs
r11530 r11564 20 20 #endregion 21 21 22 using HeuristicLab.Algorithms.GeneticAlgorithm;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 24 using HeuristicLab.Core.Networks; 26 using HeuristicLab.Data;27 using HeuristicLab.Encodings.BinaryVectorEncoding;28 using HeuristicLab.Operators;29 using HeuristicLab.Parameters;30 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.Knapsack; 32 using HeuristicLab.Problems.TravelingSalesman; 33 using System.Linq; 26 using System.Drawing; 34 27 35 28 namespace HeuristicLab.Optimization.Networks.KSPTSP { … … 37 30 [Creatable("Optimization Networks")] 38 31 [StorableClass] 39 public class KSPTSPNetwork : Network { 32 public class KSPTSPNetwork : ProgrammableNetwork { 33 public static new Image StaticItemImage { 34 get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; } 35 } 36 37 protected override string CodeTemplate { 38 get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Optimization.Networks.KSPTSP.KSPTSPNetworkCode.cs"); } 39 } 40 40 41 [StorableConstructor] 41 42 protected KSPTSPNetwork(bool deserializing) : base(deserializing) { } 42 43 protected KSPTSPNetwork(KSPTSPNetwork original, Cloner cloner) : base(original, cloner) { } 43 public KSPTSPNetwork() 44 : base("KSPTSPNetwork") { 45 Initialize(); 46 } 47 public KSPTSPNetwork(string name) 48 : base(name) { 49 Initialize(); 50 } 51 public KSPTSPNetwork(string name, string description) 52 : base(name, description) { 53 Initialize(); 54 } 44 public KSPTSPNetwork() : base("KSPTSPNetwork") { } 45 public KSPTSPNetwork(string name) : base(name) { } 46 public KSPTSPNetwork(string name, string description) : base(name, description) { } 55 47 56 48 public override IDeepCloneable Clone(Cloner cloner) { 57 49 return new KSPTSPNetwork(this, cloner); 58 50 } 59 60 protected virtual void Initialize() {61 #region ParametersNode62 var paramsNode = new UserDefinedNode("ParametersNode");63 Nodes.Add(paramsNode);64 65 var paramsPort = new MessagePort("Parameters");66 paramsNode.Ports.Add(paramsPort);67 68 paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {69 Type = PortParameterType.Output,70 DefaultValue = new DoubleMatrix(new double[,] {71 {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},72 {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},73 {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},74 {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},75 {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}76 })77 });78 paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {79 Type = PortParameterType.Output,80 DefaultValue = new DoubleValue(0.1)81 });82 paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {83 Type = PortParameterType.Output,84 DefaultValue = new IntValue(10)85 });86 paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {87 Type = PortParameterType.Output,88 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 })89 });90 paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {91 Type = PortParameterType.Output,92 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 })93 });94 #endregion95 96 #region KSPNode97 var kspNode = new AlgorithmNode("KSPNode");98 Nodes.Add(kspNode);99 100 var configPort = new ConfigurationPort("ConfigureKSP");101 kspNode.Ports.Add(configPort);102 103 configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {104 Type = PortParameterType.Input105 });106 configPort.Parameters.Add(new PortParameter<IntArray>("Values") {107 Type = PortParameterType.Input108 });109 configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {110 Type = PortParameterType.Input111 });112 113 var evaluatePort = new MessagePort("EvaluateRoute");114 kspNode.Ports.Add(evaluatePort);115 116 evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {117 Type = PortParameterType.Output118 });119 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {120 Type = PortParameterType.Output | PortParameterType.Input121 });122 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {123 Type = PortParameterType.Input124 });125 evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {126 Type = PortParameterType.Input127 });128 129 var kspGA = new GeneticAlgorithm();130 kspGA.Problem = new KnapsackProblem();131 kspGA.MaximumGenerations.Value = 50;132 kspGA.PopulationSize.Value = 10;133 kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();134 kspNode.Algorithm = kspGA;135 136 var hook = new HookOperator();137 hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });138 hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });139 hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });140 hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });141 ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);142 #endregion143 144 #region TSPNode145 var tspNode = new AlgorithmNode("TSPNode");146 Nodes.Add(tspNode);147 148 var executePort = new ExecutionPort("Execute");149 tspNode.Ports.Add(executePort);150 151 executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {152 Type = PortParameterType.Input153 });154 executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {155 Type = PortParameterType.Output156 });157 executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {158 Type = PortParameterType.Output159 });160 161 var tspGA = new GeneticAlgorithm();162 tspGA.Problem = new TravelingSalesmanProblem();163 tspGA.MaximumGenerations.Value = 100;164 tspGA.PopulationSize.Value = 50;165 tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();166 tspNode.Algorithm = tspGA;167 #endregion168 169 #region KSPTSPConnector170 var ksptspConnector = (INode)new KSPTSPConnector();171 Nodes.Add(ksptspConnector);172 #endregion173 174 #region Wire Ports175 hook.Port = evaluatePort;176 configPort.ConnectedPort = paramsPort;177 evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];178 ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;179 ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;180 181 paramsPort.SendMessage(paramsPort.PrepareMessage());182 #endregion183 }184 51 } 185 52 }
Note: See TracChangeset
for help on using the changeset viewer.