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