#pragma warning disable 436
#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Drawing;
using System.Linq;
using HeuristicLab.Algorithms.GeneticAlgorithm;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Networks;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Networks.Programmable;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.Knapsack;
using HeuristicLab.Problems.TravelingSalesman;
using HeuristicLab.Selection;
namespace HeuristicLab.Networks {
[Item("KSPTSP Concurrent-Control Network", "An optimization network which connects a KSP and a TSP.")]
public class CompiledKSPTSPConcurrentControlNetwork : ProgrammableNetwork.CompiledProgrammableNetwork {
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
}
protected CompiledKSPTSPConcurrentControlNetwork(CompiledKSPTSPConcurrentControlNetwork original, Cloner cloner) : base(original, cloner) { }
public CompiledKSPTSPConcurrentControlNetwork(ProgrammableNetwork context)
: base(context) {
if (Nodes.Count == 0)
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CompiledKSPTSPConcurrentControlNetwork(this, cloner);
}
public void Initialize() {
#region ParametersNode
var paramsNode = new UserDefinedNode("ParametersNode");
Nodes.Add(paramsNode);
var paramsPort = new MessagePort("Parameters");
paramsNode.Ports.Add(paramsPort);
paramsPort.Parameters.Add(new PortParameter("Coordinates") {
Type = PortParameterType.Output,
DefaultValue = new DoubleMatrix(new double[,] {
{00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
{00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
{00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
{00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
{00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
})
});
paramsPort.Parameters.Add(new PortParameter("TransportCostsFactor") {
Type = PortParameterType.Output,
DefaultValue = new DoubleValue(0.1)
});
paramsPort.Parameters.Add(new PortParameter("KnapsackCapacity") {
Type = PortParameterType.Output,
DefaultValue = new IntValue(10)
});
paramsPort.Parameters.Add(new PortParameter("Values") {
Type = PortParameterType.Output,
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 })
});
paramsPort.Parameters.Add(new PortParameter("Weights") {
Type = PortParameterType.Output,
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 })
});
#endregion
#region ControlNode
var controlNode = new KSPTSPControl("KSPTSP Control");
Nodes.Add(controlNode);
#endregion
#region KSPNode
var kspNode = new AlgorithmNode("KSPNode");
Nodes.Add(kspNode);
var kspConfigPort = new ConfigurationPort("ConfigureKSP");
kspNode.Ports.Add(kspConfigPort);
kspConfigPort.Parameters.Add(new PortParameter("KnapsackCapacity") {
Type = PortParameterType.Input
});
kspConfigPort.Parameters.Add(new PortParameter("Values") {
Type = PortParameterType.Input
});
kspConfigPort.Parameters.Add(new PortParameter("Weights") {
Type = PortParameterType.Input
});
var kspControlPort = new MessagePort("ControlPort");
kspNode.Ports.Add(kspControlPort);
kspControlPort.Parameters.Add(new PortParameter("KnapsackSolution") {
Type = PortParameterType.Output
});
kspControlPort.Parameters.Add(new PortParameter("Quality") {
Type = PortParameterType.Input
});
var kspReportPort = new MessagePort("ReportPort");
kspNode.Ports.Add(kspReportPort);
kspReportPort.Parameters.Add(new PortParameter("BestSolution") {
Type = PortParameterType.Output
});
var kspGa = new GeneticAlgorithm {
Problem = new KnapsackProblem(),
MaximumGenerations = { Value = 500 },
PopulationSize = { Value = 200 }
};
kspGa.Mutator = kspGa.MutatorParameter.ValidValues.First(x => x.Name == "SinglePositionBitflipManipulator");
kspGa.Crossover = kspGa.CrossoverParameter.ValidValues.First(x => x.Name == "SinglePointCrossover");
kspGa.Selector = kspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
kspNode.Algorithm = kspGa;
var kspEvalHook = new HookOperator() { Name = "KSP Eval Hook" };
kspEvalHook.Parameters.Add(new LookupParameter("KnapsackSolution") { Hidden = false });
kspEvalHook.Parameters.Add(new LookupParameter("Quality") { Hidden = false });
((InstrumentedOperator)kspGa.Problem.Evaluator).AfterExecutionOperators.Add(kspEvalHook);
var kspIterHook = new HookOperator() { Name = "KSP Iter Hook" };
kspIterHook.Parameters.Add(new LookupParameter("BestSolution") { Hidden = false });
kspGa.Analyzer.AfterExecutionOperators.Add(kspIterHook);
#endregion
#region TSPNode
var tspNode = new AlgorithmNode("TSPNode");
Nodes.Add(tspNode);
var tspConfigPort = new ConfigurationPort("ConfigureTSP");
tspNode.Ports.Add(tspConfigPort);
tspConfigPort.Parameters.Add(new PortParameter("Coordinates") {
Type = PortParameterType.Input
});
var tspControlPort = new MessagePort("ControlPort");
tspNode.Ports.Add(tspControlPort);
tspControlPort.Parameters.Add(new PortParameter("TSPTour") {
Type = PortParameterType.Output
});
tspControlPort.Parameters.Add(new PortParameter("TSPTourLength") {
Type = PortParameterType.Input
});
var tspReportPort = new MessagePort("ReportPort");
tspNode.Ports.Add(tspReportPort);
tspReportPort.Parameters.Add(new PortParameter("BestSolution") {
Type = PortParameterType.Output
});
var tspGa = new GeneticAlgorithm {
Problem = new TravelingSalesmanProblem(),
MaximumGenerations = { Value = 500 },
PopulationSize = { Value = 200 }
};
((IValueParameter)tspGa.Problem.MaximizationParameter).Value.Value = true;
tspGa.Crossover = tspGa.CrossoverParameter.ValidValues.First(x => x.Name == "EdgeRecombinationCrossover");
tspGa.Mutator = tspGa.MutatorParameter.ValidValues.First(x => x.Name == "InversionManipulator");
tspGa.Selector = tspGa.SelectorParameter.ValidValues.First(x => x.Name == "TournamentSelector");
((TournamentSelector)tspGa.Selector).GroupSizeParameter.Value.Value = 3;
tspNode.Algorithm = tspGa;
var tspEvalHook = new HookOperator() { Name = "TSP Eval Hook" };
tspEvalHook.Parameters.Add(new LookupParameter("TSPTour") { Hidden = false });
tspEvalHook.Parameters.Add(new LookupParameter("TSPTourLength") { Hidden = false });
((InstrumentedOperator)tspGa.Problem.Evaluator).AfterExecutionOperators.Add(tspEvalHook);
var tspIterHook = new HookOperator() { Name = "TSP Iter Hook" };
tspIterHook.Parameters.Add(new LookupParameter("BestSolution") { Hidden = false });
tspGa.Analyzer.AfterExecutionOperators.Add(tspIterHook);
#endregion
#region Wire Ports
((ConfigurationPort)((INode)controlNode).Ports["Configure"]).ConnectedPort = paramsPort;
kspConfigPort.ConnectedPort = paramsPort;
tspConfigPort.ConnectedPort = paramsPort;
kspEvalHook.Port = kspControlPort;
kspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate KSP"];
tspEvalHook.Port = tspControlPort;
tspControlPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Evaluate TSP"];
kspIterHook.Port = kspReportPort;
kspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add KSP Solution"];
tspIterHook.Port = tspReportPort;
tspReportPort.ConnectedPort = (IMessagePort)((INode)controlNode).Ports["Add TSP Solution"];
#endregion
paramsPort.SendMessage(paramsPort.PrepareMessage());
}
}
}