Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14610 was 14610, checked in by jkarder, 7 years ago

#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
File size: 7.7 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
67    private void RegisterEvents() {
68      MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
69      MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
70      MetaSolver.Algorithm.Prepared += OnPrepared;
71      MetaSolver.Algorithm.Started += OnStarted;
72      MetaSolver.Algorithm.Paused += OnPaused;
73      MetaSolver.Algorithm.Stopped += OnStopped;
74      MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
75    }
76
77    public LrpNetwork1() : base("LrpNetwork1") {
78      var orchestratorNode = new LrpOrchestratorNode1("Orchestrator");
79      Nodes.Add(orchestratorNode);
80
81      var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
82      var cmaes = new CMAEvolutionStrategy();
83      var vp = new VariegationProblem();
84      vp.SetMaximization(false);
85      cmaes.Problem = vp;
86      cmaes.MaximumGenerations = 80;
87      metaSolverNode.Algorithm = cmaes;
88      orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
89      Nodes.Add(metaSolverNode);
90
91      var flpSolverNode = new OrchestratedAlgorithmNode("FlpSolver");
92      var cplexSolver = new FLPCplexSolver();
93      cplexSolver.Problem = new FacilityLocationProblem();
94      cplexSolver.MaximumRuntimeParameter.Value.Value = TimeSpan.FromSeconds(3.0);
95      flpSolverNode.Algorithm = cplexSolver;
96      orchestratorNode.FlpSolverOrchestrationPort.ConnectedPort = flpSolverNode.OrchestrationPort;
97      Nodes.Add(flpSolverNode);
98
99      var vrpSolverNode = new OrchestratedAlgorithmNode("VrpSolver");
100      var ga = new GeneticAlgorithm();
101      ga.Problem = new VehicleRoutingProblem();
102      ga.PopulationSize.Value = 100;
103      var crossover = ga.CrossoverParameter.ValidValues.OfType<MultiVRPSolutionCrossover>().Single(x => x.Name == "MultiVRPSolutionCrossover");
104      ga.CrossoverParameter.Value = crossover;
105      ga.MaximumGenerations.Value = 100;
106      var mutator = ga.MutatorParameter.ValidValues.OfType<MultiVRPSolutionManipulator>().Single(x => x.Name == "MultiVRPSolutionManipulator");
107      ga.MutatorParameter.Value = mutator;
108      var selector = ga.SelectorParameter.ValidValues.OfType<TournamentSelector>().Single();
109      ga.SelectorParameter.Value = selector;
110      vrpSolverNode.Algorithm = ga;
111      orchestratorNode.VrpSolverOrchestrationPort.ConnectedPort = vrpSolverNode.OrchestrationPort;
112      Nodes.Add(vrpSolverNode);
113
114      RegisterEvents();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new LrpNetwork1(this, cloner);
119    }
120
121    [StorableHook(HookType.AfterDeserialization)]
122    private void AfterDeserialization() {
123      RegisterEvents();
124    }
125
126    #region IOptimizer Members
127    public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
128    public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
129    public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
130    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
131
132    public void Prepare() { Prepare(false); }
133    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
134    public void Start() { Orchestrator.StartAsync(); }
135    public void Pause() { Orchestrator.Pause(); }
136    public void Stop() { Orchestrator.Stop(); }
137    #endregion
138
139    private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
140    private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
141    private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
142    private void OnStarted(object sender, EventArgs e) { OnStarted(); }
143    private void OnPaused(object sender, EventArgs e) { OnPaused(); }
144    private void OnStopped(object sender, EventArgs e) { OnStopped(); }
145    private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
146
147    public event EventHandler ExecutionStateChanged;
148    private void OnExecutionStateChanged() {
149      var handler = ExecutionStateChanged;
150      if (handler != null) handler(this, EventArgs.Empty);
151    }
152
153    public event EventHandler ExecutionTimeChanged;
154    private void OnExecutionTimeChanged() {
155      var handler = ExecutionTimeChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158
159    public event EventHandler Prepared;
160    private void OnPrepared() {
161      var handler = Prepared;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164
165    public event EventHandler Started;
166    private void OnStarted() {
167      var handler = Started;
168      if (handler != null) handler(this, EventArgs.Empty);
169    }
170
171    public event EventHandler Paused;
172    private void OnPaused() {
173      var handler = Paused;
174      if (handler != null) handler(this, EventArgs.Empty);
175    }
176
177    public event EventHandler Stopped;
178    private void OnStopped() {
179      var handler = Stopped;
180      if (handler != null) handler(this, EventArgs.Empty);
181    }
182
183    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
184    private void OnExceptionOccurred(Exception exception) {
185      var handler = ExceptionOccurred;
186      if (handler != null) handler(this, new EventArgs<Exception>(exception));
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.