Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpNetwork1.cs @ 14613

Last change on this file since 14613 was 14613, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

  • fixed event handler registration
File size: 8.0 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Algorithms.CMAEvolutionStrategy;
26using HeuristicLab.Algorithms.GeneticAlgorithm;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Networks;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.FacilityLocation;
33using HeuristicLab.Problems.FacilityLocation.CplexSolver;
34using HeuristicLab.Problems.VehicleRouting;
35using HeuristicLab.Problems.VehicleRouting.Encodings.General;
36using HeuristicLab.Selection;
37
38namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
39  [Item("LrpNetwork1", "Version 1 of a TTP optimization network.")]
40  [Creatable("Optimization Networks")]
41  [StorableClass]
42  public sealed class LrpNetwork1 : Network, IOptimizer {
43    #region Nodes
44    public LrpOrchestratorNode1 Orchestrator {
45      get { return (LrpOrchestratorNode1)Nodes["Orchestrator"]; }
46    }
47
48    public OrchestratedAlgorithmNode MetaSolver {
49      get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
50    }
51
52    public OrchestratedAlgorithmNode LrpSolver {
53      get { return (OrchestratedAlgorithmNode)Nodes["LrpSolver"]; }
54    }
55
56    public OrchestratedAlgorithmNode VrpSolver {
57      get { return (OrchestratedAlgorithmNode)Nodes["VrpSolver"]; }
58    }
59    #endregion
60
61    [StorableConstructor]
62    private LrpNetwork1(bool deserializing) : base(deserializing) { }
63    private LrpNetwork1(LrpNetwork1 original, Cloner cloner) : base(original, cloner) {
64      RegisterEvents();
65    }
66    public LrpNetwork1() : base("LrpNetwork1") {
67      var orchestratorNode = new LrpOrchestratorNode1("Orchestrator");
68      Nodes.Add(orchestratorNode);
69
70      var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
71      var cmaes = new CMAEvolutionStrategy();
72      var vp = new VariegationProblem();
73      vp.SetMaximization(false);
74      cmaes.Problem = vp;
75      cmaes.MaximumGenerations = 80;
76      metaSolverNode.Algorithm = cmaes;
77      orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
78      Nodes.Add(metaSolverNode);
79
80      var flpSolverNode = new OrchestratedAlgorithmNode("FlpSolver");
81      var cplexSolver = new FLPCplexSolver();
82      cplexSolver.Problem = new FacilityLocationProblem();
83      cplexSolver.MaximumRuntimeParameter.Value.Value = TimeSpan.FromSeconds(3.0);
84      flpSolverNode.Algorithm = cplexSolver;
85      orchestratorNode.FlpSolverOrchestrationPort.ConnectedPort = flpSolverNode.OrchestrationPort;
86      Nodes.Add(flpSolverNode);
87
88      var vrpSolverNode = new OrchestratedAlgorithmNode("VrpSolver");
89      var ga = new GeneticAlgorithm();
90      ga.Problem = new VehicleRoutingProblem();
91      ga.PopulationSize.Value = 100;
92      var crossover = ga.CrossoverParameter.ValidValues.OfType<MultiVRPSolutionCrossover>().Single(x => x.Name == "MultiVRPSolutionCrossover");
93      ga.CrossoverParameter.Value = crossover;
94      ga.MaximumGenerations.Value = 100;
95      var mutator = ga.MutatorParameter.ValidValues.OfType<MultiVRPSolutionManipulator>().Single(x => x.Name == "MultiVRPSolutionManipulator");
96      ga.MutatorParameter.Value = mutator;
97      var selector = ga.SelectorParameter.ValidValues.OfType<TournamentSelector>().Single();
98      ga.SelectorParameter.Value = selector;
99      vrpSolverNode.Algorithm = ga;
100      orchestratorNode.VrpSolverOrchestrationPort.ConnectedPort = vrpSolverNode.OrchestrationPort;
101      Nodes.Add(vrpSolverNode);
102
103      RegisterEvents();
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new LrpNetwork1(this, cloner);
108    }
109
110    [StorableHook(HookType.AfterDeserialization)]
111    private void AfterDeserialization() {
112      RegisterEvents();
113    }
114
115    private void RegisterEvents() {
116      MetaSolver.AlgorithmChanged += MetaSolver_AlgorithmChanged;
117      RegisterAlgorithmEvents();
118    }
119
120    private void RegisterAlgorithmEvents() {
121      var algorithm = MetaSolver.Algorithm;
122      if (algorithm == null) return;
123
124      algorithm.ExecutionStateChanged += OnExecutionStateChanged;
125      algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
126      algorithm.Prepared += OnPrepared;
127      algorithm.Started += OnStarted;
128      algorithm.Paused += OnPaused;
129      algorithm.Stopped += OnStopped;
130      algorithm.ExceptionOccurred += OnExceptionOccurred;
131    }
132
133    private void MetaSolver_AlgorithmChanged(object sender, EventArgs e) {
134      RegisterAlgorithmEvents();
135    }
136
137    #region IOptimizer Members
138    public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
139    public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
140    public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
141    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
142
143    public void Prepare() { Prepare(false); }
144    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
145    public void Start() { Orchestrator.StartAsync(); }
146    public void Pause() { Orchestrator.Pause(); }
147    public void Stop() { Orchestrator.Stop(); }
148    #endregion
149
150    private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
151    private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
152    private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
153    private void OnStarted(object sender, EventArgs e) { OnStarted(); }
154    private void OnPaused(object sender, EventArgs e) { OnPaused(); }
155    private void OnStopped(object sender, EventArgs e) { OnStopped(); }
156    private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
157
158    public event EventHandler ExecutionStateChanged;
159    private void OnExecutionStateChanged() {
160      var handler = ExecutionStateChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163
164    public event EventHandler ExecutionTimeChanged;
165    private void OnExecutionTimeChanged() {
166      var handler = ExecutionTimeChanged;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler Prepared;
171    private void OnPrepared() {
172      var handler = Prepared;
173      if (handler != null) handler(this, EventArgs.Empty);
174    }
175
176    public event EventHandler Started;
177    private void OnStarted() {
178      var handler = Started;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
181
182    public event EventHandler Paused;
183    private void OnPaused() {
184      var handler = Paused;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187
188    public event EventHandler Stopped;
189    private void OnStopped() {
190      var handler = Stopped;
191      if (handler != null) handler(this, EventArgs.Empty);
192    }
193
194    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
195    private void OnExceptionOccurred(Exception exception) {
196      var handler = ExceptionOccurred;
197      if (handler != null) handler(this, new EventArgs<Exception>(exception));
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.