Changeset 14601 for branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork3.cs
- Timestamp:
- 01/24/17 09:31:29 (7 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief
- Files:
-
- 2 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork3.cs
r14598 r14601 1 //using HeuristicLab.Algorithms.CMAEvolutionStrategy; 2 //using HeuristicLab.Algorithms.LocalSearch; 3 //using HeuristicLab.Algorithms.ParameterlessPopulationPyramid; 4 //using HeuristicLab.Common; 5 //using HeuristicLab.Core; 6 //using HeuristicLab.Core.Networks; 7 //using HeuristicLab.Data; 8 //using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 9 10 //namespace HeuristicLab.Networks.IntegratedOptimization { 11 // [Item("TtpNetwork3", "An optimization network for the TTP.")] 12 // [Creatable("Optimization Networks")] 13 // [StorableClass] 14 // // standard ttp 15 // // cmaes variegates ksp values and tsp coordinates 16 // // 1) init cmaes (length = ksp.values.length) 17 // // 2) start cmaes 18 // // 3) evaluate vector as follows: 19 // // 4) change ksp values and tsp coordinates (mult by factor in vector) 20 // // 5) best ksp 21 // // 6) best tsp 22 // // 7) return ttp quality 23 // public sealed class TtpNetwork3 : Network { 24 // [StorableConstructor] 25 // private TtpNetwork3(bool deserializing) : base(deserializing) { } 26 // private TtpNetwork3(TtpNetwork3 original, Cloner cloner) : base(original, cloner) { } 27 // public TtpNetwork3() : base("TtpNetwork3") { 28 // var orchestratorNode = new TtpOrchestratorNode3(); 29 // Nodes.Add(orchestratorNode); 30 31 // var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver"); 32 // var cmaes = new CMAEvolutionStrategy(); 33 // cmaes.MaximumEvaluatedSolutions = 3000; 34 // cmaes.Engine = new ParallelEngine.ParallelEngine(); 35 // metaSolverNode.Algorithm = cmaes; 36 // orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort; 37 // Nodes.Add(metaSolverNode); 38 39 // var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver") { CloneAlgorithm = true }; 40 // var ls = new LocalSearch(); 41 // ls.Problem = orchestratorNode.TspParameter.Value; 42 // ls.MaximumIterations.Value = 100; 43 // tspSolverNode.Algorithm = ls; 44 // orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort; 45 // Nodes.Add(tspSolverNode); 46 47 // var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver") { CloneAlgorithm = true }; 48 // var p3 = new ParameterlessPopulationPyramid(); 49 // p3.Problem = orchestratorNode.KspParameter.Value; 50 // p3.MaximumRuntime = 3; 51 // kspSolverNode.Algorithm = p3; 52 // orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort; 53 // Nodes.Add(kspSolverNode); 54 55 // #region Import 56 // DoubleMatrix tspCoordinates; 57 // IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues; 58 // IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio; 59 // TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp", 60 // out tspCoordinates, 61 // out kspCapacity, out kspItemValues, out kspItemWeights, 62 // out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio); 63 64 // var tsp = orchestratorNode.TspParameter.Value; 65 // tsp.Coordinates = tspCoordinates; 66 67 // var ksp = orchestratorNode.KspParameter.Value; 68 // ksp.KnapsackCapacity = kspCapacity; 69 // ksp.Encoding.Length = kspItemValues.Length; 70 // ksp.Values = kspItemValues; 71 // ksp.Weights = kspItemWeights; 72 73 // orchestratorNode.AvailabilityParameter.Value = ttpAvailability; 74 // orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed; 75 // orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed; 76 // orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio; 77 // #endregion 78 // } 79 80 // public override IDeepCloneable Clone(Cloner cloner) { 81 // return new TtpNetwork3(this, cloner); 82 // } 83 // } 84 //} 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 22 using System; 23 using System.Collections.Generic; 24 using HeuristicLab.Algorithms.CMAEvolutionStrategy; 25 using HeuristicLab.Algorithms.LocalSearch; 26 using HeuristicLab.Algorithms.ParameterlessPopulationPyramid; 27 using HeuristicLab.Common; 28 using HeuristicLab.Core; 29 using HeuristicLab.Core.Networks; 30 using HeuristicLab.Data; 31 using HeuristicLab.Optimization; 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 34 namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief { 35 [Item("TtpNetwork3", "Version 3 of a TTP optimization network.")] 36 [Creatable("Optimization Networks")] 37 [StorableClass] 38 public sealed class TtpNetwork3 : Network, IOptimizer { 39 #region Nodes 40 public TtpOrchestratorNode1 Orchestrator { 41 get { return (TtpOrchestratorNode1)Nodes["Orchestrator"]; } 42 } 43 44 public OrchestratedAlgorithmNode MetaSolver { 45 get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; } 46 } 47 48 public OrchestratedAlgorithmNode TspSolver { 49 get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; } 50 } 51 52 public OrchestratedAlgorithmNode KspSolver { 53 get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; } 54 } 55 #endregion 56 57 [StorableConstructor] 58 private TtpNetwork3(bool deserializing) : base(deserializing) { } 59 private TtpNetwork3(TtpNetwork3 original, Cloner cloner) : base(original, cloner) { 60 RegisterEvents(); 61 } 62 63 private void RegisterEvents() { 64 MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged; 65 MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged; 66 MetaSolver.Algorithm.Prepared += OnPrepared; 67 MetaSolver.Algorithm.Started += OnStarted; 68 MetaSolver.Algorithm.Paused += OnPaused; 69 MetaSolver.Algorithm.Stopped += OnStopped; 70 MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred; 71 } 72 73 public TtpNetwork3() : base("TtpNetwork3") { 74 var orchestratorNode = new TtpOrchestratorNode3("Orchestrator"); 75 Nodes.Add(orchestratorNode); 76 77 var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver"); 78 var cmaes = new CMAEvolutionStrategy(); 79 cmaes.Problem = new VariegationProblem(); 80 cmaes.MaximumGenerations = 80; 81 metaSolverNode.Algorithm = cmaes; 82 orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort; 83 Nodes.Add(metaSolverNode); 84 85 var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver"); 86 var ls = new LocalSearch(); 87 ls.Problem = orchestratorNode.KspParameter.Value; 88 ls.MaximumIterations.Value = 100; 89 ls.SampleSize.Value = 2000; 90 tspSolverNode.Algorithm = ls; 91 orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort; 92 Nodes.Add(tspSolverNode); 93 94 var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver"); 95 var p3 = new ParameterlessPopulationPyramid(); 96 p3.Problem = orchestratorNode.KspParameter.Value; 97 p3.MaximumRuntime = 3; 98 kspSolverNode.Algorithm = p3; 99 orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort; 100 Nodes.Add(kspSolverNode); 101 102 #region Import 103 DoubleMatrix tspCoordinates; 104 IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues; 105 IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio; 106 TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp", 107 out tspCoordinates, 108 out kspCapacity, out kspItemValues, out kspItemWeights, 109 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio); 110 111 var tsp = orchestratorNode.TspParameter.Value; 112 tsp.Coordinates = tspCoordinates; 113 114 var ksp = orchestratorNode.KspParameter.Value; 115 ksp.KnapsackCapacity = kspCapacity; 116 ksp.Encoding.Length = kspItemValues.Length; 117 ksp.Values = kspItemValues; 118 ksp.Weights = kspItemWeights; 119 120 orchestratorNode.AvailabilityParameter.Value = ttpAvailability; 121 orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed; 122 orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed; 123 orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio; 124 #endregion 125 126 RegisterEvents(); 127 } 128 129 public override IDeepCloneable Clone(Cloner cloner) { 130 return new TtpNetwork3(this, cloner); 131 } 132 133 [StorableHook(HookType.AfterDeserialization)] 134 private void AfterDeserialization() { 135 RegisterEvents(); 136 } 137 138 #region IOptimizer Members 139 public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } } 140 public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } } 141 public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } } 142 public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } } 143 144 public void Prepare(bool clearRuns) { Prepare(); } 145 public void Prepare() { Orchestrator.Prepare(); } 146 public void Start() { 147 if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared) 148 Orchestrator.Prepare(); 149 Orchestrator.StartAsync(); 150 } 151 public void Pause() { Orchestrator.Pause(); } 152 public void Stop() { Orchestrator.Stop(); } 153 #endregion 154 155 private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); } 156 private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); } 157 private void OnPrepared(object sender, EventArgs e) { OnPrepared(); } 158 private void OnStarted(object sender, EventArgs e) { OnStarted(); } 159 private void OnPaused(object sender, EventArgs e) { OnPaused(); } 160 private void OnStopped(object sender, EventArgs e) { OnStopped(); } 161 private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); } 162 163 public event EventHandler ExecutionStateChanged; 164 private void OnExecutionStateChanged() { 165 var handler = ExecutionStateChanged; 166 if (handler != null) handler(this, EventArgs.Empty); 167 } 168 169 public event EventHandler ExecutionTimeChanged; 170 private void OnExecutionTimeChanged() { 171 var handler = ExecutionTimeChanged; 172 if (handler != null) handler(this, EventArgs.Empty); 173 } 174 175 public event EventHandler Prepared; 176 private void OnPrepared() { 177 var handler = Prepared; 178 if (handler != null) handler(this, EventArgs.Empty); 179 } 180 181 public event EventHandler Started; 182 private void OnStarted() { 183 var handler = Started; 184 if (handler != null) handler(this, EventArgs.Empty); 185 } 186 187 public event EventHandler Paused; 188 private void OnPaused() { 189 var handler = Paused; 190 if (handler != null) handler(this, EventArgs.Empty); 191 } 192 193 public event EventHandler Stopped; 194 private void OnStopped() { 195 var handler = Stopped; 196 if (handler != null) handler(this, EventArgs.Empty); 197 } 198 199 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; 200 private void OnExceptionOccurred(Exception exception) { 201 var handler = ExceptionOccurred; 202 if (handler != null) handler(this, new EventArgs<Exception>(exception)); 203 } 204 } 205 }
Note: See TracChangeset
for help on using the changeset viewer.