Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/14 05:32:41 (10 years ago)
Author:
swagner
Message:

#2205: Continued working on programmable network items

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs

    r11530 r11564  
    2020#endregion
    2121
    22 using HeuristicLab.Algorithms.GeneticAlgorithm;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    2524using HeuristicLab.Core.Networks;
    26 using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.BinaryVectorEncoding;
    28 using HeuristicLab.Operators;
    29 using HeuristicLab.Parameters;
    3025using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Problems.Knapsack;
    32 using HeuristicLab.Problems.TravelingSalesman;
    33 using System.Linq;
     26using System.Drawing;
    3427
    3528namespace HeuristicLab.Optimization.Networks.KSPTSP {
     
    3730  [Creatable("Optimization Networks")]
    3831  [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
    4041    [StorableConstructor]
    4142    protected KSPTSPNetwork(bool deserializing) : base(deserializing) { }
    4243    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) { }
    5547
    5648    public override IDeepCloneable Clone(Cloner cloner) {
    5749      return new KSPTSPNetwork(this, cloner);
    5850    }
    59 
    60     protected virtual void Initialize() {
    61       #region ParametersNode
    62       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       #endregion
    95 
    96       #region KSPNode
    97       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.Input
    105       });
    106       configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
    107         Type = PortParameterType.Input
    108       });
    109       configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
    110         Type = PortParameterType.Input
    111       });
    112 
    113       var evaluatePort = new MessagePort("EvaluateRoute");
    114       kspNode.Ports.Add(evaluatePort);
    115 
    116       evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
    117         Type = PortParameterType.Output
    118       });
    119       evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
    120         Type = PortParameterType.Output | PortParameterType.Input
    121       });
    122       evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
    123         Type = PortParameterType.Input
    124       });
    125       evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
    126         Type = PortParameterType.Input
    127       });
    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       #endregion
    143 
    144       #region TSPNode
    145       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.Input
    153       });
    154       executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
    155         Type = PortParameterType.Output
    156       });
    157       executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
    158         Type = PortParameterType.Output
    159       });
    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       #endregion
    168 
    169       #region KSPTSPConnector
    170       var ksptspConnector = (INode)new KSPTSPConnector();
    171       Nodes.Add(ksptspConnector);
    172       #endregion
    173 
    174       #region Wire Ports
    175       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       #endregion
    183     }
    18451  }
    18552}
Note: See TracChangeset for help on using the changeset viewer.