Changeset 14610 for branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork2.cs
- Timestamp:
- 01/26/17 14:12:39 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork2.cs
r14604 r14610 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using HeuristicLab.Algorithms.CMAEvolutionStrategy; 25 23 using HeuristicLab.Algorithms.LocalSearch; … … 27 25 using HeuristicLab.Common; 28 26 using HeuristicLab.Core; 29 using HeuristicLab.Core.Networks;30 27 using HeuristicLab.Optimization; 31 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 35 32 [Creatable("Optimization Networks")] 36 33 [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 { 56 35 [StorableConstructor] 57 36 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) { 77 40 var cmaes = new CMAEvolutionStrategy(); 78 41 cmaes.Problem = new VariegationProblem(); 79 42 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; 83 45 84 var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver");85 46 var ls = new LocalSearch(); 86 ls.Problem = orchestratorNode.KspParameter.Value;47 ls.Problem = Orchestrator.KspParameter.Value; 87 48 ls.MaximumIterations.Value = 100; 88 49 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; 92 52 93 var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver");94 53 var p3 = new ParameterlessPopulationPyramid(); 95 54 p3.Problem = new LootProfitProblem(); 96 55 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; 102 58 } 103 59 … … 105 61 return new TtpNetwork2(this, cloner); 106 62 } 107 108 [StorableHook(HookType.AfterDeserialization)]109 private void AfterDeserialization() {110 RegisterEvents();111 }112 113 #region IOptimizer Members114 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 #endregion129 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 }179 63 } 180 64 }
Note: See TracChangeset
for help on using the changeset viewer.