[14604] | 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 System.Linq;
|
---|
| 25 | using HeuristicLab.Algorithms.CMAEvolutionStrategy;
|
---|
| 26 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Core.Networks;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Problems.FacilityLocation;
|
---|
| 33 | using HeuristicLab.Problems.FacilityLocation.CplexSolver;
|
---|
| 34 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 35 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
| 36 | using HeuristicLab.Selection;
|
---|
| 37 |
|
---|
| 38 | namespace 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(bool clearRuns) { Prepare(); }
|
---|
| 133 | public void Prepare() { Orchestrator.Prepare(); }
|
---|
| 134 | public void Start() {
|
---|
| 135 | if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
|
---|
| 136 | Orchestrator.Prepare();
|
---|
| 137 | Orchestrator.StartAsync();
|
---|
| 138 | }
|
---|
| 139 | public void Pause() { Orchestrator.Pause(); }
|
---|
| 140 | public void Stop() { Orchestrator.Stop(); }
|
---|
| 141 | #endregion
|
---|
| 142 |
|
---|
| 143 | private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
|
---|
| 144 | private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
|
---|
| 145 | private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
|
---|
| 146 | private void OnStarted(object sender, EventArgs e) { OnStarted(); }
|
---|
| 147 | private void OnPaused(object sender, EventArgs e) { OnPaused(); }
|
---|
| 148 | private void OnStopped(object sender, EventArgs e) { OnStopped(); }
|
---|
| 149 | private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
|
---|
| 150 |
|
---|
| 151 | public event EventHandler ExecutionStateChanged;
|
---|
| 152 | private void OnExecutionStateChanged() {
|
---|
| 153 | var handler = ExecutionStateChanged;
|
---|
| 154 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | public event EventHandler ExecutionTimeChanged;
|
---|
| 158 | private void OnExecutionTimeChanged() {
|
---|
| 159 | var handler = ExecutionTimeChanged;
|
---|
| 160 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | public event EventHandler Prepared;
|
---|
| 164 | private void OnPrepared() {
|
---|
| 165 | var handler = Prepared;
|
---|
| 166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | public event EventHandler Started;
|
---|
| 170 | private void OnStarted() {
|
---|
| 171 | var handler = Started;
|
---|
| 172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public event EventHandler Paused;
|
---|
| 176 | private void OnPaused() {
|
---|
| 177 | var handler = Paused;
|
---|
| 178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public event EventHandler Stopped;
|
---|
| 182 | private void OnStopped() {
|
---|
| 183 | var handler = Stopped;
|
---|
| 184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 188 | private void OnExceptionOccurred(Exception exception) {
|
---|
| 189 | var handler = ExceptionOccurred;
|
---|
| 190 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|