Changeset 11823 for branches/OptimizationNetworks
- Timestamp:
- 01/26/15 15:52:03 (10 years ago)
- Location:
- branches/OptimizationNetworks
- Files:
-
- 2 added
- 4 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Modeler/3.3/HeuristicLab.Modeler-3.3.csproj
r11577 r11823 26 26 <DebugType>pdbonly</DebugType> 27 27 <Optimize>true</Optimize> 28 <OutputPath> bin\Release\</OutputPath>28 <OutputPath>..\..\bin\</OutputPath> 29 29 <DefineConstants>TRACE</DefineConstants> 30 30 <ErrorReport>prompt</ErrorReport> … … 39 39 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> 40 40 <DebugSymbols>true</DebugSymbols> 41 <OutputPath> bin\x64\Debug\</OutputPath>41 <OutputPath>..\..\bin\</OutputPath> 42 42 <DefineConstants>DEBUG;TRACE</DefineConstants> 43 43 <DebugType>full</DebugType> … … 47 47 </PropertyGroup> 48 48 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> 49 <OutputPath> bin\x64\Release\</OutputPath>49 <OutputPath>..\..\bin\</OutputPath> 50 50 <DefineConstants>TRACE</DefineConstants> 51 51 <Optimize>true</Optimize> … … 57 57 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> 58 58 <DebugSymbols>true</DebugSymbols> 59 <OutputPath> bin\x86\Debug\</OutputPath>59 <OutputPath>..\..\bin\</OutputPath> 60 60 <DefineConstants>DEBUG;TRACE</DefineConstants> 61 61 <DebugType>full</DebugType> … … 65 65 </PropertyGroup> 66 66 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> 67 <OutputPath> bin\x86\Release\</OutputPath>67 <OutputPath>..\..\bin\</OutputPath> 68 68 <DefineConstants>TRACE</DefineConstants> 69 69 <Optimize>true</Optimize> -
branches/OptimizationNetworks/HeuristicLab.Networks.Views/3.3/HeuristicLab.Networks.Views-3.3.csproj
r11577 r11823 27 27 <DebugType>pdbonly</DebugType> 28 28 <Optimize>true</Optimize> 29 <OutputPath> bin\Release\</OutputPath>29 <OutputPath>..\..\bin\</OutputPath> 30 30 <DefineConstants>TRACE</DefineConstants> 31 31 <ErrorReport>prompt</ErrorReport> … … 41 41 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> 42 42 <DebugSymbols>true</DebugSymbols> 43 <OutputPath> bin\x64\Debug\</OutputPath>43 <OutputPath>..\..\bin\</OutputPath> 44 44 <DefineConstants>DEBUG;TRACE</DefineConstants> 45 45 <DebugType>full</DebugType> … … 50 50 </PropertyGroup> 51 51 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> 52 <OutputPath> bin\x64\Release\</OutputPath>52 <OutputPath>..\..\bin\</OutputPath> 53 53 <DefineConstants>TRACE</DefineConstants> 54 54 <Optimize>true</Optimize> … … 61 61 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> 62 62 <DebugSymbols>true</DebugSymbols> 63 <OutputPath> bin\x86\Debug\</OutputPath>63 <OutputPath>..\..\bin\</OutputPath> 64 64 <DefineConstants>DEBUG;TRACE</DefineConstants> 65 65 <DebugType>full</DebugType> … … 70 70 </PropertyGroup> 71 71 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> 72 <OutputPath> bin\x86\Release\</OutputPath>72 <OutputPath>..\..\bin\</OutputPath> 73 73 <DefineConstants>TRACE</DefineConstants> 74 74 <Optimize>true</Optimize> -
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/HeuristicLab.Networks-3.3.csproj
r11577 r11823 27 27 <DebugType>pdbonly</DebugType> 28 28 <Optimize>true</Optimize> 29 <OutputPath> bin\Release\</OutputPath>29 <OutputPath>..\..\bin\</OutputPath> 30 30 <DefineConstants>TRACE</DefineConstants> 31 31 <ErrorReport>prompt</ErrorReport> … … 41 41 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> 42 42 <DebugSymbols>true</DebugSymbols> 43 <OutputPath> bin\x64\Debug\</OutputPath>43 <OutputPath>..\..\bin\</OutputPath> 44 44 <DefineConstants>DEBUG;TRACE</DefineConstants> 45 45 <DebugType>full</DebugType> … … 50 50 </PropertyGroup> 51 51 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> 52 <OutputPath> bin\x64\Release\</OutputPath>52 <OutputPath>..\..\bin\</OutputPath> 53 53 <DefineConstants>TRACE</DefineConstants> 54 54 <Optimize>true</Optimize> … … 61 61 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> 62 62 <DebugSymbols>true</DebugSymbols> 63 <OutputPath> bin\x86\Debug\</OutputPath>63 <OutputPath>..\..\bin\</OutputPath> 64 64 <DefineConstants>DEBUG;TRACE</DefineConstants> 65 65 <DebugType>full</DebugType> … … 70 70 </PropertyGroup> 71 71 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> 72 <OutputPath> bin\x86\Release\</OutputPath>72 <OutputPath>..\..\bin\</OutputPath> 73 73 <DefineConstants>TRACE</DefineConstants> 74 74 <Optimize>true</Optimize> … … 97 97 <Compile Include="IExecutionPort.cs" /> 98 98 <Compile Include="IUserDefinedMessagePort.cs" /> 99 <Compile Include="KSPTSPConnector.cs" />100 99 <EmbeddedResource Include="KSPTSPConnectorCode.cs" /> 100 <Compile Include="KSPTSPConcurrentControlNetwork.cs" /> 101 <Compile Include="KSPTSPConnector.cs"> 102 <SubType>Code</SubType> 103 </Compile> 104 <Compile Include="KSPTSPControl.cs" /> 105 <EmbeddedResource Include="KSPTSPControlCode.cs" /> 101 106 <Compile Include="KSPTSPNetwork.cs" /> 102 107 <EmbeddedResource Include="KSPTSPNetworkCode.cs" /> 108 <EmbeddedResource Include="KSPTSPConcurrentControlNetworkCode.cs" /> 103 109 <Compile Include="Plugin.cs" /> 104 110 <Compile Include="Programmable\IProgrammableNetwork.cs" /> … … 124 130 </ItemGroup> 125 131 <ItemGroup> 132 <ProjectReference Include="..\..\HeuristicLab.Algorithms.GeneticAlgorithm\3.3\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.csproj"> 133 <Project>{a51da44f-cb35-4f6f-99f5-2a2e904ab93b}</Project> 134 <Name>HeuristicLab.Algorithms.GeneticAlgorithm-3.3</Name> 135 </ProjectReference> 136 <ProjectReference Include="..\..\HeuristicLab.Algorithms.LocalSearch\3.3\HeuristicLab.Algorithms.LocalSearch-3.3.csproj"> 137 <Project>{4ae3fc69-c575-42d2-bc46-0fad5850efc5}</Project> 138 <Name>HeuristicLab.Algorithms.LocalSearch-3.3</Name> 139 </ProjectReference> 140 <ProjectReference Include="..\..\HeuristicLab.Algorithms.VariableNeighborhoodSearch\3.3\HeuristicLab.Algorithms.VariableNeighborhoodSearch-3.3.csproj"> 141 <Project>{e4cfb0c3-0589-4893-b38e-8bedf885c765}</Project> 142 <Name>HeuristicLab.Algorithms.VariableNeighborhoodSearch-3.3</Name> 143 </ProjectReference> 144 <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj"> 145 <Project>{887425b4-4348-49ed-a457-b7d2c26ddbf9}</Project> 146 <Name>HeuristicLab.Analysis-3.3</Name> 147 </ProjectReference> 126 148 <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj"> 127 149 <Project>{958b43bc-cc5c-4fa2-8628-2b3b01d890b6}</Project> … … 149 171 <Private>False</Private> 150 172 </ProjectReference> 173 <ProjectReference Include="..\..\HeuristicLab.Encodings.BinaryVectorEncoding\3.3\HeuristicLab.Encodings.BinaryVectorEncoding-3.3.csproj"> 174 <Project>{66d249c3-a01d-42a8-82a2-919bc8ec3d83}</Project> 175 <Name>HeuristicLab.Encodings.BinaryVectorEncoding-3.3</Name> 176 </ProjectReference> 177 <ProjectReference Include="..\..\HeuristicLab.Encodings.PermutationEncoding\3.3\HeuristicLab.Encodings.PermutationEncoding-3.3.csproj"> 178 <Project>{dbecb8b0-b166-4133-baf1-ed67c3fd7fca}</Project> 179 <Name>HeuristicLab.Encodings.PermutationEncoding-3.3</Name> 180 </ProjectReference> 151 181 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> 152 182 <Project>{23da7ff4-d5b8-41b6-aa96-f0561d24f3ee}</Project> … … 174 204 <Private>False</Private> 175 205 </ProjectReference> 206 <ProjectReference Include="..\..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj"> 207 <Project>{3540e29e-4793-49e7-8ee2-fea7f61c3994}</Project> 208 <Name>HeuristicLab.Problems.Instances-3.3</Name> 209 </ProjectReference> 210 <ProjectReference Include="..\..\HeuristicLab.Problems.Knapsack\3.3\HeuristicLab.Problems.Knapsack-3.3.csproj"> 211 <Project>{e10f395f-c8a6-48ad-b470-9aa7a1f43809}</Project> 212 <Name>HeuristicLab.Problems.Knapsack-3.3</Name> 213 </ProjectReference> 214 <ProjectReference Include="..\..\HeuristicLab.Problems.TravelingSalesman\3.3\HeuristicLab.Problems.TravelingSalesman-3.3.csproj"> 215 <Project>{d767c38d-8014-46b0-9a32-03a3aecce34a}</Project> 216 <Name>HeuristicLab.Problems.TravelingSalesman-3.3</Name> 217 </ProjectReference> 176 218 <ProjectReference Include="..\..\HeuristicLab.Scripting\3.3\HeuristicLab.Scripting-3.3.csproj"> 177 219 <Project>{21977cc3-1757-4b3b-87bd-ff817aaa900f}</Project> 178 220 <Name>HeuristicLab.Scripting-3.3</Name> 221 </ProjectReference> 222 <ProjectReference Include="..\..\HeuristicLab.Selection\3.3\HeuristicLab.Selection-3.3.csproj"> 223 <Project>{2c36cd4f-e5f5-43a4-801a-201ea895fe17}</Project> 224 <Name>HeuristicLab.Selection-3.3</Name> 179 225 </ProjectReference> 180 226 </ItemGroup> -
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPConcurrentControlNetworkCode.cs
r11711 r11823 20 20 #endregion 21 21 22 using System.Drawing; 23 using System.Linq; 22 24 using HeuristicLab.Algorithms.GeneticAlgorithm; 23 25 using HeuristicLab.Common; … … 26 28 using HeuristicLab.Data; 27 29 using HeuristicLab.Encodings.BinaryVectorEncoding; 30 using HeuristicLab.Encodings.PermutationEncoding; 28 31 using HeuristicLab.Networks.Programmable; 29 32 using HeuristicLab.Operators; … … 32 35 using HeuristicLab.Problems.Knapsack; 33 36 using HeuristicLab.Problems.TravelingSalesman; 34 using System.Drawing; 35 using System.Linq; 37 using HeuristicLab.Selection; 36 38 37 39 namespace HeuristicLab.Networks { 38 [Item("KSPTSP Network", "An optimization network which connects a KSP and a TSP.")]39 public class CompiledKSPTSP Network : ProgrammableNetwork.CompiledProgrammableNetwork {40 [Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")] 41 public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork { 40 42 public static new Image StaticItemImage { 41 43 get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; } 42 44 } 43 45 44 protected CompiledKSPTSP Network(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }45 public CompiledKSPTSP Network(ProgrammableNetwork context)46 protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { } 47 public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context) 46 48 : base(context) { 47 49 if (Nodes.Count == 0) … … 50 52 51 53 public override IDeepCloneable Clone(Cloner cloner) { 52 return new CompiledKSPTSPNetwork(this, cloner); 53 } 54 55 public override void Initialize() { 56 base.Initialize(); 57 54 return new CompiledKSPTSPConcurrentControlNetwork(this, cloner); 55 } 56 57 public void Initialize() { 58 58 #region ParametersNode 59 59 var paramsNode = new UserDefinedNode("ParametersNode"); … … 63 63 paramsNode.Ports.Add(paramsPort); 64 64 65 paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("C ities") {65 paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { 66 66 Type = PortParameterType.Output, 67 67 DefaultValue = new DoubleMatrix(new double[,] { … … 91 91 #endregion 92 92 93 #region ControlNode 94 var controlNode = new KSPTSPControl("KSPTSP Control"); 95 Nodes.Add(controlNode); 96 #endregion 97 93 98 #region KSPNode 94 99 var kspNode = new AlgorithmNode("KSPNode"); 95 100 Nodes.Add(kspNode); 96 101 97 var configPort = new ConfigurationPort("ConfigureKSP"); 98 kspNode.Ports.Add(configPort); 99 100 configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") { 101 Type = PortParameterType.Input 102 }); 103 configPort.Parameters.Add(new PortParameter<IntArray>("Values") { 104 Type = PortParameterType.Input 105 }); 106 configPort.Parameters.Add(new PortParameter<IntArray>("Weights") { 107 Type = PortParameterType.Input 108 }); 109 110 var evaluatePort = new MessagePort("EvaluateRoute"); 111 kspNode.Ports.Add(evaluatePort); 112 113 evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { 114 Type = PortParameterType.Output 115 }); 116 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") { 117 Type = PortParameterType.Output | PortParameterType.Input 118 }); 119 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") { 120 Type = PortParameterType.Input 121 }); 122 evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") { 123 Type = PortParameterType.Input 124 }); 125 126 var kspGA = new GeneticAlgorithm(); 127 kspGA.Problem = new KnapsackProblem(); 128 kspGA.MaximumGenerations.Value = 50; 129 kspGA.PopulationSize.Value = 10; 130 kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First(); 131 kspNode.Algorithm = kspGA; 132 133 var hook = new HookOperator(); 134 hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false }); 135 hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false }); 136 hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false }); 137 hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false }); 138 ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook); 102 var kspConfigPort = new ConfigurationPort("ConfigureKSP"); 103 kspNode.Ports.Add(kspConfigPort); 104 105 kspConfigPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") { 106 Type = PortParameterType.Input 107 }); 108 kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Values") { 109 Type = PortParameterType.Input 110 }); 111 kspConfigPort.Parameters.Add(new PortParameter<IntArray>("Weights") { 112 Type = PortParameterType.Input 113 }); 114 115 var kspControlPort = new MessagePort("ControlPort"); 116 kspNode.Ports.Add(kspControlPort); 117 118 kspControlPort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { 119 Type = PortParameterType.Output 120 }); 121 kspControlPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") { 122 Type = PortParameterType.Input 123 }); 124 125 var kspReportPort = new MessagePort("ReportPort"); 126 kspNode.Ports.Add(kspReportPort); 127 128 kspReportPort.Parameters.Add(new PortParameter<KnapsackSolution>("BestSolution") { 129 Type = PortParameterType.Output 130 }); 131 132 var kspGa = new GeneticAlgorithm { 133 Problem = new KnapsackProblem(), 134 MaximumGenerations = { Value = 500 }, 135 PopulationSize = { Value = 200 } 136 }; 137 kspGa.Mutator = kspGa.MutatorParameter.ValidValues.First(x => x.Name == "SinglePositionBitflipManipulator"); 138 kspGa.Crossover = kspGa.CrossoverParameter.ValidValues.First(x => x.Name == "SinglePointCrossover"); 139 kspGa.Selector = kspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector"); 140 kspNode.Algorithm = kspGa; 141 142 var kspEvalHook = new HookOperator() { Name = "KSP Eval Hook" }; 143 kspEvalHook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false }); 144 kspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false }); 145 ((InstrumentedOperator)kspGa.Problem.Evaluator).AfterExecutionOperators.Add(kspEvalHook); 146 147 var kspIterHook = new HookOperator() { Name = "KSP Iter Hook" }; 148 kspIterHook.Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution") { Hidden = false }); 149 kspGa.Analyzer.AfterExecutionOperators.Add(kspIterHook); 139 150 #endregion 140 151 … … 143 154 Nodes.Add(tspNode); 144 155 145 var executePort = new ExecutionPort("Execute"); 146 tspNode.Ports.Add(executePort); 147 148 executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { 149 Type = PortParameterType.Input 150 }); 151 executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { 152 Type = PortParameterType.Output 153 }); 154 executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { 155 Type = PortParameterType.Output 156 }); 157 158 var tspGA = new GeneticAlgorithm(); 159 tspGA.Problem = new TravelingSalesmanProblem(); 160 tspGA.MaximumGenerations.Value = 100; 161 tspGA.PopulationSize.Value = 50; 162 tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First(); 163 tspNode.Algorithm = tspGA; 164 #endregion 165 166 #region KSPTSPConnector 167 var ksptspConnector = (INode)new KSPTSPConnector(); 168 Nodes.Add(ksptspConnector); 169 #endregion 156 var tspConfigPort = new ConfigurationPort("ConfigureTSP"); 157 tspNode.Ports.Add(tspConfigPort); 158 159 tspConfigPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { 160 Type = PortParameterType.Input 161 }); 162 163 var tspControlPort = new MessagePort("ControlPort"); 164 tspNode.Ports.Add(tspControlPort); 165 166 tspControlPort.Parameters.Add(new PortParameter<Permutation>("TSPTour") { 167 Type = PortParameterType.Output 168 }); 169 tspControlPort.Parameters.Add(new PortParameter<DoubleValue>("TSPTourLength") { 170 Type = PortParameterType.Input 171 }); 172 173 var tspReportPort = new MessagePort("ReportPort"); 174 tspNode.Ports.Add(tspReportPort); 175 176 tspReportPort.Parameters.Add(new PortParameter<PathTSPTour>("BestSolution") { 177 Type = PortParameterType.Output 178 }); 179 180 var tspGa = new GeneticAlgorithm { 181 Problem = new TravelingSalesmanProblem(), 182 MaximumGenerations = { Value = 500 }, 183 PopulationSize = { Value = 200 } 184 }; 185 ((IValueParameter<BoolValue>)tspGa.Problem.MaximizationParameter).Value.Value = true; 186 tspGa.Crossover = tspGa.CrossoverParameter.ValidValues.First(x => x.Name == "EdgeRecombinationCrossover"); 187 tspGa.Mutator = tspGa.MutatorParameter.ValidValues.First(x => x.Name == "InversionManipulator"); 188 tspGa.Selector = tspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector"); 189 ((TournamentSelector)tspGa.Selector).GroupSizeParameter.Value.Value = 3; 190 tspNode.Algorithm = tspGa; 191 192 var tspEvalHook = new HookOperator() { Name = "TSP Eval Hook" }; 193 tspEvalHook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = false }); 194 tspEvalHook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = false }); 195 ((InstrumentedOperator)tspGa.Problem.Evaluator).AfterExecutionOperators.Add(tspEvalHook); 196 197 var tspIterHook = new HookOperator() { Name = "TSP Iter Hook" }; 198 tspIterHook.Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution") { Hidden = false }); 199 tspGa.Analyzer.AfterExecutionOperators.Add(tspIterHook); 200 #endregion 201 170 202 171 203 #region Wire Ports 172 hook.Port = evaluatePort; 173 configPort.ConnectedPort = paramsPort; 174 evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"]; 175 ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort; 176 ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort; 204 ((ConfigurationPort)((INode)controlNode).Ports["Configure"]).ConnectedPort = paramsPort; 205 kspConfigPort.ConnectedPort = paramsPort; 206 tspConfigPort.ConnectedPort = paramsPort; 207 208 kspEvalHook.Port = kspControlPort; 209 kspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate KSP"]; 210 tspEvalHook.Port = tspControlPort; 211 tspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate TSP"]; 212 213 kspIterHook.Port = kspReportPort; 214 kspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add KSP Solution"]; 215 tspIterHook.Port = tspReportPort; 216 tspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add TSP Solution"]; 217 #endregion 177 218 178 219 paramsPort.SendMessage(paramsPort.PrepareMessage()); 179 #endregion180 220 } 181 221 } -
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPControlCode.cs
r11711 r11823 20 20 #endregion 21 21 22 using System; 23 using System.Drawing; 24 using System.Linq; 25 using System.Threading; 22 26 using HeuristicLab.Common; 23 27 using HeuristicLab.Core; … … 25 29 using HeuristicLab.Data; 26 30 using HeuristicLab.Encodings.BinaryVectorEncoding; 31 using HeuristicLab.Encodings.PermutationEncoding; 27 32 using HeuristicLab.Networks.Programmable; 33 using HeuristicLab.Problems.Knapsack; 28 34 using HeuristicLab.Problems.TravelingSalesman; 29 using System.Drawing;30 using System.Linq;31 using System.Threading;32 35 33 36 namespace HeuristicLab.Networks { 34 [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")] 35 public class CompiledKSPTSPConnector : ProgrammableNode.CompiledProgrammableNode { 37 [Item("KSPTSPControl", "A node of an optimization network which connects a KSP and a TSP.")] 38 public class CompiledKSPTSPControl : ProgrammableNode.CompiledProgrammableNode { 39 36 40 public static new Image StaticItemImage { 37 41 get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; } 38 42 } 39 43 40 protected CompiledKSPTSPConnector(CompiledKSPTSPConnector original, Cloner cloner) : base(original, cloner) { } 41 public CompiledKSPTSPConnector(ProgrammableNode context) 44 new protected KSPTSPControl Context { 45 get { return (KSPTSPControl)base.Context; } 46 } 47 48 protected CompiledKSPTSPControl(CompiledKSPTSPControl original, Cloner cloner) : base(original, cloner) { } 49 public CompiledKSPTSPControl(ProgrammableNode context) 42 50 : base(context) { 43 51 if (Ports.Count == 0) … … 46 54 47 55 public override IDeepCloneable Clone(Cloner cloner) { 48 return new CompiledKSPTSPCon nector(this, cloner);56 return new CompiledKSPTSPControl(this, cloner); 49 57 } 50 58 51 59 public override void Initialize() { 52 60 base.Initialize(); 53 var parameters = new MessagePort("Parameters"); 54 Ports.Add(parameters); 55 parameters.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") { Type = PortParameterType.Input }); 56 parameters.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { Type = PortParameterType.Input }); 57 58 var ksp = new MessagePort("KSP Connector"); 59 Ports.Add(ksp); 60 ksp.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { Type = PortParameterType.Input }); 61 ksp.Parameters.Add(new PortParameter<DoubleValue>("Quality") { Type = PortParameterType.Input | PortParameterType.Output }); 62 ksp.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") { Type = PortParameterType.Output }); 63 ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output }); 64 65 var tsp = new MessagePort("TSP Connector"); 66 Ports.Add(tsp); 67 tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output }); 68 tsp.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { Type = PortParameterType.Input }); 69 tsp.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { Type = PortParameterType.Input }); 61 var configPort = new ConfigurationPort("Configure"); 62 Ports.Add(configPort); 63 64 configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") { 65 Type = PortParameterType.Input 66 }); 67 configPort.Parameters.Add(new PortParameter<IntArray>("Values") { 68 Type = PortParameterType.Input 69 }); 70 configPort.Parameters.Add(new PortParameter<IntArray>("Weights") { 71 Type = PortParameterType.Input 72 }); 73 configPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { 74 Type = PortParameterType.Input 75 }); 76 configPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { 77 Type = PortParameterType.Input 78 }); 79 80 var evalKspPort = new MessagePort("Evaluate KSP"); 81 Ports.Add(evalKspPort); 82 evalKspPort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { 83 Type = PortParameterType.Input 84 }); 85 evalKspPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") { 86 Type = PortParameterType.Output 87 }); 88 89 var evalTspPort = new MessagePort("Evaluate TSP"); 90 Ports.Add(evalTspPort); 91 evalTspPort.Parameters.Add(new PortParameter<Permutation>("TSPTour") { 92 Type = PortParameterType.Input 93 }); 94 evalTspPort.Parameters.Add(new PortParameter<DoubleValue>("TSPTourLength") { 95 Type = PortParameterType.Output 96 }); 97 98 var addKspSolultionPort = new MessagePort("Add KSP Solution"); 99 Ports.Add(addKspSolultionPort); 100 addKspSolultionPort.Parameters.Add(new PortParameter<KnapsackSolution>("BestSolution") { 101 Type = PortParameterType.Input 102 }); 103 104 var addTspSolutionPort = new MessagePort("Add TSP Solution"); 105 Ports.Add(addTspSolutionPort); 106 addTspSolutionPort.Parameters.Add(new PortParameter<PathTSPTour>("BestSolution") { 107 Type = PortParameterType.Input 108 }); 109 } 110 111 private void AddKspSolultionPortOnMessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 112 var cities = (KnapsackSolution)(e.Value.Values["BestSolution"]).Value; 113 AddSelectedCities(cities.BinaryVector); 114 } 115 116 private void AddTspSolutionPortOnMessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 117 var trip = (PathTSPTour)(e.Value.Values["BestSolution"]).Value; 118 AddPredefinedTrip(trip.Permutation); 119 } 120 121 private void ConfigPortOnMessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 122 Context.TransportCostFactor = (DoubleValue)(e.Value["TransportCostsFactor"]); 123 Context.Coordinates = (DoubleMatrix)(e.Value["Coordinates"]); 124 Context.Distances = CalculateEuclidean(Context.Coordinates); 125 Context.CityValues = (IntArray)(e.Value["Values"]); 126 Context.CityWeights = (IntArray)(e.Value["Weights"]); 127 Context.CityLimit = (IntValue)(e.Value["KnapsackCapacity"]); 128 } 129 130 private void EvalKspPortOnMessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 131 var cities = (BinaryVector)(e.Value.Values["KnapsackSolution"]).Value; 132 e.Value.Values["Quality"].Value = new DoubleValue(EvaluatePredefinedTrip(cities)); 133 } 134 135 private void EvalTspPortOnMessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 136 var trip = (Permutation)(e.Value.Values["TSPTour"]).Value; 137 e.Value.Values["TSPTourLength"].Value = new DoubleValue(EvaluateSelectedCities(trip)); 70 138 } 71 139 72 140 public override void RegisterEvents() { 73 141 base.RegisterEvents(); 74 var ksp = (IMessagePort)Ports["KSP Connector"]; 75 ksp.MessageReceived += Knapsack_MessageReceived; 142 ((IMessagePort)Ports["Configure"]).MessageReceived += ConfigPortOnMessageReceived; 143 ((IMessagePort)Ports["Evaluate KSP"]).MessageReceived += EvalKspPortOnMessageReceived; 144 ((IMessagePort)Ports["Evaluate TSP"]).MessageReceived += EvalTspPortOnMessageReceived; 145 ((IMessagePort)Ports["Add KSP Solution"]).MessageReceived += AddKspSolultionPortOnMessageReceived; 146 ((IMessagePort)Ports["Add TSP Solution"]).MessageReceived += AddTspSolutionPortOnMessageReceived; 147 76 148 } 77 149 public override void DeregisterEvents() { 78 var ksp = (IMessagePort)Ports["KSP Connector"]; 79 ksp.MessageReceived -= Knapsack_MessageReceived; 150 ((IMessagePort)Ports["Configure"]).MessageReceived -= ConfigPortOnMessageReceived; 151 ((IMessagePort)Ports["Evaluate KSP"]).MessageReceived -= EvalKspPortOnMessageReceived; 152 ((IMessagePort)Ports["Evaluate TSP"]).MessageReceived -= EvalTspPortOnMessageReceived; 153 ((IMessagePort)Ports["Add KSP Solution"]).MessageReceived -= AddKspSolultionPortOnMessageReceived; 154 ((IMessagePort)Ports["Add TSP Solution"]).MessageReceived -= AddTspSolutionPortOnMessageReceived; 80 155 base.DeregisterEvents(); 81 156 } 82 157 83 private void Knapsack_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 84 // get parameters 85 var parametersPort = (IMessagePort)Ports["Parameters"]; 86 var parameters = parametersPort.PrepareMessage(); 87 parametersPort.SendMessage(parameters, e.Value2); 88 var cities = (DoubleMatrix)parameters["Cities"]; 89 var factor = ((DoubleValue)parameters["TransportCostsFactor"]).Value; 90 91 // build coordinates 92 var kspMsg = e.Value; 93 var kspSolution = (BinaryVector)kspMsg["KnapsackSolution"]; 94 var kspQuality = ((DoubleValue)kspMsg["Quality"]).Value; 95 var coords = new DoubleMatrix(kspSolution.Count(x => x), 2); 96 int j = 0; 97 for (int i = 0; i < kspSolution.Length; i++) { 98 if (kspSolution[i]) { 99 coords[j, 0] = cities[i, 0]; 100 coords[j, 1] = cities[i, 1]; 101 j++; 158 public double EvaluatePredefinedTrip(BinaryVector cities) { 159 if (Context.SelectedCities.Count == 0) { 160 Context.SelectedCities.Add(cities); 161 Context.KspWait.Set(); 162 Context.TspWait.WaitOne(); 163 } 164 return EvaluateBoth(cities, Context.PredefinedTrip.Last()); 165 } 166 167 public double EvaluateSelectedCities(Permutation trip) { 168 if (Context.PredefinedTrip.Count == 0) { 169 Context.PredefinedTrip.Add(trip); 170 Context.TspWait.Set(); 171 Context.KspWait.WaitOne(); 172 } 173 return EvaluateBoth(Context.SelectedCities.Last(), trip); 174 } 175 176 public double EvaluateBoth(BinaryVector cities, Permutation trip) { 177 var cityValues = cities.Select((v, i) => v ? Context.CityValues[i] : 0).Sum(); 178 var cityWeights = cities.Select((v, i) => v ? Context.CityWeights[i] : 0).Sum(); 179 var subtour = trip.Where(x => cities[x]).ToArray(); 180 var tourLength = 0.0; 181 for (var i = 1; i < subtour.Length; i++) 182 tourLength += Context.Distances[subtour[i - 1], subtour[i]]; 183 tourLength += Context.Distances[subtour.Last(), subtour[0]]; 184 if (cityWeights > Context.CityLimit.Value) // infeasible solution 185 return Context.CityLimit.Value - cityWeights - tourLength * Context.TransportCostFactor.Value; 186 return cityValues - tourLength * Context.TransportCostFactor.Value; 187 } 188 189 public void AddPredefinedTrip(Permutation trip) { 190 Context.TspWait.Set(); 191 Context.KspWait.WaitOne(); 192 lock (Context.Locker) { 193 Context.PredefinedTrip.Add(trip); 194 } 195 } 196 197 public void AddSelectedCities(BinaryVector cities) { 198 Context.KspWait.Set(); 199 Context.TspWait.WaitOne(); 200 lock (Context.Locker) { 201 Context.SelectedCities.Add(cities); 202 } 203 } 204 205 public static DoubleMatrix CalculateEuclidean(DoubleMatrix cities) { 206 var len = cities.Rows; 207 var distances = new DoubleMatrix(len, len); 208 for (var i = 0; i < len - 1; i++) { 209 var sX = cities[i, 0]; 210 var sY = cities[i, 1]; 211 for (var j = i + 1; j < len; j++) { 212 var tX = cities[j, 0]; 213 var tY = cities[j, 1]; 214 distances[i, j] = Math.Sqrt((sX - tX) * (sX - tX) + (sY - tY) * (sY - tY)); 215 distances[j, i] = distances[i, j]; 102 216 } 103 217 } 104 105 // solve TSP 106 var tspConnectorPort = (IMessagePort)Ports["TSP Connector"]; 107 var tspMsg = tspConnectorPort.PrepareMessage(); 108 tspMsg["Coordinates"] = coords; 109 tspConnectorPort.SendMessage(tspMsg, e.Value2); 110 var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"]; 111 var tspQuality = ((DoubleValue)tspMsg["BestQuality"]).Value; 112 113 // calculate transport costs 114 ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality; 115 kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality); 116 kspMsg["Route"] = tspSolution; 117 } 218 return distances; 219 } 220 118 221 } 119 222 } -
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPNetworkCode.cs
r11713 r11823 121 121 var kspGA = new GeneticAlgorithm(); 122 122 kspGA.Problem = new KnapsackProblem(); 123 ((KnapsackProblem)kspGA.Problem).Penalty.Value = 100; 123 124 kspGA.MaximumGenerations.Value = 50; 124 125 kspGA.PopulationSize.Value = 10;
Note: See TracChangeset
for help on using the changeset viewer.