Changeset 11823


Ignore:
Timestamp:
01/26/15 15:52:03 (7 years ago)
Author:
abeham
Message:

#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
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  
    2626    <DebugType>pdbonly</DebugType>
    2727    <Optimize>true</Optimize>
    28     <OutputPath>bin\Release\</OutputPath>
     28    <OutputPath>..\..\bin\</OutputPath>
    2929    <DefineConstants>TRACE</DefineConstants>
    3030    <ErrorReport>prompt</ErrorReport>
     
    3939  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    4040    <DebugSymbols>true</DebugSymbols>
    41     <OutputPath>bin\x64\Debug\</OutputPath>
     41    <OutputPath>..\..\bin\</OutputPath>
    4242    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4343    <DebugType>full</DebugType>
     
    4747  </PropertyGroup>
    4848  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    49     <OutputPath>bin\x64\Release\</OutputPath>
     49    <OutputPath>..\..\bin\</OutputPath>
    5050    <DefineConstants>TRACE</DefineConstants>
    5151    <Optimize>true</Optimize>
     
    5757  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    5858    <DebugSymbols>true</DebugSymbols>
    59     <OutputPath>bin\x86\Debug\</OutputPath>
     59    <OutputPath>..\..\bin\</OutputPath>
    6060    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6161    <DebugType>full</DebugType>
     
    6565  </PropertyGroup>
    6666  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    67     <OutputPath>bin\x86\Release\</OutputPath>
     67    <OutputPath>..\..\bin\</OutputPath>
    6868    <DefineConstants>TRACE</DefineConstants>
    6969    <Optimize>true</Optimize>
  • branches/OptimizationNetworks/HeuristicLab.Networks.Views/3.3/HeuristicLab.Networks.Views-3.3.csproj

    r11577 r11823  
    2727    <DebugType>pdbonly</DebugType>
    2828    <Optimize>true</Optimize>
    29     <OutputPath>bin\Release\</OutputPath>
     29    <OutputPath>..\..\bin\</OutputPath>
    3030    <DefineConstants>TRACE</DefineConstants>
    3131    <ErrorReport>prompt</ErrorReport>
     
    4141  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    4242    <DebugSymbols>true</DebugSymbols>
    43     <OutputPath>bin\x64\Debug\</OutputPath>
     43    <OutputPath>..\..\bin\</OutputPath>
    4444    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4545    <DebugType>full</DebugType>
     
    5050  </PropertyGroup>
    5151  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    52     <OutputPath>bin\x64\Release\</OutputPath>
     52    <OutputPath>..\..\bin\</OutputPath>
    5353    <DefineConstants>TRACE</DefineConstants>
    5454    <Optimize>true</Optimize>
     
    6161  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    6262    <DebugSymbols>true</DebugSymbols>
    63     <OutputPath>bin\x86\Debug\</OutputPath>
     63    <OutputPath>..\..\bin\</OutputPath>
    6464    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6565    <DebugType>full</DebugType>
     
    7070  </PropertyGroup>
    7171  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    72     <OutputPath>bin\x86\Release\</OutputPath>
     72    <OutputPath>..\..\bin\</OutputPath>
    7373    <DefineConstants>TRACE</DefineConstants>
    7474    <Optimize>true</Optimize>
  • branches/OptimizationNetworks/HeuristicLab.Networks/3.3/HeuristicLab.Networks-3.3.csproj

    r11577 r11823  
    2727    <DebugType>pdbonly</DebugType>
    2828    <Optimize>true</Optimize>
    29     <OutputPath>bin\Release\</OutputPath>
     29    <OutputPath>..\..\bin\</OutputPath>
    3030    <DefineConstants>TRACE</DefineConstants>
    3131    <ErrorReport>prompt</ErrorReport>
     
    4141  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    4242    <DebugSymbols>true</DebugSymbols>
    43     <OutputPath>bin\x64\Debug\</OutputPath>
     43    <OutputPath>..\..\bin\</OutputPath>
    4444    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4545    <DebugType>full</DebugType>
     
    5050  </PropertyGroup>
    5151  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    52     <OutputPath>bin\x64\Release\</OutputPath>
     52    <OutputPath>..\..\bin\</OutputPath>
    5353    <DefineConstants>TRACE</DefineConstants>
    5454    <Optimize>true</Optimize>
     
    6161  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    6262    <DebugSymbols>true</DebugSymbols>
    63     <OutputPath>bin\x86\Debug\</OutputPath>
     63    <OutputPath>..\..\bin\</OutputPath>
    6464    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6565    <DebugType>full</DebugType>
     
    7070  </PropertyGroup>
    7171  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    72     <OutputPath>bin\x86\Release\</OutputPath>
     72    <OutputPath>..\..\bin\</OutputPath>
    7373    <DefineConstants>TRACE</DefineConstants>
    7474    <Optimize>true</Optimize>
     
    9797    <Compile Include="IExecutionPort.cs" />
    9898    <Compile Include="IUserDefinedMessagePort.cs" />
    99     <Compile Include="KSPTSPConnector.cs" />
    10099    <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" />
    101106    <Compile Include="KSPTSPNetwork.cs" />
    102107    <EmbeddedResource Include="KSPTSPNetworkCode.cs" />
     108    <EmbeddedResource Include="KSPTSPConcurrentControlNetworkCode.cs" />
    103109    <Compile Include="Plugin.cs" />
    104110    <Compile Include="Programmable\IProgrammableNetwork.cs" />
     
    124130  </ItemGroup>
    125131  <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>
    126148    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    127149      <Project>{958b43bc-cc5c-4fa2-8628-2b3b01d890b6}</Project>
     
    149171      <Private>False</Private>
    150172    </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>
    151181    <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
    152182      <Project>{23da7ff4-d5b8-41b6-aa96-f0561d24f3ee}</Project>
     
    174204      <Private>False</Private>
    175205    </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>
    176218    <ProjectReference Include="..\..\HeuristicLab.Scripting\3.3\HeuristicLab.Scripting-3.3.csproj">
    177219      <Project>{21977cc3-1757-4b3b-87bd-ff817aaa900f}</Project>
    178220      <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>
    179225    </ProjectReference>
    180226  </ItemGroup>
  • branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPConcurrentControlNetworkCode.cs

    r11711 r11823  
    2020#endregion
    2121
     22using System.Drawing;
     23using System.Linq;
    2224using HeuristicLab.Algorithms.GeneticAlgorithm;
    2325using HeuristicLab.Common;
     
    2628using HeuristicLab.Data;
    2729using HeuristicLab.Encodings.BinaryVectorEncoding;
     30using HeuristicLab.Encodings.PermutationEncoding;
    2831using HeuristicLab.Networks.Programmable;
    2932using HeuristicLab.Operators;
     
    3235using HeuristicLab.Problems.Knapsack;
    3336using HeuristicLab.Problems.TravelingSalesman;
    34 using System.Drawing;
    35 using System.Linq;
     37using HeuristicLab.Selection;
    3638
    3739namespace HeuristicLab.Networks {
    38   [Item("KSPTSPNetwork", "An optimization network which connects a KSP and a TSP.")]
    39   public class CompiledKSPTSPNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
     40  [Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")]
     41  public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
    4042    public static new Image StaticItemImage {
    4143      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
    4244    }
    4345
    44     protected CompiledKSPTSPNetwork(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
    45     public CompiledKSPTSPNetwork(ProgrammableNetwork context)
     46    protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { }
     47    public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context)
    4648      : base(context) {
    4749      if (Nodes.Count == 0)
     
    5052
    5153    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() {
    5858      #region ParametersNode
    5959      var paramsNode = new UserDefinedNode("ParametersNode");
     
    6363      paramsNode.Ports.Add(paramsPort);
    6464
    65       paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {
     65      paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
    6666        Type = PortParameterType.Output,
    6767        DefaultValue = new DoubleMatrix(new double[,] {
     
    9191      #endregion
    9292
     93      #region ControlNode
     94      var controlNode = new KSPTSPControl("KSPTSP Control");
     95      Nodes.Add(controlNode);
     96      #endregion
     97
    9398      #region KSPNode
    9499      var kspNode = new AlgorithmNode("KSPNode");
    95100      Nodes.Add(kspNode);
    96101
    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);
    139150      #endregion
    140151
     
    143154      Nodes.Add(tspNode);
    144155
    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
    170202
    171203      #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
    177218
    178219      paramsPort.SendMessage(paramsPort.PrepareMessage());
    179       #endregion
    180220    }
    181221  }
  • branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPControlCode.cs

    r11711 r11823  
    2020#endregion
    2121
     22using System;
     23using System.Drawing;
     24using System.Linq;
     25using System.Threading;
    2226using HeuristicLab.Common;
    2327using HeuristicLab.Core;
     
    2529using HeuristicLab.Data;
    2630using HeuristicLab.Encodings.BinaryVectorEncoding;
     31using HeuristicLab.Encodings.PermutationEncoding;
    2732using HeuristicLab.Networks.Programmable;
     33using HeuristicLab.Problems.Knapsack;
    2834using HeuristicLab.Problems.TravelingSalesman;
    29 using System.Drawing;
    30 using System.Linq;
    31 using System.Threading;
    3235
    3336namespace 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
    3640    public static new Image StaticItemImage {
    3741      get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
    3842    }
    3943
    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)
    4250      : base(context) {
    4351      if (Ports.Count == 0)
     
    4654
    4755    public override IDeepCloneable Clone(Cloner cloner) {
    48       return new CompiledKSPTSPConnector(this, cloner);
     56      return new CompiledKSPTSPControl(this, cloner);
    4957    }
    5058
    5159    public override void Initialize() {
    5260      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));
    70138    }
    71139
    72140    public override void RegisterEvents() {
    73141      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
    76148    }
    77149    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;
    80155      base.DeregisterEvents();
    81156    }
    82157
    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];
    102216        }
    103217      }
    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
    118221  }
    119222}
  • branches/OptimizationNetworks/HeuristicLab.Networks/3.3/KSPTSPNetworkCode.cs

    r11713 r11823  
    121121      var kspGA = new GeneticAlgorithm();
    122122      kspGA.Problem = new KnapsackProblem();
     123      ((KnapsackProblem)kspGA.Problem).Penalty.Value = 100;
    123124      kspGA.MaximumGenerations.Value = 50;
    124125      kspGA.PopulationSize.Value = 10;
Note: See TracChangeset for help on using the changeset viewer.