Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14610


Ignore:
Timestamp:
01/26/17 14:12:39 (8 years ago)
Author:
jkarder
Message:

#2205: worked on optimization networks

  • added abstract base classes for ttp networks/orchestrators
  • removed ttp networks/orchestrators from HeuristicLab.Networks.IntegratedOptimization
  • runs can now be cleared when preparing OrchestratedAlgorithmNodes
Location:
branches/OptimizationNetworks
Files:
2 added
6 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpNetwork1.cs

    r14604 r14610  
    130130    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
    131131
    132     public void Prepare(bool clearRuns) { Prepare(); }
    133     public void Prepare() { Orchestrator.Prepare(); }
    134     public void Start() {
    135       if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
    136         Orchestrator.Prepare();
    137       Orchestrator.StartAsync();
    138     }
     132    public void Prepare() { Prepare(false); }
     133    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
     134    public void Start() { Orchestrator.StartAsync(); }
    139135    public void Pause() { Orchestrator.Pause(); }
    140136    public void Stop() { Orchestrator.Stop(); }
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpOrchestratorNode1.cs

    r14607 r14610  
    6161    int nrOfDepots, nrOfCustomers;
    6262    [Storable]
    63     double[,] depotCoordinates, customerCoordinates;
     63    double[,] depotCoordinates = new double[0, 0], customerCoordinates = new double[0, 0];
    6464    [Storable]
    6565    LrpUtils.DistanceType distanceType;
    6666    [Storable]
    67     double[] depotCapacities, customerDemands, depotCosts;
     67    double[] depotCapacities = new double[0], customerDemands = new double[0], depotCosts = new double[0];
    6868    [Storable]
    6969    double vehicleCapacity, vehicleCost;
     
    187187      foreach (var m in mutator.Operators)
    188188        mutator.Operators.SetItemCheckedState(m, Regex.IsMatch(m.Name, @"Potvin(One|Two).*"));
    189     }
    190 
    191     public override void Prepare() {
     189
     190      Prepare();
     191    }
     192
     193    public override void Prepare(bool clearRuns = false) {
    192194      Results.Clear();
    193195
    194196      var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    195       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
     197      var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
     198      if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
     199      metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
    196200      var problem = new VariegationProblem();
    197201      problem.Encoding.Length = FlpParameter.Value.Encoding.Length * 2;
     
    204208      cts = new CancellationTokenSource();
    205209
    206       try {
    207         var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    208         metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
    209         MetaSolverOrchestrationPort.SendMessage(metaMsg);
    210       } catch (Exception e) { }
     210      var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
     211      metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
     212      MetaSolverOrchestrationPort.SendMessage(metaMsg);
    211213    }
    212214
     
    272274
    273275      var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
    274       flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     276      flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    275277      flpMsg["Problem"] = flp;
    276278      FlpSolverOrchestrationPort.SendMessage(flpMsg);
     
    279281      var bestFlpSolution = (IntegerVector)flpResults["Best Solution"].Value;
    280282      var flpSolution = FlpParameter.Value.GetSolution(bestFlpSolution);
    281      
     283
    282284      var depots = bestFlpSolution.Select((x, i) => Tuple.Create(x, i)).GroupBy(x => x.Item1, x => x.Item2);
    283285      var vrpSolutions = new ResultCollection(depots.Count());
     
    296298
    297299        var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
    298         vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     300        vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    299301        vrpMsg["Problem"] = vrp;
    300302        VrpSolverOrchestrationPort.SendMessage(vrpMsg);
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/HeuristicLab.Networks.IntegratedOptimization.TravelingThief-3.3.csproj

    r14604 r14610  
    171171    <Compile Include="Problems\LootProfitProblem.cs" />
    172172    <Compile Include="Problems\TourProfitProblem.cs" />
     173    <Compile Include="TtpNetwork.cs" />
    173174    <Compile Include="TtpNetwork1.cs" />
    174175    <Compile Include="TtpNetwork2.cs" />
    175176    <Compile Include="TtpNetwork3.cs" />
     177    <Compile Include="TtpOrchestratorNode.cs" />
    176178    <Compile Include="TtpOrchestratorNode1.cs" />
    177179    <Compile Include="TtpOrchestratorNode2.cs" />
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/LootProfitProblem.cs

    r14604 r14610  
    8686      while (true) {
    8787        var neighbor = individual.Copy();
    88         SinglePositionBitflipManipulator.Apply(random, neighbor.BinaryVector()); break;
     88        SinglePositionBitflipManipulator.Apply(random, neighbor.BinaryVector());
    8989        yield return neighbor;
    9090      }
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork1.cs

    r14604 r14610  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using HeuristicLab.Algorithms.CMAEvolutionStrategy;
    2523using HeuristicLab.Algorithms.LocalSearch;
     
    2725using HeuristicLab.Common;
    2826using HeuristicLab.Core;
    29 using HeuristicLab.Core.Networks;
    3027using HeuristicLab.Optimization;
    3128using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3532  [Creatable("Optimization Networks")]
    3633  [StorableClass]
    37   public sealed class TtpNetwork1 : Network, IOptimizer {
    38     #region Nodes
    39     public TtpOrchestratorNode1 Orchestrator {
    40       get { return (TtpOrchestratorNode1)Nodes["Orchestrator"]; }
    41     }
    42 
    43     public OrchestratedAlgorithmNode MetaSolver {
    44       get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
    45     }
    46 
    47     public OrchestratedAlgorithmNode TspSolver {
    48       get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; }
    49     }
    50 
    51     public OrchestratedAlgorithmNode KspSolver {
    52       get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; }
    53     }
    54     #endregion
    55 
     34  public sealed class TtpNetwork1 : TtpNetwork, IOptimizer {
    5635    [StorableConstructor]
    5736    private TtpNetwork1(bool deserializing) : base(deserializing) { }
    58     private TtpNetwork1(TtpNetwork1 original, Cloner cloner) : base(original, cloner) {
    59       RegisterEvents();
    60     }
    61 
    62     private void RegisterEvents() {
    63       MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
    64       MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
    65       MetaSolver.Algorithm.Prepared += OnPrepared;
    66       MetaSolver.Algorithm.Started += OnStarted;
    67       MetaSolver.Algorithm.Paused += OnPaused;
    68       MetaSolver.Algorithm.Stopped += OnStopped;
    69       MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
    70     }
    71 
    72     public TtpNetwork1() : base("TtpNetwork1") {
    73       var orchestratorNode = new TtpOrchestratorNode1("Orchestrator");
    74       Nodes.Add(orchestratorNode);
    75 
    76       var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
     37    private TtpNetwork1(TtpNetwork1 original, Cloner cloner) : base(original, cloner) { }
     38    public TtpNetwork1() : this("TtpNetwork1") { }
     39    public TtpNetwork1(string name) : base(name) {
    7740      var cmaes = new CMAEvolutionStrategy();
    7841      cmaes.Problem = new VariegationProblem();
    7942      cmaes.MaximumGenerations = 80;
    80       metaSolverNode.Algorithm = cmaes;
    81       orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
    82       Nodes.Add(metaSolverNode);
     43      MetaSolver.Algorithm = cmaes;
     44      Orchestrator.MetaSolverOrchestrationPort.ConnectedPort = MetaSolver.OrchestrationPort;
    8345
    84       var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver");
    8546      var ls = new LocalSearch();
    8647      ls.Problem = new TourProfitProblem();
    8748      ls.MaximumIterations.Value = 100;
    8849      ls.SampleSize.Value = 2000;
    89       tspSolverNode.Algorithm = ls;
    90       orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort;
    91       Nodes.Add(tspSolverNode);
     50      TspSolver.Algorithm = ls;
     51      Orchestrator.TspSolverOrchestrationPort.ConnectedPort = TspSolver.OrchestrationPort;
    9252
    93       var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver");
    9453      var p3 = new ParameterlessPopulationPyramid();
    95       p3.Problem = orchestratorNode.KspParameter.Value;
     54      p3.Problem = Orchestrator.KspParameter.Value;
    9655      p3.MaximumRuntime = 3;
    97       kspSolverNode.Algorithm = p3;
    98       orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort;
    99       Nodes.Add(kspSolverNode);
    100 
    101       RegisterEvents();
     56      KspSolver.Algorithm = p3;
     57      Orchestrator.KspSolverOrchestrationPort.ConnectedPort = KspSolver.OrchestrationPort;
    10258    }
    10359
     
    10561      return new TtpNetwork1(this, cloner);
    10662    }
    107 
    108     [StorableHook(HookType.AfterDeserialization)]
    109     private void AfterDeserialization() {
    110       RegisterEvents();
    111     }
    112 
    113     #region IOptimizer Members
    114     public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
    115     public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
    116     public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
    117     public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
    118 
    119     public void Prepare(bool clearRuns) { Prepare(); }
    120     public void Prepare() { Orchestrator.Prepare(); }
    121     public void Start() {
    122       if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
    123         Orchestrator.Prepare();
    124       Orchestrator.StartAsync();
    125     }
    126     public void Pause() { Orchestrator.Pause(); }
    127     public void Stop() { Orchestrator.Stop(); }
    128     #endregion
    129 
    130     private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
    131     private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
    132     private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
    133     private void OnStarted(object sender, EventArgs e) { OnStarted(); }
    134     private void OnPaused(object sender, EventArgs e) { OnPaused(); }
    135     private void OnStopped(object sender, EventArgs e) { OnStopped(); }
    136     private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
    137 
    138     public event EventHandler ExecutionStateChanged;
    139     private void OnExecutionStateChanged() {
    140       var handler = ExecutionStateChanged;
    141       if (handler != null) handler(this, EventArgs.Empty);
    142     }
    143 
    144     public event EventHandler ExecutionTimeChanged;
    145     private void OnExecutionTimeChanged() {
    146       var handler = ExecutionTimeChanged;
    147       if (handler != null) handler(this, EventArgs.Empty);
    148     }
    149 
    150     public event EventHandler Prepared;
    151     private void OnPrepared() {
    152       var handler = Prepared;
    153       if (handler != null) handler(this, EventArgs.Empty);
    154     }
    155 
    156     public event EventHandler Started;
    157     private void OnStarted() {
    158       var handler = Started;
    159       if (handler != null) handler(this, EventArgs.Empty);
    160     }
    161 
    162     public event EventHandler Paused;
    163     private void OnPaused() {
    164       var handler = Paused;
    165       if (handler != null) handler(this, EventArgs.Empty);
    166     }
    167 
    168     public event EventHandler Stopped;
    169     private void OnStopped() {
    170       var handler = Stopped;
    171       if (handler != null) handler(this, EventArgs.Empty);
    172     }
    173 
    174     public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    175     private void OnExceptionOccurred(Exception exception) {
    176       var handler = ExceptionOccurred;
    177       if (handler != null) handler(this, new EventArgs<Exception>(exception));
    178     }
    17963  }
    18064}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork2.cs

    r14604 r14610  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using HeuristicLab.Algorithms.CMAEvolutionStrategy;
    2523using HeuristicLab.Algorithms.LocalSearch;
     
    2725using HeuristicLab.Common;
    2826using HeuristicLab.Core;
    29 using HeuristicLab.Core.Networks;
    3027using HeuristicLab.Optimization;
    3128using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3532  [Creatable("Optimization Networks")]
    3633  [StorableClass]
    37   public sealed class TtpNetwork2 : Network, IOptimizer {
    38     #region Nodes
    39     public TtpOrchestratorNode2 Orchestrator {
    40       get { return (TtpOrchestratorNode2)Nodes["Orchestrator"]; }
    41     }
    42 
    43     public OrchestratedAlgorithmNode MetaSolver {
    44       get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
    45     }
    46 
    47     public OrchestratedAlgorithmNode TspSolver {
    48       get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; }
    49     }
    50 
    51     public OrchestratedAlgorithmNode KspSolver {
    52       get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; }
    53     }
    54     #endregion
    55 
     34  public sealed class TtpNetwork2 : TtpNetwork, IOptimizer {
    5635    [StorableConstructor]
    5736    private TtpNetwork2(bool deserializing) : base(deserializing) { }
    58     private TtpNetwork2(TtpNetwork2 original, Cloner cloner) : base(original, cloner) {
    59       RegisterEvents();
    60     }
    61 
    62     private void RegisterEvents() {
    63       MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
    64       MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
    65       MetaSolver.Algorithm.Prepared += OnPrepared;
    66       MetaSolver.Algorithm.Started += OnStarted;
    67       MetaSolver.Algorithm.Paused += OnPaused;
    68       MetaSolver.Algorithm.Stopped += OnStopped;
    69       MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
    70     }
    71 
    72     public TtpNetwork2() : base("TtpNetwork2") {
    73       var orchestratorNode = new TtpOrchestratorNode2("Orchestrator");
    74       Nodes.Add(orchestratorNode);
    75 
    76       var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
     37    private TtpNetwork2(TtpNetwork2 original, Cloner cloner) : base(original, cloner) { }
     38    public TtpNetwork2() : this("TtpNetwork2") { }
     39    public TtpNetwork2(string name) : base(name) {
    7740      var cmaes = new CMAEvolutionStrategy();
    7841      cmaes.Problem = new VariegationProblem();
    7942      cmaes.MaximumGenerations = 80;
    80       metaSolverNode.Algorithm = cmaes;
    81       orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
    82       Nodes.Add(metaSolverNode);
     43      MetaSolver.Algorithm = cmaes;
     44      Orchestrator.MetaSolverOrchestrationPort.ConnectedPort = MetaSolver.OrchestrationPort;
    8345
    84       var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver");
    8546      var ls = new LocalSearch();
    86       ls.Problem = orchestratorNode.KspParameter.Value;
     47      ls.Problem = Orchestrator.KspParameter.Value;
    8748      ls.MaximumIterations.Value = 100;
    8849      ls.SampleSize.Value = 2000;
    89       tspSolverNode.Algorithm = ls;
    90       orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort;
    91       Nodes.Add(tspSolverNode);
     50      TspSolver.Algorithm = ls;
     51      Orchestrator.TspSolverOrchestrationPort.ConnectedPort = TspSolver.OrchestrationPort;
    9252
    93       var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver");
    9453      var p3 = new ParameterlessPopulationPyramid();
    9554      p3.Problem = new LootProfitProblem();
    9655      p3.MaximumRuntime = 3;
    97       kspSolverNode.Algorithm = p3;
    98       orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort;
    99       Nodes.Add(kspSolverNode);
    100 
    101       RegisterEvents();
     56      KspSolver.Algorithm = p3;
     57      Orchestrator.KspSolverOrchestrationPort.ConnectedPort = KspSolver.OrchestrationPort;
    10258    }
    10359
     
    10561      return new TtpNetwork2(this, cloner);
    10662    }
    107 
    108     [StorableHook(HookType.AfterDeserialization)]
    109     private void AfterDeserialization() {
    110       RegisterEvents();
    111     }
    112 
    113     #region IOptimizer Members
    114     public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
    115     public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
    116     public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
    117     public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
    118 
    119     public void Prepare(bool clearRuns) { Prepare(); }
    120     public void Prepare() { Orchestrator.Prepare(); }
    121     public void Start() {
    122       if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
    123         Orchestrator.Prepare();
    124       Orchestrator.StartAsync();
    125     }
    126     public void Pause() { Orchestrator.Pause(); }
    127     public void Stop() { Orchestrator.Stop(); }
    128     #endregion
    129 
    130     private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
    131     private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
    132     private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
    133     private void OnStarted(object sender, EventArgs e) { OnStarted(); }
    134     private void OnPaused(object sender, EventArgs e) { OnPaused(); }
    135     private void OnStopped(object sender, EventArgs e) { OnStopped(); }
    136     private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
    137 
    138     public event EventHandler ExecutionStateChanged;
    139     private void OnExecutionStateChanged() {
    140       var handler = ExecutionStateChanged;
    141       if (handler != null) handler(this, EventArgs.Empty);
    142     }
    143 
    144     public event EventHandler ExecutionTimeChanged;
    145     private void OnExecutionTimeChanged() {
    146       var handler = ExecutionTimeChanged;
    147       if (handler != null) handler(this, EventArgs.Empty);
    148     }
    149 
    150     public event EventHandler Prepared;
    151     private void OnPrepared() {
    152       var handler = Prepared;
    153       if (handler != null) handler(this, EventArgs.Empty);
    154     }
    155 
    156     public event EventHandler Started;
    157     private void OnStarted() {
    158       var handler = Started;
    159       if (handler != null) handler(this, EventArgs.Empty);
    160     }
    161 
    162     public event EventHandler Paused;
    163     private void OnPaused() {
    164       var handler = Paused;
    165       if (handler != null) handler(this, EventArgs.Empty);
    166     }
    167 
    168     public event EventHandler Stopped;
    169     private void OnStopped() {
    170       var handler = Stopped;
    171       if (handler != null) handler(this, EventArgs.Empty);
    172     }
    173 
    174     public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    175     private void OnExceptionOccurred(Exception exception) {
    176       var handler = ExceptionOccurred;
    177       if (handler != null) handler(this, new EventArgs<Exception>(exception));
    178     }
    17963  }
    18064}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork3.cs

    r14604 r14610  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using HeuristicLab.Algorithms.CMAEvolutionStrategy;
    2523using HeuristicLab.Algorithms.LocalSearch;
     
    2725using HeuristicLab.Common;
    2826using HeuristicLab.Core;
    29 using HeuristicLab.Core.Networks;
    3027using HeuristicLab.Optimization;
    3128using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3532  [Creatable("Optimization Networks")]
    3633  [StorableClass]
    37   public sealed class TtpNetwork3 : Network, IOptimizer {
    38     #region Nodes
    39     public TtpOrchestratorNode1 Orchestrator {
    40       get { return (TtpOrchestratorNode1)Nodes["Orchestrator"]; }
    41     }
    42 
    43     public OrchestratedAlgorithmNode MetaSolver {
    44       get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
    45     }
    46 
    47     public OrchestratedAlgorithmNode TspSolver {
    48       get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; }
    49     }
    50 
    51     public OrchestratedAlgorithmNode KspSolver {
    52       get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; }
    53     }
    54     #endregion
    55 
     34  public sealed class TtpNetwork3 : TtpNetwork, IOptimizer {
    5635    [StorableConstructor]
    5736    private TtpNetwork3(bool deserializing) : base(deserializing) { }
    58     private TtpNetwork3(TtpNetwork3 original, Cloner cloner) : base(original, cloner) {
    59       RegisterEvents();
    60     }
    61 
    62     private void RegisterEvents() {
    63       MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
    64       MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
    65       MetaSolver.Algorithm.Prepared += OnPrepared;
    66       MetaSolver.Algorithm.Started += OnStarted;
    67       MetaSolver.Algorithm.Paused += OnPaused;
    68       MetaSolver.Algorithm.Stopped += OnStopped;
    69       MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
    70     }
    71 
    72     public TtpNetwork3() : base("TtpNetwork3") {
    73       var orchestratorNode = new TtpOrchestratorNode3("Orchestrator");
    74       Nodes.Add(orchestratorNode);
    75 
    76       var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
     37    private TtpNetwork3(TtpNetwork3 original, Cloner cloner) : base(original, cloner) { }
     38    public TtpNetwork3() : this("TtpNetwork3") { }
     39    public TtpNetwork3(string name) : base(name) {
    7740      var cmaes = new CMAEvolutionStrategy();
    7841      cmaes.Problem = new VariegationProblem();
    7942      cmaes.MaximumGenerations = 80;
    80       metaSolverNode.Algorithm = cmaes;
    81       orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
    82       Nodes.Add(metaSolverNode);
     43      MetaSolver.Algorithm = cmaes;
     44      Orchestrator.MetaSolverOrchestrationPort.ConnectedPort = MetaSolver.OrchestrationPort;
    8345
    84       var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver");
    8546      var ls = new LocalSearch();
    86       ls.Problem = orchestratorNode.KspParameter.Value;
     47      ls.Problem = Orchestrator.KspParameter.Value;
    8748      ls.MaximumIterations.Value = 100;
    8849      ls.SampleSize.Value = 2000;
    89       tspSolverNode.Algorithm = ls;
    90       orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort;
    91       Nodes.Add(tspSolverNode);
     50      TspSolver.Algorithm = ls;
     51      Orchestrator.TspSolverOrchestrationPort.ConnectedPort = TspSolver.OrchestrationPort;
    9252
    93       var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver");
    9453      var p3 = new ParameterlessPopulationPyramid();
    95       p3.Problem = orchestratorNode.KspParameter.Value;
     54      p3.Problem = Orchestrator.KspParameter.Value;
    9655      p3.MaximumRuntime = 3;
    97       kspSolverNode.Algorithm = p3;
    98       orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort;
    99       Nodes.Add(kspSolverNode);
    100 
    101       RegisterEvents();
     56      KspSolver.Algorithm = p3;
     57      Orchestrator.KspSolverOrchestrationPort.ConnectedPort = KspSolver.OrchestrationPort;
    10258    }
    10359
     
    10561      return new TtpNetwork3(this, cloner);
    10662    }
    107 
    108     [StorableHook(HookType.AfterDeserialization)]
    109     private void AfterDeserialization() {
    110       RegisterEvents();
    111     }
    112 
    113     #region IOptimizer Members
    114     public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
    115     public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
    116     public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
    117     public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
    118 
    119     public void Prepare(bool clearRuns) { Prepare(); }
    120     public void Prepare() { Orchestrator.Prepare(); }
    121     public void Start() {
    122       if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
    123         Orchestrator.Prepare();
    124       Orchestrator.StartAsync();
    125     }
    126     public void Pause() { Orchestrator.Pause(); }
    127     public void Stop() { Orchestrator.Stop(); }
    128     #endregion
    129 
    130     private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
    131     private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
    132     private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
    133     private void OnStarted(object sender, EventArgs e) { OnStarted(); }
    134     private void OnPaused(object sender, EventArgs e) { OnPaused(); }
    135     private void OnStopped(object sender, EventArgs e) { OnStopped(); }
    136     private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
    137 
    138     public event EventHandler ExecutionStateChanged;
    139     private void OnExecutionStateChanged() {
    140       var handler = ExecutionStateChanged;
    141       if (handler != null) handler(this, EventArgs.Empty);
    142     }
    143 
    144     public event EventHandler ExecutionTimeChanged;
    145     private void OnExecutionTimeChanged() {
    146       var handler = ExecutionTimeChanged;
    147       if (handler != null) handler(this, EventArgs.Empty);
    148     }
    149 
    150     public event EventHandler Prepared;
    151     private void OnPrepared() {
    152       var handler = Prepared;
    153       if (handler != null) handler(this, EventArgs.Empty);
    154     }
    155 
    156     public event EventHandler Started;
    157     private void OnStarted() {
    158       var handler = Started;
    159       if (handler != null) handler(this, EventArgs.Empty);
    160     }
    161 
    162     public event EventHandler Paused;
    163     private void OnPaused() {
    164       var handler = Paused;
    165       if (handler != null) handler(this, EventArgs.Empty);
    166     }
    167 
    168     public event EventHandler Stopped;
    169     private void OnStopped() {
    170       var handler = Stopped;
    171       if (handler != null) handler(this, EventArgs.Empty);
    172     }
    173 
    174     public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    175     private void OnExceptionOccurred(Exception exception) {
    176       var handler = ExceptionOccurred;
    177       if (handler != null) handler(this, new EventArgs<Exception>(exception));
    178     }
    17963  }
    18064}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode1.cs

    r14604 r14610  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Threading;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    3129using HeuristicLab.Encodings.PermutationEncoding;
    3230using HeuristicLab.Encodings.RealVectorEncoding;
    33 using HeuristicLab.Operators;
    3431using HeuristicLab.Optimization;
    35 using HeuristicLab.Parameters;
    3632using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3733using HeuristicLab.Problems.Knapsack;
     
    4137  [Item("TtpOrchestratorNode1", "Orchestrator for TTP optimization network version 1.")]
    4238  [StorableClass]
    43   public sealed class TtpOrchestratorNode1 : OrchestratorNode {
    44     #region Constants
    45     private const string InstanceParameterName = "Instance";
    46     private const string TspParameterName = "TSP";
    47     private const string KspParameterName = "KSP";
    48     private const string AvailabilityParameterName = "Availability";
    49     private const string MinSpeedParameterName = "MinSpeed";
    50     private const string MaxSpeedParameterName = "MaxSpeed";
    51     private const string RentingRatioParameterName = "RentingRatio";
    52     private const string MetaSolverName = "MetaSolver";
    53     private const string TspSolverName = "TspSolver";
    54     private const string KspSolverName = "KspSolver";
    55     #endregion
    56 
    57     private CancellationTokenSource cts;
    58     private ResultCollection tspResults, kspResults;
    59 
    60     [Storable]
    61     double[,] tspCoordinates;
    62     [Storable]
    63     TtpUtils.DistanceType distanceType;
    64     [Storable]
    65     int kspCapacity;
    66     [Storable]
    67     int[] kspItemWeights, kspItemValues, ttpAvailability;
    68     [Storable]
    69     double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio;
    70 
    71     #region Parameters
    72     public IFixedValueParameter<TextFileValue> InstanceParameter {
    73       get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
    74     }
    75 
    76     public IValueParameter<TravelingSalesmanProblem> TspParameter {
    77       get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
    78     }
    79 
    80     public IValueParameter<BinaryKnapsackProblem> KspParameter {
    81       get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
    82     }
    83 
    84     public IValueParameter<IntArray> AvailabilityParameter {
    85       get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
    86     }
    87 
    88     public IValueParameter<DoubleValue> MinSpeedParameter {
    89       get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
    90     }
    91 
    92     public IValueParameter<DoubleValue> MaxSpeedParameter {
    93       get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
    94     }
    95 
    96     public IValueParameter<DoubleValue> RentingRatioParameter {
    97       get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
    98     }
    99     #endregion
    100 
    101     #region Ports
    102     public IMessagePort MetaSolverOrchestrationPort {
    103       get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
    104     }
    105 
    106     public IMessagePort MetaSolverEvaluationPort {
    107       get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
    108     }
    109 
    110     public IMessagePort TspSolverOrchestrationPort {
    111       get { return (IMessagePort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
    112     }
    113 
    114     public IMessagePort TspSolverEvaluationPort {
    115       get { return (IMessagePort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
    116     }
    117 
    118     public IMessagePort KspSolverOrchestrationPort {
    119       get { return (IMessagePort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
    120     }
    121 
    122     public IMessagePort KspSolverEvaluationPort {
    123       get { return (IMessagePort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
    124     }
    125     #endregion
    126 
     39  public sealed class TtpOrchestratorNode1 : TtpOrchestratorNode {
    12740    [StorableConstructor]
    12841    private TtpOrchestratorNode1(bool deserializing) : base(deserializing) { }
    129     private TtpOrchestratorNode1(TtpOrchestratorNode1 original, Cloner cloner) : base(original, cloner) {
    130       tspCoordinates = (double[,])original.tspCoordinates.Clone();
    131       kspCapacity = original.kspCapacity;
    132       kspItemWeights = (int[])original.kspItemWeights.Clone();
    133       kspItemValues = (int[])original.kspItemValues.Clone();
    134       ttpAvailability = (int[])original.ttpAvailability.Clone();
    135       ttpMinSpeed = original.ttpMinSpeed;
    136       ttpMaxSpeed = original.ttpMaxSpeed;
    137       ttpRentingRatio = original.ttpRentingRatio;
    138 
    139       RegisterEvents();
    140     }
     42    private TtpOrchestratorNode1(TtpOrchestratorNode1 original, Cloner cloner) : base(original, cloner) { }
    14143    public TtpOrchestratorNode1() : this("TtpOrchestratorNode1") { }
    142     public TtpOrchestratorNode1(string name) : base(name) {
    143       #region Configure Parameters
    144       Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
    145       Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
    146       Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
    147       Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
    148       Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
    149       Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
    150       Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
    151       #endregion
    152 
    153       #region Configure Ports
    154       AddOrchestrationPort<VariegationProblem>(MetaSolverName);
    155       AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
    156       AddOrchestrationPort<TourProfitProblem>(TspSolverName);
    157       AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
    158       AddOrchestrationPort<BinaryKnapsackProblem>(KspSolverName);
    159       AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
    160 
    161       RegisterEvents();
    162       #endregion
    163     }
     44    public TtpOrchestratorNode1(string name) : base(name) { }
    16445
    16546    public override IDeepCloneable Clone(Cloner cloner) {
     
    16748    }
    16849
    169     [StorableHook(HookType.AfterDeserialization)]
    170     private void AfterDeserialization() {
    171       RegisterEvents();
    172     }
    173 
    174     private void RegisterEvents() {
    175       InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
    176       MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
    177       TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
    178       KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
    179     }
    180 
    181     private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
    182       string filePath = InstanceParameter.Value.Value;
    183       TtpUtils.Import(filePath, out tspCoordinates, out distanceType,
    184                                 out kspCapacity, out kspItemValues, out kspItemWeights,
    185                                 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
    186 
    187       var tsp = TspParameter.Value;
    188       tsp.Coordinates = new DoubleMatrix(tspCoordinates);
    189 
    190       var ksp = KspParameter.Value;
    191       ksp.KnapsackCapacity.Value = kspCapacity;
    192       ksp.Encoding.Length = kspItemValues.Length;
    193       ksp.Values = new IntArray(kspItemValues);
    194       ksp.Weights = new IntArray(kspItemWeights);
    195 
    196       AvailabilityParameter.Value = new IntArray(ttpAvailability);
    197       MinSpeedParameter.Value.Value = ttpMinSpeed;
    198       MaxSpeedParameter.Value.Value = ttpMaxSpeed;
    199       RentingRatioParameter.Value.Value = ttpRentingRatio;
    200     }
    201 
    202     public override void Prepare() {
    203       Results.Clear();
    204 
    205       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    206       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
    207       var problem = new VariegationProblem();
    208       problem.Encoding.Length = KspParameter.Value.Length;
    209       problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
    210       metaMsg["Problem"] = problem;
    211       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    212     }
    213 
    214     public override void Start() {
    215       cts = new CancellationTokenSource();
    216 
    217       try {
    218         var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    219         metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
    220         MetaSolverOrchestrationPort.SendMessage(metaMsg);
    221       } catch (Exception e) { }
    222     }
    223 
    224     public override void Pause() {
    225       cts.Cancel();
    226 
    227       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    228       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
    229       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    230 
    231       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    232       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    233       TspSolverOrchestrationPort.SendMessage(tspMsg);
    234 
    235       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    236       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    237       KspSolverOrchestrationPort.SendMessage(kspMsg);
    238     }
    239 
    240     public override void Stop() {
    241       cts.Cancel();
    242 
    243       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    244       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    245       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    246 
    247       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    248       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    249       TspSolverOrchestrationPort.SendMessage(tspMsg);
    250 
    251       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    252       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    253       KspSolverOrchestrationPort.SendMessage(kspMsg);
    254     }
    255 
    256     protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
    257       var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
    258       messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
    259       messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
    260       messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
    261       messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
    262       messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
    263       messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
    264 
    265       messageActions[port](message);
    266 
    267       base.ProcessMessage(message, port, token);
    268     }
    269 
    27050    #region MetaSolver Message Handling
    271     private void MetaSolverOrchestrationPortMessage(IMessage message) { }
    272 
    273     private void MetaSolverEvaluationPortMessage(IMessage message) {
     51    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
    27452      var factors = (RealVector)message["RealVector"];
    27553
     
    27957
    28058      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    281       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     59      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    28260      kspMsg["Problem"] = ksp;
    28361      KspSolverOrchestrationPort.SendMessage(kspMsg);
     
    29371
    29472      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    295       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     73      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    29674      var tpp = new TourProfitProblem {
    29775        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
     
    323101        Results.Add(new Result("Best Tour", tour));
    324102        Results.Add(new Result("Best Loot", loot));
    325       } else if (((DoubleValue)bestQuality.Value).Value < objectiveValue) {
     103      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
    326104        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
    327105        Results["Best Tour"].Value = tour;
     
    331109    }
    332110    #endregion
    333 
    334     #region TspSolver Message Handling
    335     private void TspSolverOrchestrationPortMessage(IMessage message) {
    336       var results = (ResultCollection)message["Results"];
    337       if (results.ContainsKey("Best TSP Solution")) {
    338         tspResults = results;
    339       }
    340     }
    341 
    342     private void TspSolverEvaluationPortMessage(IMessage message) { }
    343     #endregion
    344 
    345     #region KspSolver Message Handling
    346     private void KspSolverOrchestrationPortMessage(IMessage message) {
    347       var results = (ResultCollection)message["Results"];
    348       if (results.ContainsKey("Best Solution")) {
    349         kspResults = results;
    350       }
    351     }
    352 
    353     private void KspSolverEvaluationPortMessage(IMessage message) { }
    354     #endregion
    355 
    356     #region Event Handlers
    357     private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    358       if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
    359 
    360       var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    361       if (node == null) return;
    362 
    363       var hook = new HookOperator { Name = "Meta Eval Hook" };
    364       hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
    365       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    366       node.EvalHook = hook;
    367 
    368       node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
    369       node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
    370       node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
    371     }
    372 
    373     private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    374       if (TspSolverOrchestrationPort.ConnectedPort == null) return;
    375 
    376       var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    377       if (node == null) return;
    378 
    379       var hook = new HookOperator { Name = "TSP Eval Hook" };
    380       hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
    381       hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
    382       node.EvalHook = hook;
    383 
    384       node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
    385       node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
    386       node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
    387     }
    388 
    389     private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    390       if (KspSolverOrchestrationPort.ConnectedPort == null) return;
    391 
    392       var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    393       if (node == null) return;
    394 
    395       var hook = new HookOperator { Name = "KSP Eval Hook" };
    396       hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
    397       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    398       node.EvalHook = hook;
    399 
    400       node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
    401       node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
    402       node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
    403     }
    404     #endregion
    405111  }
    406112}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode2.cs

    r14604 r14610  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Threading;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    2927using HeuristicLab.Data;
    3028using HeuristicLab.Encodings.BinaryVectorEncoding;
    31 using HeuristicLab.Encodings.PermutationEncoding;
    3229using HeuristicLab.Encodings.RealVectorEncoding;
    33 using HeuristicLab.Operators;
    3430using HeuristicLab.Optimization;
    35 using HeuristicLab.Parameters;
    3631using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3732using HeuristicLab.Problems.Knapsack;
     
    4136  [Item("TtpOrchestratorNode2", "Orchestrator for TTP optimization network version 2.")]
    4237  [StorableClass]
    43   public sealed class TtpOrchestratorNode2 : OrchestratorNode {
    44     #region Constants
    45     private const string InstanceParameterName = "Instance";
    46     private const string TspParameterName = "TSP";
    47     private const string KspParameterName = "KSP";
    48     private const string AvailabilityParameterName = "Availability";
    49     private const string MinSpeedParameterName = "MinSpeed";
    50     private const string MaxSpeedParameterName = "MaxSpeed";
    51     private const string RentingRatioParameterName = "RentingRatio";
    52     private const string MetaSolverName = "MetaSolver";
    53     private const string TspSolverName = "TspSolver";
    54     private const string KspSolverName = "KspSolver";
    55     #endregion
    56 
    57     private CancellationTokenSource cts;
    58     private ResultCollection tspResults, kspResults;
    59 
    60     [Storable]
    61     double[,] tspCoordinates;
    62     [Storable]
    63     TtpUtils.DistanceType distanceType;
    64     [Storable]
    65     int kspCapacity;
    66     [Storable]
    67     int[] kspItemWeights, kspItemValues, ttpAvailability;
    68     [Storable]
    69     double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio;
    70 
    71     #region Parameters
    72     public IFixedValueParameter<TextFileValue> InstanceParameter {
    73       get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
    74     }
    75 
    76     public IValueParameter<TravelingSalesmanProblem> TspParameter {
    77       get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
    78     }
    79 
    80     public IValueParameter<BinaryKnapsackProblem> KspParameter {
    81       get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
    82     }
    83 
    84     public IValueParameter<IntArray> AvailabilityParameter {
    85       get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
    86     }
    87 
    88     public IValueParameter<DoubleValue> MinSpeedParameter {
    89       get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
    90     }
    91 
    92     public IValueParameter<DoubleValue> MaxSpeedParameter {
    93       get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
    94     }
    95 
    96     public IValueParameter<DoubleValue> RentingRatioParameter {
    97       get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
    98     }
    99     #endregion
    100 
    101     #region Ports
    102     public IMessagePort MetaSolverOrchestrationPort {
    103       get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
    104     }
    105 
    106     public IMessagePort MetaSolverEvaluationPort {
    107       get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
    108     }
    109 
    110     public IMessagePort TspSolverOrchestrationPort {
    111       get { return (IMessagePort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
    112     }
    113 
    114     public IMessagePort TspSolverEvaluationPort {
    115       get { return (IMessagePort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
    116     }
    117 
    118     public IMessagePort KspSolverOrchestrationPort {
    119       get { return (IMessagePort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
    120     }
    121 
    122     public IMessagePort KspSolverEvaluationPort {
    123       get { return (IMessagePort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
    124     }
    125     #endregion
    126 
     38  public sealed class TtpOrchestratorNode2 : TtpOrchestratorNode {
    12739    [StorableConstructor]
    12840    private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { }
    129     private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) {
    130       tspCoordinates = (double[,])original.tspCoordinates.Clone();
    131       kspCapacity = original.kspCapacity;
    132       kspItemWeights = (int[])original.kspItemWeights.Clone();
    133       kspItemValues = (int[])original.kspItemValues.Clone();
    134       ttpAvailability = (int[])original.ttpAvailability.Clone();
    135       ttpMinSpeed = original.ttpMinSpeed;
    136       ttpMaxSpeed = original.ttpMaxSpeed;
    137       ttpRentingRatio = original.ttpRentingRatio;
    138 
    139       RegisterEvents();
    140     }
     41    private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
    14142    public TtpOrchestratorNode2() : this("TtpOrchestratorNode2") { }
    142     public TtpOrchestratorNode2(string name) : base(name) {
    143       #region Configure Parameters
    144       Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
    145       Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
    146       Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
    147       Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
    148       Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
    149       Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
    150       Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
    151       #endregion
    152 
    153       #region Configure Ports
    154       AddOrchestrationPort<VariegationProblem>(MetaSolverName);
    155       AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
    156       AddOrchestrationPort<TravelingSalesmanProblem>(TspSolverName);
    157       AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
    158       AddOrchestrationPort<LootProfitProblem>(KspSolverName);
    159       AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
    160 
    161       RegisterEvents();
    162       #endregion
    163     }
     43    public TtpOrchestratorNode2(string name) : base(name) { }
    16444
    16545    public override IDeepCloneable Clone(Cloner cloner) {
     
    16747    }
    16848
    169     [StorableHook(HookType.AfterDeserialization)]
    170     private void AfterDeserialization() {
    171       RegisterEvents();
    172     }
    173 
    174     private void RegisterEvents() {
    175       InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
    176       MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
    177       TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
    178       KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
    179     }
    180 
    181     private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
    182       string filePath = InstanceParameter.Value.Value;
    183       TtpUtils.Import(filePath, out tspCoordinates, out distanceType,
    184                                 out kspCapacity, out kspItemValues, out kspItemWeights,
    185                                 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
    186 
    187       var tsp = TspParameter.Value;
    188       tsp.Coordinates = new DoubleMatrix(tspCoordinates);
    189 
    190       var ksp = KspParameter.Value;
    191       ksp.KnapsackCapacity.Value = kspCapacity;
    192       ksp.Encoding.Length = kspItemValues.Length;
    193       ksp.Values = new IntArray(kspItemValues);
    194       ksp.Weights = new IntArray(kspItemWeights);
    195 
    196       AvailabilityParameter.Value = new IntArray(ttpAvailability);
    197       MinSpeedParameter.Value.Value = ttpMinSpeed;
    198       MaxSpeedParameter.Value.Value = ttpMaxSpeed;
    199       RentingRatioParameter.Value.Value = ttpRentingRatio;
    200     }
    201 
    202     public override void Prepare() {
    203       Results.Clear();
    204 
    205       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    206       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
    207       var problem = new VariegationProblem();
    208       problem.Encoding.Length = TspParameter.Value.Coordinates.Rows * 2;
    209       problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
    210       metaMsg["Problem"] = problem;
    211       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    212     }
    213 
    214     public override void Start() {
    215       cts = new CancellationTokenSource();
    216 
    217       try {
    218         var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    219         metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
    220         MetaSolverOrchestrationPort.SendMessage(metaMsg);
    221       } catch (Exception e) { }
    222     }
    223 
    224     public override void Pause() {
    225       cts.Cancel();
    226 
    227       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    228       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
    229       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    230 
    231       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    232       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    233       TspSolverOrchestrationPort.SendMessage(tspMsg);
    234 
    235       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    236       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    237       KspSolverOrchestrationPort.SendMessage(kspMsg);
    238     }
    239 
    240     public override void Stop() {
    241       cts.Cancel();
    242 
    243       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    244       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    245       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    246 
    247       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    248       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    249       TspSolverOrchestrationPort.SendMessage(tspMsg);
    250 
    251       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    252       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    253       KspSolverOrchestrationPort.SendMessage(kspMsg);
    254     }
    255 
    256     protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
    257       var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
    258       messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
    259       messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
    260       messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
    261       messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
    262       messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
    263       messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
    264 
    265       messageActions[port](message);
    266 
    267       base.ProcessMessage(message, port, token);
    268     }
    269 
    27049    #region MetaSolver Message Handling
    271     private void MetaSolverOrchestrationPortMessage(IMessage message) { }
    272 
    273     private void MetaSolverEvaluationPortMessage(IMessage message) {
     50    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
    27451      var factors = (RealVector)message["RealVector"];
    27552
     
    28158
    28259      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    283       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     60      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    28461      tspMsg["Problem"] = tsp;
    28562      TspSolverOrchestrationPort.SendMessage(tspMsg);
     
    29168
    29269      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    293       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     70      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    29471      var lpp = new LootProfitProblem {
    29572        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
     
    325102        Results.Add(new Result("Best Tour", tour));
    326103        Results.Add(new Result("Best Loot", loot));
    327       } else if (((DoubleValue)bestQuality.Value).Value < objectiveValue) {
     104      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
    328105        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
    329106        Results["Best Tour"].Value = tour;
     
    333110    }
    334111    #endregion
    335 
    336     #region TspSolver Message Handling
    337     private void TspSolverOrchestrationPortMessage(IMessage message) {
    338       var results = (ResultCollection)message["Results"];
    339       if (results.ContainsKey("Best TSP Solution")) {
    340         tspResults = results;
    341       }
    342     }
    343 
    344     private void TspSolverEvaluationPortMessage(IMessage message) { }
    345     #endregion
    346 
    347     #region KspSolver Message Handling
    348     private void KspSolverOrchestrationPortMessage(IMessage message) {
    349       var results = (ResultCollection)message["Results"];
    350       if (results.ContainsKey("Best Solution")) {
    351         kspResults = results;
    352       }
    353     }
    354 
    355     private void KspSolverEvaluationPortMessage(IMessage message) { }
    356     #endregion
    357 
    358     #region Event Handlers
    359     private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    360       if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
    361 
    362       var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    363       if (node == null) return;
    364 
    365       var hook = new HookOperator { Name = "Meta Eval Hook" };
    366       hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
    367       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    368       node.EvalHook = hook;
    369 
    370       node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
    371       node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
    372       node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
    373     }
    374 
    375     private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    376       if (TspSolverOrchestrationPort.ConnectedPort == null) return;
    377 
    378       var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    379       if (node == null) return;
    380 
    381       var hook = new HookOperator { Name = "TSP Eval Hook" };
    382       hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
    383       hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
    384       node.EvalHook = hook;
    385 
    386       node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
    387       node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
    388       node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
    389     }
    390 
    391     private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    392       if (KspSolverOrchestrationPort.ConnectedPort == null) return;
    393 
    394       var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    395       if (node == null) return;
    396 
    397       var hook = new HookOperator { Name = "KSP Eval Hook" };
    398       hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
    399       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    400       node.EvalHook = hook;
    401 
    402       node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
    403       node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
    404       node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
    405     }
    406     #endregion
    407112  }
    408113}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode3.cs

    r14604 r14610  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Threading;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    3129using HeuristicLab.Encodings.PermutationEncoding;
    3230using HeuristicLab.Encodings.RealVectorEncoding;
    33 using HeuristicLab.Operators;
    3431using HeuristicLab.Optimization;
    35 using HeuristicLab.Parameters;
    3632using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3733using HeuristicLab.Problems.Knapsack;
     
    4137  [Item("TtpOrchestratorNode3", "Orchestrator for TTP optimization network version 3.")]
    4238  [StorableClass]
    43   public sealed class TtpOrchestratorNode3 : OrchestratorNode {
    44     #region Constants
    45     private const string InstanceParameterName = "Instance";
    46     private const string TspParameterName = "TSP";
    47     private const string KspParameterName = "KSP";
    48     private const string AvailabilityParameterName = "Availability";
    49     private const string MinSpeedParameterName = "MinSpeed";
    50     private const string MaxSpeedParameterName = "MaxSpeed";
    51     private const string RentingRatioParameterName = "RentingRatio";
    52     private const string MetaSolverName = "MetaSolver";
    53     private const string TspSolverName = "TspSolver";
    54     private const string KspSolverName = "KspSolver";
    55     #endregion
    56 
    57     private CancellationTokenSource cts;
    58     private ResultCollection tspResults, kspResults;
    59 
    60     [Storable]
    61     double[,] tspCoordinates;
    62     [Storable]
    63     TtpUtils.DistanceType distanceType;
    64     [Storable]
    65     int kspCapacity;
    66     [Storable]
    67     int[] kspItemWeights, kspItemValues, ttpAvailability;
    68     [Storable]
    69     double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio;
    70 
    71     #region Parameters
    72     public IFixedValueParameter<TextFileValue> InstanceParameter {
    73       get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
    74     }
    75 
    76     public IValueParameter<TravelingSalesmanProblem> TspParameter {
    77       get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
    78     }
    79 
    80     public IValueParameter<BinaryKnapsackProblem> KspParameter {
    81       get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
    82     }
    83 
    84     public IValueParameter<IntArray> AvailabilityParameter {
    85       get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
    86     }
    87 
    88     public IValueParameter<DoubleValue> MinSpeedParameter {
    89       get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
    90     }
    91 
    92     public IValueParameter<DoubleValue> MaxSpeedParameter {
    93       get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
    94     }
    95 
    96     public IValueParameter<DoubleValue> RentingRatioParameter {
    97       get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
    98     }
    99     #endregion
    100 
    101     #region Ports
    102     public IMessagePort MetaSolverOrchestrationPort {
    103       get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
    104     }
    105 
    106     public IMessagePort MetaSolverEvaluationPort {
    107       get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
    108     }
    109 
    110     public IMessagePort TspSolverOrchestrationPort {
    111       get { return (IMessagePort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
    112     }
    113 
    114     public IMessagePort TspSolverEvaluationPort {
    115       get { return (IMessagePort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
    116     }
    117 
    118     public IMessagePort KspSolverOrchestrationPort {
    119       get { return (IMessagePort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
    120     }
    121 
    122     public IMessagePort KspSolverEvaluationPort {
    123       get { return (IMessagePort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
    124     }
    125     #endregion
    126 
     39  public sealed class TtpOrchestratorNode3 : TtpOrchestratorNode {
    12740    [StorableConstructor]
    12841    private TtpOrchestratorNode3(bool deserializing) : base(deserializing) { }
    129     private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) {
    130       tspCoordinates = (double[,])original.tspCoordinates.Clone();
    131       kspCapacity = original.kspCapacity;
    132       kspItemWeights = (int[])original.kspItemWeights.Clone();
    133       kspItemValues = (int[])original.kspItemValues.Clone();
    134       ttpAvailability = (int[])original.ttpAvailability.Clone();
    135       ttpMinSpeed = original.ttpMinSpeed;
    136       ttpMaxSpeed = original.ttpMaxSpeed;
    137       ttpRentingRatio = original.ttpRentingRatio;
    138 
    139       RegisterEvents();
    140     }
     42    private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) { }
    14143    public TtpOrchestratorNode3() : this("TtpOrchestratorNode3") { }
    142     public TtpOrchestratorNode3(string name) : base(name) {
    143       #region Configure Parameters
    144       Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
    145       Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
    146       Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
    147       Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
    148       Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
    149       Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
    150       Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
    151       #endregion
    152 
    153       #region Configure Ports
    154       AddOrchestrationPort<VariegationProblem>(MetaSolverName);
    155       AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
    156       AddOrchestrationPort<TravelingSalesmanProblem>(TspSolverName);
    157       AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
    158       AddOrchestrationPort<BinaryKnapsackProblem>(KspSolverName);
    159       AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
    160 
    161       RegisterEvents();
    162       #endregion
    163     }
     44    public TtpOrchestratorNode3(string name) : base(name) { }
    16445
    16546    public override IDeepCloneable Clone(Cloner cloner) {
     
    16748    }
    16849
    169     [StorableHook(HookType.AfterDeserialization)]
    170     private void AfterDeserialization() {
    171       RegisterEvents();
    172     }
    173 
    174     private void RegisterEvents() {
    175       InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
    176       MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
    177       TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
    178       KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
    179     }
    180 
    181     private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
    182       string filePath = InstanceParameter.Value.Value;
    183       TtpUtils.Import(filePath, out tspCoordinates, out distanceType,
    184                                 out kspCapacity, out kspItemValues, out kspItemWeights,
    185                                 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
    186 
    187       var tsp = TspParameter.Value;
    188       tsp.Coordinates = new DoubleMatrix(tspCoordinates);
    189 
    190       var ksp = KspParameter.Value;
    191       ksp.KnapsackCapacity.Value = kspCapacity;
    192       ksp.Encoding.Length = kspItemValues.Length;
    193       ksp.Values = new IntArray(kspItemValues);
    194       ksp.Weights = new IntArray(kspItemWeights);
    195 
    196       AvailabilityParameter.Value = new IntArray(ttpAvailability);
    197       MinSpeedParameter.Value.Value = ttpMinSpeed;
    198       MaxSpeedParameter.Value.Value = ttpMaxSpeed;
    199       RentingRatioParameter.Value.Value = ttpRentingRatio;
    200     }
    201 
    202     public override void Prepare() {
    203       Results.Clear();
    204 
    205       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    206       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
    207       var problem = new VariegationProblem();
    208       problem.Encoding.Length = KspParameter.Value.Length + TspParameter.Value.Coordinates.Rows * 2;
    209       problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
    210       metaMsg["Problem"] = problem;
    211       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    212     }
    213 
    214     public override void Start() {
    215       cts = new CancellationTokenSource();
    216 
    217       try {
    218         var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    219         metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
    220         MetaSolverOrchestrationPort.SendMessage(metaMsg);
    221       } catch (Exception e) { }
    222     }
    223 
    224     public override void Pause() {
    225       cts.Cancel();
    226 
    227       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    228       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
    229       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    230 
    231       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    232       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    233       TspSolverOrchestrationPort.SendMessage(tspMsg);
    234 
    235       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    236       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    237       KspSolverOrchestrationPort.SendMessage(kspMsg);
    238     }
    239 
    240     public override void Stop() {
    241       cts.Cancel();
    242 
    243       var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
    244       metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    245       MetaSolverOrchestrationPort.SendMessage(metaMsg);
    246 
    247       var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    248       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    249       TspSolverOrchestrationPort.SendMessage(tspMsg);
    250 
    251       var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    252       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
    253       KspSolverOrchestrationPort.SendMessage(kspMsg);
    254     }
    255 
    256     protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
    257       var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
    258       messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
    259       messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
    260       messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
    261       messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
    262       messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
    263       messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
    264 
    265       messageActions[port](message);
    266 
    267       base.ProcessMessage(message, port, token);
    268     }
    269 
    27050    #region MetaSolver Message Handling
    271     private void MetaSolverOrchestrationPortMessage(IMessage message) { }
    272 
    273     private void MetaSolverEvaluationPortMessage(IMessage message) {
     51    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
    27452      var factors = (RealVector)message["RealVector"];
    27553      int fi = 0;
     
    28260
    28361      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
    284       kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     62      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    28563      kspMsg["Problem"] = ksp;
    28664      KspSolverOrchestrationPort.SendMessage(kspMsg);
     
    30280
    30381      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
    304       tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
     82      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
    30583      var tpp = new TourProfitProblem {
    30684        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
     
    332110        Results.Add(new Result("Best Tour", tour));
    333111        Results.Add(new Result("Best Loot", loot));
    334       } else if (((DoubleValue)bestQuality.Value).Value < objectiveValue) {
     112      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
    335113        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
    336114        Results["Best Tour"].Value = tour;
     
    340118    }
    341119    #endregion
    342 
    343     #region TspSolver Message Handling
    344     private void TspSolverOrchestrationPortMessage(IMessage message) {
    345       var results = (ResultCollection)message["Results"];
    346       if (results.ContainsKey("Best TSP Solution")) {
    347         tspResults = results;
    348       }
    349     }
    350 
    351     private void TspSolverEvaluationPortMessage(IMessage message) { }
    352     #endregion
    353 
    354     #region KspSolver Message Handling
    355     private void KspSolverOrchestrationPortMessage(IMessage message) {
    356       var results = (ResultCollection)message["Results"];
    357       if (results.ContainsKey("Best Solution")) {
    358         kspResults = results;
    359       }
    360     }
    361 
    362     private void KspSolverEvaluationPortMessage(IMessage message) { }
    363     #endregion
    364 
    365     #region Event Handlers
    366     private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    367       if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
    368 
    369       var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    370       if (node == null) return;
    371 
    372       var hook = new HookOperator { Name = "Meta Eval Hook" };
    373       hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
    374       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    375       node.EvalHook = hook;
    376 
    377       node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
    378       node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
    379       node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
    380     }
    381 
    382     private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    383       if (TspSolverOrchestrationPort.ConnectedPort == null) return;
    384 
    385       var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    386       if (node == null) return;
    387 
    388       var hook = new HookOperator { Name = "TSP Eval Hook" };
    389       hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
    390       hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
    391       node.EvalHook = hook;
    392 
    393       node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
    394       node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
    395       node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
    396     }
    397 
    398     private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
    399       if (KspSolverOrchestrationPort.ConnectedPort == null) return;
    400 
    401       var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
    402       if (node == null) return;
    403 
    404       var hook = new HookOperator { Name = "KSP Eval Hook" };
    405       hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
    406       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
    407       node.EvalHook = hook;
    408 
    409       node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
    410       node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
    411       node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
    412     }
    413     #endregion
    414120  }
    415121}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.Views/3.3/OrchestratorNodeView.cs

    r14586 r14610  
    4949
    5050    private void prepareButton_Click(object sender, System.EventArgs e) {
    51       Content.PrepareAsync();
     51      Content.PrepareAsync(false);
    5252    }
    5353  }
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/HeuristicLab.Networks.IntegratedOptimization-3.3.csproj

    r14601 r14610  
    231231    <Compile Include="OrchestratorNode.cs" />
    232232    <Compile Include="VariegationProblem.cs" />
    233     <Compile Include="TtpNetwork.cs" />
    234     <Compile Include="TtpNetwork2.cs" />
    235     <Compile Include="TtpNetwork4.cs" />
    236     <Compile Include="TtpOrchestratorNode1.cs" />
    237     <Compile Include="TtpOrchestratorNode2.cs" />
    238     <Compile Include="TtpOrchestratorNode4.cs" />
    239233    <None Include="Plugin.cs.frame" />
    240234    <None Include="Properties\AssemblyInfo.cs.frame" />
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/IOrchestratorNode.cs

    r14586 r14610  
    1 using System.Threading.Tasks;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System.Threading.Tasks;
    223using HeuristicLab.Core;
    324using HeuristicLab.Core.Networks;
     
    930    ResultCollection Results { get; }
    1031
    11     void Prepare();
    12     Task PrepareAsync();
     32    void Prepare(bool clearRuns = false);
     33    Task PrepareAsync(bool clearRuns = false);
    1334    void Start();
    1435    Task StartAsync();
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/OrchestratedAlgorithmNode.cs

    r14601 r14610  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
     
    86107              var prob = (IHeuristicOptimizationProblem)problemMsgVal.Value.Clone();
    87108
    88               if (message.HasFlag(OrchestrationMessage.QualityAdaption)) {
     109              if (message.HasFlag(OrchestrationMessage.SetEvalHook)) {
    89110                var instEval = prob.Evaluator as InstrumentedOperator;
    90111                if (instEval != null && EvalHook != null) instEval.AfterExecutionOperators.Add(EvalHook);
     
    116137              }
    117138
    118               Algorithm.Prepare();
     139              Algorithm.Prepare(true);
    119140            }
    120141            break;
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/OrchestrationMessage.cs

    r14601 r14610  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2525  [Flags]
    2626  public enum OrchestrationMessage {
    27     Unknown = 0x0,
    28     Prepare = 0x1,
    29     Start = 0x2,
    30     Pause = 0x4,
    31     Stop = 0x8,
    32     QualityAdaption = 0x100,
     27    Unknown = 0,
     28    Prepare = 1,
     29    Start = 1 << 1,       // 2
     30    Pause = 1 << 2,       // 4
     31    Stop = 1 << 3,        // 8
     32    SetEvalHook = 1 << 8, // 256
     33    ClearRuns = 1 << 9,   // 512
    3334  }
    3435}
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/OrchestratorNode.cs

    r14598 r14610  
    1 using System.Threading;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System.Threading;
    223using System.Threading.Tasks;
    324using HeuristicLab.Collections;
     
    104125    protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
    105126
    106     public abstract void Prepare();
    107     public async Task PrepareAsync() { await Task.Run(() => Prepare()); }
     127    public abstract void Prepare(bool clearRuns = false);
     128    public async Task PrepareAsync(bool clearRuns = false) { await Task.Run(() => Prepare(clearRuns)); }
    108129    public abstract void Start();
    109130    public async Task StartAsync() { await Task.Run(() => Start()); }
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/VariegationProblem.cs

    r14604 r14610  
    1 using HeuristicLab.Common;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using HeuristicLab.Common;
    223using HeuristicLab.Core;
    324using HeuristicLab.Encodings.RealVectorEncoding;
Note: See TracChangeset for help on using the changeset viewer.