#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 HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Core.Networks; using HeuristicLab.Data; using HeuristicLab.Encodings.BinaryVectorEncoding; 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 System.Linq; namespace HeuristicLab.Networks { [Item("KSPTSPNetwork", "An optimization network which connects a KSP and a TSP.")] public class CompiledKSPTSPNetwork : ProgrammableNetwork.CompiledProgrammableNetwork { protected CompiledKSPTSPNetwork(CompiledKSPTSPNetwork original, Cloner cloner) : base(original, cloner) { } public CompiledKSPTSPNetwork(ProgrammableNetwork context) : base(context) { if (Nodes.Count == 0) Initialize(); } public override IDeepCloneable Clone(Cloner cloner) { return new CompiledKSPTSPNetwork(this, cloner); } public override void Initialize() { base.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("Cities") { 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 KSPNode var kspNode = new AlgorithmNode("KSPNode"); Nodes.Add(kspNode); var configPort = new ConfigurationPort("ConfigureKSP"); kspNode.Ports.Add(configPort); configPort.Parameters.Add(new PortParameter("KnapsackCapacity") { Type = PortParameterType.Input }); configPort.Parameters.Add(new PortParameter("Values") { Type = PortParameterType.Input }); configPort.Parameters.Add(new PortParameter("Weights") { Type = PortParameterType.Input }); var evaluatePort = new MessagePort("EvaluateRoute"); kspNode.Ports.Add(evaluatePort); evaluatePort.Parameters.Add(new PortParameter("KnapsackSolution") { Type = PortParameterType.Output }); evaluatePort.Parameters.Add(new PortParameter("Quality") { Type = PortParameterType.Output | PortParameterType.Input }); evaluatePort.Parameters.Add(new PortParameter("TransportCosts") { Type = PortParameterType.Input }); evaluatePort.Parameters.Add(new PortParameter("Route") { Type = PortParameterType.Input }); var kspGA = new GeneticAlgorithm(); kspGA.Problem = new KnapsackProblem(); kspGA.MaximumGenerations.Value = 50; kspGA.PopulationSize.Value = 10; kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First(); kspNode.Algorithm = kspGA; var hook = new HookOperator(); hook.Parameters.Add(new LookupParameter("KnapsackSolution") { Hidden = false }); hook.Parameters.Add(new LookupParameter("Quality") { Hidden = false }); hook.Parameters.Add(new LookupParameter("TransportCosts") { Hidden = false }); hook.Parameters.Add(new LookupParameter("Route") { Hidden = false }); ((FixedValueParameter)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook); #endregion #region TSPNode var tspNode = new AlgorithmNode("TSPNode"); Nodes.Add(tspNode); var executePort = new ExecutionPort("Execute"); tspNode.Ports.Add(executePort); executePort.Parameters.Add(new PortParameter("Coordinates") { Type = PortParameterType.Input }); executePort.Parameters.Add(new PortParameter("BestQuality") { Type = PortParameterType.Output }); executePort.Parameters.Add(new PortParameter("Best TSP Solution") { Type = PortParameterType.Output }); var tspGA = new GeneticAlgorithm(); tspGA.Problem = new TravelingSalesmanProblem(); tspGA.MaximumGenerations.Value = 100; tspGA.PopulationSize.Value = 50; tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First(); tspNode.Algorithm = tspGA; #endregion #region KSPTSPConnector var ksptspConnector = (INode)new KSPTSPConnector(); Nodes.Add(ksptspConnector); #endregion #region Wire Ports hook.Port = evaluatePort; configPort.ConnectedPort = paramsPort; evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"]; ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort; ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort; paramsPort.SendMessage(paramsPort.PrepareMessage()); #endregion } } }